/* =========================================================================
   app.css — 東大への入口 物理
   配色: 紙色の背景・墨色の文字・アクセントは青1色（暗いテーマにも自動対応）
   ========================================================================= */

/* --- 色と寸法の変数 --- */
:root {
  --paper: #fbfaf6;        /* 背景（紙） */
  --panel: #efece3;        /* 少し濃い面 */
  --ink: #1f2328;          /* 墨色の文字 */
  --sub: #6b6f76;          /* 補助の文字 */
  --line: #d6d2c6;         /* 罫線 */
  --ghost: #c3bfb4;        /* まだ読んでいない文字 */
  --accent: #1d5fbf;       /* 青（アクセント） */
  --accent-soft: rgba(29, 95, 191, 0.14);
  --warm: #c2622d;         /* 熱・損失（図の中だけで控えめに） */
  --warm-soft: rgba(194, 98, 45, 0.22);
  --radius: 12px;
  --rsvp-font: 2.4rem;     /* RSVP の文字サイズ（1塊だけなので大きく） */
  --font: "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP", "Yu Gothic", system-ui, sans-serif;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --paper: #16181c;
    --panel: #22252b;
    --ink: #eceae4;
    --sub: #a3a7ae;
    --line: #3a3e45;
    --ghost: #4f535a;
    --accent: #7fb0ff;
    --accent-soft: rgba(127, 176, 255, 0.18);
    --warm: #f0a070;
    --warm-soft: rgba(240, 160, 112, 0.25);
  }
}

/* --- 全体 --- */
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--paper); color: var(--ink); font-family: var(--font); }
body { line-height: 1.7; -webkit-font-smoothing: antialiased; }
a { color: var(--accent); }
button, input, select { font: inherit; color: inherit; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* =========================================================================
   カリキュラム地図（index.html）
   ========================================================================= */
.home { max-width: 980px; margin: 0 auto; padding: 32px 20px 80px; padding-left: max(16px, env(safe-area-inset-left)); padding-right: max(16px, env(safe-area-inset-right)); overflow-wrap: anywhere; }
body:has(.home) { overflow-x: hidden; }
.home-hero h1 { font-size: clamp(1.6rem, 7vw, 2rem); margin: 0 0 8px; letter-spacing: 0.02em; }
.home-hero p { margin: 4px 0; color: var(--sub); }
.home-actions { display: flex; gap: 12px; flex-wrap: wrap; margin: 20px 0 8px; }
.btn { display: inline-flex; align-items: center; gap: 8px; min-height: 44px; max-width: 100%; padding: 10px 18px; border-radius: 999px; border: 1.5px solid var(--ink); background: transparent; cursor: pointer; text-decoration: none; color: var(--ink); font-weight: 600; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--paper); }
.btn:hover { filter: brightness(1.05); }
.btn:focus-visible, .ctl:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.howto { margin: 24px 0; padding: 16px 20px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel); }
.howto h2 { font-size: 1.05rem; margin: 0 0 6px; }
.howto ul { margin: 0; padding-left: 1.2em; }
.howto kbd, .kbd { font-family: ui-monospace, monospace; font-size: 0.85em; padding: 1px 6px; border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 5px; background: var(--paper); }
.progress-summary { margin: 8px 0 24px; color: var(--sub); }
.subject { margin-top: 36px; }
.subject > h2 { font-size: 1.4rem; margin: 0 0 4px; padding-bottom: 6px; border-bottom: 2px solid var(--ink); }
.unit { margin-top: 18px; }
.unit > h3 { font-size: 1rem; color: var(--sub); margin: 0 0 6px; font-weight: 600; }
.lesson-list { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(min(280px, 100%), 1fr)); gap: 8px; }
.lesson-link { display: flex; gap: 10px; align-items: baseline; min-height: 48px; padding: 12px 14px; border: 1px solid var(--line); border-radius: 10px; text-decoration: none; color: var(--ink); background: var(--paper); }
.lesson-link:hover { border-color: var(--accent); }
.lesson-link .no { font-variant-numeric: tabular-nums; color: var(--sub); font-size: 0.85rem; min-width: 2.4em; }
.lesson-link .done { margin-left: auto; color: var(--accent); font-weight: 700; }
.notice { padding: 14px 18px; border-radius: var(--radius); border: 2px solid var(--warm); background: var(--warm-soft); margin: 16px 0; }
.notice code { font-size: 0.95em; }

