:root {
  color-scheme: light;
  --background: #f6f5f1;
  --text: #1d1d1b;
  --accent: #2f5d8a;
  --amber: #b7791f;
  --red: #b42318;
  --green: #2f7d4f;
  --surface: #fff;
  --muted: #5b5b54;
  --border: #d9d8d0;
  --header-height: 80px;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 20px;
  line-height: 1.5;
  color: var(--text);
  background: var(--background);
}

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

body {
  margin: 0;
  min-width: 280px;
  min-height: 100dvh;
}

[hidden] {
  display: none !important;
}

::selection {
  color: var(--text);
  background: #dce7f0;
}

#root {
  width: 100%;
  max-width: 900px;
  margin-inline: auto;
}

#app {
  min-height: 100dvh;
}

h1,
h2,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 24px;
  font-size: 1.6rem;
  font-weight: 650;
  line-height: 1.25;
  overflow-wrap: anywhere;
}

h2 {
  margin-bottom: 20px;
  font-size: 1.25rem;
  font-weight: 650;
  line-height: 1.35;
}

p {
  margin-bottom: 24px;
}

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

button,
input,
textarea,
select,
a {
  -webkit-tap-highlight-color: transparent;
}

button {
  min-height: 48px;
  padding: 10px 22px;
  border: 1px solid var(--accent);
  border-radius: 6px;
  color: var(--surface);
  background: var(--accent);
  font-weight: 600;
  line-height: 1.3;
  text-align: center;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 100ms ease-out;
}

button.secondary {
  color: var(--accent);
  background: transparent;
}

button:active:not(:disabled) {
  transform: translateY(1px);
}

button:disabled {
  border-color: var(--border);
  color: var(--muted);
  background: #e8e7e0;
  cursor: default;
}

:where(button, input, textarea, select, a):focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 3px;
}

label {
  display: block;
  margin-bottom: 8px;
  font-weight: 600;
}

input,
textarea,
select {
  display: block;
  width: 100%;
  min-height: 52px;
  padding: 12px 14px;
  border: 1px solid #92928a;
  border-radius: 6px;
  color: var(--text);
  background: var(--surface);
}

textarea {
  min-height: 112px;
  line-height: 1.5;
  resize: vertical;
}

input:disabled,
textarea:disabled,
select:disabled {
  color: var(--muted);
  background: #eeede7;
  cursor: default;
}

.session-header {
  position: sticky;
  z-index: 20;
  top: 0;
  display: flex;
  min-height: var(--header-height);
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--background);
}

#student-id {
  flex-shrink: 0;
  font-weight: 650;
}

.session-counters {
  display: grid;
  grid-template-columns: auto auto;
  gap: 2px 16px;
  align-items: center;
  justify-content: end;
  justify-items: end;
  font-size: 0.8rem;
  font-variant-numeric: tabular-nums;
  line-height: 1.4;
}

#session-clock {
  grid-row: 1;
  grid-column: 1 / -1;
  font-size: 1.05rem;
  font-weight: 650;
  white-space: nowrap;
}

#warning-count {
  color: var(--red);
}

#help-count {
  color: var(--muted);
}

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  align-items: start;
  gap: 28px;
  padding: 24px;
}

#stage,
.help-panel {
  min-width: 0;
}

#stage {
  scroll-margin-top: calc(var(--header-height) + 20px);
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin-top: 28px;
}

.story {
  margin-bottom: 28px;
  font-size: 22px;
  line-height: 1.6;
  overflow-wrap: anywhere;
}

.story h2 {
  line-height: 1.4;
}

.story p {
  margin-bottom: 22px;
}

.story-word {
  display: inline;
  min-height: 0;
  margin: 0;
  padding: 2px 0;
  border: 0;
  border-radius: 2px;
  color: inherit;
  background: transparent;
  font: inherit;
  line-height: inherit;
  text-align: inherit;
  box-decoration-break: clone;
  -webkit-box-decoration-break: clone;
}

.story-word u {
  text-decoration-color: var(--accent);
  text-decoration-thickness: 2px;
  text-underline-offset: 4px;
}

.story-word[aria-expanded="true"] {
  background: #dce7f0;
}

.word-popover {
  position: fixed;
  z-index: 30;
  width: min(340px, calc(100vw - 24px));
  max-height: calc(100dvh - 24px);
  overflow-y: auto;
  padding: 20px;
  border: 1px solid var(--accent);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: 0 8px 24px rgb(29 29 27 / 12%);
  overflow-wrap: anywhere;
}

.word-popover > strong {
  display: block;
  margin-bottom: 8px;
}

.word-popover p {
  margin-bottom: 16px;
}

#story-review {
  margin-top: 24px;
  padding: 20px;
  border-left: 3px solid var(--accent);
  background: #eeede7;
}

#story-review .story {
  margin-bottom: 0;
}

#reading-form {
  margin-top: 28px;
}

.question + .question {
  margin-top: 24px;
}

.question label {
  overflow-wrap: anywhere;
}

#stage:has(#math-form) h1 {
  margin-bottom: 12px;
  font-size: 1.4rem;
}

.sum-prompt {
  margin-bottom: 16px;
  font-size: 48px;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
  line-height: 1.2;
  overflow-wrap: anywhere;
}

#math-form {
  display: grid;
  gap: 8px;
  align-items: start;
}

#math-form label {
  margin-bottom: 0;
}

