:root {
  --bg: #faf9f6; --panel: #f3f1ec; --card: #ffffff; --text: #23211d; --muted: #6d6a62; --faint: #9b978d;
  --line: #e4e1d8; --accent: #c4673f; --accent-soft: #f6e6dc; --ok: #3f7d58; --warn: #a86b12; --bad: #b0412e;
  --user: #efece4; --shadow: 0 1px 2px rgba(30, 25, 15, .06), 0 4px 16px rgba(30, 25, 15, .05);
  --radius: 14px; --side: 272px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1d1c1a; --panel: #161513; --card: #262522; --text: #ebe8e1; --muted: #a8a49a; --faint: #78746b;
    --line: #34322e; --accent: #e08a62; --accent-soft: #3a2a21; --ok: #6fbf8e; --warn: #e0a84e; --bad: #e57b66;
    --user: #2f2d29; --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .25);
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.6 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; -webkit-text-size-adjust: 100%; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

/* ---------- layout ---------- */
#app { display: grid; grid-template-columns: var(--side) 1fr; height: 100vh; height: 100dvh; }
#side { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; padding: 14px 12px; gap: 10px; }
.brand { display: flex; align-items: center; gap: 9px; font-weight: 650; font-size: 18px; padding: 2px 6px 6px; letter-spacing: -.01em; }
.brand svg { width: 26px; height: 26px; flex: none; }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 7px; border: 1px solid var(--line); background: var(--card); border-radius: 10px; padding: 8px 13px; line-height: 1.2; white-space: nowrap; }
.btn:hover { border-color: var(--faint); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--card); border-color: var(--line); }
.btn.danger { color: var(--bad); }
.btn:disabled { opacity: .5; cursor: default; }
#new-chat { justify-content: flex-start; width: 100%; }
.field-label { font-size: 12px; color: var(--muted); padding: 0 6px; }
select.pick, .input, textarea.input { width: 100%; border: 1px solid var(--line); background: var(--card); border-radius: 10px; padding: 8px 10px; }
textarea.input { resize: vertical; min-height: 110px; }
.input:focus, select.pick:focus, textarea:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
#convs { flex: 1; overflow-y: auto; min-height: 0; margin: 4px -4px 0; padding: 0 4px; }
.conv { display: flex; align-items: center; gap: 4px; border-radius: 9px; }
.conv a { flex: 1; min-width: 0; display: block; padding: 7px 9px; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 14px; border-radius: 9px; }
.conv a:hover { background: var(--card); text-decoration: none; }
.conv.on a { background: var(--card); font-weight: 560; }
.conv .x { visibility: hidden; border: 0; background: none; color: var(--faint); padding: 4px 6px; border-radius: 6px; }
.conv:hover .x { visibility: visible; }
.conv .x:hover { color: var(--bad); }
.group-label { font-size: 11.5px; color: var(--faint); text-transform: uppercase; letter-spacing: .06em; padding: 10px 9px 4px; }
.side-foot { border-top: 1px solid var(--line); padding-top: 10px; display: grid; gap: 6px; }
.side-foot .btn { justify-content: flex-start; width: 100%; }
.side-foot .btn.on { border-color: var(--accent); }
#stats-line { font-size: 12px; color: var(--muted); padding: 0 6px; }

