/* Handwriting pad (SPEC §15.5) — public/js/handwriting/pad.js
   A sheet of practice paper with a faint four-square guide, sumi ink and quiet motion. */

.hw {
  --hw-ink: #1b1a17;
  --hw-paper: #fffdf8;
  --hw-paper-edge: #f9f5ec;
  --hw-guide: rgba(209, 60, 47, .26);
  --hw-paper-border: rgba(120, 92, 60, .20);
  --hw-accent: var(--brand-text);
  --hw-tool-h: 44px;
  container-type: inline-size;
  display: block;
  padding: var(--s-4);
  border-radius: var(--r-xl);
  background: var(--surface-1);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-1);
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}
.hw[data-script="hiragana"] { --hw-accent: var(--hira-text); }
.hw[data-script="katakana"] { --hw-accent: var(--kata-text); }

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .hw {
    --hw-ink: #f0efec;
    --hw-paper: #1f1f1d;
    --hw-paper-edge: #191918;
    --hw-guide: rgba(229, 83, 63, .30);
    --hw-paper-border: rgba(255, 255, 255, .12);
  }
}
:root[data-theme="dark"] .hw {
  --hw-ink: #f0efec;
  --hw-paper: #1f1f1d;
  --hw-paper-edge: #191918;
  --hw-guide: rgba(229, 83, 63, .30);
  --hw-paper-border: rgba(255, 255, 255, .12);
}

/* ---------- Paper ---------- */
.hw-paper {
  position: relative;
  container-type: inline-size;
  width: min(100%, var(--hw-size, 340px));
  aspect-ratio: 1;
  border-radius: var(--r-lg);
  background:
    radial-gradient(140% 110% at 50% 0%, var(--hw-paper) 60%, var(--hw-paper-edge) 100%);
  border: 1px solid var(--hw-paper-border);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, .6),
    0 1px 2px rgba(27, 26, 23, .06),
    0 8px 24px -12px rgba(27, 26, 23, .18);
  overflow: hidden;
  touch-action: none;
  cursor: crosshair;
  transition: border-color var(--dur-2) var(--ease-out), box-shadow var(--dur-2) var(--ease-out);
}
:root[data-theme="dark"] .hw-paper { box-shadow: inset 0 1px 0 rgba(255, 255, 255, .04), 0 10px 28px -12px rgba(0, 0, 0, .7); }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .hw-paper { box-shadow: inset 0 1px 0 rgba(255, 255, 255, .04), 0 10px 28px -12px rgba(0, 0, 0, .7); }
}
.hw.is-drawing .hw-paper,
.hw-paper:hover { border-color: color-mix(in srgb, var(--hw-accent) 45%, var(--hw-paper-border)); }
.hw-paper.is-rejected { animation: hk-shake 380ms var(--ease-out); border-color: var(--danger); }

.hw-guide {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}
.hw-guide line {
  stroke: var(--hw-guide);
  stroke-width: 1.25;
  stroke-dasharray: 5 5;
  vector-effect: non-scaling-stroke;
}

.hw-ink,
.hw-fade {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
}
.hw-fade { pointer-events: none; opacity: 0; }
.hw-fade.is-fading { animation: hw-ink-out 420ms var(--ease-out) forwards; }
@keyframes hw-ink-out {
  from { opacity: 1; transform: none; filter: none; }
  to { opacity: 0; transform: scale(.94); filter: blur(1.5px); }
}

/* The recognised character, briefly shown over the paper. */
.hw-ghost {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-family: var(--font-kana);
  font-size: 62cqw;
  font-weight: 600;
  line-height: 1;
  color: var(--hw-accent);
  opacity: 0;
  pointer-events: none;
}
.hw-ghost.is-showing { animation: hw-ghost 900ms var(--ease-out) forwards; }
@keyframes hw-ghost {
  0% { opacity: 0; transform: scale(.86); }
  22% { opacity: .9; transform: scale(1); }
  65% { opacity: .55; }
  100% { opacity: 0; transform: scale(1.03); }
}

.hw-hint {
  position: absolute;
  inset: auto 0 9% 0;
  text-align: center;
  font-size: 13px;
  font-weight: 500;
  letter-spacing: .02em;
  color: var(--text-muted);
  pointer-events: none;
  transition: opacity var(--dur-2) var(--ease-out);
}
.hw.has-ink .hw-hint,
.hw.has-cands .hw-hint,
.hw-ghost.is-showing ~ .hw-hint { opacity: 0; }

/* Countdown to automatic recognition. */
.hw-progress {
  position: absolute;
  inset: auto 0 0 0;
  height: 3px;
  pointer-events: none;
}
.hw-progress span {
  display: block;
  height: 100%;
  background: var(--hw-accent);
  opacity: .7;
  transform: scaleX(0);
  transform-origin: left center;
}
.hw-progress.is-running span { animation: hw-countdown 650ms linear forwards; }
@keyframes hw-countdown { from { transform: scaleX(0); } to { transform: scaleX(1); } }

