:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --paper: #fffefb;
  --text: #1c2230;
  --muted: #687085;
  --border: #dde1e8;
  --accent: #2459d6;
  --accent-text: #ffffff;
  --error: #c62828;
  --mark: #fff2a8;
  --spelling: #d93025;
  --grammar: #2459d6;
  --punctuation: #e37400;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12151b;
    --card: #1b1f27;
    --paper: #1f232c;
    --text: #e6e9ef;
    --muted: #9aa3b5;
    --border: #2d3340;
    --accent: #5b8cff;
    --accent-text: #0b1020;
    --error: #ff7a7a;
    --mark: #5c4d00;
    --spelling: #ff6b60;
    --grammar: #7ea6ff;
    --punctuation: #ffab40;
  }
}
* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
[hidden] { display: none !important; }
h1, h2, h3 { margin: 0; }
h2 { font-size: 17px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.error { color: var(--error); margin: 0; }
.spacer { flex: 1; }
mark { background: var(--mark); color: inherit; border-radius: 2px; }
kbd { font: 12px ui-monospace, monospace; border: 1px solid currentColor; border-radius: 4px; padding: 0 4px; opacity: .8; }

button, .button, input, select { font: inherit; color: inherit; }
button, .button {
  display: inline-flex; align-items: center; gap: 6px;
  border: 1px solid var(--border);
  background: var(--card);
  border-radius: 8px;
  padding: 6px 12px;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
}
button:hover, .button:hover { border-color: var(--accent); }
button:disabled { opacity: .5; cursor: default; }
.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.ghost { background: transparent; border-color: transparent; }
.big { padding: 10px 18px; font-size: 16px; font-weight: 600; }
input, select {
  border: 1px solid var(--border);
  background: var(--bg);
  border-radius: 8px;
  padding: 6px 10px;
  min-width: 0;
}
label { display: inline-flex; align-items: center; gap: 6px; }
.card { background: var(--card); border: 1px solid var(--border); border-radius: 12px; padding: 16px; }

/* sign in */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(360px, 100%); display: grid; gap: 14px; }
.login-card label, .new-book label { display: grid; gap: 4px; }

/* top bar */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 8px 16px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 18px; }
.title-input { font-weight: 700; font-size: 17px; border-color: transparent; background: transparent; width: min(320px, 100%); }
.author-input { border-color: transparent; background: transparent; width: min(200px, 100%); }
.title-input:hover, .author-input:hover, .title-input:focus, .author-input:focus { border-color: var(--border); background: var(--bg); }

