/* ==========================================================================
   TRIDENT base — the page, the chrome, and the primitives every screen uses.
   Progress everywhere is one continuous meter (see "Progress" below); the
   tricolour appears once, as the brand band under the header.
   ========================================================================== */

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

/* `hidden` wins over any component's display rule. Without this, a fixed
   element that is "hidden" still paints: the empty outlined rectangle that
   used to sit at the bottom of every page was the toast doing exactly that. */
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  font-size: var(--step-0);
  line-height: 1.55;
  min-height: 100vh;
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
}

/* the paper grain, drawn once behind everything */
body::before {
  content: '';
  position: fixed;
  inset: 0;
  background-image: var(--grain);
  pointer-events: none;
  z-index: 0;
}

h1, h2, h3, h4 {
  font-family: var(--font-ui);
  font-weight: 700;
  line-height: 1.18;
  letter-spacing: -0.012em;
  margin: 0 0 var(--sp-3);
  color: var(--text);
}
h1 { font-size: var(--step-4); }
h2 { font-size: var(--step-3); }
h3 { font-size: var(--step-2); }
h4 { font-size: var(--step-1); }

/* the serif appears on exactly two things: a lesson title and a story title */
.title-serif { font-family: var(--font-title); font-weight: 600; letter-spacing: -0.004em; }

p { margin: 0 0 var(--sp-4); max-width: var(--measure); }
a { color: var(--teal-ink); text-underline-offset: 3px; }
a:hover { color: var(--saffron-ink); }
img { max-width: 100%; height: auto; display: block; }
ul, ol { padding-left: 1.15em; }
button, input, select, textarea { font: inherit; color: inherit; }
small { font-size: var(--step--1); }

:focus-visible {
  outline: 3px solid var(--saffron);
  outline-offset: 2px;
  border-radius: var(--radius-xs);
}

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

.skip-link {
  position: absolute; left: var(--sp-4); top: -60px; z-index: 100;
  background: var(--ink); color: var(--text-on-ink);
  padding: var(--sp-2) var(--sp-4); border-radius: var(--radius-sm);
  text-decoration: none;
}
.skip-link:focus { top: var(--sp-4); }

.shell { width: min(100% - 2rem, var(--shell)); margin-inline: auto; }
#main { padding-block: var(--sp-5) var(--sp-8); position: relative; z-index: 1; }
#main:focus { outline: none; }
.noscript { padding: var(--sp-6); }

/* ==========================================================================
   Header — brand, four destinations, and the learner's standing
   ========================================================================== */
/* Sticky, fully opaque, above page content and below menus and dialogs. Its
   height is one token, so anchors and focus land below it, never under it. */
html { scroll-padding-top: calc(var(--header-h) + var(--sp-4)); }
.app-header {
  position: sticky; top: 0; z-index: 30;
  background-color: var(--paper); background-image: none;
}
.header-inner {
  display: flex; align-items: center; gap: var(--sp-4);
  min-height: calc(var(--header-h) - var(--flag-height) - 2px); padding-block: var(--sp-2);
  min-width: 0;
}

.brand {
  display: flex; align-items: center; gap: var(--sp-2); min-height: 44px;
  text-decoration: none; color: var(--text); flex: none;
}
.brand-mark { width: 30px; height: 30px; object-fit: contain; flex: none; }
.brand-text strong {
  font-weight: 800; letter-spacing: 0.14em; font-size: 1.02rem;
  display: block; line-height: 1;
}
.brand-text em { display: none; }

/* --- the four destinations ------------------------------------------- */
.primary-nav { margin-left: var(--sp-2); min-width: 0; }
.primary-nav ul { display: flex; gap: var(--sp-1); list-style: none; margin: 0; padding: 0; }
.primary-nav a {
  display: flex; align-items: center; gap: var(--sp-2);
  padding: var(--sp-2) 10px; min-height: 44px;
  text-decoration: none; color: var(--text); font-weight: 600;
  border-radius: var(--radius-sm);
  border-bottom: 3px solid transparent;
  white-space: nowrap;
}
.primary-nav a svg { flex: none; }
.primary-nav a:hover { background: var(--surface-sunken); color: var(--text); }
.primary-nav a[aria-current="page"] {
  border-bottom-color: var(--saffron);
  background: var(--wash-saffron);
}
:root[data-theme="dark"] .primary-nav a[aria-current="page"] { background: var(--wash-saffron); }

