:root {
  --text-xs: clamp(0.75rem, 0.7rem + 0.25vw, 0.875rem);
  --text-sm: clamp(0.875rem, 0.8rem + 0.35vw, 1rem);
  --text-base: clamp(1rem, 0.95rem + 0.25vw, 1.125rem);
  --text-lg: clamp(1.125rem, 1rem + 0.75vw, 1.5rem);
  --text-xl: clamp(1.5rem, 1.2rem + 1.25vw, 2.25rem);

  --space-1: 0.25rem; --space-2: 0.5rem; --space-3: 0.75rem; --space-4: 1rem;
  --space-5: 1.25rem; --space-6: 1.5rem; --space-8: 2rem; --space-10: 2.5rem;
  --space-12: 3rem; --space-16: 4rem;

  --font-display: 'JetBrains Mono', ui-monospace, monospace;
  --font-body: 'Inter', system-ui, sans-serif;
  --radius: 14px;
  --maxw: 46rem;
}

:root, [data-theme="light"] {
  --bg: oklch(0.98 0.005 250);
  --surface: oklch(1 0 0);
  --surface-2: oklch(0.955 0.006 250);
  --border: oklch(0.9 0.008 250);
  --text: oklch(0.24 0.02 255);
  --text-muted: oklch(0.52 0.015 255);
  --accent: oklch(0.6 0.13 190);
  --accent-fg: oklch(0.99 0 0);
  --bubble-user: oklch(0.95 0.03 190);
  --shadow: 0 1px 2px oklch(0.2 0.02 255 / 0.06), 0 8px 24px oklch(0.2 0.02 255 / 0.06);
}

[data-theme="dark"] {
  --bg: oklch(0.17 0.012 255);
  --surface: oklch(0.21 0.014 255);
  --surface-2: oklch(0.25 0.016 255);
  --border: oklch(0.31 0.018 255);
  --text: oklch(0.95 0.005 255);
  --text-muted: oklch(0.70 0.012 255);
  --accent: oklch(0.75 0.13 185);
  --accent-fg: oklch(0.18 0.02 255);
  --bubble-user: oklch(0.30 0.045 195);
  --shadow: 0 1px 2px oklch(0 0 0 / 0.3), 0 10px 30px oklch(0 0 0 / 0.28);
}

* { box-sizing: border-box; }

html { height: 100%; }

