/* Shared HUD. Each world sets the variables below in its own style block. */
@import url('timer.css');
@import url('story.css');
@import url('sheet/sheet.css');

:root {
  --font: 'Rubik', system-ui, sans-serif;
  --title-font: var(--font);
  --ink: #1d2433;
  --muted: #5d6577;
  --card: rgba(255, 255, 255, 0.82);
  --card-edge: rgba(255, 255, 255, 0.9);
  --card-shadow: 0 18px 50px rgba(10, 20, 40, 0.25);
  --radius: 26px;
  --accent: #6c5ce7;
  --accent-ink: #fff;
  --accent-shade: #4b3cc4;
  --key: #fff;
  --key-ink: #1d2433;
  --key-shade: #d5d9e4;
  --good: #1fa463;
  --bad: #e0453a;
  --time: linear-gradient(90deg, #ffb347, #ff6f61);
  --x: #e8590c;
  --flip: #ff9f1c;
  --focus: rgba(255, 200, 60, 0.45);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  font-family: var(--font);
  color: var(--ink);
  background: #000;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

.scene { position: fixed; inset: 0; display: block; }

.hud { position: fixed; inset: 0; pointer-events: none; }
.hud > * { pointer-events: auto; }
[hidden] { display: none !important; }

button { font: inherit; color: inherit; cursor: pointer; border: 0; }

.card {
  background: var(--card);
  border: 2px solid var(--card-edge);
  border-radius: var(--radius);
  box-shadow: var(--card-shadow);
  backdrop-filter: blur(14px) saturate(1.3);
  -webkit-backdrop-filter: blur(14px) saturate(1.3);
}

/* Top bar */
.topbar {
  position: absolute;
  top: 14px;
  left: 14px;
  right: 14px;
  display: flex;
  align-items: center;
  gap: 12px;
  pointer-events: none;
}
.topbar > * { pointer-events: auto; }
.back, .brand, .icon-button {
  padding: 9px 16px;
  border-radius: 999px;
  background: var(--card);
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.18);
  font-weight: 700;
  color: var(--ink);
  text-decoration: none;
  backdrop-filter: blur(10px);
}
.back, .brand { white-space: nowrap; }
.brand { font-family: var(--title-font); margin-right: auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.icon-button { width: 44px; height: 44px; padding: 0; font-size: 22px; }

/* Settings */
.settings { position: relative; }
.settings-panel {
  position: absolute;
  right: 0;
  top: 54px;
  width: 230px;
  padding: 16px;
  display: grid;
  gap: 8px;
  z-index: 5;
}
.settings-label { font-size: 13px; font-weight: 700; color: var(--muted); }
.toggle, .lang {
  padding: 10px;
  border-radius: 14px;
  background: var(--key-shade);
  font-weight: 700;
}
.toggle.on, .lang.active { background: var(--accent); color: var(--accent-ink); }
.volume { width: 100%; accent-color: var(--accent); }
.lang-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

/* Side column with the active card */
.side {
  position: absolute;
  right: 18px;
  top: 74px;
  width: 380px;
  max-height: calc(100% - 92px);
  display: flex;
}
.side > .card { width: 100%; padding: 22px; overflow: auto; }

@media (max-width: 859px) {
  .side { left: 10px; right: 10px; top: auto; bottom: 10px; width: auto; max-height: 62%; }
  .side > .card { padding: 16px; }
}

/* Menu */
.title { margin: 0; font-family: var(--title-font); font-size: 30px; line-height: 1.1; }
.subtitle { margin: 6px 0 14px; color: var(--muted); }
/* One big sign per group in a single row (wraps only if there are too many); the active group's name sits below. */
.tabs { display: grid; grid-template-columns: repeat(auto-fit, minmax(40px, 1fr)); gap: 6px; }
.tab {
  display: grid;
  place-items: center;
  height: 48px;
  padding: 0;
  border-radius: 14px;
  background: var(--key-shade);
  color: var(--key-ink);
  font-weight: 800;
  font-size: 21px;
  white-space: nowrap;
  transition: transform 0.12s;
}
.tab:active { transform: translateY(3px); }
.tab.active { background: var(--accent); color: var(--accent-ink); box-shadow: 0 4px 0 var(--accent-shade); }
.group-name { margin: 10px 0 10px; font-size: 17px; font-weight: 800; color: var(--accent); }
.topics { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.topic {
  display: grid;
  grid-template-columns: 38px 1fr;
  grid-template-rows: auto auto;
  align-items: center;
  column-gap: 10px;
  padding: 10px;
  text-align: left;
  border-radius: 18px;
  background: var(--key);
  color: var(--key-ink);
  box-shadow: 0 4px 0 var(--key-shade);
  transition: transform 0.12s;
}
.topic:hover { transform: translateY(-2px); }
.topic:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--key-shade); }
.topic.last { outline: 3px solid var(--accent); }
.topic-sign {
  grid-row: span 2;
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 12px;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 22px;
  font-weight: 800;
}
.topic-name { font-weight: 700; font-size: 14px; line-height: 1.15; }
.topic-best { font-size: 12px; color: var(--muted); }

/* Times tables picker (× tab) */
.table-picker { display: grid; gap: 10px; margin-bottom: 14px; padding: 12px; border-radius: 18px; background: rgba(0, 0, 0, 0.05); }
.table-title { margin: 0; font-size: 14px; color: var(--muted); }
.table-chips { display: grid; grid-template-columns: repeat(5, 1fr); gap: 7px; }
.table-chip {
  height: 44px;
  border-radius: 14px;
  background: var(--key);
  color: var(--key-ink);
  font-size: 18px;
  font-weight: 800;
  box-shadow: 0 4px 0 var(--key-shade);
}
.table-chip.active { background: var(--accent); color: var(--accent-ink); box-shadow: 0 4px 0 var(--accent-shade); }
.table-chip:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--key-shade); }
.table-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.table-actions button { padding: 12px 8px; font-size: 16px; }
.table-actions button:disabled { opacity: 0.45; cursor: default; }
.table-actions .open-sheet { grid-column: 1 / -1; }

