/* Design tokens — every color/size comes from here. */
:root {
  --bg: #faf9f7;
  --surface: #ffffff;
  --surface-2: #f2f0ec;
  --border: #e5e2dc;
  --text: #1c1b19;
  --muted: #6b6760;
  --accent: #c8553d;
  --accent-text: #ffffff;
  --code-bg: #1e1d1b;
  --code-text: #ecebe8;
  --radius: 10px;
  --radius-lg: 16px;
  --font: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
  --mono: "JetBrains Mono", ui-monospace, "Cascadia Code", Consolas, monospace;
  color-scheme: light;
}

/* Dark colors: used when the computer is in dark mode (unless the user picked "Light" in Settings),
   or always when the user picked "Dark". theme.js sets data-theme on <html>. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #161514;
    --surface: #1e1d1b;
    --surface-2: #262523;
    --border: #34322f;
    --text: #ecebe8;
    --muted: #a19d96;
    --accent: #e07a5f;
    --accent-text: #1a1007;
    --code-bg: #0f0e0d;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #161514;
  --surface: #1e1d1b;
  --surface-2: #262523;
  --border: #34322f;
  --text: #ecebe8;
  --muted: #a19d96;
  --accent: #e07a5f;
  --accent-text: #1a1007;
  --code-bg: #0f0e0d;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; } /* "hidden" always wins over display styles */
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.6 var(--font);
  -webkit-font-smoothing: antialiased;
}
a { color: inherit; }
code, pre, kbd { font-family: var(--mono); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 40px; padding: 0 16px;
  border-radius: var(--radius); border: 1px solid var(--border);
  background: var(--surface); color: var(--text);
  font: 500 14px var(--font); text-decoration: none; cursor: pointer;
  transition: background .15s, border-color .15s, opacity .15s;
}
.btn:hover { background: var(--surface-2); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn-primary:hover { background: var(--accent); opacity: .9; }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.icon { width: 18px; height: 18px; flex: none; }

.logo { display: inline-flex; align-items: center; gap: 8px; font-weight: 650; text-decoration: none; letter-spacing: -.01em; }
.logo-mark { width: 22px; height: 22px; color: var(--accent); }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}

