/*
 *  app.css
 *  Hana Reader
 *
 *  Chrome recedes so the page can be the subject: cool ink greys, one accent
 *  drawn from Goryeo celadon, and the reading surface kept warm and separate.
 */

@font-face {
  font-family: 'Pretendard';
  src: local('Pretendard Variable'), local('Pretendard');
  font-display: swap;
}

:root {
  --ink-0: #0e1218;
  --ink-1: #12161c;
  --ink-2: #1a2027;
  --ink-3: #232c36;
  --ink-4: #2f3a46;

  --fg: #e2e7ed;
  --fg-dim: #97a3b1;
  --fg-faint: #66727f;

  --celadon: #7fb3a3;
  --celadon-deep: #4d7d6f;
  --amber: #d9a441;
  --alert: #d97a6c;

  --sans: 'Pretendard', 'Apple SD Gothic Neo', 'Noto Sans KR', 'Malgun Gothic',
          system-ui, -apple-system, 'Segoe UI', sans-serif;
  --serif: 'Noto Serif KR', 'Nanum Myeongjo', 'Apple SD Gothic Neo', serif;

  --rail-w: 76px;
  --radius: 10px;
  --shadow: 0 18px 44px -18px rgba(0, 0, 0, .78);
}

html[data-accent='amber']  { --celadon: #d9a441; --celadon-deep: #9c7526; }
html[data-accent='sky']    { --celadon: #7fb0d9; --celadon-deep: #4a7aa3; }
html[data-accent='rose']   { --celadon: #d98fa4; --celadon-deep: #a05e72; }
html[data-accent='mono']   { --celadon: #c9ced4; --celadon-deep: #8b949e; }

html[data-chrome='light'] {
  --ink-0: #f3f4f6; --ink-1: #ffffff; --ink-2: #f0f2f5; --ink-3: #dde1e6; --ink-4: #c3c9d1;
  --fg: #1d232b; --fg-dim: #4f5b68; --fg-faint: #7d8794;
  --shadow: 0 18px 44px -18px rgba(20, 30, 40, .35);
}
html[data-chrome='light'] .button.primary { color: #fff; }
html[data-ui-size='small']  { font-size: 14px; }
html[data-ui-size='large']  { font-size: 17px; }
html[data-ui-font='system'] { --sans: -apple-system, 'Apple SD Gothic Neo', system-ui, 'Segoe UI', 'Noto Sans KR', sans-serif; }
html[data-ui-font='serif']  { --sans: 'Noto Serif KR', 'Nanum Myeongjo', Georgia, serif; }

* { box-sizing: border-box; margin: 0; padding: 0; }

html { font-size: 15px; }
html, body {
  height: 100%;
  height: 100dvh;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  background: var(--ink-0);
  color: var(--fg);
  font-family: var(--sans);
  font-size: 1rem;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

body { display: flex; overflow: hidden; }

button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--celadon); outline-offset: 2px; }

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}

/* ── Left rail ─────────────────────────────────────────────────── */

.rail {
  width: var(--rail-w);
  flex: 0 0 var(--rail-w);
  background: var(--ink-1);
  border-right: 1px solid var(--ink-3);
  display: flex;
  flex-direction: column;
  align-items: stretch;
  padding: 14px 0 18px;
  gap: 2px;
}

.rail-mark {
  font-size: 22px;
  color: var(--celadon);
  text-align: center;
  padding-bottom: 16px;
  letter-spacing: .02em;
}

.rail-fill { flex: 1; }

.rail-tab {
  background: none;
  border: 0;
  border-left: 2px solid transparent;
  padding: 11px 4px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  cursor: pointer;
  color: var(--fg-faint);
  transition: color .12s ease, background .12s ease;
}
.rail-tab:hover { color: var(--fg-dim); background: var(--ink-2); }
.rail-tab[aria-current='page'] {
  color: var(--celadon);
  border-left-color: var(--celadon);
  background: var(--ink-2);
}
.rail-glyph { font-size: 17px; line-height: 1; }
.rail-label { font-size: 10.5px; letter-spacing: .01em; }

/* ── Stage & views ─────────────────────────────────────────────── */

.stage { flex: 1; min-width: 0; position: relative; }

.view {
  position: absolute;
  inset: 0;
  overflow-y: auto;
  padding: 34px 40px 60px;
}
.view[hidden] { display: none; }

.view-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 26px;
  max-width: 60rem;
}
.view-head h1 { font-size: 25px; font-weight: 600; letter-spacing: -.01em; }
.head-actions { display: flex; gap: 8px; }

.button {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 15px;
  border-radius: 7px;
  border: 1px solid var(--ink-4);
  background: var(--ink-2);
  cursor: pointer;
  color: var(--fg);
  transition: background .12s ease, border-color .12s ease;
}
.button:hover { background: var(--ink-3); border-color: var(--celadon-deep); }
.button.primary { background: var(--celadon-deep); border-color: var(--celadon-deep); color: #06110d; font-weight: 600; }
.button.primary:hover { background: var(--celadon); }
.button.quiet { background: none; border-color: transparent; color: var(--fg-dim); }
.button.quiet:hover { background: var(--ink-2); color: var(--fg); }
.button.danger:hover { border-color: var(--alert); color: var(--alert); }
.button[disabled] { opacity: .45; cursor: default; }

.icon-button {
  background: none; border: 0; cursor: pointer;
  color: var(--fg-dim); padding: 6px 9px; border-radius: 6px;
  font-size: 15px; line-height: 1;
}
.icon-button:hover { background: var(--ink-3); color: var(--fg); }

.empty { max-width: 34rem; color: var(--fg-dim); }
.empty-lead { color: var(--fg); font-size: 17px; margin-bottom: 6px; }

/* ── Bookshelf ─────────────────────────────────────────────────── */

.shelf-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(148px, 1fr));
  gap: 26px 22px;
  max-width: 76rem;
}

.book-card { position: relative; width: 100%; display: block; cursor: pointer; background: none; border: 0; padding: 0; text-align: left; }

.book-cover {
  aspect-ratio: 2 / 3;
  border-radius: 4px;
  background: linear-gradient(157deg, var(--ink-3), var(--ink-1));
  border: 1px solid var(--ink-4);
  overflow: hidden;
  display: flex;
  align-items: flex-end;
  padding: 12px;
  box-shadow: var(--shadow);
}
/* A cover fills its card edge to edge; the padding is only there to inset the
   text fallback when a book has no cover image. */
.book-cover.has-image { padding: 0; display: block; }
.book-cover img { display: block; width: 100%; height: 100%; object-fit: cover; }
.book-cover .cover-fallback {
  font-family: var(--serif);
  font-size: 17px;
  line-height: 1.35;
  color: var(--fg-dim);
  word-break: keep-all;
}

.book-meta { padding-top: 9px; }
.book-title { font-size: 13.5px; line-height: 1.35; word-break: keep-all; }
.book-author { font-size: 12px; color: var(--fg-faint); margin-top: 2px; }

.book-progress {
  height: 2px; margin-top: 7px; background: var(--ink-3); border-radius: 2px; overflow: hidden;
}
.book-progress span { display: block; height: 100%; background: var(--celadon-deep); }

.book-remove {
  position: absolute; top: 6px; right: 6px;
  background: rgba(6, 9, 13, .82); border: 0; color: var(--fg-dim);
  border-radius: 5px; width: 24px; height: 24px; cursor: pointer;
  opacity: 0; transition: opacity .12s ease;
}
.book-card:hover .book-remove, .book-remove:focus-visible { opacity: 1; }
.book-remove:hover { color: var(--alert); }

/* ── Search view ───────────────────────────────────────────────── */

.search-bar { max-width: 44rem; }
#search-input {
  width: 100%;
  padding: 13px 16px;
  font-size: 20px;
  background: var(--ink-1);
  border: 1px solid var(--ink-4);
  border-radius: 9px;
}
#search-input:focus { border-color: var(--celadon-deep); }
.search-note { color: var(--fg-faint); font-size: 13px; margin: 9px 0 22px; max-width: 44rem; }
.search-results { max-width: 44rem; }

