:root {
  --bg: #f8f4ef;
  --panel: #ffffff;
  --text: #2f251f;
  --muted: #6f6258;
  --line: #ddcec0;
  --brand: #c30000;
  --brand-soft: #f9dddd;
  --success: #2f7d4f;
  --success-soft: #e3f3e8;
  --danger: #b33632;
  --blue: #1d63d6;
  --blue-soft: #e4edfb;
  --carry: #7b3fa0;
  --carry-soft: #f1e8f7;
  --hl: #fff1b8;
  --cell: 56px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Avenir Next", "Segoe UI", sans-serif;
  color: var(--text);
  background: radial-gradient(circle at top right, #fff8f3, var(--bg) 50%);
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

h1, h2, p {
  margin: 0;
}

button {
  font: inherit;
  cursor: pointer;
}

.hidden {
  display: none !important;
}

/* ---------- Header ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 20px;
  border-bottom: 1px solid var(--line);
  background: linear-gradient(180deg, #fff, #fcf8f3);
  box-shadow: 0 4px 20px rgba(90, 52, 38, 0.08);
}

.brand-block {
  display: flex;
  align-items: center;
  gap: 14px;
  color: inherit;
  text-decoration: none;
}

.master-logo {
  width: 42px;
  height: 42px;
  object-fit: contain;
}

.topbar h1 {
  font-size: 1.3rem;
  color: var(--brand);
}

.topbar p {
  color: var(--muted);
  margin-top: 2px;
  font-size: 0.85rem;
}

/* ---------- Grade and skill screens ---------- */

.screen {
  flex: 1;
  width: 100%;
  max-width: 1100px;
  margin: 0 auto;
  padding: 24px 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.screen-title {
  font-size: 1.6rem;
  text-align: center;
  color: var(--text);
}

.grade-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 14px;
}

.grade-card,
.skill-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: 0 6px 18px rgba(90, 52, 38, 0.06);
  text-align: left;
  color: var(--text);
  transition: transform 0.15s ease, border-color 0.15s ease;
}

.grade-card:hover,
.skill-card:hover {
  border-color: var(--brand);
  transform: translateY(-2px);
}

.grade-card {
  padding: 20px 18px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-height: 120px;
}

.grade-card .grade-name {
  font-size: 1.5rem;
  font-weight: 800;
  color: var(--brand);
}

.grade-card .grade-desc {
  font-size: 0.95rem;
  color: var(--muted);
  line-height: 1.35;
}

.skill-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 14px;
}

.skill-card {
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.code-chip {
  align-self: flex-start;
  display: inline-block;
  padding: 2px 10px;
  border-radius: 999px;
  background: var(--brand-soft);
  color: var(--brand);
  font-weight: 800;
  font-size: 0.8rem;
  letter-spacing: 0.02em;
}

.skill-card .skill-name {
  font-size: 1.2rem;
  font-weight: 800;
}

.skill-card .skill-standard {
  font-size: 0.9rem;
  color: var(--muted);
  line-height: 1.4;
}

.skill-card .skill-example {
  font-size: 1rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.back-btn {
  align-self: flex-start;
  background: none;
  border: 1px solid var(--line);
  color: var(--text);
  padding: 8px 16px;
  min-height: 44px;
  border-radius: 999px;
  font-weight: 700;
}

.back-btn:hover {
  border-color: var(--brand);
  color: var(--brand);
}

/* ---------- Work screen layout ---------- */

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 16px;
  width: 100%;
  max-width: 1280px;
  margin: 0 auto;
  padding: 16px 20px;
}

.board-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 16px 12px;
  box-shadow: 0 6px 18px rgba(90, 52, 38, 0.06);
}

