/* ─────────────────────────────────────────────────────────────────────
   Kana Workbook — /<script>/workbook
   Scoped to body.workbook-app. The palette is defined once as custom
   properties and restated in the html.dark block, so the sheet becomes dark
   paper in inkstone mode instead of a bright slab in a dark page. The
   canvases (workbook.js, coaster.js) read the same properties.
   ───────────────────────────────────────────────────────────────────── */
body.workbook-app {
  -webkit-tap-highlight-color: transparent;
  /* Three colour families the whole sheet is built from, kept as raw RGB
     triples so a rule can pick its own alpha: the ruling blue-grey, the
     on-paper accent, and the muted text that sits between them. */
  --np-line-rgb: 120, 140, 170;
  --np-accent-rgb: 32, 80, 126;
  --np-muted-rgb: 80, 95, 120;

  --np-paper: #fffdf7;
  --np-paper-edge: #efe8d6;
  --np-paper-sheen: rgba(255, 255, 255, .7);
  --np-fold: rgba(0, 0, 0, .06);
  --np-rule: rgba(var(--np-line-rgb), 0.34);
  --np-margin: rgba(196, 80, 70, 0.55);
  --np-ink: #1d1a24;
  --np-active: var(--accent, #20507e);
  --np-active-glow: rgba(var(--np-accent-rgb), 0.20);
  --np-stamp: #c5352b;
  --np-paper-accent: #20507e;
  --np-paper-accent-hover: #1a4269;
  --np-chip-bg: var(--np-chip-bg);
  --np-chip-bg-hover: #fff;

  /* Canvas-side colours: js/workbook/workbook.js and coaster.js read these
     off the paper instead of carrying a light-mode palette of their own. */
  --np-guide: rgba(var(--np-line-rgb), 0.32);
  --np-ghost: rgba(120, 130, 160, 0.16);
  --np-ghost-line: rgba(100, 110, 140, 0.35);
  --np-hint-rgb: 95, 112, 140;
  --np-wash: rgba(29, 26, 36, 0.10);
  --np-sky-a: rgba(140, 195, 240, 0.30);
  --np-sky-b: rgba(180, 218, 248, 0.16);
  --np-sky-c: rgba(210, 235, 252, 0.04);
  --np-cloud-rgb: 255, 255, 255;
  --np-far-hill-rgb: 120, 150, 190;
  --np-hill-rgb: 95, 92, 105;
  --np-track: #5f5c69;
  --np-tie: #6f6b7a;
  --np-sketch: rgba(95, 92, 105, 0.16);
  --np-sketch-2: rgba(80, 78, 92, 0.30);
  --np-car-line: rgba(30, 28, 36, 0.55);
  --np-car-body: #2a2830;
  --np-car-face: #f2d9c0;
  --np-eraser: #6e6a78;
  --np-eraser-2: #8a8694;
}
/* Inkstone: the sheet becomes dark paper rather than a bright slab in a dark
   page. Same relationship to the page as .card elsewhere on the site —
   a step lighter than the background, with light ink on it. */
html.dark body.workbook-app {
  --np-line-rgb: 150, 170, 200;
  --np-accent-rgb: 110, 167, 216;
  --np-muted-rgb: 165, 180, 205;

  --np-paper: #1f2331;
  --np-paper-edge: #333a4d;
  --np-paper-sheen: rgba(255, 255, 255, .05);
  --np-fold: rgba(0, 0, 0, .22);
  --np-rule: rgba(var(--np-line-rgb), 0.20);
  --np-margin: rgba(214, 116, 104, 0.45);
  --np-ink: #e8eaf3;
  --np-active: var(--accent, #6ea7d8);
  --np-active-glow: rgba(110, 167, 216, 0.30);
  --np-stamp: #ff8272;
  --np-paper-accent: #8bbde6;
  --np-paper-accent-hover: #6ea7d8;
  --np-chip-bg: rgba(38, 44, 60, 0.92);
  --np-chip-bg-hover: #2c3245;

  --np-guide: rgba(var(--np-line-rgb), 0.18);
  --np-ghost: rgba(150, 170, 205, 0.14);
  --np-ghost-line: rgba(175, 195, 225, 0.30);
  --np-hint-rgb: 150, 175, 210;
  --np-wash: rgba(232, 234, 243, 0.10);
  /* Night sky over the ride, and a graphite that reads on dark paper. */
  --np-sky-a: rgba(58, 92, 142, 0.22);
  --np-sky-b: rgba(46, 72, 112, 0.12);
  --np-sky-c: rgba(38, 58, 92, 0.03);
  --np-cloud-rgb: 158, 178, 208;
  --np-far-hill-rgb: 96, 126, 172;
  --np-hill-rgb: 168, 178, 198;
  --np-track: #97a0b6;
  --np-tie: #7f8798;
  --np-sketch: rgba(168, 178, 198, 0.18);
  --np-sketch-2: rgba(190, 200, 220, 0.32);
  --np-car-line: rgba(226, 230, 242, 0.55);
  --np-car-body: #d6dae8;
  --np-car-face: #f2d9c0;
  --np-eraser: #99a0b1;
  --np-eraser-2: #b7bdc9;
}

/* Dragging across the sheet must never start a text selection, and iOS
   must not raise its long-press callout on the squares. Prose below the
   sheet stays selectable. */
body.workbook-app .np-toolbar,
body.workbook-app .np-sheet,
body.workbook-app .np-nowbar,
body.workbook-app .np-done__stamp {
  -webkit-user-select: none; -moz-user-select: none; user-select: none;
  -webkit-touch-callout: none;
}
body.workbook-app .np-canvas { -webkit-user-drag: none; user-drag: none; }

/* Toolbar controls ───────────────────────────────────────────── */
/* Script switch — first control in the toolbar, aligned with the selects
   and buttons on the same line. */
.np-toggle { display: flex; gap: 8px; align-self: flex-end; }

/* Toolbar ─────────────────────────────────────────────────────── */
.np-toolbar {
  display: flex; flex-wrap: wrap; align-items: flex-end; gap: 14px 18px;
  margin: 6px 0 12px;
}
.np-progress { display: flex; align-items: center; gap: 10px; flex: 1 1 160px; min-width: 140px; padding-bottom: 8px; }
.np-progress__track { flex: 1; height: 6px; border-radius: 999px; background: var(--guide, rgba(35,32,38,.06)); overflow: hidden; }
.np-progress__bar { height: 100%; width: 0; border-radius: 999px; background: var(--np-active); transition: width .35s ease; }
.np-progress__text { font-variant-numeric: tabular-nums; font-size: 13px; font-weight: 700; color: var(--muted, #5d5852); white-space: nowrap; }
.np-new { align-self: flex-end; }
.np-hint { margin: 0 0 16px; font-size: 14px; color: var(--muted, #5d5852); }

/* Masthead — the header printed at the top of the sheet: row picker on
   the left, New sheet on the right, ruled off from the squares below like
   the name/date line on a piece of notebook paper. */
.np-masthead {
  position: relative; z-index: 4;
  margin: 0 calc(var(--np-pad-r) * -1) 13px calc(var(--np-pad-l) * -1);
  padding: 2px var(--np-pad-r) 0 var(--np-pad-l);
}
/* The sheet's printed title, above the ruled info line. */
.np-masthead__title {
  margin: 0 0 12px; text-align: center;
  font-family: var(--font-display, Georgia, serif); font-weight: 500;
  font-size: clamp(19px, 2.1vw, 24px); line-height: 1.15; letter-spacing: .004em;
  color: var(--np-ink);
}
/* The control row carries the double rule, so the pronunciation note
   below it prints under the rule, not above it. */
.np-masthead__row {
  position: relative;
  display: flex; align-items: flex-end; justify-content: space-between;
  gap: 14px; flex-wrap: wrap;
  padding-bottom: 9px; margin-bottom: 5px;
}
.np-masthead__row::before,
.np-masthead__row::after {
  content: ''; position: absolute;
  left: calc(var(--np-pad-l) * -1); right: calc(var(--np-pad-r) * -1);
}
.np-masthead__row::before { bottom: 0; border-bottom: 1.5px solid rgba(var(--np-line-rgb), 0.45); }
.np-masthead__row::after  { bottom: -4px; border-bottom: 1px solid rgba(var(--np-line-rgb), 0.26); }

/* Pronunciation note for the character in play, printed under the rule. */
.np-nowline {
  display: flex; align-items: baseline; gap: 9px;
  padding: 8px 2px 2px; min-height: 27px;
}
.np-nowline__kana { font-size: 17px; line-height: 1; color: var(--np-ink); }
.np-nowline__tip { font-size: 13px; font-style: italic; color: rgba(var(--np-muted-rgb), 0.82); }

/* The same note, pinned under the site header once the printed one has
   scrolled off. Only while the sheet itself is on screen. */
.np-nowbar {
  position: fixed; top: 88px; left: 50%; transform: translateX(-50%) translateY(-8px);
  z-index: 20; pointer-events: none;
  display: flex; align-items: baseline; gap: 12px;
  max-width: min(92vw, 620px);
  padding: 12px 22px; border-radius: 999px;
  background: var(--np-paper);
  border: 1px solid var(--accent-line, rgba(var(--np-accent-rgb), 0.24));
  box-shadow: 0 10px 26px rgba(40, 50, 70, 0.18), 0 2px 6px rgba(40, 50, 70, 0.10);
  opacity: 0; transition: opacity .22s ease, transform .22s ease;
}
.np-nowbar.is-shown { opacity: 1; transform: translateX(-50%) translateY(0); }
.np-nowbar__kana { font-size: 24px; line-height: 1; color: var(--np-ink); }
.np-nowbar__tip {
  font-size: 15.5px; font-style: italic; color: rgba(var(--np-muted-rgb), 0.92);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
@media (max-width: 520px) {
  .np-nowbar { top: 76px; padding: 10px 16px; gap: 10px; }
  .np-nowbar__kana { font-size: 21px; }
  .np-nowbar__tip { font-size: 14px; }
}
@media (prefers-reduced-motion: reduce) { .np-nowbar { transition: none; } }
.np-masthead__fields { display: flex; align-items: baseline; gap: 26px; flex-wrap: wrap; }
.np-masthead__field { display: flex; align-items: baseline; gap: 9px; cursor: pointer; }
.np-masthead__label {
  font-size: 10.5px; font-weight: 700; letter-spacing: .1em; text-transform: uppercase;
  color: rgba(var(--np-muted-rgb), 0.75);
}
.np-paper-select {
  appearance: none; -webkit-appearance: none;
  background: transparent; border: 0;
  border-bottom: 1.5px dashed rgba(var(--np-line-rgb), 0.55);
  color: var(--np-ink); font: 600 16px/1.25 inherit;
  padding: 1px 22px 3px 2px; cursor: pointer;
  background-image: linear-gradient(45deg, transparent 50%, rgba(var(--np-muted-rgb), .8) 50%),
                    linear-gradient(135deg, rgba(var(--np-muted-rgb), .8) 50%, transparent 50%);
  background-position: calc(100% - 9px) 62%, calc(100% - 4px) 62%;
  background-size: 5px 5px, 5px 5px; background-repeat: no-repeat;
}
.np-paper-select--sm { font-size: 14px; }
/* Link to the current row's reference page, beside the row picker. */
.np-row-link {
  display: inline-flex; align-items: center; gap: 2px; align-self: center;
  margin-left: -16px; padding: 3px 5px; border-radius: 5px;
  font-size: 11px; font-weight: 700; letter-spacing: .03em;
  color: var(--np-paper-accent); text-decoration: none; opacity: .7;
  transition: opacity .15s ease, background .15s ease;
}
.np-row-link svg { opacity: .8; margin-bottom: 1px; }
.np-row-link:hover, .np-row-link:focus-visible {
  opacity: 1; text-decoration: underline; background: rgba(var(--np-accent-rgb), 0.08); outline: none;
}
.np-paper-select:focus { outline: none; border-bottom-color: var(--np-paper-accent); border-bottom-style: solid; }
.np-paper-btn {
  font: 700 12.5px/1 inherit; letter-spacing: .01em; cursor: pointer;
  color: var(--np-paper-accent); background: transparent;
  border: 1px solid rgba(var(--np-accent-rgb), 0.32); border-radius: 8px;
  padding: 8px 15px; transition: background .18s ease, border-color .18s ease;
}
.np-paper-btn:hover { background: rgba(var(--np-accent-rgb), 0.07); border-color: rgba(var(--np-accent-rgb), 0.5); }
.np-paper-btn:focus-visible { outline: 2px solid var(--np-paper-accent); outline-offset: 2px; }
.np-masthead__actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
/* Auto sound: a paper button that fills in when switched on. */
.np-paper-toggle { display: inline-flex; align-items: center; gap: 6px; }
.np-paper-toggle.is-on {
  color: #fff; background: var(--np-paper-accent); border-color: var(--np-paper-accent);
}
.np-paper-toggle.is-on:hover { background: var(--np-paper-accent-hover); }
.np-paper-toggle .np-paper-toggle__on { display: none; }
.np-paper-toggle.is-on .np-paper-toggle__on { display: block; }
.np-paper-toggle.is-on .np-paper-toggle__off { display: none; }

/* The sheet ──────────────────────────────────────────────────── */
.np-sheet { position: relative; }
.np-loading { padding: 40px 0; text-align: center; color: var(--faint, #938b80); font-size: 14px; }

.np-paper {
  --np-cell: 96px;
  --np-pad-l: 18px; --np-pad-r: 18px; --np-pad-y: 18px;
  position: relative;
  padding: var(--np-pad-y) var(--np-pad-r) var(--np-pad-y) var(--np-pad-l);
  background: var(--np-paper);
  border: 1px solid var(--np-paper-edge);
  border-radius: 6px 14px 14px 6px;
  box-shadow:
    0 1px 0 var(--np-paper-sheen) inset,
    0 10px 30px rgba(40, 50, 70, 0.10),
    0 2px 4px rgba(40, 50, 70, 0.06);
  overflow: hidden;
}
/* Left margin line + punched holes — the notebook cue. */
.np-paper::before {
  content: ''; position: absolute; top: 0; bottom: 0; left: 9px;
  border-left: 1.5px solid var(--np-margin); opacity: .8; pointer-events: none;
}
.np-paper::after {
  content: ''; position: absolute; top: 0; bottom: 0; right: 0; width: 12px;
  background: linear-gradient(to left, var(--np-fold), transparent); pointer-events: none;
}

/* Horizontal ruling, one line per row of squares (cell + 6px gap). Drawn
   on the rows container, not the paper, so nothing rules the header. */
.np-rows {
  background: repeating-linear-gradient(to bottom,
    transparent 0, transparent calc(var(--np-cell) + 5px),
    var(--np-rule) calc(var(--np-cell) + 5px), var(--np-rule) calc(var(--np-cell) + 6px));
}
.np-row {
  display: flex; gap: 6px; margin-bottom: 6px; align-items: flex-start;
}
.np-row:last-child { margin-bottom: 0; }

.np-cell {
  position: relative; width: var(--np-cell); height: var(--np-cell); flex: 0 0 auto; box-sizing: border-box;
  border: 1px solid var(--np-rule); border-radius: 3px;
  background: transparent;
  /* Squares that aren't in play must pass touch through, or a finger
     landing on the sheet can't scroll the page. The active square takes
     touch away below, because that's where the pen writes. */
  touch-action: pan-y pinch-zoom;
  transition: box-shadow .25s ease, border-color .25s ease, transform .2s ease;
}
.np-canvas { display: block; width: 100%; height: 100%; border-radius: 2px; touch-action: inherit; }
/* Belt to the cell's suspenders: the canvas is the real touch target. */
.np-cell--active .np-canvas { touch-action: none; }

/* Model cell: printed glyph with a small romaji tag. */
.np-cell--model { border-color: rgba(var(--np-line-rgb), 0.55); background: rgba(var(--np-line-rgb), 0.06); }
/* Link to the character's own reference page (/hiragana/a). Sits where
   the romaji tag was — a link, not the glyph, so the glyph stays the
   "play the strokes" target. */
.np-model-link {
  position: absolute; right: 2px; bottom: 0;
  display: inline-flex; align-items: center; gap: 1px;
  padding: 2px 3px; border-radius: 4px;
  font-size: 11px; font-weight: 700; letter-spacing: .03em; text-transform: lowercase;
  color: var(--np-paper-accent); text-decoration: none; opacity: .72;
  transition: opacity .15s ease, background .15s ease;
}
.np-model-link svg { opacity: .8; margin-bottom: 1px; }
.np-model-link:hover, .np-model-link:focus-visible {
  opacity: 1; text-decoration: underline; background: rgba(var(--np-accent-rgb), 0.08); outline: none;
}

.np-cell--model .np-canvas { cursor: pointer; }
/* ▶ stroke-order player, top-left of the model square. */
.np-play {
  position: absolute; left: 3px; top: 3px; width: 20px; height: 20px; padding: 0 0 0 1px;
  display: grid; place-items: center; border-radius: 50%; cursor: pointer;
  border: 1px solid rgba(var(--np-accent-rgb), 0.30); background: var(--np-chip-bg);
  color: var(--np-active); opacity: .75;
  transition: opacity .15s ease, transform .15s ease, background .15s ease;
}
.np-play:hover, .np-play:focus-visible { opacity: 1; transform: scale(1.08); background: var(--np-chip-bg-hover); outline: none; }
/* Pronunciation button, bottom-left of the model square (the ▶ player
   sits top-left, the character link bottom-right). */
.np-sound {
  position: absolute; left: 3px; bottom: 3px; width: 20px; height: 20px; padding: 0;
  display: grid; place-items: center; border-radius: 50%; cursor: pointer;
  border: 1px solid rgba(var(--np-accent-rgb), 0.30); background: var(--np-chip-bg);
  color: var(--np-paper-accent); opacity: .7;
  transition: opacity .15s ease, transform .15s ease, background .15s ease;
}
.np-sound:hover, .np-sound:focus-visible { opacity: 1; transform: scale(1.08); background: var(--np-chip-bg-hover); outline: none; }
.np-cell--playing .np-play { opacity: .35; }
.np-cell--playing .np-model-link { opacity: .35; }

/* Writing cells: locked → active → done. */
.np-cell--write { cursor: not-allowed; }
.np-cell--active {
  cursor: crosshair;
  touch-action: none;              /* the pen owns this square */
  border-color: var(--np-active);
  box-shadow: 0 0 0 3px var(--np-active-glow), 0 4px 14px rgba(var(--np-accent-rgb), 0.18);
  z-index: 1;
}
.np-cell--active::after {
  /* breathing corner tick so the eye finds the active square */
  content: ''; position: absolute; left: -1px; top: -1px; width: 10px; height: 10px;
  border-left: 3px solid var(--np-active); border-top: 3px solid var(--np-active);
  border-radius: 3px 0 0 0;
  animation: np-breathe 1.6s ease-in-out infinite;
}
@keyframes np-breathe { 0%, 100% { opacity: .45; } 50% { opacity: 1; } }
/* Finished squares recede: a soft grey wash on the paper and slightly
   softened ink, so the eye goes to the active square. Stamp stays red. */
.np-cell--done {
  cursor: default;
  background: rgba(var(--np-line-rgb), 0.075);
  border-color: rgba(var(--np-line-rgb), 0.28);
  transition: background .6s ease .25s, border-color .6s ease .25s, box-shadow .25s ease;
}
.np-cell--done .np-canvas { opacity: .78; transition: opacity .6s ease .25s; }

/* Teacher's grade on a completed cell (A+ … F-): a small red letter
   pencilled into the top-right corner — no ring, no background. */
.np-stamp {
  position: absolute; right: 3px; top: 1px;
  font: 700 8.5px/1 "Inter", system-ui, sans-serif; letter-spacing: -.01em;
  color: var(--np-stamp); opacity: 0; pointer-events: none;
  transform: scale(1.35) rotate(-8deg); transform-origin: 70% 40%;
  mix-blend-mode: multiply;
}
.np-stamp[data-grade="A+"] { font-size: 9px; }
.np-stamp--show { animation: np-stamp-in .32s cubic-bezier(.2, 1.3, .4, 1) forwards; }
@keyframes np-stamp-in {
  0%   { opacity: 0; transform: scale(1.35) rotate(-8deg); }
  100% { opacity: .78; transform: scale(1) rotate(-5deg); }
}

/* Slip: shake + flash the border red, ink has been wiped by JS. */
.np-cell--shake { animation: np-shake .42s ease; border-color: var(--red, #b1322a) !important; }
@keyframes np-shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-5px); } 40% { transform: translateX(5px); }
  60% { transform: translateX(-3px); } 80% { transform: translateX(3px); }
}
/* Partial stroke lifted early: small nudge, no penalty. */
.np-cell--nudge { animation: np-nudge .3s ease; }
@keyframes np-nudge { 50% { transform: translateY(2px); } }

/* Page complete card ─────────────────────────────────────────── */
.np-done {
  margin: 18px 0 6px; padding: 22px 24px; text-align: center;
  border: 1px dashed var(--accent-line, rgba(var(--np-accent-rgb), .24)); border-radius: 14px;
  background: var(--accent-bg, rgba(var(--np-accent-rgb), .08));
}
.np-done__stamp {
  display: inline-grid; place-items: center; width: 56px; height: 56px; margin-bottom: 8px;
  border: 3px solid var(--np-stamp); border-radius: 50%; color: var(--np-stamp);
  font-size: 26px; font-weight: 700; transform: rotate(-10deg);
}
.np-done .h2 { margin: 0 0 6px; }
.np-done p { margin: 0 0 14px; color: var(--muted, #5d5852); }
.np-done .btn-row { justify-content: center; }
.np-about { margin-top: 26px; }

/* Particle host canvas: fills the paper, invisible, never intercepts. */
.np-fx-host { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; opacity: 0; }
.np-paper .particles-overlay { z-index: 3; }

/* Options gear sits with the toolbar controls. */
.np-toolbar .options-dropdown { align-self: flex-end; margin-bottom: 2px; }

/* Coaster overlay: covers the whole paper, never intercepts the pen. */
.np-coaster {
  position: absolute; inset: 0; width: 100%; height: 100%;
  pointer-events: none; z-index: 2;
}

/* Small screens: tighter paper padding so cells stay finger-sized. */
/* ── Phones ───────────────────────────────────────────────────────
   The header has to stay compact: the toolbar on one line, and the
   masthead on two (fields, then buttons) instead of sprawling to three. */
@media (max-width: 620px) {
  .np-paper { --np-pad-l: 16px; --np-pad-r: 10px; --np-pad-y: 12px; }

  /* Toolbar: script buttons, progress and the gear share one line. */
  .np-toolbar { gap: 8px 10px; flex-wrap: nowrap; align-items: center; }
  .np-toggle { gap: 6px; align-self: center; }
  .np-toolbar .btn--sm { padding: 9px 12px; font-size: 13.5px; }
  .np-progress { flex: 1 1 60px; min-width: 52px; padding-bottom: 0; gap: 7px; }
  .np-progress__text { font-size: 12px; }
  .np-toolbar .options-dropdown { align-self: center; margin-bottom: 0; }

  /* Masthead: label above value so both pickers fit one line, buttons
     on the next. */
  .np-masthead__title { margin-bottom: 9px; }
  .np-masthead__row { flex-direction: column; align-items: stretch; gap: 9px; padding-bottom: 8px; }
  .np-masthead__fields { align-items: flex-end; gap: 16px; }
  .np-masthead__field { flex-direction: column; align-items: flex-start; gap: 1px; }
  .np-masthead__label { font-size: 9.5px; letter-spacing: .07em; }
  .np-paper-select { font-size: 15px; padding: 1px 19px 3px 2px; }
  .np-paper-select--sm { font-size: 13.5px; }
  .np-row-link { margin-left: -8px; align-self: flex-end; margin-bottom: 2px; }
  .np-masthead__actions { gap: 8px; }
  .np-paper-btn { padding: 8px 13px; font-size: 12px; }
}
@media (max-width: 380px) {
  .np-toolbar .btn--sm { padding: 8px 10px; font-size: 12.5px; }
  .np-masthead__fields { gap: 12px; }
}
