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

:root {
  --bg:           #0f1117;
  --surface:      #1a1d27;
  --surface-2:    #22263a;
  --border:       #2e3250;
  --text:         #e8eaf0;
  --text-muted:   #7c829e;
  --accent:       #4f7dff;
  --accent-hover: #6b94ff;
  --user-bubble:  #1e3a5f;
  --bot-bubble:   #1a1d27;
  --error:        #e05c5c;
  --radius:       12px;
  --font:         -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.6;
}

/* ── Layout ──────────────────────────────────────────────── */

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  max-width: 780px;
  margin: 0 auto;
}

/* ── Header ──────────────────────────────────────────────── */

#header {
  cursor: pointer;
  padding: 18px 24px 14px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: baseline;
  gap: 12px;
  flex-shrink: 0;
}

#header h1 {
  font-size: 17px;
  font-weight: 600;
  color: var(--text);
}

#header .subtitle {
  font-size: 13px;
  color: var(--text-muted);
}

#messages-remaining {
  margin-left: auto;
  font-size: 12px;
  color: var(--text-muted);
}

/* ── Messages ────────────────────────────────────────────── */

#messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  scroll-behavior: smooth;
}

.message {
  display: flex;
  flex-direction: column;
  max-width: 82%;
}

.message.user {
  align-self: flex-end;
  align-items: flex-end;
}

.message.bot {
  align-self: flex-start;
  align-items: flex-start;
}

.bubble {
  padding: 12px 16px;
  border-radius: var(--radius);
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.65;
}

.message.user .bubble {
  background: var(--user-bubble);
  border-bottom-right-radius: 4px;
  color: #d0deff;
}

.message.bot .bubble {
  background: var(--bot-bubble);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}

.bubble.streaming::after {
  content: "▋";
  display: inline-block;
  animation: blink 0.8s step-start infinite;
  margin-left: 2px;
  color: var(--accent);
}

@keyframes blink {
  50% { opacity: 0; }
}

/* ── Input area ──────────────────────────────────────────── */

#input-area {
  padding: 16px 24px 20px;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 10px;
  align-items: flex-end;
  flex-shrink: 0;
}

#message-input {
  flex: 1;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.5;
  padding: 10px 14px;
  resize: none;
  min-height: 44px;
  max-height: 160px;
  outline: none;
  transition: border-color 0.15s;
}

#message-input:focus {
  border-color: var(--accent);
}

#message-input::placeholder {
  color: var(--text-muted);
}

#message-input:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

#send-btn {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  padding: 10px 18px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, opacity 0.15s;
  height: 44px;
  flex-shrink: 0;
}

#send-btn:hover:not(:disabled) {
  background: var(--accent-hover);
}

#send-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* ── Loading / gate screens ──────────────────────────────── */

#gate {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
  gap: 14px;
  color: var(--text-muted);
  font-size: 14px;
  text-align: center;
  padding: 24px;
}

#gate .spinner {
  width: 28px;
  height: 28px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

#gate .gate-title {
  font-size: 17px;
  font-weight: 600;
  color: var(--text);
}

#gate .gate-msg {
  max-width: 340px;
  color: var(--text-muted);
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ── Error banner ────────────────────────────────────────── */

#error-banner {
  display: none;
  background: rgba(224, 92, 92, 0.12);
  border: 1px solid var(--error);
  color: var(--error);
  font-size: 13px;
  padding: 10px 16px;
  margin: 0 24px 8px;
  border-radius: 8px;
}

/* ── Welcome message ─────────────────────────────────────── */

.welcome {
  color: var(--text-muted);
  font-size: 14px;
  text-align: center;
  padding: 32px 0;
  line-height: 1.7;
}

.welcome-notice {
  display: block;
  margin-top: 8px;
  font-size: 12px;
  color: #4a4f6a;
}

/* ── Intro panel (sprints 05 and 06) ─────────────────────── */

#intro[hidden] { display: none; }

#intro:not([hidden]) {
  display: block;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  line-height: 1.6;
}

#intro p { color: var(--text); margin-bottom: 10px; }
#intro .intro-greeting { font-size: 16px; }
#intro .intro-greeting:focus { outline: none; }
#intro .intro-why { color: var(--text-muted); }

#intro .intro-examples {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 14px;
}

#intro .intro-example {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 10px 14px;
  min-height: 44px;
  font: inherit;
  font-size: 14px;
  text-align: left;
  cursor: pointer;
}

#intro .intro-example:hover { border-color: var(--accent); }

#intro-start {
  display: block;
  margin-top: 16px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  padding: 10px 18px;
  min-height: 44px;
  font: inherit;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
}

#intro-start:hover { background: var(--accent-hover); }

#intro .intro-review {
  margin: 12px 0 0;
  font-size: 12px;
  color: var(--text-muted);
}

#intro button:focus-visible,
#intro .intro-greeting:focus-visible {
  outline: 2px solid var(--accent-hover);
  outline-offset: 2px;
}

.welcome.returning { padding: 8px 0; }

@media (max-width: 480px) {
  #messages { padding: 16px; }
  #intro:not([hidden]) { padding: 16px; }
  #intro .intro-example { width: 100%; }
}

/* ── Markdown inside bot bubbles ────────────────────────── */

.message.bot .bubble strong { font-weight: 600; }
.message.bot .bubble em     { font-style: italic; }

.message.bot .bubble code {
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
  font-size: 0.88em;
  background: rgba(255,255,255,0.08);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 5px;
}

.message.bot .bubble ul,
.message.bot .bubble ol {
  margin: 6px 0 6px 20px;
  padding: 0;
}

.message.bot .bubble li   { margin-bottom: 3px; line-height: 1.6; }
.message.bot .bubble p    { margin: 0 0 8px; }
.message.bot .bubble p:last-child { margin-bottom: 0; }

/* ── Scrollbar ───────────────────────────────────────────── */

#messages::-webkit-scrollbar {
  width: 6px;
}
#messages::-webkit-scrollbar-track {
  background: transparent;
}
#messages::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}

/* ── Responsive ──────────────────────────────────────────── */

@media (max-width: 600px) {
  #header {
    padding: 14px 16px 12px;
    flex-wrap: wrap;
  }
  #messages {
    padding: 16px;
    gap: 14px;
  }
  #input-area {
    padding: 12px 16px 16px;
  }
  .message {
    max-width: 94%;
  }
}

/* ── Follow-up suggestions (sprint 07) ───────────────────── */

.followups {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: -8px 0 4px;
}

.followup {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 8px 14px;
  min-height: 40px;
  font: inherit;
  font-size: 13px;
  text-align: left;
  cursor: pointer;
}

.followup:hover { border-color: var(--accent); }
.followup:focus-visible { outline: 2px solid var(--accent-hover); outline-offset: 2px; }

@media (max-width: 480px) {
  .followup { width: 100%; }
}