/* The board's digit size is picked in script.js (fitBoard) to fill this area. */
.board-area {
  width: 100%;
  min-height: 240px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.side-panel {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.panel-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 16px;
  box-shadow: 0 6px 18px rgba(90, 52, 38, 0.06);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* benchmark line at the top of the problem area */
.work-head {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.back-btn.small {
  min-height: 36px;
  padding: 4px 12px;
  font-size: 0.9rem;
}

.work-title {
  font-weight: 800;
  font-size: 0.95rem;
}

.level-title {
  font-weight: 700;
  font-size: 0.85rem;
  color: var(--muted);
  margin-bottom: 6px;
}

.choice-row {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px;
}

.choice-btn {
  background: #fff;
  border: 1px solid var(--line);
  color: var(--text);
  padding: 6px 4px;
  min-height: 44px;
  border-radius: 12px;
  font-weight: 700;
  font-size: 0.9rem;
  line-height: 1.15;
  text-align: center;
}

.choice-btn.active {
  background: var(--brand);
  border-color: var(--brand);
  color: #fff;
}

/* ---------- The problem board ---------- */

.board {
  display: grid;
  grid-template-columns: var(--col-sizes);
  justify-content: center;
  row-gap: 4px;
}

.cell {
  height: var(--cell);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: calc(var(--cell) * 0.62);
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

.cell.sign {
  color: var(--muted);
}

/* the decimal point gets a narrow column of its own so digits line up */
.cell.point {
  font-size: calc(var(--cell) * 0.62);
}

.rule {
  grid-column: 1 / -1;
  height: 0;
  border-top: 3px solid var(--text);
  margin: 4px 0;
}

.cell.factor {
  border-radius: 10px;
  transition: background 0.2s ease;
}

/* the column being added */
.cell.factor.hl {
  background: var(--hl);
  box-shadow: inset 0 0 0 2px #e6c84f;
}

/* answer boxes */
.ans-box {
  width: calc(var(--cell) - 6px);
  height: calc(var(--cell) - 6px);
  border: 2px dashed var(--line);
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text);
  background: #fffdfa;
}

.ans-box.filled {
  border: 2px solid transparent;
  background: transparent;
}

.ans-box.active {
  border: 3px solid var(--blue);
  background: var(--blue-soft);
  animation: pulse 1.4s ease-in-out infinite;
}

.ans-box.wrong {
  border-color: var(--danger) !important;
  background: #fbe9e8 !important;
  color: var(--danger);
  animation: shake 0.35s ease;
}

/* regroup boxes */
.cell.carry-cell {
  height: calc(var(--cell) * 0.72);
  align-items: flex-end;
}

/* placeholder zeros typed in to line up decimal places */
.cell.fill-zero {
  color: var(--muted);
}

/* the decimal point's box is as narrow as its column */
.ans-box.point-box {
  width: calc(var(--cell) * 0.4);
  border-radius: 6px;
}

.think-line .tag.line {
  background: var(--blue);
}

/* a top digit that was regrouped (subtracting) */
.cell.factor.crossed {
  color: var(--muted);
  text-decoration: line-through;
  text-decoration-thickness: 3px;
  text-decoration-color: var(--text);
}

/* regroup boxes grow to fit two digits (12) */
.carry-box {
  min-width: calc(var(--cell) * 0.62);
  padding: 0 calc(var(--cell) * 0.05);
  white-space: nowrap;
  height: calc(var(--cell) * 0.62);
  border: 2px solid var(--carry);
  border-radius: 8px;
  background: var(--carry-soft);
  color: var(--carry);
  font-size: calc(var(--cell) * 0.42);
  display: flex;
  align-items: center;
  justify-content: center;
  animation: pop-in 0.35s ease;
}

.carry-box.active {
  border-width: 3px;
  animation: pop-in 0.35s ease, pulse 1.4s ease-in-out 0.35s infinite;
}

.carry-box.wrong {
  border-color: var(--danger);
  background: #fbe9e8;
  color: var(--danger);
  animation: shake 0.35s ease;
}

.carry-box.in-use {
  box-shadow: 0 0 0 3px var(--hl), 0 0 0 5px #e6c84f;
}

.carry-box.done {
  opacity: 0.35;
  text-decoration: line-through;
  text-decoration-thickness: 2px;
  animation: none;
}

@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(29, 99, 214, 0.35); }
  50% { box-shadow: 0 0 0 6px rgba(29, 99, 214, 0); }
}

@keyframes pop-in {
  0% { transform: scale(0.2); opacity: 0; }
  70% { transform: scale(1.15); opacity: 1; }
  100% { transform: scale(1); }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-5px); }
  75% { transform: translateX(5px); }
}

