:root {
  --bg: #FFFFFF;
  --card: #F6F8F2;
  --line: #E3E9DA;
  --track: #EDF1E7;
  --ink: #2E2A26;
  --ink-2: #3D3732;
  --muted: #6F655C;
  --faint: #8C8279;
  --sage: #4F6447;
  --sage-soft: #E4EBDD;
  --print: #4E7A70;
  --print-bar: #6E958B;
  --audio: #9E5838;
  --audio-bar: #D3A06A;
  --terracotta: #9E5838;
  --danger: #9C4F3A;
  --chip-genre-bg: #F3E3D6; --chip-genre-fg: #8A4A2E;
  --chip-mood-bg: #E4EBDD; --chip-mood-fg: #4F6447;
  --chip-tag-bg: #E2ECE9; --chip-tag-fg: #355B54;
  --chip-theme-bg: #EFE8DD; --chip-theme-fg: #5E554D;
  --nav-h: 64px;
  --top: env(safe-area-inset-top, 0px);
  --bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
.result .cover-fallback { padding: 5px 4px; }
.result .cover-fallback b { font-size: 8px; }
.result .cover-fallback i { font-size: 6px; }
html, body { margin: 0; background: var(--bg); color: var(--ink); }
body {
  font-family: 'Nunito Sans', system-ui, -apple-system, sans-serif;
  font-size: 16px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
input, select, textarea { font-size: 16px; }
a { color: var(--print); }
h1, h2, h3 { font-weight: 600; margin: 0; letter-spacing: -0.01em; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.boot { padding: calc(var(--top) + 80px) 24px; text-align: center; color: var(--muted); }

/* Page shell */
.page { min-height: 100vh; padding: calc(var(--top) + 20px) 20px calc(var(--nav-h) + var(--bottom) + 32px); max-width: 640px; margin: 0 auto; }
.page.no-nav { padding-bottom: calc(var(--bottom) + 32px); }
.page-title { font-size: 28px; color: var(--sage); }
.page-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 14px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.stack { display: flex; flex-direction: column; gap: 14px; }
.row { display: flex; gap: 10px; align-items: center; }
.spacer { flex: 1; }

/* Bottom nav */
.nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  height: calc(var(--nav-h) + var(--bottom)); padding: 8px 8px var(--bottom);
  background: var(--card); border-top: 1px solid var(--line);
  display: grid; grid-template-columns: repeat(4, minmax(0, 1fr));
}
.nav a { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; text-decoration: none; color: var(--muted); font-size: 12px; font-weight: 600; }
.nav a[aria-current="page"] { color: var(--sage); }
.nav svg { width: 24px; height: 24px; }

/* Buttons */
.btn { min-height: 44px; padding: 0 18px; border-radius: 22px; border: 1px solid var(--line); background: var(--bg); font-weight: 600; font-size: 15px; display: inline-flex; align-items: center; justify-content: center; gap: 6px; }
.btn.primary { background: var(--terracotta); border-color: var(--terracotta); color: #FFFFFF; }
.btn.big { width: 100%; min-height: 54px; border-radius: 27px; font-size: 17px; }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: 0.6; }
.icon-btn { width: 44px; height: 44px; border-radius: 22px; border: 1px solid var(--line); background: var(--card); display: inline-flex; align-items: center; justify-content: center; flex: none; padding: 0; }
.icon-btn svg { width: 20px; height: 20px; }
.link-btn { min-height: 44px; border: 0; background: transparent; padding: 0; color: var(--print); font-weight: 600; font-size: 15px; text-align: left; }
.back { display: inline-flex; align-items: center; gap: 2px; min-height: 44px; border: 0; background: transparent; padding: 0; color: var(--muted); font-weight: 600; text-decoration: none; }
.back svg { width: 20px; height: 20px; }
.fab { position: fixed; right: max(20px, calc(50vw - 300px)); bottom: calc(var(--nav-h) + var(--bottom) + 18px); z-index: 15; width: 58px; height: 58px; border-radius: 29px; border: 0; background: var(--terracotta); color: #FFFFFF; display: flex; align-items: center; justify-content: center; box-shadow: 0 4px 12px rgba(46,42,38,0.25); text-decoration: none; }
.fab svg { width: 26px; height: 26px; }

/* Inputs */
.search { display: flex; align-items: center; gap: 8px; min-height: 44px; padding: 0 14px; background: var(--card); border: 1px solid var(--line); border-radius: 22px; flex: 1; min-width: 0; }
.search svg { width: 18px; height: 18px; flex: none; color: var(--muted); }
.search input { flex: 1; min-width: 0; border: 0; background: transparent; outline: none; padding: 10px 0; }
.search.focus { border: 2px solid var(--print); }
.field { display: grid; grid-template-columns: 108px minmax(0, 1fr); gap: 10px; align-items: center; }
.field > span { font-size: 14px; color: var(--muted); }
.input, .textarea, .select { width: 100%; min-height: 44px; padding: 10px 12px; border-radius: 12px; border: 1px solid var(--line); background: var(--card); font-weight: 600; }
.textarea { min-height: 120px; font-weight: 400; line-height: 1.5; resize: vertical; }
.input:focus, .textarea:focus, .select:focus { outline: 2px solid var(--print); outline-offset: -1px; }
.hm { display: flex; gap: 8px; align-items: center; }
.hm .input { width: 72px; }
.seg { padding: 3px; border-radius: 12px; background: var(--track); display: grid; grid-auto-flow: column; grid-auto-columns: minmax(0, 1fr); gap: 3px; }
.seg button { min-height: 38px; border: 0; border-radius: 9px; background: transparent; font-weight: 600; font-size: 14px; color: var(--muted); }
.seg button[aria-pressed="true"] { background: var(--bg); color: var(--sage); box-shadow: 0 1px 3px rgba(46,42,38,0.12); }
.label { font-size: 12px; font-weight: 600; letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted); }
.error { color: var(--danger); font-size: 14px; }

/* Library */
.sortline { display: flex; justify-content: space-between; align-items: center; font-size: 13px; color: var(--muted); margin: 12px 0 10px; gap: 8px; }
.sortline strong { color: var(--ink); font-weight: 600; }
.chips-active { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
.grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; }
.cover-tile { position: relative; display: block; aspect-ratio: 2 / 3; border-radius: 6px; overflow: hidden; box-shadow: 0 2px 6px rgba(46,42,38,0.18); text-decoration: none; background: var(--track); }
.cover-tile img, .cover img { width: 100%; height: 100%; object-fit: cover; display: block; }
.cover-fallback { width: 100%; height: 100%; padding: 12px 10px; display: flex; flex-direction: column; justify-content: space-between; }
.cover-fallback b { font-weight: 600; font-size: 14px; line-height: 1.15; }
.cover-fallback i { font-style: normal; font-size: 10px; letter-spacing: 0.06em; text-transform: uppercase; opacity: 0.85; }
.corner { position: absolute; top: 6px; right: 6px; width: 24px; height: 24px; border-radius: 12px; background: #FFFFFF; display: flex; align-items: center; justify-content: center; box-shadow: 0 1px 3px rgba(0,0,0,0.25); }
.corner svg { width: 12px; height: 12px; }
.section-rule { display: flex; align-items: center; gap: 10px; margin: 22px 0 10px; }
.section-rule span { font-size: 17px; font-weight: 600; color: var(--sage); }
.section-rule::after { content: ''; flex: 1; height: 1px; background: var(--line); }
.empty { text-align: center; padding: 48px 12px; color: var(--muted); }
.empty h2 { color: var(--ink); font-size: 19px; margin-bottom: 6px; }

/* Chips */
.chip { display: inline-flex; align-items: center; gap: 4px; padding: 6px 12px; border-radius: 14px; font-size: 14px; font-weight: 600; border: 0; }
.chip.genres { background: var(--chip-genre-bg); color: var(--chip-genre-fg); }
.chip.moods { background: var(--chip-mood-bg); color: var(--chip-mood-fg); }
.chip.tags { background: var(--chip-tag-bg); color: var(--chip-tag-fg); }
.chip.themes, .chip.plain { background: var(--chip-theme-bg); color: var(--chip-theme-fg); }
.chip.toggle { background: var(--bg); border: 1px solid var(--line); color: var(--ink); min-height: 36px; }
.chip.toggle[aria-pressed="true"] { background: var(--sage); border-color: var(--sage); color: #FFFFFF; }
.chip .x { width: 20px; height: 20px; border: 0; background: transparent; padding: 0; margin-right: -6px; display: inline-flex; align-items: center; justify-content: center; color: inherit; opacity: 0.8; }
.chip-row { display: flex; flex-wrap: wrap; gap: 6px; }

/* Pick list */
.pick { display: flex; flex-direction: column; gap: 6px; }
.pick-box { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; min-height: 44px; padding: 5px 8px; border-radius: 12px; border: 1px solid var(--line); background: var(--card); }
.pick-box input { flex: 1; min-width: 120px; border: 0; background: transparent; outline: none; padding: 6px 4px; }
.pick-menu { border: 1px solid var(--line); border-radius: 12px; background: var(--bg); box-shadow: 0 6px 18px rgba(46,42,38,0.12); max-height: 240px; overflow-y: auto; }
.pick-menu button { display: block; width: 100%; text-align: left; min-height: 44px; padding: 10px 14px; border: 0; border-bottom: 1px solid var(--track); background: transparent; }
.pick-menu button:last-child { border-bottom: 0; }
.pick-menu .add { color: var(--print); font-weight: 600; }

/* Sheet */
.sheet-bg { position: fixed; inset: 0; z-index: 40; background: rgba(46,42,38,0.35); display: flex; align-items: flex-end; justify-content: center; }
.sheet { width: 100%; max-width: 640px; max-height: 88vh; overflow-y: auto; background: var(--bg); border-radius: 24px 24px 0 0; padding: 10px 20px calc(var(--bottom) + 24px); display: flex; flex-direction: column; gap: 18px; }
.grabber { align-self: center; width: 40px; height: 5px; border-radius: 3px; background: var(--line); flex: none; }
.sheet h2 { font-size: 22px; color: var(--sage); }
.filter-group { display: flex; flex-direction: column; gap: 8px; }

/* Add book */
.result { display: flex; gap: 12px; align-items: center; width: 100%; padding: 12px 0; border: 0; border-bottom: 1px solid var(--line); background: transparent; text-align: left; }
.result .cover { width: 46px; height: 68px; flex: none; border-radius: 4px; overflow: hidden; box-shadow: 0 1px 3px rgba(46,42,38,0.2); background: var(--track); }
.result-text { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.result-text b { font-weight: 600; }
.result svg { width: 18px; height: 18px; color: var(--muted); flex: none; }
.confirm-top { display: flex; gap: 16px; align-items: flex-end; }
.confirm-cover { flex: none; display: flex; flex-direction: column; align-items: center; gap: 8px; }
.confirm-cover .cover { width: 104px; height: 156px; border-radius: 6px; overflow: hidden; box-shadow: 0 4px 12px rgba(46,42,38,0.2); background: var(--track); }
.collapse { display: flex; justify-content: space-between; align-items: center; width: 100%; min-height: 56px; padding: 0 14px; border-radius: 14px; border: 1px solid var(--line); background: var(--bg); text-align: left; }
.collapse svg { width: 18px; height: 18px; color: var(--muted); transition: transform 0.15s; }
.collapse[aria-expanded="true"] svg { transform: rotate(180deg); }

/* Book page */
.book-head { display: flex; gap: 16px; align-items: flex-start; margin: 4px 0 18px; }
.book-head .cover { flex: none; width: 118px; height: 177px; border-radius: 6px; overflow: hidden; box-shadow: 0 4px 12px rgba(46,42,38,0.25); background: var(--track); }
.book-head h1 { font-size: 23px; line-height: 1.15; }
.badge { display: inline-flex; align-self: flex-start; padding: 4px 10px; border-radius: 12px; background: var(--sage-soft); color: var(--sage); font-size: 13px; font-weight: 600; }
.tabs { padding: 4px; border-radius: 22px; background: var(--track); display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 4px; margin-bottom: 20px; }
.tabs button { min-height: 38px; border: 0; border-radius: 18px; background: transparent; font-weight: 600; font-size: 14px; color: var(--muted); }
.tabs button[aria-selected="true"] { background: var(--bg); color: var(--sage); box-shadow: 0 1px 3px rgba(46,42,38,0.15); }
.info-row { display: grid; grid-template-columns: 104px minmax(0, 1fr); gap: 12px; padding: 9px 0; border-bottom: 1px solid var(--track); font-size: 15px; }
.info-row span:first-child { color: var(--muted); }
.info-row span:last-child { font-weight: 600; }
.h2 { font-size: 19px; color: var(--sage); }
.synopsis { white-space: pre-line; color: var(--ink-2); line-height: 1.55; margin: 0; }
.soon { background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 22px 18px; text-align: center; color: var(--muted); }
.soon h2 { color: var(--ink); font-size: 18px; margin-bottom: 6px; }

/* Settings + sign-in */
.card { background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.signin { max-width: 400px; margin: 0 auto; padding: calc(var(--top) + 64px) 24px 40px; display: flex; flex-direction: column; gap: 16px; }
.signin h1 { font-size: 30px; color: var(--sage); }
.signin label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--muted); font-weight: 600; }

/* Toast */
#toast { position: fixed; left: 50%; bottom: calc(var(--nav-h) + var(--bottom) + 90px); transform: translateX(-50%); z-index: 60; background: var(--ink); color: #FFFFFF; padding: 10px 16px; border-radius: 20px; font-size: 14px; font-weight: 600; opacity: 0; pointer-events: none; transition: opacity 0.2s; max-width: 90vw; text-align: center; }
#toast.show { opacity: 1; }
