:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --text: #1b1f24;
  --muted: #6b7280;
  --border: #e3e6ea;
  --accent: #4f46e5;
  --accent-text: #ffffff;
  --user-bubble: #4f46e5;
  --user-text: #ffffff;
  --bot-bubble: #ffffff;
  --code-bg: #f1f3f6;
  --ok: #16a34a;
  --warn: #d97706;
  --err: #dc2626;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.04);
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --panel: #171a20;
    --text: #e7e9ee;
    --muted: #9aa3af;
    --border: #2a2f38;
    --accent: #818cf8;
    --accent-text: #0f1115;
    --user-bubble: #4f46e5;
    --user-text: #ffffff;
    --bot-bubble: #1d2129;
    --code-bg: #11141a;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, sans-serif;
}
button { font: inherit; color: inherit; cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: .5; }

.app { display: flex; flex-direction: column; height: 100dvh; max-width: 920px; margin: 0 auto; }

/* ---- top bar ---- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 16px; border-bottom: 1px solid var(--border); background: var(--bg);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand > div:last-child { min-width: 0; }
.logo {
  width: 36px; height: 36px; border-radius: 10px; display: grid; place-items: center;
  background: var(--accent); color: var(--accent-text); font-size: 18px; flex: none;
}
.title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.status { white-space: nowrap; overflow: hidden; font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.dot.ok { background: var(--ok); }
.dot.err { background: var(--err); }
.actions { display: flex; gap: 6px; flex: none; }

.btn {
  border: 1px solid var(--border); background: var(--panel); border-radius: 10px;
  padding: 7px 12px; font-size: 14px;
}
.btn:hover:not(:disabled) { border-color: var(--accent); }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--err); }
.icon-btn {
  border: none; background: transparent; border-radius: 10px; width: 38px; height: 38px;
  display: grid; place-items: center; font-size: 18px; flex: none;
}
.icon-btn:hover:not(:disabled) { background: var(--code-bg); }

/* ---- chat ---- */
.chat { flex: 1; overflow-y: auto; padding: 20px 16px; position: relative; }
.empty { text-align: center; margin: 12vh auto 0; max-width: 360px; color: var(--muted); }
.empty h2 { color: var(--text); margin: 12px 0 4px; font-size: 20px; }
.empty-logo {
  width: 56px; height: 56px; margin: 0 auto; border-radius: 16px; display: grid; place-items: center;
  background: var(--accent); color: var(--accent-text); font-size: 26px;
}
.empty .btn { margin-top: 12px; }
.empty.hidden { display: none; }

.messages { display: flex; flex-direction: column; gap: 14px; }
.msg { display: flex; flex-direction: column; max-width: 82%; }
.msg.user { align-self: flex-end; align-items: flex-end; }
.msg.bot { align-self: flex-start; }
.bubble {
  padding: 10px 14px; border-radius: var(--radius); word-wrap: break-word; overflow-wrap: anywhere;
}
.msg.user .bubble { background: var(--user-bubble); color: var(--user-text); border-bottom-right-radius: 4px; white-space: pre-wrap; }
.msg.bot .bubble { background: var(--bot-bubble); border: 1px solid var(--border); border-bottom-left-radius: 4px; box-shadow: var(--shadow); }
.msg.error .bubble { border-color: var(--err); color: var(--err); }
.bubble > :first-child { margin-top: 0; }
.bubble > :last-child { margin-bottom: 0; }
.bubble pre { background: var(--code-bg); padding: 10px 12px; border-radius: 8px; overflow-x: auto; font-size: 13px; }
.bubble code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .9em; }
.bubble :not(pre) > code { background: var(--code-bg); padding: 1px 5px; border-radius: 5px; }
.bubble img { max-width: 100%; border-radius: 8px; }
.bubble a { color: var(--accent); }
.bubble table { border-collapse: collapse; }
.bubble th, .bubble td { border: 1px solid var(--border); padding: 4px 8px; }
.meta { font-size: 11px; color: var(--muted); margin-top: 4px; padding: 0 4px; }

.msg-files { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; justify-content: flex-end; }
.msg-files img { max-width: 220px; max-height: 180px; border-radius: 10px; border: 1px solid var(--border); display: block; }
.msg-files audio { max-width: 260px; }
.file-pill {
  display: inline-flex; align-items: center; gap: 6px; padding: 6px 10px; border-radius: 10px;
  background: var(--panel); border: 1px solid var(--border); font-size: 13px;
}

.sources { margin-top: 8px; display: flex; flex-wrap: wrap; gap: 6px; }
.sources summary { font-size: 12px; color: var(--muted); cursor: pointer; }
.source { font-size: 12px; background: var(--code-bg); border-radius: 8px; padding: 6px 8px; margin-top: 6px; white-space: pre-wrap; max-height: 140px; overflow: auto; }