/* Play card */
.play { position: relative; display: flex; flex-direction: column; gap: 10px; }
/* A card too full scrolls; its rows never squeeze into one another. */
.play > * { flex-shrink: 0; }
.stats { display: flex; justify-content: space-between; font-weight: 700; color: var(--muted); min-height: 22px; }
.streak { color: var(--accent); }
.prompt-row { display: flex; align-items: center; gap: 8px; min-height: 40px; }
.prompt-row.empty { display: none; }
.prompt { flex: 1; font-weight: 800; font-size: 18px; color: var(--muted); }
.help-button {
  min-height: 40px;
  padding: 6px 14px;
  border-radius: 999px;
  background: var(--key);
  color: var(--key-ink);
  font-weight: 800;
  font-size: 15px;
  box-shadow: 0 3px 0 var(--key-shade);
}
.help-button:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--key-shade); }
.problem {
  display: grid;
  place-items: center;
  min-height: 1.3em;
  font-family: var(--title-font);
  font-size: clamp(40px, 9vw, 60px);
  font-weight: 800;
  line-height: 1.1;
}
.answer {
  min-height: 64px;
  display: grid;
  place-items: center;
  padding: 4px 12px;
  border-radius: 18px;
  background: rgba(255, 255, 255, 0.7);
  border: 3px dashed var(--accent);
  font-size: 40px;
  font-weight: 800;
}
.answer.empty { opacity: 0.6; }
.answer.fraction { min-height: 2.25em; }
.answer .frac { font-size: 0.85em; }
.answer .tok-neg { color: var(--accent); }
.feedback { min-height: 24px; text-align: center; font-weight: 800; font-size: 18px; }
.feedback.good { color: var(--good); }
.feedback.bad { color: var(--bad); }
.feedback.hint { color: var(--accent); }
.feedback.show { animation: pop-in 0.35s ease-out; }
.answer-wrap { position: relative; }
.points-pop {
  position: absolute;
  right: 16px;
  top: 50%;
  margin-top: -20px;
  font-size: 30px;
  font-weight: 900;
  color: var(--good);
  opacity: 0;
  pointer-events: none;
}
.points-pop.show { animation: float-up 0.9s ease-out; }
.pad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 9px; }
.key {
  height: 58px;
  border-radius: 16px;
  background: var(--key);
  color: var(--key-ink);
  font-size: 26px;
  font-weight: 800;
  box-shadow: 0 5px 0 var(--key-shade);
  transition: transform 0.06s;
}
.key:active { transform: translateY(4px); box-shadow: 0 1px 0 var(--key-shade); }
.key.ok { background: var(--accent); color: var(--accent-ink); box-shadow: 0 5px 0 var(--accent-shade); }
.key.back { font-size: 22px; }
.key.slash { background: var(--key-shade); font-size: 30px; }
.key.sign { color: var(--accent); font-size: 36px; }
.pad.wide { grid-template-columns: repeat(4, 1fr); }
.pad.wide .ok { grid-column: 1 / -1; }
.pad.wide .sign + .ok { grid-column: 2 / -1; }
.flash-good { animation: glow 0.5s ease-out; }
.shake { animation: shake 0.4s; }

