:root {
  --bg: #f6f4ef;
  --paper: #ffffff;
  --ink: #1f2328;
  --muted: #6b6f76;
  --line: #e2ded5;
  --accent: #1d4e89;
  --accent-soft: #e6eef8;
  --done: #2f7d4f;
  --progress: #c68a12;
  --new: #b9bcc2;
  --low: #d98a00;
  --edited: #2f7d4f;
  --danger: #b3261e;
  --serif: "Noto Serif", "Gentium Plus", Georgia, serif;
  --sans: "Noto Sans", system-ui, -apple-system, "Segoe UI", sans-serif;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--ink); font: 15px/1.5 var(--sans); }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2 { font-family: var(--serif); font-weight: 600; margin: 0 0 .4rem; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.error { color: var(--danger); }
.spacer { flex: 1; }

button, .button, select, input { font: inherit; }
button, .button {
  display: inline-flex; align-items: center; gap: .3rem;
  padding: .45rem .9rem; border: 1px solid var(--line); border-radius: 6px;
  background: var(--paper); color: var(--ink); cursor: pointer; text-decoration: none;
}
button:hover, .button:hover { border-color: #c9c3b6; text-decoration: none; }
button:disabled { opacity: .5; cursor: default; }
.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.primary:hover { background: #163d6c; }
button.link { border: 0; background: none; padding: 0; color: var(--accent); }
.small-btn { padding: .3rem .8rem; }
input, select { padding: .45rem .6rem; border: 1px solid var(--line); border-radius: 6px; background: var(--paper); }
input:focus, select:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* top bar */
.topbar {
  position: sticky; top: 0; z-index: 10; height: 52px;
  display: flex; align-items: center; gap: 1rem; padding: 0 1rem;
  background: var(--paper); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: .45rem; font-weight: 600; color: var(--accent); }
.crumbs { display: flex; gap: .45rem; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--muted); }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: .8rem; }
.topbar-right form { margin: 0; }
.who { color: var(--muted); }
.lang { font-size: 13px; }

/* login */
.login { min-height: calc(100vh - 52px); display: grid; grid-template-columns: minmax(320px, 420px) 1fr; }
.login-card { align-self: center; justify-self: center; width: min(360px, 90vw); padding: 2rem 0; }
.login-card h1 { font-size: 2rem; color: var(--accent); }
.login-card form { display: grid; gap: .9rem; margin-top: 1.4rem; }
.login-card label { display: grid; gap: .3rem; font-size: 13px; color: var(--muted); }
.login-side { background: #e9e5dc; overflow: hidden; display: flex; align-items: center; justify-content: center; }
.login-side img { max-height: 86vh; box-shadow: 0 8px 30px rgba(0,0,0,.12); transform: rotate(-1.5deg); }

/* library and book */
.wrap { max-width: 1200px; margin: 0 auto; padding: 1.6rem 1rem 3rem; }
.page-title { margin-bottom: 1.2rem; }
.books { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fill, minmax(420px, 1fr)); }
.book-card { display: flex; gap: 1.1rem; padding: 1rem; background: var(--paper); border: 1px solid var(--line); border-radius: 10px; color: var(--ink); }
.book-card:hover { border-color: var(--accent); text-decoration: none; }
.cover { width: 120px; height: auto; border: 1px solid var(--line); background: #fff; }
.cover.big { width: 170px; }
.subtitle { font-family: var(--serif); font-style: italic; margin: 0 0 .3rem; }
.author { margin: 0 0 .3rem; }
.book-meta p { margin: .15rem 0; }
.bar { position: relative; display: flex; height: 8px; border-radius: 4px; background: #ebe7de; overflow: hidden; margin: .6rem 0 .3rem; width: 260px; }
.bar.wide { width: min(520px, 100%); }
.bar-done { background: var(--done); }
.bar-progress { background: var(--progress); }
.legend { display: flex; align-items: center; gap: .35rem; flex-wrap: wrap; }
.legend .dot { margin-left: .6rem; }
.legend .dot:first-child { margin-left: 0; }
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; background: var(--new); }
.dot.done { background: var(--done); }
.dot.in_progress { background: var(--progress); }

.book-head { display: flex; gap: 1.6rem; align-items: flex-start; margin-bottom: 1.4rem; }
.book-head > div { flex: 1; min-width: 0; }
.book-head h1 { font-size: 2rem; }
.actions { display: flex; gap: .6rem; margin: 1rem 0 .8rem; flex-wrap: wrap; }
.search { display: flex; gap: .5rem; max-width: 560px; }
.search input { flex: 1; }
.results { background: var(--paper); border: 1px solid var(--line); border-radius: 10px; padding: 1rem 1.2rem; margin-bottom: 1.4rem; }
.results h2 { font-size: 1.1rem; }
.results ol { margin: 0; padding: 0; list-style: none; max-height: 380px; overflow: auto; }
.results li a { display: flex; gap: 1rem; padding: .3rem 0; border-bottom: 1px solid #f0ede6; color: var(--ink); }
.results .pg { flex: 0 0 70px; color: var(--muted); font-size: 13px; }
.results .txt { font-family: var(--serif); }

.book-body { display: grid; grid-template-columns: 360px 1fr; gap: 1.4rem; align-items: start; }
.toc { position: sticky; top: 64px; max-height: calc(100vh - 80px); overflow: auto; background: var(--paper); border: 1px solid var(--line); border-radius: 10px; padding: 1rem; }
.toc h2, .thumbs-wrap h2 { font-size: 1.1rem; }
.toc ul { list-style: none; margin: 0; padding: 0; }
.toc li a { display: flex; justify-content: space-between; gap: .6rem; padding: .18rem 0; color: var(--ink); font-family: var(--serif); font-size: 14px; }
.toc li a:hover { color: var(--accent); }
.toc .pg { color: var(--muted); font-family: var(--sans); font-size: 12px; flex: none; }
.toc .lvl0 { margin-top: .5rem; }
.toc .lvl0 a { font-weight: 600; font-size: 13px; letter-spacing: .02em; }
.toc .lvl1 a { padding-left: .8rem; }
.toc .lvl2 a { padding-left: 1.6rem; font-size: 13px; color: #3b3f45; }
.thumbs { display: grid; gap: .8rem; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); }
.thumb { position: relative; display: block; background: var(--paper); border: 1px solid var(--line); border-radius: 6px; padding: 4px; color: var(--muted); }
.thumb img { width: 100%; height: 150px; object-fit: contain; display: block; }
.thumb .lbl { display: block; text-align: center; font-size: 12px; padding-top: 3px; }
.thumb::after { content: ""; position: absolute; top: 8px; right: 8px; width: 10px; height: 10px; border-radius: 50%; background: var(--new); box-shadow: 0 0 0 2px #fff; }
.thumb.done::after { background: var(--done); }
.thumb.in_progress::after { background: var(--progress); }
.thumb:hover { border-color: var(--accent); }

/* editor */
.page-editor { height: 100vh; overflow: hidden; }
.pager { display: flex; align-items: center; gap: .3rem; }
.goto { display: flex; align-items: center; gap: .3rem; margin: 0; }
.goto input { width: 58px; padding: .2rem .35rem; text-align: right; }
.goto .muted { white-space: nowrap; }
#status { padding: .3rem .5rem; }
.page-editor .who { display: none; }
.topbar-right { white-space: nowrap; }
.icon-btn { display: inline-flex; align-items: center; justify-content: center; width: 30px; height: 30px; padding: 0; border: 1px solid var(--line); border-radius: 6px; background: var(--paper); color: var(--ink); font-size: 18px; }
.icon-btn.wide { width: 38px; }
.save-state { min-width: 9rem; text-align: right; }
.save-state.dirty { color: var(--progress); }
.editor { height: calc(100vh - 52px); display: grid; grid-template-columns: minmax(0, 1.05fr) minmax(0, 1fr); }
.viewer { display: flex; flex-direction: column; min-height: 0; border-right: 1px solid var(--line); background: #e7e3da; }
.viewer-tools, .transcript-tools { display: flex; align-items: center; gap: .4rem; padding: .45rem .7rem; background: var(--paper); border-bottom: 1px solid var(--line); }
.stage-scroll { flex: 1; overflow: auto; padding: 16px; }
.stage { position: relative; margin: 0 auto; box-shadow: 0 2px 14px rgba(0,0,0,.12); background: #fff; }
.stage img, .stage svg { position: absolute; inset: 0; width: 100%; height: 100%; }
.stage svg rect { fill: transparent; stroke: rgba(29,78,137,.22); stroke-width: 1; cursor: pointer; vector-effect: non-scaling-stroke; }
.stage svg rect:hover, .stage svg rect.hover { fill: rgba(29,78,137,.07); stroke: rgba(29,78,137,.45); }
.stage svg rect.low { stroke: rgba(217,138,0,.55); }
.stage svg rect.edited { stroke: rgba(47,125,79,.6); }
.stage svg rect.active { fill: rgba(29,78,137,.12); stroke: var(--accent); stroke-width: 2.5; }
.stage svg rect.deleted, .stage.hide-boxes svg rect:not(.active) { display: none; }

.transcript { display: flex; flex-direction: column; min-height: 0; background: var(--paper); }
.tab { padding: .25rem .65rem; border: 1px solid var(--line); border-radius: 999px; font-size: 13px; background: var(--paper); color: var(--ink); }
.tab[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.kb, .history { padding: .6rem .8rem; border-bottom: 1px solid var(--line); background: #fbfaf7; }
.kb-keys { display: flex; flex-wrap: wrap; gap: .35rem; margin-top: .4rem; }
.kb-keys button { min-width: 38px; justify-content: center; font-family: var(--serif); font-size: 18px; padding: .2rem .5rem; }
.history ul { list-style: none; margin: 0; padding: 0; max-height: 180px; overflow: auto; font-size: 13px; }
.history li { display: flex; justify-content: space-between; gap: 1rem; padding: .25rem 0; border-bottom: 1px solid #efece5; }
.lines { position: relative; flex: 1; overflow: auto; margin: 0; padding: .4rem 0 2rem; list-style: none; }
.lines li { display: grid; grid-template-columns: 2.6rem 1fr 1.8rem 1.8rem; align-items: center; border-left: 3px solid transparent; }
.lines li.low { border-left-color: var(--low); }
.lines li.edited { border-left-color: var(--edited); }
.lines li.active, .lines li.hover { background: var(--accent-soft); }
.lines .num { text-align: right; padding-right: .6rem; color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.lines input { border: 0; border-radius: 0; background: transparent; padding: .38rem .2rem; font-family: var(--serif); font-size: 16.5px; width: 100%; }
.lines input:focus { outline: none; }
.lines .reset { visibility: hidden; border: 0; background: none; color: var(--muted); padding: 0; justify-content: center; }
.lines li.edited .reset { visibility: visible; }
.lines .del { visibility: hidden; border: 0; background: none; color: var(--muted); padding: 0; justify-content: center; font-size: 13px; }
.lines li:hover .del, .lines li.active .del, .lines li.deleted .del { visibility: visible; }
.lines .del:hover { color: var(--danger); }
.lines li.deleted { border-left-color: var(--danger); }
.lines li.deleted input { text-decoration: line-through; color: var(--muted); }
.lines li.deleted .reset { visibility: hidden; }
.hint { padding: .5rem .9rem; margin: 0; border-top: 1px solid var(--line); }

@media (max-width: 900px) {
  .login { grid-template-columns: 1fr; }
  .login-side { display: none; }
  .book-body { grid-template-columns: 1fr; }
  .toc { position: static; max-height: none; }
  .book-head { flex-direction: column; }
  .books { grid-template-columns: 1fr; }
  .page-editor { height: auto; overflow: auto; }
  .editor { grid-template-columns: 1fr; height: auto; }
  .stage-scroll { max-height: 60vh; }
  .crumbs, .who { display: none; }
}
