/* カラー変数（ライトモードのデフォルト値） */
:root {
  --bg-page:       #f5f6fa;
  --bg-container:  #ffffff;
  --bg-label:      #f1f2f6;
  --bg-label-hover:#dcdde1;
  --text-main:     #333333;
  --text-label:    #333333;
  --shadow:        rgba(0, 0, 0, 0.1);
  --correct-bg:    #c8f7c5;
  --incorrect-bg:  #f7c5c5;
  --diff-easy-bg:  #d4edda;
  --diff-easy-fg:  #155724;
  --diff-med-bg:   #fff3cd;
  --diff-med-fg:   #856404;
  --diff-hard-bg:  #f8d7da;
  --diff-hard-fg:  #721c24;
}

/* ダークモード変数（手動トグル） */
html.dark {
  --bg-page:       #1a1a2e;
  --bg-container:  #16213e;
  --bg-label:      #0f3460;
  --bg-label-hover:#1a4a80;
  --text-main:     #e0e0e0;
  --text-label:    #e0e0e0;
  --shadow:        rgba(0, 0, 0, 0.4);
  --correct-bg:    #1a3a22;
  --incorrect-bg:  #3a1010;
  --diff-easy-bg:  #1a3a22;
  --diff-easy-fg:  #5cb85c;
  --diff-med-bg:   #3a3010;
  --diff-med-fg:   #f0ad4e;
  --diff-hard-bg:  #3a1010;
  --diff-hard-fg:  #e94560;
}

/* OS設定でダークモード（手動でライトに切り替えていない場合） */
@media (prefers-color-scheme: dark) {
  html:not(.light) {
    --bg-page:       #1a1a2e;
    --bg-container:  #16213e;
    --bg-label:      #0f3460;
    --bg-label-hover:#1a4a80;
    --text-main:     #e0e0e0;
    --text-label:    #e0e0e0;
    --shadow:        rgba(0, 0, 0, 0.4);
    --correct-bg:    #1a3a22;
    --incorrect-bg:  #3a1010;
    --diff-easy-bg:  #1a3a22;
    --diff-easy-fg:  #5cb85c;
    --diff-med-bg:   #3a3010;
    --diff-med-fg:   #f0ad4e;
    --diff-hard-bg:  #3a1010;
    --diff-hard-fg:  #e94560;
  }
}

/* インラインスタイルのbackgroundを上書き（!importantが必要） */
html.dark label {
  background: var(--bg-label) !important;
  color: var(--text-label) !important;
}
html.dark label:hover {
  background: var(--bg-label-hover) !important;
}

@media (prefers-color-scheme: dark) {
  html:not(.light) label {
    background: var(--bg-label) !important;
    color: var(--text-label) !important;
  }
  html:not(.light) label:hover {
    background: var(--bg-label-hover) !important;
  }
}

/* 全体 */
body {
  font-family: Arial, sans-serif;
  margin: 0;
  overflow-x: hidden;
  background-color: var(--bg-page);
  color: var(--text-main);
  transition: background-color 0.2s, color 0.2s;
}

/* コンテナ */
.container {
  width: 100%;
  max-width: 800px;
  margin: 40px auto;
  padding: 20px;
  box-sizing: border-box;
  background: var(--bg-container);
  border-radius: 10px;
  box-shadow: 0 2px 8px var(--shadow);
  transition: background 0.2s;
}

/* ヘッダー */
header {
  background-color: #2f3640;
  color: white;
  padding: 15px;
  text-align: center;
}

/* フッター */
footer {
  background-color: #2f3640;
  color: white;
  text-align: center;
  padding: 10px;
  margin-top: 40px;
}

/* ボタン */
button {
  padding: 10px 16px;
  margin: 5px 5px 0 0;
  border: none;
  border-radius: 6px;
  background-color: #40739e;
  color: white;
  cursor: pointer;
}

button:hover {
  background-color: #273c75;
}

/* 選択肢 */
label {
  display: block;
  padding: 10px;
  margin: 5px 0;
  border-radius: 6px;
  background-color: var(--bg-label);
  color: var(--text-label);
  cursor: pointer;
  transition: background-color 0.15s;
}

label:hover {
  background-color: var(--bg-label-hover);
}

/* サイドバーレイアウト（サイドバーがある時だけ flex） */
.page-wrap.has-sidebar {
  display: flex;
  align-items: flex-start;
}

.sidebar-nav {
  width: 148px;
  min-width: 148px;
  flex-shrink: 0;
  background: #2a3042;
  min-height: calc(100vh - 90px);
  padding: 18px 10px;
}

.sidebar-nav .sidebar-title {
  font-size: 0.7rem;
  color: #666;
  text-transform: uppercase;
  letter-spacing: 1px;
  padding: 0 4px;
  margin-bottom: 10px;
}