/* Reuse the popup entry styling for search results. */
.search-results .entry { border-bottom: 1px solid var(--ink-2); padding: 16px 0; }
.search-results .entry:last-child { border-bottom: 0; }

/* ── Settings ──────────────────────────────────────────────────── */

.settings-body { max-width: 46rem; display: flex; flex-direction: column; gap: 34px; }

.panel { border-top: 1px solid var(--ink-3); padding-top: 18px; }
.panel > h2 { font-size: 16px; font-weight: 600; margin-bottom: 4px; }
.panel > .panel-note { color: var(--fg-faint); font-size: 13px; margin-bottom: 16px; max-width: 40rem; }

.field { display: flex; align-items: center; gap: 14px; padding: 9px 0; }
.field > label { flex: 0 0 13rem; color: var(--fg-dim); font-size: 13.5px; }
.field > .control { flex: 1; min-width: 0; display: flex; align-items: center; gap: 10px; }
.field .hint { color: var(--fg-faint); font-size: 12.5px; padding-left: calc(13rem + 14px); margin-top: -6px; }

input[type='text'], input[type='number'], input[type='url'], select, textarea {
  background: var(--ink-1);
  border: 1px solid var(--ink-4);
  border-radius: 6px;
  padding: 6px 10px;
  width: 100%;
}
input[type='range'] { flex: 1; accent-color: var(--celadon); }
input[type='checkbox'] { width: 16px; height: 16px; accent-color: var(--celadon); }
.range-value { color: var(--fg-dim); font-size: 13px; min-width: 3.4rem; text-align: right; font-variant-numeric: tabular-nums; }