body {
  margin: 0;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  font-size: var(--text-base);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

/* ---------- topbar ---------- */
.topbar {
  flex: none;
  display: flex; align-items: center; justify-content: space-between; gap: var(--space-4);
  padding: var(--space-3) var(--space-5);
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  z-index: 10;
}
.brand { display: flex; align-items: center; gap: var(--space-3); }
.logo { width: 30px; height: 30px; color: var(--accent); flex: none; }
.brand-text { display: flex; flex-direction: column; line-height: 1.1; }
.brand-name { font-family: var(--font-display); font-weight: 700; font-size: var(--text-sm); letter-spacing: -0.01em; }
.brand-sub { font-size: var(--text-xs); color: var(--text-muted); }
.topbar-actions { display: flex; align-items: center; gap: var(--space-2); }

/* ---------- buttons ---------- */
.btn {
  font-family: var(--font-body); font-size: var(--text-sm); font-weight: 500;
  border-radius: 10px; border: 1px solid transparent; cursor: pointer;
  transition: background 0.18s ease, border-color 0.18s ease, color 0.18s ease, transform 0.12s ease;
}
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn-ghost { background: transparent; color: var(--text-muted); padding: var(--space-2) var(--space-3); border-color: var(--border); }
.btn-ghost:hover { color: var(--text); background: var(--surface-2); }
.btn-xs { font-size: var(--text-xs); padding: var(--space-1) var(--space-2); border-color: transparent; }
.btn-icon { background: transparent; color: var(--text-muted); border-color: var(--border); padding: var(--space-2); display: grid; place-items: center; }
.btn-icon:hover { color: var(--text); background: var(--surface-2); }
.btn-send {
  background: var(--accent); color: var(--accent-fg); width: 42px; height: 42px;
  display: grid; place-items: center; flex: none; border-radius: 12px;
}
.btn-send:hover { transform: translateY(-1px); }
.btn-send:disabled { opacity: 0.45; cursor: not-allowed; transform: none; }

/* ---------- settings ---------- */
.settings {
  flex: none;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  padding: var(--space-4) var(--space-5);
}
.settings-grid {
  max-width: var(--maxw); margin: 0 auto;
  display: grid; gap: var(--space-3);
  grid-template-columns: 1fr 1fr;
}
.field { display: flex; flex-direction: column; gap: var(--space-2); min-width: 0; }
.field-wide { grid-column: 1 / -1; }
.field-label { font-size: var(--text-xs); font-weight: 600; letter-spacing: 0.02em; text-transform: uppercase; color: var(--text-muted); }
.hint { text-transform: none; letter-spacing: 0; font-weight: 400; opacity: 0.8; }
input, select, textarea {
  font-family: var(--font-body); font-size: var(--text-sm); color: var(--text);
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 10px; padding: var(--space-3); width: 100%; resize: vertical;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: transparent; }
.settings-note {
  max-width: var(--maxw); margin: var(--space-3) auto 0;
  font-size: var(--text-xs); color: var(--text-muted);
}
code { font-family: var(--font-display); font-size: 0.9em; background: var(--surface-2); padding: 0.1em 0.35em; border-radius: 5px; }

/* ---------- chat ---------- */
.chat {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: var(--space-8) var(--space-5) var(--space-4);
}
.chat > * { max-width: var(--maxw); margin-inline: auto; }

.empty { text-align: center; padding-block: var(--space-12); }
.empty-title { font-family: var(--font-display); font-size: var(--text-xl); font-weight: 700; margin: 0 0 var(--space-3); letter-spacing: -0.02em; }
.empty-sub { color: var(--text-muted); font-size: var(--text-sm); margin: 0 auto var(--space-6); max-width: 34rem; }
.chips { display: flex; flex-wrap: wrap; gap: var(--space-2); justify-content: center; }
.chip {
  font-family: var(--font-body); font-size: var(--text-xs); color: var(--text-muted);
  background: var(--surface); border: 1px solid var(--border); border-radius: 999px;
  padding: var(--space-2) var(--space-4); cursor: pointer; transition: all 0.18s ease;
}
.chip:hover { color: var(--text); border-color: var(--accent); }

.msg { display: flex; gap: var(--space-3); margin-bottom: var(--space-6); animation: rise 0.25s ease both; }
@keyframes rise { from { opacity: 0; transform: translateY(6px); } }
@media (prefers-reduced-motion: reduce) { .msg { animation: none; } }

.msg-role {
  flex: none; width: 30px; height: 30px; border-radius: 9px;
  display: grid; place-items: center;
  font-family: var(--font-display); font-size: 0.7rem; font-weight: 700;
  background: var(--surface-2); color: var(--text-muted); border: 1px solid var(--border);
}
.msg-user .msg-role { background: var(--bubble-user); color: var(--text); border-color: transparent; }
.msg-body { flex: 1 1 auto; min-width: 0; padding-top: 2px; }
.msg-body p { margin: 0 0 var(--space-3); white-space: pre-wrap; overflow-wrap: anywhere; }
.msg-body p:last-child { margin-bottom: 0; }
.msg-body pre {
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: var(--space-4); overflow-x: auto; font-family: var(--font-display);
  font-size: var(--text-xs); line-height: 1.55; margin: 0 0 var(--space-3);
}
.msg-error .msg-body { color: oklch(0.65 0.17 25); font-size: var(--text-sm); }

.cursor { display: inline-block; width: 0.5ch; height: 1em; background: var(--accent); vertical-align: -0.15em; animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0; } }

/* ---------- composer ---------- */
.composer-wrap {
  flex: none;
  border-top: 1px solid var(--border);
  background: var(--bg);
  padding: var(--space-4) var(--space-5) var(--space-5);
}
.composer {
  max-width: var(--maxw); margin: 0 auto;
  display: flex; align-items: flex-end; gap: var(--space-3);
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: var(--space-2); box-shadow: var(--shadow);
}
.composer:focus-within { border-color: var(--accent); }
.composer textarea {
  background: transparent; border: none; padding: var(--space-2) var(--space-2);
  max-height: 9rem; line-height: 1.5;
}
.composer textarea:focus { outline: none; }
.composer-meta {
  max-width: var(--maxw); margin: var(--space-2) auto 0;
  display: flex; justify-content: space-between; align-items: center;
  font-size: var(--text-xs); color: var(--text-muted);
}
.status { font-family: var(--font-display); font-size: 0.72rem; letter-spacing: 0.02em; }

@media (max-width: 640px) {
  .settings-grid { grid-template-columns: 1fr; }
  .chat { padding-inline: var(--space-4); }
  .composer-wrap { padding-inline: var(--space-4); }
}
