:root {
  color-scheme: light dark;
  --bg: #0f1419;
  --surface: #1a222d;
  --surface-2: #222d3d;
  --border: #2d3a4d;
  --text: #e7eef7;
  --fg: #e7eef7;
  --muted: #9fb0c4;
  --accent: #3d9cf0;
  --accent-dim: #2563a8;
  --user-bg: #243044;
  --assistant-bg: #151c26;
  --radius: 12px;
  --font: "Segoe UI", system-ui, sans-serif;
  --mono: "Cascadia Code", "Consolas", ui-monospace, monospace;
  --layout-max: min(100% - 1.5rem, 90rem);
  --layout-pad-x: clamp(0.75rem, 2.5vw, 2rem);
  --user-bubble-max: min(48rem, 72%);
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f6f9;
    --surface: #ffffff;
    --surface-2: #eef1f6;
    --border: #d5dde8;
    --text: #1a2332;
    --fg: #1a2332;
    --muted: #5c6b7e;
    --accent: #0b6bcb;
    --accent-dim: #084f98;
    --user-bg: #e3eefc;
    --assistant-bg: #f0f3f8;
  }
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  height: 100%;
  height: 100dvh;
}

body {
  margin: 0;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.55;
  display: flex;
  flex-direction: column;
}

.layout {
  max-width: var(--layout-max);
  width: 100%;
  margin: 0 auto;
  padding: 0.85rem var(--layout-pad-x) 0.5rem;
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
  overflow: hidden;
}

.header {
  flex-shrink: 0;
}

.header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

.header h1 {
  font-size: 1.15rem;
  font-weight: 650;
  margin: 0;
  letter-spacing: -0.02em;
}

.lede-details {
  margin-top: 0.45rem;
}

.lede-details summary {
  cursor: pointer;
  font-size: 0.82rem;
  color: var(--accent);
  user-select: none;
  list-style: none;
}

.lede-details summary::-webkit-details-marker {
  display: none;
}

.lede-details summary::before {
  content: "▸ ";
  display: inline-block;
  transition: transform 0.15s ease;
}

.lede-details[open] summary::before {
  transform: rotate(90deg);
}

.lede {
  margin: 0.45rem 0 0;
  color: var(--muted);
  font-size: 0.88rem;
}

.chat-shell {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  overflow: hidden;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.22);
}

.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
  padding: 1rem;
  scroll-behavior: smooth;
  overscroll-behavior: contain;
  background: linear-gradient(180deg, var(--assistant-bg) 0%, var(--bg) 100%);
}

.messages:has(.msg) .empty-state {
  display: none;
}

.empty-state {
  margin: auto;
  max-width: 36rem;
  text-align: center;
  padding: 2rem 1rem;
}

.empty-state__title {
  margin: 0 0 0.35rem;
  font-size: 1rem;
  font-weight: 600;
  color: var(--text);
}

.empty-state__hint {
  margin: 0;
  font-size: 0.88rem;
  color: var(--muted);
  line-height: 1.5;
}

.msg {
  border-radius: var(--radius);
  padding: 0.85rem 1rem;
  border: 1px solid var(--border);
  background: var(--surface);
}

.msg--user {
  background: var(--user-bg);
  align-self: flex-end;
  max-width: var(--user-bubble-max);
}

.msg--assistant {
  background: var(--assistant-bg);
  max-width: 100%;
}

.msg-role {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin-bottom: 0.35rem;
}

.msg-body {
  font-size: 0.95rem;
}

.msg-body :first-child {
  margin-top: 0;
}

.msg-body :last-child {
  margin-bottom: 0;
}

.msg-body pre,
.msg-body code {
  font-family: var(--mono);
  font-size: 0.84rem;
}

.code-block-wrap {
  margin: 0.65rem 0;
}

.code-block-toolbar {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 0.35rem;
  margin-bottom: 0.35rem;
}

.code-copy-btn {
  font: inherit;
  font-size: 0.75rem;
  font-weight: 600;
  cursor: pointer;
  padding: 0.3rem 0.65rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}

.code-copy-btn:hover {
  background: var(--user-bg);
  border-color: var(--accent);
}

.msg-body pre {
  overflow-x: auto;
  padding: 0.75rem 1rem;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.25);
  border: 1px solid var(--border);
  user-select: text;
  cursor: text;
  -webkit-user-select: text;
}

