:root {
  color-scheme: light dark;
  --radius: 0.625rem;
  --radius-sm: calc(var(--radius) * 0.6);
  --radius-md: calc(var(--radius) * 0.8);
  --radius-lg: var(--radius);
  --radius-xl: calc(var(--radius) * 1.4);
  --background: oklch(1 0 0);
  --foreground: oklch(0.145 0 0);
  --card: oklch(1 0 0);
  --card-foreground: oklch(0.145 0 0);
  --popover: oklch(1 0 0);
  --popover-foreground: oklch(0.145 0 0);
  --primary: oklch(0.205 0 0);
  --primary-foreground: oklch(0.985 0 0);
  --secondary: oklch(0.97 0 0);
  --secondary-foreground: oklch(0.205 0 0);
  --muted: oklch(0.97 0 0);
  --muted-foreground: oklch(0.556 0 0);
  --accent: oklch(0.97 0 0);
  --accent-foreground: oklch(0.205 0 0);
  --destructive: oklch(0.577 0.245 27.325);
  --destructive-foreground: oklch(0.985 0 0);
  --border: oklch(0.922 0 0);
  --input: oklch(0.922 0 0);
  --ring: oklch(0.708 0 0);
  --chart-1: oklch(0.646 0.222 41.116);
  --chart-2: oklch(0.6 0.118 184.704);
  --chart-3: oklch(0.398 0.07 227.392);
  --chart-4: oklch(0.828 0.189 84.429);
  --chart-5: oklch(0.769 0.188 70.08);
  --screen: oklch(0.97 0 0);
  --screen-foreground: oklch(0.205 0 0);
  --screen-muted: oklch(0.556 0 0);
  --screen-track: oklch(0.205 0 0 / 0.12);
  --shadow-sm: 0 1px 2px 0 oklch(0 0 0 / 0.05);
  --shadow-md: 0 4px 6px -1px oklch(0 0 0 / 0.1),
    0 2px 4px -2px oklch(0 0 0 / 0.1);
  --font-sans: Inter, ui-sans-serif, system-ui, -apple-system,
    BlinkMacSystemFont, "Segoe UI", sans-serif;
  --font-mono: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --background: oklch(0.145 0 0);
    --foreground: oklch(0.985 0 0);
    --card: oklch(0.205 0 0);
    --card-foreground: oklch(0.985 0 0);
    --popover: oklch(0.205 0 0);
    --popover-foreground: oklch(0.985 0 0);
    --primary: oklch(0.922 0 0);
    --primary-foreground: oklch(0.205 0 0);
    --secondary: oklch(0.269 0 0);
    --secondary-foreground: oklch(0.985 0 0);
    --muted: oklch(0.269 0 0);
    --muted-foreground: oklch(0.708 0 0);
    --accent: oklch(0.269 0 0);
    --accent-foreground: oklch(0.985 0 0);
    --destructive: oklch(0.704 0.191 22.216);
    --destructive-foreground: oklch(0.985 0 0);
    --border: oklch(1 0 0 / 10%);
    --input: oklch(1 0 0 / 15%);
    --ring: oklch(0.556 0 0);
    --screen: oklch(0.12 0 0);
    --screen-foreground: oklch(0.985 0 0);
    --screen-muted: oklch(0.708 0 0);
    --screen-track: oklch(1 0 0 / 0.12);
    --shadow-sm: 0 1px 2px 0 oklch(0 0 0 / 0.25);
    --shadow-md: 0 10px 30px -12px oklch(0 0 0 / 0.75);
  }
}

* {
  box-sizing: border-box;
  border-color: var(--border);
}

html,
body {
  margin: 0;
  width: 100%;
  height: 100%;
}

body {
  min-height: 100dvh;
  overflow: hidden;
  background: var(--background);
  color: var(--foreground);
  font-family: var(--font-sans);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
}

button {
  font: inherit;
}

button:focus-visible {
  outline: 2px solid var(--ring);
  outline-offset: 2px;
}

.shell {
  width: 100%;
  max-width: 840px;
  height: 100dvh;
  margin: 0 auto;
  padding:
    calc(12px + env(safe-area-inset-top))
    calc(12px + env(safe-area-inset-right))
    calc(12px + env(safe-area-inset-bottom))
    calc(12px + env(safe-area-inset-left));
  display: grid;
  grid-template-rows: minmax(0, 1fr);
}

