/* App shell: reset, layout (phone: tab bar; desktop >= 900px: top bar), screen transitions. */
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { min-height: 100%; margin: 0; }
/* The canvas under Safari's floating toolbar and the overscroll area take the theme too. */
html { background: var(--bg); }
:root { padding-block: env(safe-area-inset-top, 0px) env(safe-area-inset-bottom, 0px); }
body {
  background: var(--bg); color: var(--fg);
  font: var(--text-body)/var(--leading-body) var(--font-ui); letter-spacing: var(--track-body); -webkit-font-smoothing: antialiased;
}
body::before { content: ""; position: fixed; inset: 0 0 auto; height: env(safe-area-inset-top, 0px); z-index: 30; background: var(--blur-bg); }
button, input, textarea { font: inherit; color: inherit; }
/* iOS Safari: two quick taps on a control (the daily-limit "+", card arrows) must not zoom
   the page. Pinch zoom still works. */
a, button, input, select, textarea, label, summary, [role="button"] { touch-action: manipulation; }
button { background: none; border: 0; cursor: pointer; padding: 0; }
/* Every button answers a press: it shrinks a little on touch-down and eases back on release.
   Zero specificity on purpose: a component's own transform or press style (rows, tabs, the
   back link and text buttons dim instead) always wins. */
:where(button, [role="button"], input[type="submit"], input[type="button"], a.btn):where(:not(.row, .tab, .back)) {
  transition: transform 0.2s var(--ease-out);
}
:where(button, [role="button"], input[type="submit"], input[type="button"], a.btn):where(:not(.row, .tab, .back, :disabled, [aria-disabled="true"])):active {
  transform: scale(var(--press-scale)); transition-duration: var(--press-in);
}
a { color: var(--accent); }
h1, h2, h3, p { margin: 0; }
h1, h2, h3, h4 { font-family: var(--font-head); }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; border-radius: 8px; }
[hidden] { display: none !important; }
/* Text for screen readers only. */
.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; }
/* The router focuses the screen title for screen readers; no ring for that. */
h1[tabindex="-1"]:focus { outline: none; }

/* overflow-x: clip — a screen dragged by the back swipe must not widen the page (clip, unlike
   hidden, keeps the sticky nav bar working). */
.shell { min-height: 100%; overflow-x: clip; }
.main.swiping { position: relative; z-index: 1; min-height: 100dvh; background: var(--bg); box-shadow: -10px 0 28px rgba(0, 0, 0, 0.18); will-change: transform; }
/* The previous screen under a back swipe (swipeback.js): pinned where .main is, not scrollable. */
.main.swipe-under { position: fixed; top: 0; bottom: 0; margin: 0; max-width: none; overflow: hidden; pointer-events: none; will-change: transform; }
.main {
  max-width: 560px; margin: 0 auto;
  padding-left: max(var(--space-4), env(safe-area-inset-left, 0px));
  padding-right: max(var(--space-4), env(safe-area-inset-right, 0px));
  padding-block: 0 calc(var(--tabbar-h) + env(safe-area-inset-bottom, 0px) + var(--space-8));
}
.shell.bare .main { padding-bottom: var(--space-8); }
/* While offline the notice floats above the tab bar: room for it under the last button. */
body:has(#offline-bar:not([hidden])) .shell:not(.bare) .main {
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom, 0px) + 72px);
}
.view { display: grid; gap: var(--space-6); align-content: start; }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20; view-transition-name: tabbar;
  display: grid; grid-template-columns: repeat(3, minmax(0, 1fr));
  padding: var(--space-2) max(var(--space-4), env(safe-area-inset-right, 0px)) calc(var(--space-2) + env(safe-area-inset-bottom, 0px)) max(var(--space-4), env(safe-area-inset-left, 0px));
  background: var(--blur-bg); -webkit-backdrop-filter: saturate(180%) blur(20px); backdrop-filter: saturate(180%) blur(20px);
  border-top: 1px solid var(--line);
}
.tab { display: grid; justify-items: center; gap: 2px; min-height: 44px; font-size: var(--text-caption2); font-weight: 500; color: var(--muted); text-decoration: none; }
.tab svg { width: 26px; height: 26px; }
/* Large system text: a label wider than its third of the bar ends with "…", not under the edge. */
.tab span { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tab[aria-current="page"] { color: var(--primary); font-weight: 600; }
.tab { letter-spacing: var(--small-track); transition: opacity 0.2s; }
.tab:active { opacity: var(--press-dim); transition-duration: var(--press-in); }
.topbar { display: none; }
.shell.bare .tabbar { display: none; }
/* Focus and scrollIntoView stop above the tab bar and the offline notice, not under them. */
:root:has(.shell:not(.bare)) {
  scroll-padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom, 0px) + 56px);
}

