:root {
  --bg: #f3efe7;
  --panel: #fffdf9;
  --ink: #23201c;
  --ink-soft: #6d6459;
  --line: #e2d9cb;
  --brand: #1f6f5c;
  --brand-ink: #ffffff;
  --them: #ffffff;
  --me: #dcf3ea;
  --warn-bg: #fdf1d8;
  --warn-ink: #7a5715;
  --error-bg: #fbe5e0;
  --error-ink: #8c3a26;
  --radius: 18px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16140f;
    --panel: #1e1b16;
    --ink: #f2ece1;
    --ink-soft: #a79d8e;
    --line: #322d25;
    --them: #262119;
    --me: #1c3a33;
    --warn-bg: #3a2f16;
    --warn-ink: #f0d59a;
    --error-bg: #3d211a;
    --error-ink: #f5b7a5;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-size: 17px;
  line-height: 1.45;
  -webkit-text-size-adjust: 100%;
}

/* ── Login ─────────────────────────────────────────────────────────────── */
.login {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.login-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 32px 28px;
  width: min(380px, 100%);
  display: grid;
  gap: 10px;
  box-shadow: 0 12px 40px rgb(0 0 0 / 0.08);
}

.login-card h1 { margin: 0; font-size: 30px; letter-spacing: -0.02em; }
.login-sub { margin: 0 0 12px; color: var(--ink-soft); }
.login-card label { font-size: 14px; color: var(--ink-soft); }

.login-card input {
  font: inherit;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--ink);
}

.login-card button {
  font: inherit;
  font-weight: 600;
  margin-top: 6px;
  padding: 12px;
  border: 0;
  border-radius: 12px;
  background: var(--brand);
  color: var(--brand-ink);
  cursor: pointer;
}

.login-error { margin: 4px 0 0; color: var(--error-ink); font-size: 14px; }

/* ── App shell ─────────────────────────────────────────────────────────── */
.app {
  /* A fixed height (not min-height) is what makes .thread the scroll
     container, so new messages can be scrolled into view. */
  height: 100dvh;
  overflow: hidden;
  display: grid;
  grid-template-rows: auto auto 1fr auto;
  max-width: 720px;
  margin: 0 auto;
  background: var(--panel);
  border-inline: 1px solid var(--line);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  padding-top: max(12px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}

.who { display: flex; align-items: center; gap: 12px; }

.avatar {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: var(--brand);
  color: var(--brand-ink);
  display: grid;
  place-items: center;
  font-weight: 600;
  font-size: 15px;
}

.name { font-weight: 600; }
.sub { font-size: 13px; color: var(--ink-soft); }
.sub.live { color: var(--brand); }

.topbar-actions { display: flex; gap: 8px; align-items: center; }

.ghost {
  font: inherit;
  font-size: 14px;
  background: none;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 7px 12px;
  color: var(--ink-soft);
  cursor: pointer;
}

.banner {
  padding: 11px 16px;
  font-size: 14px;
  background: var(--warn-bg);
  color: var(--warn-ink);
  border-bottom: 1px solid var(--line);
}

/* ── Thread ────────────────────────────────────────────────────────────── */
.notice {
  padding: 11px 16px;
  font-size: 14px;
  background: color-mix(in srgb, var(--brand) 12%, transparent);
  color: var(--ink);
  border-bottom: 1px solid var(--line);
  display: flex;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
}

.notice button {
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  flex: none;
  padding: 7px 12px;
  border: 0;
  border-radius: 9px;
  background: var(--brand);
  color: var(--brand-ink);
  cursor: pointer;
}

.notice.subtle { background: none; color: var(--ink-soft); }

.thread {
  padding: 18px 16px 24px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  overflow-y: auto;
  min-height: 0; /* grid children default to min-content; without this it never scrolls */
  overscroll-behavior: contain;
}

.empty {
  margin: auto;
  max-width: 30ch;
  text-align: center;
  color: var(--ink-soft);
  font-size: 15px;
}

.msg { max-width: 78%; display: flex; flex-direction: column; gap: 5px; }
.msg.them { align-self: flex-start; }
.msg.me { align-self: flex-end; align-items: flex-end; }

.bubble {
  padding: 11px 15px;
  border-radius: var(--radius);
  background: var(--them);
  border: 1px solid var(--line);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.msg.them .bubble { border-bottom-left-radius: 6px; }

.msg.me .bubble {
  background: var(--me);
  border-color: transparent;
  border-bottom-right-radius: 6px;
}

.bubble.pending { opacity: 0.55; font-style: italic; }

.voice {
  width: 100%;
  max-width: 320px;
  height: 38px;
  margin-top: 2px;
}

.original {
  font-size: 14.5px;
  color: var(--ink-soft);
  padding: 0 6px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.original::before { content: "“"; }
.original::after { content: "”"; }

.meta {
  font-size: 12px;
  color: var(--ink-soft);
  padding: 0 6px;
  display: flex;
  gap: 8px;
  align-items: center;
}

.flag {
  font-size: 12.5px;
  padding: 7px 11px;
  border-radius: 10px;
  background: var(--error-bg);
  color: var(--error-ink);
}

/* ── Composer ──────────────────────────────────────────────────────────── */
.composer {
  display: flex;
  gap: 10px;
  align-items: flex-end;
  padding: 12px 14px;
  padding-bottom: max(12px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: var(--panel);
}

.composer textarea {
  flex: 1;
  font: inherit;
  resize: none;
  max-height: 160px;
  padding: 11px 15px;
  border-radius: 20px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--ink);
}

.composer button {
  flex: none;
  width: 46px;
  height: 46px;
  border: 0;
  border-radius: 50%;
  background: var(--brand);
  color: var(--brand-ink);
  display: grid;
  place-items: center;
  cursor: pointer;
}

/* Dictation button. Sized and coloured to be hit without looking. */
.mic {
  background: var(--bg);
  color: var(--ink-soft);
  border: 1px solid var(--line) !important;
}

.mic .mic-stop { display: none; }

.mic.recording {
  background: #c0392b;
  color: #fff;
  border-color: #c0392b !important;
  animation: pulse 1.4s ease-in-out infinite;
}

.mic.recording .mic-idle { display: none; }
.mic.recording .mic-stop { display: block; }

.mic.working {
  background: var(--bg);
  color: var(--ink-soft);
  animation: spin 1s linear infinite;
}

@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgb(192 57 43 / 0.5); }
  50%      { box-shadow: 0 0 0 10px rgb(192 57 43 / 0); }
}

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

@media (prefers-reduced-motion: reduce) {
  .mic.recording, .mic.working { animation: none; }
}

.composer button:disabled { opacity: 0.45; cursor: default; }

[hidden] { display: none !important; }
