:root {
  color-scheme: light;
  font-family: "Inter", "Segoe UI", system-ui, sans-serif;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --text-primary: #0f172a;
  --text-secondary: #475569;
  --text-muted: #94a3b8;
  --border: #e2e8f0;
  --surface: rgba(255, 255, 255, 0.9);
  --background: linear-gradient(160deg, #f9fafb 0%, #e2e8f0 40%, #f9fafb 100%);
  --shadow: 0 18px 35px rgba(15, 23, 42, 0.12);
  --success: #16a34a;
  --u: clamp(8px, 1.1vw, 14px);
  --c1: #fbbf24;
  --c1-edge: #b45309;
  --c10: #34d399;
  --c10-edge: #047857;
  --c100: #60a5fa;
  --c100-edge: #1d4ed8;
  --c1000: #a78bfa;
  --c1000-edge: #6d28d9;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0 0 2rem;
  background: var(--background);
  color: var(--text-primary);
}

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

[hidden] {
  display: none !important;
}

.page {
  width: min(1100px, 100%);
  padding: clamp(2rem, 5vw, 4rem) clamp(1rem, 4vw, 2rem) 3rem;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 1.5rem;
}

.page__header {
  text-align: center;
  display: grid;
  gap: 0.75rem;
}

.page__header h1 {
  margin: 0;
  font-size: clamp(2rem, 5vw, 3rem);
}

.page__header p {
  margin: 0;
  color: var(--text-secondary);
}

/* ---------- Modes ---------- */

.modes {
  display: inline-flex;
  justify-self: center;
  gap: 2px;
  padding: 4px;
  border-radius: 999px;
  background: rgba(15, 23, 42, 0.06);
}

.modes button {
  border: none;
  background: none;
  padding: 0.55rem 1.4rem;
  border-radius: 999px;
  font-weight: 600;
  color: var(--text-secondary);
  cursor: pointer;
}

.modes button[aria-selected="true"] {
  background: #fff;
  color: var(--text-primary);
  box-shadow: 0 2px 8px rgba(15, 23, 42, 0.12);
}

.modes button:focus-visible,
.btn:focus-visible,
.tray__item:focus-visible,
.exchange:focus-visible,
.column__blocks .block:focus-visible {
  outline: 3px solid rgba(37, 99, 235, 0.45);
  outline-offset: 2px;
}

/* ---------- Cards ---------- */

.card {
  background: var(--surface);
  border-radius: 24px;
  padding: clamp(1rem, 3vw, 1.75rem);
  box-shadow: var(--shadow);
}

.hint {
  margin: 0.75rem 0 0;
  font-size: 0.9rem;
  text-align: center;
  color: var(--text-secondary);
}

.btn {
  border: 1px solid transparent;
  border-radius: 999px;
  padding: 0.65rem 1.2rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 140ms ease, transform 100ms ease;
}

.btn:active {
  transform: scale(0.97);
}

.btn--primary {
  background: var(--accent);
  color: #fff;
  box-shadow: 0 10px 24px rgba(37, 99, 235, 0.28);
}

.btn--primary:hover {
  background: var(--accent-hover);
}

.btn--ghost {
  background: #fff;
  border-color: var(--border);
  color: var(--text-secondary);
}

.btn--ghost:hover {
  border-color: #cbd5e1;
  color: var(--text-primary);
}

.action-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
}

/* ---------- Tray ---------- */

.tray {
  touch-action: none;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.75rem;
}

.tray__item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  min-height: 64px;
  padding: 0.5rem 1rem 0.5rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: #fff;
  cursor: grab;
  touch-action: none;
  user-select: none;
  transition: border-color 140ms ease, transform 100ms ease;
}

.tray__item:hover {
  border-color: #cbd5e1;
}

.tray__item:active {
  cursor: grabbing;
  transform: scale(0.97);
}

.tray__picture {
  display: grid;
  place-items: center;
  width: 48px;
  height: 48px;
  --u: 4.4px;
}

.tray__label {
  display: grid;
  text-align: left;
  line-height: 1.2;
}

