:root {
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-2: #f1f5f9;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --success: #16a34a;
  --success-bg: #dcfce7;
  --danger: #dc2626;
  --danger-bg: #fee2e2;
  --warning: #ca8a04;
  --warning-bg: #fef9c3;

  --grid-unanswered: #e5e7eb;
  --grid-low: #fca5a5;
  --grid-mid: #fde68a;
  --grid-high: #86efac;
  --grid-current: #2563eb;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1220;
    --surface: #111a2e;
    --surface-2: #17223b;
    --border: #1f2b47;
    --text: #e2e8f0;
    --muted: #94a3b8;
    --primary: #3b82f6;
    --primary-hover: #60a5fa;
    --success: #4ade80;
    --success-bg: #064e3b;
    --danger: #f87171;
    --danger-bg: #450a0a;
    --warning: #facc15;
    --warning-bg: #422006;

    --grid-unanswered: #334155;
    --grid-low: #7f1d1d;
    --grid-mid: #78350f;
    --grid-high: #14532d;
  }
}

* { box-sizing: border-box; }

/* Always honour the HTML `hidden` attribute, even when another rule sets
   `display`. Without this, e.g. `.needs-pdf { display: flex }` would show
   elements that should be initially hidden. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Noto Sans JP",
    "Segoe UI", sans-serif;
  line-height: 1.6;
  font-size: 16px;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

/* top bar */
.top-bar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 12px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
}

.brand {
  font-weight: 700;
  font-size: 18px;
  color: var(--text);
}

.top-nav {
  display: flex;
  align-items: center;
  gap: 16px;
}

.top-nav a {
  color: var(--muted);
  font-size: 14px;
}

.top-nav a:hover { color: var(--text); }

.top-nav .quick-answer-toggle {
  margin-left: 4px;
  padding: 4px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  font-size: 13px;
}
.top-nav .quick-answer-toggle:has(input:checked) {
  background: var(--accent, #4a90e2);
  color: #fff;
  border-color: transparent;
}

.view {
  max-width: 960px;
  margin: 0 auto;
  padding: 24px 20px 80px;
  flex: 1;
  min-width: 0;
}

/* --- Layout (sidebar + main) --- */
.layout {
  display: flex;
  align-items: stretch;
  min-height: calc(100vh - 56px);
}
.sidebar {
  width: 240px;
  flex: 0 0 240px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 20px 0;
  overflow-y: auto;
  position: sticky;
  top: 0;
  align-self: stretch;
  max-height: 100vh;
}
.sidebar-section { padding: 0 12px; }
.sidebar-heading {
  font-size: 11px;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 4px 8px;
  margin-bottom: 4px;
}
.sidebar-list {
  list-style: none;
  padding: 0;
  margin: 0;
}
.sidebar-list li { margin: 2px 0; }
.sidebar-divider {
  height: 1px;
  background: var(--border);
  margin: 12px 4px;
}
.sidebar-link {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 8px 10px;
  border-radius: 8px;
  color: var(--text);
  text-decoration: none;
  font-size: 14px;
  transition: background 0.15s;
}
.sidebar-link:hover { background: var(--surface-2); }
.sidebar-link.active {
  background: var(--surface-2);
  color: var(--primary);
  font-weight: 700;
}
.sidebar-link-title { line-height: 1.3; }
.sidebar-link-meta {
  font-size: 11px;
  color: var(--muted);
  line-height: 1.2;
}
.sidebar-mini-bar {
  display: block;
  height: 4px;
  background: var(--surface-2);
  border-radius: 2px;
  overflow: hidden;
  margin-top: 4px;
}
.sidebar-mini-fill {
  display: block;
  height: 100%;
  background: linear-gradient(90deg, var(--primary), #60a5fa);
  border-radius: 2px;
  transition: width 0.3s;
}
.sidebar-accuracy {
  align-self: flex-start;
  margin-top: 4px;
  font-size: 10px;
  padding: 1px 6px;
  border-radius: 8px;
  font-weight: 700;
}
.sidebar-link.active .sidebar-mini-bar { background: rgba(255,255,255,0.2); }
.sidebar-toggle {
  display: none;
  background: transparent;
  border: none;
  color: var(--text);
  font-size: 20px;
  cursor: pointer;
  padding: 4px 8px;
}
.sidebar-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 50;
}
@media (max-width: 720px) {
  .sidebar-toggle { display: block; }
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    max-height: none;
    z-index: 60;
    transform: translateX(-100%);
    transition: transform 0.2s;
  }
  .sidebar.open { transform: translateX(0); }
  .layout { display: block; }
}

