:root {
  --bg: #f4f6f8;
  --bg-grad-1: #eef2f6;
  --bg-grad-2: #f6f8fa;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --ink: #10151c;
  --ink-soft: #3a4453;
  --muted: #6b7684;
  --line: #e4e8ec;
  --line-soft: #eef1f4;
  --accent: #0f766e;
  --accent-hover: #0c5f59;
  --accent-soft: #e6f4f2;
  --accent-ink: #ffffff;
  --user-bubble: #0f766e;
  --user-ink: #ffffff;
  --warn-bg: #fef4e2;
  --warn-ink: #92610e;
  --error-bg: #fdecec;
  --error-ink: #b42318;
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .06);
  --shadow-md: 0 4px 16px rgba(16, 24, 40, .08), 0 1px 4px rgba(16, 24, 40, .05);
  --radius: 14px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1117;
    --bg-grad-1: #0d1117;
    --bg-grad-2: #10161e;
    --surface: #161b22;
    --surface-2: #1b222c;
    --ink: #e8edf3;
    --ink-soft: #b6c0cc;
    --muted: #8b95a1;
    --line: #262c36;
    --line-soft: #21272f;
    --accent: #2dd4bf;
    --accent-hover: #5eead4;
    --accent-soft: #133b38;
    --accent-ink: #06231f;
    --user-bubble: #14b8a6;
    --user-ink: #05201d;
    --warn-bg: #3a2f16;
    --warn-ink: #f0c674;
    --error-bg: #3a1e1c;
    --error-ink: #f8b4ad;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, .4);
    --shadow-md: 0 8px 28px rgba(0, 0, 0, .45);
  }
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font: 15.5px/1.6 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--ink);
  background: linear-gradient(180deg, var(--bg-grad-1), var(--bg-grad-2));
  -webkit-font-smoothing: antialiased;
  display: flex;
  flex-direction: column;
  min-height: 100%;
}

.muted { color: var(--muted); }
.error { color: var(--error-ink); font-weight: 600; }

/* --- brand --- */
.brand {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  font-weight: 700;
  letter-spacing: -0.01em;
  color: var(--ink);
}
.brand-mark {
  width: 26px;
  height: 26px;
  border-radius: 8px;
  background: linear-gradient(135deg, var(--accent), #14b8a6);
  color: #fff;
  display: grid;
  place-items: center;
  font-size: 14px;
  font-weight: 800;
  flex: none;
  box-shadow: var(--shadow-sm);
}

/* --- login --- */
.centered { align-items: center; justify-content: center; padding: 24px; }
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 32px 30px;
  width: min(384px, 100%);
  box-shadow: var(--shadow-md);
}
.card .brand { font-size: 19px; margin-bottom: 6px; }
.card .brand-mark { width: 30px; height: 30px; border-radius: 9px; font-size: 16px; }
.card .sub { margin: 0 0 22px; color: var(--muted); font-size: 14px; }
.card form { display: flex; flex-direction: column; gap: 15px; }
.card label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 600; color: var(--ink-soft); }
.card input {
  padding: 11px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  font-size: 16px;
  background: var(--surface-2);
  color: var(--ink);
  transition: border-color .15s, box-shadow .15s;
}
.card input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.card .error {
  background: var(--error-bg);
  color: var(--error-ink);
  border-radius: 10px;
  padding: 9px 12px;
  font-size: 13.5px;
  font-weight: 500;
  margin: 0 0 4px;
}

/* --- buttons --- */
button {
  font: inherit;
  background: var(--accent);
  color: var(--accent-ink);
  border: 0;
  border-radius: 10px;
  padding: 11px 16px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s, transform .05s, opacity .15s;
}
button:hover { background: var(--accent-hover); }
button:active { transform: translateY(1px); }
button:disabled { opacity: .45; cursor: default; }
button.link {
  background: none;
  color: var(--muted);
  padding: 6px 8px;
  font-size: 13px;
  font-weight: 600;
  border-radius: 8px;
}
button.link:hover { background: var(--line-soft); color: var(--ink); }

/* --- chat header --- */
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px 16px;
  flex-wrap: wrap;
  padding: 11px 18px;
  background: var(--surface);
  background: color-mix(in srgb, var(--surface) 90%, transparent);
  -webkit-backdrop-filter: saturate(1.4) blur(8px);
  backdrop-filter: saturate(1.4) blur(8px);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 10;
}
.topbar-left { display: flex; align-items: baseline; gap: 8px; min-width: 0; }
.topbar-left .brand { font-size: 15.5px; }
.topbar-left .who { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-right { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; justify-content: flex-end; }
.topbar-right form { margin: 0; }

.client-picker {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--muted);
}
.client-picker select {
  font: inherit;
  font-size: 13.5px;
  font-weight: 600;
  text-transform: none;
  letter-spacing: 0;
  color: var(--accent);
  background: var(--accent-soft);
  border: 1px solid transparent;
  border-radius: 999px;
  padding: 6px 30px 6px 12px;
  max-width: 52vw;
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%230f766e' stroke-width='3' stroke-linecap='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 11px center;
  transition: box-shadow .15s;
}
.client-picker select:focus { outline: none; box-shadow: 0 0 0 3px var(--accent-soft); }