/* --- standing: class progress, streak, profile ------------------------ */
.hud { display: flex; align-items: center; gap: var(--sp-3); margin-left: auto; min-width: 0; }

.hud-course { display: flex; flex-direction: column; justify-content: center; gap: 3px; white-space: nowrap; min-height: 44px; }
.hud-course b { font-size: var(--step--1); font-weight: 700; line-height: 1; }
.hud-course span { font-size: var(--step--2); color: var(--text-muted); line-height: 1; }

/* streak and XP: labelled figures, quieter than the class chip and plainly
   not buttons (no border, no fill). The tooltip appears on hover and focus. */
.stat-chip {
  position: relative; display: inline-flex; align-items: center; gap: 5px;
  font-size: var(--step--1); font-weight: 700; line-height: 1;
  padding: var(--sp-2) var(--sp-1); border-radius: var(--radius-sm);
  white-space: nowrap; cursor: default;
}
.stat-chip svg { width: 16px; height: 16px; flex: none; }
.stat-unit { font-weight: 600; }
.hud-streak { color: var(--saffron-ink); }
.hud-xp { color: var(--gold-ink); }
.stat-chip:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }
.stat-tip {
  position: absolute; top: calc(100% + 8px); right: 0; z-index: 70;
  width: max-content; max-width: 240px; white-space: normal;
  padding: var(--sp-2) var(--sp-3); border-radius: var(--radius-sm);
  background: var(--ink); color: var(--text-on-ink);
  font-size: var(--step--1); font-weight: 600; line-height: 1.4;
  opacity: 0; visibility: hidden; pointer-events: none;
}
.stat-chip:hover .stat-tip, .stat-chip:focus .stat-tip { opacity: 1; visibility: visible; }

/* the profile button: the learner's portrait, "Explorer", and a chevron */
.profile-btn {
  display: inline-flex; align-items: center; gap: var(--sp-2);
  min-height: 44px; background: var(--surface); border: 1px solid var(--line-strong);
  border-radius: 999px; padding: 3px var(--sp-3) 3px 3px;
  cursor: pointer; font-weight: 700; font-size: var(--step--1);
  color: var(--text); white-space: nowrap;
}
.profile-btn:hover { border-color: var(--ink); }
.avatar-host { display: inline-flex; }

/* portrait frames: a circle, a thin navy ring and a gold keyline */
.avatar-frame {
  display: inline-block; flex: none; border-radius: 50%; overflow: hidden;
  background: var(--sky); border: 2px solid var(--ink);
  box-shadow: 0 0 0 2px var(--gold);
}
.avatar-frame img { display: block; width: 100%; height: 100%; object-fit: cover; object-position: center 30%; }
.avatar-frame.is-fallback { background: var(--paper); }
.avatar-frame.is-fallback img { object-fit: contain; padding: 3px; }
.avatar-header { width: 34px; height: 34px; margin: 2px; }
.avatar-menu { width: 56px; height: 56px; margin: 2px; }
.avatar-thumb { width: 100%; aspect-ratio: 1; border-width: 1px; box-shadow: none; }

.profile-wrap { position: relative; }
.profile-menu {
  position: absolute; right: 0; top: calc(100% + 8px); z-index: 60;
  width: min(340px, calc(100vw - 2rem)); max-height: calc(100vh - var(--header-h) - 24px); overflow-y: auto;
  background: var(--surface); border: 2px solid var(--ink); border-radius: var(--radius);
  box-shadow: var(--shadow-lift); padding: var(--sp-3);
}
.profile-menu .pm-head { display: flex; align-items: center; gap: var(--sp-3); padding: var(--sp-1) var(--sp-1) var(--sp-3); }
.pm-who b { display: block; font-size: var(--step-1); }
.pm-who span { font-size: var(--step--1); color: var(--text-muted); }
.pm-local {
  display: flex; align-items: flex-start; gap: var(--sp-2); margin: 0 0 var(--sp-3);
  padding: var(--sp-2) var(--sp-3); background: var(--surface-sunken); border-radius: var(--radius-sm);
  font-size: var(--step--1); color: var(--text); max-width: none;
}
.pm-local svg { flex: none; margin-top: 2px; }
.pm-section { border-top: 1px solid var(--line); padding-top: var(--sp-3); }
.pm-label { font-size: var(--step--1); font-weight: 700; margin: 0 0 var(--sp-2); }
.avatar-grid { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: var(--sp-2); margin-bottom: var(--sp-3); }
.avatar-option {
  display: block; padding: 3px; min-width: 44px; min-height: 44px;
  background: none; border: 2px solid transparent; border-radius: 50%; cursor: pointer;
}
.avatar-option:hover { border-color: var(--line-strong); }
.avatar-option.is-on { border-color: var(--saffron); }
.avatar-option:focus-visible { outline: 3px solid var(--ink); outline-offset: 2px; border-radius: 50%; }
.pm-links { border-top: 1px solid var(--line); padding-top: var(--sp-2); }
.pm-links a {
  display: flex; align-items: center; gap: var(--sp-3); min-height: 44px;
  padding: var(--sp-2) var(--sp-3); border-radius: var(--radius-sm);
  color: var(--text); text-decoration: none; font-size: var(--step-0);
}
.pm-links a:hover { background: var(--surface-sunken); }

/* The national band: three equal, touching stripes under the header. It is
   brand identity, not a measurement — no track, no gaps, no rounded ends, no
   animation, nothing that changes with the learner's progress. A navy
   hairline above and below keeps the white stripe visible on parchment. */
.tricolour-rule {
  display: grid; grid-template-columns: 1fr 1fr 1fr;
  height: calc(var(--flag-height) + 2px);
  border-block: 1px solid var(--flag-edge);
  background: var(--flag-white);
}
.tricolour-rule i { display: block; min-width: 0; }
.tricolour-rule i:nth-child(1) { background: var(--flag-saffron); }
.tricolour-rule i:nth-child(2) { background: var(--flag-white); }
.tricolour-rule i:nth-child(3) { background: var(--flag-green); }

/* ==========================================================================
   Mobile bar — the same four destinations, labels always visible
   ========================================================================== */
.bottom-nav {
  display: none;
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 40;
  background: var(--paper); border-top: 2px solid var(--ink);
  padding-bottom: env(safe-area-inset-bottom, 0);
}
.bottom-nav ul { display: flex; list-style: none; margin: 0; padding: 0; }
.bottom-nav li { flex: 1; }
.bottom-nav a {
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  padding: var(--sp-2) 2px calc(var(--sp-2) + 2px);
  text-decoration: none; color: var(--text-muted);
  font-size: 11px; font-weight: 700; letter-spacing: 0.01em;
  border-top: 3px solid transparent; margin-top: -2px;
  min-height: 52px; justify-content: center;
}
.bottom-nav a svg { width: 22px; height: 22px; }
.bottom-nav a[aria-current="page"] { color: var(--saffron-ink); border-top-color: var(--saffron); }

/* ==========================================================================
   Footer — one quiet line
   ========================================================================== */
.app-footer {
  border-top: 1px solid var(--line);
  color: var(--text-muted); font-size: var(--step--1);
  padding-block: var(--sp-5); position: relative; z-index: 1;
}
.app-footer p { max-width: none; margin: 0; }

/* ==========================================================================
   Progress — one component for every "how far through" in the app.
   A native <progress>: a continuous rounded track, a solid fill, and the
   real value and maximum exposed to assistive technology. Chapter
   boundaries may be marked with ticks; there are never separate boxes.
   ========================================================================== */
.meter { position: relative; width: 100%; }
.meter progress {
  -webkit-appearance: none; appearance: none;
  display: block; width: 100%; height: var(--meter-height);
  border: 1px solid var(--meter-track-edge); border-radius: var(--meter-radius);
  background: var(--meter-track); color: var(--meter-fill);
  overflow: hidden; box-sizing: border-box;
}
.meter progress::-webkit-progress-bar { background: var(--meter-track); border-radius: var(--meter-radius); }
.meter progress::-webkit-progress-value {
  background: var(--meter-fill); border-radius: var(--meter-radius);
  transition: width 320ms ease-out;
}
.meter progress::-moz-progress-bar { background: var(--meter-fill); border-radius: var(--meter-radius); }
.meter.is-quiz progress { color: var(--meter-fill-quiz); }
.meter.is-quiz progress::-webkit-progress-value { background: var(--meter-fill-quiz); }
.meter.is-quiz progress::-moz-progress-bar { background: var(--meter-fill-quiz); }

