/* Problem tools: scratch paper (public/scratchpad.js, .scratch-*), read aloud (public/speech.js,
   .speak-btn), the "no connection" page and the iOS install hint. Uses the tokens of style.css. */

/* ------------------------------------------------- buttons in the problem head */

.problem-tools {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-left: auto;
}

.tool-btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-height: 44px;
  min-width: 44px;
  padding: 0 14px 0 10px;
  border: 2px solid var(--grid-strong);
  border-radius: 999px;
  background: var(--paper-raised);
  color: var(--ink);
  font-weight: 800;
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.tool-btn[hidden] {
  display: none;
}

.tool-btn:hover {
  border-color: var(--ink);
}

/* "on" looks like a word gone over with the highlighter */
.tool-btn[aria-pressed="true"],
.tool-btn[aria-expanded="true"] {
  border-color: var(--ink);
  background: var(--marker-soft);
}

.speak-btn.is-speaking {
  color: var(--red);
  border-color: var(--red);
}

.tool-icon {
  display: inline-flex;
  width: 22px;
  height: 22px;
  flex: none;
}

.tool-icon svg,
.scratch-icon svg {
  display: block;
  width: 100%;
  height: 100%;
}

/* ------------------------------------------------------------ scratch paper */

/* a loose sheet laid on the notebook, held down with two strips of tape */
.scratch {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-width: 48rem;
  margin-top: 24px;
  padding: 12px 16px 16px;
  background: var(--paper-raised);
  border: 2px solid var(--grid-strong);
  border-radius: 5px 9px 7px 11px;
  box-shadow: var(--shadow);
}

.scratch[hidden] {
  display: none;
}

.scratch::before,
.scratch::after {
  content: "";
  position: absolute;
  top: -12px;
  width: 84px;
  height: 24px;
  background: var(--tape);
  border: 1px solid rgba(0, 0, 0, 0.05);
  pointer-events: none;
}

.scratch::before {
  left: 18px;
  transform: rotate(-4deg);
}

.scratch::after {
  right: 18px;
  transform: rotate(3deg);
}

.scratch-grip {
  display: none;
}

.scratch-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.scratch-title {
  font-family: var(--font-hand);
  font-weight: 700;
  font-size: 1.85rem;
  line-height: 1.1;
  color: var(--ink);
}

.scratch-close {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 44px;
  padding: 0 14px 0 10px;
  border: 2px solid var(--grid-strong);
  border-radius: 999px;
  background: var(--paper-raised);
  color: var(--pencil);
  font-weight: 800;
  font-size: 1rem;
  cursor: pointer;
}

.scratch-close:hover {
  color: var(--ink);
  border-color: var(--ink);
}

.scratch-close .scratch-icon {
  width: 18px;
  height: 18px;
}

.scratch-tools {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 6px 16px;
}

.scratch-group {
  display: flex;
  gap: 4px;
}

.scratch-tool {
  display: inline-grid;
  place-items: center;
  width: 44px;
  height: 44px;
  padding: 0;
  border: 2px solid transparent;
  border-radius: 50%;
  background: none;
  color: var(--pencil);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.scratch-tool:hover:not(:disabled) {
  background: var(--grid-line);
}

.scratch-tool[aria-pressed="true"] {
  border-color: currentColor;
  background: var(--marker-soft);
}

.scratch-tool:disabled {
  opacity: 0.35;
  cursor: default;
}

.scratch-pen {
  color: var(--ink);
}

.scratch-red {
  color: var(--red);
}

.scratch-pencil {
  color: var(--pencil);
}

.scratch-eraser[aria-pressed="true"] {
  color: var(--text);
}

.scratch-icon,
.scratch-swatch {
  display: inline-flex;
  width: 24px;
  height: 24px;
}

/* the writing surface: squared paper drawn by CSS, the ink lives on the canvas above it */
.scratch-paper {
  position: relative;
  height: clamp(280px, 44vh, 420px);
  overflow: hidden;
  border: 1px solid var(--grid-strong);
  border-radius: 3px;
  background-color: var(--paper);
  background-image:
    linear-gradient(var(--grid-line) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid-line) 1px, transparent 1px);
  background-size: var(--cell) var(--cell);
  background-position: -1px -1px;
}

