/*
 * Sizing model: page layout uses rem, components use em.
 * Each page sets the root size from its viewport, so a layout keeps its proportions on any screen.
 * Components scale with their context (for example a group card sizes its own text to the card).
 */

/* ===== Design tokens ===== */
:root {
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", "Noto Sans TC", "PingFang HK", sans-serif;

  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f2f4f7;
  --surface-3: #e9ecf1;
  --line: #e4e7ec;
  --line-strong: #d0d5dd;
  --ink: #101828;
  --ink-2: #344054;
  --ink-3: #5d6679;

  --brand: #2347c5;
  --brand-hover: #1c3aa6;
  --brand-soft: #eef2ff;
  --brand-ink: #ffffff;

  --live: #067647;   --live-soft: #e7f8ee;
  --paused: #a14a07; --paused-soft: #fff6e5;
  --ended: #3538cd;  --ended-soft: #eef0ff;
  --danger: #c4320a; --danger-hover: #a52a08; --danger-soft: #fff1ec;
  --warn: #a14a07;

  /* Student colours: colour-blind-safe, always paired with the letter. */
  --A: #1f5fd1; --A-soft: #e8f0ff;
  --B: #c4410c; --B-soft: #fff0e8;
  --C: #087a50; --C-soft: #e6f6ef;
  --D: #8e2fb0; --D-soft: #f7ebfc;
  --Q: #667085; --Q-soft: #f2f4f7;

  --r-sm: .5rem; --r-md: .75rem; --r-lg: 1rem; --r-xl: 1.5rem; --r-pill: 999px;
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, .06);
  --shadow-md: 0 1px 3px rgba(16, 24, 40, .06), 0 6px 16px rgba(16, 24, 40, .06);
  --shadow-lg: 0 12px 32px rgba(16, 24, 40, .14);
  --ring: 0 0 0 3px rgba(35, 71, 197, .28);

  --s1: .25rem; --s2: .5rem; --s3: .75rem; --s4: 1rem; --s5: 1.5rem; --s6: 2rem; --s7: 3rem;
  --ease: cubic-bezier(.2, .7, .2, 1);
}

html { font-size: clamp(15px, 1.6vh, 24px); -webkit-text-size-adjust: 100%; }
*, *::before, *::after { box-sizing: border-box; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 1rem/1.5 var(--font); -webkit-font-smoothing: antialiased;
}
h1, h2, h3, h4, p { margin: 0; }
h1 { font-size: 1.125em; font-weight: 650; letter-spacing: -.01em; }
h2 { font-size: 1.25em; font-weight: 650; letter-spacing: -.01em; }
h3 { font-size: 1em; font-weight: 650; }
h4, .h4 { font-size: .75em; font-weight: 650; text-transform: uppercase; letter-spacing: .06em; color: var(--ink-3); }
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }
[hidden] { display: none !important; }
:focus-visible { outline: none; box-shadow: var(--ring); border-radius: var(--r-sm); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ===== Layout helpers ===== */
.stack { display: flex; flex-direction: column; gap: var(--gap, var(--s4)); }
.cluster { display: flex; flex-wrap: wrap; align-items: center; gap: var(--gap, var(--s2)); }
.spread { display: flex; align-items: center; justify-content: space-between; gap: var(--s4); }
.grow { flex: 1; min-width: 0; }
.muted { color: var(--ink-3); }
.small { font-size: .8125em; }
.tiny { font-size: .75em; }
.num { font-variant-numeric: tabular-nums; }
.eyebrow { font-size: .75em; font-weight: 650; letter-spacing: .06em; text-transform: uppercase; color: var(--ink-3); }
.topic-text { white-space: pre-line; overflow-wrap: anywhere; }
.page { max-width: 90rem; margin: 0 auto; padding: var(--s5); }
.section-head { display: flex; align-items: flex-end; justify-content: space-between; gap: var(--s4); margin-bottom: var(--s3); }

/* ===== App bar ===== */
.appbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: var(--s4);
  padding: var(--s3) var(--s5); min-height: 4rem;
  background: rgba(255, 255, 255, .92); backdrop-filter: saturate(1.4) blur(8px);
  border-bottom: 1px solid var(--line);
}
.appbar .brand { display: flex; align-items: center; gap: var(--s3); font-weight: 650; color: var(--ink); white-space: nowrap; }
.appbar a.brand:hover { text-decoration: none; }
.brand-mark {
  width: 2rem; height: 2rem; border-radius: .5625rem; display: grid; place-items: center; flex: none;
  background: var(--brand); color: #fff; font-size: .8125rem; font-weight: 750; letter-spacing: .02em;
}
.appbar nav { display: flex; gap: var(--s1); }
.appbar nav a { display: inline-flex; align-items: center; gap: .4em; padding: .55em .85em; border-radius: var(--r-sm); color: var(--ink-2); font-weight: 550; font-size: .875rem; }
.appbar nav a svg { width: 1.15em; height: 1.15em; }
.appbar nav a:hover { background: var(--surface-2); text-decoration: none; }
.appbar nav a[aria-current="page"] { color: var(--brand); background: var(--brand-soft); }

