/* ============================================================
   Scan2Speech — paper-book theme
   Drop-in replacement for styles.css. Same selectors as the
   original; restyled to feel like reading a paper book.

   THEME MODEL — explicit day/night toggle:
   • Theming keys off  <html data-theme="light|dark">.
   • A tiny inline script in index.html sets that attribute before
     paint — from the saved choice, else the system preference —
     so there's no flash and the in-app toggle can override it.
   • Day reading is the default; night reading is the dark block.

   Bookmark-ribbon red accent; Spectral serif for reading, Hanken
   Grotesk for UI. Fonts + ribbon are pure CSS — no markup needed
   beyond the toggle button.
   ============================================================ */

@import url('https://fonts.googleapis.com/css2?family=Hanken+Grotesk:wght@400;500;600;700&family=Spectral:ital,wght@0,400;0,500;0,600;0,700;1,400;1,500&display=swap');

:root {
  color-scheme: light;

  /* Paper surfaces (day reading) */
  --bg: #ece1cb;      /* tan desk — page */
  --bg-2: #f3ead7;    /* inset fields */
  --card: #fbf6ec;    /* paper sheet */
  --line: #ddccae;    /* hairline rule */
  --line-2: #c8b694;  /* stronger rule */
  --fg: #2c2218;      /* warm ink text */
  --muted: #6d5f4b;

  /* Bookmark-ribbon accent + library status colors */
  --accent: #a23b2d;
  --accent-2: #87291d;
  --accent-soft: rgba(162, 59, 45, 0.10);
  --good: #4f7a4d;    /* clothbound green — audio ready */
  --warn: #bd8a3c;    /* sepia ochre — in-progress */
  --bad: #b23a2e;     /* brick red — error */
  --ink-blue: #3a567e;/* fountain-pen ink — transcribed */

  --radius: 6px;
  --radius-sm: 4px;
  --maxw: 920px;
  --shadow: 0 1px 2px rgba(58, 42, 22, 0.06), 0 10px 28px rgba(58, 42, 22, 0.10);

  --font-serif: "Spectral", Georgia, "Iowan Old Style", "Times New Roman", serif;
  --font-sans: "Hanken Grotesk", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

/* ── night reading (candle-lit sepia) — explicit or resolved-from-system ── */
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #1c1711; --bg-2: #18130d; --card: #2a231b; --line: #3c3225; --line-2: #51442f;
  --fg: #ece0cc; --muted: #a89880;
  --accent: #cc5a46; --accent-2: #b2402e; --accent-soft: rgba(204, 90, 70, 0.15);
  --good: #7bb07a; --warn: #d9a857; --bad: #e0705f; --ink-blue: #8aa6d0;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.30), 0 12px 30px rgba(0, 0, 0, 0.42);
}

* { box-sizing: border-box; }

/* The hidden attribute must always win over component display rules
 * (e.g. .cam/.player set display, which would otherwise override it). */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--fg);
  font: 1.0625rem/1.6 var(--font-sans);
  -webkit-font-smoothing: antialiased;
}

.wrap { max-width: var(--maxw); margin: 0 auto; padding: 0 16px; }
a { color: var(--accent); text-decoration-color: rgba(162, 59, 45, 0.4); }
:root[data-theme="dark"] a { text-decoration-color: rgba(204, 90, 70, 0.45); }

code {
  background: var(--bg-2);
  border: 1px solid var(--line);
  padding: 1px 6px;
  border-radius: 5px;
  font-size: 0.85em;
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

.muted { color: var(--muted); }
.small { font-size: 0.875em; }

/* ── header ── */
.app-header {
  border-bottom: 1px solid var(--line);
  background:
    repeating-linear-gradient(180deg, transparent 0 27px, rgba(120, 90, 50, 0.05) 27px 28px),
    linear-gradient(180deg, var(--card), var(--bg));
  padding: 26px 0;
}
.header-inner { position: relative; display: flex; align-items: center; gap: 14px; }
/* bookmark ribbon hanging from the header's top edge, just left of the toggle */
.bookmark-ribbon {
  position: absolute; top: -26px; right: 56px;
  width: 26px; height: 52px;
  background: var(--accent);
  clip-path: polygon(0 0, 100% 0, 100% 100%, 50% 76%, 0 100%);
  box-shadow: 0 4px 8px rgba(58, 42, 22, 0.22);
  pointer-events: none;
}
.logo { font-size: 2rem; display: flex; align-items: center; gap: 2px; filter: drop-shadow(0 1px 2px rgba(58, 42, 22, 0.2)); }
.logo-arrow { font-size: 1.05rem; color: var(--accent); margin: 0 1px; }
.app-header h1 { margin: 0; font-family: var(--font-serif); font-weight: 600; font-size: 1.6rem; letter-spacing: -0.005em; }
.tagline { margin: 3px 0 0; color: var(--muted); font-size: 0.95rem; }

/* day/night toggle button (pushed to the far right of the header row) */
.theme-toggle { margin-left: auto; align-self: center; font-size: 1.05rem; line-height: 1; }

/* ── layout ── */
main { padding-bottom: 132px; }
.card { margin-top: 18px; }
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
}
.card-head { display: flex; align-items: center; gap: 10px; margin-bottom: 6px; }
.card-head h2, #pagesSection h2 { margin: 0; font-family: var(--font-serif); font-weight: 600; font-size: 1.25rem; letter-spacing: -0.005em; }
.step {
  display: inline-grid; place-items: center;
  width: 26px; height: 26px; flex: 0 0 auto;
  border-radius: 50%;
  background: var(--accent-soft); color: var(--accent);
  font-size: 0.85rem; font-weight: 700;
}
#pagesSection { margin-top: 26px; }
#pagesSection .card-head { margin-bottom: 6px; }