/* Phones, and short windows where a long prompt and a 4-row pad must fit without scrolling. */
@media (max-width: 859px), (max-height: 760px) {
  .play { gap: 7px; }
  .key { height: 46px; }
  .pad.wide .ok { height: 42px; }
  .answer { min-height: 50px; font-size: 32px; }
  .prompt-row { min-height: 36px; }
  .prompt { font-size: 16px; }
  .feedback { min-height: 20px; font-size: 16px; }
}

/* Short phones: a size down and a pad row less, so the card fits without scrolling and the picture above grows. */
@media (max-width: 859px) and (max-height: 720px) {
  .side > .card.play { gap: 5px; padding: 12px; }
  .card.play .help-word { display: none; }
  .card.play .prompt { font-size: 14px; line-height: 1.25; }
  .card.play .problem { font-size: 32px; }
  .card.play .answer { min-height: 40px; font-size: 26px; }
  .card.play .answer.fraction { min-height: 2.25em; }
  .card.play .feedback { min-height: 18px; font-size: 14px; }
  .card.play .key { height: 38px; }
  /* A story or a fraction or signed pad needs the score's line. */
  .card.play:is(.storied, :has(.pad.wide)) .stats { display: none; }
  /* ⌫, 0 and ✓ stand beside the digits. */
  .pad:not(.wide) { grid-template-columns: repeat(4, 1fr); }
  .pad:not(.wide) :is(.back, .zero, .ok) { grid-column: 4; }
  .pad:not(.wide) .back { grid-row: 1; }
  .pad:not(.wide) .zero { grid-row: 2; }
  .pad:not(.wide) .ok { grid-row: 3; }
  /* On a fraction or signed pad ✓ stands in a column of its own. */
  .pad.wide { grid-template-columns: repeat(5, 1fr); }
  .card.play .pad.wide .ok { grid-column: 5; grid-row: 1 / span 3; height: auto; }
}

/* Short wide windows (laptops) need the card padding and problem size phones get too. */
@media (min-width: 860px) and (max-height: 760px) {
  .side > .card { padding: 16px; }
  .problem { font-size: 46px; }
}

/* Result card */
.result { text-align: center; display: grid; gap: 10px; }
.result-title { margin: 0; font-family: var(--title-font); font-size: 28px; }
.result-score { font-size: 72px; font-weight: 900; color: var(--accent); line-height: 1; }
.result-rows { color: var(--muted); font-weight: 700; display: grid; gap: 4px; }
.badge {
  justify-self: center;
  padding: 6px 14px;
  border-radius: 99px;
  background: #ffd23f;
  color: #4a3200;
  font-weight: 800;
  animation: pop-in 0.5s ease-out;
}
.big-button, .ghost-button { padding: 16px; border-radius: 18px; font-size: 20px; font-weight: 800; }
.big-button { background: var(--accent); color: var(--accent-ink); box-shadow: 0 5px 0 var(--accent-shade); }
.ghost-button { background: var(--key-shade); color: var(--key-ink); }

