/* Minimal, dependency-free. No build step (SPEC.md §4). The structure is
   digital-lama's; the palette is a calm business blue. */

:root {
  --bg: #f5f7fa;
  --surface: #ffffff;
  --fg: #18202b;
  --muted: #5b6574;
  --line: #d7dde5;
  --accent: #1d5fa6;
  /* A button's outline and fill (#68). `--line` is for rules between things and is
     meant to be faint; a control's edge must be visible against the page and
     against a card (at least 3:1, `tests/unit/test_look_and_feel.py`). The tint is
     the Play button's: accent text on it keeps 4.5:1. */
  --control-line: #7d8a9c;
  --control-bg: #e9eef5;
  --accent-tint: #e3eefb;
  /* Text on an accent background. White fails contrast on the light accent
     dark mode uses, so it is a token, not a literal. */
  --on-accent: #ffffff;
  --error: #a12a2a;
  --error-bg: #fbeeee;
  color-scheme: light;
}

/* The OS preference applies unless the student picked a theme with the
   toggle; `theme-init.js` sets `data-theme` before the first paint. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #11151b;
    --surface: #171c24;
    --fg: #e5e9ef;
    --muted: #9aa5b4;
    --line: #2b333e;
    --accent: #7cb2f0;
    --control-line: #6d7a8c;
    --control-bg: #222a36;
    --accent-tint: #1b2a3d;
    --on-accent: #0b1220;
    --error: #f2a5a5;
    --error-bg: #2d1b1d;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #11151b;
  --surface: #171c24;
  --fg: #e5e9ef;
  --muted: #9aa5b4;
  --line: #2b333e;
  --accent: #7cb2f0;
  --control-line: #6d7a8c;
  --control-bg: #222a36;
  --accent-tint: #1b2a3d;
  --on-accent: #0b1220;
  --error: #f2a5a5;
  --error-bg: #2d1b1d;
  color-scheme: dark;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  /* The top clears the theme toggle. */
  padding: 56px 16px 24px;
  background: var(--bg);
  color: var(--fg);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
}

[hidden] { display: none !important; }

.card {
  width: 100%;
  max-width: 24rem;
  padding: 32px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
}

.masthead {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 20px;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 4px;
}

.brand {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--accent);
}

h2 { margin: 0 0 16px; font-size: 1.5rem; }

.intro { margin: 0 0 20px; color: var(--muted); }

.aside { margin: 24px 0 0; color: var(--muted); font-size: 0.9375rem; }
.aside a { color: var(--accent); }

.notice {
  margin: 0 0 20px;
  padding: 12px 14px;
  border-left: 3px solid var(--accent);
  background: var(--surface);
}

label {
  display: block;
  margin: 16px 0 6px;
  font-size: 0.875rem;
  font-weight: 500;
}

input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--bg);
  color: var(--fg);
  font: inherit;
}

input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