.dict-row {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 0; border-bottom: 1px solid var(--ink-2);
}
.dict-row:last-of-type { border-bottom: 0; }
.dict-row .dict-info { flex: 1; min-width: 0; }
.dict-row .dict-name { font-size: 14.5px; }
.dict-row .dict-sub { color: var(--fg-faint); font-size: 12.5px; margin-top: 2px; }
.dict-row .warn { color: var(--amber); }

.import-progress { margin-top: 12px; }
.import-progress .bar { height: 3px; background: var(--ink-3); border-radius: 2px; overflow: hidden; }
.import-progress .bar span { display: block; height: 100%; background: var(--celadon); transition: width .2s ease; }
.import-progress .label { color: var(--fg-dim); font-size: 12.5px; margin-top: 6px; }

.field-templates { display: flex; flex-direction: column; gap: 8px; }
.field-template { display: flex; gap: 8px; align-items: center; }
.field-template input:first-child { flex: 0 0 11rem; }
.diag {
  margin-top: 12px; padding: 11px 13px;
  background: var(--ink-1); border: 1px solid var(--ink-4); border-radius: 7px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px; line-height: 1.6; color: var(--fg-dim);
  white-space: pre-wrap; word-break: break-all; max-height: 22rem; overflow: auto;
}
.diag[hidden] { display: none; }

.placeholder-legend { font-size: 12.5px; color: var(--fg-faint); line-height: 1.7; }
.placeholder-legend code { color: var(--celadon); background: var(--ink-2); padding: 1px 5px; border-radius: 4px; }

/* ── Reader ────────────────────────────────────────────────────── */