/* =========================================================================
   学習プレイヤー（player.html）
   画面は「ヘッダー1行 / 図 / RSVP 枠 / 操作バー」の4段。スクロールせずに全部が見える
   高さが足りないときは図だけが縮む（文字とボタンは切れない）
   ========================================================================= */
.player {
  height: 100vh;             /* 100dvh 非対応ブラウザ向け */
  height: 100dvh;            /* アドレスバーの出入りに追従する高さ */
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  grid-template-columns: minmax(0, 1fr); /* 長いレッスン名で横に広がらないように */
  overflow: hidden;
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

/* --- ヘッダー（今の単元とレッスン名を1行で） --- */
.bar { display: flex; align-items: center; gap: 8px; padding: 6px 12px; padding-top: max(6px, env(safe-area-inset-top)); border-bottom: 1px solid var(--line); min-height: 48px; min-width: 0; }
.bar .crumb { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 0.95rem; }
.bar .crumb .unit-name { color: var(--sub); }
.bar .crumb .lesson-name { font-weight: 700; }
.bar .count { color: var(--sub); font-size: 0.85rem; font-variant-numeric: tabular-nums; white-space: nowrap; }
.icon-btn { flex: none; display: inline-flex; align-items: center; min-height: 36px; border: 1px solid var(--line); background: transparent; border-radius: 8px; padding: 4px 10px; cursor: pointer; text-decoration: none; color: var(--ink); font-size: 0.9rem; white-space: nowrap; }
.icon-btn:hover { border-color: var(--accent); }

/* --- 中央: 図と RSVP 枠を1本の読みやすいカラムに縦に並べる --- */
.stage { min-height: 0; min-width: 0; display: flex; justify-content: center; padding: 8px 12px 6px; }
.stage-inner {
  /* 広い画面では「高さから逆算した16:9の幅」までに抑え、図が縦に収まるようにする */
  width: min(100%, 960px, calc((100dvh - var(--chrome-h)) * 16 / 9));
  --chrome-h: 330px;
  min-width: 0; min-height: 0;
  display: flex; flex-direction: column; justify-content: center; gap: 8px;
}
/* 図: 横幅いっぱい・16:9 が基本。高さが足りなければ縮み、SVG は中で縮小表示される */
.canvas-wrap { flex: 0 1 auto; width: 100%; aspect-ratio: 16 / 9; min-height: 0; border: 1px solid var(--line); border-radius: var(--radius); background: var(--paper); overflow: hidden; }
.canvas-wrap svg { width: 100%; height: 100%; display: block; }
.canvas-wrap svg text { font-family: var(--font); }

/* --- RSVP 枠（位置・高さは固定。塊が替わってもガタつかない） --- */
.reader { flex: none; position: relative; border-radius: var(--radius); background: var(--panel); padding: 8px 14px 12px; }
.reader-meta { display: flex; align-items: center; gap: 10px; height: 18px; font-size: 0.78rem; color: var(--sub); }
.reader-meta .tag { color: var(--accent); font-weight: 700; letter-spacing: 0.05em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.reader-meta .beat-no { margin-left: auto; font-variant-numeric: tabular-nums; white-space: nowrap; }
.rsvp {
  position: relative;
  height: calc(var(--rsvp-font) * 2.3);
  font-size: var(--rsvp-font);
  font-weight: 600;
  line-height: 1.2;
  display: flex; align-items: center;
  overflow: hidden;
}
/* 焦点の縦線（目印）。焦点の文字は塊の左寄りにあるので、線も中央よりやや左（--focus-x）に置く */
.rsvp { --focus-x: 42%; }
.rsvp::before, .rsvp::after { content: ""; position: absolute; left: var(--focus-x); width: 2px; height: 0.28em; background: var(--line); transform: translateX(-1px); }
.rsvp::before { top: 0.12em; }
.rsvp::after { bottom: 0.12em; }
/* 左・焦点・右の3つ。左の列を「縦線 − 半文字」で止め、焦点の1文字（幅1em）の中心を縦線に合わせる */
.rsvp-line { width: 100%; display: grid; grid-template-columns: minmax(0, calc(var(--focus-x) - 0.5em)) minmax(1em, auto) minmax(0, 1fr); align-items: baseline; white-space: nowrap; }
.rsvp-line .f { text-align: center; }
.rsvp-line .l { text-align: right; overflow: hidden; }
.rsvp-line .r { text-align: left; overflow: hidden; }
.rsvp-line .f.hl { color: var(--accent); }
.rsvp-line b { font-weight: 800; }
.rsvp .katex { font-size: 0.92em; }
/* 片側に入らない長い塊（長い数式など）は、焦点合わせをやめて中央寄せ */
.rsvp.centered::before, .rsvp.centered::after { display: none; }
.rsvp.centered .rsvp-line { display: block; text-align: center; overflow: hidden; }
.rsvp.centered .rsvp-line > span { overflow: visible; }
.beat-bar { position: absolute; left: 14px; right: 14px; bottom: 5px; height: 3px; background: var(--line); border-radius: 2px; overflow: hidden; }
.beat-bar i { display: block; height: 100%; width: 0; background: var(--accent); }

/* --- レッスン内のビート一覧（小さな区切りバー） --- */
.strip { flex: none; display: flex; gap: 3px; height: 8px; }
.strip button { flex: 1; min-width: 0; border: 0; padding: 0; border-radius: 3px; background: var(--line); cursor: pointer; }
.strip button.past { background: var(--accent-soft); }
.strip button.now { background: var(--accent); }

/* --- 操作バー --- */
.controls { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 8px 16px; padding: 8px 12px; padding-bottom: max(10px, env(safe-area-inset-bottom)); border-top: 1px solid var(--line); font-size: 0.88rem; min-width: 0; }
.ctl-group { display: flex; align-items: center; gap: 6px; }
.ctl { border: 1.5px solid var(--ink); background: transparent; border-radius: 999px; min-width: 44px; height: 40px; padding: 0 12px; cursor: pointer; font-weight: 700; white-space: nowrap; }
.ctl.play { background: var(--accent); border-color: var(--accent); color: var(--paper); min-width: 100px; }
.ctl:hover { filter: brightness(1.08); }
.slider { display: flex; align-items: center; gap: 6px; min-width: 0; }
.slider label { color: var(--sub); white-space: nowrap; }
.slider input[type=range] { width: 110px; min-width: 0; accent-color: var(--accent); }
.slider output { min-width: 5.6em; font-variant-numeric: tabular-nums; font-weight: 700; white-space: nowrap; }
.chunk { display: flex; align-items: center; gap: 6px; color: var(--sub); white-space: nowrap; }
.chunk select { height: 36px; border: 1px solid var(--line); border-radius: 8px; background: var(--paper); padding: 0 6px; color: var(--ink); font-weight: 700; }
.more { display: contents; }
.more-btn { display: none; font-weight: 600; }
.check { display: flex; align-items: center; gap: 4px; color: var(--sub); white-space: nowrap; }
.check input { accent-color: var(--accent); }

/* --- 目次（左から出るパネル） --- */
.toc { position: fixed; inset: 0 auto 0 0; width: min(420px, 92vw); background: var(--paper); border-right: 1px solid var(--line); box-shadow: 8px 0 30px rgba(0, 0, 0, 0.12); transform: translateX(-102%); transition: transform 0.2s ease; z-index: 20; display: flex; flex-direction: column; }
.toc.open { transform: none; }
.toc-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px; border-bottom: 1px solid var(--line); }
.toc-body { overflow: auto; padding: 8px 16px 40px; font-size: 0.92rem; }
.toc-body h4 { margin: 18px 0 4px; font-size: 1rem; border-bottom: 1.5px solid var(--ink); }
.toc-body h5 { margin: 10px 0 2px; color: var(--sub); font-weight: 600; font-size: 0.85rem; }
.toc-body button { display: flex; width: 100%; gap: 8px; text-align: left; border: 0; background: transparent; padding: 5px 6px; border-radius: 6px; cursor: pointer; }
.toc-body button:hover { background: var(--panel); }
.toc-body button.now { background: var(--accent-soft); font-weight: 700; }
.toc-body button .d { margin-left: auto; color: var(--accent); }
.scrim { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.25); z-index: 19; display: none; }
.scrim.show { display: block; }

