:root {
  color-scheme: light;
  --ink: #171717;
  --muted: #64615d;
  --paper: #fffdf8;
  --line: #202020;
  --soft-line: #d8d1c5;
  --desk: #ece6dc;
  --accent: #0e6f68;
  --accent-strong: #07534e;
  --correct: #dff3e3;
  --correct-line: #188350;
  --wrong: #ffe2dd;
  --wrong-line: #c74235;
  --gold: #f3c24f;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background:
    linear-gradient(90deg, rgba(0, 0, 0, 0.025) 1px, transparent 1px) 0 0 / 34px 34px,
    linear-gradient(#f7f3ec, var(--desk));
  color: var(--ink);
  font-family: "Avenir Next", Avenir, "Trebuchet MS", Arial, sans-serif;
}

button,
input,
select {
  font: inherit;
}

.app-shell {
  display: grid;
  grid-template-columns: minmax(680px, 1fr) minmax(300px, 380px);
  gap: 22px;
  width: min(1380px, 100%);
  margin: 0 auto;
  padding: 20px;
}

.worksheet-panel,
.score-panel {
  min-width: 0;
}

.topbar {
  display: grid;
  grid-template-columns: minmax(160px, 1fr) auto minmax(310px, 1.1fr);
  gap: 14px;
  align-items: end;
  margin-bottom: 14px;
}

label,
.timer-label {
  display: block;
  margin-bottom: 5px;
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 700;
}

input,
select {
  width: 100%;
  min-height: 46px;
  border: 2px solid #c9c0b4;
  border-radius: 7px;
  background: #fffefa;
  padding: 8px 12px;
}

.timer-card {
  min-width: 178px;
  border: 2px solid var(--ink);
  border-radius: 8px;
  background: var(--paper);
  padding: 9px 18px;
  text-align: center;
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.08);
}

#timer {
  display: block;
  font-size: 2.5rem;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

.controls {
  display: grid;
  grid-template-columns: 1fr 112px;
  gap: 8px;
  align-items: end;
}

.controls label {
  grid-column: 1 / -1;
  margin-bottom: -3px;
}

button {
  min-height: 46px;
  border: 0;
  border-radius: 7px;
  padding: 9px 16px;
  font-weight: 800;
  cursor: pointer;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.primary-btn,
.done-btn {
  background: var(--accent);
  color: white;
}

.primary-btn:active,
.done-btn:active {
  background: var(--accent-strong);
}

.quiet-btn {
  border: 2px solid #bfb6aa;
  background: #fffefa;
  color: var(--ink);
}

.paper {
  width: min(100%, 900px);
  margin: 0 auto;
  background: var(--paper);
  border: 1px solid #eee7dc;
  box-shadow: 0 12px 35px rgba(59, 43, 23, 0.18);
  padding: 28px 34px 24px;
}

.paper-head {
  display: grid;
  grid-template-columns: 1fr 2fr 1fr;
  align-items: end;
  margin-bottom: 20px;
}

.date-line {
  font-weight: 800;
  white-space: nowrap;
}

.date-line span {
  display: inline-block;
  width: 140px;
  border-bottom: 2px solid var(--ink);
  transform: translateY(-3px);
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  grid-column: 2;
  text-align: center;
  font-size: 1.55rem;
  letter-spacing: 0;
}

.exercise-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  border-top: 2px solid var(--line);
  border-left: 2px solid var(--line);
}

.exercise-cell {
  position: relative;
  min-height: 84px;
  border-right: 2px solid var(--line);
  border-bottom: 2px solid var(--line);
  padding: 8px 8px 7px;
  background: rgba(255, 255, 255, 0.26);
}

.exercise-cell.correct {
  background: var(--correct);
}

.exercise-cell.wrong {
  background: var(--wrong);
}

.equation-row {
  display: grid;
  grid-template-columns: auto minmax(78px, 1fr);
  align-items: center;
  gap: 8px;
}

.equation {
  font-size: 1.4rem;
  font-weight: 800;
  white-space: nowrap;
}

.answer-pad {
  position: relative;
  min-height: 58px;
  border-bottom: 2px solid #746d65;
  background:
    linear-gradient(transparent 70%, rgba(14, 111, 104, 0.07)),
    rgba(255, 255, 255, 0.35);
}

.answer-canvas {
  display: block;
  width: 100%;
  height: 58px;
  touch-action: none;
  cursor: crosshair;
}

