:root {
  --bg: #f6f2ec;
  --surface: #fffdf9;
  --ink: #1d1916;
  --muted: #6f655c;
  --line: #e4dcd1;
  --accent: #b5561f;
  --accent-ink: #fff;
  --mark: #b5561f;          /* chart marks — validated vs --surface (light) */
  --track: #efe7dc;         /* meter/bar track: lighter step, same warm ramp */
  --user: #ece3d6;
  --ok: #3f8f5a;
  --down: #c2412d;
  --rec: #c2412d;
  --display: 'Fraunces', Georgia, serif;
  --body: 'Inter', system-ui, -apple-system, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14110f;
    --surface: #1d1916;
    --ink: #eee6dc;
    --muted: #a09385;
    --line: #2f2924;
    --accent: #e0864a;
    --accent-ink: #1a0f08;
    --mark: #d4763a;        /* dark step, validated vs --surface (dark) */
    --track: #2c2520;
    --user: #2a241f;
    --ok: #6cc08a;
    --down: #ef6b55;
    --rec: #ef6b55;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--ink); font: 16px/1.55 var(--body); -webkit-font-smoothing: antialiased; }
button, input, textarea, select { font: inherit; color: inherit; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 0.88rem; }
.error { color: var(--down); margin: 0; }
.ok { color: var(--ok); }

.wordmark { font: 650 1.6rem/1 var(--display); letter-spacing: -0.02em; margin: 0; }
.wordmark::after { content: '.'; color: var(--accent); }

button.primary { background: var(--accent); color: var(--accent-ink); border: 0; border-radius: 10px; padding: 11px 16px; font-weight: 600; cursor: pointer; }
button.primary:disabled { opacity: 0.5; cursor: default; }
.ghost { background: none; border: 1px solid var(--line); border-radius: 8px; padding: 6px 12px; cursor: pointer; color: var(--muted); }
.ghost:hover { color: var(--ink); border-color: var(--muted); }
input[type="search"], input[type="password"], select {
  background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; min-width: 0;
}
input:focus, select:focus, textarea:focus, button:focus-visible, [tabindex]:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* ---- login ---- */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(340px, 100%); display: grid; gap: 12px; text-align: center; }
.login-card .wordmark { font-size: 3rem; }
.login-card p { margin: 0 0 8px; }
.login-card input { padding: 12px 14px; border-radius: 10px; }

/* ---- shell ---- */
.app { height: 100dvh; display: grid; grid-template-rows: auto 1fr; grid-template-columns: minmax(0, 1fr); max-width: 1120px; margin: 0 auto; }
.bar { display: flex; align-items: center; gap: 14px; padding: 12px 16px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.status { display: flex; gap: 5px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--line); }
.dot.ok { background: var(--ok); }
.dot.down { background: var(--down); }
.tabs { margin-left: auto; display: flex; gap: 2px; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 3px; }
.tabs a { padding: 6px 12px; border-radius: 7px; color: var(--muted); text-decoration: none; font-size: 0.93rem; font-weight: 500; }
.tabs a:hover { color: var(--ink); }
.tabs a[aria-current="page"] { background: var(--user); color: var(--ink); }
@media (max-width: 600px) {
  .tabs { margin-left: 0; width: 100%; justify-content: space-between; }
  .tabs a { flex: 1; text-align: center; padding: 6px 4px; }
}
.view { min-height: 0; overflow-y: auto; }
.view-page { padding: 20px 16px 48px; }