.think-line .tag {
  display: inline-block;
  padding: 1px 10px;
  border-radius: 999px;
  font-size: 0.85rem;
  margin-right: 6px;
  vertical-align: 2px;
  background: var(--success);
  color: #fff;
}

.think-line .tag.regroup {
  background: var(--carry);
}

/* A hint or "You did it!" takes the step line's place, so the board never jumps. */
.feedback {
  font-weight: 700;
  text-align: center;
  border-radius: 10px;
  font-size: 1.05rem;
  width: 100%;
  max-width: 440px;
  height: 4rem;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1.3;
  padding: 4px 12px;
}

.feedback:empty {
  display: none;
}

.board-panel:has(.feedback:not(:empty)) .think-line {
  display: none;
}

.feedback.correct {
  color: var(--success);
  background: var(--success-soft);
  animation: feedback-pop 0.5s ease;
}

.feedback.incorrect {
  color: var(--danger);
  background: #fbe9e8;
}

@keyframes feedback-pop {
  0% { transform: scale(0.7); opacity: 0; }
  60% { transform: scale(1.08); opacity: 1; }
  100% { transform: scale(1); }
}

/* ---------- Keypad ---------- */

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

.key-btn {
  min-height: 54px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: #fffdfa;
  font-size: 1.6rem;
  font-weight: 800;
  color: var(--text);
  box-shadow: 0 3px 0 var(--line);
  touch-action: manipulation;
}

.key-btn:active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 var(--line);
}

/* the decimal point key gets its own full-width row (decimal problems only) */
.key-btn.point-key {
  grid-column: 1 / -1;
  min-height: 44px;
  line-height: 0.6;
  font-size: 2.2rem;
}

.keypad.locked .key-btn {
  opacity: 0.4;
  pointer-events: none;
}

.practice-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  align-items: center;
}

.primary.success {
  background: var(--success);
}

.text-link-btn {
  background: none;
  border: none;
  color: var(--muted);
  text-decoration: underline;
  font-size: 0.9rem;
  padding: 6px;
}

.score-line {
  text-align: center;
  color: var(--muted);
  font-size: 0.9rem;
}

.think-line {
  height: 4rem;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  text-align: center;
  font-size: 1.15rem;
  font-weight: 700;
  max-width: 34ch;
  line-height: 1.4;
}

.primary {
  border: none;
  border-radius: 999px;
  padding: 14px 24px;
  min-height: 52px;
  font-weight: 800;
  font-size: 1.1rem;
  color: #fff;
  background: var(--brand);
  width: 100%;
}

.skal-tip {
  text-align: center;
  color: var(--muted);
  font-size: 0.8rem;
}

.skal-tip strong {
  color: var(--brand);
}

.site-footer {
  text-align: center;
  padding: 6px 20px 10px;
  color: var(--muted);
  font-size: 0.75rem;
}

.site-footer a {
  color: var(--brand);
}

/* Big enough screens (laptops, tablets, smart boards): the work screen fits on
   one screen, board on the left and panel on the right. */
@media (min-width: 760px) and (min-height: 560px) {
  body.working {
    height: 100vh;
    height: 100dvh;
  }
  .layout {
    flex: 1;
    min-height: 0;
    grid-template-columns: minmax(0, 1fr) clamp(280px, 30vw, 360px);
    grid-template-rows: minmax(0, 1fr);
  }
  .board-panel {
    min-height: 0;
  }
  .board-area {
    flex: 1;
    min-height: 0;
  }
  .side-panel {
    min-height: 0;
    overflow-y: auto;
  }
}

/* Phones (and very short screens): the board is stacked above the panel, so
   the number pad (and Next Problem) sit right under the board where thumbs are. */
@media (max-width: 759px), (max-height: 559px) {
  #keypadSlot {
    order: -2;
  }
  .practice-actions {
    order: -1;
  }
  .think-line,
  .feedback {
    height: 3.6rem;
    font-size: 1rem;
    line-height: 1.35;
  }
}