/* ===== Surfaces ===== */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-lg); box-shadow: var(--shadow-sm); }
.card-pad { padding: var(--s5); }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: var(--s3); padding: var(--s4) var(--s5); border-bottom: 1px solid var(--line); }

/* ===== Buttons ===== */
.btn {
  --h: 2.857em;
  display: inline-flex; align-items: center; justify-content: center; gap: .571em;
  height: var(--h); padding: 0 1.143em; border-radius: .571em;
  border: 1px solid var(--line-strong); background: var(--surface); color: var(--ink-2);
  font: 600 .875em/1 var(--font); cursor: pointer; white-space: nowrap; user-select: none;
  transition: background .15s, border-color .15s, color .15s, box-shadow .15s, transform .05s;
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: not-allowed; transform: none; }
.btn svg { width: 1.286em; height: 1.286em; flex: none; }
.btn-primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); box-shadow: var(--shadow-sm); }
.btn-primary:hover { background: var(--brand-hover); border-color: var(--brand-hover); }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-danger:hover { background: var(--danger-hover); border-color: var(--danger-hover); }
.btn-danger-soft { background: var(--danger-soft); border-color: transparent; color: var(--danger); }
.btn-danger-soft:hover { background: #ffe4da; }
.btn-ghost { background: transparent; border-color: transparent; }
.btn-ghost:hover { background: var(--surface-2); }
.btn-sm { --h: 2.462em; padding: 0 .923em; font-size: .8125em; }
.btn-lg { --h: 3.25em; padding: 0 1.375em; font-size: 1em; border-radius: .75em; }
.btn-xl { --h: 3.556em; padding: 0 1.556em; font-size: 1.125em; border-radius: .667em; }
.btn-block { width: 100%; }
.btn-icon { width: var(--h); padding: 0; }

/* ===== Form fields ===== */
.field { display: flex; flex-direction: column; gap: .375em; }
.field > label, .label { font-size: .8125em; font-weight: 600; color: var(--ink-2); }
.hint { font-size: .75em; color: var(--ink-3); }
.hint.error { color: var(--danger); font-weight: 600; }
.input, .textarea {
  width: 100%; font: 1em/1.4 var(--font); color: var(--ink);
  padding: .625em .75em; border: 1px solid var(--line-strong); border-radius: .5em; background: var(--surface);
  transition: border-color .15s, box-shadow .15s;
}
.input:focus, .textarea:focus { outline: none; border-color: var(--brand); box-shadow: var(--ring); }
.textarea { resize: vertical; min-height: 6em; }
.input-lg { font-size: 1.125em; padding: .778em .889em; border-radius: .667em; }

.segmented { display: inline-flex; flex-wrap: wrap; gap: .375em; }
.segmented button {
  height: 2.615em; padding: 0 .923em; border-radius: var(--r-pill); border: 1px solid var(--line-strong);
  background: var(--surface); color: var(--ink-2); font: 600 .8125em/1 var(--font); cursor: pointer;
}
.segmented button[aria-pressed="true"] { background: var(--brand-soft); border-color: var(--brand); color: var(--brand); }

/* ===== Badges & status ===== */
.badge {
  display: inline-flex; align-items: center; gap: .5em; height: 2em; padding: 0 .833em;
  border-radius: var(--r-pill); background: var(--surface-2); color: var(--ink-3);
  font: 650 .75em/1 var(--font); letter-spacing: .01em; white-space: nowrap;
}
.badge::before { content: ""; width: .5em; height: .5em; border-radius: 50%; background: currentColor; }
.badge.plain::before { display: none; }
.badge.live { background: var(--live-soft); color: var(--live); }
.badge.live::before { animation: pulse 1.6s infinite; }
.badge.paused { background: var(--paused-soft); color: var(--paused); }
.badge.ended { background: var(--ended-soft); color: var(--ended); }
.badge.ready { background: var(--brand-soft); color: var(--brand); }
.badge.mock { background: var(--danger-soft); color: var(--danger); }
@keyframes pulse { 50% { opacity: .25; } }

.conn { display: inline-flex; align-items: center; gap: .5em; font-size: .75em; color: var(--ink-3); white-space: nowrap; }
.conn::before { content: ""; flex: none; width: .667em; height: .667em; border-radius: 50%; background: var(--live); }
.conn.down::before { background: var(--paused); animation: pulse 1s infinite; }

/* ===== Student avatar ===== */
.who {
  --c: var(--Q);
  display: inline-flex; align-items: center; justify-content: center; flex: none;
  width: 2.154em; height: 2.154em; border-radius: .615em; background: var(--c); color: #fff;
  font: 750 .8125em/1 var(--font);
}
.who.A { --c: var(--A); } .who.B { --c: var(--B); } .who.C { --c: var(--C); } .who.D { --c: var(--D); }
.who-sm { font-size: .6875em; width: 1.818em; height: 1.818em; border-radius: .545em; }
.who-lg { font-size: 1.125em; width: 2.444em; height: 2.444em; border-radius: .667em; }
.who-name { display: inline-flex; align-items: center; gap: .5em; font-weight: 600; min-width: 0; }
.avatars { display: inline-flex; gap: .25em; }

/* ===== Keyword chips & feed ===== */
.chips { display: flex; flex-wrap: wrap; gap: .375em; }
.chip {
  display: inline-flex; align-items: center; height: 2em; padding: 0 .714em; border-radius: .571em;
  background: var(--chip-bg, var(--surface-2)); color: var(--ink); font-size: .875em; font-weight: 550;
}
.chip-empty { background: transparent !important; color: var(--ink-3); font-weight: 500; font-style: italic; padding: 0; }
.feed { display: flex; flex-direction: column; }
.feed-item { display: grid; grid-template-columns: auto 1fr auto; gap: .75em; align-items: start; padding: .625em 0; border-top: 1px solid var(--line); }
.feed-item:first-child { border-top: 0; padding-top: 0; }
.feed-item.A { --chip-bg: var(--A-soft); } .feed-item.B { --chip-bg: var(--B-soft); }
.feed-item.C { --chip-bg: var(--C-soft); } .feed-item.D { --chip-bg: var(--D-soft); }
.feed-item.fresh { animation: rise .45s var(--ease); }
.feed-time { padding-top: .5em; }
@keyframes rise { from { opacity: 0; transform: translateY(-6px); } }
.feed-empty { display: flex; gap: .625em; align-items: center; color: var(--ink-3); font-size: .8125em; padding: .875em; border: 1px dashed var(--line-strong); border-radius: .75em; }
.feed-empty svg { width: 1.5em; height: 1.5em; flex: none; }

/* ===== Talk balance ===== */
.balance { display: flex; height: .5em; border-radius: var(--r-pill); overflow: hidden; background: var(--surface-3); }
.balance > span { height: 100%; transition: width .6s var(--ease); }
.balance-wrap { display: flex; flex-direction: column; gap: .5em; }
.balance-legend { display: flex; flex-wrap: wrap; gap: .25em .875em; font-size: .75em; color: var(--ink-2); }
.balance-legend > span { display: inline-flex; align-items: center; gap: .5em; }
.balance-rows { display: flex; flex-direction: column; gap: .625em; }
.balance-row { display: grid; grid-template-columns: minmax(0, 1fr) 2.75em; gap: .375em .75em; align-items: center; }
.balance-row .pct { text-align: right; font-weight: 650; color: var(--ink-2); }
.balance-row .bar { grid-column: 1 / -1; height: .5em; border-radius: var(--r-pill); background: var(--surface-3); overflow: hidden; }
.balance-row .bar > span { display: block; height: 100%; border-radius: inherit; transition: width .6s var(--ease); }

/* ===== Timer ===== */
.timer { font: 700 1.75em/1 var(--font); font-variant-numeric: tabular-nums; letter-spacing: -.02em; }
.timer.soon { color: var(--warn); }
.timer.over { color: var(--danger); }
.progress { height: .25rem; background: var(--surface-3); border-radius: var(--r-pill); overflow: hidden; }
.progress > span { display: block; height: 100%; background: var(--brand); transition: width 1s linear; }
.progress.soon > span { background: var(--warn); }
.progress.over > span { background: var(--danger); }

/* ===== Summary ===== */
.summary { display: flex; flex-direction: column; gap: 1em; }
.summary .overview { font-size: 1.0625em; line-height: 1.5; color: var(--ink); }
.summary ul { margin: .375em 0 0; padding-left: 1.25em; display: flex; flex-direction: column; gap: .25em; }
.summary li::marker { color: var(--ink-3); }
.summary .contrib { list-style: none; padding: 0; gap: .5em; }
.summary .contrib li { display: grid; grid-template-columns: auto 1fr; gap: .625em; align-items: start; }
.summary-sec { display: flex; flex-direction: column; gap: .25em; }
.summary.compact { gap: .75em; }
.summary.compact .overview { font-size: 1em; }
.note { display: flex; gap: .5em; align-items: flex-start; font-size: .75em; color: var(--ink-3); padding: .5em .75em; border-radius: .5em; background: var(--surface-2); }
.note svg { width: 1.333em; height: 1.333em; flex: none; margin-top: .1em; }
.pending { display: flex; align-items: center; gap: .625em; color: var(--ink-3); font-size: .875em; }
.spinner { width: 1.143em; height: 1.143em; flex: none; border: 2px solid var(--line-strong); border-top-color: var(--brand); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ===== Callouts, alerts, empty states ===== */
.callout { display: flex; gap: .75em; padding: .75em 1em; border-radius: .75em; background: var(--brand-soft); color: var(--ink-2); }
.callout.warn { background: var(--paused-soft); color: #6b3505; }
.callout.danger { background: var(--danger-soft); color: #7a1f06; }
.callout svg { flex: none; width: 1.25em; height: 1.25em; margin-top: .1em; }
.alert { display: flex; gap: .5em; align-items: flex-start; font-size: .8125em; font-weight: 550; padding: .5em .75em; border-radius: .5em; }
.alert svg { width: 1.23em; height: 1.23em; flex: none; margin-top: .1em; }
.alert.warn { background: var(--paused-soft); color: #6b3505; }
.alert.danger { background: var(--danger-soft); color: #7a1f06; }
.empty { display: grid; place-items: center; align-content: center; text-align: center; gap: .5em; padding: 3em 1.5em; color: var(--ink-3); }
.empty svg { width: 2.25em; height: 2.25em; color: var(--line-strong); }
.empty strong { color: var(--ink-2); }

/* ===== Dialog & toast ===== */
dialog.modal {
  border: 0; padding: 0; border-radius: var(--r-xl); width: min(27.5rem, calc(100vw - 2rem));
  box-shadow: var(--shadow-lg); color: var(--ink); font-size: 1rem;
}
dialog.modal.modal-wide { width: min(46rem, calc(100vw - 2rem)); }
dialog.modal.modal-narrow { width: min(23.75rem, calc(100vw - 2rem)); }
dialog.modal::backdrop { background: rgba(16, 24, 40, .45); backdrop-filter: blur(2px); }
dialog.modal[open] { animation: pop .2s var(--ease); }
@keyframes pop { from { opacity: 0; transform: translateY(8px) scale(.98); } }
.modal-body { padding: 1.5rem 1.5rem 1rem; display: flex; flex-direction: column; gap: .75rem; max-height: calc(100vh - 10rem); overflow: auto; }
.modal-body p { color: var(--ink-2); }
.modal-foot { display: flex; justify-content: flex-end; gap: .5rem; padding: 1rem 1.5rem 1.5rem; }
.modal-icon { width: 2.75rem; height: 2.75rem; border-radius: .75rem; display: grid; place-items: center; background: var(--brand-soft); color: var(--brand); }
.modal-icon.danger { background: var(--danger-soft); color: var(--danger); }
.modal-icon svg { width: 1.375rem; height: 1.375rem; }
.pin-input { text-align: center; letter-spacing: .4em; font-size: 1.5em; font-weight: 650; }
.shake { animation: shake .35s; }
@keyframes shake { 20%, 60% { transform: translateX(-6px); } 40%, 80% { transform: translateX(6px); } }
.toasts { position: fixed; bottom: 1.25rem; left: 50%; transform: translateX(-50%); z-index: 50; display: flex; flex-direction: column; gap: .5rem; align-items: center; }
.toast { background: var(--ink); color: #fff; padding: .625rem 1rem; border-radius: .75rem; box-shadow: var(--shadow-lg); font-size: .875rem; animation: pop .2s var(--ease); max-width: min(35rem, 90vw); }
.toast.error { background: #7a1f06; }

/* ===== Mic level meter ===== */
.meter { display: inline-flex; align-items: flex-end; gap: .1875em; height: 1.125em; }
.meter > i { width: .25em; height: 20%; border-radius: 2px; background: var(--line-strong); transition: height .08s linear, background .2s; }
.meter.on > i { background: var(--live); }

/* ===== Group card =====
 * Every size inside is in em, so the card's own font-size scales the whole card.
 * Inside `.fit-grid` each cell is a size container and the card picks its text size from the cell.
 */
.groups { display: grid; grid-template-columns: repeat(auto-fill, minmax(18rem, 1fr)); gap: var(--s4); align-items: start; }
.fit-grid { display: grid; grid-template-columns: repeat(var(--cols, 3), minmax(0, 1fr)); grid-auto-rows: minmax(0, 1fr); gap: var(--gap, 1rem); min-height: 0; }
.fit-grid > .cell { container-type: size; min-width: 0; min-height: 0; }
.fit-grid > .cell > .gcard { height: 100%; font-size: clamp(.875rem, var(--card-scale, 5.5cqmin), var(--card-max, 2rem)); }
.gcard {
  position: relative; display: flex; flex-direction: column; gap: .875em; padding: 1.125em 1.125em 1.25em;
  background: var(--surface); border: 1px solid var(--line); border-radius: 1em; box-shadow: var(--shadow-sm); overflow: hidden;
}
.gcard::before { content: ""; position: absolute; inset: 0 0 auto; height: .1875em; background: var(--line); }
.gcard[data-status="live"]::before { background: var(--live); }
.gcard[data-status="paused"]::before { background: var(--paused); }
.gcard[data-status="ended"]::before { background: var(--ended); }
.gcard[data-status="ready"]::before { background: var(--brand); }
.gcard.clickable { cursor: pointer; transition: box-shadow .15s, border-color .15s; }
.gcard.clickable:hover { box-shadow: var(--shadow-md); border-color: var(--line-strong); }
.gcard-head { display: flex; align-items: center; gap: .75em; flex: none; }
.gcard-head h3 { font-size: 1.0625em; line-height: 1.2; white-space: nowrap; }
.gcard-head .open-hint { width: 1.25em; height: 1.25em; color: var(--ink-3); flex: none; }
.gnum {
  font: 750 1.0625em/1 var(--font); width: 2.353em; height: 2.353em; border-radius: .706em;
  display: grid; place-items: center; flex: none; background: var(--surface-2); color: var(--ink); font-variant-numeric: tabular-nums;
}
.gcard[data-status="live"] .gnum { background: var(--live-soft); color: var(--live); }
.gcard[data-status="ready"] .gnum { background: var(--brand-soft); color: var(--brand); }
.gcard[data-status="ended"] .gnum { background: var(--ended-soft); color: var(--ended); }
.gcard-wait { font-size: .875em; color: var(--ink-3); }
.ready-row { display: flex; align-items: center; gap: .75em; flex-wrap: wrap; }
.gcard .feed, .gcard .summary { flex: 1; min-height: 0; overflow: hidden; }
.fit-grid .gcard .feed, .fit-grid .gcard .summary { -webkit-mask-image: linear-gradient(#000 78%, transparent); mask-image: linear-gradient(#000 78%, transparent); }
.gcard .balance-wrap, .gcard .alert { flex: none; }

/* ===== Suggested direction ===== */
.nudge {
  display: flex; gap: .625em; align-items: flex-start; flex: none; padding: .625em .75em; border-radius: .625em;
  background: var(--brand-soft); border-left: .25em solid var(--brand); color: var(--ink);
}
.nudge > svg { width: 1.25em; height: 1.25em; flex: none; color: var(--brand); margin-top: .1em; }
.nudge-label { display: block; font-size: .7em; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; color: var(--brand); }
.nudge-text { font-weight: 650; line-height: 1.35; margin-top: .15em; }
.nudge-reason { font-size: .75em; color: var(--ink-3); margin-top: .2em; }
.nudge .btn-icon { margin: -.25em -.25em 0 0; }
.gcard-sub { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ===== Checkbox row and select ===== */
.check { display: flex; gap: .625em; align-items: flex-start; cursor: pointer; }
.check input { width: 1.125em; height: 1.125em; margin: .2em 0 0; flex: none; accent-color: var(--brand); }
.check strong { display: block; font-size: .875em; font-weight: 600; color: var(--ink-2); }
.select {
  width: 100%; font: 1em/1.4 var(--font); color: var(--ink); padding: .5em .75em; border: 1px solid var(--line-strong);
  border-radius: .5em; background: var(--surface);
}
.select:focus { outline: none; border-color: var(--brand); box-shadow: var(--ring); }

/* ===== Tabs ===== */
.tabs { display: flex; gap: .25em; overflow-x: auto; border-bottom: 1px solid var(--line); scrollbar-width: none; }
.tabs button {
  flex: none; height: 2.857em; padding: 0 1em; border: 0; background: none; cursor: pointer;
  font: 600 .875em/1 var(--font); color: var(--ink-3); border-bottom: 2px solid transparent; margin-bottom: -1px;
}
.tabs button:hover { color: var(--ink); }
.tabs button[aria-selected="true"] { color: var(--brand); border-bottom-color: var(--brand); }

@media (max-width: 640px) {
  .page { padding: var(--s4); }
  .appbar { padding: var(--s2) var(--s4); gap: var(--s3); }
  .appbar nav a span { display: none; }
  .appbar nav a { padding: .55em; }
  .appbar .brand { gap: var(--s2); }
  .appbar .conn { max-width: .667em; overflow: hidden; gap: 0; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