/* ---------- App ---------- */
.app { display: flex; height: 100dvh; overflow: hidden; }
.sidebar {
  width: 272px; flex: none; display: flex; flex-direction: column;
  background: var(--surface-2); border-right: 1px solid var(--border);
}
.sidebar-top { padding: 12px; display: flex; flex-direction: column; gap: 12px; }
.sidebar-top .row { display: flex; align-items: center; justify-content: space-between; height: 40px; padding: 0 4px; }
.history { flex: 1; overflow-y: auto; padding: 0 8px 8px; }
.history h2 { font-size: 12px; font-weight: 500; color: var(--muted); margin: 8px 8px 6px; }
.history-empty { color: var(--muted); font-size: 14px; padding: 8px; }
.chat-item { display: flex; align-items: center; border-radius: 8px; }
.chat-item:hover, .chat-item.active { background: var(--border); }
.chat-item button.open { flex: 1; min-width: 0; text-align: left; background: none; border: 0; color: var(--text); font: 14px var(--font); padding: 9px 10px; cursor: pointer; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item button.del { opacity: 0; background: none; border: 0; color: var(--muted); cursor: pointer; padding: 6px 8px; border-radius: 6px; }
.chat-item:hover .del, .chat-item .del:focus-visible { opacity: 1; }
.sidebar-bottom { padding: 12px; border-top: 1px solid var(--border); }
.sidebar-bottom .btn { width: 100%; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar { display: flex; align-items: center; gap: 8px; height: 56px; padding: 0 16px; border-bottom: 1px solid var(--border); }
.icon-btn { display: inline-flex; align-items: center; justify-content: center; width: 40px; height: 40px; border-radius: var(--radius); background: none; border: 0; color: var(--text); cursor: pointer; }
.icon-btn:hover { background: var(--surface-2); }
.menu-btn { display: none; }
select.model {
  height: 36px; border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); color: var(--text); font: 14px var(--font); padding: 0 10px;
}

.messages { flex: 1; overflow-y: auto; }
.thread { max-width: 760px; margin: 0 auto; padding: 24px 16px 8px; }
.msg { margin-bottom: 24px; }
.msg-user { display: flex; justify-content: flex-end; }
.msg-user .bubble { background: var(--surface-2); border: 1px solid var(--border); padding: 10px 14px; border-radius: 14px; max-width: 85%; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg-ai { overflow-wrap: anywhere; }
.msg-ai p { margin: 0 0 12px; }
.msg-ai pre { background: var(--code-bg); color: var(--code-text); padding: 14px 16px; border-radius: var(--radius); overflow-x: auto; font-size: 14px; line-height: 1.55; }
.msg-ai :not(pre) > code { background: var(--surface-2); padding: 1px 6px; border-radius: 5px; font-size: .9em; }
.msg-ai details { color: var(--muted); font-size: 14px; margin-bottom: 12px; border-left: 2px solid var(--border); padding-left: 12px; }
.msg-ai summary { cursor: pointer; }
.msg-error { color: #c0392b; background: color-mix(in srgb, #c0392b 10%, transparent); border-radius: var(--radius); padding: 10px 14px; font-size: 14px; }
.cursor::after { content: "▍"; color: var(--accent); animation: blink 1s steps(1) infinite; }
@keyframes blink { 50% { opacity: 0; } }

.welcome { text-align: center; padding: 12vh 16px 24px; }
.welcome h1 { font-size: 28px; letter-spacing: -.02em; margin: 12px 0 6px; }
.welcome p { color: var(--muted); margin: 0 0 24px; }
.suggestions { display: grid; gap: 10px; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); max-width: 640px; margin: 0 auto; }
.suggestions button { text-align: left; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; color: var(--text); font: 14px var(--font); cursor: pointer; }
.suggestions button:hover { border-color: var(--accent); }

.composer-wrap { padding: 8px 16px 16px; }
.composer { max-width: 760px; margin: 0 auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); padding: 10px; display: flex; gap: 8px; align-items: flex-end; }
.composer:focus-within { border-color: var(--accent); }
.composer textarea { flex: 1; border: 0; background: none; color: var(--text); font: 16px/1.5 var(--font); resize: none; max-height: 200px; padding: 6px; outline: none; }
.hint { text-align: center; color: var(--muted); font-size: 12px; margin-top: 8px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

.scrim { display: none; }
@media (max-width: 768px) {
  .menu-btn { display: inline-flex; }
  .sidebar { position: fixed; inset: 0 auto 0 0; z-index: 40; transform: translateX(-100%); transition: transform .2s ease-out; }
  .app.nav-open .sidebar { transform: none; }
  .app.nav-open .scrim { display: block; position: fixed; inset: 0; z-index: 30; background: rgba(0,0,0,.4); }
}

/* ---------- Customize panel ---------- */
.customize-btn { margin-left: auto; }
.active-tools { max-width: 760px; margin: 0 auto 6px; font-size: 13px; color: var(--muted); min-height: 1em; }
.active-tools:empty { display: none; }
.panel {
  width: min(560px, calc(100vw - 32px)); max-height: min(720px, calc(100dvh - 32px));
  border: 1px solid var(--border); border-radius: var(--radius-lg); padding: 0 20px 20px;
  background: var(--surface); color: var(--text); overflow-y: auto;
}
.panel::backdrop { background: rgba(0,0,0,.45); }
.panel-head { position: sticky; top: 0; background: var(--surface); display: flex; align-items: center; justify-content: space-between; padding: 16px 0 8px; z-index: 1; }
.panel h2 { font-size: 18px; margin: 0; }
.panel h3 { font-size: 15px; margin: 20px 0 2px; }
.panel h4 { font-size: 14px; margin: 16px 0 2px; }
.muted { color: var(--muted); font-size: 14px; margin: 0 0 10px; }
.muted.small { font-size: 12px; margin-top: 6px; }
.option-list { display: flex; flex-direction: column; gap: 6px; }
.option {
  display: flex; align-items: center; gap: 12px; padding: 10px 12px;
  border: 1px solid var(--border); border-radius: var(--radius); cursor: pointer;
}
.option:has(input:checked) { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 8%, transparent); }
.option input { width: 18px; height: 18px; accent-color: var(--accent); flex: none; margin: 0; }
.option .text { flex: 1; min-width: 0; }
.option strong { display: block; font-size: 14px; font-weight: 500; }
.option span.desc { display: block; font-size: 13px; color: var(--muted); overflow-wrap: anywhere; }
.option .remove { background: none; border: 0; color: var(--muted); cursor: pointer; min-width: 32px; min-height: 32px; border-radius: 6px; }
.option .remove:hover { background: var(--surface-2); color: var(--text); }
.add-form { margin-top: 10px; }
.add-form summary { cursor: pointer; color: var(--accent); font-size: 14px; padding: 6px 0; }
.add-form form { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; }
.add-form label { font-size: 13px; font-weight: 500; }
.add-form input, .add-form textarea {
  font: 15px/1.5 var(--font); padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--bg); color: var(--text);
}
.add-form .btn { align-self: flex-start; margin-top: 6px; }
.msg-ai .tool-status { font-size: 13px; color: var(--muted); margin: 0 0 6px; display: flex; gap: 6px; align-items: center; }
.msg-ai .tool-status::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: var(--accent); flex: none; }
@media (max-width: 768px) { .customize-btn { padding: 0 10px; } }

