:root {
  --cell: 56px;

  --desk: #c9ccd0;
  --desk-shade: #b9bdc2;
  --paper: #fbf5e3;
  --grid-line: #b7d1ea;
  --margin-line: #e2656c;
  --ink-print: #3b3f46;
  --ink-child: #1d46b8;
  --ink-scratch: #5b78c9;
  --focus-fill: rgba(255, 214, 102, 0.55);
  --focus-ring: #1d46b8;

  --font-hand: "Marck Script", "Segoe Print", "Bradley Hand", cursive;
  --font-ui: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

*, *::before, *::after { box-sizing: border-box; }

html, body { margin: 0; min-height: 100%; }

body {
  font-family: var(--font-ui);
  color: var(--ink-print);
  background:
    radial-gradient(ellipse at 50% 0%, rgba(255, 255, 255, 0.35), transparent 60%),
    var(--desk);
}

/* ---------- Sheet (notebook page) ---------- */

.ws-page {
  --pad: calc(var(--cell) / 2);
  position: relative;
  margin: 0 auto;
  width: calc((var(--cols) + 1) * var(--cell));
  height: calc((var(--rows) + 1) * var(--cell));
  padding: var(--pad);
  border-radius: 14px;
  background-color: var(--paper);
  /* grid lines coincide with the cell boundaries: they start at the padding edge */
  background-image:
    linear-gradient(to right, var(--grid-line) 1px, transparent 1px),
    linear-gradient(to bottom, var(--grid-line) 1px, transparent 1px);
  background-size: var(--cell) var(--cell);
  background-position: var(--pad) var(--pad);
  box-shadow:
    0 1px 1px rgba(40, 30, 10, 0.18),
    0 6px 12px -2px rgba(40, 30, 10, 0.18),
    0 22px 40px -12px rgba(40, 30, 10, 0.32);
  overflow: hidden;
}

/* red margin line, a little left of the first column */
.ws-page::before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: calc(var(--pad) - var(--cell) * 0.18);
  width: 2px;
  background: var(--margin-line);
  opacity: 0.75;
  pointer-events: none;
}

.ws-grid {
  display: grid;
  grid-template-columns: repeat(var(--cols), var(--cell));
  grid-template-rows: repeat(var(--rows), var(--cell));
  position: relative;
}

.ws-cell {
  min-width: 0;
  min-height: 0;
  font-family: var(--font-hand);
}

/* ---------- Printed cells ---------- */

.ws-static {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--cell) * 0.7);
  line-height: 1;
  color: var(--ink-print);
  user-select: none;
}

.ws-static.ws-word {
  justify-content: flex-end;
  padding-right: calc(var(--cell) * 0.06);
  font-size: calc(var(--cell) * 0.4);
  white-space: nowrap;
  color: var(--ink-scratch);
}

/* Vertical bar of the division corner: right edge of the cell */
.ws-bar::before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  right: -1px;
  width: 2px;
  background: var(--ink-print);
}
.ws-bar.ws-bar-end::before { bottom: 50%; margin-bottom: -1px; }

/* Rule: a line across the middle of its cell */
.ws-rule { position: relative; }
.ws-rule::before {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  top: 50%;
  height: 2px;
  margin-top: -1px;
  background: var(--ink-print);
}

/* ---------- Input cells ---------- */

.ws-input {
  appearance: none;
  -webkit-appearance: none;
  align-self: center;
  justify-self: center;
  width: calc(100% - 6px);
  height: calc(100% - 6px);
  margin: 0;
  padding: 0;
  text-align: center;
  font-size: calc(var(--cell) * 0.66);
  line-height: 1;
  color: var(--ink-child);
  background: transparent;
  border: 1px dotted rgba(29, 70, 184, 0.42);
  border-radius: 5px;
  caret-color: transparent;
  outline: none;
  cursor: text;
  transition: background-color 0.12s ease, box-shadow 0.12s ease;
}

.ws-input::selection { background: transparent; color: inherit; }

.ws-scratch {
  font-size: calc(var(--cell) * 0.5);
  color: var(--ink-scratch);
  border-color: rgba(29, 70, 184, 0.24);
}

.ws-small {
  width: 55%;
  height: 55%;
  font-size: calc(var(--cell) * 0.34);
  border-radius: 4px;
}

.ws-input:hover { background-color: rgba(255, 214, 102, 0.18); }

