:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e2e8f0;
  --text: #0f172a;
  --text-muted: #64748b;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-soft: #eff6ff;
  --accent: #0ea5e9;
  --success: #16a34a;
  --success-soft: #dcfce7;
  --warning: #d97706;
  --warning-soft: #fef3c7;
  --danger: #dc2626;
  --danger-soft: #fee2e2;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.06), 0 4px 12px rgba(15, 23, 42, 0.04);
  --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  padding: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

button, input, select, textarea {
  font-family: inherit;
  font-size: 0.92rem;
}

button {
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 0.55rem 0.9rem;
  min-height: 40px;
  font-weight: 500;
  transition: all 0.15s ease;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  touch-action: manipulation;
}
button:hover:not(:disabled) { background: var(--surface-2); border-color: #cbd5e1; }
button:disabled { opacity: 0.5; cursor: not-allowed; }
button.btn-primary {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}
button.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }
button.btn-danger {
  background: var(--danger-soft);
  color: var(--danger);
  border-color: #fecaca;
}
button.btn-success {
  background: var(--success);
  color: #fff;
  border-color: var(--success);
}
button.btn-sm { padding: 0.4rem 0.7rem; font-size: 0.82rem; min-height: 36px; }
button.active-tool {
  background: var(--primary-soft);
  color: var(--primary);
  border-color: var(--primary);
}

input[type="text"], input[type="password"], input[type="date"], input[type="number"], select, textarea {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.52rem 0.75rem;
  background: var(--surface);
  color: var(--text);
  width: 100%;
  font-size: 16px;
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid rgba(37, 99, 235, 0.25);
  border-color: var(--primary);
}

/* Header */
.app-header {
  position: sticky;
  top: 0;
  z-index: 40;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0.6rem 1.15rem;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}
.brand {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  font-weight: 700;
  font-size: 1rem;
}
.brand-icon {
  width: 34px;
  height: 34px;
  border-radius: 9px;
  background: linear-gradient(135deg, var(--primary), var(--accent));
  color: #fff;
  display: grid;
  place-items: center;
  font-size: 1.1rem;
  flex-shrink: 0;
}
.header-badges {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  flex-wrap: wrap;
}
.pill {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.25rem 0.65rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text-muted);
}
.pill.online { background: var(--success-soft); color: var(--success); border-color: #bbf7d0; }
.pill.warn { background: var(--warning-soft); color: var(--warning); border-color: #fde68a; }
.pill.danger { background: var(--danger-soft); color: var(--danger); border-color: #fecaca; }

/* Nav Tabs (Desktop top bar, Mobile fixed bottom bar) */
.main-nav {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 1.25rem;
  display: flex;
  gap: 0.25rem;
  overflow-x: auto;
}
.nav-tab {
  border: none;
  background: transparent;
  border-bottom: 2px solid transparent;
  border-radius: 0;
  padding: 0.75rem 1rem;
  color: var(--text-muted);
  font-weight: 600;
  white-space: nowrap;
}
.nav-tab:hover { color: var(--text); background: var(--surface-2); }
.nav-tab.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
  background: transparent;
}

/* Layout */
.app-container {
  max-width: 1380px;
  margin: 0 auto;
  padding: 1.15rem;
}
.view-panel { display: none; }
.view-panel.active { display: block; }

.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1rem; }
.grid-3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 1rem; }
.grid-4 { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 1rem; }
.editor-layout {
  display: grid;
  grid-template-columns: 290px minmax(0, 1fr) 315px;
  gap: 1rem;
  align-items: start;
}
.scan-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  gap: 1rem;
  align-items: start;
}

@media (max-width: 1080px) {
  .editor-layout { grid-template-columns: 1fr; }
  .scan-layout { grid-template-columns: 1fr; }
  .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 768px) {
  body { padding-bottom: 72px; }
  .app-header { padding: 0.5rem 0.85rem; }
  .brand-sub, #backupBadge { display: none; }
  .main-nav {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 50;
    border-bottom: none;
    border-top: 1px solid var(--border);
    padding: 0.25rem 0.2rem;
    justify-content: space-around;
    box-shadow: 0 -4px 16px rgba(15, 23, 42, 0.08);
    background: rgba(255, 255, 255, 0.96);
    backdrop-filter: blur(8px);
  }
  .nav-tab {
    flex: 1;
    flex-direction: column;
    gap: 0.15rem;
    padding: 0.4rem 0.2rem;
    font-size: 0.7rem;
    border-bottom: none;
    border-radius: var(--radius-sm);
    min-height: 52px;
  }
  .nav-tab.active {
    background: var(--primary-soft);
    color: var(--primary);
  }
  .nav-tab .tab-full { display: none; }
  .nav-tab .tab-short { display: inline; }
  .grid-2, .grid-3 { grid-template-columns: 1fr; }
  .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.65rem; }
  .app-container { padding: 0.75rem; }
  .stage-box { min-height: 230px !important; }
  .toast-container { bottom: 76px !important; left: 0.75rem; right: 0.75rem; max-width: none !important; }
  button { min-height: 44px; }
  button.btn-sm { min-height: 38px; }
}

