/* ============ tokens ============ */
:root {
  --bg: #ffffff;
  --bg-chat: #ffffff;
  --surface: #ffffff;
  --surface-2: #f4f4f6;
  --border: #e7e7ec;
  --border-strong: #d7d7de;
  --text: #1c1c20;
  --muted: #71717a;
  --accent: #5b57e0;
  --accent-2: #a855f7;
  --accent-ring: rgba(91, 87, 224, .28);
  --on-accent: #ffffff;
  --user-bubble: #f1f2f6;
  --code-bg: #f5f5f7;
  --danger: #dc2626;
  --shadow-sm: 0 1px 2px rgba(20, 20, 40, .06);
  --shadow-md: 0 10px 34px rgba(20, 20, 40, .14);
  --shadow-composer: 0 4px 24px rgba(20, 20, 40, .08);
  --radius: 14px;
}

:root[data-theme="dark"] {
  --bg: #0f1013;
  --bg-chat: #0f1013;
  --surface: #17181d;
  --surface-2: #212228;
  --border: rgba(255, 255, 255, .09);
  --border-strong: rgba(255, 255, 255, .16);
  --text: #ececee;
  --muted: #9a9aa6;
  --accent: #8a8fff;
  --accent-2: #c084fc;
  --accent-ring: rgba(138, 143, 255, .30);
  --on-accent: #0d0e18;
  --user-bubble: #23242c;
  --code-bg: #0b0c11;
  --danger: #ff7070;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, .4);
  --shadow-md: 0 14px 44px rgba(0, 0, 0, .55);
  --shadow-composer: 0 4px 28px rgba(0, 0, 0, .45);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    --bg: #0f1013; --bg-chat: #0f1013; --surface: #17181d; --surface-2: #212228;
    --border: rgba(255,255,255,.09); --border-strong: rgba(255,255,255,.16);
    --text: #ececee; --muted: #9a9aa6; --accent: #8a8fff; --accent-2: #c084fc;
    --accent-ring: rgba(138,143,255,.30); --on-accent: #0d0e18; --user-bubble: #23242c;
    --code-bg: #0b0c11; --danger: #ff7070;
    --shadow-sm: 0 1px 2px rgba(0,0,0,.4); --shadow-md: 0 14px 44px rgba(0,0,0,.55);
    --shadow-composer: 0 4px 28px rgba(0,0,0,.45);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.6 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  display: flex;
  flex-direction: column;
  -webkit-font-smoothing: antialiased;
}
.muted { color: var(--muted); }

.brand-logo, .empty-logo, .avatar {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  border-radius: 8px;
  flex: none;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 6px;
}

/* ============ login ============ */
.login-body { align-items: center; justify-content: center; }
.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 34px 32px;
  width: min(370px, 92vw);
  margin: auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
  box-shadow: var(--shadow-md);
}
.login-logo {
  width: 40px; height: 40px; border-radius: 11px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  margin-bottom: 2px;
}
.login-card h1 { margin: 0; font-size: 22px; letter-spacing: -.01em; }
.login-card .muted { margin: -6px 0 6px; font-size: 14px; }
.login-card input {
  background: var(--bg); border: 1px solid var(--border-strong);
  color: var(--text); border-radius: 10px; padding: 12px 13px; font-size: 15px;
  transition: border-color .15s, box-shadow .15s;
}
.login-card input:focus {
  outline: none; border-color: var(--accent); box-shadow: 0 0 0 4px var(--accent-ring);
}
.login-card button {
  background: var(--accent); color: var(--on-accent); border: 0; border-radius: 10px;
  padding: 12px; font-weight: 600; cursor: pointer; font-size: 15px; margin-top: 4px;
  transition: filter .15s;
}
.login-card button:hover { filter: brightness(1.06); }
.login-error { color: var(--danger); min-height: 18px; font-size: 13px; }

/* ============ topbar ============ */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px; min-height: 56px;
  border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(10px);
  position: sticky; top: 0; z-index: 40;
}
.brand { display: flex; align-items: center; gap: 9px; font-weight: 650; letter-spacing: -.01em; }
.brand-logo { width: 24px; height: 24px; }
.topbar-tools { display: flex; align-items: center; gap: 6px; }
.inline { margin: 0; display: flex; }

.icon-ghost {
  display: inline-flex; align-items: center; justify-content: center;
  width: 38px; height: 38px; border-radius: 10px;
  background: transparent; color: var(--muted); border: 1px solid transparent;
  cursor: pointer; font-size: 16px; transition: background .15s, color .15s;
}
.icon-ghost:hover { background: var(--surface-2); color: var(--text); }