/* --- Group cards (home) --- */
.group-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 16px;
  margin-top: 12px;
}
.group-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  position: relative;
  transition: border-color 0.15s, transform 0.15s;
}
.group-card:hover { border-color: var(--primary); }
.group-card-link {
  display: block;
  color: inherit;
  text-decoration: none;
  padding: 18px 20px;
}
.group-archive-icon {
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 3;
}
.archive-subhead {
  margin-top: 32px;
  font-size: 16px;
  color: var(--muted);
  border-top: 1px solid var(--border);
  padding-top: 18px;
}
.group-card-title {
  font-size: 17px;
  font-weight: 700;
  margin: 0 0 4px;
}
.group-card-subtitle {
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 14px;
}
.group-card-stats {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.gstat {
  display: grid;
  grid-template-columns: 90px 1fr auto;
  align-items: center;
  gap: 8px;
  font-size: 13px;
}
.gstat-label {
  color: var(--muted);
  font-size: 12px;
}
.gstat-value { font-weight: 600; }
.gstat .progress-bar {
  grid-column: 1 / -1;
  height: 8px;
  margin-top: 2px;
}
.gstat .progress-fill {
  background: linear-gradient(90deg, var(--primary), #60a5fa);
}

/* --- Group detail --- */
.group-view .back {
  display: inline-block;
  margin-bottom: 12px;
  color: var(--primary);
  text-decoration: none;
  font-size: 14px;
}
.group-view .back:hover { text-decoration: underline; }
.group-view .group-subtitle {
  color: var(--muted);
  font-size: 14px;
  margin: -8px 0 8px;
}
.group-summary {
  background: var(--surface-2);
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 13px;
  margin-bottom: 16px;
}

/* --- Review corner (グループ横断復習) --- */
.review-corner {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  margin-bottom: 18px;
  padding: 0;
}
.review-corner > summary {
  list-style: none;
  cursor: pointer;
  padding: 12px 16px;
  font-weight: 700;
  font-size: 15px;
  border-bottom: 1px solid transparent;
}
.review-corner[open] > summary { border-bottom-color: var(--border); }
.review-corner > summary::-webkit-details-marker { display: none; }
.review-count {
  font-weight: 400;
  font-size: 12px;
  color: var(--muted);
  margin-left: 6px;
}
.review-content { padding: 8px 12px 12px; }
.review-filters {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-top: 6px;
}
.review-filter-row {
  display: grid;
  grid-template-columns: 200px 1fr auto;
  align-items: center;
  gap: 12px;
  padding: 8px 12px;
  border-radius: 6px;
  color: inherit;
  text-decoration: none;
  background: var(--surface-2);
  transition: background 0.15s;
}
.review-filter-row:hover { background: var(--border); }
.review-filter-label { font-weight: 600; font-size: 14px; }
.review-filter-desc { color: var(--muted); font-size: 12px; }
.review-filter-count {
  font-weight: 700;
  font-size: 13px;
  color: var(--primary);
  font-variant-numeric: tabular-nums;
}
@media (max-width: 640px) {
  .review-filter-row { grid-template-columns: 1fr auto; }
  .review-filter-desc { display: none; }
}

/* --- Group review page --- */
.group-review-view { padding-top: 8px; }
.group-review-view .back {
  display: inline-block;
  margin-bottom: 12px;
  color: var(--primary);
  text-decoration: none;
  font-size: 14px;
}
.group-review-view .back:hover { text-decoration: underline; }
.group-review-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 12px 0 16px;
  gap: 12px;
  flex-wrap: wrap;
  font-size: 14px;
  color: var(--muted);
}
.review-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.review-item a {
  display: block;
  padding: 10px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  text-decoration: none;
  color: inherit;
  transition: border-color 0.15s;
}
.review-item a:hover { border-color: var(--primary); }
.review-item-head {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.review-item-exam { font-size: 13px; color: var(--muted); }
.review-item-qn { font-weight: 700; font-size: 14px; }
.review-item-imp { color: #ca8a04; font-size: 11px; }
.review-item-imp.imp-3 { color: #dc2626; }
.review-item-stats {
  display: flex;
  gap: 6px;
  margin-top: 6px;
  flex-wrap: wrap;
}
.chip {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 700;
  color: #0f172a;
}
.chip-mute { background: var(--surface-2); color: var(--muted); }
.chip-danger { background: var(--danger-bg); color: var(--danger); }
.chip-flag { background: var(--warning-bg); color: var(--warning); }

/* --- Reference corner (group detail page) --- */
.ref-corner {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  margin-bottom: 18px;
  padding: 0;
}
.ref-corner > summary {
  list-style: none;
  cursor: pointer;
  padding: 12px 16px;
  font-weight: 700;
  font-size: 15px;
  border-bottom: 1px solid transparent;
}
.ref-corner[open] > summary { border-bottom-color: var(--border); }
.ref-corner > summary::-webkit-details-marker { display: none; }
.ref-count {
  font-weight: 400;
  font-size: 12px;
  color: var(--muted);
  margin-left: 6px;
}
.ref-content { padding: 12px 16px 16px; }
.ref-chapter { margin-bottom: 18px; }
.ref-chapter:last-child { margin-bottom: 0; }
.ref-chapter-title {
  font-size: 13px;
  font-weight: 700;
  color: var(--muted);
  text-transform: none;
  letter-spacing: 0;
  margin: 0 0 6px;
  padding-bottom: 4px;
  border-bottom: 1px solid var(--border);
}
.ref-items { display: flex; flex-direction: column; gap: 6px; }
.ref-card {
  background: var(--surface-2);
  border-radius: 6px;
  font-size: 13px;
}
.ref-card-summary {
  cursor: pointer;
  padding: 6px 12px;
  list-style: none;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}
.ref-card-summary::-webkit-details-marker { display: none; }
.ref-card-summary::before {
  content: '▸';
  margin-right: 4px;
  color: var(--muted);
  font-size: 10px;
  transition: transform 0.2s;
}
.ref-card[open] .ref-card-summary::before { transform: rotate(90deg); }
.ref-card-page { font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; }
.ref-card-body { padding: 8px 12px 12px; }
.ref-card-img {
  width: 100%;
  max-width: 700px;
  height: auto;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: #fff;
}

/* --- Stats page --- */
.stats h1 { margin-bottom: 8px; }
.stats .hint { color: var(--muted); font-size: 13px; margin: 0 0 16px; }
.stats-section {
  margin-top: 28px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 18px 20px;
}
.stats-section h2 { margin-top: 0; }
.stats-kpi-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
}
.kpi-tile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.kpi-label {
  font-size: 11px;
  color: var(--muted);
  font-weight: 600;
  letter-spacing: 0.03em;
}
.kpi-value {
  font-size: 28px;
  font-weight: 800;
  line-height: 1.1;
  color: var(--text);
}
.kpi-unit { font-size: 13px; font-weight: 600; color: var(--muted); margin-left: 3px; }
.kpi-sub { font-size: 11px; color: var(--muted); }

/* Heatmap */
.heatmap-wrap { overflow-x: auto; padding: 4px 0 8px; }
.heatmap-grid {
  display: grid;
  gap: 3px;
}
.hcell {
  display: inline-block;
  width: 14px;
  height: 14px;
  border-radius: 3px;
  background: var(--surface-2);
}
.hcell-empty { visibility: hidden; }
.hcell-0 { background: var(--surface-2); }
.hcell-1 { background: #c7e3ff; }
.hcell-2 { background: #7eb6ff; }
.hcell-3 { background: #3b82f6; }
.hcell-4 { background: #1e3a8a; }
@media (prefers-color-scheme: dark) {
  .hcell-1 { background: #1e3a5f; }
  .hcell-2 { background: #2563eb; }
  .hcell-3 { background: #60a5fa; }
  .hcell-4 { background: #93c5fd; }
}
.heatmap-legend {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  color: var(--muted);
  margin-top: 8px;
}

/* Daily bars */
.daily-bars { padding-bottom: 24px; }
.daily-bars-header {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 18px;
}
.daily-bars-summary {
  font-size: 12px;
  color: var(--muted);
}
.bars-chart {
  display: flex;
  align-items: stretch;
  gap: 6px;
  padding-top: 16px; /* バー上の数値が summary と重ならないよう余白を確保 */
}
.bars-yaxis {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  height: 160px;
  font-size: 10px;
  color: var(--muted);
  padding-right: 4px;
  border-right: 1px solid var(--border);
  min-width: 24px;
  text-align: right;
}
.bars-ytick { line-height: 1; }
.bars-grid {
  position: relative;
  display: grid;
  grid-template-columns: repeat(30, 1fr);
  gap: 3px;
  align-items: end;
  height: 160px;
  flex: 1;
}
.bars-gridline {
  position: absolute;
  left: 0;
  right: 0;
  height: 1px;
  background: var(--border);
  pointer-events: none;
}
.bars-gridline-top { top: 0; }
.bars-gridline-mid { top: 50%; }
.bar-col {
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  height: 100%;
  min-height: 1px;
}
.bar-num {
  position: absolute;
  top: -14px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 9px;
  color: var(--text);
  font-weight: 700;
  line-height: 1;
  white-space: nowrap;
  pointer-events: none;
}
.bar-fill {
  background: var(--primary);
  border-radius: 2px 2px 0 0;
  min-height: 2px;
  width: 100%;
  position: relative;
  z-index: 1;
}
.bar-fill-time {
  background: linear-gradient(180deg, #f97316, #ea580c);
}
.stats-section h3.subhead {
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  margin: 18px 0 6px;
}
.stats-section h3.subhead:first-of-type { margin-top: 8px; }
.bar-lbl {
  position: absolute;
  bottom: -18px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 9px;
  color: var(--muted);
  white-space: nowrap;
}

/* Stats table */
.stats-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
  margin-top: 6px;
}
.stats-table th,
.stats-table td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  text-align: left;
}
.stats-table th {
  font-size: 11px;
  color: var(--muted);
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}
.stats-table .num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}
.stats-table .empty-msg { text-align: center; color: var(--muted); padding: 16px; }
.stats-table tr.group-subtotal td {
  background: var(--surface-2);
  font-weight: 700;
  border-top: 2px solid var(--border);
}
.stats-table tr.group-row .group-row-title {
  padding-left: 28px;
  color: var(--muted);
}
.stats-table tr.grand-total td {
  background: var(--primary);
  color: #fff;
  font-weight: 800;
  border-top: 2px solid var(--primary-hover);
}
@media (prefers-color-scheme: dark) {
  .stats-table tr.grand-total td { color: #fff; }
}
.stats-table .progress-cell { min-width: 180px; }
.stats-table .progress-cell-text {
  font-size: 12px;
  margin-bottom: 4px;
  font-variant-numeric: tabular-nums;
}
.stats-table .progress-cell .progress-bar {
  height: 5px;
  background: var(--surface-2);
  border-radius: 3px;
  overflow: hidden;
}
.stats-table .progress-cell .progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--primary), #60a5fa);
}

h1 { font-size: 24px; margin: 0 0 16px; }
h2 { font-size: 18px; margin: 0 0 12px; }
h3 { font-size: 16px; margin: 0 0 8px; }

/* home */
.exam-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 16px;
}

.exam-card {
  position: relative;
  display: block;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px;
  color: var(--text);
  transition: border-color 0.15s, transform 0.15s;
}
.exam-card:hover {
  border-color: var(--primary);
  transform: translateY(-1px);
}
.exam-card-title {
  margin: 0 0 16px;
  font-size: 18px;
}
.exam-card-link {
  color: var(--text);
  text-decoration: none;
}
.exam-card-link:hover { text-decoration: none; }
/* Stretch the link over the whole card so the entire surface is clickable. */
.exam-card-link::after {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: inherit;
  z-index: 1;
}
/* Keep the button above the overlay so it stays independently clickable. */
.exam-card .exam-actions {
  position: relative;
  z-index: 2;
}

.exam-stats {
  display: grid;
  gap: 10px;
  margin-bottom: 16px;
}

.stat {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 4px 12px;
  font-size: 13px;
}

.stat-label {
  color: var(--muted);
  grid-column: 1;
}

.stat-value {
  grid-column: 2;
  font-weight: 600;
  text-align: right;
}

.progress-bar {
  grid-column: 1 / -1;
  height: 6px;
  background: var(--surface-2);
  border-radius: 3px;
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  background: var(--primary);
  transition: width 0.3s;
}

.exam-actions {
  display: flex;
  gap: 8px;
}

/* buttons */
.btn-primary, .btn-secondary, .btn-danger {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 16px;
  border-radius: 8px;
  border: 1px solid transparent;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  transition: background 0.15s, border-color 0.15s, opacity 0.15s;
}
.btn-primary {
  background: var(--primary);
  color: #fff;
}
.btn-primary:hover { background: var(--primary-hover); }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-secondary {
  background: var(--surface-2);
  color: var(--text);
  border-color: var(--border);
}
.btn-secondary:hover { background: var(--border); }
.btn-secondary.btn-ghost {
  background: transparent;
  color: var(--muted);
}
.btn-secondary.btn-ghost:hover {
  background: var(--surface-2);
  color: var(--text);
}
.answer-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
}

.btn-danger {
  background: var(--danger-bg);
  color: var(--danger);
  border-color: var(--danger);
}

.file-label { cursor: pointer; }

/* exam view */
.exam-header {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 16px;
}
.exam-header h1 {
  flex: 1;
  margin: 0;
  font-size: 20px;
}
.back {
  font-size: 14px;
  color: var(--muted);
  text-decoration: none;
}
.back:hover { color: var(--primary); text-decoration: underline; }
/* exam-header の戻りリンク (グループへ or ホームへ) */
.exam-header .back {
  color: var(--primary);
  font-weight: 600;
  text-decoration: none;
}
.exam-mode {
  font-size: 13px;
  color: var(--warning);
  background: var(--warning-bg);
  padding: 4px 10px;
  border-radius: 999px;
}
.exam-mode:empty { display: none; }

/* grid nav */
.grid-nav {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(34px, 1fr));
  gap: 4px;
  margin-bottom: 12px;
  padding: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
}

.grid-nav button {
  aspect-ratio: 1;
  border: 1px solid var(--border);
  background: var(--grid-unanswered);
  color: var(--text);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  border-radius: 5px;
  padding: 0;
  line-height: 1;
  font-family: inherit;
  transition: transform 0.08s, box-shadow 0.08s;
  position: relative;
}
/* 重要度バッジ (右上に小さい★) */
.grid-nav button.imp-1::after,
.grid-nav button.imp-2::after,
.grid-nav button.imp-3::after {
  content: '★';
  position: absolute;
  top: -3px;
  right: -2px;
  font-size: 8px;
  line-height: 1;
  color: var(--warning);
  pointer-events: none;
  text-shadow: 0 0 2px #fff;
}
.grid-nav button.imp-2::after { content: '★★'; font-size: 7px; }
.grid-nav button.imp-3::after { content: '★★★'; font-size: 7px; color: #dc2626; }
/* 連続正解ストリーク: 左辺の縦バー、長さが下から積み上がる (1/3 → 2/3 → 全体) */
.grid-nav button.streak-1::before,
.grid-nav button.streak-2::before,
.grid-nav button.streak-3plus::before {
  content: '';
  position: absolute;
  left: 2px;
  bottom: 3px;
  width: 4px;
  border-radius: 2px;
  background: var(--success);
  pointer-events: none;
}
.grid-nav button.streak-1::before { height: 30%; }
.grid-nav button.streak-2::before { height: 60%; }
.grid-nav button.streak-3plus::before {
  top: 3px;
  height: auto;  /* top と bottom で全高 */
  background: linear-gradient(180deg, #16a34a, #0d5a2b);
  box-shadow: 0 0 4px rgba(22, 163, 74, 0.5);
}
.grid-nav button:hover {
  transform: scale(1.08);
}
.grid-nav button.current {
  outline: 2px solid var(--grid-current);
  outline-offset: 1px;
  z-index: 1;
}
.grid-nav button.filtered {
  opacity: 0.35;
  pointer-events: none;
}
.grid-nav button.flagged {
  box-shadow: inset 0 -3px 0 var(--warning);
}

.grid-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 12px 20px;
  font-size: 12px;
  color: var(--muted);
  margin: 24px 0 12px;
  padding: 0 4px;
}
.grid-legend .sw {
  display: inline-block;
  width: 14px;
  height: 14px;
  border-radius: 3px;
  margin-right: 4px;
  vertical-align: middle;
  border: 1px solid var(--border);
}
.sw-grey { background: var(--grid-unanswered); }
.sw-red { background: var(--grid-low); }
.sw-yellow { background: var(--grid-mid); }
.sw-green { background: var(--grid-high); }
.sw-current { background: transparent; border: 2px solid var(--grid-current); }
.sw-flag { background: var(--grid-unanswered); box-shadow: inset 0 -3px 0 var(--warning); }
/* 凡例スウォッチも「積み上がる」イメージで下から緑バーを伸ばす */
.sw-streak1,
.sw-streak2,
.sw-streak3 {
  background: var(--grid-unanswered);
  position: relative;
}
.sw-streak1::before,
.sw-streak2::before,
.sw-streak3::before {
  content: '';
  position: absolute;
  left: 2px;
  bottom: 2px;
  width: 3px;
  border-radius: 1.5px;
  background: var(--success);
}
.sw-streak1::before { height: 30%; }
.sw-streak2::before { height: 60%; }
.sw-streak3::before { top: 2px; height: auto; background: linear-gradient(180deg, #16a34a, #0d5a2b); }

/* question */
.question {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 24px;
}

.question-meta {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 16px;
  margin-bottom: 12px;
  color: var(--muted);
  font-size: 13px;
}
.q-number { font-weight: 700; color: var(--text); font-size: 15px; }
.q-stats { flex: 1; }

.flag-btn {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  color: var(--muted);
  padding: 0 2px;
  line-height: 1;
  transition: color 0.15s;
}
.flag-btn:hover { color: var(--warning); }
.flag-btn.active { color: var(--warning); }

.quick-answer-toggle {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: var(--muted);
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
}
.quick-answer-toggle input[type="checkbox"] {
  margin: 0;
  cursor: pointer;
}
.quick-answer-toggle:hover { color: var(--text); }

.question-text {
  font-size: 16px;
  white-space: pre-wrap;
  margin-bottom: 20px;
  line-height: 1.8;
}

.needs-pdf {
  background: var(--warning-bg);
  color: var(--warning);
  border-radius: 8px;
  padding: 10px 14px;
  margin-bottom: 16px;
  font-size: 13px;
}
.needs-pdf .pdf-images {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: 10px;
}
.needs-pdf .pdf-img {
  width: 100%;
  height: auto;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
}
/* 画像問題用: 警告色なし */
.needs-pdf.image-only {
  background: var(--bg);
  color: var(--text);
  padding: 0;
  border: none;
}
.needs-pdf.image-only .pdf-images {
  margin-top: 0;
}

.choices {
  list-style: none;
  padding: 0;
  margin: 0 0 20px;
  display: grid;
  gap: 10px;
}
.choices li {
  background: var(--surface-2);
  border: 2px solid var(--border);
  border-radius: 10px;
  padding: 12px 16px;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
  display: flex;
  gap: 12px;
  align-items: flex-start;
}
.choices li:hover { border-color: var(--primary); }
.choices li.selected {
  border-color: var(--primary);
  background: color-mix(in srgb, var(--primary) 10%, var(--surface-2));
}
.choices li.correct {
  border-color: var(--success);
  background: var(--success-bg);
}
.choices li.incorrect {
  border-color: var(--danger);
  background: var(--danger-bg);
}
.choices li.locked { cursor: default; }
.choices li.locked:hover { border-color: var(--border); }
.choices li.locked.selected:hover { border-color: var(--primary); }
.choices li.locked.correct:hover { border-color: var(--success); }
.choices li.locked.incorrect:hover { border-color: var(--danger); }

.choice-num {
  font-weight: 700;
  min-width: 20px;
  color: var(--muted);
}
.choice-text { flex: 1; white-space: pre-wrap; }

.answer-actions { margin-bottom: 16px; }

.result {
  margin: 0 0 16px;
  padding: 16px;
  background: var(--surface-2);
  border-radius: 10px;
  border: 1px solid var(--border);
}
.result-banner {
  font-weight: 700;
  font-size: 16px;
  margin-bottom: 10px;
}
.result-banner.correct { color: var(--success); }
.result-banner.incorrect { color: var(--danger); }
.result-banner.revealed { color: var(--warning); }
.explanation-body {
  white-space: pre-wrap;
  font-size: 14px;
  line-height: 1.8;
  color: var(--text);
}
.result-time {
  margin-top: 10px;
  display: flex;
  gap: 16px;
  font-size: 12px;
  color: var(--muted);
}

.question-nav {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}
/* 「次へ」ボタンは常に右寄せ (前へ非表示時も左に寄らないよう margin-left:auto) */
.question-nav [data-slot="next"] { margin-left: auto; }

.history-details {
  font-size: 13px;
  color: var(--muted);
}
.history-details summary {
  cursor: pointer;
  padding: 6px 0;
}
.history-list {
  list-style: none;
  padding: 0;
  margin: 8px 0 0;
  display: grid;
  gap: 4px;
}
.history-list li {
  padding: 6px 10px;
  background: var(--surface-2);
  border-radius: 6px;
  display: flex;
  justify-content: space-between;
  gap: 10px;
}
.history-list li.correct { color: var(--success); }
.history-list li.incorrect { color: var(--danger); }

/* settings */
.settings-block {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 20px;
}
.settings-block .hint {
  color: var(--muted);
  font-size: 13px;
  margin: 0 0 16px;
}
.settings-block label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 12px;
}
.settings-block input[type="text"],
.settings-block input[type="password"] {
  display: block;
  width: 100%;
  margin-top: 4px;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
}
.button-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
.status-msg {
  margin-top: 12px;
  padding: 10px 14px;
  border-radius: 8px;
  font-size: 13px;
  min-height: 20px;
}
.status-msg.ok { background: var(--success-bg); color: var(--success); }
.status-msg.err { background: var(--danger-bg); color: var(--danger); }
.status-msg:empty { display: none; }

/* footer */
.footer {
  text-align: center;
  padding: 16px 20px;
  color: var(--muted);
  font-size: 12px;
}

/* card archive icon (右上の × / ↩ アイコン) */
.exam-card { position: relative; }
.card-icon-group {
  position: absolute;
  top: 8px;
  right: 8px;
  display: flex;
  gap: 2px;
  z-index: 3;
}
.card-archive-icon,
.card-reset-icon {
  position: static;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  transition: background 0.15s, color 0.15s;
  /* カード全面を覆う .exam-card-link::after (z-index:1) より上に出す */
  z-index: 3;
}
.card-archive-icon:hover {
  background: var(--surface-2);
  color: var(--danger, #dc2626);
}
.card-archive-icon.restore {
  color: var(--primary);
}
.card-archive-icon.restore:hover {
  background: var(--surface-2);
  color: var(--primary);
}
.card-reset-icon {
  font-size: 16px;
}
.card-reset-icon:hover:not(:disabled) {
  background: var(--surface-2);
  color: var(--danger, #dc2626);
}
.card-reset-icon:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}

/* 直近の正答率の背景色分け（赤→黄→緑のグラデーション） */
.accuracy-badge,
.stat-value.accuracy-badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 12px;
  font-weight: 700;
  color: #0f172a;
  min-width: 48px;
  text-align: center;
}
.accuracy-badge.none,
.stat-value.accuracy-badge.none {
  background: var(--surface-2);
  color: var(--muted);
  font-weight: 500;
}

/* mode dropdown (学習モード選択) */
.mode-dropdown {
  width: 100%;
  appearance: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  padding: 10px 36px 10px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font-size: 14px;
  cursor: pointer;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><polyline points='6 9 12 15 18 9'></polyline></svg>");
  background-repeat: no-repeat;
  background-position: right 12px center;
}
.mode-dropdown:hover {
  border-color: var(--primary);
}
.mode-dropdown:focus {
  outline: 2px solid var(--primary);
  outline-offset: -1px;
}

.archive-empty {
  padding: 24px;
  text-align: center;
  color: var(--muted);
  background: var(--surface-2);
  border-radius: 8px;
}
.archive-view .hint {
  color: var(--muted);
  margin-bottom: 16px;
}

/* shuffle */
.shuffle-note {
  margin-bottom: 8px;
  padding: 6px 10px;
  font-size: 12px;
  color: var(--muted);
  background: var(--surface-2);
  border-left: 3px solid var(--border);
  border-radius: 4px;
}
/* 解説中の置換成功部分（黄色ハイライト、ホバーで元の表記を tooltip 表示） */
.shuffle-replaced {
  background: #fef3c7;
  color: #92400e;
  padding: 0 4px;
  border-radius: 3px;
  font-weight: 600;
  cursor: help;
}
/* 解説中の置換失敗部分（赤系で警告、元のまま残す） */
.shuffle-failed {
  background: #fee2e2;
  color: #991b1b;
  padding: 0 4px;
  border-radius: 3px;
  font-weight: 600;
  text-decoration: underline wavy;
  cursor: help;
}
.toggle-row {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
}
.toggle-row input[type="checkbox"] {
  width: 18px;
  height: 18px;
  cursor: pointer;
}

/* responsive */
@media (max-width: 600px) {
  .view { padding: 16px 12px 80px; }
  .question { padding: 16px; }
  .grid-nav { grid-template-columns: repeat(auto-fill, minmax(30px, 1fr)); }
  .exam-header h1 { font-size: 18px; }
}

/* ============================================================
 * 数式表示 (MathFmt)
 * ============================================================ */

/* 数式コンテナ全般: 数式向けフォントを優先 */
.mf-math {
  font-family: 'Cambria Math', 'STIX Two Math', 'Latin Modern Math',
               'Times New Roman', 'Hiragino Mincho ProN', 'Yu Mincho', serif;
  font-style: italic;
  white-space: nowrap;
  /* インライン中で行高を崩さないよう、行送りを少し稼ぐ */
  line-height: 1.0;
}
.mf-math.mf-block {
  display: block;
  text-align: center;
  margin: 0.6em 0;
  font-size: 1.1em;
}

/* 添え字・指数 */
.mf-math sup, .mf-math sub {
  font-size: 0.7em;
  line-height: 0;
  position: relative;
  vertical-align: baseline;
  font-style: italic;
}
.mf-math sup { top: -0.55em; }
.mf-math sub { bottom: -0.25em; }

/* 関数名はローマン体 */
.mf-math .mf-fn {
  font-style: normal;
  margin-right: 0.15em;
}

/* 大演算子 */
.mf-math .mf-bigop {
  font-style: normal;
  font-size: 1.3em;
  vertical-align: -0.1em;
  margin: 0 0.1em;
}

/* 分数 */
.mf-frac {
  display: inline-flex;
  flex-direction: column;
  vertical-align: -0.45em;
  text-align: center;
  margin: 0 0.18em;
  font-style: italic;
}
.mf-frac > .mf-num {
  border-bottom: 1px solid currentColor;
  padding: 0 0.25em 0.05em;
  font-size: 0.85em;
  line-height: 1.1;
}
.mf-frac > .mf-den {
  padding: 0.05em 0.25em 0;
  font-size: 0.85em;
  line-height: 1.1;
}

/* 平方根 */
.mf-sqrt {
  display: inline-flex;
  align-items: stretch;
  vertical-align: -0.1em;
  font-style: italic;
}
.mf-sqrt::before {
  content: '√';
  font-style: normal;
  font-size: 1.1em;
  margin-right: 0.05em;
}
.mf-sqrt > .mf-radicand {
  border-top: 1px solid currentColor;
  padding: 0.05em 0.2em 0;
  display: inline-block;
}

/* 装飾アクセント */
.mf-hat, .mf-bar, .mf-vec, .mf-dot, .mf-tilde {
  display: inline-block;
  position: relative;
  padding-top: 0.3em;
  font-style: italic;
}
.mf-hat::before, .mf-bar::before, .mf-vec::before,
.mf-dot::before, .mf-tilde::before {
  position: absolute;
  left: 50%;
  top: 0;
  transform: translateX(-50%);
  font-style: normal;
  font-size: 0.85em;
  line-height: 1;
}
.mf-hat::before { content: '^'; }
.mf-bar::before { content: '‾'; }
.mf-vec::before { content: '→'; font-size: 0.7em; top: 0.1em; }
.mf-dot::before { content: '·'; font-size: 1.1em; top: -0.1em; }
.mf-tilde::before { content: '~'; }
.mf-overline { text-decoration: overline; }
.mf-underline { text-decoration: underline; }

/* 設問・選択肢・解説で数式行が改行されにくくする */
.question-text, .choice-text, .explanation-body {
  word-break: normal;
  overflow-wrap: anywhere;
}
.question-text .mf-math,
.choice-text .mf-math,
.explanation-body .mf-math {
  /* 長い数式は折り返し可 */
  white-space: nowrap;
}

/* ダークモード対応: 線色は currentColor を使うので追加スタイル不要 */