#main { display: flex; flex-direction: column; min-width: 0; min-height: 0; position: relative; }
.topbar { display: none; align-items: center; gap: 8px; padding: 10px 12px; border-bottom: 1px solid var(--line); background: var(--bg); padding-top: calc(10px + env(safe-area-inset-top, 0px)); }
.topbar .t { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------- chat ---------- */
#chat-view { flex: 1; display: flex; flex-direction: column; min-height: 0; }
#scroller { flex: 1; overflow-y: auto; }
#messages { max-width: 780px; margin: 0 auto; padding: 28px 22px 24px; }
.msg { margin: 0 0 26px; }
.msg.user { display: flex; justify-content: flex-end; }
.msg.user .bubble { background: var(--user); border-radius: 18px 18px 4px 18px; padding: 10px 16px; max-width: 85%; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.assistant .body { overflow-wrap: anywhere; }
.body p { margin: 0 0 12px; }
.body h3, .body h4 { margin: 18px 0 8px; line-height: 1.3; }
.body h3 { font-size: 17px; } .body h4 { font-size: 15.5px; }
.body ul, .body ol { margin: 0 0 12px; padding-left: 22px; }
.body li { margin: 3px 0; }
.body code { background: var(--panel); border: 1px solid var(--line); border-radius: 5px; padding: 0 4px; font-size: 13.5px; }
.body pre { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 12px; overflow-x: auto; }
.body pre code { border: 0; padding: 0; background: none; }
.body blockquote { margin: 0 0 12px; padding: 2px 14px; border-left: 3px solid var(--line); color: var(--muted); }
.body table { border-collapse: collapse; margin: 0 0 12px; font-size: 14px; display: block; overflow-x: auto; }
.body th, .body td { border: 1px solid var(--line); padding: 5px 9px; text-align: left; vertical-align: top; }
.body hr { border: 0; border-top: 1px solid var(--line); margin: 16px 0; }
.cite { display: inline-flex; align-items: center; justify-content: center; min-width: 19px; height: 19px; padding: 0 5px; margin: 0 1px; border-radius: 6px; border: 0; background: var(--accent-soft); color: var(--accent); font-size: 11.5px; font-weight: 650; vertical-align: 2px; line-height: 1; }
.cite:hover { background: var(--accent); color: #fff; }
.typing::after { content: ""; display: inline-block; width: 8px; height: 15px; margin-left: 2px; background: var(--accent); vertical-align: -2px; animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0; } }
.thinking { color: var(--muted); font-size: 14px; display: flex; align-items: center; gap: 8px; }
.dots i { display: inline-block; width: 6px; height: 6px; margin: 0 2px; border-radius: 50%; background: var(--faint); animation: bob 1.2s infinite; }
.dots i:nth-child(2) { animation-delay: .15s; } .dots i:nth-child(3) { animation-delay: .3s; }
@keyframes bob { 0%, 80%, 100% { opacity: .3; transform: none; } 40% { opacity: 1; transform: translateY(-3px); } }
.sources-row { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 4px; }
.src-chip { display: inline-flex; align-items: center; gap: 6px; max-width: 280px; border: 1px solid var(--line); background: var(--card); border-radius: 999px; padding: 3px 10px 3px 4px; font-size: 12.5px; color: var(--text); }
.src-chip b { display: inline-grid; place-items: center; min-width: 18px; height: 18px; border-radius: 50%; background: var(--accent-soft); color: var(--accent); font-size: 11px; }
.src-chip span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.src-chip:hover { border-color: var(--faint); }
.msg-actions { display: flex; gap: 2px; margin-top: 8px; }
.msg-actions button { border: 0; background: none; color: var(--faint); border-radius: 7px; padding: 4px 7px; font-size: 13px; }
.msg-actions button:hover, .msg-actions button.on { color: var(--text); background: var(--panel); }
.teach { margin-top: 10px; border: 1px solid var(--line); background: var(--card); border-radius: 12px; padding: 12px; display: grid; gap: 8px; }
.teach .row { display: flex; gap: 8px; justify-content: flex-end; }
.error-note { color: var(--bad); font-size: 14px; }

.empty { max-width: 640px; margin: 12vh auto 0; padding: 0 22px; text-align: center; }
.empty h1 { font-size: 30px; font-weight: 560; letter-spacing: -.02em; margin: 0 0 10px; }
.empty p { color: var(--muted); margin: 0 0 18px; }
.suggest { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; }

#composer { padding: 10px 22px calc(16px + env(safe-area-inset-bottom, 0px)); }
.composer-box { max-width: 780px; margin: 0 auto; border: 1px solid var(--line); background: var(--card); border-radius: 18px; box-shadow: var(--shadow); padding: 10px 10px 10px 16px; display: flex; gap: 10px; align-items: flex-end; }
.composer-box:focus-within { border-color: var(--faint); }
#q { flex: 1; border: 0; outline: 0; background: none; resize: none; max-height: 220px; padding: 6px 0; line-height: 1.5; font-size: 15.5px; }
#send { width: 36px; height: 36px; border-radius: 10px; border: 0; background: var(--accent); color: #fff; display: grid; place-items: center; flex: none; }
#send:disabled { opacity: .35; cursor: default; }
#send svg { width: 18px; height: 18px; }
.composer-meta { max-width: 780px; margin: 6px auto 0; display: flex; justify-content: space-between; gap: 10px; font-size: 12px; color: var(--faint); padding: 0 6px; }

/* citation panel */
#cite-panel { position: fixed; top: 0; right: 0; bottom: 0; width: min(420px, 100vw); background: var(--card); border-left: 1px solid var(--line); box-shadow: -8px 0 30px rgba(0, 0, 0, .08); z-index: 30; display: flex; flex-direction: column; }
#cite-panel header { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; border-bottom: 1px solid var(--line); padding-top: calc(14px + env(safe-area-inset-top, 0px)); }
#cite-body { overflow-y: auto; padding: 16px; }
#cite-body h3 { margin: 4px 0 6px; font-size: 16px; line-height: 1.35; }
.excerpt { white-space: pre-wrap; background: var(--panel); border-radius: 10px; padding: 12px; font-size: 14px; margin: 12px 0; }
.links { display: flex; flex-wrap: wrap; gap: 8px; }

/* ---------- library ---------- */
#library-view { flex: 1; overflow-y: auto; }
.lib { max-width: 1060px; margin: 0 auto; padding: 26px 24px 60px; }
.lib-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.lib-head h1 { font-size: 24px; margin: 0; font-weight: 600; letter-spacing: -.01em; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 14px; }
.chip { border: 1px solid var(--line); background: var(--card); border-radius: 999px; padding: 5px 12px; font-size: 13.5px; }
.chip.on { background: var(--text); color: var(--bg); border-color: var(--text); }
.chip .n { opacity: .6; margin-left: 4px; }
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--line); margin-bottom: 18px; }
.tab { border: 0; background: none; padding: 9px 14px; color: var(--muted); border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tab.on { color: var(--text); border-bottom-color: var(--accent); font-weight: 560; }
.brain-desc { color: var(--muted); font-size: 14px; margin: -6px 0 16px; display: flex; gap: 8px; align-items: baseline; flex-wrap: wrap; }
.totals { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; margin-bottom: 18px; }
.total { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; }
.total b { display: block; font-size: 22px; font-weight: 600; letter-spacing: -.01em; }
.total span { font-size: 12.5px; color: var(--muted); }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 12px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 15px; display: grid; gap: 8px; color: var(--text); }
a.card:hover { border-color: var(--faint); text-decoration: none; }
.card-top { display: flex; gap: 11px; align-items: flex-start; }
.kind-icon { width: 34px; height: 34px; border-radius: 9px; display: grid; place-items: center; flex: none; font-size: 15px; background: var(--panel); }
.kind-icon.yt { background: #ffe3e0; color: #d4271c; }
@media (prefers-color-scheme: dark) { .kind-icon.yt { background: #3d1f1c; color: #ff6b5e; } }
.kind-icon svg { width: 17px; height: 17px; }
.card-title { font-weight: 600; line-height: 1.35; overflow-wrap: anywhere; }
.card-sub { font-size: 12.5px; color: var(--muted); overflow-wrap: anywhere; }
.pill { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; padding: 2px 9px; border-radius: 999px; background: var(--panel); color: var(--muted); white-space: nowrap; }
.pill::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.pill.ready { color: var(--ok); } .pill.learning, .pill.listing, .pill.new, .pill.pending { color: var(--warn); }
.pill.error, .pill.no_text { color: var(--bad); } .pill.paused { color: var(--faint); }
.bar { height: 5px; border-radius: 9px; background: var(--panel); overflow: hidden; }
.bar i { display: block; height: 100%; background: var(--ok); border-radius: 9px; }
.meta-line { display: flex; flex-wrap: wrap; gap: 4px 12px; font-size: 12.5px; color: var(--muted); }
.empty-lib { text-align: center; padding: 50px 20px; border: 1px dashed var(--line); border-radius: var(--radius); color: var(--muted); }
.empty-lib h2 { color: var(--text); font-size: 18px; margin: 0 0 6px; }

.back { display: inline-flex; gap: 6px; font-size: 14px; margin-bottom: 12px; }
.detail-head { display: flex; justify-content: space-between; gap: 16px; flex-wrap: wrap; align-items: flex-start; margin-bottom: 14px; }
.detail-head h1 { font-size: 22px; margin: 0 0 4px; line-height: 1.3; letter-spacing: -.01em; overflow-wrap: anywhere; }
.actions { display: flex; flex-wrap: wrap; gap: 6px; }
.filters { display: flex; gap: 8px; flex-wrap: wrap; margin: 6px 0 12px; }
.filters .input { max-width: 320px; }
.filters select.pick { width: auto; }
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius); background: var(--card); }
table.items { width: 100%; border-collapse: collapse; font-size: 14px; }
table.items th { text-align: left; font-weight: 560; font-size: 12.5px; color: var(--muted); padding: 9px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; }
table.items td { padding: 8px 12px; border-bottom: 1px solid var(--line); vertical-align: middle; }
table.items tr:last-child td { border-bottom: 0; }
table.items tr.click { cursor: pointer; }
table.items tr.click:hover td { background: var(--panel); }
.thumb { width: 88px; aspect-ratio: 16 / 9; object-fit: cover; border-radius: 6px; background: var(--panel); display: block; }
.t-title { font-weight: 520; min-width: 220px; }
.nowrap { white-space: nowrap; }
.settings { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; display: grid; gap: 12px; margin-bottom: 14px; }
.settings .row { display: flex; gap: 12px; flex-wrap: wrap; align-items: end; }
.settings label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.confirm { display: inline-flex; gap: 6px; align-items: center; font-size: 13.5px; color: var(--bad); }

.summary-box { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; margin-bottom: 16px; }
.summary-box h2, .section-h { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 0 0 8px; font-weight: 600; }
.transcript { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 6px 0; }
.line { display: grid; grid-template-columns: 64px 1fr; gap: 10px; padding: 7px 16px; font-size: 14.5px; }
.line:hover { background: var(--panel); }
.line .ts { font-variant-numeric: tabular-nums; font-size: 12.5px; padding-top: 2px; }
.line.plain { grid-template-columns: 1fr; }
.result { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; margin-bottom: 10px; display: grid; gap: 6px; }
.result .txt { font-size: 14px; }
mark { background: var(--accent-soft); color: inherit; border-radius: 3px; padding: 0 1px; }
.events { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); }
.ev { display: grid; grid-template-columns: 26px 1fr auto; gap: 10px; padding: 9px 14px; border-bottom: 1px solid var(--line); font-size: 14px; align-items: baseline; }
.ev:last-child { border-bottom: 0; }
.ev .when { font-size: 12px; color: var(--faint); white-space: nowrap; }

