:root {
  --bg: #f4f6f9;
  --panel: #ffffff;
  --ink: #16202e;
  --muted: #57616e;
  --line: #dbe1ea;
  --primary: #2563eb;
  --primary-d: #1d4ed8;
  --ok: #16a34a;
  --ng: #dc2626;
  --warn: #d97706;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(16,24,40,.08), 0 1px 2px rgba(16,24,40,.04);
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: "Segoe UI", "Hiragino Kaku Gothic ProN", "Yu Gothic UI", Meiryo, sans-serif;
  background: var(--bg);
  color: var(--ink);
  line-height: 1.7;
  font-size: 15px;
}

.app-header {
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  padding: 12px 20px;
  position: sticky;
  top: 0;
  z-index: 10;
}
.app-header h1 { font-size: 18px; margin: 0 0 8px; }

.tabs { display: flex; gap: 4px; flex-wrap: wrap; }
.tab {
  border: none;
  background: transparent;
  padding: 8px 16px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  color: var(--muted);
  font-weight: 600;
}
.tab:hover { background: var(--bg); }
.tab.active { background: var(--primary); color: #fff; }
.badge {
  display: inline-block;
  min-width: 18px;
  padding: 0 5px;
  margin-left: 6px;
  border-radius: 9px;
  background: var(--ng);
  color: #fff;
  font-size: 11px;
  text-align: center;
  line-height: 18px;
}
.tab.active .badge { background: rgba(255,255,255,.3); }

main {
  max-width: 860px;
  margin: 0 auto;
  padding: 20px;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  margin-bottom: 16px;
}
.panel h2 { font-size: 16px; margin: 0 0 14px; }

.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 13px; }

.field { margin-bottom: 16px; }
.field > label { display: block; font-weight: 600; font-size: 13px; margin-bottom: 6px; }
.radio-row { display: flex; gap: 16px; flex-wrap: wrap; }
.radio-row label, .field label input { font-weight: 400; }