/* ---- chat ---- */
.view-chat { display: grid; grid-template-rows: 1fr auto; grid-template-columns: minmax(0, 1fr); overflow: hidden; }
.log { overflow-y: auto; padding: 20px max(16px, calc((100% - 820px) / 2)) 8px; display: flex; flex-direction: column; gap: 18px; }
.empty { margin: auto 0; text-align: center; }
.empty-title { font: 500 clamp(1.5rem, 6vw, 1.9rem)/1.2 var(--display); margin: 0 0 20px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.chips button { background: var(--surface); border: 1px solid var(--line); border-radius: 999px; padding: 8px 14px; cursor: pointer; font-size: 0.92rem; }
.chips button:hover { border-color: var(--accent); }

.msg { max-width: 100%; overflow-wrap: anywhere; }
.msg.user { align-self: flex-end; background: var(--user); padding: 10px 14px; border-radius: 16px 16px 4px 16px; max-width: 85%; white-space: pre-wrap; }
.msg.assistant > :first-child { margin-top: 0; }
.msg.assistant > :last-child { margin-bottom: 0; }
.msg.assistant p, .msg.assistant ul, .msg.assistant ol { margin: 0 0 0.7em; }
.msg.assistant ul, .msg.assistant ol { padding-left: 1.3em; }
.msg.assistant li + li { margin-top: 0.25em; }
.msg.assistant h3, .msg.assistant h4, .msg.assistant h5 { font-family: var(--display); font-weight: 650; margin: 1em 0 0.4em; }
.msg.assistant a { color: var(--accent); }
.msg.assistant code { background: var(--user); padding: 1px 5px; border-radius: 4px; font-size: 0.9em; }
.msg.assistant pre { background: var(--surface); border: 1px solid var(--line); padding: 10px; border-radius: 8px; overflow-x: auto; }
.msg.assistant hr { border: 0; border-top: 1px solid var(--line); }
.msg.pending { color: var(--muted); font-style: italic; }
.msg.pending::before { content: ''; display: inline-block; width: 8px; height: 8px; margin-right: 8px; border-radius: 50%; background: var(--accent); animation: pulse 1s ease-in-out infinite; }
.msg.error { color: var(--down); }
@keyframes pulse { 50% { opacity: 0.25; } }
.poster { display: inline-block; width: 132px; aspect-ratio: 2 / 3; object-fit: cover; border-radius: 8px; margin: 4px 8px 4px 0; vertical-align: top; box-shadow: 0 4px 14px rgb(0 0 0 / 0.25); }
.msg .table { overflow-x: auto; margin: 0 0 0.7em; }

.composer { display: flex; gap: 8px; align-items: flex-end; padding: 12px max(16px, calc((100% - 820px) / 2)) calc(12px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); }
.composer textarea { flex: 1; min-width: 0; resize: none; border: 1px solid var(--line); border-radius: 12px; padding: 11px 14px; background: var(--surface); max-height: 180px; }
.composer .primary { width: 46px; height: 46px; font-size: 1.2rem; padding: 0; }
.composer .ghost { height: 46px; }
.mic { height: 46px; min-width: 46px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 0 12px; color: var(--muted); }
.mic svg { width: 20px; height: 20px; fill: currentColor; }
.mic:hover { color: var(--ink); border-color: var(--muted); }
.mic[data-state="rec"] { background: var(--rec); border-color: var(--rec); color: #fff; animation: pulse 1.4s ease-in-out infinite; }
.mic[data-state="busy"] { opacity: 0.6; cursor: default; }
.mic-time { font-size: 0.85rem; font-variant-numeric: tabular-nums; }
.mic-time:empty { display: none; }

/* ---- library ---- */
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 10px; }
.toolbar input[type="search"] { flex: 1 1 220px; }
.toolbar select { flex: 0 1 auto; }
.count { margin: 0 0 8px; font-size: 0.9rem; }
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); }
.lib-table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
.lib-table th { position: sticky; top: 0; background: var(--surface); text-align: left; font-weight: 500; color: var(--muted); padding: 9px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.lib-table td { padding: 7px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
.lib-table tr:last-child td { border-bottom: 0; }
.lib-table .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.lib-table .title { font-weight: 500; }
.lib-table .orig { display: block; color: var(--muted); font-size: 0.82rem; }
.lib-table .c-lib, .lib-table .c-added, .lib-table .c-watched { white-space: nowrap; font-variant-numeric: tabular-nums; color: var(--muted); }
.lib-table tr.is-missing .title { color: var(--muted); }
.tag { display: inline-block; font-size: 0.78rem; padding: 1px 7px; border-radius: 999px; border: 1px solid var(--line); color: var(--muted); white-space: nowrap; }
.more { display: block; margin: 12px auto 0; }
@media (max-width: 760px) { .c-lib, .c-added, .c-rating { display: none; } }
@media (max-width: 520px) { .c-audio, .c-watched { display: none; } }

/* ---- stats ---- */
.segmented { display: inline-flex; flex-wrap: wrap; gap: 2px; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 3px; margin-bottom: 18px; }
.segmented button { border: 0; background: none; padding: 6px 12px; border-radius: 7px; cursor: pointer; color: var(--muted); font-size: 0.93rem; }
.segmented button[aria-checked="true"] { background: var(--user); color: var(--ink); font-weight: 500; }
.stats { align-items: start; display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 340px), 1fr)); gap: 14px; }
.hero, .kpis, .meters { grid-column: 1 / -1; }
.hero-value { font: 600 3.5rem/1 var(--body); letter-spacing: -0.02em; }
.hero-label { color: var(--muted); margin-top: 6px; }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 170px), 1fr)); gap: 10px; }
.tile { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; }
.tile-label { color: var(--muted); font-size: 0.86rem; }
.tile-value { font-weight: 600; font-size: 1.6rem; line-height: 1.25; }
.tile-sub { color: var(--muted); font-size: 0.8rem; }
.meters { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 260px), 1fr)); gap: 12px 20px; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 14px; }
.meter-head { display: flex; justify-content: space-between; gap: 8px; font-size: 0.88rem; margin-bottom: 6px; }
.meter-val { color: var(--muted); font-variant-numeric: tabular-nums; text-align: right; }
.meter { height: 10px; border-radius: 5px; background: var(--track); overflow: hidden; }
.meter-fill { height: 100%; background: var(--mark); border-radius: 0 5px 5px 0; }
.chart-card { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 14px 16px; min-width: 0; }
.chart-card h3 { margin: 0; font-size: 1rem; font-weight: 600; }
.chart-card > p { margin: 2px 0 0; }
.chart-card > :nth-child(2), .chart-card > h3 + div { margin-top: 12px; }

