:root {
  --bg: #ffffff; --panel: #f7f7f8; --text: #1a1a1a; --muted: #6b6b6b;
  --border: #e3e3e6; --accent: #4f46e5; --accent-fg: #fff;
  --user-bubble: #f0f0f2; --code-bg: #f2f2f4; --shadow: 0 1px 2px rgba(0,0,0,.06);
}
body[data-theme="dark"] {
  --bg: #101012; --panel: #17171a; --text: #ececee; --muted: #9a9aa2;
  --border: #2a2a2f; --accent: #7c73ff; --user-bubble: #1e1e23; --code-bg: #1c1c20;
}
@media (prefers-color-scheme: dark) {
  body[data-theme="system"] {
    --bg: #101012; --panel: #17171a; --text: #ececee; --muted: #9a9aa2;
    --border: #2a2a2f; --accent: #7c73ff; --user-bubble: #1e1e23; --code-bg: #1c1c20;
  }
}
* { box-sizing: border-box; }
/* the `hidden` attribute must win over our display rules, or hidden panels still capture clicks */
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.6 ui-sans-serif, -apple-system, "Segoe UI", Roboto, sans-serif;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); }

/* ---------- login ---------- */
.login-wrap { display: grid; place-items: center; min-height: 100%; padding: 24px; }
.login-card {
  width: 340px; background: var(--panel); border: 1px solid var(--border);
  border-radius: 14px; padding: 28px; display: grid; gap: 10px; box-shadow: var(--shadow);
}
.login-card h1 { margin: 0; font-size: 22px; }
.login-card .sub { margin: 0 0 8px; color: var(--muted); font-size: 13px; }
.login-card input { padding: 10px 12px; border-radius: 9px; border: 1px solid var(--border); background: var(--bg); }
.primary { background: var(--accent); color: var(--accent-fg); border: 0; border-radius: 9px; padding: 10px 14px; font-weight: 600; }
.linkish { background: none; border: 0; color: var(--muted); padding: 4px; font-size: 13px; }
.linkish:hover { color: var(--text); text-decoration: underline; }
.err { color: #d33; font-size: 13px; min-height: 16px; margin: 0; }
.wide { width: 100%; }
.ghost { background: transparent; border: 1px solid var(--border); border-radius: 9px; padding: 8px 12px; }
.ghost:hover { background: var(--user-bubble); }

/* ---------- layout ---------- */
#app { display: grid; grid-template-columns: 264px 1fr; height: 100%; }
/* 3 children: header, scrollable list, footer. The middle row is the only flexible one. */
.sidebar {
  background: var(--panel); border-right: 1px solid var(--border);
  display: grid; grid-template-rows: auto 1fr auto; gap: 8px; padding: 10px; min-height: 0;
}
.search { padding: 8px 10px; border-radius: 8px; border: 1px solid var(--border); background: var(--bg); }
.conv-list { overflow-y: auto; display: grid; gap: 0; align-content: start; min-height: 0; }
.conv {
  padding: 3px 6px; border-radius: 6px; cursor: pointer; display: grid;
  grid-template-columns: 1fr 18px; gap: 0 4px; align-items: center;
}
.conv:hover, .conv.active { background: var(--user-bubble); }
.conv.active { font-weight: 600; }

/* active chat: rainbow glow travelling around the edges. Painted above the row
   background but below the text — no negative z-index, which would fall behind
   the sidebar fill. */
.conv.active { position: relative; }
.conv.active::before {
  content: ""; position: absolute; inset: -1.5px; border-radius: 7.5px; z-index: 0;
  padding: 1.5px; pointer-events: none;
  background: conic-gradient(from var(--a), #ff4d4d, #ffb03a, #ffe94d, #52e07a,
    #3ad6e0, #4d7cff, #b44dff, #ff4dc4, #ff4d4d);
  -webkit-mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
  -webkit-mask-composite: xor; mask-composite: exclude;
  animation: rainbow-spin 3.4s linear infinite;
}
.conv.active > * { position: relative; z-index: 1; }
@property --a { syntax: "<angle>"; initial-value: 0deg; inherits: false; }
@keyframes rainbow-spin { to { --a: 360deg; } }
@media (prefers-reduced-motion: reduce) {
  .conv.active::before { animation: none; --a: 130deg; }
}
.conv .t {
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  font-size: 12.5px; line-height: 1.25; display: flex; align-items: center; gap: 4px;
}
.conv .pinmark { font-size: 9px; flex: none; }
.conv .m {
  grid-column: 1 / 2; color: var(--muted); font-size: 10px; line-height: 1.15;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: 1px;
}
.conv .more {
  grid-row: 1 / 3; grid-column: 2 / 3; align-self: center;
  border: 0; background: none; color: var(--muted); font-size: 15px; line-height: 1;
  padding: 1px 2px; border-radius: 4px; opacity: .55;
}
.conv:hover .more, .conv.active .more { opacity: 1; }
.conv .more:hover { background: var(--border); color: var(--text); }
/* pin mark: a text glyph, not a colour emoji, so it matches the UI in both themes */
.conv .pinmark { font-size: 9px; flex: none; color: var(--accent); }
.conv-sep {
  font-size: 9.5px; text-transform: uppercase; letter-spacing: .07em;
  color: var(--muted); padding: 7px 6px 3px; opacity: .8;
}
.conv-none { padding: 8px 8px; color: var(--muted); font-size: 12px; }

/* per-chat context menu */
.conv-menu {
  position: fixed; z-index: 40; min-width: 132px; padding: 4px;
  background: var(--bg); border: 1px solid var(--border); border-radius: 9px;
  box-shadow: 0 6px 22px rgba(0,0,0,.16);
}
.menu-item {
  display: block; width: 100%; text-align: left; border: 0; background: none;
  padding: 6px 9px; border-radius: 6px; font-size: 12.5px;
}
.menu-item:hover { background: var(--user-bubble); }
.menu-item.danger { color: #d33; }
.menu-item.danger:hover { background: rgba(221,51,51,.1); }
/* logout (left) · settings (middle) · new chat (right) — compact icon buttons */
.side-foot {
  display: flex; align-items: center; gap: 6px;
  border-top: 1px solid var(--border); padding-top: 8px;
}
.icon-btn {
  width: 30px; height: 30px; flex: none;
  display: flex; align-items: center; justify-content: center;
  background: none; border: 1px solid transparent; border-radius: 7px;
  color: var(--muted); padding: 0;
}
.icon-btn:hover { color: var(--text); background: var(--user-bubble); }
.primary-ic { margin-left: auto; color: var(--accent-fg); background: var(--accent); }
.primary-ic:hover { color: var(--accent-fg); background: var(--accent); filter: brightness(1.08); }
/* new chat gets a box spanning the free space, so it's obvious what the + does */
.primary-ic.labelled {
  width: auto; gap: 7px; padding: 0 12px 0 9px;
  font-size: 12.5px; font-weight: 500; white-space: nowrap;
}
.primary-ic.labelled .lbl { line-height: 1; }

/* ---------- narrow / phone ---------- */
.only-narrow { display: none; }
#scrim { display: none; }
@media (max-width: 760px) {
  .only-narrow { display: inline-block; }
  /* the sidebar becomes an overlay drawer, otherwise there is no way to switch
     chats or reach settings on a phone */
  #app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; width: 82vw; max-width: 300px; z-index: 30;
    transform: translateX(-100%); transition: transform .18s ease;
    box-shadow: 0 0 30px rgba(0,0,0,.28);
  }
  body.nav-open .sidebar { transform: translateX(0); }
  body.nav-open #scrim {
    display: block; position: fixed; inset: 0; z-index: 25; background: rgba(0,0,0,.4);
  }
  .topbar { padding: 8px 12px; }
  .tb-right .pill { font-size: 11px; padding: 2px 7px; }
  #chat-model { display: none; }
  .composer-wrap { padding: 6px 10px 10px; }
  .toolbar { gap: 7px; font-size: 12px; }
  .ctl select { max-width: 128px; }
  .msg { margin-bottom: 16px; }
  .drawer-panel { width: 100%; max-width: 100%; }
}

.main { display: grid; grid-template-rows: auto 1fr auto; min-height: 0; min-width: 0; }
.topbar { display: flex; justify-content: space-between; align-items: center; padding: 10px 16px; border-bottom: 1px solid var(--border); gap: 12px; }
.tb-left { display: flex; align-items: baseline; gap: 10px; min-width: 0; }
.who { color: var(--muted); font-size: 12.5px; }
.conv-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tb-right { display: flex; gap: 6px; }
.pill { border: 1px solid var(--border); border-radius: 999px; padding: 2px 9px; font-size: 11.5px; color: var(--muted); white-space: nowrap; }
.cost-pill { color: var(--text); font-weight: 600; border-color: var(--border); }
.toast {
  max-width: 780px; margin: 0 auto 6px; padding: 5px 10px; text-align: center;
  font-size: 11.5px; color: var(--muted); background: var(--code-bg);
  border: 1px solid var(--border); border-radius: 8px;
}

.thread { overflow-y: auto; padding: 12px 16px 4px; min-height: 0; }
/* messages sit close together: 5px gap, and the hover action row is taken OUT of
   flow so it doesn't reserve invisible height under every bubble */
.msg { margin: 0 auto 5px; max-width: 780px; position: relative; }
.msg .role { font-size: 11.5px; color: var(--muted); margin-bottom: 4px; display: flex; gap: 8px; }
.msg .prov { opacity: .7; }
.bubble { padding: 8px 12px; border-radius: 11px; word-wrap: break-word; border: 1px solid var(--border); }
.msg.user .bubble { background: var(--user-bubble); border-color: transparent; white-space: pre-wrap; }
.msg.assistant .bubble { background: var(--panel); border-color: transparent; padding: 10px 16px; }
.bubble > :first-child { margin-top: 0; }
.bubble > :last-child { margin-bottom: 0; }
.bubble h1 { font-size: 18px; } .bubble h2 { font-size: 15.5px; } .bubble h3 { font-size: 14px; }
.bubble p { margin: 5px 0; }
.bubble ul, .bubble ol { margin: 4px 0; padding-left: 19px; }
.bubble ul { list-style: disc outside; }
.bubble ul ul { list-style: circle outside; }
.bubble ol { list-style: decimal outside; }
.bubble li { display: list-item; }
.bubble hr { border: 0; border-top: 1px solid var(--border); margin: 14px 0; }
.bubble blockquote { margin: 8px 0; padding: 2px 12px; border-left: 3px solid var(--border); color: var(--muted); }
.bubble code { background: var(--code-bg); padding: 1px 5px; border-radius: 5px; font-size: 13px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.bubble img { max-width: 100%; border-radius: 8px; }
.bubble table { border-collapse: collapse; margin: 10px 0; font-size: 13.5px; display: block; overflow-x: auto; }
.bubble th, .bubble td { border: 1px solid var(--border); padding: 6px 10px; text-align: left; }
.bubble th { background: var(--code-bg); font-weight: 600; }
.codeblk { margin: 10px 0; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; background: var(--code-bg); }
.codehead { display: flex; justify-content: space-between; align-items: center; padding: 4px 10px;
  border-bottom: 1px solid var(--border); font-size: 11.5px; color: var(--muted); }
.codehead .copy { background: none; border: 1px solid var(--border); border-radius: 6px; color: var(--muted);
  font-size: 11px; padding: 2px 8px; }
.codehead .copy:hover { color: var(--text); }
.codeblk pre { margin: 0; padding: 12px; overflow-x: auto; }
.codeblk code { background: none; padding: 0; font-size: 13px; line-height: 1.5; }
.codeblk .k { color: #b453d6; font-weight: 600; }
.codeblk .s { color: #0a7d55; }
.codeblk .c { color: var(--muted); font-style: italic; }
/* per-message cost sits at the end of the reply, right-aligned */
.msg .meta {
  color: var(--muted); font-size: 11px; margin-top: 6px; display: flex;
  gap: 10px; justify-content: flex-end; align-items: baseline;
}
.msg .meta .na { opacity: .7; font-style: italic; }
.msg .meta .cost { font-weight: 600; color: var(--text); }
/* hover actions float over the bubble instead of sitting below it */
.msg-actions {
  position: absolute; top: 4px; right: 8px; z-index: 2;
  display: flex; gap: 2px; padding: 1px 4px; border-radius: 7px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  opacity: 0; transition: opacity .12s; pointer-events: none;
}
.msg:hover .msg-actions, .msg-actions:focus-within { opacity: 1; pointer-events: auto; }
.msg .msg-actions .linkish { font-size: 11.5px; padding: 2px 5px; }
.reasoning { margin-top: 6px; font-size: 12.5px; color: var(--muted); }
.reasoning summary { cursor: pointer; }
.reasoning pre { white-space: pre-wrap; background: var(--code-bg); padding: 8px 10px; border-radius: 8px; font-size: 12px; }
.attachments { margin-top: 6px; display: grid; gap: 3px; font-size: 12px; color: var(--muted); }
.err-inline { color: #d33; }
/* typing indicator shown until the first token arrives */
.typing { display: inline-flex; gap: 4px; padding: 4px 0; }
.typing i { width: 6px; height: 6px; border-radius: 50%; background: var(--muted); opacity: .45; animation: blink 1.3s infinite; }
.typing i:nth-child(2) { animation-delay: .18s; }
.typing i:nth-child(3) { animation-delay: .36s; }
@keyframes blink { 0%, 60%, 100% { opacity: .25; } 30% { opacity: .9; } }
@media (prefers-reduced-motion: reduce) { .typing i { animation: none; opacity: .6; } }
.empty { color: var(--muted); text-align: center; margin-top: 12vh; }
.empty h2 { margin: 0 0 6px; color: var(--text); }

/* ---------- composer ---------- */
.composer-wrap { padding: 8px 16px 12px; }
.composer { display: flex; gap: 8px; align-items: flex-end; border: 1px solid var(--border);
  border-radius: 13px; padding: 7px; background: var(--panel); max-width: 780px; margin: 0 auto; }
.composer textarea { flex: 1; border: 0; background: none; resize: none; outline: none;
  max-height: 200px; padding: 7px 2px; line-height: 1.5; }
.icon { background: none; border: 0; font-size: 17px; padding: 6px; border-radius: 8px; }
.icon:hover { background: var(--user-bubble); }
/* attachment button: double size, so it reads as a first-class action */
.attach-btn {
  padding: 3px 6px; border-radius: 10px; color: var(--muted);
  align-self: flex-end; display: flex; align-items: center;
}
.attach-btn:hover { color: var(--text); }
.attach-btn svg { width: 30px; height: 30px; display: block; }
.send { width: 32px; height: 32px; border-radius: 9px; border: 0; background: var(--accent); color: var(--accent-fg); font-size: 15px; }
.send.stop { background: #d33; font-size: 12px; }
/* toolbar icon buttons: 4 equal pills on one line, 15px icons */
.chip.ic {
  display: inline-flex; align-items: center; justify-content: center;
  width: 32px; height: 30px; padding: 0; flex: none; border-radius: 9px;
}
.chip.ic svg { width: 15px; height: 15px; display: block; }
.chip.ic:hover { color: var(--text); border-color: var(--muted); }
.chip.ic.on { color: var(--accent-fg); }
.toolbar { max-width: 780px; margin: 8px auto 0; display: flex; gap: 7px;
  align-items: center; flex-wrap: nowrap; font-size: 12.5px; color: var(--muted); }
.ctl { display: flex; align-items: center; gap: 5px; }
.ctl.inline { cursor: pointer; }
/* toggle buttons: always visible, state shown by fill — no checkboxes */
.chip {
  border: 1px solid var(--border); background: var(--bg); color: var(--muted);
  border-radius: 999px; padding: 3px 11px; font-size: 12.5px; line-height: 1.5;
}
.chip:hover { color: var(--text); border-color: var(--muted); }
.chip.on { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); font-weight: 500; }
/* settings gear in the toolbar: same pill size as the chips, icon only */
.chip-icon {
  display: inline-flex; align-items: center; justify-content: center;
  padding: 3px 9px; color: var(--muted);
}
.chip-icon:hover { color: var(--text); border-color: var(--muted); }
.ctl select {
  background: var(--bg); border: 1px solid var(--border); border-radius: 9px;
  padding: 6px 8px; font-size: 12.5px; height: 30px; max-width: 200px;
  flex: 0 1 auto; min-width: 0;
}
.attach-list { max-width: 780px; margin: 0 auto 6px; display: grid; gap: 3px; font-size: 12px; color: var(--muted); }

/* ---------- drawer ---------- */
.drawer { position: fixed; inset: 0; background: rgba(0,0,0,.35); display: flex; justify-content: flex-end; z-index: 20; }
.drawer-panel { width: 440px; max-width: 94vw; background: var(--bg); border-left: 1px solid var(--border); overflow-y: auto; padding: 16px 18px 40px; }
.drawer-head { display: flex; justify-content: space-between; align-items: center; }
.drawer-head h2 { margin: 0; font-size: 18px; }
.sec { border-top: 1px solid var(--border); margin-top: 14px; padding-top: 12px; }
.sec h3 { margin: 0 0 9px; font-size: 12.5px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.row { display: grid; grid-template-columns: 1fr 1.2fr; gap: 8px; align-items: center; margin-bottom: 8px; font-size: 13.5px; }
.row.col { grid-template-columns: 1fr; }
.row select, .row input, .sec > input, .sec textarea { padding: 7px 9px; border-radius: 8px; border: 1px solid var(--border); background: var(--panel); width: 100%; }
.hint { color: var(--muted); font-size: 12px; margin: 4px 0 8px; }
.sec button.ghost { margin-right: 6px; margin-top: 4px; }
.cost-box { font-size: 13px; color: var(--muted); display: grid; gap: 3px; }
.cost-box b { color: var(--text); }
#save-settings { margin-top: 16px; }