.tray__label span {
  font-size: 0.85rem;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
}

/* ---------- Blocks ---------- */

.block {
  display: block;
  padding: 0;
  border-radius: 2px;
  flex-shrink: 0;
}

button.block {
  cursor: pointer;
}

.block--1 {
  width: var(--u);
  height: var(--u);
  background: var(--c1);
  border: 1px solid var(--c1-edge);
}

.block--10 {
  width: var(--u);
  height: calc(var(--u) * 10);
  background-color: var(--c10);
  background-image: linear-gradient(to bottom, var(--c10-edge) 1px, transparent 1px);
  background-size: 100% var(--u);
  background-position: 0 -1px;
  border: 1px solid var(--c10-edge);
}

.block--100 {
  width: calc(var(--u) * 10);
  height: calc(var(--u) * 10);
  background-color: var(--c100);
  background-image:
    linear-gradient(to bottom, var(--c100-edge) 1px, transparent 1px),
    linear-gradient(to right, var(--c100-edge) 1px, transparent 1px);
  background-size: var(--u) var(--u);
  background-position: -1px -1px;
  border: 1px solid var(--c100-edge);
}

.block--1000 {
  width: calc(var(--u) * 13);
  height: calc(var(--u) * 13);
  border: none;
  background: none;
}

.block--1000 svg {
  display: block;
  width: 100%;
  height: 100%;
  overflow: visible;
}

.cube__front,
.cube__top,
.cube__right {
  stroke: var(--c1000-edge);
  stroke-width: 0.8;
  stroke-linejoin: round;
}

.cube__front {
  fill: var(--c1000);
}

.cube__top {
  fill: #c4b5fd;
}

.cube__right {
  fill: #8b5cf6;
}

.cube__lines {
  fill: none;
  stroke: var(--c1000-edge);
  stroke-width: 0.22;
}

button.block:hover {
  filter: brightness(1.08);
  outline: 2px solid rgba(220, 38, 38, 0.55);
  outline-offset: 1px;
}

.block.is-new {
  animation: pop-in 320ms cubic-bezier(0.2, 0.9, 0.3, 1.3) both;
}

@keyframes pop-in {
  from {
    transform: scale(0.3);
    opacity: 0;
  }
}

/* Moved with transform only (see positionGhost), so dragging stays smooth. */
.block--ghost {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 100;
  pointer-events: none;
  opacity: 0.85;
  will-change: transform;
  --u: clamp(8px, 1.1vw, 14px);
}

/* ---------- Board ---------- */

.board-card {
  padding: clamp(0.75rem, 2vw, 1.25rem);
}

.board {
  display: grid;
  grid-template-columns: var(--columns, 2.2fr 1.3fr 1fr);
  min-height: calc(var(--u) * 26);
  border-radius: 16px;
  transition: box-shadow 140ms ease, background 140ms ease;
}

.board.is-drop-target {
  background: rgba(37, 99, 235, 0.05);
  box-shadow: inset 0 0 0 3px rgba(37, 99, 235, 0.35);
}

.column {
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-width: 0;
  padding: 0 clamp(0.4rem, 1.2vw, 1rem);
}

.column + .column {
  border-left: 2px dashed #cbd5e1;
}

.column__head {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.2rem 0.5rem;
  padding: 0.35rem 0 0.6rem;
  margin-bottom: 0.75rem;
  border-bottom: 4px solid var(--place-color);
}

.column--1 {
  --place-color: var(--c1);
}

.column--10 {
  --place-color: var(--c10);
}

.column--100 {
  --place-color: var(--c100);
}

.column--1000 {
  --place-color: var(--c1000);
}

.column__name {
  font-weight: 700;
  font-size: clamp(0.8rem, 1.6vw, 1rem);
}