.trainer-console,
.stats-page {
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  background: var(--card);
  color: var(--card-foreground);
  box-shadow: var(--shadow-md);
}

.resource-links {
  display: flex;
  align-items: center;
  gap: 6px;
}

.icon-link {
  width: 36px;
  height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--background);
  color: var(--foreground);
  box-shadow: var(--shadow-xs);
  text-decoration: none;
  transition:
    transform 150ms ease,
    background-color 150ms ease,
    border-color 150ms ease,
    color 150ms ease;
}

.icon-link svg {
  width: 17px;
  height: 17px;
  fill: currentColor;
}

.icon-link:hover {
  transform: translateY(-1px);
  border-color: var(--ring);
  background: var(--accent);
}

.icon-link:active {
  transform: translateY(0) scale(0.985);
}

.icon-link:focus-visible {
  outline: 2px solid var(--ring);
  outline-offset: 2px;
}

h2,
h3,
p {
  margin: 0;
}

h2 {
  font-size: clamp(1.35rem, 4vw, 1.8rem);
  line-height: 1;
  font-weight: 700;
}

h3 {
  font-size: 0.95rem;
  line-height: 1;
  font-weight: 600;
}

.view {
  display: none;
  min-width: 0;
  min-height: 0;
}

.view.active {
  display: block;
  height: 100%;
  animation: panel-in 180ms ease-out;
}

.trainer-console {
  height: 100%;
  min-width: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 10px;
  padding: 12px;
  overflow: hidden;
  animation: enter-up 220ms ease-out 40ms both;
}

.console-top {
  min-width: 0;
  display: grid;
  grid-template-columns: minmax(92px, auto) minmax(0, 1fr) minmax(92px, auto);
  gap: 10px;
}

.level-stepper {
  min-width: 0;
  display: grid;
  grid-template-columns: 48px minmax(0, 1fr) 48px;
  gap: 0;
}

.level-readout {
  min-width: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 8px 12px;
  border: 1px solid var(--input);
  border-right: 0;
  border-left: 0;
  border-radius: var(--radius-lg);
  background: var(--background);
  text-align: center;
  transition:
    border-color 150ms ease,
    box-shadow 150ms ease,
    transform 150ms ease;
}

.level-readout span,
.finish-summary dt,
.stats-grid dt,
.level-cell span,
.history-item span {
  color: var(--muted-foreground);
  font-family: var(--font-mono);
  font-size: 0.64rem;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.level-readout strong {
  margin-top: 3px;
  color: var(--foreground);
  font-size: clamp(1.05rem, 3.3vw, 1.28rem);
  line-height: 1;
  font-weight: 700;
}

.level-button,
.primary,
.secondary,
.danger,
.answer-clear,
.key,
.level-cell {
  min-height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--input);
  border-radius: var(--radius-lg);
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  transition:
    background-color 150ms ease,
    color 150ms ease,
    border-color 150ms ease,
    transform 120ms ease,
    opacity 120ms ease;
}

.level-button,
.secondary,
.answer-clear,
.key,
.level-cell {
  background: var(--background);
  color: var(--foreground);
}

.level-button {
  min-height: 50px;
  font-size: 1.35rem;
  font-weight: 700;
}

.level-stepper .level-button:first-child {
  border-top-right-radius: 0;
  border-bottom-right-radius: 0;
}

.level-stepper .level-button:last-child {
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
}

.level-stepper .level-readout {
  border-radius: 0;
}

.primary,
.secondary,
.danger {
  min-width: 0;
  min-height: 50px;
  padding: 0 12px;
}

.primary {
  background: var(--primary);
  color: var(--primary-foreground);
  border-color: var(--primary);
}

.secondary:hover:not(:disabled),
.answer-clear:hover:not(:disabled),
.key:hover:not(:disabled),
.level-button:hover:not(:disabled),
.level-cell:hover:not(:disabled) {
  background: var(--accent);
  color: var(--accent-foreground);
}

.primary:hover:not(:disabled) {
  opacity: 0.9;
}

.danger {
  background: transparent;
  color: var(--destructive);
  border-color: var(--destructive);
}

.danger:hover:not(:disabled) {
  background: var(--destructive);
  color: var(--destructive-foreground);
}

