*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #f0f4f8;
  --surface: #ffffff;
  --primary: #2563eb;
  --primary-light: #eff6ff;
  --primary-dark: #1d4ed8;
  --text: #1e293b;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --correct: #16a34a;
  --correct-bg: #f0fdf4;
  --correct-border: #86efac;
  --incorrect: #dc2626;
  --incorrect-bg: #fef2f2;
  --incorrect-border: #fca5a5;
  --bottom-nav-h: 60px;
  --header-h: 52px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html { -webkit-text-size-adjust: 100%; }

body {
  font-family: 'Hiragino Sans', 'Hiragino Kaku Gothic ProN', 'Yu Gothic UI', 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
  overscroll-behavior: none;
}

/* ── Header ─────────────────────────────── */
#header {
  position: fixed; top: 0; left: 0; right: 0;
  height: var(--header-h);
  background: var(--primary);
  color: #fff;
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 16px;
  z-index: 100;
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
}
#header h1 { font-size: 15px; font-weight: 700; }
#header-stats { display: flex; gap: 10px; font-size: 12px; }
.hstat { display: flex; align-items: center; gap: 4px; opacity: 0.9; }
.hstat-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.hstat-dot.correct { background: #4ade80; }
.hstat-dot.incorrect { background: #f87171; }

/* ── Layout ──────────────────────────────── */
#app {
  padding-top: var(--header-h);
  padding-bottom: calc(var(--bottom-nav-h) + var(--safe-bottom));
  min-height: 100vh;
}

/* ── Sub-category chips ───────────────────── */
#subcat-bar {
  position: sticky;
  top: var(--header-h);
  z-index: 90;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 10px 12px;
  display: flex;
  gap: 8px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
#subcat-bar::-webkit-scrollbar { display: none; }

.chip {
  flex-shrink: 0;
  padding: 6px 14px;
  border-radius: 20px;
  border: 1.5px solid var(--border);
  background: var(--surface);
  font-size: 13px;
  font-weight: 500;
  color: var(--text-muted);
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.15s;
  -webkit-tap-highlight-color: transparent;
}
.chip:active { opacity: 0.7; }
.chip.active {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

/* ── Main content ────────────────────────── */
#main { padding: 12px; }

/* Progress card */
.progress-card {
  background: var(--surface);
  border-radius: 14px;
  padding: 14px 16px;
  margin-bottom: 14px;
  box-shadow: 0 1px 4px rgba(0,0,0,0.06);
}
.progress-card-top {
  display: flex; justify-content: space-between; align-items: flex-start;
  margin-bottom: 10px;
}
.progress-card-title { font-size: 13px; font-weight: 600; color: var(--text-muted); }
.progress-pct { font-size: 24px; font-weight: 800; color: var(--primary); line-height: 1; }
.progress-bar-wrap { background: var(--bg); border-radius: 6px; height: 6px; overflow: hidden; margin-bottom: 10px; }
.progress-bar-fill { height: 100%; background: linear-gradient(90deg, var(--primary), #60a5fa); border-radius: 6px; transition: width 0.4s ease; }
.progress-counts { display: flex; gap: 14px; }
.pcount { display: flex; align-items: center; gap: 5px; font-size: 12px; color: var(--text-muted); }
.pcount-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.pcount strong { color: var(--text); font-weight: 700; }

/* Filter pills */
.filter-bar {
  display: flex; gap: 6px; margin-bottom: 12px; overflow-x: auto;
  -webkit-overflow-scrolling: touch; scrollbar-width: none;
}
.filter-bar::-webkit-scrollbar { display: none; }
.filter-pill {
  flex-shrink: 0;
  padding: 5px 14px; border-radius: 20px;
  border: 1.5px solid var(--border);
  background: var(--surface); cursor: pointer;
  font-size: 12px; font-weight: 600; color: var(--text-muted);
  transition: all 0.15s;
  -webkit-tap-highlight-color: transparent;
}
.filter-pill.active { background: var(--primary); color: #fff; border-color: var(--primary); }

/* Group header */
.group-header {
  display: flex; align-items: center; gap: 8px;
  margin: 16px 0 10px;
}
.group-name { font-size: 15px; font-weight: 700; color: var(--text); }
.group-count { font-size: 12px; color: var(--text-muted); }
.group-pct {
  margin-left: auto;
  font-size: 11px; font-weight: 700;
  padding: 2px 8px; border-radius: 10px;
  background: var(--primary-light); color: var(--primary);
}

/* Question grid — 2 columns on mobile */
.q-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.q-card {
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: 12px;
  padding: 10px;
  cursor: pointer;
  position: relative;
  display: flex; flex-direction: column; gap: 4px;
  -webkit-tap-highlight-color: transparent;
  active: transform scale(0.97);
  transition: box-shadow 0.15s;
  min-height: 120px;
}
.q-card:active { transform: scale(0.97); }
.q-card.correct { border-color: var(--correct); border-left-width: 4px; }
.q-card.incorrect { border-color: var(--incorrect); border-left-width: 4px; }

.q-status-icon {
  position: absolute; top: 8px; right: 8px;
  width: 20px; height: 20px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 11px; font-weight: 800; z-index: 1;
}
.q-status-icon.correct { background: var(--correct); color: #fff; }
.q-status-icon.incorrect { background: var(--incorrect); color: #fff; }

.q-nendo { font-size: 10px; font-weight: 600; color: var(--text-muted); }
.q-number { font-size: 16px; font-weight: 800; color: var(--text); margin-bottom: 4px; }

.q-image-wrap { flex: 1; display: flex; align-items: center; justify-content: center; }
.q-image-thumb {
  width: 100%; max-height: 80px; object-fit: contain;
  border-radius: 4px; background: var(--bg);
}
.q-no-image {
  width: 100%; height: 60px; background: var(--bg);
  border-radius: 6px; display: flex; align-items: center; justify-content: center;
  font-size: 10px; color: var(--text-muted);
}

/* Empty state */
.empty-state {
  text-align: center; padding: 48px 24px;
  color: var(--text-muted); font-size: 14px;
}
.empty-icon { font-size: 40px; margin-bottom: 12px; }

/* ── Bottom Navigation ───────────────────── */
#bottom-nav {
  position: fixed; bottom: 0; left: 0; right: 0;
  height: calc(var(--bottom-nav-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  z-index: 100;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  box-shadow: 0 -2px 12px rgba(0,0,0,0.08);
}
#bottom-nav::-webkit-scrollbar { display: none; }

.bottom-nav-item {
  flex: 1; min-width: 72px;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px; cursor: pointer; padding: 6px 4px;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.15s;
  position: relative;
}
.bottom-nav-item:active { background: var(--bg); }
.bottom-nav-item.active .bni-label { color: var(--primary); font-weight: 700; }
.bottom-nav-item.active::after {
  content: '';
  position: absolute; top: 0; left: 20%; right: 20%;
  height: 3px; border-radius: 0 0 3px 3px;
  background: var(--primary);
}
.bni-label { font-size: 10px; color: var(--text-muted); white-space: nowrap; }
.bni-badge {
  font-size: 10px; font-weight: 700;
  background: var(--bg); color: var(--text-muted);
  padding: 0 5px; border-radius: 8px;
  line-height: 16px; min-width: 20px; text-align: center;
}
.bottom-nav-item.active .bni-badge { background: var(--primary-light); color: var(--primary); }

/* ── Modal (bottom sheet on mobile) ─────── */
#modal-overlay {
  display: none; position: fixed; inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 200;
  align-items: flex-end;
}
#modal-overlay.open { display: flex; }

#modal {
  background: var(--surface);
  border-radius: 20px 20px 0 0;
  width: 100%;
  max-height: 95vh;
  display: flex; flex-direction: column;
  overflow: hidden;
  will-change: auto;
}

#modal-handle {
  width: 40px; height: 4px;
  background: var(--border); border-radius: 2px;
  margin: 12px auto 0;
  flex-shrink: 0;
}

#modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px 10px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.modal-title { font-size: 17px; font-weight: 800; }
.modal-meta { display: flex; align-items: center; gap: 8px; margin-top: 2px; flex-wrap: wrap; }
.modal-counter { font-size: 12px; color: var(--text-muted); }
#modal-close {
  width: 32px; height: 32px; border-radius: 50%;
  border: none; background: var(--bg); cursor: pointer;
  font-size: 16px; color: var(--text-muted);
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}

#modal-body {
  flex: 1; overflow-y: auto;
  padding: 16px;
  display: flex; flex-direction: column; align-items: center;
  -webkit-overflow-scrolling: touch;
}
#modal-image {
  max-width: 100%;
  max-height: 45vh;
  object-fit: contain;
  border-radius: 8px;
}
.modal-no-image {
  width: 100%; height: 160px;
  background: var(--bg); border-radius: 12px;
  display: flex; align-items: center; justify-content: center;
  color: var(--text-muted); font-size: 13px; text-align: center;
  line-height: 1.8;
}

#modal-footer {
  padding: 12px 16px calc(12px + var(--safe-bottom));
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
/* Choice buttons イロハニ */
.choice-section { margin-bottom: 12px; }
.choice-label {
  font-size: 11px; font-weight: 700; color: var(--text-muted);
  text-transform: uppercase; letter-spacing: 0.05em;
  margin-bottom: 8px; text-align: center;
}
.choice-btns {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px;
}
.btn-choice {
  padding: 14px 6px;
  border-radius: 12px; border: 2px solid var(--border);
  background: var(--surface); color: var(--text);
  font-size: 20px; font-weight: 800;
  cursor: pointer; transition: all 0.15s;
  -webkit-tap-highlight-color: transparent;
  display: flex; align-items: center; justify-content: center;
}
.btn-choice:active { transform: scale(0.93); }
.btn-choice.selected {
  background: var(--primary); color: #fff;
  border-color: var(--primary);
  box-shadow: 0 4px 12px rgba(37,99,235,0.3);
}

/* Answer eval buttons */
.eval-section { margin-bottom: 10px; }
.eval-label {
  font-size: 11px; font-weight: 700; color: var(--text-muted);
  text-transform: uppercase; letter-spacing: 0.05em;
  margin-bottom: 8px; text-align: center;
}
.modal-answer-btns {
  display: grid; grid-template-columns: 1fr 1fr;
  gap: 10px;
}
.btn-answer {
  padding: 13px;
  border-radius: 12px; border: 2px solid;
  font-size: 15px; font-weight: 800;
  cursor: pointer; transition: all 0.15s;
  -webkit-tap-highlight-color: transparent;
  display: flex; align-items: center; justify-content: center; gap: 6px;
}
.btn-answer:active { transform: scale(0.96); }
.btn-correct {
  background: var(--correct-bg); color: var(--correct);
  border-color: var(--correct-border);
}
.btn-correct.active { background: var(--correct); color: #fff; }
.btn-incorrect {
  background: var(--incorrect-bg); color: var(--incorrect);
  border-color: var(--incorrect-border);
}
.btn-incorrect.active { background: var(--incorrect); color: #fff; }

/* 採点結果バナー */
.result-banner {
  width: 100%;
  padding: 14px 18px;
  border-radius: 12px;
  display: flex; align-items: center; gap: 12px;
  font-size: 15px; font-weight: 700;
  margin-top: 4px;
}
.result-banner.correct {
  background: var(--correct-bg); color: var(--correct);
  border: 2px solid var(--correct-border);
}
.result-banner.incorrect {
  background: var(--incorrect-bg); color: var(--incorrect);
  border: 2px solid var(--incorrect-border);
}
.result-icon { font-size: 22px; flex-shrink: 0; }
.result-text { font-size: 14px; }
.result-text strong { font-size: 18px; }

/* 選択肢：正解/不正解 */
.btn-choice-correct {
  background: var(--correct) !important;
  color: #fff !important;
  border-color: var(--correct) !important;
  box-shadow: 0 4px 12px rgba(22,163,74,0.3);
}
.btn-choice-wrong {
  background: var(--incorrect) !important;
  color: #fff !important;
  border-color: var(--incorrect) !important;
  box-shadow: 0 4px 12px rgba(220,38,38,0.3);
}

/* カードの選択肢バッジ */
.q-choice-badge.correct { background: var(--correct); color: #fff; }
.q-choice-badge.incorrect { background: var(--incorrect); color: #fff; }

.modal-nav-row {
  display: flex; gap: 8px; align-items: center;
}
.btn-nav {
  flex: 1; padding: 10px;
  border-radius: 10px; border: 1.5px solid var(--border);
  background: var(--bg); color: var(--text);
  font-size: 14px; font-weight: 600; cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.15s;
}
.btn-nav:active { background: var(--border); }
.btn-nav:disabled { opacity: 0.35; }
.btn-reset {
  padding: 10px 14px;
  border-radius: 10px; border: 1.5px solid var(--border);
  background: var(--bg); color: var(--text-muted);
  font-size: 12px; font-weight: 600; cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

/* Choice badge on card */
.q-choice-badge {
  display: inline-flex; align-items: center; justify-content: center;
  width: 20px; height: 20px; border-radius: 50%;
  font-size: 11px; font-weight: 800;
  background: var(--primary-light); color: var(--primary);
  position: absolute; bottom: 8px; right: 8px;
}
.q-card.correct .q-choice-badge { background: var(--correct-bg); color: var(--correct); }
.q-card.incorrect .q-choice-badge { background: var(--incorrect-bg); color: var(--incorrect); }

/* tag */
.tag {
  display: inline-block; padding: 3px 10px; border-radius: 20px;
  font-size: 11px; font-weight: 700;
}
.tag-shobunrui { background: var(--primary-light); color: var(--primary); }

/* ── Fullscreen image viewer ─────────────── */
#fullscreen-viewer {
  display: none;
  position: fixed; inset: 0;
  background: #000;
  z-index: 400;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  touch-action: none;
}
#fullscreen-viewer.open { display: flex; }

#fullscreen-toolbar {
  position: absolute; top: 0; left: 0; right: 0;
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px;
  background: linear-gradient(to bottom, rgba(0,0,0,0.6), transparent);
  z-index: 1;
}
#fullscreen-label { color: #fff; font-size: 14px; font-weight: 600; opacity: 0.9; }
#fullscreen-close {
  width: 36px; height: 36px; border-radius: 50%;
  background: rgba(255,255,255,0.2); border: none;
  color: #fff; font-size: 18px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  -webkit-tap-highlight-color: transparent;
}

#fullscreen-wrap {
  width: 100%; height: 100%;
  display: flex; align-items: center; justify-content: center;
  overflow: hidden;
  touch-action: none;
}
#fullscreen-img {
  max-width: 100%; max-height: 100%;
  object-fit: contain;
  transform-origin: center center;
  user-select: none;
  -webkit-user-drag: none;
  transition: transform 0.1s ease;
}

.modal-img-hint {
  position: absolute; bottom: 8px; left: 50%; transform: translateX(-50%);
  background: rgba(0,0,0,0.5); color: #fff;
  font-size: 11px; padding: 3px 10px; border-radius: 12px;
  pointer-events: none; white-space: nowrap;
}
#modal-img-wrapper {
  position: relative; width: 100%; display: flex;
  align-items: center; justify-content: center;
  cursor: zoom-in;
}

#fullscreen-hint {
  position: absolute; bottom: 48px;
  color: rgba(255,255,255,0.5); font-size: 12px;
  text-align: center; pointer-events: none;
  transition: opacity 0.5s;
}