@media (min-width: 1280px) {
  .main { padding-left: var(--space-16); padding-right: var(--space-16); }
}

@media (min-width: 900px) {
  :root:has(.shell:not(.bare)) {
    scroll-padding-top: calc(var(--topbar-h) + env(safe-area-inset-top, 0px));
    scroll-padding-bottom: 0;
  }
  .tabbar { display: none; }
  .topbar {
    display: grid; grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
    align-items: center; gap: var(--space-4); height: var(--topbar-h); view-transition-name: sidebar;
    position: sticky; top: 0; z-index: 20;
    padding-inline: max(var(--space-4), env(safe-area-inset-left, 0px)) max(var(--space-4), env(safe-area-inset-right, 0px));
    border-bottom: 1px solid var(--line); background: var(--surface);
  }
  .shell.bare .topbar { display: none; }
  .topbar-nav { display: flex; gap: var(--space-2); }
  .topbar-theme { justify-self: end; }
  .topbar .brand, .topbar-link {
    display: flex; align-items: center; gap: var(--space-2); min-height: 44px; padding-inline: var(--space-3);
    border-radius: var(--radius-group); corner-shape: var(--corner); color: var(--fg); text-decoration: none;
  }
  .topbar .brand { justify-self: start; font-size: var(--text-title3); }
  .topbar-link { font-size: var(--text-subhead); font-weight: 500; }
  .topbar-link svg { width: 22px; height: 22px; color: var(--muted); }
  .topbar .brand:hover, .topbar-link:hover { background: var(--fill); }
  .topbar-link[aria-current="page"] { background: var(--primary-soft); color: var(--primary); }
  .topbar-link[aria-current="page"] svg { color: currentColor; }
  ::view-transition-group(sidebar), ::view-transition-new(sidebar) { animation: none; }
  ::view-transition-old(sidebar) { display: none; }
  .shell:not(.bare) .nav { top: calc(var(--topbar-h) + env(safe-area-inset-top, 0px)); }
  .shell:not(.bare) .exam-status {
    top: calc(var(--topbar-h) + 52px + env(safe-area-inset-top, 0px));
  }
  /* Center the full-width content beneath the navigation. */
  .main { width: 100%; max-width: 1160px; padding-left: var(--space-12); padding-right: var(--space-12); padding-bottom: var(--space-12); }
  .shell.bare .main { max-width: 720px; } /* the study session: a 624px landscape card */
}

.offline-bar {
  position: fixed; z-index: 25; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom, 0px) + 12px);
  padding: var(--space-2) var(--space-4); border-radius: 999px; background: var(--fg); color: var(--bg);
  font-size: var(--text-footnote); font-weight: 500; box-shadow: var(--shadow);
}

/* Screen transitions (View Transitions API): push / pop / modal / fade. */
::view-transition-old(root), ::view-transition-new(root) { animation-duration: 0.42s; animation-timing-function: var(--ease-out); }
html.vt-push::view-transition-new(root) { animation-name: vt-in-right; }
html.vt-push::view-transition-old(root) { animation-name: vt-out-left; }
html.vt-pop::view-transition-new(root) { animation-name: vt-in-left; }
html.vt-pop::view-transition-old(root) { animation-name: vt-out-right; z-index: 2; }
html.vt-up::view-transition-new(root) { animation-name: vt-in-up; z-index: 2; }
html.vt-up::view-transition-old(root) { animation-name: vt-dim; }
html.vt-down::view-transition-old(root) { animation-name: vt-out-down; z-index: 2; }
html.vt-down::view-transition-new(root) { animation-name: vt-undim; }
html.vt-fade::view-transition-old(root) { animation: vt-fade-out 0.18s ease-out both; }
html.vt-fade::view-transition-new(root) { animation: vt-fade-in 0.24s ease-out both; }
@keyframes vt-in-right { from { transform: translateX(100%); } }
@keyframes vt-out-left { to { transform: translateX(-28%); opacity: 0.5; } }
@keyframes vt-in-left { from { transform: translateX(-28%); opacity: 0.5; } }
@keyframes vt-out-right { to { transform: translateX(100%); } }
@keyframes vt-in-up { from { transform: translateY(100%); } }
@keyframes vt-dim { to { transform: scale(0.95); opacity: 0.4; } }
@keyframes vt-out-down { to { transform: translateY(100%); } }
@keyframes vt-undim { from { transform: scale(0.95); opacity: 0.4; } }
@keyframes vt-fade-out { to { opacity: 0; } }
@keyframes vt-fade-in { from { opacity: 0; transform: translateY(6px); } }
@media (prefers-reduced-motion: reduce) {
  *, ::view-transition-old(root), ::view-transition-new(root) { animation: none !important; transition: none !important; }
  /* A press still answers, but by dimming, not by moving. */
  .btn:active, .answer:active { transform: none; opacity: 0.7; }
  :where(button, [role="button"], input[type="submit"], input[type="button"], a.btn):where(:not(.row, .tab, .back)):active { transform: none; opacity: 0.7; }
}
/* "Reduce transparency" in the system settings: solid bars instead of frosted glass. */
@media (prefers-reduced-transparency: reduce) {
  body::before, .tabbar, .topbar, .nav { background: var(--bg); -webkit-backdrop-filter: none; backdrop-filter: none; }
}