.reader-view { padding: 0; display: flex; flex-direction: column; background: var(--reader-bg, #faf7f0); overflow: hidden; }

.reader-bar {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 12px;
  background: var(--ink-1);
  border-bottom: 1px solid var(--ink-3);
  flex: 0 0 auto;
}
.reader-title { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.reader-title > span:first-child { font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.reader-chapter { font-size: 11.5px; color: var(--fg-faint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.reader-stage { flex: 1 1 auto; min-height: 0; height: 100%; position: relative; display: flex; justify-content: center; }
.reader-container {
  flex: 1 1 auto;
  min-width: 0;
  height: 100%;
  max-width: var(--reader-max-width, 46rem);
  padding: 0 var(--reader-gutter, 6%);
}
.reader-view[data-scheme='dark'] .page-edge:hover { background: linear-gradient(90deg, rgba(255,255,255,.06), transparent); }
.reader-view[data-scheme='dark'] .page-edge.right:hover { background: linear-gradient(270deg, rgba(255,255,255,.06), transparent); }
.reader-container iframe { border: 0; }

.page-edge {
  flex: 0 0 44px;
  border: 0; background: none; cursor: pointer;
  opacity: 0; transition: opacity .15s ease;
}
.page-edge:hover { opacity: 1; background: linear-gradient(90deg, rgba(0,0,0,.06), transparent); }
.page-edge.right:hover { background: linear-gradient(270deg, rgba(0,0,0,.06), transparent); }

.tap-catcher {
  position: absolute;
  inset: 0;
  z-index: 10;
  background: transparent;
  cursor: pointer;
  /* pan-y, not none: in continuous mode this element is a child of the
     scroller, and the browser must stay free to scroll it natively. */
  touch-action: pan-y;
  -webkit-tap-highlight-color: transparent;
}
.tap-catcher[data-mode='scrolled'] { inset: 0 0 auto 0; width: 100%; }
.tap-catcher[hidden] { display: none; }
.tap-catcher[data-debug='on'] {
  outline: 2px dashed var(--celadon);
  outline-offset: -2px;
  background: color-mix(in srgb, var(--celadon) 6%, transparent);
}

.page-pads { display: none; }

@media (max-width: 760px) {
  .page-pads {
    display: flex;
    justify-content: space-between;
    position: absolute;
    left: 0; right: 0; bottom: 10px;
    padding: 0 10px;
    pointer-events: none;
    z-index: 20;
  }
  .page-pad {
    pointer-events: auto;
    width: 46px; height: 46px;
    border-radius: 50%;
    border: 1px solid color-mix(in srgb, var(--reader-fg, #222) 18%, transparent);
    background: color-mix(in srgb, var(--reader-bg, #fff) 72%, transparent);
    color: color-mix(in srgb, var(--reader-fg, #222) 62%, transparent);
    font-size: 22px; line-height: 1;
    -webkit-backdrop-filter: blur(6px); backdrop-filter: blur(6px);
    cursor: pointer;
  }
  .page-pad:active { background: var(--reader-bg, #fff); }
}

.reader-foot {
  flex: 0 0 auto;
  display: flex; align-items: center; gap: 12px;
  padding: 7px 14px;
  background: var(--ink-1);
  border-top: 1px solid var(--ink-3);
}
.progress-track { flex: 1; height: 2px; background: var(--ink-3); border-radius: 2px; overflow: hidden; }
.progress-fill { height: 100%; width: 0; background: var(--celadon-deep); transition: width .25s ease; }
.progress-label { font-size: 11.5px; color: var(--fg-faint); font-variant-numeric: tabular-nums; min-width: 6rem; text-align: right; }

.drawer {
  position: absolute; top: 0; bottom: 0; left: 0;
  width: min(330px, 82vw);
  background: var(--ink-1);
  border-right: 1px solid var(--ink-3);
  display: flex; flex-direction: column;
  z-index: 40;
  box-shadow: var(--shadow);
}
.drawer.right { left: auto; right: 0; border-right: 0; border-left: 1px solid var(--ink-3); }
.drawer[hidden] { display: none; }
.drawer-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 14px; border-bottom: 1px solid var(--ink-3);
}
.drawer-head h2 { font-size: 14.5px; font-weight: 600; }
.drawer-body { padding: 14px; overflow-y: auto; }
.drawer-body .field > label { flex: 0 0 7.5rem; }

.toc-list { list-style: none; overflow-y: auto; padding: 6px 0 20px; }
.toc-list li a {
  display: block; padding: 8px 16px; color: var(--fg-dim);
  text-decoration: none; font-size: 13.5px; line-height: 1.4;
  border-left: 2px solid transparent; word-break: keep-all;
}
.toc-list li a:hover { background: var(--ink-2); color: var(--fg); }
.toc-list li a[aria-current] { color: var(--celadon); border-left-color: var(--celadon); }
.toc-list li ol { list-style: none; }
.toc-list li li a { padding-left: 30px; font-size: 13px; }

.theme-swatches { display: flex; gap: 7px; flex-wrap: wrap; }
.theme-swatch {
  width: 30px; height: 30px; border-radius: 6px; cursor: pointer;
  border: 2px solid transparent;
}
.theme-swatch[aria-pressed='true'] { border-color: var(--celadon); }

/* ── Lookup popup ──────────────────────────────────────────────── */

.hana-popup {
  position: fixed;
  z-index: 200;
  width: min(410px, calc(100vw - 16px));
  background: var(--ink-1);
  border: 1px solid var(--ink-4);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  font-family: var(--sans);
  font-size: 14px;
  color: var(--fg);
}
.hana-popup[hidden] { display: none; }

.popup-chrome {
  display: flex; align-items: center; gap: 2px;
  padding: 4px 5px;
  background: var(--ink-2);
  border-bottom: 1px solid var(--ink-3);
  flex: 0 0 auto;
}
.chrome-spacer { flex: 1; }
.chrome-button {
  background: none; border: 0; cursor: pointer; color: var(--fg-faint);
  width: 26px; height: 24px; border-radius: 5px; line-height: 1; font-size: 14px;
}
.chrome-button:hover { background: var(--ink-3); color: var(--fg); }
.chrome-button.back { visibility: hidden; }
.hana-popup.has-history .chrome-button.back { visibility: visible; }
.hana-popup.pinned .chrome-button.pin { color: var(--celadon); }

.popup-body { overflow-y: auto; padding: 4px 14px 14px; overscroll-behavior: contain; }

.entry + .entry { border-top: 1px solid var(--ink-2); margin-top: 12px; padding-top: 12px; }

.entry-header { display: flex; align-items: flex-start; gap: 8px; padding-top: 10px; }
.expression-scroll { flex: 1; min-width: 0; overflow-x: auto; scrollbar-width: none; }
.expression-scroll::-webkit-scrollbar { display: none; }
.expression {
  font-family: var(--serif);
  font-size: 25px;
  line-height: 1.25;
  white-space: nowrap;
  letter-spacing: -.01em;
}
.expression .reading { font-size: 15px; color: var(--fg-dim); }

.entry-actions { display: flex; gap: 4px; flex: 0 0 auto; padding-top: 3px; }
.entry-action {
  width: 27px; height: 27px; border-radius: 6px; cursor: pointer;
  border: 1px solid var(--ink-4); background: var(--ink-2);
  color: var(--fg-dim); font-size: 15px; line-height: 1;
}
.entry-action:hover { border-color: var(--amber); color: var(--amber); }
.entry-action.added { border-color: var(--celadon-deep); color: var(--celadon); }
.entry-action.duplicate { opacity: .4; }
.entry-action.audio.playing { border-color: var(--celadon); color: var(--celadon); }
.entry-action.audio.loading { border-color: var(--celadon-deep); }
.entry-action.audio.loading::after {
  content: ''; position: absolute; inset: 4px;
  border: 1.5px solid transparent; border-top-color: var(--celadon);
  border-radius: 50%; animation: hana-spin .7s linear infinite;
}
.entry-action.audio.loading { position: relative; color: transparent; }
@keyframes hana-spin { to { transform: rotate(360deg); } }
.entry-action.audio.none { opacity: .35; }
.audio-menu {
  display: flex; flex-wrap: wrap; gap: 5px; margin-top: 8px;
}
.audio-menu button {
  font-size: 11.5px; padding: 2px 8px; border-radius: 4px; cursor: pointer;
  border: 1px solid var(--ink-4); background: var(--ink-2); color: var(--fg-dim);
}
.audio-menu button:hover, .audio-menu button.playing { border-color: var(--celadon); color: var(--celadon); }
.entry-action.failed { border-color: var(--alert); color: var(--alert); }

.pronunciation {
  display: flex; flex-wrap: wrap; gap: 10px;
  font-family: var(--serif);
  font-size: 14.5px;
  color: var(--celadon);
  margin-top: 3px;
}
.pronunciation[hidden] { display: none; }
.pronunciation .lemma { color: var(--fg-faint); }

.entry-tags { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 8px; }
.matched-tag,
.deinflection-tag,
.frequency-tag {
  font-size: 11.5px;
  line-height: 1.5;
  border-radius: 4px;
  padding: 1px 7px;
  border: 1px solid var(--ink-4);
  color: var(--fg-dim);
}
.matched-tag { font-family: var(--serif); color: var(--fg); background: var(--ink-3); border-color: var(--ink-3); }
.deinflection-tag { color: var(--celadon); border-color: var(--celadon-deep); }
.frequency-tag { display: inline-flex; gap: 0; padding: 0; overflow: hidden; }
.frequency-tag .frequency-dict { padding: 1px 6px; background: var(--ink-3); color: var(--fg-faint); }
.frequency-tag .frequency-value { padding: 1px 7px; font-variant-numeric: tabular-nums; }

.glossary-group { margin-top: 12px; }
.glossary-group > summary {
  cursor: pointer;
  list-style: none;
  font-size: 11.5px;
  color: var(--fg-faint);
  padding-bottom: 5px;
  border-bottom: 1px solid var(--ink-2);
  display: flex; align-items: center; gap: 6px;
}
.glossary-group > summary::-webkit-details-marker { display: none; }
.glossary-group > summary::before {
  content: '▾'; font-size: 9px; color: var(--ink-4); transition: transform .12s ease;
}
.glossary-group:not([open]) > summary::before { transform: rotate(-90deg); }

.glossary-content { padding-top: 7px; }
.glossary-list { padding-left: 1.45em; margin: 0; }
/* Dictionary structured content brings its own lists; the global reset strips
   their indent, which pushed KRDICT's sense numbers outside the popup. */
.glossary-content ol, .glossary-content ul { padding-left: 1.45em; margin: 2px 0; }
.glossary-content li { margin: 2px 0; }
.glossary-list[data-count='1'] { list-style: none; padding-left: 0; }
.glossary-item { margin: 3px 0; line-height: 1.55; }
.glossary-item::marker { color: var(--fg-faint); font-size: 12px; }

.glossary-tags { display: flex; flex-wrap: wrap; gap: 4px; margin: 5px 0; }
.glossary-tag {
  font-size: 11px; padding: 0 6px; border-radius: 3px;
  background: var(--ink-3); color: var(--fg-dim);
}
.glossary-tag[data-category='partOfSpeech'] { color: var(--celadon); }

.lookup-text { cursor: pointer; }

.sc-table-wrap { overflow-x: auto; margin: 6px 0; }
.sc-table { border-collapse: collapse; font-size: 13px; }
.sc-table td, .sc-table th { border: 1px solid var(--ink-3); padding: 3px 7px; text-align: left; }
.sc-image { display: inline-block; max-width: 100%; }
.sc-image img { max-width: 100%; border-radius: 4px; }
.sc-image[data-appearance='monochrome'] img { filter: grayscale(1) invert(.85); }
.sc-image-caption { display: block; font-size: 11.5px; color: var(--fg-faint); }
.sc-link { color: var(--celadon); }

.low-confidence {
  margin-top: 10px; padding: 9px 11px;
  border: 1px dashed var(--ink-4); border-radius: 7px;
  color: var(--fg-dim); font-size: 12.5px; line-height: 1.5;
}
.low-confidence b { color: var(--fg); font-family: var(--serif); font-weight: 400; }

.popup-empty { padding: 16px 2px 6px; }
.popup-empty-title { font-size: 15px; margin-bottom: 5px; }
.popup-empty-detail { color: var(--fg-dim); font-size: 13px; line-height: 1.55; }

/* ── Toast ─────────────────────────────────────────────────────── */

.diag-log {
  position: fixed; left: 8px; width: min(420px, calc(100vw - 16px)); top: 8px;
  max-height: 30vh; overflow-y: auto; z-index: 400;
  opacity: .93;
  background: rgba(8, 11, 15, .93);
  border: 1px solid var(--celadon-deep); border-radius: 8px;
  padding: 8px 10px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11px; line-height: 1.5; color: #cfe6dd;
  white-space: pre-wrap; word-break: break-word;
}
.diag-log[hidden] { display: none; }
.diag-log .diag-close {
  position: sticky; top: 0; float: right;
  background: none; border: 0; color: var(--celadon); cursor: pointer; font-size: 14px;
}

.toast {
  position: fixed; left: 50%; bottom: 26px; transform: translateX(-50%);
  background: var(--ink-3); border: 1px solid var(--ink-4);
  padding: 9px 16px; border-radius: 8px; z-index: 300;
  font-size: 13.5px; box-shadow: var(--shadow); max-width: 32rem;
}
.toast[hidden] { display: none; }
.toast.error { border-color: var(--alert); color: #f3d6d1; }

/* ── Phones ────────────────────────────────────────────────────── */

.install-prompt {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(env(safe-area-inset-bottom, 0px) + 76px);
  background: var(--celadon-deep); color: #06110d; font-weight: 600;
  border: 0; border-radius: 999px; padding: 10px 20px; cursor: pointer;
  z-index: 250; box-shadow: var(--shadow);
}
.install-prompt[hidden] { display: none; }
@media (min-width: 761px) { .install-prompt { display: none !important; } }

@media (max-width: 760px) {
  body { flex-direction: column-reverse; }

  .rail {
    width: 100%; flex: 0 0 auto; flex-direction: row;
    border-right: 0; border-top: 1px solid var(--ink-3);
    padding: 0 0 env(safe-area-inset-bottom, 0px); align-items: stretch;
  }
  .rail-mark, .rail-fill { display: none; }
  .rail-tab { flex: 1; border-left: 0; border-top: 2px solid transparent; padding: 9px 4px 7px; }
  .rail-tab[aria-current='page'] { border-left-color: transparent; border-top-color: var(--celadon); }

  .view { padding: calc(env(safe-area-inset-top, 0px) + 18px) 16px 32px; }
  .reader-view { padding: 0; }
  .view-head { flex-direction: column; align-items: flex-start; gap: 12px; margin-bottom: 18px; }
  .view-head h1 { font-size: 21px; }

  .shelf-grid { grid-template-columns: repeat(auto-fill, minmax(112px, 1fr)); gap: 20px 14px; }
  .book-remove { opacity: 1; }

  .field { flex-direction: column; align-items: flex-start; gap: 6px; }
  .field > label { flex: none; }
  .field > .control { width: 100%; }
  .field .hint { padding-left: 0; }
  .field-template { flex-direction: column; align-items: stretch; }
  .field-template input:first-child { flex: none; }

  .page-edge { display: none; }
  .reader-bar { padding-top: calc(env(safe-area-inset-top, 0px) + 8px); }
  .reader-container { padding: 0 var(--reader-gutter, 5%); }

  .drawer { width: 100%; padding-bottom: env(safe-area-inset-bottom, 0px); }
  .drawer-head { padding-top: calc(env(safe-area-inset-top, 0px) + 12px); }

  /* The popup becomes a bottom sheet. An anchored 410px panel has nowhere to
     go on a phone: it covers the word it is defining, and half of it lands
     off-screen. A sheet always opens from the bottom, never hides the line
     being read, and can be flicked away. */
  .hana-popup {
    left: 0 !important;
    right: 0;
    top: auto !important;
    bottom: 0;
    width: 100% !important;
    max-height: 62dvh !important;
    border-radius: 16px 16px 0 0;
    border-width: 1px 0 0;
    padding-bottom: env(safe-area-inset-bottom, 0px);
    transform: translateY(0);
    transition: transform .18s ease-out;
  }
  .hana-popup[data-sheet='closing'] { transform: translateY(100%); }
  .popup-chrome {
    padding: 8px 10px;
    position: relative;
  }
  .popup-chrome::before {
    content: '';
    position: absolute; left: 50%; top: 5px; transform: translateX(-50%);
    width: 36px; height: 4px; border-radius: 2px; background: var(--ink-4);
  }
  .chrome-button { width: 34px; height: 30px; font-size: 16px; }
  .popup-body { padding: 6px 16px 18px; }
  .expression { font-size: 27px; }
  .entry-action { width: 32px; height: 32px; }
  .diag-log {
  position: fixed; left: 8px; width: min(420px, calc(100vw - 16px)); top: 8px;
  max-height: 30vh; overflow-y: auto; z-index: 400;
  opacity: .93;
  background: rgba(8, 11, 15, .93);
  border: 1px solid var(--celadon-deep); border-radius: 8px;
  padding: 8px 10px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11px; line-height: 1.5; color: #cfe6dd;
  white-space: pre-wrap; word-break: break-word;
}
.diag-log[hidden] { display: none; }
.diag-log .diag-close {
  position: sticky; top: 0; float: right;
  background: none; border: 0; color: var(--celadon); cursor: pointer; font-size: 14px;
}

.toast { bottom: calc(env(safe-area-inset-bottom, 0px) + 70px); left: 16px; right: 16px; transform: none; max-width: none; }
}

@media (display-mode: standalone) {
  .install-prompt { display: none !important; }
}