.ws-input:focus {
  background-color: var(--focus-fill);
  border-style: solid;
  border-color: var(--focus-ring);
  box-shadow: 0 0 0 2px rgba(29, 70, 184, 0.18);
}

@media (prefers-reduced-motion: reduce) {
  .ws-input { transition: none; }
}

/* ---------- Screens ---------- */

.app { min-height: 100vh; }

.card {
  background-color: var(--paper);
  background-image:
    linear-gradient(to right, var(--grid-line) 1px, transparent 1px),
    linear-gradient(to bottom, var(--grid-line) 1px, transparent 1px);
  background-size: 28px 28px;
  border-radius: 18px;
  box-shadow:
    0 1px 1px rgba(40, 30, 10, 0.18),
    0 6px 12px -2px rgba(40, 30, 10, 0.18),
    0 22px 40px -12px rgba(40, 30, 10, 0.32);
  position: relative;
  overflow: hidden;
}

/* red margin line of the notebook */
.card::before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: 44px;
  width: 2px;
  background: var(--margin-line);
  opacity: 0.6;
  pointer-events: none;
}

.card-title {
  margin: 0 0 8px;
  font-family: var(--font-hand);
  font-weight: 400;
  font-size: 44px;
  line-height: 1.1;
  color: var(--ink-child);
}

.field-label {
  margin: 28px 0 10px;
  font-size: 18px;
  color: var(--ink-print);
}

/* ---------- Buttons ---------- */

.btn {
  appearance: none;
  -webkit-appearance: none;
  font: inherit;
  font-size: 20px;
  font-weight: 600;
  padding: 14px 28px;
  min-height: 56px;
  border: 0;
  border-radius: 16px;
  cursor: pointer;
  color: #fff;
  background: var(--ink-child);
  box-shadow: 0 3px 0 rgba(15, 35, 100, 0.55), 0 8px 14px -6px rgba(15, 35, 100, 0.5);
  transition: transform 0.08s ease, box-shadow 0.08s ease, background-color 0.12s ease;
}
.btn:hover { background: #2754d4; }
.btn:active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 rgba(15, 35, 100, 0.55);
}
.btn:disabled { cursor: default; opacity: 0.7; }

.btn-secondary {
  color: var(--ink-child);
  background: #fff;
  box-shadow: inset 0 0 0 2px var(--ink-child), 0 3px 0 rgba(15, 35, 100, 0.25);
}
.btn-secondary:hover { background: #eef2fd; }
.btn-wide { width: 100%; margin-top: 36px; }

.btn:focus-visible,
.op-btn:focus-visible,
.chip:focus-visible,
.step-num:focus-visible,
.step-btn:focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 3px;
}

/* ---------- Start screen ---------- */

.app-start {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 32px 16px;
}

.start-card {
  width: min(520px, 100%);
  padding: 36px 40px 40px 68px;
}

.op-toggles {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
}