.clear-pad {
  position: absolute;
  right: -3px;
  top: -8px;
  display: grid;
  place-items: center;
  width: 28px;
  min-height: 28px;
  height: 28px;
  border: 1px solid #c7beb2;
  border-radius: 50%;
  background: #fffefa;
  color: var(--muted);
  padding: 0;
  font-size: 1rem;
  line-height: 1;
  opacity: 0;
  pointer-events: none;
}

.has-ink .clear-pad,
.answer-pad:focus-within .clear-pad,
.exercise-cell:hover .clear-pad {
  opacity: 1;
  pointer-events: auto;
}

.clear-pad:disabled {
  opacity: 0;
  pointer-events: none;
}

.mark {
  position: absolute;
  right: 9px;
  top: 8px;
  font-size: 1.4rem;
  font-weight: 900;
}

.correct .mark {
  color: var(--correct-line);
}

.wrong .mark {
  color: var(--wrong-line);
}

.correction {
  display: none;
  margin-top: 3px;
  color: var(--wrong-line);
  font-size: 0.88rem;
  font-weight: 800;
}

.wrong .correction {
  display: block;
}

.paper-foot {
  display: flex;
  flex-wrap: wrap;
  gap: 18px 46px;
  padding-top: 16px;
  font-weight: 800;
}

.paper-foot span {
  display: inline-block;
  min-width: 64px;
  border-bottom: 2px solid var(--ink);
  text-align: center;
  color: var(--accent);
}

.action-row {
  display: flex;
  justify-content: center;
  gap: 12px;
  margin-top: 14px;
}

.done-btn {
  min-width: 180px;
  min-height: 58px;
  font-size: 1.35rem;
}

.score-panel {
  display: grid;
  align-content: start;
  gap: 14px;
}

.result-card {
  border: 1px solid var(--soft-line);
  border-radius: 8px;
  background: rgba(255, 253, 248, 0.92);
  padding: 16px;
  box-shadow: 0 5px 18px rgba(59, 43, 23, 0.08);
}

h2 {
  margin-bottom: 12px;
  font-size: 1.2rem;
}

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

.summary-item {
  border: 1px solid #ddd4c7;
  border-radius: 7px;
  padding: 10px;
  background: white;
}

.summary-item span {
  display: block;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 800;
}

.summary-item strong {
  display: block;
  margin-top: 4px;
  font-size: 1.35rem;
}

.empty-state {
  color: var(--muted);
  line-height: 1.45;
}

#progressChart {
  display: block;
  width: 100%;
  height: auto;
  background: white;
  border: 1px solid #e5ded4;
  border-radius: 7px;
}

.leaderboard {
  display: grid;
  gap: 8px;
  margin: 0;
  padding-left: 24px;
}

.leaderboard li {
  padding: 8px 0 8px 4px;
  border-bottom: 1px solid #e7ded2;
}

.run-main {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  font-weight: 900;
}

.run-meta {
  margin-top: 3px;
  color: var(--muted);
  font-size: 0.85rem;
}

.celebration {
  position: relative;
  overflow: hidden;
  border-radius: 8px;
  background: #fff7d7;
  border: 2px solid var(--gold);
  padding: 14px 16px;
  font-weight: 900;
}

.celebration::after {
  content: "";
  position: absolute;
  inset: -60%;
  background:
    radial-gradient(circle, #0e6f68 0 3px, transparent 4px),
    radial-gradient(circle, #c74235 0 3px, transparent 4px),
    radial-gradient(circle, #f3c24f 0 4px, transparent 5px);
  background-size: 46px 42px, 58px 54px, 70px 62px;
  animation: sparkle 1.4s linear infinite;
  opacity: 0.18;
}

@keyframes sparkle {
  to {
    transform: translate3d(28px, 32px, 0) rotate(8deg);
  }
}

.is-running .paper {
  box-shadow: 0 12px 35px rgba(14, 111, 104, 0.22);
}

@media (max-width: 1050px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .score-panel {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .celebration {
    grid-column: 1 / -1;
  }
}

@media (max-width: 760px) {
  .app-shell {
    padding: 10px;
  }

  .topbar,
  .score-panel {
    grid-template-columns: 1fr;
  }

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

  .paper {
    padding: 18px 12px;
  }

  .paper-head {
    grid-template-columns: 1fr;
    gap: 14px;
  }

  h1 {
    grid-column: auto;
    font-size: 1.32rem;
  }

  .exercise-cell {
    min-height: 92px;
    padding: 7px 5px;
  }

  .equation-row {
    grid-template-columns: 1fr;
    gap: 2px;
  }

  .equation {
    font-size: 1rem;
  }

  .answer-pad,
  .answer-canvas {
    min-height: 54px;
    height: 54px;
  }
}