@media (prefers-color-scheme: light) {
  .msg-body pre {
    background: #eef2f7;
  }
}

.msg-body pre code {
  background: none;
  padding: 0;
}

.msg-body code {
  padding: 0.1em 0.35em;
  border-radius: 4px;
  background: rgba(0, 0, 0, 0.2);
}

@media (prefers-color-scheme: light) {
  .msg-body code {
    background: #e2e8f0;
  }
}

.composer {
  flex-shrink: 0;
  display: flex;
  align-items: flex-end;
  gap: 0.6rem;
  padding: 0.65rem 0.75rem;
  border-top: 1px solid var(--border);
  background: var(--surface);
}

.composer textarea {
  flex: 1;
  width: 100%;
  min-height: 2.75rem;
  max-height: 9rem;
  resize: none;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.6rem 0.75rem;
  font: inherit;
  line-height: 1.45;
  color: var(--text);
  background: var(--bg);
}

.composer textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.btn-health {
  flex-shrink: 0;
  font: inherit;
  font-size: 0.78rem;
  font-weight: 500;
  cursor: pointer;
  padding: 0.35rem 0.65rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--muted);
}

.btn-health:hover {
  background: var(--surface-2);
  color: var(--fg);
  border-color: var(--accent);
}

/* Loading dots */
.msg--loading .msg-body p {
  margin: 0;
  letter-spacing: 0.15em;
  color: var(--muted);
  animation: pulse 1.2s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 0.35; }
  50% { opacity: 1; }
}

@media (min-width: 900px) {
  :root {
    --layout-max: min(100% - 3rem, 96rem);
  }

  .messages {
    padding: 1.15rem 1.35rem;
  }

  .composer {
    padding: 0.75rem 1rem;
  }
}

@media (min-width: 1400px) {
  :root {
    --layout-max: min(100% - 4rem, 108rem);
  }
}

@media (max-width: 540px) {
  :root {
    --layout-max: 100%;
    --user-bubble-max: 92%;
  }

  .layout {
    padding: 0.5rem 0.65rem 0.35rem;
  }

  .header h1 {
    font-size: 1rem;
  }

  .messages {
    padding: 0.75rem;
  }

  .composer {
    padding: 0.55rem;
    gap: 0.45rem;
  }

  .btn-send {
    padding: 0.6rem 0.75rem;
  }
}

/* Health status toast */
.health-toast {
  position: fixed;
  bottom: 5.5rem;
  right: 1rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.75rem 1rem;
  font-size: 0.82rem;
  max-width: 280px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.3);
  z-index: 999;
  animation: fadeInUp 0.2s ease;
}
.health-toast.ok   { border-left: 3px solid #4caf50; }
.health-toast.error{ border-left: 3px solid #f44336; }
.health-toast .ht-row { display: flex; justify-content: space-between; gap: 1rem; margin-bottom: 0.2rem; }
.health-toast .ht-label { color: var(--muted); }
.health-toast .ht-val   { font-family: monospace; font-weight: 600; }
.health-toast .ht-val.ok    { color: #4caf50; }
.health-toast .ht-val.error { color: #f44336; }

@keyframes fadeInUp {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

.btn-send {
  flex-shrink: 0;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  border: none;
  border-radius: 10px;
  padding: 0.6rem 1rem;
  min-height: 2.75rem;
  background: var(--accent);
  color: #fff;
}

.btn-send:hover {
  background: var(--accent-dim);
}

.btn-send:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.footer {
  flex-shrink: 0;
  padding: 0.15rem 0 0.25rem;
}

.footer small {
  color: var(--muted);
  font-size: 0.72rem;
  line-height: 1.35;
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.footer code {
  font-family: var(--mono);
  font-size: 0.78em;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

.msg--error {
  border-color: #c94a4a;
  background: rgba(201, 74, 74, 0.12);
}

.msg--error .msg-body {
  white-space: pre-wrap;
}

/* Streaming bubble — plain text while tokens arrive */
.msg-body--streaming {
  white-space: pre-wrap;
  font-family: var(--font);
  line-height: 1.6;
  min-height: 1.2em;
}

/* Blinking cursor shown while streaming */
.msg-body--streaming::after {
  content: "▋";
  display: inline-block;
  animation: blink 0.8s step-end infinite;
  margin-left: 1px;
  opacity: 0.7;
}

@keyframes blink {
  0%, 100% { opacity: 0.7; }
  50%       { opacity: 0; }
}