/* library */
.library {
  max-width: 1000px; margin: 0 auto; padding: 16px;
  display: grid; grid-template-columns: 320px minmax(0, 1fr); gap: 20px; align-items: start;
}
.new-book { display: grid; gap: 12px; }
.book-list { list-style: none; padding: 0; margin: 12px 0 0; display: grid; gap: 8px; }
.book-list li { display: flex; align-items: center; gap: 8px; }
.book-list a {
  flex: 1; min-width: 0; display: grid;
  padding: 12px 14px; border: 1px solid var(--border); border-radius: 10px;
  background: var(--card); color: inherit; text-decoration: none;
}
.book-list a:hover { border-color: var(--accent); }
.book-list strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* book */
.book-layout {
  display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 16px;
  padding: 16px; max-width: 1500px; margin: 0 auto;
}
.book-main { display: grid; gap: 14px; align-content: start; min-width: 0; }
.scanner { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 16px; }
.camera { position: relative; background: #000; border-radius: 8px; overflow: hidden; aspect-ratio: 4 / 3; }
.camera video { width: 100%; height: 100%; object-fit: contain; display: block; }
.camera-off { position: absolute; inset: 0; display: grid; place-content: center; justify-items: center; gap: 8px; color: #cfd5e2; text-align: center; padding: 16px; }
.camera-off p { margin: 0; }
.flash { position: absolute; inset: 0; background: #fff; animation: flash .35s ease-out forwards; }
@keyframes flash { from { opacity: .8; } to { opacity: 0; } }
.scanner-controls { display: flex; flex-direction: column; gap: 10px; align-items: flex-start; min-width: 0; }
.scanner-controls select { max-width: 220px; }
.capture-row { display: flex; gap: 8px; flex-wrap: wrap; }
.mode-banner { background: var(--mark); border-radius: 8px; padding: 4px 4px 4px 10px; display: flex; align-items: center; gap: 6px; }
.check { font-size: 14px; }

.toolbar {
  position: sticky; top: 52px; z-index: 5;
  display: flex; gap: 4px; flex-wrap: wrap; align-items: center;
  padding: 6px; background: var(--card); border: 1px solid var(--border); border-radius: 10px;
}
.toolbar button { padding: 4px 10px; }
.toolbar .sep { width: 1px; align-self: stretch; background: var(--border); margin: 0 4px; }

.pages { display: grid; gap: 0; }
.empty { text-align: center; padding: 24px; }
.page { border-top: 1px dashed var(--border); padding: 4px 0 12px; }
.page:first-child { border-top: 0; }
.page-head { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; padding: 4px 0; color: var(--muted); }
.page-label { font-size: 13px; }
.page-label input { width: 64px; padding: 2px 6px; font-size: 13px; }
.small-btn { padding: 2px 8px; font-size: 13px; color: var(--muted); }
.page-body { display: grid; gap: 12px; }
.page.show-photo .page-body { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); align-items: start; }
.page-photo { width: 100%; border: 1px solid var(--border); border-radius: 8px; position: sticky; top: 110px; }
.page-text {
  background: var(--paper);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 20px 28px;
  font: 17px/1.65 Georgia, "Times New Roman", serif;
  min-height: 80px;
  outline: none;
}
.page-text:focus { border-color: var(--accent); }
.page-text p { margin: 0 0 .6em; text-indent: 1.5em; }
.page-text h1 { font-size: 1.5em; text-align: center; margin: .8em 0 .6em; }
.page-text h2 { font-size: 1.25em; text-align: center; margin: .6em 0 .5em; }
.page-text h3 { font-size: 1.1em; margin: .5em 0 .4em; }
.page.busy .page-text { opacity: .55; }
.page.failed .page-info { color: var(--error); }
::highlight(issue-spelling) { text-decoration: underline wavy var(--spelling); text-decoration-thickness: 1.5px; }
::highlight(issue-grammar) { text-decoration: underline wavy var(--grammar); text-decoration-thickness: 1.5px; }
::highlight(issue-punctuation) { text-decoration: underline wavy var(--punctuation); text-decoration-thickness: 1.5px; }
::highlight(issue-current) { background: color-mix(in srgb, var(--accent) 30%, transparent); }

/* sidebar */
.sidebar {
  position: sticky; top: 64px; align-self: start;
  max-height: calc(100vh - 80px); overflow: auto;
  background: var(--card); border: 1px solid var(--border); border-radius: 12px;
}
.tabs { display: flex; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--card); z-index: 1; }
.tabs button { flex: 1; justify-content: center; border: 0; border-radius: 0; background: none; padding: 10px; border-bottom: 2px solid transparent; }
.tabs button[aria-selected="true"] { border-bottom-color: var(--accent); font-weight: 600; }
.badge { background: var(--accent); color: var(--accent-text); border-radius: 10px; padding: 0 7px; font-size: 12px; }
.tab-panel { padding: 12px 14px; display: grid; gap: 10px; }
.tab-panel p { margin: 0; }
.toc { margin: 0; padding: 0; list-style: none; display: grid; gap: 2px; }
.toc a { display: flex; gap: 8px; color: inherit; text-decoration: none; padding: 4px 6px; border-radius: 6px; }
.toc a:hover { background: var(--bg); }
.toc .title { flex: 1; min-width: 0; }
.toc .level-2 { padding-left: 22px; font-size: 14px; }
.toc .level-3 { padding-left: 38px; font-size: 13px; }
.row { display: flex; gap: 6px; flex-wrap: wrap; }
.filters { display: flex; gap: 10px; flex-wrap: wrap; font-size: 13px; }
.dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; }
.dot.spelling { background: var(--spelling); }
.dot.grammar { background: var(--grammar); }
.dot.punctuation { background: var(--punctuation); }
.issues { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.issues .page-group { font-size: 12px; color: var(--muted); font-weight: 600; margin-top: 4px; }
.issue { border: 1px solid var(--border); border-left: 4px solid var(--grammar); border-radius: 8px; padding: 8px 10px; display: grid; gap: 6px; cursor: pointer; }
.issue.spelling { border-left-color: var(--spelling); }
.issue.punctuation { border-left-color: var(--punctuation); }
.issue.current { border-color: var(--accent); }
.issue .context { font-size: 13px; color: var(--muted); }
.issue .context b { color: var(--text); text-decoration: underline wavy; }
.issue .message { font-size: 14px; }
.issue .fixes { display: flex; gap: 4px; flex-wrap: wrap; }
.issue .fixes button { padding: 2px 8px; font-size: 13px; }
.issue .fixes .fix { border-color: var(--accent); color: var(--accent); }
.chips { list-style: none; padding: 0; margin: 6px 0; display: flex; gap: 4px; flex-wrap: wrap; }
.chips li { border: 1px solid var(--border); border-radius: 12px; padding: 0 4px 0 10px; display: inline-flex; align-items: center; gap: 2px; }
.chips button { padding: 0 6px; border: 0; background: none; }

@media (max-width: 1000px) {
  .book-layout { grid-template-columns: minmax(0, 1fr); }
  .sidebar { position: static; max-height: none; }
  .scanner { grid-template-columns: minmax(0, 1fr); }
  .toolbar { top: 0; }
  .library { grid-template-columns: minmax(0, 1fr); }
  .page.show-photo .page-body { grid-template-columns: minmax(0, 1fr); }
  .page-photo { position: static; }
  .topbar { position: static; }
}
@media (max-width: 600px) {
  .page-text { padding: 14px 16px; font-size: 16px; }
  .title-input, .author-input { width: 100%; }
}
