:root {
  --bg: #f4efe6;
  --surface: #ffffff;
  --text: #2a2623;
  --muted: #6d655d;
  --accent: #2f7d5b;
  --accent-ink: #ffffff;
  --border: #ddd3c4;
  --board: #234b3a;
  --board-edge: #8a5a35;
  --chalk: #f3f1ea;
  --chalk-yellow: #ffe37a;
  --chalk-pink: #ff9bb0;
  --chalk-blue: #9fd3ff;
  --note: #fff8d6;
  --note-ink: #3b3326;
  --bubble: #ffffff;
  --bubble-T: #e5f0fb;
  --bubble-S: #fde8ec;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1c1b19; --surface: #272522; --text: #ece7df; --muted: #a89f94;
    --accent: #4fae84; --border: #3d3934; --board: #1d3d30; --board-edge: #5e3f27;
    --note: #3a3527; --note-ink: #f2ead3; --bubble: #2c2a27; --bubble-T: #23303d; --bubble-S: #3a2830;
  }
}
:root[data-theme="dark"] {
  --bg: #1c1b19; --surface: #272522; --text: #ece7df; --muted: #a89f94;
  --accent: #4fae84; --border: #3d3934; --board: #1d3d30; --board-edge: #5e3f27;
  --note: #3a3527; --note-ink: #f2ead3; --bubble: #2c2a27; --bubble-T: #23303d; --bubble-S: #3a2830;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font-family: "Zen Maru Gothic", "Hiragino Maru Gothic ProN", sans-serif;
  overflow-x: hidden;
}
button, select { font: inherit; color: inherit; }

/* ── header ── */
.top { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between;
  gap: 8px 16px; padding: 12px 16px; max-width: 1200px; margin: 0 auto; }
.title { display: flex; align-items: center; gap: 10px; }
.title h1 { font-size: clamp(16px, 2.4vw, 22px); margin: 0; }
.badge { background: var(--accent); color: var(--accent-ink); border-radius: 999px; padding: 2px 10px; font-size: 13px; }
.scenes { display: flex; flex-wrap: wrap; gap: 6px; }
.scenes button { border: 1px solid var(--border); background: var(--surface); border-radius: 999px;
  padding: 3px 10px; font-size: 13px; cursor: pointer; }
.scenes button.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }

.wrap { max-width: 1200px; margin: 0 auto; padding: 0 16px 24px; display: grid; gap: 14px; }

/* ── blackboard ── */
.board-area { display: grid; grid-template-columns: 1fr 230px; gap: 14px; }
.board { position: relative; background: var(--board);
  background-image: radial-gradient(ellipse at 30% 20%, rgba(255,255,255,.06), transparent 60%),
                    radial-gradient(ellipse at 80% 90%, rgba(0,0,0,.18), transparent 60%);
  border: 12px solid var(--board-edge); border-radius: 10px;
  box-shadow: inset 0 0 40px rgba(0,0,0,.35), 0 4px 14px rgba(0,0,0,.18);
  min-height: 380px; height: min(56vh, 520px); color: var(--chalk); }
.board-inner { position: absolute; inset: 0 0 14px 0; overflow-y: auto; overflow-x: hidden;
  padding: 18px 22px 30px; font-family: "Klee One", serif; scroll-behavior: smooth;
  scrollbar-color: rgba(255,255,255,.25) transparent; scrollbar-width: thin; }
.chalk-tray { position: absolute; left: 0; right: 0; bottom: 0; height: 10px; background: rgba(0,0,0,.25); }
.board .katex { font-size: 1.4em; color: var(--chalk); }

.line { margin: 6px 0 14px; white-space: nowrap; overflow: visible; }
.line.cont { padding-left: 2.2em; margin-top: -6px; }
.line.final { display: inline-block; border: 2px solid var(--chalk-yellow); border-radius: 8px;
  padding: 8px 14px; box-shadow: 0 0 18px rgba(255,227,122,.25); }
.line.final .katex { color: var(--chalk-yellow); }

/* chalk write-in */
.anim-in { animation: chalk 1s ease-out both; }
@keyframes chalk {
  from { clip-path: inset(0 100% 0 0); opacity: .4; }
  to   { clip-path: inset(0 0 0 0); opacity: 1; }
}