/* --- シーン内のHTML（公式カード・比較パネル） --- */
.svg-html { width: 100%; height: 100%; font-family: var(--font); color: var(--ink); }
.f-card { height: 100%; display: flex; flex-direction: column; justify-content: center; align-items: center; gap: 10px; }
.f-main { font-size: 2.6rem; color: var(--accent); transition: opacity 0.2s; }
.f-main .katex-display { margin: 0; }
.f-labels { list-style: none; margin: 6px 0 0; padding: 0; font-size: 1.25rem; line-height: 1.8; text-align: left; }
.f-labels li { transition: opacity 0.3s; }
.f-note { margin: 8px 0 0; font-size: 1.05rem; color: var(--sub); transition: opacity 0.3s; }
.c-wrap { height: 100%; display: grid; grid-template-columns: 1fr 1fr; gap: 22px; }
.c-side { position: relative; border: 2px solid var(--line); border-radius: 16px; padding: 22px 22px; background: var(--paper); transition: opacity 0.3s; }
.c-side.bad { border-color: var(--sub); }
.c-side.good { border-color: var(--accent); }
.c-side h3 { margin: 0 0 12px; font-size: 1.35rem; }
.c-side ul { margin: 0; padding-left: 1.1em; font-size: 1.2rem; line-height: 1.8; }
.c-mark { position: absolute; top: 10px; right: 16px; font-size: 3rem; line-height: 1; font-weight: 800; transform: scale(0); transition: transform 0.2s; }
.c-side.bad .c-mark { color: var(--sub); }
.c-side.good .c-mark { color: var(--accent); }
.tex-fallback { font-family: ui-monospace, monospace; }