button {
  width: 100%;
  margin-top: 24px;
  padding: 11px 16px;
  border: 0;
  border-radius: 6px;
  background: var(--accent);
  color: var(--on-accent);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

button:hover { filter: brightness(1.08); }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.error {
  margin: 0 0 16px;
  padding: 10px 12px;
  border-radius: 6px;
  background: var(--error-bg);
  color: var(--error);
  font-size: 0.9375rem;
}

/* ---- theme toggle ---- */

.theme-toggle {
  position: absolute;
  top: 12px;
  right: 16px;
  width: auto;
  margin: 0;
  padding: 6px 12px;
  border: 1px solid var(--control-line);
  background: var(--control-bg);
  color: var(--fg);
  font-size: 0.875rem;
  font-weight: 500;
}

.theme-toggle:hover { filter: none; border-color: var(--accent); }

/* In the signed-in pages' masthead it is one of its items, not in the corner. */
.masthead .theme-toggle { position: static; }

/* ---- signed-in pages ---- */

.page {
  width: 100%;
  max-width: 44rem;
  margin: 0 auto;
  padding: 0 0 24px;
}

body:has(.page) { align-items: flex-start; }

/* The masthead (the name, Practice, Past sessions, Admin, Sign out, and the
   theme toggle) stays at the top of the window while the page scrolls, so a long
   lesson never needs a scroll back to reach them (#68). The body's top padding,
   which cleared the corner toggle, moves into the bar so it sits flush. */
body:has(.page) { padding-top: 0; }
/* Something scrolled into view must not end up under the bar. */
html:has(.page) { scroll-padding-top: 5rem; }

.page .masthead {
  position: sticky;
  top: 0;
  z-index: 20;
  align-items: center;
  padding: 12px 0;
  margin-bottom: 20px;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
}

/* On a phone the name and the toggle share the first row and the links take the
   second, so the bar is two lines tall and not three. */
@media (max-width: 36rem) {
  .page .masthead { gap: 6px 12px; padding: 8px 0; margin-bottom: 14px; }
  .page .masthead .theme-toggle { order: 1; margin-left: auto; padding: 4px 10px; font-size: 0.8125rem; }
  .page .masthead .site-nav { order: 2; flex-basis: 100%; }
}

.site-nav { display: flex; flex-wrap: wrap; gap: 4px 18px; align-items: baseline; }
.site-nav a { color: var(--muted); font-size: 0.9375rem; text-decoration: none; }
.site-nav a:hover { color: var(--accent); }
.site-nav a[aria-current="page"] { color: var(--fg); font-weight: 600; }
.site-nav form { margin: 0; }

.link-button {
  width: auto;
  margin: 0;
  padding: 0;
  background: none;
  border: 0;
  color: var(--muted);
  font-size: 0.9375rem;
  font-weight: 400;
  cursor: pointer;
}

.link-button:hover { color: var(--accent); filter: none; }

.page-head {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 20px;
}

.page-head h2 { margin: 0; }
.page-head form { margin: 0; }

/* A secondary action: bordered, not filled, so Send stays the one obvious
   button on the page. */
.quiet {
  width: auto;
  margin: 0;
  padding: 6px 14px;
  border: 1px solid var(--control-line);
  background: var(--control-bg);
  color: var(--fg);
  font-size: 0.9375rem;
  font-weight: 500;
}

.quiet:hover { border-color: var(--accent); color: var(--accent); filter: none; }
.quiet:disabled { opacity: 0.5; cursor: not-allowed; }

.back { color: var(--muted); font-size: 0.9375rem; }
.back:hover { color: var(--accent); }

.page-actions { display: flex; flex-wrap: wrap; gap: 10px 18px; align-items: center; }

/* The download menu: a <details>, so it works without JavaScript. The panel
   opens from the menu's left edge: the menu is always the first of the page
   actions, which together are wider than the panel, so it stays on screen
   whether or not the head wraps at phone width. */
.download { position: relative; color: var(--muted); font-size: 0.9375rem; }
.download summary { cursor: pointer; list-style: none; border-radius: 4px; }
.download summary::-webkit-details-marker { display: none; }
.download summary::after { content: "\25BE"; margin-left: 0.3em; font-size: 0.8em; }
.download summary:hover,
.download[open] summary { color: var(--accent); }
.download summary:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.download ul {
  position: absolute;
  left: 0;
  z-index: 10;
  min-width: 11rem;
  margin: 6px 0 0;
  padding: 4px 0;
  list-style: none;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: 0 4px 16px color-mix(in srgb, var(--fg) 14%, transparent);
}

.download a {
  display: block;
  padding: 8px 14px;
  color: var(--fg);
  text-decoration: none;
  white-space: nowrap;
}

.download a:hover,
.download a:focus-visible {
  outline: none;
  background: color-mix(in srgb, var(--accent) 12%, transparent);
  color: var(--accent);
}

.tag {
  margin-left: 6px;
  padding: 1px 6px;
  border: 1px solid var(--accent);
  border-radius: 4px;
  color: var(--accent);
  font-size: 0.6875rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  vertical-align: 2px;
}

/* ---- past sessions ---- */

.sessions { margin: 0; padding: 0; list-style: none; }
.sessions li { border-bottom: 1px solid var(--line); }

.sessions a {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 12px;
  align-items: baseline;
  justify-content: space-between;
  padding: 14px 4px;
  color: inherit;
  text-decoration: none;
}

.sessions a:hover .sessions-title { color: var(--accent); }
.sessions time { color: var(--muted); font-size: 0.8125rem; font-variant-numeric: tabular-nums; }

/* ---- turns ---- */

.turns { margin: 0 0 24px; padding: 0; list-style: none; }

.turn { margin-bottom: 20px; }

.who {
  display: block;
  margin-bottom: 4px;
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.said { white-space: pre-wrap; overflow-wrap: anywhere; }

.turn--user .said {
  padding: 12px 14px;
  border-radius: 8px;
  background: color-mix(in srgb, var(--accent) 10%, transparent);
}

.turn--assistant .said {
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
}

/* Where one session ends and the next begins, on a page left open past
   04:00 or after "New session" in another tab. */
.divider {
  display: flex;
  gap: 12px;
  align-items: center;
  margin: 28px 0;
  color: var(--muted);
  font-size: 0.8125rem;
  font-weight: 600;
  letter-spacing: 0.04em;
}

.divider::before,
.divider::after {
  content: "";
  flex: 1;
  border-top: 1px solid var(--line);
}

/* ---- rendered answers ----
   Markdown arrives as HTML, so the newlines between blocks are markup, not
   text: pre-wrap would show each one as an extra blank line. Headings are
   scaled under the page's own, and everything uses the theme tokens so dark
   mode needs nothing of its own. */
.said--md { white-space: normal; }
.said--md > :first-child { margin-top: 0; }
.said--md > :last-child { margin-bottom: 0; }

.said--md p,
.said--md ul,
.said--md ol,
.said--md blockquote,
.said--md pre,
.said--md table { margin: 0 0 0.75em; }

.said--md h1,
.said--md h2,
.said--md h3,
.said--md h4,
.said--md h5,
.said--md h6 { margin: 1.25em 0 0.5em; font-weight: 600; line-height: 1.3; }
.said--md h1 { font-size: 1.375rem; }
.said--md h2 { font-size: 1.25rem; }
.said--md h3 { font-size: 1.125rem; }
.said--md h4,
.said--md h5,
.said--md h6 { font-size: 1rem; }

.said--md ul,
.said--md ol { padding-left: 1.5em; }
.said--md li + li { margin-top: 0.25em; }
.said--md li > ul,
.said--md li > ol { margin: 0.25em 0 0; }

.said--md blockquote {
  padding-left: 12px;
  border-left: 3px solid var(--accent);
  color: var(--muted);
}

.said--md code {
  padding: 0.1em 0.3em;
  border-radius: 4px;
  background: color-mix(in srgb, var(--fg) 8%, transparent);
  font: 0.875em/1.45 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.said--md pre {
  padding: 10px 12px;
  overflow-x: auto;
  border-radius: 6px;
  background: color-mix(in srgb, var(--fg) 6%, transparent);
}

.said--md pre code { padding: 0; background: none; white-space: pre; }
.said--md hr { margin: 1.25em 0; border: 0; border-top: 1px solid var(--line); }
.said--md a { color: var(--accent); }

/* Wide tables scroll inside the turn instead of widening the page. */
.said--md table {
  display: block;
  width: max-content;
  max-width: 100%;
  overflow-x: auto;
  border-collapse: collapse;
}

.said--md th,
.said--md td { padding: 6px 10px; border-bottom: 1px solid var(--line); text-align: left; }
.said--md th { white-space: normal; }
.said--md .align-center { text-align: center; }
.said--md .align-right { text-align: right; }

/* The gateway sends nothing until the whole answer is ready (up to a minute
   in the spikes), so this is a state a student really sits in. */
.thinking .said { color: var(--muted); font-style: italic; }

.thinking .said::after {
  content: "";
  display: inline-block;
  width: 0.5em;
  height: 0.5em;
  margin-left: 0.4em;
  border-radius: 50%;
  background: currentColor;
  animation: pulse 1.2s ease-in-out infinite;
}

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

@media (prefers-reduced-motion: reduce) {
  .thinking .said::after { animation: none; opacity: 0.6; }
}

.turn .error { margin: 8px 0 0; }

.retry {
  width: auto;
  margin: 8px 0 0;
  padding: 6px 14px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: none;
  color: var(--fg);
  font-size: 0.875rem;
  font-weight: 500;
}

.retry:hover { border-color: var(--accent); color: var(--accent); filter: none; }
.retry:disabled { opacity: 0.5; cursor: not-allowed; }

/* Quiet until the turn is hovered or the button is focused: one per turn,
   and they would otherwise compete with the text for attention. */
.copy {
  width: auto;
  margin: 6px 0 0;
  padding: 2px 8px;
  border: 1px solid transparent;
  border-radius: 6px;
  background: none;
  color: var(--muted);
  font-size: 0.8125rem;
  font-weight: 400;
  opacity: 0;
  transition: opacity 0.12s ease-in;
}

.turn:hover .copy,
.copy:focus-visible { opacity: 1; }
.copy:hover { border-color: var(--line); color: var(--accent); filter: none; }

/* A touch screen has no hover, so the button would never appear. */
@media (hover: none) {
  .copy { opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
  .copy { transition: none; }
}

/* ---- composer ---- */

/* `scrollIntoView({ block: "end" })` puts a new turn's end at the bottom of
   the viewport, which is where the sticky composer sits: without room kept
   for it, the end of the newest answer and its Copy button hide behind it. */
html:has(#composer) { scroll-padding-bottom: 10rem; }

#composer {
  position: sticky;
  bottom: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: flex-end;
  padding: 12px 0;
  background: linear-gradient(to top, var(--bg) 70%, transparent);
}

#composer textarea {
  flex: 1;
  min-width: 0;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  color: var(--fg);
  font: inherit;
  resize: vertical;
}

#composer textarea:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
#composer button { width: auto; margin: 0; padding: 10px 20px; }
#composer textarea:disabled,
#composer button:disabled { opacity: 0.6; cursor: not-allowed; }

#composer + .aside { margin-top: 4px; font-size: 0.8125rem; }

/* ---- voice input (#56) ---- */

/* Inside the sticky composer, on its own row, so the status and the consent line
   stay in view however long the page is. `voice.js` reveals it together with
   the Record button. */
#voice {
  flex-basis: 100%;
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 2px 12px;
  align-items: center;
}
#voice[hidden] { display: none; }

#voice .consent {
  grid-column: 1 / -1;
  margin: 0;
  color: var(--muted);
  font-size: 0.8125rem;
  line-height: 1.4;
}

/* About the last action, not about the button. Empty most of the time, so it
   claims no space. */
.voice-status {
  grid-column: 1 / -1;
  margin: 0;
  color: var(--accent);
  font-size: 0.8125rem;
}
.voice-status:empty { display: none; }

#voice-level { width: 6rem; height: 0.5rem; grid-column: 1 / -1; }

/* On a phone the textarea would be squeezed to half a screen between the two
   buttons, so it takes the whole first row and the buttons the second. */
@media (max-width: 30rem) {
  #composer textarea { flex-basis: 100%; }
  #composer button { flex: 1; }
}

/* Recording is the one state where the control must be unmistakable: the
   microphone is open, and the student needs to see that at a glance. */
#record.recording {
  border-color: var(--error);
  color: var(--error);
  background: var(--error-bg);
}
#record.recording::before {
  content: "";
  display: inline-block;
  width: 0.5em;
  height: 0.5em;
  margin-right: 0.5em;
  border-radius: 50%;
  background: var(--error);
  animation: pulse 1.4s ease-in-out infinite;
}
@media (prefers-reduced-motion: reduce) {
  #record.recording::before { animation: none; }
}