.level-button:hover:not(:disabled),
.primary:hover:not(:disabled),
.secondary:hover:not(:disabled),
.danger:hover:not(:disabled),
.answer-clear:hover:not(:disabled),
.key:hover:not(:disabled),
.level-cell:hover:not(:disabled) {
  transform: translateY(-1px);
}

.level-button:active:not(:disabled),
.primary:active:not(:disabled),
.secondary:active:not(:disabled),
.danger:active:not(:disabled),
.answer-clear:active:not(:disabled),
.key:active:not(:disabled),
.level-cell:active:not(:disabled) {
  transform: translateY(0) scale(0.985);
}

.level-button:disabled,
.primary:disabled,
.secondary:disabled,
.danger:disabled,
.answer-clear:disabled,
.key:disabled,
.level-cell:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.screen-card {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 8px;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  background: var(--screen);
  color: var(--screen-foreground);
  box-shadow: inset 0 -20px 80px -20px oklch(0 0 0 / 0.1);
  overflow: hidden;
}

.screen-chrome {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
}

.phase-badge,
.round-badge,
.result-badge {
  min-width: 64px;
  min-height: 28px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 9px;
  border-radius: 999px;
  background: var(--background);
  color: var(--screen-muted);
  font-family: var(--font-mono);
  font-size: 0.66rem;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.result-badge {
  justify-self: end;
  color: transparent;
}

.result-badge:empty {
  visibility: hidden;
}

.phase-badge {
  justify-self: start;
}

.round-badge {
  justify-self: center;
  min-width: 74px;
}

.result-badge.correct {
  background: oklch(0.6 0.118 184.704 / 0.22);
  color: oklch(0.9 0.09 170);
}

.result-badge.wrong {
  background: oklch(0.704 0.191 22.216 / 0.22);
  color: oklch(0.9 0.1 22);
}

.screen {
  position: relative;
  min-width: 0;
  min-height: 150px;
  display: grid;
  place-items: center;
  padding: 22px 16px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--background);
  overflow: hidden;
}

.screen::after {
  display: none;
}

.timer-track {
  position: absolute;
  left: 12px;
  right: 12px;
  top: 10px;
  height: 5px;
  border-radius: 999px;
  background: var(--screen-track);
  overflow: hidden;
}

.timer-fill {
  width: 100%;
  height: 100%;
  border-radius: inherit;
  background: var(--screen-foreground);
  transform-origin: left center;
}

.problem-display {
  max-width: 100%;
  font-size: clamp(2.2rem, 9vw, 4.5rem);
  line-height: 0.95;
  font-weight: 700;
  text-align: center;
}

body:not(.playing) .problem-display {
  font-size: clamp(1.85rem, 8vw, 3.25rem);
}

.problem-display.flash {
  animation: pulse 180ms ease;
}

.finish-summary,
.stats-grid {
  display: grid;
  gap: 8px;
  margin: 0;
}

.finish-summary {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.finish-summary[hidden] {
  display: none;
}

.finish-summary div {
  min-width: 0;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--card);
  box-shadow: var(--shadow-sm);
}

.finish-summary dd,
.stats-grid dd {
  margin: 4px 0 0;
  font-size: 1rem;
  font-weight: 700;
}

.finish-summary:not([hidden]) {
  animation: panel-in 180ms ease-out;
}

.answer-panel {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 8px;
  min-width: 0;
  min-height: 0;
}

.answer-row {
  min-width: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 70px;
  gap: 8px;
}

.answer-value {
  min-height: 50px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding: 0 14px;
  border: 1px solid var(--input);
  border-radius: var(--radius-lg);
  background: var(--background);
  font-size: 1.55rem;
  font-weight: 700;
  transition:
    border-color 150ms ease,
    box-shadow 150ms ease,
    color 150ms ease;
}

.answer-value:not(.empty) {
  border-color: var(--ring);
  box-shadow: 0 0 0 3px oklch(0.708 0 0 / 0.14);
}

.answer-value.empty {
  color: var(--muted-foreground);
}

.answer-clear {
  min-height: 50px;
}

.keypad {
  min-width: 0;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  min-height: 0;
}

.key {
  min-width: 0;
  min-height: 50px;
  font-size: clamp(1.05rem, 4vw, 1.45rem);
  font-weight: 600;
}

.key.zero {
  grid-column: 2;
}