@media (min-width: 769px) {
  .nav-tab .tab-short { display: none; }
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  box-shadow: var(--shadow);
}
.card-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.85rem;
}
.card-title {
  margin: 0;
  font-size: 0.98rem;
  font-weight: 700;
}
.muted { color: var(--text-muted); font-size: 0.84rem; }

/* Widgets & Quick Cards */
.widget-card {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.4rem;
  padding: 0.9rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  box-shadow: var(--shadow);
  transition: transform 0.15s ease, border-color 0.15s ease;
  text-align: left;
  width: 100%;
}
.widget-card:hover { transform: translateY(-2px); border-color: var(--primary); }
.widget-icon {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background: var(--primary-soft);
  color: var(--primary);
  display: grid;
  place-items: center;
  font-size: 1.15rem;
}

/* Stage / Canvas */
.stage-box {
  background: #0f172a;
  border-radius: var(--radius);
  min-height: 320px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
  padding: 0.65rem;
}
.stage-box canvas, .stage-box video {
  max-width: 100%;
  max-height: 520px;
  border-radius: 6px;
  display: block;
}
#liveOverlayCanvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  display: none;
}
#editorCanvas {
  touch-action: pan-y;
  cursor: default;
}
#editorCanvas.interactive-lock {
  touch-action: none;
  cursor: crosshair;
}
.crop-overlay-hint {
  position: absolute;
  bottom: 10px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(15, 23, 42, 0.85);
  color: #fff;
  padding: 0.3rem 0.75rem;
  border-radius: 999px;
  font-size: 0.74rem;
  pointer-events: none;
  text-align: center;
  max-width: 92%;
}
.auto-status-pill {
  position: absolute;
  top: 10px;
  left: 10px;
  z-index: 5;
  display: none;
}

/* Thumbnails & Batch Tray */
.thumb-list {
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
  max-height: 400px;
  overflow-y: auto;
}
.thumb-item {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.45rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  cursor: pointer;
}
.thumb-item.active { border-color: var(--primary); background: var(--primary-soft); }
.thumb-item img {
  width: 44px;
  height: 58px;
  object-fit: cover;
  border-radius: 4px;
  border: 1px solid var(--border);
  background: #fff;
}
.thumb-meta { flex: 1; min-width: 0; }

/* Controls */
.control-group { margin-bottom: 0.85rem; }
.control-group label {
  display: flex;
  justify-content: space-between;
  font-size: 0.82rem;
  font-weight: 600;
  margin-bottom: 0.3rem;
}
input[type="range"] { width: 100%; accent-color: var(--primary); height: 28px; }
.btn-row { display: flex; flex-wrap: wrap; gap: 0.45rem; }
.batch-bar {
  background: var(--primary-soft);
  border: 1px solid #bfdbfe;
  border-radius: var(--radius-sm);
  padding: 0.65rem;
  margin-bottom: 0.85rem;
}

/* Dropzone */
.dropzone {
  border: 2px dashed #cbd5e1;
  border-radius: var(--radius);
  padding: 1.1rem;
  text-align: center;
  background: var(--surface-2);
  transition: border-color 0.15s ease, background 0.15s ease;
}
.dropzone.dragover { border-color: var(--primary); background: var(--primary-soft); }

/* Library Cards */
.doc-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 1rem;
}
.doc-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow);
}
.doc-preview {
  height: 150px;
  background: #e2e8f0;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}
.doc-preview img { width: 100%; height: 100%; object-fit: cover; }
.doc-body { padding: 0.85rem; flex: 1; display: flex; flex-direction: column; gap: 0.4rem; }
.doc-title { font-weight: 700; font-size: 0.92rem; word-break: break-word; }

/* Modal & Toast */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.55);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 60;
  padding: 1rem;
}
.modal-backdrop.open { display: flex; }
.modal-box {
  background: var(--surface);
  border-radius: var(--radius);
  max-width: 760px;
  width: 100%;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 20px 50px rgba(15, 23, 42, 0.25);
}
.modal-head {
  padding: 0.9rem 1.15rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.modal-body { padding: 1.15rem; overflow-y: auto; }

.toast-container {
  position: fixed;
  bottom: 1rem;
  right: 1rem;
  z-index: 80;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  max-width: 340px;
}
.toast {
  background: #0f172a;
  color: #fff;
  padding: 0.7rem 0.95rem;
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  box-shadow: 0 8px 24px rgba(15, 23, 42, 0.22);
  animation: slideIn 0.2s ease;
}
.toast.success { border-left: 4px solid var(--success); }
.toast.warn { border-left: 4px solid var(--warning); }
.toast.error { border-left: 4px solid var(--danger); }
@keyframes slideIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Signature pad */
.sig-pad {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: #fff;
  touch-action: none;
  cursor: crosshair;
  width: 100%;
  height: 130px;
}

/* Print */
@media print {
  .app-header, .main-nav, .no-print { display: none !important; }
  body { background: #fff; padding-bottom: 0; }
  .print-only-sheet { display: block !important; }
}
.print-only-sheet { display: none; }