/* ---- quick actions and lessons ---- */

.course { margin: 20px 0 0; padding-top: 16px; border-top: 1px solid var(--line); }
.quick-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.quick-actions .quiet { width: auto; margin: 0; }
.lessons { margin-top: 16px; }
.lessons summary { cursor: pointer; color: var(--muted); font-size: 0.9375rem; }
.lessons summary:hover,
.lessons[open] summary { color: var(--accent); }
.lessons summary:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.lessons ol { margin: 10px 0 0; padding: 0; list-style: none; }
.lessons li {
  display: flex;
  flex-wrap: wrap;
  gap: 2px 12px;
  align-items: baseline;
  padding: 6px 0;
  border-bottom: 1px solid var(--line);
}
.lessons .link-button { text-align: left; overflow-wrap: anywhere; }
.lessons .link-button:disabled { opacity: 0.5; cursor: not-allowed; }
.lessons a { color: var(--muted); font-size: 0.8125rem; white-space: nowrap; }
.lessons a:hover { color: var(--accent); }
.course-book { display: inline-block; margin-top: 12px; color: var(--accent); }

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/* ---- admin ---- */

.page--wide { max-width: 64rem; }
.muted { color: var(--muted); font-size: 0.8125rem; }
.admin-tabs { display: flex; flex-wrap: wrap; gap: 4px 18px; margin: -8px 0 20px; }
.admin-tabs a { color: var(--muted); text-decoration: none; }
.admin-tabs a:hover { color: var(--accent); }
.admin-tabs a[aria-current="page"] { color: var(--fg); font-weight: 600; }
.admin-facts { display: flex; flex-wrap: wrap; gap: 4px 24px; margin: 0 0 20px; color: var(--muted); }
.admin-allowance fieldset,
.admin-form fieldset {
  display: flex;
  flex-wrap: wrap;
  gap: 12px 20px;
  align-items: flex-end;
  margin: 0 0 24px;
  padding: 12px 16px 16px;
  border: 1px solid var(--line);
  border-radius: 6px;
}
.admin-allowance legend,
.admin-form legend { padding: 0 6px; font-weight: 600; }
.admin-allowance label,
.admin-form label { flex: 1 1 12rem; margin: 0; }
.admin-allowance input,
.admin-form input { margin-top: 4px; }
.admin-allowance button,
.admin-form button { width: auto; margin: 0; }
.admin-allowance .aside { flex-basis: 100%; margin: 0; font-size: 0.8125rem; }
.table-wrap { overflow-x: auto; margin: 0 0 24px; }
.admin-table { width: 100%; border-collapse: collapse; font-size: 0.9375rem; }
.admin-table th,
.admin-table td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
.admin-table th { color: var(--muted); font-weight: 600; font-size: 0.8125rem; }
.admin-table .num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.admin-actions form { display: inline; margin: 0; }
.admin-actions { white-space: nowrap; }
.admin-actions > * + * { margin-left: 12px; }
.invite-link { overflow-wrap: anywhere; user-select: all; }
.slot--needs_reset td { color: var(--accent); }


/* ---- spoken replies (#57) ---- */

/* Sits beside Copy, under the reply it speaks. */
button.play {
  /* The base `button` is full width; this one sits beside Copy. */
  width: auto;
  margin: 6px 0 0 8px;
  padding: 3px 12px;
  border: 1px solid var(--accent);
  border-radius: 6px;
  background: var(--accent-tint);
  color: var(--accent);
  font: inherit;
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
}
/* A play triangle before the label, drawn here so no copy lives in the style;
   the empty alternative keeps screen readers from reading it out. */
button.play::before { content: "\25B6" / ""; margin-right: 0.45em; font-size: 0.75em; }
button.play:hover { background: var(--accent); color: var(--on-accent); filter: none; }
button.play.loading { border-color: var(--control-line); color: var(--muted); background: var(--control-bg); cursor: progress; }
button.play.loading::before { content: "\2026" / ""; }
button.play.playing { background: var(--accent); color: var(--on-accent); }
button.play.playing::before { content: "\25A0" / ""; }
.audio-error { margin: 4px 0 0; font-size: 0.8125rem; }