/* dialog */
dialog { border: 1px solid var(--line); border-radius: 16px; background: var(--card); color: var(--text); padding: 0; width: min(560px, calc(100vw - 24px)); box-shadow: 0 20px 60px rgba(0, 0, 0, .25); }
dialog::backdrop { background: rgba(20, 18, 15, .45); }
.dlg-head { display: flex; justify-content: space-between; align-items: center; padding: 16px 18px 6px; }
.dlg-head h2 { margin: 0; font-size: 18px; }
.dlg-body { padding: 8px 18px 18px; display: grid; gap: 14px; }
.dlg-body label { display: grid; gap: 5px; font-size: 13px; color: var(--muted); }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 10px; padding: 2px; background: var(--panel); }
.seg button { border: 0; background: none; padding: 6px 14px; border-radius: 8px; color: var(--muted); }
.seg button.on { background: var(--card); color: var(--text); box-shadow: var(--shadow); }
.hint { font-size: 12.5px; color: var(--muted); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.dlg-foot { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.form-error { color: var(--bad); font-size: 13.5px; }
.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 9px 16px; border-radius: 10px; font-size: 14px; z-index: 50; box-shadow: var(--shadow); max-width: calc(100vw - 32px); }

/* locked */
#locked { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.lock-card { max-width: 420px; width: 100%; background: var(--card); border: 1px solid var(--line); border-radius: 18px; padding: 28px; box-shadow: var(--shadow); text-align: center; display: grid; gap: 12px; }
.lock-card h1 { margin: 0; font-size: 22px; }
.lock-card p { margin: 0; color: var(--muted); }
.login { display: grid; gap: 10px; text-align: left; }
.login .btn { width: 100%; padding: 10px; }
#open-login { justify-content: flex-start; color: var(--muted); }

/* ---------- phones ---------- */
.scrim { display: none; }
@media (max-width: 820px) {
  #app { grid-template-columns: 1fr; }
  #side { position: fixed; inset: 0 auto 0 0; width: min(300px, 86vw); z-index: 40; transform: translateX(-102%); transition: transform .2s ease; padding-top: calc(14px + env(safe-area-inset-top, 0px)); }
  body.side-open #side { transform: none; }
  body.side-open .scrim { display: block; position: fixed; inset: 0; background: rgba(0, 0, 0, .35); z-index: 35; }
  .topbar { display: flex; }
  #messages { padding: 18px 16px; }
  #composer { padding-left: 12px; padding-right: 12px; }
  #q { font-size: 16px; }
  .lib { padding: 18px 16px 50px; }
  .empty { margin-top: 8vh; }
  .grid2 { grid-template-columns: 1fr; }
}
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }
