/* =============================================================================
   BASE — document, layout shell, typography.
   ============================================================================= */

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

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

/* `hidden` must win over a class that sets display (the read-aloud rating row
   is display:flex and hidden until a grown-up opens the gate). */
[hidden] { display: none !important; }

body {
  margin: 0;
  min-height: 100dvh;
  font-family: var(--font-body);
  font-size: var(--t-md);
  line-height: 1.55;
  color: var(--ink);
  background-color: var(--bg);
  /* A soft dappled light through leaves. Two radial gradients: no image
     request, scales to any viewport, and it is the same shape in both themes
     because it is built from tokens. */
  background-image:
    radial-gradient(circle at 12% 8%, var(--bg-pattern) 0 18%, transparent 18.5%),
    radial-gradient(circle at 88% 22%, var(--bg-pattern) 0 12%, transparent 12.5%),
    radial-gradient(circle at 76% 84%, var(--bg-pattern) 0 22%, transparent 22.5%);
  background-attachment: fixed;
  -webkit-font-smoothing: antialiased;
  overflow-wrap: break-word;
}

h1, h2, h3 {
  font-family: var(--font-display);
  line-height: 1.15;
  margin: 0 0 var(--s-3);
  letter-spacing: -0.01em;
}
h1 { font-size: var(--t-2xl); }
h2 { font-size: var(--t-xl); }
h3 { font-size: var(--t-lg); }
p { margin: 0 0 var(--s-3); }

button, input, select { font: inherit; color: inherit; }

/* One visible focus style everywhere, including for a child using a switch
   device or a keyboard. Never removed (spec §40). */
:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 3px;
  border-radius: var(--r-sm);
}

/* --- app shell ------------------------------------------------------------ */

.app {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: var(--s-3);
  padding: var(--s-3) var(--s-4);
  /* Solid, not a blurred glass: backdrop-filter would make this bar the
     containing block of the phone's fixed bottom nav, which lives inside it. */
  background: var(--surface);
  border-bottom: var(--border-w) solid var(--line);
}

.topbar__brand {
  display: flex;
  align-items: center;
  gap: var(--s-2);
  font-family: var(--font-display);
  font-size: var(--t-lg);
  font-weight: 700;
  color: var(--ink);
  text-decoration: none;
}
.topbar__spacer { flex: 1; }

/* On a phone the brand name, two chips and the theme button did not fit, and
   the theme button was pushed off-screen. Keep the acorn, drop the words. */
@media (max-width: 640px) {
  .topbar { gap: var(--s-2); padding: var(--s-2) var(--s-3); }
  .topbar__brand span:not([aria-hidden]) {
    position: absolute; width: 1px; height: 1px; overflow: hidden;
    clip: rect(0 0 0 0); white-space: nowrap;
  }
}

.main {
  flex: 1;
  width: 100%;
  max-width: 1080px;
  margin: 0 auto;
  padding: var(--s-5) var(--s-4) var(--s-8);
}

/* --- the nav: tabs in the top bar when there is room, a bottom bar when not */

.mainnav__list { list-style: none; margin: 0; padding: 0; display: flex; gap: var(--s-1); }

.mainnav__link {
  display: flex;
  align-items: center;
  gap: var(--s-2);
  min-height: 48px;
  padding: var(--s-1) var(--s-3);
  border-radius: var(--r-pill);
  color: var(--ink-soft);
  font-family: var(--font-display);
  font-weight: 700;
  text-decoration: none;
  transition: background-color var(--dur-fast) var(--ease-out);
}
.mainnav__link:hover { background: var(--surface-sunken); color: var(--ink); }
.mainnav__link[aria-current='page'] { background: var(--primary-soft); color: var(--primary); }
.mainnav__icon { font-size: 1.4rem; line-height: 1; }

@media (min-width: 900px) and (max-width: 1150px) {
  /* Icons alone before the tabs crowd the stats off the bar. */
  .mainnav__label { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
}

/* Narrow: five big icons pinned to the bottom, where a thumb already is. */
@media (max-width: 899.98px) {
  .mainnav {
    position: fixed;
    inset: auto 0 0 0;
    z-index: 30;
    padding: var(--s-1) var(--s-2) calc(var(--s-1) + env(safe-area-inset-bottom));
    background: var(--surface);
    border-top: var(--border-w) solid var(--line);
  }
  .mainnav__list { justify-content: space-around; }
  .mainnav__link {
    flex-direction: column;
    gap: 0;
    min-height: var(--tap-min);
    padding: var(--s-1) var(--s-2);
    border-radius: var(--r-md);
    font-size: 0.6875rem;
  }
  .mainnav__icon { font-size: 1.6rem; }
  .app--nav .main { padding-bottom: calc(var(--s-8) + 64px); }
}
@media (max-width: 420px) {
  .mainnav__label { display: none; }
}

/* During an exercise or the level game the nav steps aside entirely. */
.app[data-screen='focus'] .mainnav { display: none; }
.app--nav[data-screen='focus'] .main { padding-bottom: var(--s-8); }

.stack     { display: flex; flex-direction: column; gap: var(--s-4); }
.stack-lg  { display: flex; flex-direction: column; gap: var(--s-6); }
.row       { display: flex; align-items: center; gap: var(--s-3); flex-wrap: wrap; }
.center    { text-align: center; }
.muted     { color: var(--ink-soft); }
.tiny      { font-size: var(--t-sm); }

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

/* --- reduced motion ------------------------------------------------------- */
/* Honoured for the OS setting AND for the per-learner preference, which the app
   mirrors onto <html data-motion="reduced">. */

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

html[data-motion='reduced'] *,
html[data-motion='reduced'] *::before,
html[data-motion='reduced'] *::after {
  animation-duration: 0.01ms !important;
  animation-iteration-count: 1 !important;
  transition-duration: 0.01ms !important;
}

/* --- print ---------------------------------------------------------------- */
/* The real printable worksheet is server-rendered at /print/exercise/:id. This
   is only a safety net for anyone who hits Ctrl+P on the app itself. */

@media print {
  .topbar, .no-print { display: none !important; }
  body { background: #fff; color: #000; }
  .main { max-width: none; padding: 0; }
}