.cat-select {
  width: 100%;
  padding: 8px 6px;
  background: #0f3460;
  border: 1px solid #1e3a6e;
  border-radius: 6px;
  color: #fff;
  font-size: 0.88rem;
  cursor: pointer;
  appearance: auto;
}

.cat-select:focus {
  outline: none;
  border-color: #7ec8e3;
}

.cat-select option {
  background: #16213e;
  color: #fff;
}

.page-wrap.has-sidebar .container {
  flex: 1;
  margin: 40px 24px;
  max-width: 800px;
}

/* 正解・不正解 */
.correct   { background-color: var(--correct-bg) !important; }
.incorrect { background-color: var(--incorrect-bg) !important; }

html.dark label.correct   { background-color: var(--correct-bg) !important; }
html.dark label.incorrect { background-color: var(--incorrect-bg) !important; }

@media (prefers-color-scheme: dark) {
  html:not(.light) label.correct   { background-color: var(--correct-bg) !important; }
  html:not(.light) label.incorrect { background-color: var(--incorrect-bg) !important; }
}

/* フォーム入力 ダークモード（details内のみ） */
html.dark details input,
html.dark details select {
  background: var(--bg-label);
  color: var(--text-main);
  border-color: #2d5986 !important;
}

@media (prefers-color-scheme: dark) {
  html:not(.light) details input,
  html:not(.light) details select {
    background: var(--bg-label);
    color: var(--text-main);
    border-color: #2d5986 !important;
  }
}

/* ===== レスポンシブ ===== */
@media (max-width: 600px) {
  /* サイドバーを非表示にして本文を全幅に */
  .sidebar-nav {
    display: none;
  }

  .page-wrap.has-sidebar .container {
    margin: 12px;
    max-width: 100%;
  }

  .container {
    margin: 12px;
    padding: 14px;
  }

  /* ヘッダーのナビリンク群を小さくして折り返しを許容 */
  header {
    padding: 10px 12px;
  }
}

/* ===== クイズ演習 スマホ対応 ===== */

/* フォントサイズ CSS変数（スマホ幅別・問題ごとに変えない） */
@media (max-width: 380px) {
  :root {
    --quiz-question-font-size: 16px;
    --quiz-choice-font-size: 14px;
  }
}
@media (min-width: 381px) and (max-width: 480px) {
  :root {
    --quiz-question-font-size: 17px;
    --quiz-choice-font-size: 15px;
  }
}
@media (min-width: 481px) and (max-width: 767px) {
  :root {
    --quiz-question-font-size: 18px;
    --quiz-choice-font-size: 16px;
  }
}

/* 進捗バー（PC・スマホ共通） */
.quiz-progress-wrap { margin-bottom: 10px; }
.quiz-progress-labels {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 6px;
  font-size: 0.82rem;
  color: var(--text-main);
  opacity: 0.75;
}
.quiz-progress-track {
  background: var(--bg-label);
  border-radius: 4px;
  height: 7px;
  overflow: hidden;
}
.quiz-progress-fill {
  background: #40739e;
  height: 100%;
  transition: width 0.4s ease;
}

/* コンパクトヘッダー: PC では非表示 */
.quiz-compact-header { display: none; }

/* アクションバー: PC では通常フロー */
.quiz-action-bar {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 16px;
}

/* ヒントボタン行 */
.quiz-hint-row { margin-top: 8px; }

/* 解説 details */
.explanation-details { margin-top: 4px; }
.explanation-summary {
  cursor: pointer;
  font-weight: bold;
  padding: 4px 0;
  color: var(--text-main);
  opacity: 0.8;
}

/* プレースホルダー（前へボタンなし時・PC は非表示） */
.quiz-action-placeholder { display: none; }