/* ---------- Attachments, voice, toast ---------- */
.toast { position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: var(--radius); font-size: 14px; z-index: 50; max-width: calc(100vw - 32px); }
.attachments { max-width: 760px; margin: 0 auto 6px; display: flex; flex-wrap: wrap; gap: 6px; }
.attachments:empty { display: none; }
.chip { display: inline-flex; align-items: center; gap: 4px; font-size: 13px; padding: 4px 4px 4px 10px; border: 1px solid var(--border); border-radius: 999px; background: var(--surface); max-width: 100%; }
.chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip button { background: none; border: 0; color: var(--muted); cursor: pointer; min-width: 24px; min-height: 24px; border-radius: 50%; }
.msg-user { flex-direction: column; align-items: flex-end; }
.msg-user .chips { display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-end; margin-bottom: 6px; }
#mic[aria-pressed="true"] { color: #c0392b; background: color-mix(in srgb, #c0392b 12%, transparent); }
#mic[aria-pressed="true"] svg { animation: blink 1s steps(1) infinite; }
.composer .icon-btn { flex: none; }

/* ---------- Message actions, copy, search ---------- */
.msg-actions { display: flex; gap: 4px; margin-top: 4px; }
.msg-user .msg-actions { justify-content: flex-end; }
.msg-action { background: none; border: 0; color: var(--muted); font: 13px var(--font); padding: 4px 8px; border-radius: 6px; cursor: pointer; min-height: 28px; }
.msg-action:hover, .msg-action:focus-visible { background: var(--surface-2); color: var(--text); }
.msg-ai pre { position: relative; }
.copy-code { position: absolute; top: 8px; right: 8px; background: #2c2a27; color: var(--code-text); border: 1px solid #3a3835; border-radius: 6px; font: 12px var(--font); padding: 3px 8px; cursor: pointer; opacity: .85; }
.copy-code:hover, .copy-code:focus-visible { opacity: 1; }
.chat-search { width: calc(100% - 8px); margin: 0 4px 4px; height: 36px; padding: 0 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); color: var(--text); font: 14px var(--font); }
.chat-item .rename { display: inline-flex; align-items: center; }
.key-form { display: flex; gap: 8px; }
.key-form input { flex: 1; min-width: 0; font: 15px var(--font); padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); color: var(--text); }