/* ---- model picker ---- */
.model-picker { position: relative; }
.model-btn {
  display: inline-flex; align-items: center; gap: 7px;
  max-width: 44vw; height: 38px; padding: 0 12px;
  background: var(--surface-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 10px;
  cursor: pointer; font: inherit; font-size: 14px; font-weight: 550;
  transition: background .15s, border-color .15s;
}
.model-btn:hover { border-color: var(--border-strong); }
.mp-btn-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chev { flex: none; color: var(--muted); transition: transform .18s; }
.model-btn[aria-expanded="true"] .chev { transform: rotate(180deg); }

.model-panel {
  position: absolute; top: calc(100% + 8px); right: 0;
  width: min(380px, 88vw);
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; box-shadow: var(--shadow-md);
  padding: 8px; z-index: 60;
  display: flex; flex-direction: column; gap: 6px;
}
.model-panel[hidden] { display: none; }
.model-search {
  background: var(--bg); border: 1px solid var(--border-strong); color: var(--text);
  border-radius: 9px; padding: 9px 11px; font: inherit; font-size: 14px;
}
.model-search:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-ring); }
.model-list { list-style: none; margin: 0; padding: 2px; max-height: 340px; overflow-y: auto; }
.mp-row {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 8px 10px; border-radius: 9px; cursor: pointer;
}
.mp-row:hover, .mp-row.active { background: var(--surface-2); }
.mp-row-main { min-width: 0; display: flex; flex-direction: column; gap: 1px; }
.mp-row-name { font-size: 14px; font-weight: 550; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mp-row-meta { font-size: 12px; color: var(--muted); }
.mp-badges { display: flex; align-items: center; gap: 7px; flex: none; }
.mp-vis {
  font-size: 11px; font-weight: 600; letter-spacing: .02em;
  color: var(--accent); background: color-mix(in srgb, var(--accent) 14%, transparent);
  border-radius: 999px; padding: 2px 7px;
}
.mp-check { color: var(--accent); display: none; }
.mp-row[aria-selected="true"] .mp-check { display: inline-flex; }
.mp-empty { padding: 14px 10px; color: var(--muted); font-size: 14px; text-align: center; }

/* ============ chat ============ */
.chat { flex: 1; overflow-y: auto; background: var(--bg-chat); }
.messages {
  max-width: 780px; width: 100%; margin: 0 auto;
  padding: 26px 20px 12px;
  display: flex; flex-direction: column; gap: 22px;
}

/* empty state */
.empty {
  margin: auto; padding: 8vh 8px 4vh; text-align: center;
  display: flex; flex-direction: column; align-items: center; gap: 8px;
}
.empty-logo { width: 52px; height: 52px; border-radius: 15px; margin-bottom: 6px; }
.empty h1 { margin: 0; font-size: 26px; font-weight: 650; letter-spacing: -.02em; }
.empty .muted { margin: 0; max-width: 420px; }
.suggestions {
  margin-top: 18px; display: grid; gap: 10px; width: 100%;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}
.suggestion {
  text-align: left; background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 12px;
  padding: 13px 15px; font: inherit; font-size: 14px; cursor: pointer; line-height: 1.4;
  transition: border-color .15s, background .15s, transform .12s;
}
.suggestion:hover { border-color: var(--accent); transform: translateY(-1px); }

/* messages */
.msg { display: flex; gap: 12px; }
.msg.user { justify-content: flex-end; }
.msg.user .bubble {
  background: var(--user-bubble); border-radius: 20px 20px 6px 20px;
  padding: 11px 16px; max-width: 80%; overflow-wrap: anywhere; white-space: pre-wrap;
}
.avatar {
  width: 28px; height: 28px; border-radius: 9px; margin-top: 2px;
}
.msg-body { min-width: 0; flex: 1; }
.msg.assistant .bubble { overflow-wrap: anywhere; }
.msg-actions { margin-top: 6px; height: 26px; }
.msg-copy {
  display: inline-flex; align-items: center; gap: 5px;
  background: transparent; border: 1px solid transparent; color: var(--muted);
  border-radius: 7px; font: inherit; font-size: 12.5px; padding: 3px 8px;
  cursor: pointer; opacity: 0; transition: opacity .15s, background .15s, color .15s;
}
.msg.assistant:hover .msg-copy, .msg-copy:focus-visible { opacity: 1; }
.msg-copy:hover { background: var(--surface-2); color: var(--text); }

/* typing dots */
.dots { display: inline-flex; gap: 5px; padding: 6px 0; }
.dots i {
  width: 7px; height: 7px; border-radius: 50%; background: var(--muted);
  animation: blink 1.3s infinite ease-in-out both;
}
.dots i:nth-child(2) { animation-delay: .18s; }
.dots i:nth-child(3) { animation-delay: .36s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

/* ============ attachments ============ */
.chips {
  max-width: 780px; width: 100%; margin: 0 auto; padding: 0 20px;
  display: flex; flex-wrap: wrap; gap: 8px;
}
.chips:not(:empty) { margin-bottom: 8px; }
.chip {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 999px;
  padding: 5px 6px 5px 12px; font-size: 13px; display: flex; gap: 6px; align-items: center;
  max-width: 240px;
}
.chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip button {
  display: inline-flex; align-items: center; justify-content: center;
  width: 20px; height: 20px; border-radius: 50%;
  background: none; border: 0; color: var(--muted); cursor: pointer; font-size: 14px;
}
.chip button:hover { background: var(--border); color: var(--text); }

/* ============ composer ============ */
.composer-wrap {
  max-width: 780px; width: 100%; margin: 0 auto;
  padding: 8px 20px 18px;
  background: linear-gradient(to top, var(--bg) 62%, transparent);
  position: sticky; bottom: 0;
}
.composer {
  display: flex; align-items: flex-end; gap: 8px;
  background: var(--surface); border: 1px solid var(--border-strong);
  border-radius: 26px; padding: 7px 7px 7px 8px;
  box-shadow: var(--shadow-composer);
  transition: border-color .15s, box-shadow .15s;
}
.composer:focus-within { border-color: var(--accent); box-shadow: 0 0 0 4px var(--accent-ring); }
#input {
  flex: 1; resize: none; max-height: 220px; min-height: 26px;
  background: transparent; border: 0; color: var(--text);
  padding: 8px 4px; font: inherit; font-size: 15px; line-height: 1.5;
}
#input:focus { outline: none; }
#input::placeholder { color: var(--muted); }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center; flex: none;
  width: 40px; height: 40px; border-radius: 50%;
  border: 0; background: transparent; color: var(--muted); cursor: pointer;
  transition: background .15s, color .15s;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.send {
  display: inline-flex; align-items: center; justify-content: center; flex: none;
  width: 40px; height: 40px; border-radius: 50%;
  border: 0; background: var(--accent); color: var(--on-accent); cursor: pointer;
  transition: filter .15s, opacity .15s, transform .1s;
}
.send:hover:not(:disabled) { filter: brightness(1.08); }
.send:active:not(:disabled) { transform: scale(.94); }
.send:disabled { opacity: .4; cursor: default; }
.status {
  min-height: 18px; font-size: 12.5px; color: var(--muted);
  text-align: center; margin-top: 8px;
}
.status.error { color: var(--danger); }
.notice.error {
  background: color-mix(in srgb, var(--danger) 12%, transparent);
  border: 1px solid color-mix(in srgb, var(--danger) 34%, transparent);
  color: var(--danger); border-radius: 10px; padding: 10px 13px; font-size: 14px;
}

/* ============ markdown ============ */
.bubble p { margin: 0 0 10px; }
.bubble > :first-child { margin-top: 0; }
.bubble > :last-child { margin-bottom: 0; }
.bubble h1, .bubble h2, .bubble h3, .bubble h4 { margin: 16px 0 8px; line-height: 1.3; letter-spacing: -.01em; }
.bubble h1 { font-size: 1.4em; } .bubble h2 { font-size: 1.25em; }
.bubble h3 { font-size: 1.12em; } .bubble h4 { font-size: 1em; }
.bubble pre {
  background: var(--code-bg); border: 1px solid var(--border); border-radius: 12px;
  padding: 14px 16px; overflow-x: auto; margin: 10px 0; line-height: 1.5;
}
.bubble code { font-family: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, monospace; font-size: 13.5px; }
.bubble :not(pre) > code {
  background: var(--code-bg); border: 1px solid var(--border);
  padding: 1.5px 5px; border-radius: 6px; font-size: 13px;
}
.bubble table { border-collapse: collapse; margin: 10px 0; display: block; overflow-x: auto; font-size: 14px; }
.bubble th, .bubble td { border: 1px solid var(--border); padding: 7px 11px; text-align: left; }
.bubble th { background: var(--surface-2); font-weight: 600; }
.bubble ul, .bubble ol { margin: 8px 0; padding-left: 24px; }
.bubble li { margin: 3px 0; }
.bubble img { max-width: 100%; border-radius: 10px; }
.bubble a { color: var(--accent); text-decoration: none; }
.bubble a:hover { text-decoration: underline; }
.bubble blockquote {
  margin: 10px 0; padding: 2px 14px; border-left: 3px solid var(--border-strong); color: var(--muted);
}

.pre-head { display: flex; justify-content: flex-end; margin-bottom: -8px; }
.copy-btn {
  position: relative; z-index: 1;
  background: var(--surface); border: 1px solid var(--border); color: var(--muted);
  border-radius: 7px; font-size: 12px; padding: 3px 9px; cursor: pointer;
  transition: color .15s, border-color .15s;
}
.copy-btn:hover { color: var(--text); border-color: var(--border-strong); }

/* ============ responsive ============ */
@media (max-width: 640px) {
  .brand { font-size: 15px; }
  .model-btn { max-width: 40vw; }
  .messages, .chips, .composer-wrap { padding-left: 14px; padding-right: 14px; }
  .suggestions { grid-template-columns: 1fr; }
  .msg.user .bubble { max-width: 88%; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .001ms !important; transition-duration: .001ms !important; }
}