@media (max-width: 600px) {
  .key-btn {
    min-height: 48px;
  }
  .topbar {
    padding: 6px 16px;
  }
  .topbar p {
    display: none;
  }
  .master-logo {
    width: 34px;
    height: 34px;
  }
  .topbar h1 {
    font-size: 1.15rem;
  }
  .screen {
    padding: 16px;
    gap: 12px;
  }
  .screen-title {
    font-size: 1.35rem;
  }
  .grade-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
  }
  .grade-card {
    padding: 14px;
    min-height: 0;
  }
  .grade-card .grade-name {
    font-size: 1.25rem;
  }
  .grade-card .grade-desc {
    font-size: 0.85rem;
  }
  .layout {
    padding: 12px 16px 16px;
    gap: 12px;
  }
  .board-area {
    min-height: 180px;
  }
}

/* ---------- Teach Me ---------- */

.mute-btn {
  flex-shrink: 0;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--panel);
  font-size: 1.3rem;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.mute-btn:hover {
  border-color: var(--brand);
}

.mode-tabs {
  display: flex;
  gap: 8px;
  border-bottom: 1px solid var(--line);
}

.mode-tab {
  flex: 1;
  background: none;
  border: none;
  padding: 10px 12px;
  min-height: 48px;
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--muted);
  border-bottom: 3px solid transparent;
}

.mode-tab.active {
  color: var(--brand);
  border-bottom-color: var(--brand);
}

.teach-dots {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}

.teach-dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: none;
  background: var(--line);
  padding: 0;
}

.teach-dot.active {
  background: var(--brand);
  transform: scale(1.25);
}

.teach-dot.done {
  background: var(--success);
}

.teach-caption {
  text-align: center;
  font-size: 1.05rem;
  font-weight: 600;
  line-height: 1.5;
}

.teach-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.speak-btn {
  flex: 1;
  background: var(--panel);
  border: 1px solid var(--line);
  color: var(--text);
  padding: 12px 16px;
  min-height: 52px;
  border-radius: 999px;
  font-weight: 600;
}

.speak-btn:hover {
  border-color: var(--brand);
  color: var(--brand);
}

.ghost {
  background: none;
  border: 1px solid var(--line);
  color: var(--text);
  padding: 12px 16px;
  min-height: 52px;
  border-radius: 999px;
  font-weight: 700;
}

.ghost:disabled {
  opacity: 0.35;
  cursor: default;
}

/* the number sentence under a worked example */
.demo-math {
  display: inline-block;
  padding: 4px 16px;
  border-radius: 999px;
  background: var(--hl);
  font-size: 1.15rem;
  line-height: 1.3;
  font-weight: 800;
  animation: feedback-pop 0.5s ease;
}

/* Short laptop and projector screens: trim the side panel so the number pad
   and the lesson's Next button fit without scrolling. */
@media (min-width: 760px) and (max-height: 720px) {
  .layout {
    padding-top: 10px;
    padding-bottom: 10px;
    gap: 12px;
  }
  .side-panel {
    gap: 8px;
  }
  .panel-card {
    padding: 12px;
    gap: 8px;
  }
  .mode-tab {
    min-height: 40px;
    padding: 6px 12px;
  }
  .choice-btn {
    min-height: 38px;
  }
  .key-btn {
    min-height: 44px;
  }
  .key-btn.point-key {
    min-height: 34px;
  }
  .ghost,
  .speak-btn {
    min-height: 44px;
    padding: 8px 14px;
  }
  .teach-caption {
    font-size: 0.98rem;
    line-height: 1.4;
  }
  .board-panel {
    padding: 10px 12px;
    gap: 4px;
  }
}

/* Phones: the lesson's Back/Play/Next and the number pad sit right under the
   tabs, where thumbs (and eyes) are. */
@media (max-width: 759px), (max-height: 559px) {
  .teach-nav {
    order: -3;
  }
  #teachKeypadSlot {
    order: -2;
  }
  .teach-caption {
    font-size: 1rem;
  }
}

@media (max-width: 600px) {
  .mute-btn {
    width: 36px;
    height: 36px;
    font-size: 1.1rem;
  }
  .mode-tab {
    min-height: 40px;
    padding: 6px 12px;
  }
  .ghost,
  .speak-btn {
    min-height: 44px;
    padding: 8px 12px;
  }
  /* phones keep the back button and benchmark code on one line; the name
     would push the number pad under Safari's bottom bar */
  .work-title {
    display: none;
  }
}