/* ── Desktop overrides ───────────────────── */
@media (min-width: 768px) {
  :root { --sidebar-w: 200px; }

  #app { padding-bottom: 0; display: flex; }
  #bottom-nav { display: none; }

  /* Desktop sidebar */
  #sidebar-desktop {
    width: var(--sidebar-w);
    position: fixed; top: var(--header-h); left: 0; bottom: 0;
    background: var(--surface); border-right: 1px solid var(--border);
    overflow-y: auto; padding: 12px 0; display: block !important;
  }
  .sd-label {
    font-size: 10px; font-weight: 700; color: var(--text-muted);
    text-transform: uppercase; letter-spacing: 0.06em;
    padding: 8px 16px 4px;
  }
  .sd-item {
    display: flex; align-items: center; justify-content: space-between;
    padding: 9px 16px; cursor: pointer; font-size: 13px; color: var(--text);
    transition: background 0.15s;
  }
  .sd-item:hover { background: var(--bg); }
  .sd-item.active {
    background: var(--primary-light); color: var(--primary);
    font-weight: 700; border-right: 3px solid var(--primary);
  }
  .sd-badge {
    font-size: 11px; background: var(--bg); color: var(--text-muted);
    padding: 1px 6px; border-radius: 8px;
  }
  .sd-item.active .sd-badge { background: var(--primary); color: #fff; }
  .sd-divider { height: 1px; background: var(--border); margin: 6px 0; }

  #content-area { margin-left: var(--sidebar-w); flex: 1; }
  #subcat-bar { top: var(--header-h); }
  #main { padding: 20px 24px; max-width: 1000px; }

  /* Desktop modal — centered */
  #modal-overlay { align-items: center; justify-content: center; }
  #modal {
    border-radius: 16px; width: 90%; max-width: 720px;
    animation: fadeIn 0.2s ease;
    max-height: 88vh;
  }
  @keyframes fadeIn { from { opacity: 0; transform: scale(0.96); } to { opacity: 1; transform: scale(1); } }
  #modal-handle { display: none; }
  #modal-image { max-height: 55vh; }

  /* 3-column grid on desktop */
  .q-grid { grid-template-columns: repeat(3, 1fr); }

  #header h1 { font-size: 16px; }
  #header-stats { font-size: 13px; }
  .hstat-dot { width: 10px; height: 10px; }
}

@media (min-width: 1024px) {
  .q-grid { grid-template-columns: repeat(4, 1fr); }
}
