/* Threes Trainer, built for a phone.
   One screen, no page scroll, controls in the thumb zone. The palette echoes
   the real game so what you learn here transfers: blue 1s, red 2s, pale
   3-and-up tiles on a deep plum ground. */

:root {
  --bg: #2b2735;
  --bg-deep: #211e29;
  --panel: #34303f;
  --panel-2: #3d3849;
  --line: #4a4456;
  --ink: #f3f1f7;
  --ink-dim: #a49eb4;
  --ink-faint: #7a7389;

  --tile: #e9e7ef;
  --tile-ink: #2a2733;
  --one: #4db1e8;
  --one-deep: #2f8ec4;
  --two: #ef6b7d;
  --two-deep: #cf4a5e;

  --good: #5fd0a0;
  --warn: #f0b552;
  --bad: #f0707f;
  --accent: #9b8cf0;

  --r-sm: 8px;
  --r-md: 12px;
  --r-lg: 18px;
  --shadow: 0 10px 30px rgba(0, 0, 0, .38);

  --font: ui-rounded, "SF Pro Rounded", system-ui, -apple-system, "Segoe UI", sans-serif;

  --safe-t: env(safe-area-inset-top);
  --safe-b: env(safe-area-inset-bottom);
}

* { box-sizing: border-box; }

/* .sheet and .overlay set an explicit display, which beats the user agent's
   [hidden] { display: none }. Without this they never hide. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  overscroll-behavior: none;
  overflow: hidden;
}

button { font: inherit; }

/* The whole app is one non-scrolling column that fits the viewport. The board
   absorbs whatever height is left over. */
.app {
  height: 100dvh;
  max-width: 480px;
  margin: 0 auto;
  padding: calc(var(--safe-t) + 8px) 12px calc(var(--safe-b) + 8px);
  display: flex;
  flex-direction: column;
  gap: 9px;
}

/* ---------------------------------------------------------------- top bar */

.topbar { display: flex; gap: 8px; align-items: stretch; flex: none; }

.statline {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  flex: 1;
  min-width: 0;
}

.stat {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  padding: 5px 7px;
  min-width: 0;
}

.stat strong {
  display: block;
  font-size: 16px;
  font-variant-numeric: tabular-nums;
  letter-spacing: -.02em;
  line-height: 1.15;
  overflow: hidden;
  text-overflow: ellipsis;
}

.stat-label {
  display: block;
  font-size: 9px;
  text-transform: uppercase;
  letter-spacing: .09em;
  color: var(--ink-faint);
  font-weight: 800;
}

.stat-agree strong { color: var(--accent); }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  background: var(--panel);
  color: var(--ink-dim);
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  padding: 0 12px;
  font-size: 13px;
  cursor: pointer;
  flex: none;
}
.icon-btn:active { background: var(--panel-2); }
.icon-btn.icon-only { padding: 8px; }

.icon-btn svg, .btn svg, .strip-chev {
  width: 18px; height: 18px; flex: none;
  fill: none; stroke: currentColor; stroke-width: 2;
  stroke-linecap: round; stroke-linejoin: round;
}

/* ------------------------------------------------------------ next + bag */

.next-row {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 8px;
  flex: none;
}

.next-block, .bag {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  padding: 7px 9px;
}

.next-block {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
}

.tile-next {
  position: relative;
  width: 38px;
  height: 50px;
  font-size: 18px;
  box-shadow: 0 3px 0 rgba(0,0,0,.28);
}

.bag-rows { display: flex; flex-direction: column; gap: 3px; margin-top: 4px; }
.bag-row { display: flex; align-items: center; gap: 7px; }