/* --- 画面が狭い・低いとき（スマホ縦・横）: 操作バーを2段にまとめ、細かい設定は「設定」に畳む --- */
@media (max-width: 720px), (max-height: 560px) {
  .controls { gap: 6px 8px; font-size: 0.85rem; }
  .more-btn { display: inline-flex; align-items: center; padding: 0 12px; }
  .more { display: none; }
  .controls.open .more { display: flex; flex-wrap: wrap; justify-content: center; gap: 6px 14px; flex-basis: 100%; order: 6; }
  .slider input[type=range] { width: 96px; }
  .chunk > span { display: none; }
}
@media (max-width: 720px) {
  :root { --rsvp-font: clamp(1.7rem, 7.6vw, 2.4rem); }
  .bar { padding-left: 10px; padding-right: 10px; gap: 6px; }
  .bar .count, .bar .crumb .unit-name { display: none; }
  .stage { padding: 6px 10px 4px; }
  .stage-inner { width: 100%; gap: 6px; }
  /* 読む速さは2段目に横幅いっぱいで置く（指で動かしやすく） */
  .slider.read { order: 5; flex-basis: 100%; justify-content: center; }
  .slider.read input[type=range] { flex: 1; max-width: 260px; }
  .f-main { font-size: 1.8rem; }
  .f-labels, .c-side ul { font-size: 1rem; }
}
/* 高さが厳しい画面（375×667 など）: 余白を詰める。縮むのは図だけ */
@media (max-width: 720px) and (max-height: 700px) {
  :root { --rsvp-font: clamp(1.6rem, 7.4vw, 2.2rem); }
  .bar { min-height: 44px; }
  .reader { padding: 6px 12px 10px; }
  .rsvp { height: calc(var(--rsvp-font) * 2.1); }
  .ctl { height: 38px; }
}
/* スマホ横向き: 1段にまとめ、RSVP を少し小さく */
@media (max-height: 560px) and (min-width: 561px) {
  :root { --rsvp-font: 1.8rem; }
  .bar { min-height: 40px; padding-top: max(4px, env(safe-area-inset-top)); padding-bottom: 4px; }
  .stage-inner { --chrome-h: 250px; }
  .reader { padding: 4px 12px 9px; }
  .rsvp { height: calc(var(--rsvp-font) * 1.9); }
  .ctl { height: 36px; }
}
@media (prefers-reduced-motion: reduce) {
  .toc { transition: none; }
}