/* ticks at the boundaries between steps, drawn over the track */
.meter-ticks {
  position: absolute; inset: 0; pointer-events: none; border-radius: var(--meter-radius);
  --n: 1;
  background-image: repeating-linear-gradient(90deg,
    transparent 0 calc(100% / var(--n) - 1px), var(--meter-tick) calc(100% / var(--n) - 1px) calc(100% / var(--n) + 1px));
  background-size: 100% 100%;
  -webkit-mask: linear-gradient(90deg, #000 0 calc(100% - 3px), transparent 0);
          mask: linear-gradient(90deg, #000 0 calc(100% - 3px), transparent 0);
}
.meter[data-steps="2"] .meter-ticks { --n: 2; }
.meter[data-steps="3"] .meter-ticks { --n: 3; }
.meter[data-steps="4"] .meter-ticks { --n: 4; }
.meter[data-steps="5"] .meter-ticks { --n: 5; }
.meter[data-steps="6"] .meter-ticks { --n: 6; }
.meter[data-steps="7"] .meter-ticks { --n: 7; }
.meter[data-steps="8"] .meter-ticks { --n: 8; }
.meter[data-steps="9"] .meter-ticks { --n: 9; }
.meter[data-steps="10"] .meter-ticks { --n: 10; }
.meter[data-steps="11"] .meter-ticks { --n: 11; }
.meter[data-steps="12"] .meter-ticks { --n: 12; }

/* the words above a meter: what it counts on the left, the share on the right */
.meter-legend {
  display: flex; align-items: baseline; justify-content: space-between; flex-wrap: wrap;
  gap: var(--sp-1) var(--sp-3); margin-bottom: var(--sp-2);
}
.meter-legend b { font-weight: 700; font-size: var(--step-0); }
.meter-legend span { color: var(--text-muted); font-size: var(--step--1); }
.meter-legend .is-complete { color: var(--green-ink); font-weight: 700; display: inline-flex; align-items: center; gap: 4px; }

@media (prefers-reduced-motion: reduce) {
  .meter progress::-webkit-progress-value { transition: none; }
}


/* ==========================================================================
   Buttons — the screen gets one saffron control and no more
   ========================================================================== */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--sp-2);
  font-weight: 700; font-size: var(--step-0); line-height: 1.2;
  padding: var(--sp-3) var(--sp-5); min-height: 44px;
  border-radius: var(--radius-sm); border: 2px solid transparent;
  cursor: pointer; text-decoration: none; text-align: center;
  background: var(--surface); color: var(--text); border-color: var(--line-strong);
  transition: transform 120ms ease, background var(--transition), border-color var(--transition);
}
.btn:hover { border-color: var(--ink); color: var(--text); }
.btn:active { transform: translateY(1px); }
.btn[disabled], .btn[aria-disabled="true"] { opacity: 0.5; cursor: not-allowed; }

/* the one primary action */
.btn-primary {
  background: var(--saffron); border-color: var(--saffron);
  color: var(--text-on-saffron);
}
.btn-primary:hover { background: #F07F00; border-color: #F07F00; color: var(--text-on-saffron); }

.btn-ghost { background: transparent; border-color: var(--line-strong); }
.btn-quiet { background: transparent; border-color: transparent; text-decoration: underline; padding-inline: var(--sp-2); }
.btn-sm { padding: var(--sp-2) var(--sp-3); min-height: 44px; font-size: var(--step--1); }
.btn-block { width: 100%; }
.btn-done { background: var(--green); border-color: var(--green); color: var(--text-on-green); }

/* ==========================================================================
   Sheets and sections — white paper on the warm page, ruled not boxed
   ========================================================================== */
.sheet {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); padding: var(--sp-5);
  box-shadow: var(--shadow-sheet);
}
.sheet-flat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: var(--sp-5); }

.section { margin-bottom: var(--sp-7); }
.section-head {
  display: flex; align-items: baseline; justify-content: space-between;
  gap: var(--sp-4); margin-bottom: var(--sp-4);
  padding-bottom: var(--sp-2); border-bottom: 2px solid var(--ink);
}
.section-head h2, .section-head h3 { margin: 0; }
.section-head .meta { color: var(--text-muted); font-size: var(--step--1); white-space: nowrap; }

.rule-soft { height: 1px; background: var(--line); margin: var(--sp-4) 0; }