/* ── forms ── */
input[type=text], input[type=password], input[type=number], select, textarea {
  background: var(--bg-2);
  color: var(--fg);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font: inherit;
  width: 100%;
  transition: border-color .15s, box-shadow .15s;
}
input::placeholder, textarea::placeholder { color: var(--muted); opacity: 0.7; }
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
button:focus-visible, .dropzone:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-top: 12px; }
.row input[type=password], .row input[type=text] { flex: 1 1 220px; }

button {
  background: var(--accent);
  color: #fff;
  border: 0;
  border-radius: var(--radius-sm);
  padding: 10px 18px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: background .15s, transform .05s, opacity .15s;
}
button:hover { background: var(--accent-2); }
button:active { transform: translateY(1px); }
button:disabled { opacity: 0.4; cursor: not-allowed; }
button.ghost { background: transparent; color: var(--fg); border: 1px solid var(--line); }
button.ghost:hover { background: var(--bg-2); border-color: var(--line-2); }
button.ghost.danger { color: var(--bad); border-color: rgba(178, 58, 46, 0.45); }
button.ghost.danger:hover { background: rgba(178, 58, 46, 0.08); }
button.icon { padding: 10px 12px; }

.status { margin: 12px 0 0; min-height: 1.2em; font-size: 0.9em; color: var(--muted); }
.status.ok { color: var(--good); }
.status.err { color: var(--bad); }

/* ── settings ── */
details#settings { margin-top: 16px; border-top: 1px solid var(--line); padding-top: 12px; }
summary { cursor: pointer; color: var(--accent); user-select: none; font-size: 0.92rem; font-weight: 600; }
.settings-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-top: 16px; }
.settings-grid label { display: flex; flex-direction: column; gap: 6px; font-size: 0.9em; color: var(--muted); }
.settings-grid label.full { grid-column: 1 / -1; }
.settings-grid .hint { font-size: 0.8em; color: var(--muted); opacity: 0.85; }
#ttsModelCustom { margin-top: 6px; }

/* ── dropzone ── */
.dropzone {
  border: 1.5px dashed var(--line-2);
  border-radius: var(--radius);
  padding: 30px 16px;
  text-align: center;
  cursor: pointer;
  transition: border-color .15s, background .15s, transform .1s;
  background: linear-gradient(180deg, rgba(162, 59, 45, 0.04), transparent);
}
.dropzone:hover, .dropzone.drag {
  border-color: var(--accent);
  background: var(--accent-soft);
}
.dropzone.drag { transform: scale(1.005); }
.dz-icon { font-size: 2.2rem; }
.dz-title { margin: 8px 0 4px; font-size: 1.02rem; }
.dz-inner p { margin: 4px 0; }

/* ── camera ── */
.cam-open {
  width: 100%;
  padding: 14px;
  font-size: 1.02rem;
  margin-bottom: 12px;
  box-shadow: 0 4px 14px rgba(162, 59, 45, 0.30);
}
.check { flex-direction: row !important; align-items: center; gap: 8px !important; color: var(--fg) !important; }
.check input { width: auto; }