.column__count {
  font-size: clamp(1.4rem, 3.5vw, 2.2rem);
  font-weight: 800;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

.column__count.is-over {
  color: #dc2626;
}

.column__blocks {
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;
  gap: calc(var(--u) * 0.5);
  min-height: calc(var(--u) * 11);
}

/* Ones in groups of ten, each two rows of five, with extra space between tens. */
.column--1 .column__blocks {
  flex-direction: column;
  flex-wrap: nowrap;
  gap: calc(var(--u) * 1.1);
}

.ones-group {
  display: grid;
  grid-template-columns: repeat(5, var(--u));
  grid-auto-rows: var(--u);
  gap: calc(var(--u) * 0.45);
}

/* A little extra space after every fifth ten. */
.column--10 .block:nth-child(5n) {
  margin-right: calc(var(--u) * 0.8);
}

.column__actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  padding-top: 0.9rem;
}

.exchange {
  flex: 1 1 auto;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: #fff;
  padding: 0.4rem 0.7rem;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-secondary);
  cursor: pointer;
  white-space: nowrap;
}

.exchange:hover:not(:disabled) {
  border-color: var(--accent);
  color: var(--accent);
}

.exchange:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* ---------- Readout & controls ---------- */

.readout-card {
  display: grid;
  grid-template-columns: minmax(0, 1.2fr) minmax(0, 1fr);
  gap: 1.5rem;
  align-items: start;
}

.readout {
  display: grid;
  gap: 0.35rem;
}

.readout__number {
  font-size: clamp(3rem, 9vw, 5rem);
  font-weight: 800;
  letter-spacing: -0.02em;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

.readout__number.is-hidden {
  color: var(--text-muted);
}

.readout__expanded {
  font-size: clamp(1.3rem, 3vw, 1.8rem);
  font-weight: 700;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}

.readout__words {
  font-size: clamp(1.1rem, 2.6vw, 1.5rem);
  font-weight: 600;
  color: var(--text-secondary);
  overflow-wrap: anywhere;
}

.readout__warning {
  margin: 0.4rem 0 0;
  min-height: 1.4em;
  font-weight: 600;
  color: #dc2626;
}

.build-controls {
  display: grid;
  gap: 1rem;
}

.build-form {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.6rem;
}

.build-form label {
  font-weight: 600;
  color: var(--text-secondary);
}

.build-form input,
.field select {
  border: 1px solid rgba(148, 163, 184, 0.4);
  border-radius: 12px;
  padding: 0.6rem 0.8rem;
  font-size: 1rem;
  background: rgba(255, 255, 255, 0.95);
  color: var(--text-primary);
}

.build-form input {
  width: 7rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.build-form input:focus,
.field select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.2);
}

.checks {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.5rem 1rem;
}

.check {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.95rem;
  cursor: pointer;
}

.check input {
  width: 1.1rem;
  height: 1.1rem;
  accent-color: var(--accent);
}

.field {
  display: grid;
  gap: 0.35rem;
  font-size: 0.9rem;
  color: var(--text-secondary);
}

/* ---------- Practice ---------- */

.task {
  display: grid;
  gap: 0.75rem;
}

.task__prompt {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--text-secondary);
}

.task__target {
  font-size: clamp(2.6rem, 8vw, 4.4rem);
  font-weight: 800;
  line-height: 1.05;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
  overflow-wrap: anywhere;
}

.task__target.is-words {
  font-size: clamp(1.6rem, 4.5vw, 2.6rem);
}

.action-row--start {
  justify-content: flex-start;
}

.feedback {
  margin: 0;
  min-height: 1.5em;
  font-weight: 600;
}

.feedback.is-right {
  color: var(--success);
}

.feedback.is-almost {
  color: #b45309;
}

.feedback.is-wrong {
  color: #dc2626;
}

.feedback.is-hint {
  color: var(--text-secondary);
}

.score {
  margin: 0;
  font-weight: 600;
  color: var(--text-secondary);
}

/* ---------- Notes ---------- */

.notes {
  background: rgba(255, 255, 255, 0.7);
  border-radius: 20px;
  padding: 1.5rem;
  box-shadow: 0 12px 28px rgba(15, 23, 42, 0.08);
  display: grid;
  gap: 1rem;
}

.notes h2 {
  margin: 0;
  font-size: 1.25rem;
}