.exam-view, .exam-content, .exam-footer {
  display: grid;
  gap: var(--space-4);
  min-width: 0;
}
.exam-status {
  position: sticky;
  top: calc(52px + env(safe-area-inset-top, 0px));
  z-index: 2;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: var(--space-1) var(--space-4);
  padding: var(--space-3) var(--space-4);
  border: 1px solid var(--line);
  border-radius: var(--radius-group); corner-shape: var(--corner);
  background: var(--surface);
  scroll-margin-top: calc(52px + env(safe-area-inset-top, 0px));
}
.exam-time time, .exam-total { font-weight: 650; font-variant-numeric: tabular-nums; }
.exam-pager, .exam-actions { display: flex; flex-wrap: wrap; gap: var(--space-2); }
.exam-confirm { flex-basis: 100%; margin: 0 0 var(--space-1); font-weight: 600; }
.exam-pager .btn { min-width: 44px; }
.exam-question {
  display: grid;
  gap: var(--space-3);
  min-width: 0;
  background: var(--surface);
  border-radius: var(--radius-card); corner-shape: var(--corner);
  padding: clamp(var(--space-4), 3vw, var(--space-6));
}
.exam-question .exercise {
  background: none;
  box-shadow: none;
  padding: 0;
}
.exam-question .exercise legend { font-size: clamp(var(--text-body), 2vw, var(--text-title3)); }
.exam-number { color: var(--muted); font-variant-numeric: tabular-nums; }
.exam-instruction, .exam-feedback { white-space: pre-line; overflow-wrap: anywhere; }
.exam-feedback {
  display: grid;
  gap: var(--space-2);
  padding: var(--space-3);
  border-radius: var(--radius-group); corner-shape: var(--corner);
  background: var(--fill);
  font-size: var(--text-subhead);
}
.exam-muted { color: var(--muted); }
/* Only the short verdict is coloured; the explanation stays in the plain text colour. */
.exam-feedback-score { display: flex; align-items: center; gap: var(--space-2); margin: 0; font-weight: 600; }
.exam-feedback-score svg { width: 18px; height: 18px; flex: none; }
.exam-feedback-text { color: var(--fg); font-weight: 400; }
.exam-feedback-label { display: block; }
.exam-score-right { color: var(--know); }
.exam-score-wrong { color: var(--again); }
.exam-summary {
  display: grid;
  gap: var(--space-1);
  background: var(--surface);
  border-radius: var(--radius-card); corner-shape: var(--corner);
  padding: var(--space-6);
}
.exam-summary h2 { scroll-margin-top: calc(118px + env(safe-area-inset-top, 0px)); }
.exam-total { font-size: var(--text-title2); }
.exam-stats { flex-wrap: wrap; gap: var(--space-3) var(--space-6); margin: var(--space-2) 0 var(--space-1); }
.exam-stats div { flex: 1 1 130px; }
.exam-stats b { font-size: clamp(var(--text-title1), 6vw, var(--text-large-title)); }
.exam-summary .exam-total { font-size: var(--text-body); }
.exam-answered { color: var(--muted); font-size: var(--text-subhead); }
/* "Перевірити" and "Не пам'ятаю" share the row; on a narrow phone they stack. */
.exercise-actions { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: var(--space-2); }
.exam-error { color: var(--again); }
.exam-error:empty { display: none; }