.cam {
  position: fixed; inset: 0; z-index: 100;
  background: #000;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
}
.cam-video { width: 100%; height: 100%; object-fit: contain; }
.cam-error {
  position: absolute; top: 16px; left: 16px; right: 16px;
  background: rgba(40, 20, 24, 0.92); color: #fff;
  border: 1px solid var(--bad); border-radius: 12px; padding: 12px 14px;
  font-size: 0.9rem;
}
.cam-controls {
  position: absolute; left: 0; right: 0; bottom: 0;
  display: flex; align-items: center; justify-content: center; gap: 28px;
  padding: 20px 20px calc(28px + env(safe-area-inset-bottom));
  background: linear-gradient(180deg, transparent, rgba(0, 0, 0, 0.55));
}
.cam-done {
  background: rgba(255, 255, 255, 0.16); color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.3);
  padding: 10px 18px; border-radius: 999px;
}
.cam-done:hover { background: rgba(255, 255, 255, 0.26); }
.cam-shutter {
  width: 74px; height: 74px; border-radius: 50%; padding: 0;
  background: #fff; border: 4px solid rgba(255, 255, 255, 0.55);
  box-shadow: 0 0 0 2px rgba(0, 0, 0, 0.3);
  transition: transform .1s;
}
.cam-shutter:hover { background: #fff; }
.cam-shutter:active, .cam-shutter.flash { transform: scale(0.9); }
.cam-count {
  min-width: 40px; text-align: center; color: #fff; font-weight: 700; font-size: 1.1rem;
  background: rgba(255, 255, 255, 0.16); border-radius: 999px; padding: 6px 10px;
}

/* ── batch bar ── */
.batch-bar { margin-top: 16px; }
.progress-track {
  height: 6px;
  border-radius: 999px;
  background: var(--bg-2);
  overflow: hidden;
  border: 1px solid var(--line);
}
.progress-fill {
  height: 100%;
  width: 0%;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--warn), var(--accent));
  transition: width .35s ease;
}
.batch-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-top: 10px; }
.batch-row .status { margin: 0; flex: 1 1 auto; }
.batch-btns { display: flex; gap: 8px; flex-wrap: wrap; }
.batch-btns button { padding: 8px 14px; font-size: 0.92em; }

/* ── pages ── */
.pages { list-style: none; margin: 16px 0 0; padding: 0; display: flex; flex-direction: column; gap: 14px; }
.page {
  display: grid;
  grid-template-columns: 92px 1fr;
  gap: 16px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  transition: border-color .2s, box-shadow .2s;
}
.page.playing { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent), 0 0 0 5px var(--accent-soft); }
.page .thumb {
  width: 92px; height: 124px;
  object-fit: cover;
  border-radius: var(--radius-sm);
  background: var(--bg-2);
  border: 1px solid var(--line);
  display: block;
}
.thumb-link {
  position: relative;
  display: block;
  width: 92px; height: 124px;
  border-radius: var(--radius-sm);
  overflow: hidden;
  cursor: grab; /* the thumbnail doubles as the drag-to-reorder handle */
  touch-action: none; /* a touch on the thumb drags the card, never scrolls */
  -webkit-touch-callout: none; /* no iOS long-press link preview mid-drag */
  user-select: none;
  -webkit-user-select: none;
}
.thumb-link:active { cursor: grabbing; }
.thumb-grip {
  position: absolute; left: 4px; top: 4px;
  width: 20px; height: 20px;
  display: grid; place-items: center;
  font-size: 0.72rem; color: #fff;
  background: rgba(44, 34, 24, 0.55);
  border-radius: 6px;
  pointer-events: none;
}
body.drag-lock { user-select: none; -webkit-user-select: none; }
.thumb-link .thumb { width: 100%; height: 100%; border-radius: 0; transition: transform .2s, filter .2s; }
.thumb-link:hover .thumb, .thumb-link:focus-visible .thumb { transform: scale(1.06); filter: brightness(1.05); }
.thumb-zoom {
  position: absolute; right: 4px; bottom: 4px;
  width: 20px; height: 20px;
  display: grid; place-items: center;
  font-size: 0.7rem; color: #fff;
  background: rgba(44, 34, 24, 0.6);
  border-radius: 6px;
  opacity: 0; transition: opacity .15s;
  pointer-events: none;
}
.thumb-link:hover .thumb-zoom, .thumb-link:focus-visible .thumb-zoom { opacity: 1; }
.page-body { min-width: 0; }
.page-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.page.dragging { opacity: 0.55; border-style: dashed; border-color: var(--accent); }

/* ── ⋯ overflow menu (page cards + batch bar) ── */
.menu-wrap { position: relative; margin-left: auto; }
.menu-btn { font-weight: 700; letter-spacing: 1px; }
.menu {
  position: absolute; right: 0; top: calc(100% + 6px);
  min-width: 200px;
  background: var(--card);
  border: 1px solid var(--line-2);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 6px;
  z-index: 30;
  display: flex; flex-direction: column; gap: 2px;
}
.menu button {
  background: transparent;
  color: var(--fg);
  border: 0;
  text-align: left;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  font-size: 0.92em;
  font-weight: 500;
  white-space: nowrap;
}
.menu button:hover:not(:disabled) { background: var(--bg-2); }
.menu button:disabled { opacity: 0.4; }
.menu button.danger { color: var(--bad); }
.menu button.danger:hover:not(:disabled) { background: rgba(178, 58, 46, 0.08); }
.menu hr { border: 0; border-top: 1px solid var(--line); margin: 4px 2px; }
.page-num { font-weight: 700; font-size: 0.98rem; }