/* Messages (unreadable drawing, engine loading errors). */
.hw-notice {
  position: absolute;
  top: 10px;
  left: 50%;
  display: inline-flex;
  align-items: center;
  gap: var(--s-2);
  width: max-content;
  max-width: calc(100% - 24px);
  padding: 6px 12px;
  border-radius: var(--r-md);
  font-size: 12.5px;
  font-weight: 600;
  line-height: 1.3;
  text-align: center;
  background: var(--surface-1);
  color: var(--text-secondary);
  box-shadow: var(--shadow-2);
  opacity: 0;
  transform: translate(-50%, -6px);
  pointer-events: none;
  transition: opacity var(--dur-2) var(--ease-out), transform var(--dur-2) var(--ease-out);
}
.hw-notice.is-visible { opacity: 1; transform: translate(-50%, 0); pointer-events: auto; }
.hw-notice[data-tone="error"] {
  background: color-mix(in srgb, var(--danger) 10%, var(--surface-1));
  color: var(--danger-text);
  border: 1px solid color-mix(in srgb, var(--danger) 30%, transparent);
}
.hw-notice-btn {
  padding: 2px 10px;
  border: 0;
  border-radius: var(--r-pill);
  background: var(--danger);
  color: #fff;
  font: inherit;
  cursor: pointer;
}
.hw-notice-btn:focus-visible { outline: none; box-shadow: var(--ring-danger); }

/* Loading: a quiet spinner in the corner (drawing already works). */
.hw[data-state="loading"] .hw-paper::after {
  content: "";
  position: absolute;
  top: 12px;
  right: 12px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 2px solid var(--border-strong);
  border-top-color: var(--hw-accent);
  animation: hk-spin .8s linear infinite;
}

/* ---------- Side: suggestions and tools ---------- */
.hw-side {
  display: flex;
  flex-direction: column;
  gap: var(--s-3);
  width: min(100%, max(var(--hw-size, 340px), 300px));
  min-width: 0;
}
.hw-tip {
  margin: 0;
  min-height: 48px;
  display: flex;
  align-items: center;
  font-size: 13px;
  line-height: 1.45;
  color: var(--text-muted);
}
.hw.has-cands .hw-tip { display: none; }
.hw-cand-row { display: none; flex-direction: column; gap: 6px; min-height: 48px; }
.hw.has-cands .hw-cand-row { display: flex; }
.hw.is-strict .hw-cand-row { display: none; }
.hw-cand-label { font-size: 11.5px; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; color: var(--text-muted); }
.hw-cands { display: flex; flex-wrap: wrap; gap: 6px; }
.hw-cand {
  display: inline-grid;
  place-items: center;
  min-width: 44px;
  height: 44px;
  padding: 0 6px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--text-primary);
  font-family: var(--font-kana);
  font-size: 22px;
  font-weight: 600;
  line-height: 1;
  cursor: pointer;
  box-shadow: 0 1px 0 var(--border-strong);
  animation: hk-pop-in var(--dur-3) var(--ease-spring) both;
  animation-delay: calc(var(--i, 0) * 35ms);
  transition: background-color var(--dur-1) var(--ease-out), border-color var(--dur-1) var(--ease-out), transform var(--dur-1) var(--ease-out);
}
.hw-cand:hover { background: var(--surface-3); border-color: var(--border-strong); }
.hw-cand:active { transform: scale(.94); }
.hw-cand:focus-visible { outline: none; box-shadow: var(--ring); }
.hw-cand[aria-pressed="true"] {
  border-color: var(--hw-accent);
  background: color-mix(in srgb, var(--hw-accent) 12%, var(--surface-1));
  color: var(--hw-accent);
  box-shadow: 0 0 0 1px var(--hw-accent) inset;
  cursor: default;
}

.hw-tools { display: flex; flex-wrap: wrap; justify-content: space-between; gap: var(--s-2); }
.hw-tools-group { display: flex; gap: var(--s-2); }
.hw-tool {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-width: 48px;
  height: var(--hw-tool-h);
  padding: 0 var(--s-3);
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--text-primary);
  font: 600 14px/1 var(--font-ui);
  cursor: pointer;
  box-shadow: 0 1px 0 var(--border-strong);
  transition: background-color var(--dur-1) var(--ease-out), opacity var(--dur-2) var(--ease-out), transform var(--dur-1) var(--ease-out);
}
.hw-tool:hover:not(:disabled) { background: var(--surface-3); }
.hw-tool:active:not(:disabled) { transform: scale(.95); }
.hw-tool:focus-visible { outline: none; box-shadow: var(--ring); }
.hw-tool:disabled { opacity: .45; cursor: default; box-shadow: none; }
.hw-tool-icon { flex: none; }
.hw-clear, .hw-undo { color: var(--text-secondary); }
.hw-small .kana { font-family: var(--font-jp); font-size: 19px; line-height: 1; }
.hw-small.is-on {
  border-color: var(--hw-accent);
  background: color-mix(in srgb, var(--hw-accent) 12%, var(--surface-1));
  color: var(--hw-accent);
}
.hw-back { min-width: 60px; }
.hw-tool.is-rejected { animation: hk-shake 350ms var(--ease-out); border-color: var(--danger); }

/* ---------- Wide pads: paper on the left, suggestions and tools beside it ---------- */
.hw-body { display: flex; flex-direction: column; align-items: center; gap: var(--s-4); width: 100%; }
@container (min-width: 600px) {
  .hw-body { flex-direction: row; align-items: stretch; justify-content: center; gap: var(--s-5); }
  .hw-paper { flex: none; width: var(--hw-size-wide, 320px); }
  .hw-side { width: auto; flex: 1; max-width: 340px; justify-content: space-between; }
  .hw-tip { align-items: flex-start; }
}

/* Short screens: keep the paper and the question in view together. */
@media (max-height: 760px) { .hw { --hw-size: 280px; --hw-size-wide: 280px; } }

@media (max-width: 380px) {
  .hw { padding: var(--s-3); }
  .hw-tool-text { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  .hw-ghost.is-showing, .hw-fade.is-fading, .hw-progress.is-running span { animation: none; }
  .hw-cand { animation: none; }
}