.stats-page {
  height: 100%;
  padding: 12px;
  overflow: auto;
  animation: enter-up 200ms ease-out both;
}

.stats-head,
.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.stats-head {
  margin-bottom: 12px;
}

.stats-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: 8px;
}

.stats-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 16px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
}

.stats-grid.lifetime {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.stats-grid div {
  min-width: 0;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  background: var(--card);
  color: var(--card-foreground);
  box-shadow: var(--shadow-sm);
}

.stats-section {
  margin-top: 14px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
}

.section-head {
  margin-bottom: 10px;
}

.section-head span {
  color: var(--muted-foreground);
  font-family: var(--font-mono);
  font-size: 0.78rem;
  font-weight: 600;
}

.level-map {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 8px;
}

.level-cell {
  min-height: 66px;
  display: grid;
  gap: 2px;
  align-content: center;
  justify-items: start;
  padding: 9px;
  text-align: left;
  box-shadow: var(--shadow-sm);
}

.level-cell.current {
  border-color: var(--ring);
  background: var(--accent);
  color: var(--accent-foreground);
}

.level-cell.complete {
  border-color: var(--chart-2);
}

.level-cell strong {
  font-size: 1.05rem;
  line-height: 1;
  font-weight: 700;
}

.history-list {
  display: grid;
  gap: 8px;
}

.history-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  background: var(--card);
  color: var(--card-foreground);
  box-shadow: var(--shadow-sm);
}

.history-item strong {
  display: block;
  margin-bottom: 4px;
  font-size: 0.95rem;
}

.history-score {
  min-width: 66px;
  text-align: right;
  font-size: 1.1rem;
  font-weight: 700;
}

.history-score span {
  display: block;
  margin-top: 4px;
}

.empty-state {
  margin: 0;
  padding: 18px;
  border: 1px dashed var(--border);
  border-radius: var(--radius-xl);
  color: var(--muted-foreground);
  background: var(--muted);
  text-align: center;
}

.text-pop {
  animation: text-pop 120ms ease-out;
}

@keyframes panel-in {
  from {
    opacity: 0.96;
    transform: translateY(3px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes enter-down {
  from {
    opacity: 0.96;
    transform: translateY(-3px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes enter-up {
  from {
    opacity: 0.96;
    transform: translateY(3px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes pulse {
  from {
    opacity: 0.55;
    transform: scale(0.985);
  }

  to {
    opacity: 1;
    transform: scale(1);
  }
}

@keyframes text-pop {
  0% {
    opacity: 0.7;
  }

  100% {
    opacity: 1;
  }
}

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

@media (max-width: 720px) {
  .shell {
    padding:
      calc(8px + env(safe-area-inset-top))
      calc(8px + env(safe-area-inset-right))
      calc(8px + env(safe-area-inset-bottom))
      calc(8px + env(safe-area-inset-left));
    gap: 8px;
  }

  .console-top {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
  }

  .console-top .level-stepper {
    grid-column: 1 / -1;
    grid-row: 2;
  }

  .console-top #start-btn {
    grid-column: 1;
    grid-row: 1;
  }

  .console-top #stats-tab {
    grid-column: 2;
    grid-row: 1;
  }

  .stats-head {
    align-items: flex-start;
    flex-direction: column;
  }

  .stats-actions {
    width: 100%;
    justify-content: flex-end;
  }

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

  .stats-footer .resource-links {
    justify-content: flex-start;
  }

  .stats-grid.lifetime {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .level-map {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
}

@media (max-width: 480px) {
  .trainer-console,
  .stats-page {
    padding: 8px;
  }

  .level-stepper {
    grid-template-columns: 46px minmax(0, 1fr) 46px;
  }

  .level-button,
  .primary,
  .secondary {
    min-height: 44px;
  }

  .screen-card {
    padding: 8px;
  }

  .screen {
    min-height: 118px;
  }

  .finish-summary {
    gap: 6px;
  }

  .finish-summary div {
    padding: 7px 6px;
  }

  .finish-summary dt {
    font-size: 0.55rem;
  }

  .finish-summary dd {
    font-size: 0.84rem;
  }

  .answer-row {
    grid-template-columns: minmax(0, 1fr) 64px;
    gap: 6px;
  }

  .answer-value,
  .answer-clear {
    min-height: 44px;
  }

  .key {
    min-height: 42px;
  }

  .level-map {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}