.badge {
  font-size: 0.74em;
  font-weight: 600;
  letter-spacing: 0.02em;
  padding: 3px 10px;
  border-radius: 999px;
  border: 1px solid var(--line);
  color: var(--muted);
  white-space: nowrap;
  display: inline-flex; align-items: center; gap: 5px;
}
.badge.b-transcribing, .badge.b-generating { color: var(--warn); border-color: rgba(189, 138, 60, 0.45); background: rgba(189, 138, 60, 0.13); }
.badge.b-transcribed { color: var(--ink-blue); border-color: rgba(58, 86, 126, 0.40); background: rgba(58, 86, 126, 0.12); }
.badge.b-audio { color: var(--good); border-color: rgba(79, 122, 77, 0.42); background: rgba(79, 122, 77, 0.13); }
.badge.b-error { color: var(--bad); border-color: rgba(178, 58, 46, 0.42); background: rgba(178, 58, 46, 0.10); }
:root[data-theme="dark"] .badge.b-transcribing, :root[data-theme="dark"] .badge.b-generating { background: rgba(217, 168, 87, 0.14); }
:root[data-theme="dark"] .badge.b-transcribed { background: rgba(138, 166, 208, 0.14); }
:root[data-theme="dark"] .badge.b-audio { background: rgba(123, 176, 122, 0.14); }
:root[data-theme="dark"] .badge.b-error { background: rgba(224, 112, 95, 0.14); }

/* transcribed pages read in serif, like a book */
.page textarea { margin-top: 12px; min-height: 110px; resize: vertical; font-family: var(--font-serif); font-size: 1.0625rem; line-height: 1.75; padding: 12px 14px; }
.page-primary { margin-top: 12px; }
.page-primary button { padding: 10px 20px; font-size: 0.95em; }
.page-err { color: var(--bad); font-size: 0.85em; margin-top: 10px; background: rgba(178, 58, 46, 0.07); border: 1px solid rgba(178, 58, 46, 0.42); border-radius: var(--radius-sm); padding: 8px 10px; }
.spinner {
  width: 12px; height: 12px; border-radius: 50%;
  border: 2px solid rgba(120, 90, 50, 0.2); border-top-color: currentColor;
  display: inline-block; animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── player ── */
.player {
  position: fixed; left: 0; right: 0; bottom: 0;
  background: rgba(251, 246, 236, 0.90);
  border-top: 1px solid var(--line);
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
  padding: 12px 0 calc(12px + env(safe-area-inset-bottom));
  z-index: 50;
  box-shadow: 0 -6px 22px rgba(58, 42, 22, 0.14);
}
:root[data-theme="dark"] .player { background: rgba(42, 35, 27, 0.92); }
.player-inner { display: flex; align-items: center; gap: 14px; }
.big-play {
  width: 56px; height: 56px; border-radius: 50%;
  font-size: 1.25rem; padding: 0; flex: 0 0 auto;
  box-shadow: 0 4px 14px rgba(162, 59, 45, 0.3);
}
.player-mid { flex: 1 1 auto; min-width: 0; }
.p-now { font-size: 0.86em; color: var(--fg); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 500; }
.seek { width: 100%; margin: 6px 0 0; accent-color: var(--accent); height: 4px; }
.p-times { display: flex; justify-content: space-between; font-size: 0.72em; color: var(--muted); margin-top: 2px; }
.player-ctrls { display: flex; align-items: center; gap: 6px; flex: 0 0 auto; }
.player-ctrls .icon { padding: 9px 11px; }
.speed { width: auto; padding: 8px 8px; }

/* ── toast ── */
.toast {
  position: fixed; left: 50%; bottom: 104px; transform: translateX(-50%);
  background: #fffdf6; color: var(--fg);
  border: 1px solid var(--line-2);
  padding: 11px 18px; border-radius: 10px;
  max-width: 90%; z-index: 60; box-shadow: 0 10px 30px rgba(40, 28, 14, 0.22);
  font-size: 0.92em;
  animation: toast-in .2s ease;
}
:root[data-theme="dark"] .toast { background: #322a20; }
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, 8px); } }

.footer-space { height: 12px; }

/* ── responsive ── */
@media (max-width: 560px) {
  .settings-grid { grid-template-columns: 1fr; }
  .page { grid-template-columns: 60px 1fr; gap: 12px; padding: 13px; }
  .page .thumb, .thumb-link { width: 60px; height: 82px; }
  .page-primary button { width: 100%; }
  .batch-row .status { flex-basis: 100%; }
  .player-ctrls .icon { padding: 9px; }
  .speed { padding: 8px 4px; }
  .app-header h1 { font-size: 1.4rem; }
  .bookmark-ribbon { right: 52px; }
}