.lede { font-size: var(--step-1); color: var(--text-muted); max-width: 58ch; }
.hint { font-size: var(--step--1); color: var(--text-muted); }

/* ==========================================================================
   Stamps — struck marks for anything earned. The one cut-corner shape.
   ========================================================================== */
.stamp {
  --cut: 7px;
  display: inline-flex; align-items: center; gap: var(--sp-2);
  padding: var(--sp-1) var(--sp-3); font-size: var(--step--1); font-weight: 700;
  border: 2px solid currentColor; clip-path: var(--clip-stamp);
  background: var(--surface); max-width: 100%;
}
/* a stamp carrying a sentence is a notice, not a chip: let it wrap */
p.stamp { align-items: flex-start; padding: var(--sp-2) var(--sp-3); }
.stamp-done { color: var(--green-ink); background: var(--wash-green); }
.stamp-xp { color: var(--gold-ink); background: var(--wash-gold); }
.stamp-live { color: var(--teal-ink); background: var(--wash-teal); }
.stamp-warn { color: var(--saffron-ink); background: var(--wash-saffron); }
.stamp-error { color: var(--red-ink); background: var(--wash-red); }

@keyframes stamp-press {
  0% { transform: scale(1.18); opacity: 0; }
  60% { transform: scale(0.97); opacity: 1; }
  100% { transform: scale(1); opacity: 1; }
}
.stamp-press { animation: stamp-press 240ms cubic-bezier(0.3, 1.4, 0.5, 1) both; }

/* ==========================================================================
   Toast — announced and shown. Hidden means hidden: the empty outlined box
   that used to sit at the bottom of every page came from a display rule
   overriding the hidden attribute.
   ========================================================================== */
.toast {
  position: fixed; left: 50%; bottom: var(--sp-6); transform: translateX(-50%);
  z-index: 70; display: flex; align-items: center; gap: var(--sp-2);
  background: var(--ink); color: var(--text-on-ink);
  padding: var(--sp-3) var(--sp-5); border-radius: var(--radius);
  box-shadow: var(--shadow-lift); font-weight: 600;
  width: max-content; max-width: min(92vw, 460px);
}
.toast[hidden] { display: none !important; }
.toast.is-xp { color: var(--gold); }
.toast.is-badge { color: var(--gold); }

/* ==========================================================================
   Modal
   ========================================================================== */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(20, 42, 67, 0.55);
  display: grid; place-items: center; padding: var(--sp-4); z-index: 80;
}
.modal {
  background: var(--surface); border: 2px solid var(--ink);
  border-radius: var(--radius); padding: var(--sp-6);
  max-width: 560px; width: 100%; max-height: 86vh; overflow-y: auto;
  box-shadow: var(--shadow-lift);
}
.modal h2 { margin-top: 0; }
.modal-actions { display: flex; gap: var(--sp-3); justify-content: flex-end; margin-top: var(--sp-5); flex-wrap: wrap; }

/* ==========================================================================
   Forms
   ========================================================================== */
label { font-weight: 600; font-size: var(--step--1); }
input[type="text"], input[type="search"], select, textarea {
  background: var(--surface); border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm); padding: var(--sp-2) var(--sp-3);
  min-height: 42px; width: 100%;
}
input:focus-visible, select:focus-visible { border-color: var(--ink); }

/* ==========================================================================
   Motion: everything above answers a tap. Nothing moves on its own.
   ========================================================================== */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
}

/* XP rising once toward its new total */
.hud-xp.is-rising { color: var(--gold-ink); }
.hud-xp.is-rising svg { animation: xp-glint 650ms ease-out; }
@keyframes xp-glint { 0% { transform: scale(1) rotate(0); } 50% { transform: scale(1.35) rotate(40deg); } 100% { transform: scale(1) rotate(90deg); } }

/* a new badge opens briefly rather than simply appearing */
@keyframes badge-reveal {
  0% { transform: translate(-50%, 16px) scale(0.86); opacity: 0; }
  60% { transform: translate(-50%, -3px) scale(1.03); opacity: 1; }
  100% { transform: translate(-50%, 0) scale(1); opacity: 1; }
}
.toast.is-reveal { animation: badge-reveal 360ms cubic-bezier(0.3, 1.3, 0.5, 1) both; border: 2px solid var(--gold); }
