:root {
  /* surface */
  --bg: #faf9f6;
  --surface: #f0efe9;
  --ink: #1a1a18;
  --muted: #6e6e66;
  --faint: #8a8a80;
  --hairline: #e3e2db;
  --accent: #2f5e8d;
  --accent-soft: rgba(47, 94, 141, 0.12);
  --error: #b23b2e;

  /* type */
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans TC", sans-serif;
  --mono: ui-monospace, "Cascadia Mono", "Segoe UI Mono", Consolas, monospace;

  /* spacing — 4px base */
  --s1: 4px;
  --s2: 8px;
  --s3: 12px;
  --s4: 16px;
  --s5: 24px;
  --s6: 32px;

  --measure: 720px;
  --radius: 5px;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  overflow: hidden;
  font-family: var(--sans);
  background: var(--bg);
  color: var(--ink);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

/* ---- shell ---- */
#app {
  height: 100dvh;
  max-width: var(--measure);
  margin: 0 auto;
  padding: 0 var(--s4);
  display: flex;
  flex-direction: column;
}

/* ---- header ---- */
#app-header {
  flex: 0 0 auto;
  padding: var(--s5) 0 var(--s4);
  border-bottom: 1px solid var(--hairline);
}
#app-header h1 {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 600;
  letter-spacing: -0.01em;
}

/* ---- conversation ---- */
#conversation {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: var(--s4);
  padding: var(--s5) 0;
}
/* children must not shrink, or they compress to fit instead of overflowing — which kills the scroll */
#conversation > * {
  flex-shrink: 0;
}

.bubble {
  max-width: 86%;
  padding: var(--s3) var(--s4);
  border-radius: var(--radius);
  font-size: 0.95rem;
  white-space: pre-wrap;
  word-wrap: break-word;
  animation: rise 160ms ease-out;
}
.bubble.bot {
  align-self: flex-start;
  background: var(--surface);
  color: var(--ink);
}
.bubble.user {
  align-self: flex-end;
  background: var(--ink);
  color: var(--bg);
}
.bubble.thinking {
  color: var(--muted);
  font-family: var(--mono);
  font-size: 0.85rem;
}
.bubble.thinking::after {
  content: "";
  display: inline-block;
  animation: dots 1.4s infinite;
}
.bubble.system {
  align-self: flex-start;
  background: transparent;
  border: 1px solid var(--hairline);
  color: var(--muted);
  font-family: var(--mono);
  font-size: 0.85rem;
}

/* ---- starters (empty-state panel) ---- */
.starters {
  align-self: stretch;
  border: 1px solid var(--hairline);
  border-radius: var(--radius);
  overflow: hidden;
  animation: rise 160ms ease-out;
}
.starters-label {
  margin: 0;
  padding: var(--s3) var(--s4);
  font-family: var(--mono);
  font-size: 0.72rem;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  color: var(--faint);
  border-bottom: 1px solid var(--hairline);
}
.starter {
  display: block;
  width: 100%;
  font: inherit;
  font-size: 0.92rem;
  text-align: left;
  background: transparent;
  border: none;
  border-bottom: 1px solid var(--hairline);
  padding: var(--s3) var(--s4);
  color: var(--ink);
  cursor: pointer;
  transition: background-color 120ms ease-out, color 120ms ease-out;
}
.starter:last-child { border-bottom: none; }
.starter:hover {
  background: var(--surface);
  color: var(--accent);
}
.starter:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
  color: var(--accent);
}

/* ---- composer ---- */
#chat-form {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  gap: var(--s3);
  padding: var(--s4) 0 calc(var(--s5) + env(safe-area-inset-bottom));
  border-top: 1px solid var(--hairline);
}
.composer {
  display: flex;
  flex-direction: column;
  gap: var(--s1);
}
#user-input {
  font: inherit;
  font-size: 0.95rem;
  padding: var(--s3);
  background: #fff;
  color: var(--ink);
  border: 1px solid var(--hairline);
  border-radius: var(--radius);
  resize: vertical;
  transition: border-color 120ms ease-out, box-shadow 120ms ease-out;
}
#user-input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
#user-input:disabled {
  background: var(--surface);
  color: var(--muted);
}
.char-count {
  margin: 0;
  align-self: flex-end;
  font-family: var(--mono);
  font-size: 0.72rem;
  color: var(--faint);
}
.form-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: var(--s3);
  flex-wrap: wrap;
}
.aux-link {
  display: inline-flex;
  align-items: center;
  gap: var(--s2);
  font-size: 1.1rem;
  color: var(--accent);
  text-decoration: none;
  white-space: nowrap;
}
.aux-link__icon {
  flex-shrink: 0;
  width: 1.15em;
  height: 1.15em;
}
.aux-link span {
  border-bottom: 1px solid transparent;
  transition: border-color 120ms ease-out;
}
.aux-link:hover span { border-bottom-color: var(--accent); }
.aux-link:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
  border-radius: 2px;
}
#send-btn {
  font: inherit;
  font-size: 0.9rem;
  padding: var(--s3) var(--s5);
  background: var(--ink);
  color: var(--bg);
  border: 1px solid var(--ink);
  border-radius: var(--radius);
  cursor: pointer;
  transition: background-color 120ms ease-out, border-color 120ms ease-out, opacity 120ms ease-out;
}
#send-btn:not(:disabled):hover {
  background: var(--accent);
  border-color: var(--accent);
}
#send-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
#send-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}
.status {
  margin: 0;
  min-height: 1.3em;
  font-family: var(--mono);
  font-size: 0.78rem;
  color: var(--muted);
}
.status.error { color: var(--error); }

/* ---- motion ---- */
@keyframes rise {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: none; }
}
@keyframes dots {
  0%, 20% { content: ""; }
  40% { content: "·"; }
  60% { content: "··"; }
  80%, 100% { content: "···"; }
}

/* ---- responsive ---- */
@media (max-width: 480px) {
  #app { padding: 0 var(--s3); }
  #app-header { padding: var(--s4) 0 var(--s3); }
  #app-header h1 { font-size: 1.3rem; }
  #conversation { padding: var(--s4) 0; }
  .bubble { max-width: 92%; }
  .form-row { flex-direction: column; align-items: stretch; }
  .aux-link { justify-content: center; padding: var(--s1) 0; }
  #send-btn { width: 100%; }
  #turnstile-widget { display: flex; justify-content: center; }
}

@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
}