.col-chart { margin-top: 12px; }
.cols { height: 150px; display: flex; align-items: stretch; gap: 2px; border-bottom: 1px solid var(--line); padding-top: 18px; }
.col-hit { flex: 1; min-width: 0; display: flex; flex-direction: column; justify-content: flex-end; align-items: center; position: relative; cursor: default; }
.col { width: min(100%, 24px); background: var(--mark); border-radius: 4px 4px 0 0; min-height: 0; }
.col-hit.is-hover .col, .col-hit:focus-visible .col { filter: brightness(1.18); }
.col-peak { position: absolute; top: -18px; font-size: 0.75rem; color: var(--muted); white-space: nowrap; font-variant-numeric: tabular-nums; }
.col-axis { display: flex; gap: 2px; margin-top: 4px; }
.col-axis span { flex: 1; min-width: 0; font-size: 0.7rem; color: var(--muted); white-space: nowrap; overflow: visible; text-align: center; }

.bars { display: grid; gap: 6px; margin-top: 12px; }
.bar-row { display: grid; grid-template-columns: minmax(80px, 38%) 1fr auto; align-items: center; gap: 10px; font-size: 0.86rem; padding: 2px 0; border-radius: 4px; }
.bar-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-track { height: 14px; }
.hbar { display: block; height: 100%; background: var(--mark); border-radius: 0 4px 4px 0; min-width: 2px; }
.bar-row.is-hover .hbar, .bar-row:focus-visible .hbar { filter: brightness(1.18); }
.bar-val { color: var(--muted); font-variant-numeric: tabular-nums; text-align: right; min-width: 3.5em; }

.as-table { margin-top: 10px; font-size: 0.85rem; }
.as-table summary { color: var(--muted); cursor: pointer; font-size: 0.8rem; }
.as-table table { border-collapse: collapse; margin-top: 6px; width: 100%; }
.as-table th, .as-table td { text-align: left; padding: 3px 10px 3px 0; border-bottom: 1px solid var(--line); }
.as-table .num { text-align: right; font-variant-numeric: tabular-nums; }

.tip { position: fixed; z-index: 10; pointer-events: none; background: var(--ink); color: var(--bg); border-radius: 8px; padding: 6px 10px; font-size: 0.82rem; display: grid; box-shadow: 0 6px 20px rgb(0 0 0 / 0.25); max-width: 260px; }
.tip strong { font-size: 0.95rem; font-variant-numeric: tabular-nums; }

/* ---- settings ---- */
.settings { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 320px), 1fr)); gap: 14px; max-width: 820px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 16px; display: grid; gap: 10px; align-content: start; }
.card h2 { margin: 0; font: 600 1.1rem/1.3 var(--body); }
.card p { margin: 0; }
.card label { display: grid; gap: 4px; font-size: 0.9rem; color: var(--muted); }
.card label input { color: var(--ink); }
.card .ghost { justify-self: start; }
.facts { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin: 0; font-size: 0.92rem; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; overflow-wrap: anywhere; }