.typing { display: inline-flex; gap: 4px; padding: 4px 0; }
.typing span { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite ease-in-out; }
.typing span:nth-child(2) { animation-delay: .15s; }
.typing span:nth-child(3) { animation-delay: .3s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

/* ---- composer ---- */
.composer-wrap { padding: 8px 16px 14px; position: relative; }
.composer {
  display: flex; align-items: flex-end; gap: 4px; background: var(--panel);
  border: 1px solid var(--border); border-radius: 18px; padding: 6px; box-shadow: var(--shadow);
}
.composer:focus-within { border-color: var(--accent); }
.composer textarea {
  flex: 1; border: none; outline: none; resize: none; background: transparent; color: var(--text);
  font: inherit; padding: 8px 6px; max-height: 200px; min-height: 38px;
}
.send-btn {
  width: 38px; height: 38px; border-radius: 12px; border: none; flex: none;
  background: var(--accent); color: var(--accent-text); font-size: 16px;
}
#micBtn.recording { background: var(--err); color: #fff; animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { opacity: .7; } }
.composer-note { font-size: 12px; color: var(--muted); text-align: center; margin-top: 6px; min-height: 16px; }
.composer-note.err { color: var(--err); }

.attachments { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.attachments:empty { display: none; }
.chip {
  position: relative; display: flex; align-items: center; gap: 8px; padding: 6px 28px 6px 6px;
  border: 1px solid var(--border); background: var(--panel); border-radius: 12px; font-size: 13px; max-width: 240px;
}
.chip img { width: 40px; height: 40px; object-fit: cover; border-radius: 8px; }
.chip .ico { width: 40px; height: 40px; display: grid; place-items: center; background: var(--code-bg); border-radius: 8px; font-size: 18px; flex: none; }
.chip .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip .size { color: var(--muted); font-size: 11px; }
.chip .x {
  position: absolute; top: 4px; right: 4px; width: 20px; height: 20px; border: none; border-radius: 50%;
  background: var(--code-bg); font-size: 11px; line-height: 20px; padding: 0;
}
.drop-hint {
  display: none; position: absolute; inset: -6px 10px 6px; border: 2px dashed var(--accent);
  border-radius: 18px; background: color-mix(in srgb, var(--accent) 10%, var(--bg));
  place-items: center; font-weight: 600; color: var(--accent); z-index: 2; pointer-events: none;
}
body.dragging .drop-hint { display: grid; }

/* ---- settings dialog ---- */
dialog {
  border: 1px solid var(--border); border-radius: 16px; padding: 0; width: min(560px, calc(100vw - 32px));
  background: var(--panel); color: var(--text); box-shadow: 0 20px 60px rgba(0,0,0,.25);
}
dialog::backdrop { background: rgba(0,0,0,.4); }
.settings { padding: 18px 20px; display: flex; flex-direction: column; }
.settings header, .settings footer { display: flex; align-items: center; gap: 8px; }
.settings header { justify-content: space-between; margin-bottom: 8px; }
.settings h3 { margin: 0; font-size: 17px; }
.settings label { font-weight: 600; font-size: 13px; margin-top: 12px; }
.settings input[type=text], .settings input[type=password] {
  margin-top: 6px; padding: 9px 11px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text); font: 13px ui-monospace, SFMono-Regular, Menlo, monospace; width: 100%;
}
.settings input:focus { outline: 2px solid color-mix(in srgb, var(--accent) 40%, transparent); border-color: var(--accent); }
.settings .check { display: flex; gap: 8px; align-items: flex-start; font-weight: 400; }
.settings .check input { margin-top: 4px; }
.hint { font-size: 12px; color: var(--muted); margin: 6px 0 0; }
.muted { color: var(--muted); font-weight: 400; }
.spacer { flex: 1; }
.settings footer { margin-top: 18px; }

.checks { list-style: none; padding: 0; margin: 14px 0 0; display: flex; flex-direction: column; gap: 6px; }
.checks:empty { display: none; }
.checks li { display: flex; gap: 8px; font-size: 13px; padding: 8px 10px; border-radius: 10px; background: var(--bg); }
.checks .ic { flex: none; width: 18px; text-align: center; font-weight: 700; }
.checks .ok .ic { color: var(--ok); }
.checks .bad .ic { color: var(--err); }
.checks .unk .ic { color: var(--warn); }
.checks .nm { font-weight: 600; }
.checks .dt { color: var(--muted); overflow-wrap: anywhere; }
.checks .summary { font-weight: 600; justify-content: center; }
.checks .summary.ok { color: var(--ok); }
.checks .summary.bad { color: var(--err); }

@media (max-width: 560px) {
  .msg { max-width: 92%; }
  .btn.ghost { padding: 7px 10px; }
  .lbl { display: none; }
  .topbar { padding: 10px 12px; }
  .chat { padding: 16px 12px; }
  .composer-wrap { padding: 8px 12px 12px; }
}