.pip {
  width: 18px; height: 21px;
  border-radius: 4px;
  display: grid; place-items: center;
  font-size: 11px; font-weight: 800;
  flex: none;
}
.pip-1 { background: var(--one); color: #fff; }
.pip-2 { background: var(--two); color: #fff; }
.pip-3 { background: var(--tile); color: var(--tile-ink); }

.pips { display: flex; gap: 3px; }

.dot {
  width: 13px; height: 17px;
  border-radius: 3px;
  background: var(--panel-2);
  border: 1px solid var(--line);
}
.dot.on { border-color: transparent; }
.bag-row[data-v="1"] .dot.on { background: var(--one); }
.bag-row[data-v="2"] .dot.on { background: var(--two); }
.bag-row[data-v="3"] .dot.on { background: var(--tile); }

#bag-total { font-style: normal; color: var(--ink-dim); }

.bag-note {
  margin: 5px 0 0;
  font-size: 10.5px;
  line-height: 1.35;
  color: var(--ink-faint);
}
.bag-note strong { color: var(--warn); font-weight: 800; }

/* ----------------------------------------------------------------- board */

/* Takes all remaining height. js/ui.js sets --board-px from the measured box
   so the board is never taller than the space it has, nor wider than the
   column. */
.board-wrap {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.board {
  width: var(--board-px, 100%);
  aspect-ratio: 3 / 4;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: 2.2%;
  background: var(--bg-deep);
  border-radius: var(--r-lg);
  padding: 2.2%;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.cell {
  position: relative;
  border-radius: var(--r-md);
  background: #1a1822;
  min-width: 0;
  min-height: 0;
}

.cell.spawn-target::after {
  content: "";
  position: absolute;
  inset: 3px;
  border-radius: 10px;
  border: 2px dashed var(--accent);
  animation: pulse 1.1s ease-in-out infinite;
  pointer-events: none;
  z-index: 3;
}
@keyframes pulse { 0%,100% { opacity: .35; } 50% { opacity: 1; } }

.tile {
  width: 100%;
  height: 100%;
  border-radius: var(--r-md);
  display: grid;
  place-items: center;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  letter-spacing: -.03em;
  background: var(--tile);
  color: var(--tile-ink);
  box-shadow: 0 4px 0 rgba(0, 0, 0, .28);
}

/* Board tiles scale with the board, not the viewport, so they stay right at
   any phone size. */
.board .tile { font-size: calc(var(--board-px, 380px) * .085); }
.board .tile.big { font-size: calc(var(--board-px, 380px) * .066); }
.board .tile.huge { font-size: calc(var(--board-px, 380px) * .053); }

.tile[data-v="1"] { background: var(--one); color: #fff; box-shadow: 0 4px 0 var(--one-deep); }
.tile[data-v="2"] { background: var(--two); color: #fff; box-shadow: 0 4px 0 var(--two-deep); }
.tile[data-v="48"], .tile[data-v="96"] { color: #c9502f; }
.tile[data-v="192"], .tile[data-v="384"] { color: #d99b1c; }
.tile[data-v="768"], .tile[data-v="1536"] { color: #2f8ec4; }
.tile[data-v="3072"], .tile[data-v="6144"] { color: #7b4fd4; }

.tile.fresh { animation: pop .18s ease-out; }
@keyframes pop { from { transform: scale(.74); } to { transform: scale(1); } }

.tile.merged { animation: bump .2s ease-out; }
@keyframes bump { 0% { transform: scale(1); } 45% { transform: scale(1.1); } 100% { transform: scale(1); } }

.tile-next.hidden-value { color: transparent; }
.tile-next.hidden-value::after {
  content: "?";
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: rgba(42, 39, 51, .3);
  font-size: 17px;
}
.tile-next.hidden-value[data-v="1"]::after,
.tile-next.hidden-value[data-v="2"]::after { color: rgba(255,255,255,.55); }

/* ------------------------------------------------------------- game over */

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(33, 30, 41, .9);
  border-radius: var(--r-lg);
  z-index: 5;
}
.overlay-card { text-align: center; padding: 20px; }
.overlay-card h2 { margin: 0 0 6px; font-size: 21px; }
.overlay-card p { margin: 0 0 14px; color: var(--ink-dim); font-size: 13px; line-height: 1.5; }

/* ----------------------------------------------------------- coach strip */

.coach-strip {
  flex: none;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 9px;
  width: 100%;
  text-align: left;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  padding: 8px 10px;
  color: var(--ink-dim);
  cursor: pointer;
}
.coach-strip:active { background: var(--panel-2); }

.strip-text {
  font-size: 12px;
  line-height: 1.35;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.strip-chev { color: var(--ink-faint); }

.badge {
  font-size: 9.5px;
  text-transform: uppercase;
  letter-spacing: .08em;
  padding: 4px 8px;
  border-radius: 20px;
  font-weight: 800;
  flex: none;
  background: var(--panel-2);
  color: var(--ink-faint);
}
.badge.best { background: rgba(95,208,160,.16); color: var(--good); }
.badge.fine { background: rgba(155,140,240,.16); color: var(--accent); }
.badge.loose { background: rgba(240,181,82,.16); color: var(--warn); }
.badge.bad { background: rgba(240,112,127,.16); color: var(--bad); }

/* ------------------------------------------------------------------ dock */

.dock { flex: none; display: flex; flex-direction: column; gap: 7px; }

.dock-actions { display: grid; grid-template-columns: repeat(3, 1fr); gap: 7px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 42px;
  background: var(--panel);
  border: 1px solid var(--line);
  color: var(--ink-dim);
  border-radius: var(--r-md);
  font-size: 12.5px;
  font-weight: 700;
  cursor: pointer;
}
.btn:active { background: var(--panel-2); }
.btn:disabled { opacity: .4; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; height: 44px; padding: 0 22px; }

.dock-hint {
  margin: 0;
  text-align: center;
  font-size: 10.5px;
  color: var(--ink-faint);
  line-height: 1.3;
}

/* Short screens: drop the least important things rather than squeeze the board. */
@media (max-height: 700px) {
  .dock-hint { display: none; }
  .bag-note { display: none; }
  .btn { height: 38px; }
}

/* ------------------------------------------------------------ bottom sheet */

.sheet { position: fixed; inset: 0; z-index: 50; display: flex; align-items: flex-end; }
.sheet-scrim { position: absolute; inset: 0; background: rgba(20, 18, 26, .7); }

.sheet-card {
  position: relative;
  background: var(--panel);
  border-top-left-radius: 20px;
  border-top-right-radius: 20px;
  width: 100%;
  max-width: 480px;
  margin: 0 auto;
  max-height: 88dvh;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow);
  animation: slideup .22s cubic-bezier(.2,.8,.3,1);
  padding-bottom: var(--safe-b);
}
@keyframes slideup { from { transform: translateY(100%); } to { transform: none; } }

.sheet-grip { padding: 10px 0 2px; display: flex; justify-content: center; cursor: pointer; }
.sheet-grip::after { content: ""; width: 38px; height: 4px; border-radius: 3px; background: var(--line); }

.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 6px 16px 10px; }
.sheet-head h2 { margin: 0; font-size: 16px; }

.sheet-tabs { display: flex; gap: 6px; padding: 0 16px 12px; overflow-x: auto; }
.tab {
  background: var(--panel-2);
  border: 1px solid transparent;
  color: var(--ink-dim);
  border-radius: 20px;
  padding: 7px 13px;
  font-size: 12.5px;
  font-weight: 700;
  white-space: nowrap;
  cursor: pointer;
}
.tab.active { background: rgba(155,140,240,.18); border-color: var(--accent); color: var(--accent); }

.sheet-body { padding: 0 16px 20px; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.sheet-body h3 { margin: 16px 0 6px; font-size: 13.5px; }
.sheet-body h3:first-child { margin-top: 0; }
.sheet-body p, .sheet-body li { font-size: 13px; line-height: 1.55; color: var(--ink-dim); }
.sheet-body p { margin: 0 0 8px; }
.sheet-body ul { margin: 0 0 10px; padding-left: 19px; }
.sheet-body code {
  background: var(--panel-2); padding: 1px 5px; border-radius: 4px;
  font-size: 12px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

.verdict-head { display: flex; align-items: center; gap: 9px; margin-bottom: 8px; font-size: 15px; font-weight: 800; }

/* --------------------------------------------------------------- ranking */

.ranking { list-style: none; margin: 0 0 4px; padding: 0; display: flex; flex-direction: column; gap: 6px; }

.rank-row {
  display: grid;
  grid-template-columns: 30px minmax(0, 1fr);
  align-items: center;
  gap: 10px;
  padding: 9px 10px;
  background: var(--panel-2);
  border: 1px solid transparent;
  border-radius: var(--r-sm);
  cursor: pointer;
}
.rank-row.active { border-color: var(--accent); }
.rank-row .arrow { width: 26px; height: 26px; display: grid; place-items: center; border-radius: 6px; background: rgba(0,0,0,.2); }
.rank-row .arrow svg { width: 15px; height: 15px; fill: none; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }
.rank-row[data-grade="best"] .arrow { color: var(--good); }
.rank-row[data-grade="fine"] .arrow { color: var(--accent); }
.rank-row[data-grade="loose"] .arrow { color: var(--warn); }
.rank-row[data-grade="bad"] .arrow { color: var(--bad); }

.rank-why { font-size: 12px; line-height: 1.45; color: var(--ink-dim); min-width: 0; }
.rank-why b { color: var(--ink); font-weight: 800; text-transform: capitalize; }

.hint-foot { margin: 8px 0 0; font-size: 11px; color: var(--ink-faint); line-height: 1.4; }

/* In "grade after" mode the ranking stays hidden until you commit. The grade
   colour on the arrow would give it away through the blur, so the whole row
   goes neutral, not just the text. */
.masked .rank-why { filter: blur(4.5px); user-select: none; }
.masked .rank-row .arrow { color: var(--ink-faint) !important; }
.masked .rank-row { cursor: default; }

/* --------------------------------------------------------------- lessons */

.lessons { display: flex; flex-direction: column; gap: 11px; }
.lesson { border-left: 3px solid var(--accent); padding: 2px 0 2px 10px; }
.lesson.warn { border-color: var(--warn); }
.lesson h3 { margin: 0 0 3px; font-size: 13px; font-weight: 800; }
.lesson.warn h3 { color: var(--warn); }
.lesson p { margin: 0; font-size: 12.5px; line-height: 1.5; color: var(--ink-dim); }
.lessons-empty { font-size: 13px; color: var(--ink-faint); margin: 0; line-height: 1.5; }

/* -------------------------------------------------------------- settings */

.setting { display: flex; align-items: center; justify-content: space-between; gap: 14px; padding: 12px 0; border-bottom: 1px solid var(--line); }
.setting:last-child { border-bottom: 0; }
.setting-text strong { display: block; font-size: 13.5px; margin-bottom: 2px; }
.setting-text span { font-size: 11.5px; color: var(--ink-faint); line-height: 1.4; display: block; }
.setting select {
  background: var(--panel-2); color: var(--ink); border: 1px solid var(--line);
  border-radius: var(--r-sm); padding: 9px; font: inherit; font-size: 13px; flex: none;
}
.switch { position: relative; width: 46px; height: 27px; flex: none; }
.switch input { opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.switch .track { position: absolute; inset: 0; border-radius: 20px; background: var(--panel-2); border: 1px solid var(--line); pointer-events: none; }
.switch .track::after { content: ""; position: absolute; top: 3px; left: 3px; width: 19px; height: 19px; border-radius: 50%; background: var(--ink-faint); transition: transform .15s, background .15s; }
.switch input:checked + .track { background: rgba(155,140,240,.3); border-color: var(--accent); }
.switch input:checked + .track::after { transform: translateX(19px); background: var(--accent); }

/* ---------------------------------------------------------------- toasts */

.toasts {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--safe-b) + 16px);
  display: flex; flex-direction: column; gap: 8px; z-index: 60;
  pointer-events: none; width: min(440px, calc(100vw - 24px));
}
.toast {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-left: 3px solid var(--accent);
  border-radius: var(--r-sm);
  padding: 10px 14px;
  font-size: 12.5px;
  line-height: 1.4;
  color: var(--ink);
  box-shadow: var(--shadow);
  animation: rise .2s ease-out;
}
.toast.warn { border-left-color: var(--warn); }
.toast.good { border-left-color: var(--good); }
@keyframes rise { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

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