:root {
  --bg: #f4f6f3;
  --panel: #ffffff;
  --ink: #1d2a22;
  --muted: #5d6b62;
  --line: #dfe5df;
  --brand: #0b6b3a;
  --brand-2: #0f8a4b;
  --gold: #c99a1c;
  --gold-soft: #fff6dc;
  --user: #0b6b3a;
  --user-ink: #ffffff;
  --sec: #f6f8f5;
  --mark: #ffe58a;
  --right: #1f9d55;
  --wrong: #d64545;
  --shadow: 0 2px 10px rgba(16, 40, 24, .07);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1612; --panel: #17211b; --ink: #e4ece6; --muted: #9aa99f; --line: #26332b;
    --brand: #2fb36b; --brand-2: #37c779; --gold: #e2b84a; --gold-soft: #2a2614;
    --user: #1d7a47; --sec: #1c2821; --mark: #6b5a12; --shadow: 0 2px 10px rgba(0, 0, 0, .35);
  }
}
:root[data-theme="dark"] {
  --bg: #0f1612; --panel: #17211b; --ink: #e4ece6; --muted: #9aa99f; --line: #26332b;
  --brand: #2fb36b; --brand-2: #37c779; --gold: #e2b84a; --gold-soft: #2a2614;
  --user: #1d7a47; --sec: #1c2821; --mark: #6b5a12; --shadow: 0 2px 10px rgba(0, 0, 0, .35);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; display: flex; flex-direction: column;
  background: var(--bg); color: var(--ink);
  font: 15px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
button { font: inherit; cursor: pointer; }

.top {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
  background: linear-gradient(120deg, #0b6b3a, #0f8a4b 60%, #13994f); color: #fff;
  box-shadow: 0 2px 12px rgba(0, 0, 0, .15); z-index: 2;
}
.brand { display: flex; align-items: center; gap: 10px; }
.logo { font-size: 28px; background: rgba(255, 255, 255, .15); border-radius: 12px; width: 44px; height: 44px; display: grid; place-items: center; }
.brand h1 { margin: 0; font-size: 19px; letter-spacing: .3px; }
.brand p { margin: 0; font-size: 12.5px; opacity: .85; }
.top-r { display: flex; gap: 8px; align-items: center; }
.offline, .score { font-size: 12px; background: rgba(255, 255, 255, .16); padding: 4px 10px; border-radius: 999px; white-space: nowrap; }

.log { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 14px; }
.msg { display: flex; gap: 10px; max-width: 860px; width: 100%; margin: 0 auto; }
.msg.user { justify-content: flex-end; }
.avatar { flex: 0 0 34px; height: 34px; border-radius: 50%; background: var(--gold-soft); display: grid; place-items: center; font-size: 18px; border: 1px solid var(--line); }
.bubble { background: var(--panel); border: 1px solid var(--line); border-radius: 4px 16px 16px 16px; padding: 12px 14px; box-shadow: var(--shadow); min-width: 0; overflow-wrap: anywhere; }
.msg.user .bubble { background: var(--user); color: var(--user-ink); border-color: transparent; border-radius: 16px 4px 16px 16px; max-width: 80%; }
.bubble p { margin: 0 0 8px; }
.bubble p:last-child { margin-bottom: 0; }
.muted, .hint { color: var(--muted); font-size: 13px; }
.opener { font-size: 15.5px; }
mark { background: var(--mark); color: inherit; border-radius: 3px; padding: 0 1px; }

.sec { background: var(--sec); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; margin: 10px 0; }
.sec-h { font-weight: 700; font-size: 13.5px; margin-bottom: 6px; color: var(--brand); letter-spacing: .2px; }
.sec.eli5 { background: var(--gold-soft); border-color: color-mix(in srgb, var(--gold) 40%, var(--line)); }
.sec.eli5 .sec-h { color: var(--gold); }
.analogy { margin-top: 6px !important; }
.sec.fact .sec-h { color: var(--gold); }
.points, .auth { margin: 0; padding-left: 18px; }
.points li, .auth li { margin: 5px 0; }
.auth { list-style: none; padding-left: 0; }
.cite { color: var(--brand); cursor: pointer; font-weight: 700; }

.quiz-q { font-style: italic; }
.quiz-opts { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 8px; margin-top: 8px; }
.quiz-opt { padding: 9px 10px; border-radius: 10px; border: 1.5px solid var(--line); background: var(--panel); color: var(--ink); text-align: left; transition: transform .1s; }
.quiz-opt:hover { border-color: var(--brand); transform: translateY(-1px); }
.quiz.done .quiz-opt { cursor: default; transform: none; }
.quiz-opt.right { border-color: var(--right); background: color-mix(in srgb, var(--right) 16%, var(--panel)); }
.quiz-opt.wrong { border-color: var(--wrong); background: color-mix(in srgb, var(--wrong) 14%, var(--panel)); }
.quiz-fb { margin-top: 10px; font-weight: 600; animation: pop .3s ease; }
.quiz-fb small { font-weight: 400; color: var(--muted); }
@keyframes pop { from { transform: scale(.96); opacity: 0; } to { transform: none; opacity: 1; } }

details.src { border: 1px solid var(--line); border-radius: 10px; margin: 6px 0; background: var(--panel); transition: box-shadow .3s; }
details.src.flash { box-shadow: 0 0 0 3px var(--gold); }
details.src summary { list-style: none; cursor: pointer; padding: 8px 10px; display: grid; grid-template-columns: auto 1fr; gap: 2px 8px; align-items: center; }
details.src summary::-webkit-details-marker { display: none; }
.src-n { grid-row: span 3; width: 22px; height: 22px; border-radius: 50%; background: var(--brand); color: #fff; font-size: 12px; display: grid; place-items: center; align-self: start; }
.src-badge { font-size: 11.5px; color: var(--muted); }
.src-t { font-weight: 600; font-size: 13.5px; }
.src-d { font-size: 12px; color: var(--muted); }
.src-body { padding: 0 12px 10px 40px; font-size: 13.5px; }
.src-actions { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.src-more { background: none; border: 0; color: var(--brand); padding: 0; font-weight: 600; }
.src-open { color: var(--brand); font-size: 13px; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.chip { border: 1px solid var(--line); background: var(--panel); color: var(--ink); border-radius: 999px; padding: 6px 12px; font-size: 13px; text-align: left; }
.chip:hover { border-color: var(--brand); color: var(--brand); }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 10px 0; }
.stats div { background: var(--sec); border: 1px solid var(--line); border-radius: 12px; padding: 10px; text-align: center; }
.stats b { display: block; font-size: 20px; color: var(--brand); }
.stats span { font-size: 12px; color: var(--muted); }

.dots { display: inline-flex; gap: 4px; vertical-align: middle; }
.dots i { width: 7px; height: 7px; border-radius: 50%; background: var(--brand); animation: b 1s infinite ease-in-out; }
.dots i:nth-child(2) { animation-delay: .15s; } .dots i:nth-child(3) { animation-delay: .3s; }
@keyframes b { 0%, 80%, 100% { transform: scale(.5); opacity: .4; } 40% { transform: none; opacity: 1; } }
.think { color: var(--muted); font-style: italic; margin-left: 6px; }

.bar { background: var(--panel); border-top: 1px solid var(--line); padding: 10px 16px; padding-bottom: max(8px, env(safe-area-inset-bottom)); }
.docs { display: flex; flex-wrap: wrap; gap: 6px; max-width: 860px; margin: 0 auto 8px; }
.doc-pill { font-size: 12.5px; background: var(--gold-soft); border: 1px solid var(--line); border-radius: 999px; padding: 3px 4px 3px 10px; display: inline-flex; align-items: center; gap: 6px; }
.doc-pill button { border: 0; background: none; color: var(--muted); font-size: 16px; line-height: 1; padding: 0 6px; }
.ask { display: flex; gap: 8px; align-items: flex-end; max-width: 860px; margin: 0 auto; }
.attach { font-size: 20px; width: 42px; height: 42px; display: grid; place-items: center; border-radius: 12px; border: 1px solid var(--line); cursor: pointer; flex: 0 0 auto; }
.attach:hover { border-color: var(--brand); }
#q { flex: 1; resize: none; border: 1.5px solid var(--line); border-radius: 12px; padding: 10px 12px; font: inherit; background: var(--bg); color: var(--ink); max-height: 140px; min-width: 0; }
#q:focus { outline: none; border-color: var(--brand); }
#send { width: 42px; height: 42px; border-radius: 12px; border: 0; background: var(--brand); color: #fff; font-size: 18px; flex: 0 0 auto; }
#send:disabled, #q:disabled { opacity: .5; }
.disclaimer { text-align: center; font-size: 11px; color: var(--muted); margin: 6px 0 0; }

.dropzone { position: fixed; inset: 0; display: none; place-items: center; font-size: 22px; font-weight: 700; color: var(--brand); background: color-mix(in srgb, var(--bg) 85%, transparent); border: 3px dashed var(--brand); z-index: 9; pointer-events: none; }
body.drag .dropzone { display: grid; }

@media (max-width: 560px) {
  .log { padding: 12px; }
  .avatar { display: none; }
  .msg.user .bubble { max-width: 90%; }
  .brand p { display: none; }
  .stats b { font-size: 17px; }
}
.src-snip, .src-full { white-space: pre-line; }

/* AI mode */
.ai-btn { font-size: 12px; background: rgba(255, 255, 255, .16); color: #fff; border: 1px solid rgba(255, 255, 255, .35); padding: 4px 10px; border-radius: 999px; white-space: nowrap; }
.ai-btn.on { background: var(--gold); border-color: var(--gold); color: #1d2a22; font-weight: 700; }
.ai-panel { position: fixed; inset: 0; background: rgba(0, 0, 0, .35); display: grid; place-items: start end; padding: 64px 12px 12px; z-index: 10; }
.ai-panel[hidden] { display: none; }
.ai-card { background: var(--panel); color: var(--ink); border: 1px solid var(--line); border-radius: 16px; padding: 14px; width: min(380px, 100%); box-shadow: 0 10px 30px rgba(0, 0, 0, .25); }
.ai-top { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; }
.ai-x { border: 0; background: none; color: var(--muted); font-size: 22px; line-height: 1; }
.ai-opt { display: flex; justify-content: space-between; align-items: center; gap: 10px; border: 1.5px solid var(--line); border-radius: 12px; padding: 10px; margin: 8px 0; }
.ai-opt.on { border-color: var(--gold); background: var(--gold-soft); }
.ai-opt button, .ai-off { border: 0; background: var(--brand); color: #fff; border-radius: 10px; padding: 7px 14px; font-weight: 600; flex: 0 0 auto; }
.ai-opt button:disabled { opacity: .6; cursor: default; }
.ai-off { background: var(--sec); color: var(--ink); border: 1px solid var(--line); width: 100%; }
.ai-bar { height: 8px; background: var(--sec); border-radius: 999px; overflow: hidden; margin: 6px 0; }
.ai-bar i { display: block; height: 100%; width: 0; background: linear-gradient(90deg, var(--brand), var(--gold)); transition: width .2s; }
.sec.ai { border-color: color-mix(in srgb, var(--gold) 55%, var(--line)); background: linear-gradient(180deg, var(--gold-soft), var(--sec)); }
.sec.ai .sec-h { color: var(--gold); }
.ai-model { font-weight: 400; font-size: 11.5px; color: var(--muted); margin-left: 6px; }
.ai-out p { margin: 0 0 8px; }
.ai-stop { border: 1px solid var(--line); background: var(--panel); color: var(--ink); border-radius: 999px; padding: 4px 12px; font-size: 12.5px; margin-top: 4px; }
.caret { display: inline-block; width: 7px; height: 15px; background: var(--gold); vertical-align: text-bottom; animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0; } }
@media (max-width: 560px) { .offline { display: none; } .ai-panel { place-items: start center; } }

/* Web search */
.web-btn { background: none; color: var(--ink); font-size: 19px; }
.web-btn.on { border-color: #2b7de9; background: color-mix(in srgb, #2b7de9 15%, var(--panel)); box-shadow: 0 0 0 2px color-mix(in srgb, #2b7de9 35%, transparent); }
.offline.web-on { background: rgba(43, 125, 233, .45); }
.web-panel { place-items: end start; padding: 12px 12px 96px; }
.switch { display: flex; align-items: center; gap: 8px; font-weight: 600; margin: 10px 0; cursor: pointer; }
.switch input { width: 18px; height: 18px; accent-color: var(--brand); }
label.ai-opt { cursor: pointer; justify-content: flex-start; }
label.ai-opt input { accent-color: var(--brand); flex: 0 0 auto; }
.brave-key input { width: 100%; padding: 9px 10px; border-radius: 10px; border: 1.5px solid var(--line); background: var(--bg); color: var(--ink); font: inherit; margin-bottom: 4px; }
.sec.webres { border-color: color-mix(in srgb, #2b7de9 45%, var(--line)); }
.sec.webres .sec-h { color: #2b7de9; }
.src-badge.web { color: #2b7de9; font-weight: 600; }
@media (max-width: 560px) { .web-panel { place-items: end center; } }