/* ===== スマホ専用レイアウト ===== */
@media (max-width: 767px) {

  /* デスクトップヘッダーを演習中は非表示（.desktop-headerはPC幅では表示されるが、スマホでは既にnone） */
  body.quiz-playing .desktop-header {
    display: none !important;
  }

  /* フッター非表示 */
  body.quiz-playing footer {
    display: none !important;
  }

  /* コンテナ余白リセット・min-height は auto */
  body.quiz-playing .page-wrap .container {
    margin: 0 !important;
    padding: 10px 10px 0 10px !important;
    min-height: auto !important;
    border-radius: 0 !important;
  }

  /* コンパクトヘッダー表示 */
  .quiz-compact-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 7px 12px;
    background: #2f3640;
    color: white;
    position: sticky;
    top: 0;
    z-index: 40;
  }
  .quiz-compact-back {
    color: #7ec8e3;
    text-decoration: none;
    font-size: 0.88rem;
    min-width: 44px;
  }
  .quiz-compact-title {
    font-weight: bold;
    font-size: 0.83rem;
    text-align: center;
    flex: 1;
  }
  .quiz-compact-progress {
    color: #aaa;
    font-size: 0.78rem;
    text-align: right;
    min-width: 44px;
  }

  /* quiz-main: 下部固定バー分のpadding */
  .quiz-main {
    padding-bottom: calc(76px + env(safe-area-inset-bottom));
  }

  /* 進捗テキスト非表示（コンパクトヘッダーに出るので不要） */
  .quiz-progress-labels { display: none; }

  /* 問題文エリア: 高さ固定でスクロール（選択肢位置を安定させる） */
  .question-area {
    height: clamp(80px, 15dvh, 120px);
    overflow-y: auto;
    margin-bottom: 8px;
    -webkit-overflow-scrolling: touch;
  }
  .question-text {
    font-size: var(--quiz-question-font-size, 17px);
    line-height: 1.35;
    font-weight: 700;
    margin: 0;
  }

  /* 選択肢エリア: 4行グリッドで高さ固定 */
  .choices-area {
    display: grid;
    grid-template-rows: repeat(4, 1fr);
    gap: 6px;
    height: clamp(224px, 42dvh, 292px);
    min-height: 0;
  }

  /* 選択肢ラッパー div（グリッドアイテム） */
  .choices-area > div {
    min-height: 0;
    height: 100%;
  }

  /* 選択肢ラベル: flex で高さ100%・テキスト枠スクロール対応 */
  .choice-label {
    display: flex !important;
    align-items: flex-start;
    gap: 8px;
    padding: 8px 10px !important;
    height: 100%;
    min-height: 0 !important;
    overflow: hidden;
    box-sizing: border-box;
    font-size: var(--quiz-choice-font-size, 15px) !important;
    line-height: 1.3;
  }

  /* 選択肢テキスト: 長文は枠内スクロール（ページ全体を押し下げない） */
  .choice-text {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    word-break: break-word;
    line-height: 1.3;
  }

  /* 自動再生バー: スマホではコンパクト */
  #autoplay-bar {
    padding: 6px 10px !important;
    font-size: 13px !important;
    margin-bottom: 8px !important;
    border-radius: 8px !important;
  }
  #autoplay-bar span,
  #autoplay-bar strong { font-size: 13px !important; }
  #autoplay-bar button { font-size: 12px !important; padding: 3px 10px !important; }

  /* ヒントボタン */
  .quiz-hint-row { margin-top: 6px; }
  .quiz-hint-row button { font-size: 0.75rem; padding: 4px 8px; }

  /* アクションバー: 画面下部固定 */
  .quiz-action-bar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 50;
    display: grid;
    grid-template-columns: 1fr 1.4fr 1fr;
    gap: 8px;
    padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
    background: var(--bg-container);
    border-top: 1px solid var(--bg-label-hover);
    box-shadow: 0 -4px 16px rgba(0, 0, 0, 0.15);
    margin-top: 0;
  }
  .quiz-action-bar button {
    min-height: 44px;
    border-radius: 10px;
    font-size: 14px;
    font-weight: 700;
    margin: 0;
    padding: 8px 4px;
    width: 100%;
    text-align: center;
    box-sizing: border-box;
  }
  .quiz-btn-check { font-size: 15px !important; }

  /* 前へなし時のプレースホルダー */
  .quiz-action-placeholder {
    display: block;
    visibility: hidden;
    min-height: 44px;
  }
}

/* iPhone SE など小さい端末の追加調整 */
@media (max-width: 380px) {
  body.quiz-playing .page-wrap .container {
    padding: 8px 8px 0 8px !important;
  }
  .question-area { height: clamp(70px, 14dvh, 100px); }
  .question-text { line-height: 1.32; }
  .choices-area { gap: 5px; height: clamp(200px, 40dvh, 260px); }
  .choice-label {
    padding: 7px 8px !important;
    line-height: 1.25;
  }
  .choice-text { line-height: 1.25; }
  #autoplay-bar {
    padding: 5px 8px !important;
    font-size: 12px !important;
  }
  .quiz-action-bar {
    gap: 6px;
    padding: 8px 10px calc(8px + env(safe-area-inset-bottom));
  }
  .quiz-action-bar button {
    min-height: 40px;
    font-size: 13px;
    border-radius: 9px;
  }
  .quiz-main {
    padding-bottom: calc(68px + env(safe-area-inset-bottom));
  }
}

/* ===== ヘッダーレイアウト切り替え (PC / スマホ) ===== */

/* --- PC: 768px以上 --- */
@media (min-width: 768px) {
  .desktop-header {
    display: flex;
  }
  .mobile-header {
    display: none !important;
  }
  .mobile-overlay {
    display: none !important;
  }
  .mobile-drawer {
    display: none !important;
  }
}

