:root {
  --bg: #f3f4f6;
  --surface: #ffffff;
  --text: #111827;
  --muted: #6b7280;
  --border: #d1d5db;
  --primary: #2563eb;
  --primary-text: #ffffff;
  --danger: #dc2626;
  --topbar: #1f2937;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111827;
    --surface: #1f2937;
    --text: #f9fafb;
    --muted: #9ca3af;
    --border: #374151;
    --primary: #3b82f6;
    --topbar: #030712;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

button, input { font: inherit; color: inherit; }

button {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 8px;
  padding: 10px 14px;
  cursor: pointer;
}
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
button.danger { color: var(--danger); }
button.link { background: none; border: none; color: #e5e7eb; padding: 8px; }

input[type=text], input:not([type]), input[type=password] {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
}

.hint { color: var(--muted); text-align: center; margin: 12px 0; }
.error { color: var(--danger); margin: 0; }

/* ---------- Login ---------- */
.login-body { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 360px;
  background: var(--surface);
  padding: 24px; border-radius: 12px;
  display: grid; gap: 14px;
  box-shadow: 0 4px 24px rgb(0 0 0 / .08);
}
.login-card h1 { margin: 0 0 4px; font-size: 1.4rem; }
.login-card label { display: grid; gap: 6px; color: var(--muted); font-size: .9rem; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; justify-content: space-between; align-items: center;
  background: var(--topbar);
  padding: 6px 8px;
  padding-top: max(6px, env(safe-area-inset-top));
}
.topbar form { margin: 0; }
.tabs { display: flex; gap: 4px; }
.tab { background: none; border: none; color: #9ca3af; padding: 8px 14px; }
.tab.active { color: #fff; background: rgb(255 255 255 / .12); }

.view { max-width: 720px; margin: 0 auto; padding: 12px 16px 32px; }

/* ---------- Camera ---------- */
.camera {
  position: relative;
  background: #000;
  border-radius: 12px;
  overflow: hidden;
  aspect-ratio: 3 / 4;
  max-height: 60vh;
  margin: 0 auto;
}
.camera video { width: 100%; height: 100%; object-fit: cover; display: block; }
.camera-message {
  position: absolute; inset: 0; margin: 0;
  display: grid; place-items: center; text-align: center;
  padding: 24px; color: #e5e7eb;
}
.camera-controls {
  position: absolute; left: 0; right: 0; bottom: 0;
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  padding: 14px;
  background: linear-gradient(transparent, rgb(0 0 0 / .6));
}
.file-button {
  justify-self: start;
  color: #fff; background: rgb(255 255 255 / .15);
  padding: 8px 12px; border-radius: 8px; font-size: .9rem; cursor: pointer;
}
.shutter {
  width: 68px; height: 68px; border-radius: 50%;
  background: #fff; border: 5px solid rgb(255 255 255 / .45);
  background-clip: padding-box; padding: 0;
}
.shutter:active { transform: scale(.94); }
.cv-status { justify-self: end; color: #d1d5db; font-size: .8rem; }

/* ---------- Draft ---------- */
.draft { margin-top: 16px; }
.draft-header { display: flex; gap: 8px; }
.draft-header input { flex: 1; min-width: 0; }
.mode { display: flex; }
.mode-btn { border-radius: 0; padding: 10px; }
.mode-btn:first-child { border-radius: 8px 0 0 8px; }
.mode-btn:last-child { border-radius: 0 8px 8px 0; border-left: none; }
.mode-btn.active { background: var(--primary); border-color: var(--primary); color: #fff; }

.pages {
  list-style: none; padding: 0; margin: 14px 0 0;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)); gap: 10px;
}
.page {
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  padding: 6px; display: grid; gap: 6px;
}
.page img, .page canvas { width: 100%; aspect-ratio: 1 / 1.414; object-fit: contain; background: #e5e7eb; border-radius: 4px; }
.bw .page img { filter: grayscale(1) contrast(1.15); }
.page-number { font-size: .8rem; color: var(--muted); text-align: center; }
.page-actions { display: grid; grid-template-columns: repeat(4, 1fr); gap: 2px; }
.page-actions button { padding: 4px 0; font-size: .85rem; }

.draft-actions { display: flex; justify-content: space-between; gap: 8px; margin-top: 16px; }
.draft-actions .primary { flex: 1; }

/* ---------- Documents ---------- */
.documents { list-style: none; padding: 0; margin: 0; display: grid; gap: 8px; }
.document {
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 12px; display: flex; gap: 10px; align-items: center;
}
.document-info { flex: 1; min-width: 0; }
.document-title { font-weight: 600; overflow-wrap: anywhere; }
.document-meta { color: var(--muted); font-size: .85rem; }
.document a { color: var(--primary); text-decoration: none; padding: 8px; }
.document-actions { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; justify-content: flex-end; }

/* ---------- Document editor ---------- */
/* Starts below the top bar (its height is measured in app.js), so the tabs stay usable. */
.doc-editor {
  position: fixed; inset: 0; top: var(--topbar-height, 0px); z-index: 10;
  background: var(--bg);
  display: grid; grid-template-rows: auto 1fr auto;
  padding-bottom: env(safe-area-inset-bottom);
}
.doc-editor-header { padding: 12px 16px 0; max-width: 720px; width: 100%; margin: 0 auto; }
.doc-editor-title { font-weight: 600; overflow-wrap: anywhere; }
.doc-editor-header .hint { margin: 4px 0 0; text-align: left; font-size: .85rem; }
.doc-editor-pages {
  overflow-y: auto; min-height: 0; align-content: start;
  grid-template-columns: 1fr; gap: 16px;
  padding: 0 16px 16px; max-width: 720px; width: 100%; margin: 12px auto 0;
}
.doc-editor .editor-actions { max-width: 720px; width: 100%; margin: 0 auto; border-top: 1px solid var(--border); }
.doc-editor-pages .page { padding: 8px; gap: 8px; }
.doc-editor-pages .page canvas { aspect-ratio: auto; height: auto; }
.doc-editor-pages .page-actions { grid-template-columns: repeat(5, 1fr); gap: 4px; }
.doc-editor-pages .page-actions button { padding: 10px 0; font-size: 1.1rem; }

/* ---------- Editor ---------- */
.editor {
  position: fixed; inset: 0; z-index: 20;
  background: #000;
  display: grid; grid-template-rows: 1fr auto auto;
  padding-bottom: env(safe-area-inset-bottom);
}
.editor-stage { position: relative; display: grid; place-items: center; overflow: hidden; padding: 16px; }
.editor-stage canvas { max-width: 100%; max-height: 100%; touch-action: none; }
.editor-hint { color: #9ca3af; margin: 6px; }
.editor-actions { display: flex; gap: 8px; padding: 12px 16px; }
.editor-actions .primary { flex: 1; }

/* ---------- Busy / toast ---------- */
.busy {
  position: fixed; inset: 0; z-index: 30;
  background: rgb(0 0 0 / .55); color: #fff;
  display: grid; place-content: center; justify-items: center; gap: 12px;
}
.spinner {
  width: 40px; height: 40px; border-radius: 50%;
  border: 4px solid rgb(255 255 255 / .3); border-top-color: #fff;
  animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 40;
  background: #111827; color: #fff; padding: 10px 16px; border-radius: 8px;
  max-width: calc(100% - 32px);
  box-shadow: 0 4px 16px rgb(0 0 0 / .3);
}
.toast.error { background: var(--danger); }

[hidden] { display: none !important; }