.notes ul {
  margin: 0;
  padding-left: 1.2rem;
  display: grid;
  gap: 0.5rem;
}

.notes li {
  color: var(--text-secondary);
}

/* ---------- Responsive ---------- */

@media (max-width: 760px) {
  .readout-card {
    grid-template-columns: minmax(0, 1fr);
  }
}

@media (max-width: 520px) {
  .tray__item {
    padding: 0.4rem 0.7rem 0.4rem 0.4rem;
    gap: 0.4rem;
    min-height: 54px;
  }

  .tray__picture {
    width: 40px;
    height: 40px;
    --u: 3.6px;
  }

  .tray__label strong {
    font-size: 0.85rem;
  }

  .checks {
    grid-template-columns: minmax(0, 1fr);
  }

  .exchange {
    font-size: 0.75rem;
    padding: 0.35rem 0.4rem;
  }
}

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

/* Tray pictures: keep the small cube visible and the big one inside its box. */
.tray__picture .block--1 {
  width: 14px;
  height: 14px;
}

.tray__picture .block--1000 {
  width: calc(var(--u) * 10.5);
  height: calc(var(--u) * 10.5);
}

/* ---------- Block rain ---------- */

.block-rain {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 90;
  pointer-events: none;
}

.block-rain[hidden] {
  display: none;
}

/* ---------- Tower ---------- */

.tower {
  --t: 18px;
  display: grid;
  gap: 0.5rem;
  padding: 0.9rem 1rem;
  border-radius: 18px;
  background: rgba(15, 23, 42, 0.03);
}

.tower__head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
}

.tower__title {
  font-weight: 700;
}

.tower__count {
  font-weight: 800;
  color: var(--success);
  font-variant-numeric: tabular-nums;
}

.tower__stage {
  display: flex;
  justify-content: center;
  align-items: flex-end;
  min-height: calc(var(--t) * 4);
  padding-top: 0.5rem;
  border-bottom: 3px solid #cbd5e1;
}

/* Built from the bottom up: hundreds, tens, then loose ones. */
.tower__stack {
  display: flex;
  flex-direction: column-reverse;
  align-items: flex-start;
  gap: 2px;
  width: calc(var(--t) * 10);
}

.tower__row {
  width: calc(var(--t) * 10);
  height: var(--t);
  border-radius: 2px;
  background-size: var(--t) 100%;
  background-position: -1px 0;
}

.tower__row--10 {
  background-color: var(--c10);
  background-image: linear-gradient(to right, var(--c10-edge) 1px, transparent 1px);
  border: 1px solid var(--c10-edge);
}

.tower__row--100 {
  height: calc(var(--t) * 1.4);
  background-color: var(--c100);
  background-image: linear-gradient(to right, var(--c100-edge) 1px, transparent 1px);
  border: 1px solid var(--c100-edge);
  box-shadow: inset 0 -3px 0 rgba(29, 78, 216, 0.35);
}

.tower__ones {
  display: flex;
  gap: 0;
  min-height: var(--t);
  transform-origin: left center;
}

.tower__cube {
  width: var(--t);
  height: var(--t);
  background: var(--c1);
  border: 1px solid var(--c1-edge);
  border-radius: 2px;
}

.tower__row.is-new,
.tower__cube.is-new {
  animation: tower-drop 420ms cubic-bezier(0.2, 0.9, 0.3, 1.25) both;
}

@keyframes tower-drop {
  from {
    transform: translateY(-30px);
    opacity: 0;
  }
}

/* Ten blocks squeeze together and glow before becoming one bigger block. */
.tower__ones.is-merging,
.tower__row--10.is-merging {
  animation: tower-merge 600ms ease-in both;
}

@keyframes tower-merge {
  50% {
    filter: brightness(1.35);
    transform: scaleY(1.25);
  }
  to {
    filter: brightness(1.6);
    transform: scaleY(0.6);
    opacity: 0.4;
  }
}

.tower__reset {
  justify-self: start;
  border: none;
  background: none;
  padding: 0.2rem 0;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-secondary);
  text-decoration: underline;
  cursor: pointer;
}