/* --- スマホ: 767px以下 --- */
@media (max-width: 767px) {
  .desktop-header {
    display: none !important;
  }

  /* スマホヘッダー */
  .mobile-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 14px;
    height: 48px;
    background: #2f3640;
    color: white;
    position: sticky;
    top: 0;
    z-index: 200;
    box-sizing: border-box;
    flex-shrink: 0;
  }

  /* quiz-playing ページはコンパクトヘッダー使用 → モバイルヘッダー非表示 */
  body.quiz-playing .mobile-header {
    display: none !important;
  }

  .mobile-header-left {
    display: flex;
    align-items: center;
    gap: 5px;
    min-width: 0;
    flex: 1;
    overflow: hidden;
  }
  .mobile-header-brand {
    color: #fff;
    text-decoration: none;
    font-weight: bold;
    font-size: 0.95rem;
    white-space: nowrap;
  }
  .mobile-header-cat {
    color: #7ec8e3;
    font-size: 0.78rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .mobile-header-right {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-shrink: 0;
  }
  .mobile-hud {
    color: #ccc;
    font-size: 0.72rem;
    white-space: nowrap;
  }
  .mobile-menu-btn {
    background: none !important;
    border: none !important;
    color: #fff !important;
    font-size: 1.35rem;
    cursor: pointer;
    padding: 4px 6px;
    margin: 0 !important;
    line-height: 1;
    min-height: auto !important;
  }

  /* ドロワーオーバーレイ */
  .mobile-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 300;
  }
  .mobile-overlay.open { display: block; }

  /* ドロワー本体 */
  .mobile-drawer {
    display: flex;
    flex-direction: column;
    position: fixed;
    top: 0;
    right: -290px;
    width: 280px;
    height: 100dvh;
    background: #1a2030;
    z-index: 400;
    transition: right 0.28s ease;
    overflow-y: auto;
  }
  .mobile-drawer.open { right: 0; }

  .mobile-drawer-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 14px 16px;
    background: #2f3640;
    font-weight: bold;
    color: #fff;
    font-size: 1rem;
    flex-shrink: 0;
  }
  .mobile-drawer-close {
    background: none !important;
    border: none !important;
    color: #ccc !important;
    font-size: 1.3rem;
    cursor: pointer;
    padding: 2px 4px;
    margin: 0 !important;
    line-height: 1;
    min-height: auto !important;
  }
  .mobile-drawer-user {
    padding: 10px 16px;
    color: #7ec8e3;
    font-size: 0.82rem;
    border-bottom: 1px solid #2d3a4a;
  }
  .mobile-drawer-link {
    display: block;
    padding: 14px 16px;
    color: #ddd;
    text-decoration: none;
    border-bottom: 1px solid #1e2a38;
    font-size: 0.95rem;
    transition: background 0.15s;
  }
  .mobile-drawer-link:active { background: #2d5986; }
  .mobile-drawer-divider {
    height: 1px;
    background: #2d3a4a;
    margin: 8px 0;
  }
  .mobile-drawer-hud {
    padding: 10px 16px;
    color: #aaa;
    font-size: 0.82rem;
    line-height: 1.9;
  }
  .mobile-drawer-theme-btn {
    margin: 10px 16px 16px;
    padding: 10px 14px;
    background: #2d5986 !important;
    color: #fff !important;
    border: none !important;
    border-radius: 6px;
    cursor: pointer;
    text-align: left;
    font-size: 0.88rem;
    width: calc(100% - 32px);
  }
}

/* ===== 解答セッション履歴カード ===== */
.session-list { display: flex; flex-direction: column; gap: 10px; margin-bottom: 16px; }
.session-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: var(--bg-container);
  border: 1px solid rgba(100,130,160,0.25);
  border-radius: 8px;
  box-shadow: 0 1px 4px var(--shadow);
  gap: 10px;
}
.session-card-body { flex: 1; min-width: 0; }
.session-card-date {
  font-size: 0.82rem;
  color: #7ec8e3;
  margin-bottom: 3px;
}
.session-card-stats {
  font-size: 0.88rem;
  color: var(--text-main);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
@media (max-width: 480px) {
  .session-card { padding: 10px 12px; }
  .session-card-stats { font-size: 0.82rem; white-space: normal; }
}

/* ===== 難易度バッジ ===== */

/* 難易度バッジ */
.difficulty-badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 4px;
  font-size: 0.78em;
  font-weight: bold;
}
.difficulty-easy   { background: var(--diff-easy-bg); color: var(--diff-easy-fg); }
.difficulty-medium { background: var(--diff-med-bg);  color: var(--diff-med-fg); }
.difficulty-hard   { background: var(--diff-hard-bg); color: var(--diff-hard-fg); }

/* 問題演習中は難易度バッジ非表示（PC・スマホ共通） */
.quiz-playing .difficulty-badge-wrap {
  display: none !important;
}