:root {
  color-scheme: light;
  --paper: #f6f2e8;
  --panel: #fffdf7;
  --ink: #172033;
  --muted: #5f6572;
  --line: #d7cdbb;
  --teal: #176b87;
  --teal-dark: #0f4d62;
  --coral: #c2415d;
  --mint: #dcefdc;
  --yellow: #ffcf5a;
  --green: #17824f;
  --red: #b83245;
  --shadow: 0 18px 38px rgba(23, 32, 51, 0.13);
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  background: var(--paper);
}

body {
  min-height: 100%;
  margin: 0;
  background:
    linear-gradient(rgba(23, 32, 51, 0.035) 1px, transparent 1px),
    linear-gradient(90deg, rgba(23, 32, 51, 0.035) 1px, transparent 1px),
    var(--paper);
  background-size: 34px 34px;
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  letter-spacing: 0;
  overscroll-behavior: none;
}

button,
input,
select {
  font: inherit;
  letter-spacing: 0;
}

button {
  -webkit-tap-highlight-color: transparent;
}

.app-shell {
  width: min(100%, 1366px);
  min-height: 100dvh;
  margin: 0 auto;
  padding: calc(18px + env(safe-area-inset-top)) calc(22px + env(safe-area-inset-right)) calc(22px + env(safe-area-inset-bottom)) calc(22px + env(safe-area-inset-left));
  display: grid;
  grid-template-rows: auto auto 1fr;
  gap: 18px;
}