select, input[type="number"] {
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: 14px;
  background: #fff;
  margin-right: 8px;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  padding: 9px 16px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
}
.btn:hover { background: var(--bg); }
.btn.primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn.primary:hover { background: var(--primary-d); }
.btn.danger { color: var(--ng); border-color: #f3c2c2; }
.btn.danger:hover { background: #fdecec; }

/* Quiz */
.quiz-top { margin-bottom: 16px; }
.progress { height: 6px; background: var(--line); border-radius: 3px; overflow: hidden; }
.progress-bar { height: 100%; width: 0; background: var(--primary); transition: width .3s; }
.quiz-meta { display: flex; justify-content: space-between; margin-top: 6px; font-size: 13px; }

.q-tags { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 10px; }
.tag {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 6px;
  background: #eef2ff;
  color: var(--primary-d);
  font-weight: 600;
}
.tag.year { background: #f1f5f9; color: var(--muted); }

.q-text { font-size: 16px; margin-bottom: 16px; white-space: pre-wrap; }

.choices { display: flex; flex-direction: column; gap: 8px; }
.choice {
  text-align: left;
  border: 1px solid var(--line);
  background: #fff;
  padding: 12px 14px;
  border-radius: 10px;
  cursor: pointer;
  font-size: 14px;
  display: flex;
  gap: 10px;
  align-items: flex-start;
}
.choice:hover:not(:disabled) { border-color: var(--primary); background: #f8faff; }
.choice .mark { font-weight: 700; color: var(--muted); flex-shrink: 0; }
.choice.correct { border-color: var(--ok); background: #f0fdf4; }
.choice.wrong { border-color: var(--ng); background: #fef2f2; }
.choice.correct .mark { color: var(--ok); }
.choice.wrong .mark { color: var(--ng); }
.choice:disabled { cursor: default; }

.feedback {
  margin-top: 16px;
  padding: 14px;
  border-radius: 10px;
  font-size: 14px;
}
.feedback.ok { background: #f0fdf4; border: 1px solid #bbf7d0; }
.feedback.ng { background: #fef2f2; border: 1px solid #fecaca; }
.feedback .verdict { font-weight: 700; margin-bottom: 6px; }
.feedback .exp { white-space: pre-wrap; color: var(--ink); }

.quiz-actions { display: flex; gap: 10px; margin-top: 18px; flex-wrap: wrap; }

.result-score { font-size: 32px; font-weight: 800; text-align: center; margin: 10px 0; }
.result-score small { font-size: 14px; color: var(--muted); font-weight: 500; }

/* Lists */
.q-list { display: flex; flex-direction: column; gap: 8px; }
.q-row {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px 14px;
  cursor: pointer;
  background: #fff;
}
.q-row:hover { background: var(--bg); }
.q-row-head { display: flex; justify-content: space-between; gap: 10px; align-items: center; }
.q-row-title { font-size: 14px; }
.q-row-meta { font-size: 12px; color: var(--muted); flex-shrink: 0; }
.q-row-body { margin-top: 10px; padding-top: 10px; border-top: 1px dashed var(--line); }
.miss-count { color: var(--ng); font-weight: 700; }

.empty { text-align: center; color: var(--muted); padding: 30px; }

/* Stat cards */
.stat-cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px,1fr)); gap: 12px; }
.stat-card { background: var(--bg); border-radius: 10px; padding: 14px; text-align: center; }
.stat-card .num { font-size: 26px; font-weight: 800; }
.stat-card .lbl { font-size: 12px; color: var(--muted); }

/* Bar chart */
.seg-toggle { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; margin-bottom: 14px; }
.seg { border: none; background: #fff; padding: 6px 14px; cursor: pointer; font-size: 13px; font-weight: 600; color: var(--muted); }
.seg.active { background: var(--primary); color: #fff; }

.bar-row { margin-bottom: 12px; }
.bar-label { display: flex; justify-content: space-between; font-size: 13px; margin-bottom: 4px; }
.bar-label .rate { font-weight: 700; }
.bar-track { height: 20px; background: var(--line); border-radius: 5px; overflow: hidden; }
.bar-fill { height: 100%; border-radius: 5px; transition: width .4s; display:flex; align-items:center; }
.bar-fill.good { background: var(--ok); }
.bar-fill.mid { background: var(--warn); }
.bar-fill.bad { background: var(--ng); }

/* Trend */
.trend-wrap { overflow-x: auto; }
svg.trend { display: block; }

.code-block {
  background: #0f172a;
  color: #e2e8f0;
  padding: 14px;
  border-radius: 10px;
  overflow-x: auto;
  font-size: 12px;
  line-height: 1.5;
}

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: #1f2733;
  color: #fff;
  padding: 10px 18px;
  border-radius: 8px;
  font-size: 14px;
  z-index: 100;
  box-shadow: var(--shadow);
}

/* ===== 問題画像（IPA公式過去問） ===== */
.q-image {
  margin-bottom: 16px;
  overflow-x: auto;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px;
}
.q-image img {
  display: block;
  width: 100%;
  max-width: 100%;
  height: auto;
  image-rendering: -webkit-optimize-contrast;
}
.answer-line {
  margin: 12px 0;
  font-size: 15px;
}
.answer-line b { color: var(--ok); font-size: 18px; }

/* 画像方式は選択肢が画像内にあるので ア/イ/ウ/エ を横並びの大きなボタンに */
.choices.letters { flex-direction: row; gap: 10px; }
.choices.letters .choice {
  flex: 1;
  justify-content: center;
  padding: 16px 0;
  font-size: 20px;
  font-weight: 700;
}
.choices.letters .choice .mark { font-size: 22px; color: var(--ink); }
.choices.letters .choice.correct .mark { color: var(--ok); }
.choices.letters .choice.wrong .mark { color: var(--ng); }

/* ===== 解説管理 ===== */
.badge.flag { background: var(--warn); }

.q-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(38px, 1fr));
  gap: 6px;
  margin: 12px 0;
}
.q-grid .cell {
  position: relative;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px 0;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
  background: #eef1f5;
  color: var(--muted);
}
.q-grid .cell:hover { outline: 2px solid var(--primary); }
.q-grid .cell.done { background: #dcfce7; color: #14532d; border-color: #bbf7d0; }
.q-grid .cell.none { background: #eef1f5; color: #64748b; }
.q-grid .cell.flagged { background: #fee2e2; color: #991b1b; border-color: #fecaca; }
.q-grid .cell.dup::after {
  content: "";
  position: absolute; top: 2px; right: 2px;
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--primary);
}

.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 12px; color: var(--muted); }
.legend i {
  display: inline-block; width: 12px; height: 12px; border-radius: 3px;
  margin-right: 4px; vertical-align: -2px; border: 1px solid var(--line);
}
.legend .sw.done { background: #dcfce7; }
.legend .sw.none { background: #eef1f5; }
.legend .sw.flagged { background: #fee2e2; }
.legend .sw.dup { background: #fff; position: relative; }
.legend .sw.dup::after {
  content: ""; position: absolute; top: 1px; right: 1px;
  width: 5px; height: 5px; border-radius: 50%; background: var(--primary);
}

.tag.dup-tag { background: #eff6ff; color: var(--primary-d); }
.tag.flag-tag { background: #fee2e2; color: #991b1b; }
.tag.none-tag { background: #f1f5f9; color: var(--muted); }

/* ===== 問題エディタ ===== */
.q-form label { display: block; font-weight: 600; font-size: 13px; margin-bottom: 6px; }
.q-form input[type="text"], .q-form textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: 15px;
  font-family: inherit;
  margin-top: 4px;
}
.q-form textarea { resize: vertical; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 12px; }
.q-form label.full { margin-bottom: 12px; }

.choice-inputs { display: flex; flex-direction: column; gap: 8px; margin-bottom: 10px; }
.choice-input { display: flex; align-items: center; gap: 8px; }
.ci-radio { display: flex; align-items: center; gap: 4px; flex-shrink: 0; margin: 0; font-weight: 700; color: var(--muted); }
.ci-radio input { width: 18px; height: 18px; }
.ci-text { flex: 1; margin-top: 0 !important; }
.ci-del { border: none; background: #f1f5f9; color: var(--muted); width: 32px; height: 36px; border-radius: 8px; cursor: pointer; font-size: 18px; flex-shrink: 0; }
.ci-del:hover { background: #fde2e2; color: var(--ng); }
.btn.small { padding: 6px 12px; font-size: 13px; }

/* ===== 使い方 ===== */
.help h3 { font-size: 15px; margin: 22px 0 8px; padding-top: 14px; border-top: 1px solid var(--line); }
.help h3:first-of-type { border-top: none; padding-top: 0; }
.help ul, .help ol { margin: 6px 0; padding-left: 22px; }
.help li { margin-bottom: 6px; }
.help p { margin: 8px 0; }
.help code { background: #eef2ff; color: var(--primary-d); padding: 1px 6px; border-radius: 4px; font-size: 13px; }
.help .code-block { margin: 8px 0; }

/* ===== レスポンシブ（スマホ対応） ===== */
@media (max-width: 640px) {
  body { font-size: 15px; }
  main { padding: 12px; }
  .app-header { padding: 10px 12px; }
  .app-header h1 { font-size: 16px; }
  .tabs {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    padding-bottom: 2px;
  }
  .tabs::-webkit-scrollbar { display: none; }
  .tab { padding: 8px 12px; flex-shrink: 0; }

  .panel { padding: 14px; border-radius: 10px; }
  .q-text { font-size: 16px; }

  /* タップしやすい選択肢 */
  .choice { padding: 14px; font-size: 15px; }

  .form-grid { grid-template-columns: 1fr; }
  .stat-cards { grid-template-columns: repeat(2, 1fr); }
  .radio-row { flex-direction: column; gap: 8px; }
  .quiz-actions .btn { flex: 1; justify-content: center; }
  select, input[type="number"] { font-size: 16px; } /* iOSの自動ズーム防止 */
}
