:root {
  --cell: clamp(38px, 11vw, 66px);
  --gap: 4px;

  --bg: #f4f6fb;
  --ink: #1f2430;

  --fixed-bg: #f8d99b;
  --fixed-bg-2: #f3cd80;
  --fixed-border: #e0b25f;

  --blank-bg: #fcf3e1;
  --blank-border: #e7cfa0;

  --tile-bg: #e7f7f1;
  --tile-ink: #0c7a4d;
  --tile-border: #2c9e72;

  --ok: #2c9e72;
  --ok-bg: #d8f5e7;
  --bad: #e05656;
  --bad-bg: #fbe0e0;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

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

body {
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, Arial, sans-serif;
  color: var(--ink);
  background:
    radial-gradient(1200px 600px at 50% -10%, #ffffff, var(--bg));
  height: 100vh;
  height: 100dvh;            /* fit exactly; no page scroll */
  display: flex;
  flex-direction: column;
  overflow: hidden;
  touch-action: manipulation;
}

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 14px 2px;
  flex: 0 0 auto;
}
.title {
  margin: 0;
  font-size: clamp(20px, 5vw, 30px);
  font-weight: 800;
  letter-spacing: 0.5px;
  background: linear-gradient(120deg, #0c7a4d, #1f9d8f);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.topbar__right { display: flex; gap: 8px; }
.icon-btn {
  border: none;
  background: #ffffff;
  border: 1px solid #e3e7ef;
  width: 40px; height: 40px;
  border-radius: 12px;
  font-size: 18px;
  cursor: pointer;
  box-shadow: 0 1px 2px rgba(0,0,0,.06);
  transition: transform .12s ease, background .12s ease;
}
.icon-btn:hover { background: #f3f6ff; }
.icon-btn:active { transform: scale(.92); }
.icon-btn.is-off { opacity: .45; }

/* ---------- Level nav ---------- */
.level-bar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px 2px;
  max-width: 640px;
  margin: 0 auto;
  width: 100%;
  flex: 0 0 auto;
}
.level-bar[hidden] { display: none; }
.nav-arrow {
  flex: 0 0 auto;
  border: 1px solid #d8deea;
  background: #fff;
  width: 34px; height: 34px;
  border-radius: 10px;
  font-size: 20px;
  line-height: 1;
  color: #4a5468;
  cursor: pointer;
  box-shadow: 0 1px 2px rgba(0,0,0,.05);
}
.nav-arrow:active { transform: scale(.92); }
.level-nav {
  display: flex;
  gap: 7px;
  overflow-x: auto;
  flex-wrap: nowrap;
  padding: 4px 2px;
  scroll-behavior: smooth;
  scrollbar-width: none;          /* Firefox */
  -ms-overflow-style: none;       /* IE/Edge */
}
.level-nav::-webkit-scrollbar { display: none; } /* WebKit */
.level-chip {
  flex: 0 0 auto;
  border: 1px solid #d8deea;
  background: #fff;
  color: #6b7488;
  width: 34px;
  height: 34px;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 800;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  transition: transform .12s ease, background .12s ease, color .12s ease;
}
.level-chip:active { transform: scale(.92); }
.level-chip.is-active {
  background: linear-gradient(120deg, #0c7a4d, #1f9d8f);
  border-color: transparent;
  color: #fff;
}
.level-chip.is-done {
  background: #e7f7f1;
  border-color: #bfe6d6;
  color: #0c7a4d;
}
.level-chip.is-done.is-active { background: linear-gradient(120deg, #0c7a4d, #1f9d8f); color: #fff; }
.level-chip.is-done::after {
  content: "✓";
  position: absolute;
  top: -4px; right: -4px;
  background: var(--ok);
  color: #fff;
  width: 14px; height: 14px;
  border-radius: 50%;
  font-size: 9px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 0 0 2px #fff;
}

/* ---------- Level title ---------- */
.level-title {
  margin: 2px 0 0;
  font-size: 17px;
  font-weight: 800;
  color: #2b3346;
  text-align: center;
  display: flex;
  gap: 8px;
  align-items: center;
}
.level-title::after {
  content: attr(data-difficulty);
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .5px;
  color: #0c7a4d;
  background: #e7f7f1;
  border: 1px solid #bfe6d6;
  padding: 2px 8px;
  border-radius: 999px;
}

/* ---------- Stage ---------- */
.stage {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 4px 12px 8px;
  min-height: 0;
}
.status {
  margin: 0;
  font-size: 13px;
  color: #5a6477;
  min-height: 18px;
  text-align: center;
  transition: color .2s ease;
}
.status.is-bad { color: var(--bad); font-weight: 700; }
.status.is-ok { color: var(--ok); font-weight: 700; }

/* ---------- Board ---------- */
.board-wrap {
  width: 100%;
  display: flex;
  justify-content: center;
  padding: 2px 4px;
  flex: 0 0 auto;
}
.board {
  display: grid;
  gap: var(--gap);
  position: relative;
}

.cell {
  width: var(--cell);
  height: var(--cell);
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: calc(var(--cell) * 0.44);
  user-select: none;
}

.cell--fixed {
  background: linear-gradient(160deg, var(--fixed-bg), var(--fixed-bg-2));
  border: 1px solid var(--fixed-border);
  box-shadow: inset 0 1px 0 rgba(255,255,255,.5);
}

.cell--blank {
  background: var(--blank-bg);
  border: 2px dashed var(--blank-border);
  transition: transform .12s ease, background .15s ease, border-color .15s ease;
}
.cell--blank.is-over {
  background: #fff7e6;
  border-color: var(--tile-border);
  transform: scale(1.06);
}
.cell--blank.is-filled {
  border-style: solid;
  background: var(--tile-bg);
  color: var(--tile-ink);
  border-color: var(--tile-border);
  cursor: grab;
}
.cell--blank.is-correct {
  background: var(--ok-bg);
  border-color: var(--ok);
  color: var(--ok);
}
.cell--blank.is-wrong {
  animation: shake .4s ease;
  background: var(--bad-bg);
  border-color: var(--bad);
  color: var(--bad);
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-6px); }
  40% { transform: translateX(6px); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(4px); }
}
@keyframes pop {
  0% { transform: scale(.6); }
  60% { transform: scale(1.12); }
  100% { transform: scale(1); }
}
.is-pop { animation: pop .28s ease; }

/* ---------- Tray ---------- */
.tray-wrap {
  width: 100%;
  max-width: 620px;
  margin-top: 2px;
  border-top: 1px solid #e6eaf2;
  padding-top: 8px;
  flex: 0 0 auto;
}
.tray-label {
  margin: 0 0 6px;
  text-align: center;
  font-size: 11px;
  letter-spacing: 1px;
  text-transform: uppercase;
  color: #9aa3b5;
  font-weight: 700;
}
.tray {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: center;
  min-height: var(--cell);
}

.tile {
  width: var(--cell);
  height: var(--cell);
  border-radius: 12px;
  background: var(--tile-bg);
  border: 2px solid var(--tile-border);
  color: var(--tile-ink);
  font-weight: 800;
  font-size: calc(var(--cell) * 0.44);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: grab;
  box-shadow: 0 2px 0 rgba(44,158,114,.25);
  touch-action: none;
  transition: transform .12s ease, box-shadow .12s ease;
}
.tile:active { cursor: grabbing; }
.tile.is-hidden { visibility: hidden; }
.tile.is-dragging { opacity: .25; }

/* ---------- Floating drag ghost ---------- */
.drag-layer {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 50;
}
.ghost {
  position: fixed;
  width: var(--cell);
  height: var(--cell);
  border-radius: 12px;
  background: var(--tile-bg);
  border: 2px solid var(--tile-border);
  color: var(--tile-ink);
  font-weight: 800;
  font-size: calc(var(--cell) * 0.44);
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 10px 24px rgba(0,0,0,.22);
  transform: translate(-50%, -50%) scale(1.08);
  pointer-events: none;
  will-change: left, top;
}

/* ---------- Win overlay ---------- */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(20, 28, 40, .45);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 60;
  animation: fade .25s ease;
}
.overlay[hidden] { display: none; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.overlay__card {
  background: #fff;
  border-radius: 22px;
  padding: 28px 26px 22px;
  width: min(86vw, 340px);
  text-align: center;
  box-shadow: 0 20px 60px rgba(0,0,0,.3);
  animation: pop .35s ease;
}
.overlay__emoji { font-size: 54px; }
.overlay__title {
  margin: 4px 0 2px;
  font-size: 26px;
  color: var(--ok);
}
.overlay__sub { margin: 0 0 18px; color: #5a6477; font-size: 15px; }
.overlay__buttons { display: flex; gap: 10px; justify-content: center; }

.btn {
  border: none;
  border-radius: 12px;
  padding: 11px 18px;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  transition: transform .12s ease, filter .12s ease;
}
.btn:active { transform: scale(.95); }
.btn--primary { background: linear-gradient(120deg, #0c7a4d, #1f9d8f); color: #fff; }
.btn--primary:hover { filter: brightness(1.05); }
.btn--ghost { background: #eef1f7; color: #4a5468; }

#confetti {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 55;
}

/* ---------- Locked level chip ---------- */
.level-chip.is-locked {
  opacity: .5;
  cursor: default;
  color: #aab2c2;
}
.level-chip.is-locked::after {
  content: "🔒";
  position: absolute;
  top: -5px; right: -5px;
  font-size: 10px;
}

/* ---------- Win stars ---------- */
.stars {
  font-size: 34px;
  letter-spacing: 4px;
  color: #f5b301;
  margin: 2px 0 6px;
  line-height: 1;
  text-shadow: 0 1px 0 rgba(0,0,0,.08);
}

/* ---------- Endless bar ---------- */
.endless-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 4px 10px 2px;
  flex: 0 0 auto;
}
.endless-bar[hidden] { display: none; }
.streak {
  font-weight: 800;
  font-size: 15px;
  color: #e0662a;
  background: #fff2e8;
  border: 1px solid #ffd5b8;
  padding: 5px 12px;
  border-radius: 999px;
}
.pill {
  border: 1px solid transparent;
  border-radius: 999px;
  padding: 7px 14px;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
  background: linear-gradient(120deg, #0c7a4d, #1f9d8f);
  color: #fff;
}
.pill:active { transform: scale(.95); }
.pill--ghost { background: #eef1f7; color: #4a5468; border-color: #dfe4ee; }

/* ---------- Number line ---------- */
.number-line { display: none; width: 100%; max-width: 620px; flex: 0 0 auto; }
.number-line.show { display: block; }
.nl-track {
  display: flex;
  justify-content: space-between;
  position: relative;
  padding: 6px 4px 2px;
}
.nl-track::before {
  content: "";
  position: absolute;
  left: 8px; right: 8px; top: 11px;
  height: 2px;
  background: #d8deea;
}
.nl-tick { display: flex; flex-direction: column; align-items: center; gap: 2px; }
.nl-tick i { width: 2px; height: 8px; background: #c2cad8; border-radius: 2px; }
.nl-tick b { font-size: 11px; font-weight: 700; color: #7b8499; }

/* ---------- Level map ---------- */
.overlay--map { align-items: stretch; padding: 0; }
.map {
  background: #fff;
  width: min(100vw, 560px);
  margin: auto;
  max-height: 92dvh;
  border-radius: 18px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 20px 60px rgba(0,0,0,.3);
}
.map__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid #eef1f7;
}
.map__head h2 { margin: 0; font-size: 20px; }
.map__body { overflow-y: auto; padding: 12px 14px 20px; }
.map-band { margin-bottom: 16px; }
.map-band__head { display: flex; align-items: baseline; gap: 8px; margin: 0 0 8px; }
.map-band__name { font-weight: 800; font-size: 15px; color: #2b3346; }
.map-band__diff {
  font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: .5px;
  color: #0c7a4d; background: #e7f7f1; border: 1px solid #bfe6d6;
  padding: 1px 7px; border-radius: 999px;
}
.map-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.map-card {
  text-align: left;
  border: 1px solid #e3e7ef;
  background: #fff;
  border-radius: 12px;
  padding: 10px 12px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-height: 56px;
  transition: transform .12s ease, border-color .12s ease;
}
.map-card:active { transform: scale(.97); }
.map-card.is-done { background: #f3fbf7; border-color: #bfe6d6; }
.map-card.is-locked { opacity: .55; cursor: default; background: #f7f8fb; }
.map-num { font-weight: 800; font-size: 14px; color: #0c7a4d; }
.map-lock { font-size: 16px; }
.map-name { font-size: 13px; font-weight: 600; color: #3a4255; }
.map-stars { color: #f5b301; font-size: 14px; letter-spacing: 1px; }
.map-practice {
  grid-column: 1 / -1;
  border: 1px dashed #bfe6d6;
  background: #f3fbf7;
  color: #0c7a4d;
  font-weight: 700;
  font-size: 13px;
  border-radius: 12px;
  padding: 9px;
  cursor: pointer;
}
.map-practice.is-locked { opacity: .5; cursor: default; border-color: #e3e7ef; background: #f7f8fb; color: #aab2c2; }

/* ---------- Kid mode (early bands): bigger, friendlier ---------- */
body.kid .cell { border-radius: 14px; }
body.kid .tile { border-radius: 16px; border-width: 3px; }
body.kid .level-title { font-size: 19px; }