.op-btn {
  appearance: none;
  -webkit-appearance: none;
  font-family: var(--font-ui);
  font-size: 40px;
  line-height: 1;
  height: 76px;
  border-radius: 18px;
  cursor: pointer;
  color: var(--ink-print);
  background: #fff;
  border: 2px dashed rgba(29, 70, 184, 0.35);
  transition: background-color 0.12s ease, transform 0.08s ease;
}
.op-btn:hover { background: #eef2fd; }
.op-btn:active { transform: translateY(2px); }
.op-btn.is-on {
  color: #fff;
  background: var(--ink-child);
  border: 2px solid var(--ink-child);
  box-shadow: 0 3px 0 rgba(15, 35, 100, 0.55);
}
.op-btn.is-on:hover { background: #2754d4; }
.op-btn[aria-disabled="true"] { cursor: not-allowed; }

.opt-group[hidden] { display: none; }

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
}

.chip {
  appearance: none;
  -webkit-appearance: none;
  font-family: var(--font-ui);
  font-size: 20px;
  line-height: 1;
  padding: 14px 22px;
  border-radius: 14px;
  cursor: pointer;
  color: var(--ink-print);
  background: #fff;
  border: 2px dashed rgba(29, 70, 184, 0.35);
  transition: background-color 0.12s ease, transform 0.08s ease;
}
.chip:hover { background: #eef2fd; }
.chip:active { transform: translateY(2px); }
.chip.is-on {
  color: #fff;
  background: var(--ink-child);
  border: 2px solid var(--ink-child);
  box-shadow: 0 3px 0 rgba(15, 35, 100, 0.55);
}
.chip.is-on:hover { background: #2754d4; }
.chip[aria-disabled="true"] { cursor: not-allowed; }

.notice-slot { margin-top: 16px; }
.notice {
  margin: 0;
  padding: 12px 16px;
  border-radius: 12px;
  background: #fdf3e3;
  border: 1px solid rgba(160, 100, 20, 0.35);
  color: var(--ink-print);
  font-size: 17px;
}

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

.step-btn {
  appearance: none;
  -webkit-appearance: none;
  width: 60px;
  height: 60px;
  font-family: var(--font-ui);
  font-size: 34px;
  line-height: 1;
  border-radius: 50%;
  cursor: pointer;
  color: var(--ink-child);
  background: #fff;
  border: 2px solid var(--ink-child);
}
.step-btn:hover:not(:disabled) { background: #eef2fd; }
.step-btn:disabled {
  cursor: default;
  opacity: 0.35;
}

.step-num {
  width: 2.6ch;
  padding: 0;
  border: none;
  border-bottom: 2px dashed rgba(29, 70, 184, 0.45);
  border-radius: 0;
  background: transparent;
  text-align: center;
  font-family: var(--font-hand);
  font-size: 56px;
  line-height: 1;
  color: var(--ink-child);
}

/* ---------- Solving screen ---------- */

/* Nothing below the sheet: fitCell leaves exactly 24px under it, the page never scrolls. */
.app-solving { min-height: 0; }

.solve-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  padding: 12px 24px;
  height: 76px;
}

.solve-info {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.solve-title {
  font-size: 22px;
  font-weight: 600;
  line-height: 1;
}

.dots { display: flex; gap: 8px; }

.dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 2px solid var(--ink-child);
  background: transparent;
}
.dot.is-done { background: var(--ink-child); }
.dot.is-current {
  background: var(--focus-fill);
  box-shadow: 0 0 0 3px rgba(29, 70, 184, 0.2);
}

.progress {
  width: 220px;
  height: 10px;
  border-radius: 5px;
  border: 2px solid var(--ink-child);
  overflow: hidden;
}
.progress-fill {
  display: block;
  height: 100%;
  background: var(--ink-child);
}

.solve-bar .btn { padding: 12px 26px; min-height: 52px; }

.solve-sheet {
  display: block;
  width: 100%;
}

/* ---------- Result screen ---------- */

.app-result {
  display: flex;
  justify-content: center;
  padding: 32px 16px 48px;
}

.result-card {
  width: min(680px, 100%);
  padding: 36px 40px 40px 68px;
}

.score {
  font-size: 80px;
  font-weight: 700;
  line-height: 1;
  color: var(--ink-child);
}

.message {
  margin: 8px 0 24px;
  font-size: 26px;
  font-weight: 600;
}

.review {
  list-style: none;
  margin: 28px 0 0;
  padding: 0;
}

.review-row {
  display: grid;
  grid-template-columns: 1fr auto 28px;
  align-items: baseline;
  column-gap: 16px;
  padding: 10px 0;
  border-bottom: 1px dashed rgba(29, 70, 184, 0.3);
  font-size: 22px;
}

.review-problem { color: var(--ink-print); }
.review-answer {
  font-family: var(--font-hand);
  font-size: 28px;
  color: var(--ink-child);
}
.review-mark { font-size: 24px; font-weight: 700; text-align: center; }
.is-ok .review-mark { color: #2f8f4e; }
.is-bad .review-mark { color: #c0392b; }
.is-bad .review-answer:not(.is-empty) { text-decoration: line-through; text-decoration-color: rgba(192, 57, 43, 0.5); }

.review-correct {
  grid-column: 1 / -1;
  margin-top: 2px;
  font-size: 18px;
  color: var(--ink-print);
}
.review-correct b { color: #2f8f4e; }

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
}

@media (max-width: 560px) {
  .start-card, .result-card { padding: 28px 20px 28px 52px; }
  .card::before { left: 30px; }
}

@media (prefers-reduced-motion: reduce) {
  .btn, .op-btn, .chip { transition: none; }
}

/* The result card takes focus programmatically only; no outline around the whole card */
.result-card:focus { outline: none; }
