:root {
  --bg: #f4f1ea;
  --panel: #fffdf8;
  --ink: #1d1b18;
  --muted: #6d675d;
  --line: #ddd6c8;
  --accent: #c8322a;
  --accent-ink: #fff;
  --hl: #ffe066;
  --bar-h: 52px;
  color-scheme: light;
}
/* dark: follows the system unless the Dark mode setting pins <html data-theme> */
:root[data-theme="dark"] {
  --bg: #17181b; --panel: #222327; --ink: #ecebe7; --muted: #a29e95;
  --line: #3a3b40; --accent: #e5534b; --hl: #ffd43b;
  color-scheme: dark;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #17181b; --panel: #222327; --ink: #ecebe7; --muted: #a29e95;
    --line: #3a3b40; --accent: #e5534b; --hl: #ffd43b;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--ink);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
}
a { color: inherit; }

.btn {
  font: inherit; border: 1px solid var(--line); background: var(--panel); color: var(--ink);
  height: 34px; min-width: 34px; padding: 0 12px; border-radius: 8px; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; text-decoration: none;
}
.btn:hover:not(:disabled) { border-color: var(--muted); }
.btn:disabled { opacity: .4; cursor: default; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.ghost { background: transparent; border-color: transparent; font-size: 18px; padding: 0 8px; }
.btn[aria-pressed="true"] { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.btn:focus-visible, .play:focus-visible { outline: 3px solid var(--hl); outline-offset: 2px; }

/* ---------------- library */
.lib-head { max-width: 1100px; margin: 0 auto; padding: 28px 16px 8px; display: grid; grid-template-columns: 1fr auto; align-items: start; }
.lib-head h1 { margin: 0; font-size: 28px; }
.lib-head p { margin: 4px 0 0; color: var(--muted); grid-column: 1; }
.lib-head .btn { grid-row: 1; grid-column: 2; }
.shelf { list-style: none; margin: 0 auto; padding: 16px; max-width: 1100px; display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 20px; }
.card { display: flex; flex-direction: column; text-decoration: none; gap: 6px; }
.card img { width: 100%; height: 240px; object-fit: contain; background: var(--panel); border: 1px solid var(--line); border-radius: 6px; }
.card:hover img { border-color: var(--muted); }
.card-title { font-weight: 600; }
.card-meta { color: var(--muted); font-size: 13px; }
.error { padding: 24px; }

/* ---------------- reader */
.bar {
  position: sticky; top: 0; z-index: 5; height: var(--bar-h);
  display: flex; align-items: center; gap: 6px; padding: 0 10px;
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.bar .title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.bar .spacer { flex: 1; }
.bar .pageno { min-width: 64px; text-align: center; font-variant-numeric: tabular-nums; color: var(--muted); }

.stage { display: flex; justify-content: center; padding: 12px; }
.page {
  position: relative;
  width: min(100%, calc((100vh - var(--bar-h) - 24px) * var(--ar)));
  aspect-ratio: var(--ar);
  box-shadow: 0 2px 14px rgba(0,0,0,.18);
  user-select: none;
}
.page > img { display: block; width: 100%; height: 100%; }

.block { position: absolute; display: grid; place-items: center; }
.block .text {
  position: absolute; inset: 0; display: none;
  font-family: "Century Schoolbook", "TeX Gyre Schola", "C059", Georgia, serif;
  line-height: 1.2; color: #111; white-space: pre; overflow: visible;
  background: rgba(255,255,255,.82); border-radius: 4px; padding: 0 .2em;
}
.revealed .block .text { display: block; }
.block .text span.hl { background: var(--hl); border-radius: 2px; box-shadow: 0 0 0 1px var(--hl); }

.play {
  position: relative; z-index: 1;
  width: clamp(36px, 4.2vmin, 64px); aspect-ratio: 1; border-radius: 50%;
  border: 3px solid #fff; background: var(--accent); cursor: pointer;
  box-shadow: 0 2px 10px rgba(0,0,0,.35); display: grid; place-items: center;
  transition: transform .12s;
}
.play:hover { transform: scale(1.08); }
.play .icon { width: 0; height: 0; border-style: solid; border-width: 9px 0 9px 15px; border-color: transparent transparent transparent #fff; margin-left: 4px; }
.block.playing .play { animation: pulse 1.1s ease-in-out infinite; }
.block.playing .play .icon { width: 14px; height: 14px; border: 0; background: #fff; margin: 0; border-radius: 2px; }
@keyframes pulse { 50% { box-shadow: 0 0 0 10px rgba(200,50,42,.25), 0 2px 10px rgba(0,0,0,.35); } }

/* when revealed, the button moves to the block's top-left corner so the text is readable */
.revealed .block .play { position: absolute; left: 0; top: 0; transform: translate(-60%, -60%) scale(.7); }
.revealed .block .play:hover { transform: translate(-60%, -60%) scale(.8); }
@media (prefers-reduced-motion: reduce) { .block.playing .play { animation: none; } }

.sent { max-width: 900px; margin: 0 auto; padding: 0 16px 24px; color: var(--muted); }
.sent summary { cursor: pointer; }
.sent code { display: block; margin: 6px 0; white-space: pre-wrap; word-break: break-word; font-size: 13px; color: var(--ink); }

/* ---------------- settings drawer */
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; z-index: 10; width: min(380px, 100vw);
  background: var(--panel); border-left: 1px solid var(--line); box-shadow: -6px 0 24px rgba(0,0,0,.15);
  transform: translateX(105%); transition: transform .18s ease; overflow-y: auto; padding: 0 16px 24px;
}
.drawer-open .drawer { transform: none; }
.drawer-head { display: flex; align-items: center; justify-content: space-between; position: sticky; top: 0; background: var(--panel); padding: 12px 0 8px; }
.drawer-head h2 { margin: 0; font-size: 18px; }
.drawer-actions { display: flex; gap: 8px; margin-bottom: 8px; }
.drawer fieldset { border: 0; border-top: 1px solid var(--line); margin: 12px 0 0; padding: 8px 0 0; }
.drawer legend { font-weight: 600; padding: 0 6px 0 0; }
.field { display: grid; grid-template-columns: 1fr auto 3.3em; gap: 2px 8px; align-items: center; margin: 7px 0; }
.field[hidden] { display: none; }
.field-name { font-size: 14px; white-space: nowrap; }
.field-control { display: inline-flex; align-items: center; gap: 8px; justify-self: end; }
.field .unit { color: var(--muted); font-size: 13px; }
.field input[type=number] {
  width: 76px; font: inherit; font-size: 14px; padding: 4px 6px; text-align: right; font-variant-numeric: tabular-nums;
  background: var(--bg); color: var(--ink); border: 1px solid var(--line); border-radius: 6px;
}
.field input[type=number]:focus { outline: 2px solid var(--accent); outline-offset: 0; }
.field input:disabled { opacity: .45; }
.field select { font: inherit; font-size: 14px; padding: 4px; max-width: 200px; background: var(--bg); color: var(--ink); border: 1px solid var(--line); border-radius: 6px; }
.field small { grid-column: 1 / -1; color: var(--muted); font-size: 12px; }
.field-checkbox { cursor: pointer; }
.field input[type=checkbox] { width: 18px; height: 18px; margin: 0; accent-color: var(--accent); cursor: pointer; }
.field .same { display: inline-flex; align-items: center; gap: 5px; font-size: 13px; color: var(--muted); cursor: pointer; }

@media (max-width: 640px) {
  .bar .title { display: none; }
  .bar { gap: 3px; padding: 0 4px; }
  .btn { padding: 0 8px; }
}

/* Additions for the standalone (upload + OCR + edit) reader, layered on web/style.css */
:root { --edit: #2f6fde; --ok: #2e8b57; --warn: #b7791f; }
:root[data-theme="dark"] { --edit: #6ea0ff; --ok: #5cc98a; --warn: #e0a84a; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) { --edit: #6ea0ff; --ok: #5cc98a; --warn: #e0a84a; } }

/* library */
.card { position: relative; }
.card-link { display: flex; flex-direction: column; gap: 6px; text-decoration: none; }
.thumb { height: 240px; display: grid; place-items: center; background: var(--panel); border: 1px solid var(--line); border-radius: 6px; overflow: hidden; }
.thumb img { max-width: 100%; max-height: 100%; height: auto; border: 0; border-radius: 0; object-fit: contain; }
.card-link:hover .thumb { border-color: var(--muted); }
.card .bar-progress { display: none; height: 4px; border-radius: 2px; background: linear-gradient(90deg, var(--accent) var(--progress, 0%), var(--line) 0); }
.card.busy .bar-progress { display: block; }
.card-del {
  position: absolute; top: 6px; right: 6px; width: 28px; height: 28px; border-radius: 50%;
  border: 1px solid var(--line); background: var(--panel); color: var(--muted); cursor: pointer; opacity: 0;
  transition: opacity .15s;
}
.card:hover .card-del, .card-del:focus-visible { opacity: 1; }
@media (hover: none) { .card-del { opacity: .8; } }
.add {
  height: 240px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
  border: 2px dashed var(--line); border-radius: 6px; cursor: pointer; text-align: center; padding: 12px;
}
.add:hover, .add:focus-visible, .dropping .add { border-color: var(--accent); outline: none; }
.add .plus { font-size: 44px; line-height: 1; color: var(--accent); }
.lib-foot { max-width: 1100px; margin: 0 auto; padding: 8px 16px 32px; color: var(--muted); font-size: 13px; display: flex; flex-wrap: wrap; gap: 6px 14px; }
.byline { display: flex; align-items: center; gap: 5px; font-size: 14px; }
.lib-head .byline { margin-top: 2px; }
.byline a { color: inherit; }
.byline a:hover { color: var(--ink); }
.byline .gh { display: inline-flex; text-decoration: none; }
.byline .gh svg { width: 16px; height: 16px; display: block; }
.linkish { font: inherit; background: none; border: 0; padding: 0; color: var(--accent); text-decoration: underline; cursor: pointer; }
.dropping::after {
  content: 'Drop PDF to add it'; position: fixed; inset: 12px; z-index: 50; display: grid; place-items: center;
  border: 3px dashed var(--accent); border-radius: 12px; background: color-mix(in srgb, var(--bg) 85%, transparent);
  font-size: 22px; font-weight: 600; pointer-events: none;
}

/* reader: fills the window exactly, never scrolls (the edit panel scrolls inside itself) */
#app.reader-view { height: 100vh; height: 100dvh; display: flex; flex-direction: column; overflow: hidden; }
.reader-body { flex: 1; min-height: 0; display: flex; }
.reader-body.with-panel { display: grid; grid-template-columns: minmax(0, 1fr) 360px; }
.stage { position: relative; flex: 1; min-width: 0; min-height: 0; display: grid; place-items: center; padding: 12px; container-type: size; }
.page { width: min(100cqw, 100cqh * var(--ar)); }
#ocr-banner { position: absolute; bottom: 10px; left: 50%; transform: translateX(-50%); z-index: 6; width: max-content; max-width: calc(100% - 24px); }
.banner { box-shadow: 0 2px 10px rgba(0,0,0,.12); }

.bar .pageno { display: inline-flex; align-items: center; gap: 4px; min-width: 0; }
.pageno-input {
  width: 3.2em; height: 30px; font: inherit; text-align: center; font-variant-numeric: tabular-nums;
  background: var(--bg); color: var(--ink); border: 1px solid var(--line); border-radius: 6px; padding: 0 2px;
  appearance: textfield; -moz-appearance: textfield;
}
.pageno-input::-webkit-inner-spin-button, .pageno-input::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }
.pageno-input:focus { outline: 2px solid var(--accent); outline-offset: 0; }
.pageno-total { white-space: nowrap; }

/* page corner tools: play / reveal / edit */
.page-tools { position: absolute; top: 8px; right: 8px; z-index: 4; display: flex; flex-direction: column; gap: 6px; }
.tool {
  width: 38px; height: 38px; padding: 0; border-radius: 50%; display: grid; place-items: center; cursor: pointer;
  border: 1px solid rgba(0,0,0,.12); background: rgba(255,255,255,.88); color: #777;
  box-shadow: 0 1px 4px rgba(0,0,0,.18); transition: color .12s, background .12s;
}
.tool .svg-icon, .tool svg { display: block; width: 20px; height: 20px; }
.tool:hover:not(:disabled) { color: #333; background: #fff; }
.tool[aria-pressed="true"], .tool[aria-pressed="true"]:hover:not(:disabled) { color: #fff; background: #666; border-color: #666; }
.tool.edit-tool[aria-pressed="true"], .tool.edit-tool[aria-pressed="true"]:hover { background: var(--edit); border-color: var(--edit); }
.tool.active { color: var(--accent); }
.tool:disabled { opacity: .35; cursor: default; }
.tool:focus-visible { outline: 3px solid var(--hl); outline-offset: 2px; }
.page.editing .tool:not(.edit-tool) { visibility: hidden; }
.canvas-holder { position: absolute; inset: 0; display: grid; place-items: center; background: var(--panel); }
.canvas-holder canvas { display: block; width: 100%; height: 100%; }
.loading { color: var(--muted); font-size: 14px; }
.banner { max-width: 900px; padding: 8px 12px; border-radius: 8px; background: var(--panel); border: 1px solid var(--line); font-size: 14px; }
.banner.warn { border-color: var(--warn); }
.banner.subtle { color: var(--muted); }
#ocr-banner:empty { display: none; }

/* edit mode */
.btn.small { height: 26px; min-width: 26px; padding: 0 6px; font-size: 13px; }
.edit-layer { position: absolute; inset: 0; cursor: crosshair; touch-action: none; }
.ebox {
  position: absolute; border: 2px solid var(--edit); background: color-mix(in srgb, var(--edit) 10%, transparent);
  border-radius: 3px; cursor: move;
}
.ebox.selected { border-width: 3px; background: color-mix(in srgb, var(--edit) 18%, transparent); box-shadow: 0 0 0 2px rgba(255,255,255,.7); }
.ebox.drawing { border-style: dashed; }
.ebox .ebox-num { position: absolute; left: -2px; top: -2px; transform: translateY(-100%); }
.ebox-num {
  display: inline-grid; place-items: center; min-width: 20px; height: 20px; padding: 0 5px; border-radius: 10px;
  background: var(--edit); color: #fff; font-size: 12px; font-weight: 600;
}
.ebox-del {
  position: absolute; right: -11px; top: -11px; width: 22px; height: 22px; border-radius: 50%; border: 0;
  background: var(--accent); color: #fff; font-size: 12px; cursor: pointer; display: none;
}
.ebox.selected .ebox-del, .ebox:hover .ebox-del { display: block; }
.handle { position: absolute; width: 12px; height: 12px; background: #fff; border: 2px solid var(--edit); border-radius: 2px; }
.h-nw { left: -7px; top: -7px; cursor: nwse-resize; } .h-se { right: -7px; bottom: -7px; cursor: nwse-resize; }
.h-ne { right: -7px; top: -7px; cursor: nesw-resize; } .h-sw { left: -7px; bottom: -7px; cursor: nesw-resize; }
.ebox:not(.selected):not(:hover) .handle { opacity: 0; }

.edit-panel {
  min-height: 0; overflow-y: auto;
  padding: 12px 16px 24px; border-left: 1px solid var(--line); background: var(--panel);
}
.edit-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.edit-head h2 { margin: 0; font-size: 17px; }
#save-status { font-size: 12px; color: var(--ok); }
#save-status[data-state=saving] { color: var(--muted); }
#save-status[data-state=error] { color: var(--accent); }
.hint { color: var(--muted); font-size: 13px; margin: 6px 0 10px; }
.eitems { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.eitem { border: 1px solid var(--line); border-radius: 8px; padding: 6px; }
.eitem.selected { border-color: var(--edit); box-shadow: 0 0 0 1px var(--edit); }
.eitem-head { display: flex; align-items: center; gap: 4px; margin-bottom: 4px; }
.eitem-head .spacer { flex: 1; }
.eitem textarea {
  width: 100%; resize: vertical; min-height: 44px; font: 14px/1.4 "Century Schoolbook", "TeX Gyre Schola", Georgia, serif;
  background: var(--bg); color: var(--ink); border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px; overflow: hidden;
}
.eitem textarea:focus { outline: 2px solid var(--edit); outline-offset: 0; }
.edit-actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 14px 0; }
.title-field { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.title-field input { font: inherit; font-size: 15px; color: var(--ink); background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px; }

#toast {
  position: fixed; left: 50%; bottom: 20px; transform: translate(-50%, 20px); z-index: 60; max-width: min(560px, calc(100vw - 32px));
  background: var(--ink); color: var(--bg); padding: 10px 14px; border-radius: 8px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 860px) {
  .reader-body.with-panel { grid-template-columns: 1fr; grid-template-rows: minmax(0, 1.2fr) minmax(0, 1fr); }
  .edit-panel { border-left: 0; border-top: 1px solid var(--line); }
}
@media (max-width: 480px) {
  .bar .btn { min-width: 30px; padding: 0 6px; }
  .pageno-input { width: 2.6em; }
}

/* settings dialog */
.settings-modal {
  width: min(720px, calc(100vw - 32px)); max-height: calc(100dvh - 48px); padding: 0; overflow-y: auto;
  border: 1px solid var(--line); border-radius: 12px; background: var(--panel); color: var(--ink);
  box-shadow: 0 20px 60px rgba(0,0,0,.35);
}
.settings-modal::backdrop { background: rgba(0,0,0,.4); }
.settings-inner { padding: 0 20px 20px; }
.settings-head {
  position: sticky; top: 0; z-index: 1; display: flex; flex-wrap: wrap; align-items: center; gap: 8px;
  padding: 14px 0 10px; background: var(--panel);
}
.settings-head h2 { margin: 0; font-size: 18px; flex: 1; white-space: nowrap; }
.settings-form { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 0 32px; }
.settings-form fieldset { border: 0; border-top: 1px solid var(--line); margin: 10px 0 0; padding: 6px 0 0; min-width: 0; }
.settings-form legend { font-weight: 600; padding: 0 6px 0 0; }
@media (max-width: 480px) { .settings-inner { padding: 0 14px 16px; } }