/* rows k=1,2,3,…,n */
table.rows { border-collapse: collapse; margin: 6px 0 12px; }
table.rows td { padding: 3px 6px; white-space: nowrap; vertical-align: middle; }
table.rows td.lab { color: var(--chalk-blue); padding-right: 16px; font-size: .95em; }
table.rows td.lab .katex { color: var(--chalk-blue); font-size: 1.05em; }
table.rows td.l1, table.rows td.l2 { text-align: center; }
table.rows tr.plus td.l1 { position: relative; padding-left: 2em; }
table.rows tr.plus td.l1::before { content: "+)"; position: absolute; left: .2em; top: 50%;
  transform: translateY(-50%); color: var(--chalk-pink); font-family: "Klee One", serif; }
table.rows tr.sumrow td.res { border-top: 2px solid var(--chalk); padding-top: 8px; text-align: center; }
table.rows tr.sumrow td.res .katex { color: var(--chalk-yellow); }
.term { position: relative; display: inline-block; padding: 0 3px; border-radius: 6px; transition: background .3s, box-shadow .3s, opacity .4s; }

/* highlight & cancel */
.hl { background: rgba(255,227,122,.22); box-shadow: 0 0 0 2px rgba(255,227,122,.75); border-radius: 6px; }
.hl .katex, .hl.katex { color: var(--chalk-yellow); }
.x { opacity: .55; }
.x::after { content: ""; position: absolute; left: -8%; right: -8%; top: 50%; height: 3px; border-radius: 2px;
  background: var(--chalk-pink); transform-origin: left center; transform: rotate(-28deg) scaleX(1); }
.x.anim::after { animation: strike .45s ease-out both; }
@keyframes strike { from { transform: rotate(-28deg) scaleX(0); } to { transform: rotate(-28deg) scaleX(1); } }
[id^="s1__"], [id^="sum__"] { display: inline-block; position: relative; border-radius: 6px; padding: 0 2px; transition: background .3s, box-shadow .3s; }

.flyer { position: fixed; z-index: 50; pointer-events: none; color: var(--chalk-yellow);
  font-family: "Klee One", serif; transition: transform 1.1s cubic-bezier(.5,0,.2,1), opacity 1.1s; }
.flyer .katex { color: var(--chalk-yellow); font-size: 1.25em; }