.topbar,
.controls-panel,
.problem-panel,
.work-panel {
  background: rgba(255, 253, 247, 0.94);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.topbar {
  min-height: 104px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding: 18px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 16px;
}

.brand-mark {
  width: 64px;
  height: 64px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  align-items: center;
  justify-items: center;
  border: 2px solid var(--ink);
  border-radius: 8px;
  background: var(--yellow);
  color: var(--ink);
  font-size: 1.3rem;
  font-weight: 900;
}

.brand h1 {
  margin: 0;
  font-size: 2rem;
  line-height: 1;
}

.brand p,
.eyebrow,
.answer-label,
.select-field span,
.pen-size span {
  margin: 0;
  color: var(--muted);
  font-size: 0.92rem;
  font-weight: 800;
  text-transform: uppercase;
}

.score-strip {
  display: grid;
  grid-template-columns: repeat(3, minmax(86px, 1fr));
  gap: 10px;
  margin: 0;
}

.score-strip div {
  min-height: 66px;
  padding: 10px 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
}

.score-strip dt {
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 800;
  text-transform: uppercase;
}

.score-strip dd {
  margin: 3px 0 0;
  font-size: 1.9rem;
  font-weight: 900;
  line-height: 1;
}

.controls-panel {
  display: grid;
  grid-template-columns: 1.25fr 1fr 150px;
  gap: 14px;
  padding: 14px;
  align-items: end;
}

.segmented-group {
  min-width: 0;
  margin: 0;
  padding: 0;
  border: 0;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
}

.segmented-group legend {
  grid-column: 1 / -1;
  margin: 0 0 8px;
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 900;
  text-transform: uppercase;
}

.segmented-group label {
  min-width: 0;
}

.segmented-group input {
  position: absolute;
  inline-size: 1px;
  block-size: 1px;
  opacity: 0;
}

.segmented-group span,
.select-field select {
  width: 100%;
  min-height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  color: var(--ink);
  font-weight: 850;
  text-align: center;
}

.segmented-group input:focus-visible + span,
.select-field select:focus-visible,
button:focus-visible,
.answer-box:focus-within {
  outline: 4px solid rgba(255, 207, 90, 0.76);
  outline-offset: 2px;
}

.segmented-group input:checked + span {
  border-color: var(--teal);
  background: var(--teal);
  color: #ffffff;
}

.select-field {
  display: grid;
  gap: 8px;
}

.select-field select {
  appearance: none;
  padding: 0 38px 0 14px;
  background-image:
    linear-gradient(45deg, transparent 50%, var(--ink) 50%),
    linear-gradient(135deg, var(--ink) 50%, transparent 50%);
  background-position:
    calc(100% - 22px) 21px,
    calc(100% - 15px) 21px;
  background-size: 7px 7px, 7px 7px;
  background-repeat: no-repeat;
}

.practice-grid {
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(420px, 0.92fr) minmax(460px, 1.08fr);
  gap: 18px;
}

.problem-panel,
.work-panel {
  min-width: 0;
  min-height: 0;
  padding: 18px;
}

.problem-panel {
  display: grid;
  grid-template-rows: auto auto auto 1fr;
  gap: 18px;
}

.work-panel {
  display: grid;
  grid-template-rows: auto 1fr;
  gap: 14px;
}

.panel-heading {
  min-height: 54px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.panel-heading h2 {
  margin: 2px 0 0;
  font-size: 1.35rem;
  line-height: 1.1;
}

.icon-button {
  width: 48px;
  height: 48px;
  flex: 0 0 48px;
  display: inline-grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  color: var(--ink);
}

.icon-button svg {
  width: 24px;
  height: 24px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.equation-wrap {
  min-height: 178px;
  display: grid;
  place-items: center;
  border: 2px solid var(--ink);
  border-radius: 8px;
  background: #fff7df;
  padding: 18px;
}

.equation {
  width: 100%;
  text-align: center;
  font-size: 4.3rem;
  font-weight: 950;
  line-height: 1.06;
}

.division-layout {
  display: grid;
  grid-template-columns: auto 22px auto;
  align-items: center;
  gap: 2px;
  font-size: 4.3rem;
  font-weight: 950;
  line-height: 1;
}

.division-bracket {
  width: 22px;
  height: 72px;
  border-left: 5px solid var(--ink);
  border-top: 5px solid var(--ink);
  border-top-left-radius: 8px;
}

.answer-form {
  display: grid;
  gap: 10px;
}

.answer-box {
  position: relative;
  height: 128px;
  overflow: hidden;
  border: 2px solid var(--ink);
  border-radius: 8px;
  background:
    linear-gradient(transparent 72%, rgba(23, 32, 51, 0.22) 72%, rgba(23, 32, 51, 0.22) calc(72% + 3px), transparent calc(72% + 3px)),
    #ffffff;
  display: grid;
  align-items: center;
}

.answer-box input {
  position: absolute;
  inset: 0;
  width: 100%;
  min-width: 0;
  height: 100%;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--ink);
  opacity: 0;
  outline: 0;
  pointer-events: none;
}

#answerCanvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  cursor: crosshair;
}

.typed-answer-preview {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: var(--ink);
  font-size: 4.4rem;
  font-weight: 950;
  line-height: 1;
  pointer-events: none;
}

.result-text {
  min-height: 34px;
  margin: 0;
  color: var(--muted);
  font-size: 1.2rem;
  font-weight: 900;
}

.result-text.is-correct {
  color: var(--green);
}

.result-text.is-wrong {
  color: var(--red);
}

.action-row {
  display: grid;
  grid-template-columns: 1fr 1fr 1.05fr;
  gap: 10px;
}

.primary-button,
.secondary-button,
.ghost-button,
.number-pad button {
  min-height: 58px;
  border-radius: 8px;
  font-weight: 900;
}

.primary-button {
  border: 1px solid var(--teal-dark);
  background: var(--teal);
  color: #ffffff;
}

.secondary-button {
  border: 1px solid #73500c;
  background: var(--yellow);
  color: var(--ink);
}

.ghost-button {
  border: 1px solid var(--line);
  background: #ffffff;
  color: var(--ink);
}

.number-pad {
  align-self: end;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.number-pad button {
  border: 1px solid var(--line);
  background: #ffffff;
  color: var(--ink);
  font-size: 1.45rem;
}

.number-pad button:active,
.primary-button:active,
.secondary-button:active,
.ghost-button:active,
.icon-button:active,
.swatch:active {
  transform: translateY(1px);
}

.tool-strip {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  flex-wrap: wrap;
}

.swatch {
  width: 44px;
  height: 44px;
  padding: 5px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
}

.swatch span {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: 6px;
}

.swatch.is-active {
  border-color: var(--teal);
  box-shadow: 0 0 0 3px rgba(23, 107, 135, 0.18);
}

.pen-size {
  min-height: 44px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
}

.pen-size input {
  width: 92px;
  accent-color: var(--teal);
}

#workCanvas {
  width: 100%;
  height: 100%;
  min-height: 620px;
  display: block;
  border: 2px solid var(--ink);
  border-radius: 8px;
  background:
    linear-gradient(rgba(23, 32, 51, 0.08) 1px, transparent 1px),
    linear-gradient(90deg, rgba(23, 32, 51, 0.08) 1px, transparent 1px),
    #ffffff;
  background-size: 32px 32px;
  touch-action: none;
  cursor: crosshair;
}

body[data-result="correct"] .answer-box {
  border-color: var(--green);
  background:
    linear-gradient(transparent 72%, rgba(23, 130, 79, 0.24) 72%, rgba(23, 130, 79, 0.24) calc(72% + 3px), transparent calc(72% + 3px)),
    var(--mint);
}

body[data-result="wrong"] .answer-box {
  border-color: var(--red);
}

@media (orientation: portrait) {
  .app-shell {
    width: min(100%, 1024px);
  }

  .practice-grid {
    grid-template-columns: 1fr;
    grid-template-rows: auto minmax(520px, 1fr);
  }

  .problem-panel {
    grid-template-columns: 1fr 270px;
    grid-template-rows: auto auto auto;
    align-items: start;
  }

  .problem-panel .panel-heading,
  .problem-panel .equation-wrap,
  .problem-panel .answer-form {
    grid-column: 1;
  }

  .number-pad {
    grid-column: 2;
    grid-row: 1 / span 3;
    align-self: stretch;
  }

  .number-pad button {
    min-height: 70px;
  }

  #workCanvas {
    min-height: 560px;
  }
}

@media (max-width: 860px) {
  .app-shell {
    padding: calc(12px + env(safe-area-inset-top)) calc(12px + env(safe-area-inset-right)) calc(14px + env(safe-area-inset-bottom)) calc(12px + env(safe-area-inset-left));
    gap: 12px;
  }

  .topbar,
  .controls-panel {
    align-items: stretch;
  }

  .topbar {
    flex-direction: column;
  }

  .controls-panel {
    grid-template-columns: 1fr;
  }

  .segmented-group {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .practice-grid,
  .problem-panel {
    grid-template-columns: 1fr;
  }

  .problem-panel .panel-heading,
  .problem-panel .equation-wrap,
  .problem-panel .answer-form,
  .number-pad {
    grid-column: auto;
    grid-row: auto;
  }

  .score-strip {
    width: 100%;
  }

  .equation,
  .division-layout {
    font-size: 3.1rem;
  }

  .typed-answer-preview {
    font-size: 3.5rem;
  }

  .action-row {
    grid-template-columns: 1fr;
  }

  #workCanvas {
    min-height: 460px;
  }
}
