:root {
  --navy: #2E4053;
  --navy-deep: #223142;
  --navy-soft: #3B5169;
  --peach: #FFDAB9;
  --peach-deep: #F2B98A;
  --bg: #F5F1EA;
  --surface: #FFFFFF;
  --surface-2: #FAF7F2;
  --ink: #1F2A36;
  --ink-2: #4A5866;
  --muted: #6B7785;
  --line: #E4DED4;
  --grid: #ECE7DF;
  --danger: #B3402F;
  --good: #2F7A55;
  --focus: #E58E4A;
  --shadow: 0 1px 2px rgba(31, 42, 54, .06), 0 8px 24px rgba(31, 42, 54, .06);
  --serif: "Libre Baskerville", "Book Antiqua", Palatino, Georgia, serif;
  --sans: Inter, system-ui, -apple-system, "Segoe UI", sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #141B23; --surface: #1C252F; --surface-2: #222D39;
    --ink: #EEF1F4; --ink-2: #C3CBD4; --muted: #95A1AE;
    --line: #2F3B48; --grid: #2A3542; --navy-soft: #6F8BA8;
    --danger: #E07A68; --good: #6CC49A;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 8px 24px rgba(0,0,0,.25);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #141B23; --surface: #1C252F; --surface-2: #222D39;
  --ink: #EEF1F4; --ink-2: #C3CBD4; --muted: #95A1AE;
  --line: #2F3B48; --grid: #2A3542; --navy-soft: #6F8BA8;
  --danger: #E07A68; --good: #6CC49A;
  --shadow: 0 1px 2px rgba(0,0,0,.3), 0 8px 24px rgba(0,0,0,.25);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { font-family: var(--sans); background: var(--bg); color: var(--ink); line-height: 1.5; -webkit-font-smoothing: antialiased; }
[hidden] { display: none !important; }
h1, h2 { font-family: var(--serif); font-weight: 700; margin: 0; letter-spacing: -.01em; }
button, input { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.muted { color: var(--muted); margin: .25rem 0 0; }

/* Brand mark: an open book */
.brand-mark { width: 34px; height: 34px; border-radius: 9px; background: var(--peach); position: relative; flex: none; }
.brand-mark::before, .brand-mark::after { content: ""; position: absolute; top: 9px; width: 9px; height: 16px; background: var(--navy); }
.brand-mark::before { left: 7px; border-radius: 2px 0 0 5px; }
.brand-mark::after { right: 7px; border-radius: 0 2px 5px 0; }
.brand-mark.big { width: 64px; height: 64px; border-radius: 16px; }
.brand-mark.big::before, .brand-mark.big::after { top: 16px; width: 17px; height: 32px; }
.brand-mark.big::before { left: 13px; } .brand-mark.big::after { right: 13px; }

/* Splash */
.splash { position: fixed; inset: 0; display: grid; place-items: center; background: var(--navy); color: #fff; z-index: 50; transition: opacity .4s ease; }
.splash.out { opacity: 0; pointer-events: none; }
.splash-inner { text-align: center; padding: 24px; width: min(420px, 100%); }
.splash-inner .brand-mark { margin: 0 auto 18px; width: 56px; height: 56px; border-radius: 14px; }
.splash-inner .brand-mark::before, .splash-inner .brand-mark::after { top: 14px; width: 15px; height: 28px; }
.splash-inner .brand-mark::before { left: 11px; } .splash-inner .brand-mark::after { right: 11px; }
.splash-title { font-size: clamp(28px, 6vw, 40px); }
.splash-tag { color: var(--peach); margin: 8px 0 28px; font-family: var(--serif); font-style: italic; }
.splash-bar { height: 6px; background: rgba(255,255,255,.15); border-radius: 99px; overflow: hidden; }
.splash-bar span { display: block; height: 100%; width: 0; background: var(--peach); border-radius: 99px; animation: load 1.4s ease-in-out forwards; }
@keyframes load { to { width: 100%; } }

/* Auth */
.auth { min-height: 100vh; display: grid; grid-template-columns: 1fr 1fr; }
.auth-art { background: var(--navy); color: #fff; display: grid; place-items: center; padding: 40px; position: relative; overflow: hidden; }
.auth-art::after { content: ""; position: absolute; inset: auto -20% -30% auto; width: 70%; aspect-ratio: 1; border-radius: 50%; background: radial-gradient(circle, rgba(255,218,185,.18), transparent 70%); }
.auth-art-inner { position: relative; z-index: 1; }
.auth-art-title { font-family: var(--serif); font-size: 56px; font-weight: 700; margin: 20px 0 4px; }
.auth-art-tag { color: var(--peach); font-family: var(--serif); font-style: italic; font-size: 18px; margin: 0; }
.auth-panel { display: flex; flex-direction: column; justify-content: center; padding: 40px clamp(16px, 6vw, 80px); background: var(--surface); }
.auth-form { display: grid; gap: 14px; width: min(380px, 100%); }
.auth-form h2 { font-size: 28px; }
.switch { margin: 4px 0 0; color: var(--muted); font-size: 14px; }
.demo-note { margin-top: 28px; font-size: 13px; color: var(--muted); max-width: 380px; }

label { display: grid; gap: 6px; font-size: 14px; font-weight: 600; color: var(--ink-2); }
input:not([type=file]) { width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface-2); font-weight: 400; color: var(--ink); min-height: 44px; }
input:focus { border-color: var(--navy-soft); outline: none; box-shadow: 0 0 0 3px rgba(229, 142, 74, .25); }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 44px; padding: 0 18px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); font-weight: 600; cursor: pointer; transition: background .15s, border-color .15s, transform .05s; }
.btn:hover { border-color: var(--navy-soft); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--navy); color: #fff; border-color: var(--navy); }
.btn.primary:hover { background: var(--navy-deep); }
.btn.danger { color: var(--danger); }
.btn.danger:hover { border-color: var(--danger); }
.btn.ghost { background: transparent; }
.btn.small { min-height: 36px; padding: 0 12px; font-size: 14px; }
.link { background: none; border: 0; padding: 0; color: var(--navy-soft); font-weight: 600; cursor: pointer; text-decoration: underline; text-underline-offset: 3px; }
.btn-row { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 4px; }
.btn-row.end { justify-content: flex-end; }

/* App shell */
.app { display: grid; grid-template-columns: 240px 1fr; min-height: 100vh; }
.sidebar { background: var(--navy); color: #fff; padding: 22px 14px; display: flex; flex-direction: column; gap: 22px; position: sticky; top: 0; height: 100vh; }
.side-brand { display: flex; align-items: center; gap: 10px; font-family: var(--serif); font-weight: 700; font-size: 18px; padding: 0 8px; }
.side-nav { display: grid; gap: 4px; }
.nav-btn { display: flex; align-items: center; gap: 12px; width: 100%; padding: 11px 12px; border: 0; border-radius: 10px; background: transparent; color: rgba(255,255,255,.82); font-weight: 600; cursor: pointer; text-align: left; }
.nav-btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.nav-btn:hover { background: rgba(255,255,255,.08); color: #fff; }
.nav-btn.active { background: var(--peach); color: var(--navy); }
.logout { margin-top: auto; }

.main { padding: 32px clamp(16px, 4vw, 48px) 24px; min-width: 0; }
.page-head { margin-bottom: 24px; }
.page-head h1 { font-size: clamp(26px, 4vw, 34px); }
.eyebrow { margin: 0 0 4px; color: var(--peach-deep); font-weight: 700; text-transform: uppercase; letter-spacing: .08em; font-size: 12px; }
.section-title { font-size: 20px; margin: 32px 0 14px; }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: 16px; box-shadow: var(--shadow); padding: 20px; min-width: 0; }
.split { display: grid; grid-template-columns: minmax(280px, 380px) 1fr; gap: 20px; align-items: start; }
.form { display: grid; gap: 14px; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.search-row { display: flex; gap: 8px; }
.search-row input { flex: 1; }

.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: 16px; padding: 18px; box-shadow: var(--shadow); display: grid; gap: 2px; }
.stat-num { font-family: var(--serif); font-size: 32px; font-weight: 700; color: var(--ink); font-variant-numeric: tabular-nums; }
.stat-label { color: var(--muted); font-size: 14px; }
.tiles { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; margin-top: 14px; }
.tile { text-align: left; padding: 18px; border-radius: 16px; border: 1px solid var(--line); background: var(--surface-2); cursor: pointer; display: grid; gap: 4px; transition: border-color .15s, transform .15s; }
.tile:hover { border-color: var(--navy-soft); transform: translateY(-2px); }
.tile strong { font-family: var(--serif); font-size: 17px; color: var(--ink); }
.tile span { color: var(--muted); font-size: 14px; }

.shelf { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 18px; }
.book { display: grid; gap: 8px; }
.book-cover { aspect-ratio: 2 / 3; border-radius: 6px 10px 10px 6px; overflow: hidden; background: var(--navy); box-shadow: inset 4px 0 0 rgba(0,0,0,.18), var(--shadow); display: grid; place-items: center; color: var(--peach); font-family: var(--serif); text-align: center; padding: 0; }
.book-cover img { width: 100%; height: 100%; object-fit: cover; display: block; }
.book-cover .ph { padding: 12px; font-size: 14px; line-height: 1.3; }
.book-title { font-weight: 600; font-size: 14px; line-height: 1.3; }
.book-meta { color: var(--muted); font-size: 13px; }

.cover-pick { display: flex; align-items: end; gap: 14px; }
.cover-preview { width: 96px; aspect-ratio: 2 / 3; border-radius: 6px 10px 10px 6px; background: var(--surface-2); border: 1px dashed var(--line); display: grid; place-items: center; overflow: hidden; color: var(--muted); font-size: 12px; flex: none; }
.cover-preview img { width: 100%; height: 100%; object-fit: cover; }
.cover-preview.small { width: 72px; }
.file-btn { position: relative; overflow: hidden; }
.file-btn input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }

.found { display: flex; gap: 14px; align-items: start; padding: 12px; background: var(--surface-2); border-radius: 12px; }
.found dl { margin: 0; display: grid; grid-template-columns: auto 1fr; gap: 2px 12px; font-size: 14px; }
.found dt { color: var(--muted); }
.found dd { margin: 0; font-weight: 600; overflow-wrap: anywhere; }

.table-card { padding: 0; overflow: hidden; }
.table-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 16px 20px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.table-head h2 { font-size: 18px; }
.table-head input { width: min(220px, 100%); min-height: 38px; padding: 6px 12px; }
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th { text-align: left; font-weight: 600; color: var(--muted); padding: 10px 16px; background: var(--surface-2); white-space: nowrap; }
.table td { padding: 11px 16px; border-top: 1px solid var(--line); white-space: nowrap; }
.table .num { text-align: right; font-variant-numeric: tabular-nums; }
.table tbody tr { cursor: pointer; }
.table tbody tr:hover { background: var(--surface-2); }
.table tbody tr.selected { background: rgba(255, 218, 185, .35); }
.table .empty td { text-align: center; color: var(--muted); padding: 28px; cursor: default; }
.tag-late { color: var(--danger); font-weight: 600; white-space: nowrap; }
.tag-late::before { content: "! "; }

/* Chart */
.chart-card { position: relative; }
.chart-tools { display: flex; justify-content: flex-end; margin-bottom: 8px; }
.seg { display: inline-flex; background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px; padding: 3px; }
.seg-btn { border: 0; background: transparent; padding: 6px 14px; border-radius: 8px; font-weight: 600; font-size: 14px; color: var(--muted); cursor: pointer; }
.seg-btn.active { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }
.chart-box svg { width: 100%; height: auto; display: block; overflow: visible; }
.chart-box .grid { stroke: var(--grid); stroke-width: 1; }
.chart-box .base { stroke: var(--line); stroke-width: 1; }
.chart-box .axis { fill: var(--muted); font-size: 12px; font-family: var(--sans); }
.chart-box .bar { fill: var(--navy); }
:root[data-theme="dark"] .chart-box .bar { fill: var(--peach); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .chart-box .bar { fill: var(--peach); } }
.chart-box .hit { fill: transparent; cursor: pointer; }
.chart-box .col:hover .bar, .chart-box .col.on .bar { opacity: .82; }
.chart-tip { position: absolute; pointer-events: none; background: var(--ink); color: var(--surface); padding: 8px 10px; border-radius: 8px; font-size: 13px; box-shadow: var(--shadow); white-space: nowrap; transform: translate(-50%, -100%); }
.chart-tip strong { display: block; }

.foot { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; margin-top: 40px; color: var(--muted); font-size: 13px; }
.foot .link { font-size: 13px; color: var(--muted); }

.dialog { border: 0; border-radius: 16px; padding: 24px; width: min(380px, calc(100% - 32px)); background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }
.dialog::backdrop { background: rgba(20, 27, 35, .5); }
.dialog h2 { font-size: 20px; }
.dialog p { color: var(--ink-2); }

.toasts { position: fixed; top: 16px; right: 16px; display: grid; gap: 8px; z-index: 60; max-width: calc(100% - 32px); }
.toast { background: var(--ink); color: var(--surface); padding: 12px 16px; border-radius: 12px; box-shadow: var(--shadow); font-weight: 500; font-size: 14px; animation: pop .2s ease; display: flex; gap: 10px; align-items: center; }
.toast::before { content: "✓"; font-weight: 700; color: var(--good); }
.toast.error::before { content: "!"; color: var(--danger); }
@keyframes pop { from { opacity: 0; transform: translateY(-6px); } }

.mobile-nav { display: none; }

@media (max-width: 1100px) {
  .stats, .tiles { grid-template-columns: repeat(2, 1fr); }
  .split { grid-template-columns: 1fr; }
}
@media (max-width: 760px) {
  .auth { grid-template-columns: 1fr; }
  .auth-art { padding: 28px 16px; }
  .auth-art-title { font-size: 36px; margin-top: 12px; }
  .brand-mark.big { display: none; }
  .auth-panel { padding: 28px 16px 40px; }
  .app { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; flex-direction: row; align-items: center; justify-content: space-between; padding: 12px 16px; }
  .side-nav { display: none; }
  .logout { margin: 0; width: auto; padding: 8px 10px; }
  .main { padding: 20px 16px 96px; }
  .two { grid-template-columns: 1fr; }
  .mobile-nav:not([hidden]) { display: grid; grid-template-columns: repeat(5, 1fr); position: fixed; inset: auto 0 0 0; background: var(--navy); padding: 6px 6px calc(6px + env(safe-area-inset-bottom)); z-index: 40; }
  .mobile-nav .nav-btn { justify-content: center; padding: 10px 4px; font-size: 13px; min-height: 44px; }
  .toasts { left: 16px; right: 16px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition: none !important; }
}