/* Token lines (core/tokens.js): the problem, the typed answer and the explanation */
.toks { display: inline-flex; align-items: center; gap: 0.24em; white-space: pre; }
.tok { position: relative; display: inline-flex; align-items: center; justify-content: center; border-radius: 0.2em; }
.tok-in { display: inline-flex; align-items: center; }
.tok.coef + .tok, .tok.open + .tok, .tok + .tok.close { margin-left: -0.2em; }
/* Token lines keep the text font: a title font (Cube Isle's pixel one) may lack the '−' glyph and fall back low. */
.tok-sign { font-style: normal; font-family: var(--font); color: var(--ink); }
.tok-neg { margin-right: 0.03em; }
.tok-bracket { color: var(--muted); font-weight: 600; }
.tok-unit { margin-left: 0.03em; }
.tok.x { color: var(--x); }
.tok.x, .tab.var { font-style: italic; font-family: Georgia, 'Times New Roman', serif; }
.tok.op, .tok.eq { color: var(--muted); }
.tok-box {
  display: inline-grid;
  place-items: center;
  min-width: 1em;
  height: 1.15em;
  padding: 0 0.12em;
  border: 0.06em dashed var(--accent);
  border-radius: 0.18em;
  color: var(--accent);
}
.frac { display: inline-flex; flex-direction: column; align-items: center; font-size: 0.72em; line-height: 1.05; }
.frac .tok-box { min-width: 1.2em; border-width: 0.09em; }
.frac-by { display: inline-flex; align-items: center; gap: 0.18em; white-space: nowrap; }
.tok-by { display: inline-flex; align-items: center; gap: 0.18em; color: var(--accent); }
.frac-bar { align-self: stretch; height: 0.1em; min-height: 2px; margin: 0.1em -0.1em; border-radius: 1em; background: currentColor; }
.tok-old { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; }

/* Explanation card (core/explain.js) */
/* minmax(0, 1fr): a too-wide token line must not stretch the card, or fitFont measures the stretched box. */
.explain { display: grid; grid-template-columns: minmax(0, 1fr); gap: 12px; text-align: center; }
/* A term growing at the top of its arc must not flash a scrollbar on the card. */
.side > .explain { overflow-x: hidden; }
.explain-title { margin: 0; font-family: var(--title-font); font-size: 22px; }
.explain-line {
  position: relative;
  display: grid;
  place-items: center;
  padding: 1.1em 8px 0.3em;
  border-radius: 20px;
  background: rgba(128, 128, 128, 0.12);
  font-family: var(--font);
  font-size: 52px;
  font-weight: 800;
  line-height: 1.15;
}
.explain-line .ghost { position: absolute; pointer-events: none; }
.tok.focus { background: var(--focus); box-shadow: 0 0 0 0.08em var(--focus); animation: focus-pulse 1s ease-in-out infinite alternate; }
.tok.cross { color: var(--flip); text-shadow: 0 0 0.25em var(--flip); }
.explain-say { margin: 0; min-height: 3em; font-size: 19px; font-weight: 700; line-height: 1.35; }
.explain-say.show { animation: pop-in 0.35s ease-out; }
.explain-dots { display: flex; justify-content: center; gap: 8px; }
.dot { width: 12px; height: 12px; border-radius: 50%; background: var(--key-shade); transition: background 0.3s, transform 0.3s; }
.dot.done { background: var(--accent); opacity: 0.5; }
.dot.now { background: var(--accent); transform: scale(1.35); }
.explain-buttons { display: grid; grid-template-columns: auto 1fr; gap: 10px; white-space: nowrap; }
.big-button.ready { animation: ready-pulse 0.9s ease-in-out 3 alternate; }

@media (max-width: 859px) {
  .explain { gap: 9px; }
  .explain-line { font-size: 40px; }
  .explain-say { font-size: 17px; min-height: 2.7em; }
  .big-button, .ghost-button { padding: 13px; font-size: 18px; }
}

@keyframes pop-in { from { transform: scale(0.6); opacity: 0; } to { transform: scale(1); opacity: 1; } }
@keyframes float-up { 0% { opacity: 0; transform: translateY(10px) scale(0.8); } 20% { opacity: 1; } 100% { opacity: 0; transform: translateY(-50px) scale(1.2); } }
@keyframes glow { 0% { box-shadow: 0 0 0 0 var(--good), var(--card-shadow); } 100% { box-shadow: 0 0 0 18px transparent, var(--card-shadow); } }
@keyframes shake { 20%, 60% { transform: translateX(-8px); } 40%, 80% { transform: translateX(8px); } }
@keyframes focus-pulse { to { box-shadow: 0 0 0 0.18em var(--focus); } }
@keyframes ready-pulse { to { transform: scale(1.06); } }