.chip {
  background: var(--accent-soft);
  color: var(--accent);
  border-radius: 999px;
  padding: 5px 12px;
  font-size: 13px;
  font-weight: 600;
}
.chip.warn { background: var(--warn-bg); color: var(--warn-ink); }

@media (max-width: 520px) {
  .topbar-right { flex: 1 1 100%; justify-content: space-between; }
  .client-picker select { max-width: none; flex: 1; }
}

/* --- messages --- */
.messages {
  flex: 1;
  width: min(780px, 100%);
  margin: 0 auto;
  padding: 26px 16px 32px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.msg {
  padding: 13px 16px;
  border-radius: var(--radius);
  word-wrap: break-word;
  overflow-wrap: anywhere;
  max-width: 88%;
  animation: rise .22s ease both;
  font-size: 15px;
}
@keyframes rise { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

.msg.user {
  background: var(--user-bubble);
  color: var(--user-ink);
  align-self: flex-end;
  border-bottom-right-radius: 5px;
  white-space: pre-wrap;
}
.msg.assistant {
  background: var(--surface);
  border: 1px solid var(--line);
  align-self: flex-start;
  border-bottom-left-radius: 5px;
  box-shadow: var(--shadow-sm);
  max-width: 94%;
}
.msg.assistant.intro { background: var(--surface-2); color: var(--ink-soft); box-shadow: none; }
.msg.err {
  background: var(--error-bg);
  color: var(--error-ink);
  align-self: flex-start;
  border-bottom-left-radius: 5px;
  white-space: pre-wrap;
}

/* markdown inside assistant messages */
.msg.assistant > :first-child { margin-top: 0; }
.msg.assistant > :last-child { margin-bottom: 0; }
.msg.assistant p { margin: 0 0 10px; }
.msg.assistant h1, .msg.assistant h2, .msg.assistant h3, .msg.assistant h4 {
  margin: 16px 0 8px;
  line-height: 1.3;
  font-weight: 700;
  letter-spacing: -0.01em;
}
.msg.assistant h1 { font-size: 1.2em; }
.msg.assistant h2 { font-size: 1.12em; }
.msg.assistant h3 { font-size: 1.04em; }
.msg.assistant h4 { font-size: 1em; color: var(--ink-soft); }
.msg.assistant ul, .msg.assistant ol { margin: 0 0 10px; padding-left: 1.35em; }
.msg.assistant li { margin: 3px 0; }
.msg.assistant strong { font-weight: 700; }
.msg.assistant code {
  background: var(--surface-2);
  border: 1px solid var(--line-soft);
  border-radius: 5px;
  padding: 1px 5px;
  font: 0.88em ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.msg.assistant pre {
  background: var(--surface-2);
  border: 1px solid var(--line-soft);
  border-radius: 10px;
  padding: 12px 14px;
  overflow-x: auto;
  margin: 0 0 10px;
}
.msg.assistant pre code { background: none; border: 0; padding: 0; font-size: 0.86em; }
.msg.assistant blockquote {
  margin: 0 0 10px;
  padding: 2px 0 2px 12px;
  border-left: 3px solid var(--line);
  color: var(--muted);
}
.msg.assistant hr { border: 0; border-top: 1px solid var(--line); margin: 14px 0; }
.msg.assistant .table-wrap { overflow-x: auto; margin: 0 0 10px; }
.msg.assistant table { border-collapse: collapse; font-size: 0.92em; width: 100%; }
.msg.assistant th, .msg.assistant td {
  border: 1px solid var(--line);
  padding: 6px 10px;
  text-align: left;
  vertical-align: top;
}
.msg.assistant th { background: var(--surface-2); font-weight: 600; white-space: nowrap; }
.msg.assistant td { min-width: 4.5em; }

/* thinking dots */
.dots { display: inline-flex; gap: 4px; align-items: center; padding: 3px 0; }
.dots span {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--muted);
  animation: blink 1.4s infinite both;
}
.dots span:nth-child(2) { animation-delay: .2s; }
.dots span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

/* --- composer --- */
.composer {
  position: sticky;
  bottom: 0;
  display: flex;
  align-items: flex-end;
  gap: 9px;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: linear-gradient(180deg, transparent, var(--bg-grad-2) 22%);
  width: min(780px, 100%);
  margin: 0 auto;
}
.composer-inner {
  flex: 1;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 6px 6px 6px 14px;
  box-shadow: var(--shadow-md);
  transition: border-color .15s, box-shadow .15s;
}
.composer-inner:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft), var(--shadow-md);
}
.composer textarea {
  flex: 1;
  resize: none;
  border: 0;
  background: none;
  color: var(--ink);
  padding: 8px 0;
  font: inherit;
  line-height: 1.5;
  max-height: 168px;
}
.composer textarea:focus { outline: none; }
.composer textarea::placeholder { color: var(--muted); }
.send-btn {
  flex: none;
  width: 38px;
  height: 38px;
  padding: 0;
  border-radius: 11px;
  display: grid;
  place-items: center;
}
.send-btn svg { width: 18px; height: 18px; }
