:root {
  --bg: #fff4d6;
  --paper: #ffffff;
  --ink: #3a2e1f;
  --guide: #d9d3c6;
  --paint: #ff6b35;
  --paint2: #ffb35c;
  --done: #4fc3f7;
  --accent: #ff8c42;
  --accent-dark: #c75a16;
  --green: #5fd068;
  --shadow: 0 6px 0 rgba(0, 0, 0, 0.12);
  --radius: 22px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body {
  font-family: "Zen Maru Gothic", "Hiragino Maru Gothic ProN", "Rounded Mplus 1c", sans-serif;
  font-weight: 700;
  color: var(--ink);
  background: var(--bg);
  background-image:
    radial-gradient(circle at 10% 20%, rgba(255, 255, 255, 0.7) 0 18px, transparent 19px),
    radial-gradient(circle at 85% 70%, rgba(255, 255, 255, 0.6) 0 26px, transparent 27px),
    radial-gradient(circle at 70% 15%, rgba(255, 255, 255, 0.5) 0 12px, transparent 13px);
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

.screen {
  position: fixed; inset: 0;
  display: flex; flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
[hidden] { display: none !important; }

/* ---------- ボタン共通 ---------- */
button { font: inherit; color: inherit; cursor: pointer; border: 0; }
.icon-btn {
  width: 56px; height: 56px; border-radius: 50%;
  background: var(--paper); box-shadow: var(--shadow);
  font-size: 26px; line-height: 1;
}
.icon-btn:active { transform: translateY(3px); box-shadow: 0 3px 0 rgba(0,0,0,.12); }
.big-btn {
  min-width: 220px; padding: 16px 32px;
  border-radius: 999px; font-size: 28px; font-weight: 900;
  color: #fff; background: var(--accent); box-shadow: 0 6px 0 var(--accent-dark);
}
.big-btn.secondary { color: var(--ink); background: #fff; box-shadow: var(--shadow); }
.big-btn:active { transform: translateY(4px); box-shadow: 0 2px 0 var(--accent-dark); }
.big-btn.secondary:active { box-shadow: 0 2px 0 rgba(0,0,0,.12); }
.nav-btn {
  min-width: 88px; height: 64px; padding: 0 22px;
  border-radius: 999px; font-size: 24px; font-weight: 900; white-space: nowrap;
  background: var(--paper); box-shadow: var(--shadow);
}
.nav-btn:active { transform: translateY(3px); box-shadow: 0 3px 0 rgba(0,0,0,.12); }
.nav-btn[disabled] { opacity: .35; pointer-events: none; }
.danger-btn {
  padding: 12px 20px; border-radius: 12px; background: #ffe1e1; color: #a12020;
  position: relative; overflow: hidden;
}
.danger-btn.holding::after {
  content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 0;
  background: rgba(161, 32, 32, .25); animation: hold 2s linear forwards;
}
@keyframes hold { to { width: 100%; } }

/* ---------- ホーム ---------- */
.home-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 20px 6px;
}
.home-head h1 { margin: 0; font-size: 34px; font-weight: 900; letter-spacing: .04em; }
/* セット切替タブ */
.set-tabs { display: flex; gap: 6px; background: rgba(255,255,255,.6); border-radius: 999px; padding: 5px; }
.set-tab {
  position: relative; padding: 8px 22px; border-radius: 999px; font-size: 22px; font-weight: 900;
  background: transparent; color: #8a7a60;
}
.set-tab.active { background: var(--accent); color: #fff; box-shadow: 0 3px 0 var(--accent-dark); }
.set-tab .badge {
  position: absolute; right: -4px; top: -6px; min-width: 22px; height: 22px; padding: 0 6px; border-radius: 11px;
  background: var(--done); color: #fff; font-size: 14px; line-height: 22px; text-align: center;
}
.number-grid {
  flex: 1; display: grid; gap: 10px; padding: 8px 20px;
  min-height: 0;
}
/* すうじ: 10 列 × 2 段 */
.number-grid.grid-suuji { grid-template-columns: repeat(10, 1fr); grid-auto-rows: 1fr; }
/* ひらがな: 五十音表。横長画面では 11 列（列＝行、上から あいうえお）、縦長では 5 列（1 行＝1 行） */
.number-grid.grid-hiragana { grid-template-rows: repeat(5, 1fr); grid-template-columns: repeat(11, 1fr); grid-auto-flow: column; }
.num-btn {
  position: relative; border-radius: 18px; background: var(--paper);
  box-shadow: var(--shadow); font-size: clamp(26px, 4.5vw, 44px); font-weight: 900;
  display: flex; align-items: center; justify-content: center; min-height: 0;
}
.blank { pointer-events: none; }
.num-btn:active { transform: translateY(3px); box-shadow: 0 3px 0 rgba(0,0,0,.12); }
.num-btn.done { background: #fffbe8; }
.num-btn .mini { position: absolute; right: 4px; bottom: 4px; width: 38%; aspect-ratio: 1; }
.num-btn .mini svg { width: 100%; height: 100%; }
.home-foot { display: flex; gap: 16px; justify-content: center; padding: 10px 20px 16px; }

/* ---------- なぞり画面 ---------- */
.trace-head {
  display: flex; align-items: center; justify-content: space-between; padding: 12px 20px 0;
}
.now { display: flex; align-items: baseline; gap: 14px; }
.now-number { font-size: 44px; font-weight: 900; }
.now-reading { font-size: 26px; color: #7a6a52; }
.stage-wrap { position: relative; flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; }
.stage {
  width: 100%; height: 100%; max-height: 100%;
  touch-action: none;
}
.trace-foot { display: flex; justify-content: center; gap: 18px; padding: 8px 20px 16px; }

/* 文字の表示用 SVG（画データをそのまま描く。高さは周囲の font-size に合わせる） */
.glyph { display: inline-block; height: 1em; width: auto; vertical-align: -0.12em; overflow: visible; }
.glyph path { fill: none; stroke: currentColor; stroke-width: var(--sw, 20); stroke-linecap: round; stroke-linejoin: round; }
.num-btn .glyph { height: 1.1em; }
.slot .n .glyph { height: 1.2em; }

/* SVG レイヤ */
.guide-stroke { fill: none; stroke: var(--guide); stroke-width: var(--sw, 20); stroke-linecap: round; stroke-linejoin: round; }
.done-stroke  { fill: none; stroke: var(--done);  stroke-width: var(--sw, 20); stroke-linecap: round; stroke-linejoin: round; }
.paint-stroke { fill: none; stroke: var(--paint); stroke-width: var(--sw, 20); stroke-linecap: round; stroke-linejoin: round; }
.flow-stroke {
  fill: none; stroke: #fff; stroke-width: 4; stroke-linecap: round; stroke-linejoin: round;
  stroke-dasharray: 6 14; opacity: .9; animation: flow 1.2s linear infinite;
}
@keyframes flow { to { stroke-dashoffset: -20; } }
.start-dot { fill: var(--paint); stroke: #fff; stroke-width: 2.5; animation: pulse 1s ease-in-out infinite; transform-box: fill-box; transform-origin: center; }
.start-label { fill: #fff; font-size: 11px; font-weight: 900; text-anchor: middle; dominant-baseline: central; pointer-events: none; }
@keyframes pulse { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.25); } }

#truck { pointer-events: none; transition: none; }
#truck.driving { transition: none; }
.wheel { animation: none; }
#truck.moving .wheel { animation: spin .35s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* キラキラ粒子 */
.sparkles { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.particle {
  position: fixed; z-index: 60; pointer-events: none; width: 14px; height: 14px; border-radius: 50%;
  background: var(--c, #ffd54a);
  animation: burst var(--d, 900ms) ease-out forwards;
}
.particle.star {
  border-radius: 0;
  clip-path: polygon(50% 0, 61% 35%, 98% 35%, 68% 57%, 79% 91%, 50% 70%, 21% 91%, 32% 57%, 2% 35%, 39% 35%);
}
@keyframes burst {
  0%   { transform: translate(0, 0) scale(.4) rotate(0deg); opacity: 1; }
  100% { transform: translate(var(--dx), var(--dy)) scale(1.1) rotate(220deg); opacity: 0; }
}

/* ---------- オーバーレイ ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 20;
  background: rgba(60, 40, 20, .45);
  display: flex; align-items: center; justify-content: center;
  padding: 16px;
}
.done-card, .book-card, .settings-card {
  background: var(--paper); border-radius: 28px; box-shadow: 0 16px 40px rgba(0,0,0,.25);
  padding: 20px 28px; max-width: 92vw; max-height: 92vh; overflow: auto;
  animation: pop .45s cubic-bezier(.2, 1.4, .4, 1);
}
@keyframes pop { from { transform: scale(.6); opacity: 0; } }
.done-card { text-align: center; }
.done-title { margin: 0 0 6px; font-size: 44px; font-weight: 900; color: var(--accent); }
.done-label { margin: 6px 0 16px; font-size: 24px; }

/* シール */
.sticker { position: relative; display: inline-block; width: 120px; height: 120px; }
.sticker.big { width: min(42vh, 260px); height: min(42vh, 260px); }
.sticker svg { width: 100%; height: 100%; filter: drop-shadow(0 4px 6px rgba(0,0,0,.2)); }
.sticker.big svg { animation: wobble 2.4s ease-in-out infinite; }
@keyframes wobble { 0%, 100% { transform: rotate(-4deg) scale(1); } 50% { transform: rotate(4deg) scale(1.04); } }
.sticker::before, .sticker::after {
  content: ""; position: absolute; width: 22%; height: 22%; pointer-events: none;
  background: #fff;
  clip-path: polygon(50% 0, 60% 40%, 100% 50%, 60% 60%, 50% 100%, 40% 60%, 0 50%, 40% 40%);
  animation: twinkle 1.6s ease-in-out infinite;
  will-change: transform, opacity;
}
/* シール帳のマス内（最大 46 枚）は無限アニメ・フィルタを切る（iPad で枚数に比例して重くなるため）。
   キラキラは貼った瞬間の 1 枚（.pasted）だけ */
.slot .sticker::before, .slot .sticker::after { display: none; }
.slot .sticker.pasted::before, .slot .sticker.pasted::after { display: block; animation-iteration-count: 3; }
.slot .sticker svg { filter: none; }
.sticker::before { left: 4%; top: 8%; }
.sticker::after  { right: 2%; bottom: 10%; width: 16%; height: 16%; animation-delay: .8s; }
@keyframes twinkle { 0%, 100% { transform: scale(.3) rotate(0deg); opacity: .2; } 50% { transform: scale(1.1) rotate(45deg); opacity: 1; } }
/* ホログラム風の走る光 */
.sticker.big .glint {
  position: absolute; inset: 0; border-radius: 50%; overflow: hidden; pointer-events: none;
}
.sticker.big .glint::before {
  content: ""; position: absolute; top: -20%; bottom: -20%; left: -60%; width: 40%;
  background: linear-gradient(100deg, transparent, rgba(255,255,255,.75), transparent);
  transform: skewX(-20deg); animation: glint 2.2s ease-in-out infinite;
}
@keyframes glint { 0% { left: -60%; } 60%, 100% { left: 130%; } }

/* シールちょう */
.book-card { width: min(92vw, 980px); }
.book-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.book-head h2 { margin: 0; font-size: 28px; font-weight: 900; }
.book-grid { display: grid; gap: 8px; }
.book-grid.grid-suuji { grid-template-columns: repeat(10, 1fr); }
.book-grid.grid-hiragana { grid-template-rows: repeat(5, 1fr); grid-template-columns: repeat(11, 1fr); grid-auto-flow: column; }
.slot {
  aspect-ratio: 1; border-radius: 16px; background: #f4efe3; border: 3px dashed #d9d3c6;
  display: flex; align-items: center; justify-content: center; position: relative;
}
.slot .n { position: absolute; left: 6px; top: 2px; font-size: 16px; color: #9a8b70; }
.slot .sticker { width: 78%; height: 78%; }
.slot.filled { border-style: solid; background: #fff; }

/* せってい */
.settings-card { width: min(92vw, 640px); font-weight: 700; }
.setting { border: 0; padding: 0; margin: 0 0 18px; }
.setting legend { font-size: 18px; font-weight: 900; margin-bottom: 6px; }
.setting label { display: block; padding: 8px 4px; font-size: 17px; }
.setting input { transform: scale(1.4); margin-right: 12px; }
.note { font-size: 14px; color: #8a7a60; margin: 4px 0 0; }

/* スマホ縦など狭い幅 */
@media (max-width: 700px) {
  .number-grid, .number-grid.grid-suuji, .number-grid.grid-hiragana { grid-template-columns: repeat(5, 1fr); grid-template-rows: none; grid-auto-rows: minmax(56px, 1fr); grid-auto-flow: row; overflow-y: auto; }
  .book-grid, .book-grid.grid-suuji, .book-grid.grid-hiragana { grid-template-columns: repeat(5, 1fr); grid-template-rows: none; grid-auto-flow: row; }
  .home-head { flex-wrap: wrap; gap: 8px; }
  .home-head h1 { font-size: 24px; }
  .set-tab { padding: 6px 14px; font-size: 18px; }
  .big-btn { min-width: 0; font-size: 22px; padding: 12px 22px; }
}

/* ---------- シール貼り（v1.1） ---------- */
.done-badge { display: flex; align-items: baseline; justify-content: center; gap: 12px; margin: 4px 0; }
.done-badge #done-number { font-size: 72px; font-weight: 900; color: var(--done); }
.done-badge #done-reading { font-size: 28px; color: #7a6a52; }

.book-card { display: flex; flex-direction: column; gap: 10px; }
.book-grid { touch-action: none; }
.slot.target { border-color: var(--accent); background: #fff3e0; }
.slot.target::after {
  content: ""; position: absolute; inset: -8px; border-radius: 22px; pointer-events: none;
  border: 5px solid rgba(255, 140, 66, .6); animation: glow 1s ease-in-out infinite; will-change: opacity;
}
@keyframes glow { 0%, 100% { opacity: 0; } 50% { opacity: 1; } }
.slot.nope { animation: shake .4s; }
@keyframes shake { 0%, 100% { transform: translateX(0); } 25% { transform: translateX(-6px); } 75% { transform: translateX(6px); } }
.slot .sticker { transform: rotate(var(--rot, 0deg)) translate(var(--dx, 0), var(--dy, 0)); }
.slot .sticker.pasted { animation: paste .5s cubic-bezier(.2, 1.5, .4, 1); }
@keyframes paste { 0% { transform: scale(1.8) rotate(var(--rot, 0deg)); opacity: .6; } 100% { transform: scale(1) rotate(var(--rot, 0deg)) translate(var(--dx, 0), var(--dy, 0)); } }

.book-tray { background: #fff7e0; border-radius: 18px; padding: 10px 14px; }
.tray-label { margin: 0 0 6px; font-size: 20px; text-align: center; }
.tray-list { display: flex; justify-content: center; gap: 14px; touch-action: none; }
.tray-item { width: clamp(64px, 9vw, 96px); height: clamp(64px, 9vw, 96px); border-radius: 50%; transition: transform .15s; }
.tray-item.selected { transform: scale(1.18); box-shadow: 0 0 0 5px var(--accent); }
.tray-item.ghost { opacity: .25; }
.book-foot { display: flex; justify-content: center; }

.sticker.drag {
  position: fixed; left: 0; top: 0; z-index: 50; pointer-events: none;
  width: clamp(80px, 11vw, 120px); height: clamp(80px, 11vw, 120px);
  /* 位置は main.js が --x/--y に入れる。left/top でなく transform で動かす（合成のみで再描画しない） */
  transform: translate3d(var(--x, 0px), var(--y, 0px), 0) translate(-50%, -50%) scale(1.15) rotate(-6deg);
  will-change: transform;
  border-radius: 50%; box-shadow: 0 12px 14px rgba(0, 0, 0, .3);
}
.sticker.drag svg { filter: none; }
.big-btn.wiggle { animation: wiggle 1.2s ease-in-out infinite; }
@keyframes wiggle { 0%, 100% { transform: rotate(0); } 25% { transform: rotate(-3deg); } 75% { transform: rotate(3deg); } }