/* ---------- Answer formatting: code colors, tables, headings ---------- */
.msg-ai pre code.hljs { background: transparent; padding: 0; }
.msg-ai .md-heading { font-size: 16px; margin: 16px 0 8px; letter-spacing: -.01em; }
.msg-ai h3.md-heading { font-size: 18px; }
.table-wrap { overflow-x: auto; margin: 0 0 12px; border: 1px solid var(--border); border-radius: var(--radius); }
.msg-ai table { border-collapse: collapse; width: 100%; font-size: 14px; }
.msg-ai th, .msg-ai td { text-align: left; padding: 8px 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
.msg-ai th { background: var(--surface-2); font-weight: 600; white-space: nowrap; }
.msg-ai tr:last-child td { border-bottom: 0; }
.msg-ai ul, .msg-ai ol { margin: 0 0 12px; padding-left: 22px; }
.msg-ai li { margin: 2px 0; }
.limit-notice { max-width: 760px; margin: 0 auto 6px; padding: 8px 12px; border-radius: var(--radius); font-size: 14px; background: color-mix(in srgb, var(--accent) 12%, transparent); border: 1px solid color-mix(in srgb, var(--accent) 35%, transparent); }
.install-btn { margin-bottom: 8px; }

/* ---------- Voice mode ---------- */
.voice { width: 100vw; height: 100dvh; max-width: none; max-height: none; margin: 0; border: 0; padding: 16px; background: #141312; color: #ecebe8; }
.voice::backdrop { background: #141312; }
.voice[open] { display: grid; place-items: center; }
.voice-inner { width: min(560px, 100%); display: flex; flex-direction: column; align-items: center; gap: 20px; text-align: center; }
.voice-label { font: 13px var(--mono); color: #9a958d; margin: 0; }
.orb { position: relative; width: 200px; height: 200px; border: 0; background: none; padding: 0; cursor: pointer; border-radius: 50%; --level: 0; }
.orb:focus-visible { outline: 3px solid #e07a5f; outline-offset: 8px; }
.orb-core {
  position: absolute; inset: 30px; border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #f3a58c, #c8553d 55%, #7a2e1f);
  transform: scale(calc(1 + var(--level) * .35)); transition: transform .08s linear;
}
.orb-ring { position: absolute; inset: 30px; border-radius: 50%; border: 2px solid #e07a5f; opacity: 0; }
.orb[data-state="listening"] .orb-ring { animation: ripple 2s ease-out infinite; }
.orb[data-state="listening"] .orb-ring.two { animation-delay: 1s; }
.orb[data-state="thinking"] .orb-core { animation: breathe 1.2s ease-in-out infinite; }
.orb[data-state="speaking"] .orb-core { animation: talk .35s ease-in-out infinite alternate; }
.orb[data-state="muted"] .orb-core, .orb[data-state="idle"] .orb-core { filter: grayscale(.8) brightness(.7); }
@keyframes ripple { from { transform: scale(1); opacity: .7; } to { transform: scale(1.6); opacity: 0; } }
@keyframes breathe { 50% { transform: scale(.88); opacity: .8; } }
@keyframes talk { from { transform: scale(.96); } to { transform: scale(1.08); } }
.voice-status { font-size: 20px; font-weight: 500; margin: 0; min-height: 1.5em; }
.voice-transcript { min-height: 96px; max-height: 30dvh; overflow-y: auto; display: grid; gap: 8px; width: 100%; }
.voice-transcript p { margin: 0; overflow-wrap: anywhere; }
.voice-transcript .said { color: #9a958d; }
.voice-transcript .said:not(:empty)::before { content: "You: "; color: #e07a5f; }
.voice-transcript .reply { font-size: 17px; line-height: 1.5; }
.voice-actions { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; }
.voice-actions .btn:not(.btn-primary) { background: transparent; color: #ecebe8; border-color: #3a3734; }
.voice-actions .btn[aria-pressed="true"] { border-color: #e07a5f; color: #e07a5f; }
.voice-hint { font-size: 13px; color: #9a958d; margin: 0; }
@media (prefers-reduced-motion: reduce) { .orb-core { transition: none; } }
.settings-link { margin-bottom: 8px; width: 100%; }
.msg-images { display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-end; margin-bottom: 6px; }
.msg-images img { max-width: 220px; max-height: 180px; border-radius: var(--radius); border: 1px solid var(--border); object-fit: cover; }