.scratch-canvas {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: crosshair;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

.scratch[data-tool="eraser"] .scratch-canvas {
  cursor: cell;
}

.scratch-hint {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  text-align: center;
  font-family: var(--font-hand);
  font-weight: 500;
  font-size: 1.6rem;
  line-height: 1.2;
  color: var(--muted);
  pointer-events: none;
}

.scratch-hint[hidden] {
  display: none;
}

/* wide screens, single-column views (practice, daily problem, no-help test): the paper sits
   beside the problem and stays in view while the child scrolls through the answer fields.
   In a normal test the right column holds the help notes, so the paper stays below there. */
@media (min-width: 1100px) {
  .practice:has(.scratch:not([hidden])),
  .daily:has(.scratch:not([hidden])) {
    max-width: none;
  }

  .practice .problem:has(> .scratch:not([hidden])),
  .daily .problem:has(> .scratch:not([hidden])),
  .game-body.is-nohelp .problem:has(> .scratch:not([hidden])) {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(360px, 460px);
    grid-template-rows: auto auto auto auto 1fr;
    column-gap: 44px;
    align-items: start;
  }

  .problem:has(> .scratch:not([hidden])) > * {
    grid-column: 1;
  }

  .practice .problem > .scratch,
  .daily .problem > .scratch,
  .game-body.is-nohelp .problem > .scratch {
    grid-column: 2;
    grid-row: 1 / 6;
    position: sticky;
    top: 84px;
    margin-top: 6px;
  }

  .practice .problem > .scratch .scratch-paper,
  .daily .problem > .scratch .scratch-paper,
  .game-body.is-nohelp .problem > .scratch .scratch-paper {
    height: clamp(320px, calc(100vh - 290px), 560px);
  }
}

/* phones: the paper is a bottom sheet over the page; the page gets room to scroll above it */
@media (max-width: 600px) {
  .scratch {
    position: fixed;
    z-index: 30;
    left: 0;
    right: 0;
    bottom: 0;
    height: 62vh;
    height: min(62dvh, 600px);
    max-width: none;
    margin: 0;
    padding: 6px 12px calc(12px + env(safe-area-inset-bottom));
    gap: 8px;
    border-width: 2px 0 0;
    border-color: var(--ink);
    border-radius: 18px 18px 0 0;
    box-shadow: 0 -14px 32px -10px rgba(20, 30, 60, 0.35);
    animation: scratch-sheet-up 0.22s ease-out;
  }

  .scratch::before,
  .scratch::after {
    display: none;
  }

  .scratch-grip {
    display: block;
    width: 46px;
    height: 5px;
    margin: 0 auto;
    border-radius: 3px;
    background: var(--grid-strong);
  }

  .scratch-title {
    font-size: 1.6rem;
  }

  .scratch-close {
    color: var(--ink);
    border-color: var(--ink);
  }

  .scratch-paper {
    flex: 1;
    height: auto;
    min-height: 0;
  }

  body.has-scratch-sheet .page {
    padding-bottom: calc(62vh + 24px);
    padding-bottom: calc(min(62dvh, 600px) + 24px);
  }

  body.has-scratch-sheet .corner-mascot {
    display: none;
  }
}

@keyframes scratch-sheet-up {
  from {
    transform: translateY(100%);
  }
}

@media (max-width: 380px) {
  .problem-tools .tool-label {
    display: none;
  }

  .problem-tools .tool-btn {
    padding: 0 10px;
  }
}

/* ------------------------------------------------------- no connection page */

.offline {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 18px;
  max-width: 44rem;
  margin-top: 6vh;
}

@media (min-width: 640px) {
  .offline {
    grid-template-columns: auto minmax(0, 1fr);
    align-items: center;
    gap: 32px;
  }
}

.offline-art {
  display: grid;
  place-items: center;
  color: var(--ink);
}

.offline-icon {
  width: 120px;
  height: 120px;
}

.offline-body {
  display: grid;
  gap: 14px;
  justify-items: start;
}

.offline-title {
  display: flex;
  align-items: center;
  gap: 10px;
  font-family: var(--font-hand);
  font-weight: 700;
  font-size: 2.4rem;
  line-height: 1.05;
  color: var(--ink);
}

.offline-mark {
  width: 40px;
  height: 40px;
  flex: none;
}

.offline-text {
  max-width: 36ch;
  font-size: 1.2rem;
}

.offline-status {
  min-height: 1.5em;
  color: var(--pencil);
  font-weight: 700;
}

/* --------------------------------------------------------- iOS install hint */

.ios-hint {
  position: fixed;
  z-index: 25;
  left: 50%;
  bottom: max(16px, env(safe-area-inset-bottom));
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 10px 14px;
  align-items: start;
  width: min(26rem, calc(100vw - 28px));
  padding: 22px 18px 16px;
  color: var(--note-ink);
  background: var(--note);
  border-radius: 3px 3px 18px 3px;
  box-shadow: var(--shadow), 0 10px 24px rgba(0, 0, 0, 0.12);
  transform: translateX(-50%) rotate(-0.6deg);
}

.ios-hint::before {
  content: "";
  position: absolute;
  top: -11px;
  left: 50%;
  width: 92px;
  height: 24px;
  transform: translateX(-50%) rotate(-3deg);
  background: var(--tape);
  border: 1px solid rgba(0, 0, 0, 0.05);
}

.ios-hint-icon {
  width: 52px;
  height: 52px;
  border-radius: 12px;
}

.ios-hint-title {
  font-family: var(--font-hand);
  font-weight: 700;
  font-size: 1.6rem;
  line-height: 1.1;
  margin-bottom: 4px;
}

.ios-hint-text {
  font-size: 1rem;
  line-height: 1.45;
}

.ios-hint-close {
  grid-column: 1 / -1;
  justify-self: end;
  min-height: 44px;
}

/* ----------------------------------------------------------- reduced motion */

@media (prefers-reduced-motion: reduce) {
  .scratch {
    animation: none;
  }
}