#math-answer {
  min-height: 60px;
  font-size: 1.4rem;
  font-variant-numeric: tabular-nums;
}

.keypad {
  display: grid;
  width: 100%;
  max-width: 360px;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
  margin-top: 8px;
}

.keypad button {
  min-height: 48px;
  padding: 6px;
  border-color: var(--border);
  color: var(--text);
  background: var(--surface);
  font-size: 1.2rem;
  font-variant-numeric: tabular-nums;
}

.keypad .check-button {
  grid-column: 1 / -1;
  border-color: var(--accent);
  color: var(--surface);
  background: var(--accent);
  font-size: 1rem;
}

.keypad button:disabled {
  border-color: var(--border);
  color: var(--muted);
  background: #e8e7e0;
}

.math-feedback {
  min-height: 1.5em;
  margin: 8px 0;
  font-weight: 600;
  overflow-wrap: anywhere;
}

.math-feedback.error,
.math-feedback.wrong {
  color: var(--red);
}

.math-feedback.correct,
.math-feedback.success {
  color: var(--green);
}

.tempo-track {
  height: 10px;
  overflow: hidden;
  border-radius: 5px;
  background: #dfded6;
}

.tempo-fill {
  width: 100%;
  height: 100%;
  border-radius: inherit;
  background: var(--accent);
}

.tempo-track.warning .tempo-fill,
.tempo-fill.warning {
  background: var(--amber);
}

.tempo-label {
  min-height: 1.5em;
  margin: 6px 0 12px;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--amber);
}

.banner {
  padding: 16px 24px;
  border-bottom: 1px solid var(--border);
  border-left: 4px solid var(--accent);
  color: var(--text);
  background: #e8eef3;
  font-weight: 600;
  overflow-wrap: anywhere;
}

.banner.error {
  border-color: var(--red);
  color: var(--red);
  background: #faeeeb;
}

.help-panel {
  padding: 20px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
}

.help-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.help-heading h2 {
  margin-bottom: 0;
}

.help-heading button {
  padding-inline: 12px;
  font-size: 0.9rem;
}

.help-panel > p {
  margin-bottom: 16px;
  color: var(--muted);
  font-size: 0.9rem;
}

.help-history {
  max-height: 280px;
  margin-block: 16px;
  overflow-y: auto;
  overscroll-behavior-y: contain;
  scrollbar-gutter: stable;
  overflow-wrap: anywhere;
}

.help-history:empty {
  display: none;
}

.help-message {
  margin-bottom: 12px;
  padding: 12px;
  border-left: 3px solid var(--border);
  background: var(--background);
  font-size: 0.9rem;
  line-height: 1.5;
  white-space: pre-wrap;
}

.help-message.assistant {
  border-left-color: var(--accent);
  background: #edf2f6;
}

#help-form button {
  width: 100%;
  margin-top: 12px;
}

#help-text {
  min-height: 112px;
}

.skip-link {
  position: fixed;
  z-index: 40;
  top: 8px;
  left: 12px;
  padding: 12px 16px;
  border-radius: 6px;
  color: var(--surface);
  background: var(--accent);
  transform: translateY(-160%);
}

.skip-link:focus {
  transform: translateY(0);
}

.no-script {
  margin: 24px;
  padding: 20px;
  border-left: 4px solid var(--red);
  background: var(--surface);
}

@media (min-width: 760px) {
  #math-form {
    grid-template-columns: minmax(0, 1fr) 300px;
    grid-template-rows: auto auto 1fr;
    column-gap: 32px;
  }

  #math-form label {
    grid-row: 1;
    grid-column: 1;
  }

  #math-answer {
    grid-row: 2;
    grid-column: 1;
  }

  #math-form .keypad {
    grid-row: 1 / span 3;
    grid-column: 2;
    margin-top: 0;
  }
}

@media (min-width: 900px) {
  .workspace:has(> .help-panel:not([hidden])) {
    grid-template-columns: minmax(0, 1fr) 300px;
    gap: 24px;
  }

  .help-panel {
    position: sticky;
    top: calc(var(--header-height) + 20px);
    max-height: calc(100dvh - var(--header-height) - 40px);
    overflow-y: auto;
  }

  .workspace:has(> .help-panel:not([hidden])) #math-form {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto;
  }

  .workspace:has(> .help-panel:not([hidden])) #math-form .keypad {
    grid-row: 3;
    grid-column: 1;
    margin-top: 8px;
  }
}

@media (max-width: 599px) {
  :root {
    --header-height: 92px;
  }

  .session-header {
    align-items: flex-start;
    padding: 14px 16px;
  }

  .session-counters {
    grid-template-columns: auto;
    gap: 2px;
    font-size: 0.7rem;
  }

  #session-clock {
    font-size: 0.9rem;
  }

  .workspace {
    padding: 24px 16px;
  }

  h1 {
    font-size: 1.4rem;
  }

  .actions {
    flex-direction: column;
    align-items: stretch;
  }

  .help-toggle {
    width: 100%;
  }

  .banner {
    padding: 14px 16px;
  }

  #story-review,
  .help-panel {
    padding: 16px;
  }
}

@media (hover: hover) {
  button:hover:not(:disabled) {
    background: #264c72;
  }

  button.secondary:hover:not(:disabled),
  .keypad button:not(.check-button):hover:not(:disabled),
  button.story-word:hover:not(:disabled) {
    background: #e6edf3;
  }
}

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