/* css/viewer.css */

/* 全体レイアウト (記事形式をデフォルト化) */
body {
  height: auto;
  min-height: 100vh;
  margin: 0;
  overflow-y: auto; /* 常に縦スクロール */
  background: var(--col-bg);
  display: flex;
  flex-direction: column;
}

/* ポインターモード時のカーソル変更・レイアウト固定 */
body.pointer-active {
  cursor: crosshair;
  overflow: hidden;
  overflow-x: hidden;
  width: 100%;
  margin: 0;
  padding: 0;
}
body.pointer-active .explanation-area,
body.pointer-active .sim-embed {
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

/* --- 録画モード（ポインターモード時）：タブバー・タブ移動ボタンは表示、終了ボタンは重ならない位置に --- */
body.pointer-active {
  height: 100vh;
  height: 100dvh;
}

/* 操作モード: 画面上部はタブのみ（タイトル行は非表示）、詰めて表示 */
body.pointer-active .card-tabs {
  /* 画面拡大時もサイズ・文字も同じ比率で固定表示（逆スケールで打ち消し） */
  transform: scale(calc(1 / var(--zoom-scale, 1))) translateZ(0);
  transform-origin: top center;
}

/* 操作モード: タブバー内の文字サイズをpxで固定し拡大時も同じ比率で表示 */
body.pointer-active .card-tabs .card-tab {
  font-size: 14px;
}
body.pointer-active .viewer-toolbar {
  display: none !important;
}
body.pointer-active .viewer-page-header {
  display: none !important;
}
body.pointer-active .skip-link {
  display: none !important;
}

/* 操作モード時は画面下部のフッター（ハブ等）を非表示 */
body.pointer-active .site-footer {
  display: none !important;
}

body.pointer-active .viewer-container {
  flex: 1;
  min-height: 0;
  position: relative;
  width: 100%;
  overflow-x: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y pinch-zoom;
  background: var(--col-bg);
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}
/* ポインターモード時は解説エリアでも指で拡大縮小を許可 */
body.pointer-active .explanation-area,
body.pointer-active .viewer-split-content {
  touch-action: pan-y pinch-zoom;
}

body.pointer-active .viewer-split-content {
  min-height: 100%;
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body.pointer-active .explanation-area {
  min-height: 100%;
  padding-top: 24px;
  padding-left: 20px;
  padding-right: 20px;
  margin-left: auto;
  margin-right: auto;
  background: var(--col-bg);
  box-sizing: border-box;
}

body.pointer-active .card-tabs__list {
  position: relative;
  top: 0;
  background: var(--col-bg);
  z-index: 10;
  padding: 8px 100px 8px 24px; /* 右に終了ボタン用の余白 */
  margin: 0;
  border-bottom: 1px solid #e2e8f0;
  min-height: 52px;
  box-sizing: border-box;
}

/* ポインターモード時は疑問点・感想・リアクションボタンを非表示（録画用） */
body.pointer-active .card-reaction-footer {
  display: none !important;
}

/* 操作モード時は広告枠を非表示にしてレイアウトを安定 */
body.pointer-active .ad-slot.ad-slot--sidebar {
  display: none !important;
}

/* 操作モード用フロート（通常時は非表示） */
.recording-float-bar {
  display: none;
  position: fixed;
  bottom: calc(48px + env(safe-area-inset-bottom, 0px));
  right: calc(24px + env(safe-area-inset-right, 0px));
  z-index: 2147483647;
  align-items: center;
  justify-content: center;
  pointer-events: auto;
  transform: translateZ(0);
  isolation: isolate;
}

/* 操作モード時: 終了ボタンをタブバー右端に固定表示 */
body.pointer-active .recording-float-bar {
  display: flex;
  top: 0;
  right: calc(12px + env(safe-area-inset-right, 0px));
  bottom: auto;
  height: 52px;
  align-items: center;
  justify-content: flex-end;
  padding-right: 0;
  /* 画面拡大時もサイズを変えず固定表示 */
  transform: scale(calc(1 / var(--zoom-scale, 1))) translateZ(0);
  transform-origin: top right;
}

/* 操作モード時: 終了ボタンをタブに馴染む控えめなデザインに（文字はpxで固定・拡大時も同じ比率） */
body.pointer-active .recording-float-btn-exit {
  padding: 8px 14px;
  min-height: 36px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  background: transparent;
  color: var(--col-text-sub, #64748b);
  box-shadow: none;
  border: 1px solid transparent;
}

body.pointer-active .recording-float-btn-exit:hover {
  background: rgba(59, 130, 246, 0.06);
  color: var(--col-text, #1e293b);
  border-color: #e2e8f0;
}

body.pointer-active .recording-float-btn-exit:active {
  background: rgba(0, 0, 0, 0.04);
}

.recording-float-btn {
  padding: 10px 18px;
  min-height: 44px;
  min-width: 44px;
  border: none;
  border-radius: 24px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s, color 0.2s, border-color 0.2s;
  font-family: var(--font-base);
}

.recording-float-btn:active {
  transform: scale(0.97);
}

.recording-float-btn-exit {
  background: var(--col-accent);
  color: #fff;
  box-shadow: 0 2px 16px rgba(244, 63, 94, 0.35);
}

body:not(.pointer-active) .recording-float-btn-exit:hover {
  background: #e11d48;
  color: #fff;
}

/* 解説ページ全体で指の縦スクロール・ピンチズームを常に許可 */
.viewer-container {
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 100%;
  touch-action: pan-y pinch-zoom;
  -webkit-overflow-scrolling: touch;
  contain: layout style paint;
}
.viewer-container .explanation-area,
.viewer-container .viewer-split-content {
  touch-action: pan-y pinch-zoom;
}

/* --- 1. 上部ヘッダー --- */
/* --- 固定ツールバー（ボタン類のみ・画面上部固定） --- */
.viewer-toolbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: var(--z-header, 100);
  display: flex;
  align-items: center;
  padding: 8px 12px;
  padding-top: max(8px, env(safe-area-inset-top, 0px));
  padding-left: max(12px, env(safe-area-inset-left, 0px));
  padding-right: max(12px, env(safe-area-inset-right, 0px));
  pointer-events: none;
  gap: 8px;
}
.viewer-toolbar > * { pointer-events: auto; }
.toolbar-spacer { flex: 1; }

/* --- ページタイトル（メインサイト base.css の header と同じスタイル） --- */
.viewer-page-header {
  max-width: var(--card-width, 860px);
  width: 100%;
  margin: 0 auto 40px auto;
  text-align: center;
  padding: 60px 20px 0;
  box-sizing: border-box;
}
.viewer-page-header p {
  color: #8b5cf6;
  font-family: "Poppins", sans-serif;
  font-weight: 600;
  letter-spacing: 0.1em;
  font-size: 0.9em;
  margin-bottom: 5px;
  opacity: 0.9;
  text-align: left;
}
.viewer-page-header p a {
  text-decoration: none;
  color: inherit;
}
.viewer-page-header h1 {
  font-size: 2.2em;
  margin-top: 5px;
  background: linear-gradient(135deg, #0ea5e9, #8b5cf6);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  border-bottom: 3px solid transparent;
  border-image: linear-gradient(90deg, #0ea5e9, #8b5cf6) 1;
  display: inline-block;
  padding-bottom: 10px;
  letter-spacing: 0.02em;
}

/* --- タブバー（メインサイト components.css と同じスタイルを適用） --- */
.card-tabs {
  position: sticky;
  top: 0;
  z-index: 500;
  max-width: var(--card-width);
  width: 100%;
  margin: 0 auto;
  padding: 0;
  background: transparent;
  border: none;
  border-radius: 0;
  box-shadow: none;
}

.card-tabs__list {
  display: flex;
  flex-wrap: nowrap;
  gap: 6px;
  padding: 10px 16px 12px;
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
  scrollbar-color: var(--card-border) transparent;
}
.card-tabs__list::-webkit-scrollbar { height: 6px; }
.card-tabs__list::-webkit-scrollbar-track { background: transparent; }
.card-tabs__list::-webkit-scrollbar-thumb { background: var(--card-border); border-radius: 3px; }
.card-tabs__list::-webkit-scrollbar-thumb:hover { background: var(--text-sub); }

.card-tab {
  flex-shrink: 0;
  padding: 8px 14px;
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text-sub);
  background: rgba(255, 255, 255, 0.6);
  border: 1px solid var(--card-border);
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  white-space: nowrap;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}
.card-tab:hover {
  color: var(--col-main);
  border-color: var(--col-main);
  background: rgba(14, 165, 233, 0.06);
  transform: translateY(-1px);
}
.card-tab--active {
  color: #fff;
  border-color: var(--col-main);
  background: linear-gradient(135deg, var(--col-main), #38bdf8);
  box-shadow: 0 2px 8px rgba(14, 165, 233, 0.25);
}
.card-tab:active {
  transform: scale(0.96);
  transition-duration: 0.1s;
}

/* タブ幅はカード幅と同じ（.card-tabs の max-width: var(--card-width) がそのまま適用） */

/* 解説画面下・左右に固定のタブ移動ボタン（複数タブ時のみ表示） */
.explanation-tab-nav {
  display: none;
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(24px + env(safe-area-inset-bottom, 0px));
  z-index: var(--z-header, 100);
  pointer-events: none;
  padding: 0 20px;
  box-sizing: border-box;
}

.explanation-tab-nav.is-visible {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.explanation-tab-nav .tab-nav-btn {
  pointer-events: auto;
  width: 48px;
  height: 48px;
  min-width: 48px;
  min-height: 48px;
  border-radius: 50%;
  border: none;
  background: rgba(255, 255, 255, 0.95);
  color: var(--col-text-sub, #64748b);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.12);
  cursor: pointer;
  font-size: 1.4rem;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.2s ease, background 0.2s ease, color 0.2s ease, box-shadow 0.2s ease;
  -webkit-tap-highlight-color: transparent;
}

.explanation-tab-nav .tab-nav-btn:hover:not(:disabled) {
  background: var(--col-primary, #3b82f6);
  color: #fff;
  box-shadow: 0 6px 24px rgba(59, 130, 246, 0.35);
  transform: scale(1.05);
}

.explanation-tab-nav .tab-nav-btn:active:not(:disabled) {
  transform: scale(0.97);
}

.explanation-tab-nav .tab-nav-btn:disabled {
  opacity: 0.35;
  cursor: default;
}

/* 操作モードで画面拡大時も左右タブボタンはサイズ・文字も同じ比率で固定表示 */
body.pointer-active .explanation-tab-nav.is-visible {
  transform: scale(calc(1 / var(--zoom-scale, 1))) translateZ(0);
  transform-origin: bottom center;
}

/* 操作モード: 左右タブボタン内のアイコン（‹ ›）サイズをpxで固定し拡大時も同じ比率で表示 */
body.pointer-active .explanation-tab-nav .tab-nav-btn {
  font-size: 22px;
}

/* 操作モードでも左右のタブ移動ボタンは表示（終了ボタンは上にずらして重ならない） */

/* ページ上部のタイトルを非表示（固定バーにタイトルがあるため） */
.prob-title-sub {
  display: none !important;
}

/* 戻るボタンは削除済み — パンくずリストでナビゲーション */

/* h1 は .viewer-page-header 内で表示（上部でスタイル定義済み） */

.btn-sm {
  white-space: nowrap; font-size: 0.78rem; padding: 5px 10px;
  border-radius: 8px; border: 1px solid var(--col-primary);
  background: rgba(255, 255, 255, 0.85); color: var(--col-primary); cursor: pointer;
  transition: all 0.2s;
  flex-shrink: 0;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.3;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}
.btn-sm:hover { background: var(--col-primary); color: #fff; }
.btn-sm.active {
  background: var(--col-accent);
  border-color: var(--col-accent);
  color: #fff;
  box-shadow: 0 0 10px rgba(244, 63, 94, 0.4);
}

.header-actions {
  display: flex;
  gap: 6px;
  align-items: center;
  flex-shrink: 0;
}

/* 生徒画面: 操作モードボタンを非表示にしてヘッダーを詰める */
body.viewer-student #btn-toggle-pointer {
  display: none !important;
}

/* prob-title-main（解説HTML内のh2タイトル）は不要 — 非表示 */
.prob-title-main {
  display: none !important;
}

/* QRトリガー（ブックマークの左） */
.qr-trigger {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  padding: 6px 8px;
  min-width: auto;
  flex-shrink: 0;
}
.qr-trigger-icon {
  font-size: 1.1rem;
  line-height: 1;
  opacity: 0.9;
}
.qr-trigger-label {
  font-size: 0.8rem;
}

/* QRコード拡大モーダル */
.qr-modal {
  position: fixed;
  inset: 0;
  z-index: 10000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  pointer-events: none;
  visibility: hidden;
  opacity: 0;
  transition: visibility 0.2s, opacity 0.2s;
}
.qr-modal.is-open {
  pointer-events: auto;
  visibility: visible;
  opacity: 1;
}
.qr-modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(4px);
}
.qr-modal-content {
  position: relative;
  background: #fff;
  border-radius: 16px;
  padding: 28px 32px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.25);
  max-width: 100%;
  text-align: center;
}
.qr-modal-close {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 36px;
  height: 36px;
  border: none;
  background: #f1f5f9;
  color: #475569;
  font-size: 1.5rem;
  line-height: 1;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s, color 0.2s;
}
.qr-modal-close:hover {
  background: #e2e8f0;
  color: #0f172a;
}
.qr-modal-title {
  margin: 0 0 20px;
  font-size: 1rem;
  color: var(--col-text-sub);
}
.qr-modal-canvas-wrap {
  width: 220px;
  height: 220px;
  margin: 0 auto 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fff;
  border: 1px solid #e2e8f0;
  border-radius: 12px;
  overflow: hidden;
}
.qr-modal-canvas-wrap canvas,
.qr-modal-canvas-wrap img {
  max-width: 100%;
  max-height: 100%;
  width: 200px;
  height: 200px;
  object-fit: contain;
  display: block;
}
.qr-modal-canvas-wrap table {
  width: 200px !important;
  height: 200px !important;
  border-collapse: collapse;
  table-layout: fixed;
}
.qr-modal-canvas-wrap table td {
  width: auto;
  height: auto;
  padding: 0;
  margin: 0;
  border: none;
}
.qr-modal-url {
  margin: 0;
  font-size: 0.75rem;
  color: var(--col-text-sub);
  word-break: break-all;
  max-width: 280px;
  margin-left: auto;
  margin-right: auto;
}

.btn-bookmark.bookmarked {
  background: var(--col-warning, #f59e0b);
  border-color: var(--col-warning, #f59e0b);
  color: #fff;
}

/* --- 2. メインエリア --- */
.viewer-split-content {
  flex: 1;
  display: block; /* Flex廃止 -> ブロック表示 */
  padding: 0;
  overflow: visible;
  position: relative;
}

/* 左: シミュレーションエリア (スライド用なので非表示) */
.simulation-area {
  display: none;
}

/* シミュレーション操作パネル（枠下に配置・重なり防止） */
.sim-controls-panel {
  position: static;
  transform: none;
  width: 100%;
  background: #f1f5f9;
  padding: 10px 20px;
  border-top: 1px solid #e2e8f0;
  border-radius: 0 0 12px 12px;
  box-shadow: none;
  display: flex; gap: 15px; align-items: center;
  justify-content: center;
  z-index: var(--z-sim-controls, 200);
  box-sizing: border-box;
}
.sim-slider-wrap { display: flex; align-items: center; gap: 8px; font-size: 0.85rem; font-weight: bold; }


/* 右: 解説エリア（カード幅と統一） */
.explanation-area {
  box-sizing: border-box;
  width: 100%;
  max-width: var(--card-width, 860px);
  margin: 0 auto;
  background: transparent;
  border-radius: 0;
  contain: layout style;
  box-shadow: none;
  overflow: visible;
  padding: 0 0 100px;
  scroll-snap-type: none;
  will-change: scroll-position;
  transition: background var(--transition-normal, 0.3s);
}

/* --- 埋め込みシミュレーション (Inline Simulation) --- */
.sim-embed {
  width: 100%;
  max-width: 460px; /* components.css と統一 — 授業ページの .interactive-area (640px) よりコンパクト */
  min-height: 280px;
  height: auto;
  background: #f8fafc;
  border: 1px solid #e2e8f0;
  border-radius: 12px;
  margin: 24px auto; /* 中央寄せ */
  position: relative;
  overflow: visible;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  justify-content: center;
  box-shadow: inset 0 0 10px rgba(0,0,0,0.02);
  box-sizing: border-box;
}

/* p5.jsが生成するラッパーdivの幅制約を解除 */
.sim-embed > div,
.sim-embed > main {
  width: 100%;
  box-sizing: border-box;
}

/* シミュレーション内のCanvas操作を有効化（スクロール阻害防止） */
.sim-embed canvas {
  touch-action: none;
  display: block;
  max-width: 100%;
  /* タッチ操作の最適化 */
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
  /* タッチ時のハイライトを無効化 */
  -webkit-tap-highlight-color: transparent;
}

/* --- ポインター用キャンバス（解説に張り付きスクロール連動） --- */
#pointer-canvas.pointer-canvas-inline {
  position: absolute;
  top: 0;
  left: 0;
  z-index: var(--z-pointer, 1000);
  pointer-events: none;
  /* touch-action は指定しない（下のコンテナの pan-y pinch-zoom を透過） */
  /* サイズは JS で container に合わせて設定 */
}

/* --- MathJax --- */
mjx-container { font-size: 1.15em !important; }

/* 数式が長い場合に横スクロールさせる (スマホ対策) */
mjx-container[display="true"] {
  overflow-x: auto;
  overflow-y: hidden;
  max-width: 100%;
  display: block !important;
  -webkit-overflow-scrolling: touch;
}

/* --- カード内リアクションUI (追加) --- */
.card {
  position: relative;
  margin-bottom: 2em;
  padding-bottom: 0.5em;
  contain: layout style;
  content-visibility: auto;
  box-sizing: border-box;
}

/* 動画タブ: 埋め込みまわりの余白を広めに */
.card.youtube-tab {
  padding-left: 20px;
  padding-right: 20px;
  padding-top: 8px;
  padding-bottom: 24px;
}
.youtube-embed-wrap {
  width: 100%;
  max-width: 100%;
  margin: 20px 0 28px;
  box-sizing: border-box;
}
.youtube-embed-container {
  position: relative;
  width: 100%;
  padding-bottom: 56.25%; /* 16:9 */
  height: 0;
  overflow: hidden;
  border-radius: 8px;
  background: #000;
}
.youtube-embed-container iframe {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  border: 0;
}
.youtube-embed-container .youtube-fallback-link {
  display: inline-block;
  padding: 12px 24px;
  background: #ff0000;
  color: #fff;
  border-radius: 8px;
  text-decoration: none;
  margin: 20px 0;
}

.card-reaction-footer {
  margin-top: 15px;
  padding-top: 10px;
  border-top: 1px dashed #e2e8f0;
  display: flex;
  align-items: flex-start; /* メモ欄が広がってもボタンは上寄せ */
  justify-content: space-between;
  gap: 15px; /* 要素間の隙間を確保 */
  flex-wrap: wrap; /* 幅が狭い場合に折り返す安全策 */
}

.card-memo-area {
  flex: 1;
  min-width: 200px; /* 極端に狭くなるのを防ぐ */
  /* ↓重要: Flexアイテム内でのコンテンツはみ出し防止 */
  min-width: 0; 
}

.card-memo-input {
  width: 100%;
  height: 40px;
  min-height: 36px;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 0.85rem;
  resize: vertical;
  background: #f8fafc;
  transition: all 0.2s;
  box-sizing: border-box; /* パディングを含めて幅計算し、はみ出し防止 */
}
.card-memo-input:focus {
  background: #fff;
  border-color: var(--col-primary);
  outline: none;
  height: 60px; /* フォーカス時に少し広げる */
}

.card-reaction-buttons {
  flex-shrink: 0;
  display: flex;
  gap: 8px;
  margin-left: auto; /* 右寄せを維持 */
}

.btn-reaction {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  background: #fff;
  cursor: pointer;
  font-size: 1.2rem;
  transition: all 0.2s;
  color: #64748b;
}

.btn-reaction:hover {
  background: #f1f5f9;
  transform: translateY(-2px);
}

.btn-reaction.active {
  background: #fff1f2; /* 薄い赤 */
  border-color: var(--col-primary);
  color: var(--col-primary);
  box-shadow: 0 2px 8px rgba(244, 63, 94, 0.2);
}

.btn-reaction span {
  font-size: 0.65rem;
  margin-top: 2px;
  font-weight: bold;
}

/* --- レスポンシブ対応 (スマホ・タブレット) --- */
@media (max-width: 768px) {
  body {
    background: #fff;
    min-height: 100vh;
    min-height: 100dvh;
    touch-action: pan-y pinch-zoom;
    padding-top: env(safe-area-inset-top, 0px);
  }

  /* タブバー */
  /* タブバー（components.css の .card-tabs が適用される） */

  /* ページタイトル（メインサイトのモバイル header と同じ） */
  .viewer-page-header {
    padding: 52px 12px 0;
    margin-bottom: 10px;
  }
  .viewer-page-header p {
    font-size: 0.8em;
  }
  .viewer-page-header h1 {
    font-size: 1.5em;
    padding-bottom: 6px;
  }

  /* ツールバー */
  .viewer-toolbar {
    padding: 6px 8px;
    padding-top: max(6px, env(safe-area-inset-top, 0px));
  }

  .btn-back-circle,
  .btn-sm,
  .qr-trigger,
  .btn-bookmark {
    min-width: 36px;
    min-height: 36px;
    padding: 5px 6px;
    font-size: 0.75rem;
  }

  .qr-trigger .qr-trigger-label {
    display: none;
  }
  .qr-trigger {
    padding: 8px;
  }

  /* メインエリア */
  .viewer-container {
    flex: 1;
    min-height: 0;
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }

  .viewer-split-content {
    flex-direction: column;
    padding: 0;
    min-height: 100%;
  }

  .simulation-area {
    height: 300px;
    width: 100%;
    order: 1;
  }

  .explanation-area {
    padding: 12px 0 24px;
    padding-bottom: max(24px, env(safe-area-inset-bottom, 0px) + 24px);
    width: 100%;
    order: 2;
    box-sizing: border-box;
  }

  /* カードのパディングを調整 */
  .card {
    padding: 20px 14px;
    margin-bottom: 24px;
    border-radius: 14px;
  }

  /* シミュレーションエリアの調整 */
  .sim-embed {
    min-height: 250px;
    margin: 20px 0;
    border-radius: 8px;
  }

  /* スマホでは縦並びにして確実に重なりを防ぐ */
  .card-reaction-footer {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
  }
  .card-reaction-buttons {
    width: 100%;
    justify-content: flex-end;
    margin-left: 0;
  }
  .card-memo-area {
    width: 100%;
  }
  
  :root { --card-width: 100%; }

  /* 数式の横スクロールを改善 */
  mjx-container[display="true"] {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding: 10px 0;
  }

  /* タッチ操作の最適化 */
  .sim-embed canvas {
    touch-action: none;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
  }

  /* ボタンのタッチフィードバック改善 */
  button, .btn-sm, .btn-reaction, .tab-btn {
    -webkit-tap-highlight-color: rgba(59, 130, 246, 0.2);
    tap-highlight-color: rgba(59, 130, 246, 0.2);
  }

}

/* タブレット対応 (768px - 1024px) */
@media (min-width: 769px) and (max-width: 1024px) {
  .card {
    padding: 30px;
  }
}

/* タブバーは components.css の .card-tabs / .card-tabs__list を使用 */

/* YouTube タブの色（components.css の .card-tab を上書き） */
.card-tab--youtube {
  color: #dc2626;
}
.card-tab--youtube.card-tab--active {
  background: linear-gradient(135deg, #dc2626, #f87171);
  border-color: #dc2626;
  color: #fff;
  box-shadow: 0 2px 8px rgba(220, 38, 38, 0.25);
}

/* iPad横画面（1024px以上）でのレイアウト最適化 */
@media (min-width: 1024px) {
  .card {
    padding: 40px;
  }
}

/* 大型タブレット・デスクトップ（1366px以上） */
@media (min-width: 1366px) {
  .card {
    padding: 50px;
  }
}

/* --- 印刷用スタイル (追加・修正版) --- */
@media print {
  /* 不要なUIを非表示 */
  .card-tabs,
  .viewer-toolbar,
  .sim-controls-panel,
  #pointer-canvas,
  #btn-toggle-pointer,
  .btn-back-circle,
  .card-reaction-footer, /* 印刷時はリアクションエリアを隠す */
  .card-tabs-bar { /* 印刷時はタブバーを非表示 */
    display: none !important;
  }

  /* 印刷時はすべてのカード・Pointを表示（タブで非表示のものも） */
  .card,
  .box-alert {
    display: block !important;
    page-break-inside: avoid;
    break-inside: avoid;
    margin-bottom: 30px;
  }

  /* 背景とレイアウトのリセット */
  body {
    background: #fff;
    height: auto;
    overflow: visible;
    display: block; /* Flex解除 */
    color: #000;
  }

  .viewer-container {
    height: auto;
    display: block;
  }

  .viewer-split-content {
    display: block;
  }

  .explanation-area {
    width: 100%;
    max-width: 100% !important; /* 幅をページ一杯に */
    margin: 0;
    padding: 0;
    box-shadow: none;
    border: none;
  }

  /* シミュレーション埋め込み枠の調整 */
  .sim-embed {
    break-inside: avoid; /* 途中で切れないようにする */
    page-break-inside: avoid;
    border: 1px solid #ccc; /* 枠線は残す */
    box-shadow: none;
    background: #fff;
    
    /* はみ出し防止対応 */
    width: 100% !important;
    max-width: 100% !important;
    box-sizing: border-box;
    margin: 10px 0;
    display: block; /* Flexによる予期せぬ拡張を防止 */
  }
  
  /* シミュレーション内部のコンテンツも縮小させる */
  .sim-embed canvas,
  .sim-embed > div {
    max-width: 100% !important;
    height: auto !important;
  }
}

/* --- 音声コントロールパネル --- */
.audio-controls-panel {
  position: fixed;
  bottom: 20px;
  right: 20px;
  width: 320px;
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.15), 0 8px 10px -6px rgba(0, 0, 0, 0.1);
  z-index: 1000;
  border: 1px solid #e2e8f0;
  overflow: hidden;
}

.audio-controls-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  background: var(--col-primary);
  color: #fff;
  font-weight: bold;
}

.audio-close-btn {
  background: transparent;
  border: none;
  color: #fff;
  font-size: 1.2rem;
  cursor: pointer;
  padding: 0;
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  transition: background 0.2s;
}

.audio-close-btn:hover {
  background: rgba(255, 255, 255, 0.2);
}

.audio-controls-body {
  padding: 16px;
}

.audio-progress-container {
  width: 100%;
  height: 4px;
  background: #e2e8f0;
  border-radius: 2px;
  margin-bottom: 16px;
  overflow: hidden;
}

.audio-progress {
  height: 100%;
  background: var(--col-primary);
  width: 0%;
  transition: width 0.3s;
  border-radius: 2px;
}

.audio-buttons {
  display: flex;
  gap: 8px;
  justify-content: center;
  margin-bottom: 12px;
}

.audio-btn {
  padding: 10px 16px;
  border: 1px solid var(--col-primary);
  background: #fff;
  color: var(--col-primary);
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.9rem;
  transition: all 0.2s;
  white-space: nowrap;
}

.audio-btn:hover {
  background: var(--col-primary);
  color: #fff;
}

.audio-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.audio-error {
  color: #ef4444;
  font-size: 0.85rem;
  text-align: center;
  padding: 8px;
  background: #fef2f2;
  border-radius: 6px;
  border: 1px solid #fecaca;
}

/* 音声再生中のカードハイライト */
.card.audio-playing {
  background: #fef3c7;
  border-left: 4px solid #f59e0b;
  transition: background 0.3s, border-left 0.3s;
  box-shadow: 0 4px 12px rgba(245, 158, 11, 0.2);
}

/* レスポンシブ対応 */
@media (max-width: 768px) {
  .audio-controls-panel {
    width: calc(100% - 40px);
    right: 20px;
    left: 20px;
    bottom: 20px;
  }

  .audio-buttons {
    flex-wrap: wrap;
  }

  .audio-btn {
    flex: 1;
    min-width: 80px;
  }
}

/* タッチターゲット最小 44px は上記 @media (max-width: 768px) で確保。ピンチズーム等は mobile-touch.js に委譲 */

/* --- 目次パネル --- */
.btn-toc {
  position: relative;
}
.btn-toc.active {
  background: var(--col-primary);
  color: #fff;
}
.toc-panel {
  position: fixed;
  top: 60px;
  right: 16px;
  z-index: 1100;
  background: #fff;
  border: 1px solid #e2e8f0;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
  max-height: 60vh;
  overflow-y: auto;
  min-width: 220px;
  max-width: 340px;
  padding: 8px 0;
}
.toc-list {
  list-style: none;
  margin: 0;
  padding: 0;
}
.toc-list li {
  margin: 0;
}
.toc-list a {
  display: block;
  padding: 10px 16px;
  color: var(--col-text);
  text-decoration: none;
  font-size: 0.88rem;
  line-height: 1.4;
  transition: background 0.12s;
}
.toc-list a:hover {
  background: #f1f5f9;
  color: var(--col-primary);
}
.toc-empty {
  padding: 12px 16px;
  color: var(--col-text-sub);
  font-size: 0.85rem;
  margin: 0;
}
@media (max-width: 768px) {
  .toc-panel {
    right: 8px;
    left: 8px;
    max-width: none;
    top: 52px;
  }
}

/* ================================================================
   ヒントリンクバナー（viewer.html で解説冒頭に表示）
   ================================================================ */
.hint-link-banner {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 18px;
  margin: 0 0 20px 0;
  background: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%);
  border-left: 5px solid #f59e0b;
  border-radius: 10px;
  box-shadow: 0 2px 8px rgba(245, 158, 11, 0.15);
}
.hint-link-banner-icon {
  font-size: 28px;
  flex-shrink: 0;
}
.hint-link-banner-text {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}
.hint-link-banner-text strong {
  color: #78350f;
  font-size: 15px;
  font-weight: 700;
}
.hint-link-banner-sub {
  color: #92400e;
  font-size: 12px;
  opacity: 0.9;
}
.hint-link-banner-btn {
  flex-shrink: 0;
  padding: 10px 18px;
  background: #f59e0b;
  color: #fff;
  border-radius: 999px;
  text-decoration: none;
  font-size: 13px;
  font-weight: 700;
  white-space: nowrap;
  box-shadow: 0 2px 6px rgba(245, 158, 11, 0.3);
  transition: all 0.15s;
}
.hint-link-banner-btn:hover {
  background: #d97706;
  transform: translateY(-1px);
  box-shadow: 0 3px 10px rgba(245, 158, 11, 0.4);
  text-decoration: none;
  color: #fff;
}

@media (max-width: 640px) {
  .hint-link-banner {
    flex-wrap: wrap;
    gap: 8px;
  }
  .hint-link-banner-btn {
    width: 100%;
    text-align: center;
  }
}

/* ツールバー内の「💡 ヒント」ボタン */
.btn-to-hint {
  background: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%) !important;
  color: #92400e !important;
  border: 1.5px solid #f59e0b !important;
  font-weight: 700 !important;
  text-decoration: none !important;
}
.btn-to-hint:hover {
  background: linear-gradient(135deg, #fde68a 0%, #fcd34d 100%) !important;
  color: #78350f !important;
  text-decoration: none !important;
}