/* ── figures ── */
.line.figure { white-space: normal; width: min(100%, 460px); background: rgba(0,0,0,.14); border-radius: 10px; padding: 6px; }
.line.figure svg { width: 100%; height: auto; display: block; font-family: "Zen Maru Gothic", "Hiragino Maru Gothic ProN", sans-serif; touch-action: none; }
.fig-grid { stroke: rgba(255,255,255,.09); stroke-width: 1; }
.fig-axis { stroke: var(--chalk); stroke-width: 1.5; }
.fig-text { fill: var(--chalk); font-size: 13px; }
.fig-dim { fill: rgba(243,241,234,.65); font-size: 11px; }
.fig-vec { stroke-width: 3; stroke-linecap: round; }
.fig-vec.va { stroke: var(--chalk-yellow); } .fig-text.va { fill: var(--chalk-yellow); font-weight: 700; }
.fig-vec.vb { stroke: var(--chalk-blue); } .fig-text.vb { fill: var(--chalk-blue); font-weight: 700; }
.fig-angle { fill: none; stroke: var(--chalk-pink); stroke-width: 2; }
.fig-angle-label { fill: var(--chalk-pink); font-size: 14px; }
.fig-curve { fill: none; stroke-width: 2.5; }
.fig-shade { fill: rgba(255,227,122,.28); stroke: none; }
.fig-vline { stroke: var(--chalk-pink); stroke-width: 1.5; stroke-dasharray: 5 4; }
.fig-tangent { stroke: var(--chalk-blue); stroke-width: 2; }
.fig-point { fill: var(--chalk-yellow); } .fig-point-live { fill: var(--chalk-pink); }
.fig-tri { fill: rgba(159,211,255,.12); stroke: var(--chalk); stroke-width: 2; stroke-linejoin: round; }
.fig-vlabel { font-size: 15px; font-weight: 700; } .fig-side { fill: var(--chalk-yellow); }
.fig-handle { fill: rgba(255,255,255,.12); stroke: #fff; stroke-width: 1.5; stroke-dasharray: 3 3; cursor: grab; }
.fig-handle:active { cursor: grabbing; fill: rgba(255,255,255,.28); }
.fig-interactive::after { content: "◎ ドラッグで動かせます"; display: block; font-size: 12px; color: var(--chalk-blue); font-family: "Zen Maru Gothic", sans-serif; margin: 4px 0 0 6px; }
.fig-info .fig-text { font-size: 12.5px; font-variant-numeric: tabular-nums; }

.pause-banner { display: flex; align-items: center; gap: 12px; background: var(--note); color: var(--note-ink); border-radius: 12px; padding: 10px 14px; font-weight: 700; box-shadow: 0 2px 6px rgba(0,0,0,.15); }
.pause-banner[hidden] { display: none; }
.pause-banner span:first-child { flex: 1; }
.pause-banner button { border: 0; border-radius: 999px; padding: 6px 14px; cursor: pointer; }

/* ── notes ── */
.notes { display: flex; flex-direction: column; gap: 12px; align-content: start; }
.note { background: var(--note); color: var(--note-ink); border-radius: 4px; padding: 10px 12px;
  box-shadow: 0 2px 6px rgba(0,0,0,.15); transform: rotate(-1deg); font-family: "Klee One", serif; overflow-x: auto; }
.note:nth-child(even) { transform: rotate(1deg); }
.note .katex { color: var(--note-ink); font-size: 1em; }
.note { white-space: nowrap; overflow: hidden; }
.note.anim-in { animation: pop .45s ease-out both; }
@keyframes pop { from { transform: scale(.6) rotate(-6deg); opacity: 0; } to { opacity: 1; } }
.note.hl { box-shadow: 0 0 0 3px #e8b923, 0 2px 6px rgba(0,0,0,.15); background: var(--note); }
.note.hl .katex { color: var(--note-ink); }

/* ── stage ── */
.stage { display: grid; grid-template-columns: 130px 1fr 130px; align-items: end; gap: 12px; }
.char { margin: 0; text-align: center; transition: transform .3s, filter .3s; filter: saturate(.75) brightness(.97); }
.char svg { width: 100%; height: auto; display: block; }
.char figcaption { font-weight: 700; margin-top: 2px; }
.char.speaking { transform: translateY(-6px) scale(1.04); filter: none; }
.char.speaking svg { animation: bob 1.2s ease-in-out infinite; }
@keyframes bob { 50% { transform: translateY(-3px); } }
.char .mouth { transform-box: fill-box; transform-origin: center; }
.char.speaking .mouth { animation: talk .22s ease-in-out infinite alternate; }
@keyframes talk { from { transform: scaleY(1); } to { transform: scaleY(2.6) scaleX(.85); } }
.char .eyes { transform-box: fill-box; transform-origin: center; animation: blink 4.5s infinite; }
#char-S .eyes { animation-delay: 1.7s; }
@keyframes blink { 0%, 95%, 100% { transform: scaleY(1); } 97% { transform: scaleY(.1); } }

.bubble-wrap { display: flex; align-items: center; min-height: 120px; }
.bubble { position: relative; width: 100%; background: var(--bubble); border: 2px solid var(--border);
  border-radius: 18px; padding: 12px 18px; font-size: clamp(15px, 2vw, 19px); line-height: 1.6;
  box-shadow: 0 3px 10px rgba(0,0,0,.08); transition: background .3s; }
.bubble::after { content: ""; position: absolute; bottom: 22px; width: 18px; height: 18px;
  background: inherit; border: inherit; border-top: 0; border-right: 0; display: none; }
.bubble.T { background: var(--bubble-T); }
.bubble.T::after { display: block; left: -11px; transform: rotate(45deg); }
.bubble.S { background: var(--bubble-S); }
.bubble.S::after { display: block; right: -11px; transform: rotate(-135deg); }
.bubble .who { font-size: .8em; color: var(--muted); font-weight: 700; }
.bubble.nosubs .say { visibility: hidden; }
.bubble.loading .who::after { content: "  音声を準備中…"; font-weight: 500; animation: fade 1s ease-in-out infinite alternate; }
@keyframes fade { from { opacity: .3; } to { opacity: 1; } }

/* ── controls ── */
.controls { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 10px 14px; }
.controls .row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; }
.controls .opts { margin-top: 8px; font-size: 14px; color: var(--muted); }
.controls button { border: 1px solid var(--border); background: var(--bg); border-radius: 8px; padding: 6px 12px; cursor: pointer; }
.controls select { border: 1px solid var(--border); background: var(--bg); border-radius: 6px; padding: 3px 6px; max-width: 200px; }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 700; }
.progress { flex: 1; min-width: 80px; height: 8px; background: var(--border); border-radius: 999px; overflow: hidden; }
.progress div { height: 100%; width: 0; background: var(--accent); transition: width .3s; }
.count { font-variant-numeric: tabular-nums; color: var(--muted); font-size: 14px; }
.msg { margin: 8px 0 0; font-size: 13px; color: #b5651d; }

/* ── overlay ── */
.overlay { position: fixed; inset: 0; background: rgba(20,18,15,.55); display: grid; place-items: center; z-index: 100; padding: 16px; }
.overlay[hidden] { display: none; }
.card { background: var(--surface); border-radius: 16px; padding: 24px 28px; max-width: 460px; text-align: center; box-shadow: 0 10px 40px rgba(0,0,0,.3); }
.card h2 { margin: 0 0 8px; }
.card .formula { font-size: 1.1em; overflow-x: auto; }
.card p { color: var(--muted); line-height: 1.7; }
button.big { font-size: 18px; padding: 10px 28px; border-radius: 999px; border: 0; cursor: pointer; }

/* ── responsive ── */
@media (max-width: 820px) {
  .board-area { grid-template-columns: 1fr; }
  .notes { flex-direction: row; flex-wrap: wrap; }
  .note { flex: 1 1 160px; }
  .board { height: 46vh; min-height: 300px; border-width: 8px; }
  .board-inner { padding: 12px 14px 24px; }
}
@media (max-width: 560px) {
  .stage { grid-template-columns: 64px 1fr 64px; gap: 6px; }
  .char figcaption { font-size: 12px; }
  .bubble { padding: 10px 12px; }
  .bubble-wrap { min-height: 100px; }
  .controls select { max-width: 150px; }
}
@media (prefers-reduced-motion: reduce) {
  .anim-in, .x.anim::after, .char.speaking svg, .char.speaking .mouth, .char .eyes { animation: none !important; }
}


/* ── 操作バー ──
   パソコン（広い画面）: 小さめ・1 行・固定しない / スマホ: 大きいボタン・画面の下に固定。
   「固定」は、プレーヤーの操作バー（#controls）だけ。共有・フィードバックのブロック（.controls.share）は固定しない。 */
#controls button, #controls summary, .scenes button { touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
.transport { display: flex; gap: 10px; align-items: stretch; }
.opts-wrap > summary { cursor: pointer; font-weight: 700; min-height: 44px; display: flex; align-items: center; color: var(--muted); list-style-position: inside; }
#controls .opts label { min-height: 40px; display: inline-flex; align-items: center; gap: 8px; }
#controls .opts input[type=checkbox] { width: 20px; height: 20px; }
.audio-banner { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; background: var(--note); color: var(--note-ink); border-radius: 12px; padding: 10px 12px; margin-bottom: 10px; font-weight: 700; line-height: 1.5; }
.audio-banner[hidden] { display: none; }
.audio-banner span { flex: 1 1 220px; }
.audio-banner button { min-height: 48px; padding: 0 20px; border-radius: 12px; font-size: 17px; border: 0; cursor: pointer; }
.pause-banner button { min-height: 44px; padding: 0 18px; font-size: 16px; }

/* パソコン */
@media (min-width: 721px) {
  #controls { display: grid; grid-template-columns: auto 1fr; gap: 6px 16px; align-items: center; position: static; }
  #controls .audio-banner, #controls .opts-wrap, #controls .msg { grid-column: 1 / -1; }
  #controls .transport button { min-height: 40px; font-size: 17px; border-radius: 8px; }
  #controls .t-side { width: 46px; flex: none; }
  #controls .t-play { flex: 0 0 auto; min-width: 124px; padding: 0 16px; font-size: 16px; font-weight: 700; }
  #controls .row.prog { margin: 0; flex-wrap: nowrap; }
  .progress { height: 8px; }
}

/* スマホ */
@media (max-width: 720px) {
  #controls { position: sticky; bottom: 0; z-index: 40; margin: 0 -16px; border-radius: 18px 18px 0 0; padding: 10px 16px calc(10px + env(safe-area-inset-bottom)); box-shadow: 0 -8px 20px rgba(0,0,0,.25); }
  #controls .transport button { min-height: 60px; font-size: 24px; border-radius: 14px; }
  #controls .t-side { width: 68px; flex: none; }
  #controls .t-play { flex: 1; font-size: 22px; font-weight: 700; letter-spacing: .02em; }
  #controls .row.prog { margin-top: 10px; flex-wrap: nowrap; }
  .progress { height: 12px; }
  #controls .opts label { min-height: 44px; }
  #controls .opts input[type=checkbox] { width: 22px; height: 22px; }
  .audio-banner button { min-height: 52px; font-size: 18px; border-radius: 14px; }
  .scenes button { padding: 9px 13px; font-size: 14px; min-height: 40px; }
  .top { padding-bottom: 4px; }
}
@media (max-width: 380px) { #controls .t-side { width: 58px; } #controls .t-play { font-size: 20px; } }
