/* Deep Reader — colours taken from Neko-samma's coat:
   espresso stripes & eyes, tabby brown, pink ears & nose, warm white blaze. */
:root {
  --paper: #F6F2ED;
  --panel: #FFFFFF;
  --ink: #3B2E26;
  --ink-soft: #5E4A3D;
  --text: #2A211C;
  --muted: #7A6B60;
  --line: #E6DDD3;
  --lantern: #E59AA0;
  --lantern-soft: #FBE7E5;
  --matcha: #7A5A45;
  --matcha-soft: #F1E8E0;
  --sakura: #E9A9A6;
  --bad: #B9384B;
  --neko: #2A211C;
  --side-w: 420px;
  --ui: "Be Vietnam Pro", system-ui, -apple-system, "Segoe UI", sans-serif;
  --read: "Literata", Georgia, serif;
  --r-s: 8px; --r-m: 12px; --r-l: 20px;
  --bar-h: 56px;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font: 15px/1.55 var(--ui); color: var(--text); background: var(--paper); display: flex; flex-direction: column; -webkit-tap-highlight-color: transparent; }
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }
code { background: #EFE7DF; padding: 1px 5px; border-radius: 5px; font-size: .9em; }
::selection { background: var(--lantern-soft); }

/* ---------- controls ---------- */
button { cursor: pointer; border: 1px solid var(--line); background: var(--panel); border-radius: var(--r-s); padding: 7px 12px; min-height: 36px; }
button:hover { border-color: var(--ink-soft); }
button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, summary:focus-visible { outline: 3px solid var(--lantern); outline-offset: 2px; }
button.primary { background: var(--matcha); border-color: var(--matcha); color: #fff; font-weight: 600; }
button.primary:hover { background: #664A38; }
button.ghost { background: transparent; border-color: transparent; color: var(--muted); }
button.icon { width: 36px; padding: 0; font-size: 18px; line-height: 1; }
button:disabled { opacity: .5; cursor: wait; }
input, select, textarea { border: 1px solid var(--line); border-radius: var(--r-s); padding: 8px 10px; background: #fff; min-height: 36px; }
textarea { width: 100%; resize: vertical; }
select { max-width: 100%; }

/* ---------- Neko-samma ---------- */
.neko { width: 28px; height: 28px; flex: 0 0 auto; overflow: visible; }
.neko-lg { width: 84px; height: 84px; }
.neko-xl { width: 120px; height: 120px; }
.neko-sm { width: 40px; height: 40px; }
.neko-think { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 14px; padding: 6px 0; }
.neko-think .neko { width: 36px; height: 36px; animation: bob 1.6s ease-in-out infinite; }
@keyframes bob { 0%, 100% { transform: rotate(-6deg); } 50% { transform: rotate(6deg) translateY(-2px); } }
.empty { display: flex; align-items: center; gap: 12px; color: var(--muted); padding: 14px 4px; margin: 0; }
.empty .neko { width: 44px; height: 44px; }
.bubble { position: relative; background: var(--panel); border: 1px solid var(--line); border-radius: var(--r-m); padding: 10px 14px; color: var(--text); margin: 8px 0 18px; }
@media (prefers-reduced-motion: reduce) { .neko-think .neko { animation: none; } }

/* ---------- top bar ---------- */
#topbar { display: flex; align-items: center; gap: 8px; padding: 8px 14px; background: var(--ink); color: #fff; flex: 0 0 auto; min-height: var(--bar-h); }
#topbar button { background: transparent; color: #fff; border-color: transparent; }
#topbar button:hover { background: rgba(255,255,255,.1); }
#topbar select { background: var(--ink-soft); color: #fff; border-color: transparent; }
#topbar .brand { display: flex; align-items: center; gap: 8px; font-family: var(--read); font-size: 18px; font-weight: 600; padding: 4px 8px; }
#readerBar { display: flex; align-items: center; gap: 6px; flex: 1; min-width: 0; }
.book-title { font-family: var(--read); font-style: italic; color: #D6C7BB; max-width: 240px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#chapterSelect { max-width: 340px; min-width: 0; }
.sep { width: 1px; height: 22px; background: rgba(255,255,255,.2); margin: 0 4px; }
.zoom-group { display: inline-flex; align-items: center; }
#zoomLabel { font-size: 13px; color: #D6C7BB; min-width: 44px; text-align: center; }
#topbar .right { margin-left: auto; display: flex; gap: 6px; align-items: center; }
.pill { font-size: 12.5px; padding: 3px 10px; border-radius: 99px; background: rgba(255,255,255,.12); color: #EADFD6; white-space: nowrap; }
.pill.on, .pill.sync-ok { background: rgba(233,169,166,.28); color: #FBE7E5; }
.pill.sync-busy { background: rgba(233,169,166,.3); color: var(--lantern-soft); }
.pill.sync-err { background: rgba(185,56,75,.45); color: #FFE0E6; }
.pill.sync-offline { background: rgba(255,255,255,.1); color: #D6C7BB; }

/* ---------- login ---------- */
#login { flex: 1; display: flex; align-items: center; justify-content: center; padding: 24px; }
.login-box { text-align: center; max-width: 380px; }
.login-box h1 { font-family: var(--read); font-weight: 600; font-size: 34px; margin: 8px 0 0; color: var(--ink); }
button.big { font-size: 16px; padding: 12px 24px; border-radius: var(--r-m); }

/* ---------- library ---------- */
#library { overflow: auto; flex: 1; padding: 36px max(20px, calc(50% - 460px)) 60px; }
.hero-row { display: flex; gap: 20px; align-items: center; }
.hero h1 { font-family: var(--read); font-weight: 600; font-size: clamp(28px, 4vw, 40px); line-height: 1.15; margin: 0 0 8px; color: var(--ink); }
.hero p { color: var(--muted); max-width: 60ch; margin: 0; }
#dropzone { display: grid; justify-items: center; gap: 2px; padding: 26px 20px; margin: 26px 0 36px; border: 2px dashed #D9CBBE; border-radius: var(--r-l); background: var(--panel); cursor: pointer; text-align: center; }
#dropzone.drag, #dropzone:hover { border-color: var(--matcha); background: var(--matcha-soft); }
.dz-icon { font-size: 28px; color: var(--matcha); line-height: 1; }
.dz-main { font-weight: 600; font-size: 16px; }
.dz-sub, #dropzone small { color: var(--muted); font-size: 13.5px; }
#library h2 { font-family: var(--read); font-weight: 600; font-size: 22px; color: var(--ink); margin: 0 0 14px; }
.book-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 16px; }
.book { position: relative; background: var(--panel); border-radius: 4px var(--r-m) var(--r-m) 4px; padding: 18px 16px 16px 22px; cursor: pointer; display: flex; flex-direction: column; gap: 6px; border: 1px solid var(--line); border-left: 8px solid var(--ink); min-height: 140px; }
.book:hover { border-left-color: var(--lantern); }
.book h3 { font-family: var(--read); font-size: 18px; line-height: 1.25; margin: 0; color: var(--ink); }
.book .meta { color: var(--muted); font-size: 13px; }
.book .warn { color: #8A4F49; font-size: 13px; }
.book.missing { border-left-color: #D9CBBE; }
.book .del { position: absolute; top: 6px; right: 6px; border: none; background: none; color: var(--muted); min-height: 0; padding: 4px 8px; opacity: .5; }
.book:hover .del, .book .del:focus-visible { opacity: 1; }
#importProgress { margin-top: 12px; }

/* ---------- reader ---------- */
#reader { flex: 1; display: flex; min-height: 0; }
#pdfPane { flex: 1; overflow: auto; padding: 20px 24px 80px; min-width: 0; }
#chapterHead, #chapterFoot { max-width: 900px; margin: 0 auto; }
#chapterHead h2 { font-family: var(--read); font-weight: 600; font-size: 24px; margin: 4px 0 2px; color: var(--ink); }
#chapterHead .meta { color: var(--muted); font-size: 13.5px; margin-bottom: 16px; }
#chapterFoot { text-align: center; padding: 30px 0; color: var(--muted); }
#chapterFoot .row { justify-content: center; }
#pages { display: flex; flex-direction: column; align-items: center; gap: 28px; padding-top: 18px; }
.page { position: relative; background: #fff; box-shadow: 0 1px 2px rgba(59,46,38,.08), 0 6px 18px rgba(59,46,38,.08); border-radius: 2px; }
.page canvas { display: block; }
.page .pnum { position: absolute; top: -20px; left: 0; font-size: 12px; color: var(--muted); }
.page.flash { outline: 3px solid var(--lantern); }
.textLayer { opacity: 1; }
.textLayer ::selection, .textLayer span::selection { background: rgba(233,169,166,.45); }

#splitter { width: 6px; cursor: col-resize; background: var(--line); flex: 0 0 auto; }
#splitter:hover { background: var(--lantern); }
#side { width: var(--side-w); flex: 0 0 auto; display: flex; flex-direction: column; background: var(--panel); min-height: 0; }
.sheet-handle { display: none; }
.tabs { display: flex; border-bottom: 1px solid var(--line); flex: 0 0 auto; }
.tabs button { flex: 1; border: none; border-radius: 0; background: none; padding: 10px 4px 9px; border-bottom: 3px solid transparent; font-size: 14px; color: var(--muted); display: flex; align-items: center; justify-content: center; gap: 6px; min-height: 48px; }
.tabs button.active { border-bottom-color: var(--lantern); color: var(--ink); font-weight: 600; }
.count { font-size: 11px; background: var(--lantern-soft); color: #8A4F49; border-radius: 99px; padding: 0 7px; font-weight: 600; }
.count:empty { display: none; }
.tab-panel { flex: 1; overflow: auto; padding: 14px; display: flex; flex-direction: column; gap: 12px; }
.hint { color: var(--muted); font-size: 13.5px; margin: 0; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--r-m); padding: 12px; }
.composer { display: flex; flex-direction: column; gap: 8px; background: #FBF8F4; }
.composer .pg { font-size: 13px; color: var(--muted); margin-left: auto; display: flex; align-items: center; gap: 6px; }
.composer .pg input { width: 72px; }
.sticky-bottom { position: sticky; bottom: 0; box-shadow: 0 -10px 14px var(--panel); }
blockquote { margin: 0; padding: 6px 12px; border-left: 3px solid var(--lantern); background: #FDF3F1; font-family: var(--read); font-size: 14.5px; line-height: 1.6; color: #3B2E26; white-space: pre-wrap; max-height: 160px; overflow: auto; border-radius: 0 var(--r-s) var(--r-s) 0; }
.filters { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { border-radius: 99px; padding: 4px 12px; font-size: 13px; min-height: 32px; }
.chip.active { background: var(--ink); color: #fff; border-color: var(--ink); }
.inline { display: flex; gap: 6px; margin-left: auto; }
.inline input { width: 150px; }

/* notes: colour-coded by type via the left edge, not identical cards */
.note { display: flex; flex-direction: column; gap: 6px; border-left: 4px solid var(--line); border-radius: 4px var(--r-m) var(--r-m) 4px; }
.note:has(.badge.insight) { border-left-color: var(--lantern); }
.note:has(.badge.question) { border-left-color: #A78B76; }
.note:has(.badge.connection) { border-left-color: var(--matcha); }
.note:has(.badge.highlight) { border-left-color: var(--sakura); }
.note .head, .term .head, .qa .head { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); flex-wrap: wrap; }
.head .actions { margin-left: auto; display: flex; gap: 2px; align-items: center; }
.head .actions button { border: none; background: none; padding: 4px 8px; color: var(--muted); min-height: 32px; }
.head .actions button:hover { color: var(--ink); }
.pagelink { color: var(--matcha); cursor: pointer; text-decoration: underline; text-decoration-style: dotted; text-underline-offset: 3px; }
.note .content { white-space: pre-wrap; }
.badge { font-size: 12px; font-weight: 600; color: var(--ink-soft); }

.md { font-size: 14.5px; }
.md p { margin: 6px 0; }
.md ul, .md ol { margin: 4px 0; padding-left: 20px; }
.md h3, .md h4, .md h5, .md h6 { font-size: 15px; margin: 10px 0 4px; }
.md table { border-collapse: collapse; font-size: 13px; }
.md td, .md th { border: 1px solid var(--line); padding: 3px 6px; }
.qa .q { font-weight: 600; }
.loading { color: var(--muted); }

.kind-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.kind-grid button { text-align: left; display: flex; flex-direction: column; gap: 3px; padding: 12px; border-radius: var(--r-m); background: #FBF8F4; }
.kind-grid button:hover { border-color: var(--lantern); background: #FDF3F1; }
.kind-grid span { font-size: 12.5px; color: var(--muted); }
.reflection { display: flex; flex-direction: column; gap: 10px; border: 2px solid var(--lantern); }
.reflection .question { font-family: var(--read); font-size: 16.5px; line-height: 1.6; }
.hintbox { background: #FDF3F1; border: 1px dashed #EBC0BC; border-radius: var(--r-s); padding: 8px 12px; font-size: 14px; }
.hintbox b { color: #8A4F49; }
.feedback { background: var(--matcha-soft); border-radius: var(--r-s); padding: 8px 12px; }
h3.sub { font-size: 14px; color: var(--muted); margin: 12px 0 0; font-weight: 600; }
details.card summary { cursor: pointer; }

.term { display: flex; flex-direction: column; gap: 4px; }
.term .word { font-family: var(--read); font-size: 19px; font-weight: 600; color: var(--ink); }
.term .ipa { color: var(--matcha); font-size: 15px; }
.term .ctx { font-family: var(--read); font-style: italic; color: #5E4A3D; font-size: 14px; }
.term .tip { font-size: 13px; color: #8A4F49; }
.term.new { animation: pop 1.4s ease; }
@keyframes pop { from { background: var(--lantern-soft); } to { background: var(--panel); } }
.speak { font-size: 16px; border: none !important; background: none !important; min-height: 32px; padding: 2px 6px; }

/* selection toolbar */
#selToolbar { position: fixed; z-index: 50; display: flex; gap: 2px; padding: 4px; background: var(--ink); border-radius: var(--r-m); box-shadow: 0 8px 24px rgba(59,46,38,.35); }
#selToolbar button { background: transparent; color: #fff; border: none; font-size: 14px; padding: 6px 10px; }
#selToolbar button:hover { background: rgba(255,255,255,.14); }
#selToolbar button[data-act="explain"] { color: var(--lantern); font-weight: 600; }

/* dialogs & toast */
dialog { border: none; border-radius: var(--r-l); padding: 20px 22px; width: min(620px, 94vw); background: var(--panel); box-shadow: 0 24px 60px rgba(59,46,38,.3); max-height: 90vh; overflow: auto; }
dialog::backdrop { background: rgba(59,46,38,.45); }
dialog h3 { font-family: var(--read); margin: 10px 0 6px; color: var(--ink); }
dialog label { display: flex; flex-direction: column; gap: 4px; margin: 10px 0; font-weight: 600; font-size: 14px; }
dialog form { display: flex; flex-direction: column; gap: 8px; }
#chaptersText { font-family: ui-monospace, Consolas, monospace; font-size: 13px; }
.filebtn { display: inline-flex !important; flex-direction: row !important; margin: 0 !important; font-weight: 400 !important; border: 1px solid var(--line); border-radius: var(--r-s); padding: 7px 12px; cursor: pointer; }
#toast { position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%); background: var(--ink); color: #fff; padding: 10px 18px; border-radius: var(--r-m); font-size: 14px; opacity: 0; pointer-events: none; transition: opacity .25s; z-index: 100; max-width: 88vw; }
#toast.show { opacity: .97; }
#toast.error { background: var(--bad); }

/* ================= mobile ================= */
@media (max-width: 820px) {
  :root { --bar-h: 52px; }
  body { font-size: 15px; }
  #topbar { flex-wrap: wrap; padding: 6px 8px; gap: 4px; }
  #topbar .brand span { display: none; }
  #topbar .brand { padding: 4px; }
  #readerBar { order: 3; flex-basis: 100%; }
  .book-title, .zoom-only, .tool .lbl { display: none; }
  #chapterSelect { flex: 1; max-width: none; }
  #aiStatus { display: none; }
  #library { padding: 22px 16px 40px; }
  .hero-row { gap: 14px; align-items: flex-start; }
  .neko-lg { width: 60px; height: 60px; }
  .book-grid { grid-template-columns: 1fr; }
  #pdfPane { padding: 14px 8px calc(var(--bar-h) + 70px); }
  #chapterHead h2 { font-size: 20px; }
  .page .pnum { left: 4px; }
  #splitter { display: none; }

  /* side panel becomes a bottom sheet; the tab row is the bottom bar */
  #side { position: fixed; left: 0; right: 0; bottom: 0; width: auto; z-index: 40; height: auto; max-height: 72vh;
          border-radius: var(--r-l) var(--r-l) 0 0; box-shadow: 0 -8px 28px rgba(59,46,38,.18); padding-bottom: env(safe-area-inset-bottom); }
  #side .tab-panel { display: none; }
  #side.open { height: 72vh; }
  #side.open .tab-panel:not([hidden]) { display: flex; }
  #side .tabs { order: 2; border-bottom: none; border-top: 1px solid var(--line); }
  #side.open .tabs { order: 0; border-top: none; border-bottom: 1px solid var(--line); }
  #side:not(.open) .tabs button.active { border-bottom-color: transparent; }
  .tabs button { font-size: 14px; padding: 6px 2px; min-height: 52px; position: relative; }
  .count { position: absolute; top: 6px; right: 6px; }
  .sheet-handle { display: none; }
  #side.open .sheet-handle { display: flex; justify-content: center; border: none; background: none; min-height: 22px; padding: 8px 0 2px; }
  .sheet-handle span { width: 44px; height: 5px; border-radius: 99px; background: var(--line); }
  .kind-grid { grid-template-columns: 1fr; }
  .inline input { width: 120px; }

  /* toolbar docks above the bottom bar so it doesn't fight the phone's own selection menu */
  #selToolbar.docked { left: 8px !important; right: 8px; top: auto !important; bottom: calc(68px + env(safe-area-inset-bottom)); justify-content: space-around; }
  #selToolbar.docked button { padding: 10px 6px; }
  #toast { bottom: auto; top: calc(var(--bar-h) + 54px); }
}

/* text-only actions */
button.tool { font-size: 14px; }
#topbar #settingsBtn { color: #EADFD6; }
.speak { color: var(--matcha); font-size: 13px; font-weight: 600; }
.term .vi { color: var(--ink-soft); }
.head .actions button { font-size: 13px; }
@media (max-width: 820px) { #topbar button.tool { padding: 6px 8px; font-size: 14px; } #editChaptersBtn, #exportBtn { display: none; } }

/* AI language switch */
.langbar { display: flex; align-items: center; gap: 4px; padding: 8px 14px; border-bottom: 1px solid var(--line); font-size: 13px; color: var(--muted); flex-wrap: wrap; }
.langbar span { margin-right: 4px; }
.langbar button { min-height: 30px; padding: 3px 10px; font-size: 13px; border-radius: 99px; background: transparent; }
.langbar button.active { background: var(--ink); border-color: var(--ink); color: #fff; }
@media (max-width: 820px) {
  #side .langbar { display: none; }
  #side.open .langbar { display: flex; order: 0; }
  #side.open .tabs { order: 0; }
}

/* voice settings */
.voice-row { display: grid; grid-template-columns: 1fr 1fr auto; gap: 12px; align-items: end; }
.voice-row label { margin: 0; font-weight: 500; }
.voice-row input[type="range"] { accent-color: var(--matcha); }
@media (max-width: 820px) { .voice-row { grid-template-columns: 1fr; } #selToolbar.docked button { padding: 10px 4px; font-size: 13px; } }
dialog label textarea, dialog label select, dialog label input { font-weight: 400; }
