:root {
  --bg: #efe7d6;          /* パーチメント */
  --card: #fcf8ee;        /* アイボリー */
  --ink: #382f26;         /* 焦げ茶 */
  --ink-soft: #6d6151;
  --line: #e2d7c1;        /* 温かみのある罫線 */
  --brand: #1f6e5f;       /* 深い緑（ヘッダーに合わせる） */
  --brand-dark: #14564a;
  --brand-soft: #e3ece4;  /* 淡い緑 */
  --ok: #4e7d3f;          /* オリーブ */
  --ok-soft: #e9efdb;
  --ng: #b5452f;          /* テラコッタ */
  --ng-soft: #f5e6df;
  --warn: #a9802e;        /* 黄土色 */
  --gold: #b78a3c;        /* 金土色のアクセント */
  --serif: "Yu Mincho", "游明朝", "Hiragino Mincho ProN", "ＭＳ Ｐ明朝", Georgia, serif;
  --shadow: 0 1px 3px rgba(60,45,20,.12), 0 6px 16px rgba(60,45,20,.06);
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, "Segoe UI", "Hiragino Kaku Gothic ProN", "Yu Gothic UI", Meiryo, sans-serif;
  background: var(--bg);
  color: var(--ink);
  line-height: 1.7;
  -webkit-text-size-adjust: 100%;
  overflow-x: hidden;
}
.app-header {
  background-image:
    linear-gradient(100deg, rgba(18, 66, 56, .82), rgba(28, 96, 82, .55)),
    url("header.jpg");
  background-size: cover;
  background-position: center 38%;
  color: #fff;
  padding: 18px 20px;
  position: sticky;
  top: 0;
  z-index: 10;
  border-bottom: 3px solid var(--gold);
  box-shadow: var(--shadow);
}
.app-header h1 {
  margin: 0; font-family: var(--serif); font-size: 1.25rem; font-weight: 700;
  letter-spacing: .02em; cursor: pointer; display: inline-block;
  text-shadow: 0 1px 4px rgba(0, 0, 0, .55);
  overflow-wrap: anywhere;
}
.header-sub {
  font-size: .76rem; opacity: .95; margin-top: 3px;
  text-shadow: 0 1px 3px rgba(0, 0, 0, .55);
}
.app-footer {
  text-align: center;
  color: var(--ink-soft);
  font-size: .74rem;
  padding: 24px 16px 40px;
}
#app { max-width: 760px; margin: 0 auto; padding: 18px 16px 8px; }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 18px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
}
h2.section-title {
  font-family: var(--serif); font-size: 1.05rem; font-weight: 700;
  margin: 2px 0 14px; padding-bottom: 7px; color: var(--ink);
  border-bottom: 1px solid var(--line);
}
h2.section-title::before {
  content: ""; display: inline-block; width: 8px; height: 8px;
  margin-right: 8px; border-radius: 50%; background: var(--gold);
  vertical-align: middle; transform: translateY(-1px);
}

/* 折りたたみセクション */
details.collapse > summary {
  font-family: var(--serif); font-size: 1.05rem; font-weight: 700; color: var(--ink);
  cursor: pointer; list-style: none; display: flex; align-items: center;
}
details.collapse > summary::-webkit-details-marker { display: none; }
details.collapse > summary::before {
  content: ""; display: inline-block; width: 8px; height: 8px;
  margin-right: 8px; border-radius: 50%; background: var(--gold);
}
details.collapse > summary::after {
  content: "▾"; margin-left: auto; color: var(--ink-soft); transition: transform .2s;
}
details.collapse[open] > summary {
  border-bottom: 1px solid var(--line); padding-bottom: 7px; margin-bottom: 14px;
}
details.collapse[open] > summary::after { transform: rotate(180deg); }

/* ---- ドット絵マスコット（正答率60%未満で踊る） ---- */
.mascots { display: inline-flex; gap: 2px; margin-left: 8px; vertical-align: middle; }
.mascot {
  width: 32px; height: 32px;
  image-rendering: pixelated; image-rendering: crisp-edges;
  transform-origin: bottom center;
}
.m-horse { animation: dance-l .55s ease-in-out infinite; }
.m-deer { animation: dance-r .55s ease-in-out infinite; animation-delay: .27s; }
@keyframes dance-l {
  0%   { transform: translateY(0) rotate(-6deg); }
  50%  { transform: translateY(-5px) rotate(6deg); }
  100% { transform: translateY(0) rotate(-6deg); }
}
@keyframes dance-r {
  0%   { transform: translateY(0) rotate(6deg); }
  50%  { transform: translateY(-5px) rotate(-6deg); }
  100% { transform: translateY(0) rotate(6deg); }
}
@media (prefers-reduced-motion: reduce) {
  .m-horse, .m-deer { animation: none; }
}

/* ---- AI分析の相棒FAB（ホーム右下に常駐） ---- */
.ai-fab {
  position: fixed;
  right: calc(16px + env(safe-area-inset-right, 0px));
  bottom: calc(18px + env(safe-area-inset-bottom, 0px));
  z-index: 30;
  display: flex; align-items: flex-end; justify-content: center;
  width: 68px; height: 68px;
  padding: 0;
  border: 2px solid #fff;
  border-radius: 50%;
  background: radial-gradient(120% 120% at 30% 20%, #2a8a78, var(--brand) 55%, var(--brand-dark));
  box-shadow: 0 6px 18px rgba(20, 86, 74, .38), 0 2px 5px rgba(0,0,0,.18);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  animation: fab-pop .45s cubic-bezier(.22,1.3,.4,1) both;
  transition: transform .18s ease, box-shadow .18s ease;
}
.ai-fab:hover { transform: translateY(-3px) scale(1.05); box-shadow: 0 10px 24px rgba(20, 86, 74, .45); }
.ai-fab:active { transform: scale(.94); }
.ai-fab:focus-visible { outline: 3px solid var(--gold); outline-offset: 3px; }

.ai-fab-pets { display: inline-flex; gap: 1px; padding-bottom: 9px; }
.ai-fab .mascot { width: 26px; height: 26px; }
/* 通常はゆるくゆらゆら、苦戦中(cheer)は踊って応援、好調(happy)は弾む */
.ai-fab.idle  .m-horse { animation: wobble-l 2.2s ease-in-out infinite; }
.ai-fab.idle  .m-deer  { animation: wobble-r 2.2s ease-in-out infinite; animation-delay: .3s; }
.ai-fab.cheer .m-horse { animation: dance-l .55s ease-in-out infinite; }
.ai-fab.cheer .m-deer  { animation: dance-r .55s ease-in-out infinite; animation-delay: .27s; }
.ai-fab.happy .m-horse { animation: hop 1s ease-in-out infinite; }
.ai-fab.happy .m-deer  { animation: hop 1s ease-in-out infinite; animation-delay: .2s; }
/* hover中はどの気分でも元気に踊る */
.ai-fab:hover .m-horse { animation: dance-l .5s ease-in-out infinite; }
.ai-fab:hover .m-deer  { animation: dance-r .5s ease-in-out infinite; animation-delay: .25s; }

/* 分析を示す小バッジ */
.ai-fab-badge {
  position: absolute; right: -4px; top: -6px;
  width: 26px; height: 26px;
  display: flex; align-items: center; justify-content: center;
  font-size: 14px; line-height: 1;
  background: var(--gold); color: #fff;
  border: 2px solid #fff; border-radius: 50%;
  box-shadow: 0 1px 4px rgba(0,0,0,.25);
}

/* 吹き出し */
.ai-fab-bubble {
  position: absolute; right: 0; bottom: calc(100% + 12px);
  max-width: 60vw; white-space: nowrap;
  background: var(--card); color: var(--ink);
  font-size: .8rem; font-weight: 600;
  padding: 7px 12px; border-radius: 14px;
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  transform-origin: bottom right;
  animation: bubble-in .5s cubic-bezier(.22,1.3,.4,1) .35s both;
}
.ai-fab-bubble::after {
  content: ""; position: absolute; right: 22px; top: 100%;
  border: 7px solid transparent;
  border-top-color: var(--card);
  filter: drop-shadow(0 1px 0 var(--line));
}
/* 苦戦中はセリフをテラコッタ寄りに */
.ai-fab.cheer .ai-fab-bubble { border-color: #e7c2b6; color: var(--ng); }

@keyframes fab-pop {
  0%   { transform: scale(0) rotate(-25deg); }
  100% { transform: scale(1) rotate(0); }
}
@keyframes bubble-in {
  0%   { opacity: 0; transform: scale(.6) translateY(6px); }
  60%  { opacity: 1; transform: scale(1.04); }
  100% { opacity: 1; transform: scale(1); }
}
@keyframes wobble-l {
  0%,100% { transform: rotate(-4deg); }
  50%     { transform: rotate(4deg); }
}
@keyframes wobble-r {
  0%,100% { transform: rotate(4deg); }
  50%     { transform: rotate(-4deg); }
}
@keyframes hop {
  0%,100% { transform: translateY(0); }
  40%     { transform: translateY(-6px); }
}
/* FABがリセットボタン等に被らないよう余白 */
body.has-fab .app-footer { padding-bottom: 96px; }

@media (prefers-reduced-motion: reduce) {
  .ai-fab, .ai-fab-bubble { animation: none; }
  .ai-fab .m-horse, .ai-fab .m-deer { animation: none !important; }
}

@media (max-width: 560px) {
  .ai-fab { width: 62px; height: 62px; right: 12px; bottom: 14px; }
  .ai-fab .mascot { width: 23px; height: 23px; }
}

/* ---- ダッシュボード ---- */
.stat-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.stat {
  background: var(--brand-soft);
  border-radius: 12px;
  padding: 12px 8px;
  text-align: center;
}
.stat .num { font-family: var(--serif); font-size: 1.6rem; font-weight: 700; color: var(--brand-dark); }
.stat .lbl { font-size: .72rem; color: var(--ink-soft); margin-top: 2px; }
.progress-line { height: 8px; background: var(--line); border-radius: 99px; overflow: hidden; margin-top: 14px; }
.progress-line > span { display: block; height: 100%; background: var(--brand); transition: width .3s; }
.progress-cap { font-size: .76rem; color: var(--ink-soft); margin-top: 6px; }

/* ---- メニュー ---- */
.menu-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.mode-btn {
  display: flex; flex-direction: column; gap: 4px;
  text-align: left;
  background: var(--card);
  border: 1.5px solid var(--line);
  border-radius: 12px;
  padding: 14px;
  cursor: pointer;
  font: inherit;
  transition: border-color .15s, transform .05s;
}
.mode-btn:hover { border-color: var(--brand); }
.mode-btn:active { transform: scale(.99); }
.mode-btn .mb-title { font-weight: 700; font-size: .96rem; }
.mode-btn .mb-desc { font-size: .76rem; color: var(--ink-soft); }
.mode-btn.wide { grid-column: 1 / -1; }
.mode-btn.danger { border-color: var(--ng); color: var(--ng); }

/* ---- 分野別正答率 ---- */
.cat-list { display: flex; flex-direction: column; gap: 8px; }
.cat-row {
  display: grid; grid-template-columns: 1fr 90px 42px auto; align-items: center; gap: 10px;
  background: #fff; border: 1px solid var(--line); border-radius: 10px;
  padding: 9px 12px; cursor: pointer; font: inherit; text-align: left;
  transition: border-color .12s;
}
.cat-row:hover { border-color: var(--brand); }
.cr-name { font-size: .88rem; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cr-bar { height: 8px; background: var(--line); border-radius: 99px; overflow: hidden; }
.cr-bar > span { display: block; height: 100%; border-radius: 99px; }
.cr-bar > span.lo { background: var(--ng); }
.cr-bar > span.mid { background: var(--warn); }
.cr-bar > span.hi { background: var(--ok); }
.cr-pct { font-size: .85rem; font-weight: 700; text-align: right; color: var(--ink); }
.cr-sub { font-size: .72rem; color: var(--ink-soft); white-space: nowrap; }
@media (max-width: 480px) {
  .cat-row { grid-template-columns: 1fr 60px 38px; }
  .cr-sub { display: none; }
}

/* ---- ブックマークボタン（問題画面） ---- */
.bm-btn {
  margin-left: auto;
  display: inline-flex; align-items: center; gap: 4px;
  font-size: .74rem; padding: 3px 10px; border-radius: 99px;
  border: 1px solid var(--line); background: #fff; color: var(--ink-soft);
  cursor: pointer; font-family: inherit;
}
.bm-btn .bm-ic { filter: grayscale(1); opacity: .5; }
.bm-btn:hover { border-color: var(--brand); }
.bm-btn.on { background: #fdf0d9; border-color: var(--warn); color: var(--warn); font-weight: 700; }
.bm-btn.on .bm-ic { filter: none; opacity: 1; }

/* ---- 問題検索 ---- */
.search-head {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  margin: 16px 0 10px; font-size: .85rem; color: var(--ink-soft); font-weight: 600;
}
.btn.sm { padding: 6px 12px; font-size: .8rem; }
.search-list { display: flex; flex-direction: column; gap: 8px; }
.search-item {
  display: flex; flex-direction: column; gap: 3px;
  background: #fff; border: 1px solid var(--line); border-radius: 10px;
  padding: 10px 12px; cursor: pointer; font: inherit; text-align: left;
  transition: border-color .12s;
}
.search-item:hover { border-color: var(--brand); }
.si-meta { font-size: .72rem; color: var(--brand-dark); font-weight: 700; }
.si-stem { font-size: .86rem; color: var(--ink); }

/* ---- AI分析レポート ---- */
.report-note { font-size: .82rem; color: var(--ink-soft); margin: 0 0 12px; line-height: 1.6; }
.report-area {
  width: 100%; height: 320px; margin-top: 12px;
  font-family: ui-monospace, "SFMono-Regular", Consolas, "Courier New", monospace;
  font-size: .76rem; line-height: 1.5; color: var(--ink);
  border: 1px solid var(--line); border-radius: 10px; padding: 12px;
  background: #faf4e7; resize: vertical; white-space: pre;
}

label.field { display: block; margin-bottom: 12px; font-size: .85rem; color: var(--ink-soft); }
label.field select, label.field input {
  display: block; width: 100%; margin-top: 5px;
  padding: 9px 10px; font: inherit;
  border: 1.5px solid var(--line); border-radius: 9px; background: #fff;
}
.row-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  background: var(--brand); color: #fff; border: none;
  padding: 11px 18px; border-radius: 10px; font: inherit; font-weight: 700;
  cursor: pointer; transition: background .15s, transform .05s;
}
.btn:hover { background: var(--brand-dark); }
.btn:active { transform: scale(.98); }
.btn:disabled { background: #b8c2cc; cursor: default; }
.btn.ghost { background: transparent; color: var(--brand); border: 1.5px solid var(--brand); }
.btn.ghost:hover { background: var(--brand-soft); }
.btn.block { width: 100%; }
.btn-row { display: flex; gap: 10px; margin-top: 10px; }

/* ---- クイズ ---- */
.quiz-top { display: flex; align-items: center; gap: 12px; margin-bottom: 4px; }
.quiz-top .qprog { font-size: .82rem; color: var(--ink-soft); white-space: nowrap; }
.quiz-top .bar { flex: 1; height: 7px; background: var(--line); border-radius: 99px; overflow: hidden; }
.quiz-top .bar > span { display: block; height: 100%; background: var(--brand); transition: width .25s; }
.timer { font-variant-numeric: tabular-nums; font-weight: 700; color: var(--brand-dark); }
.timer.warn { color: var(--ng); }

.qmeta { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin: 4px 0 10px; }
.badge {
  font-size: .72rem; padding: 2px 9px; border-radius: 99px;
  background: var(--brand-soft); color: var(--brand-dark); font-weight: 700;
}
.badge.cat { background: #ece1cd; color: var(--ink-soft); }
.badge.multi { background: #fdf0d9; color: var(--warn); }
.badge.excl { background: var(--ng-soft); color: var(--ng); }
.stars { color: var(--gold); letter-spacing: 1px; font-size: .85rem; }

.stem { font-size: 1.04rem; white-space: pre-wrap; margin: 6px 0 14px; }
.fig { text-align: center; margin: 12px 0; }
.fig img {
  max-width: 100%; border: 1px solid var(--line); border-radius: 8px;
  cursor: zoom-in; background: #fff;
}

.choices { display: flex; flex-direction: column; gap: 9px; }
.choice {
  display: flex; align-items: flex-start; gap: 10px;
  border: 1.5px solid var(--line); border-radius: 11px;
  padding: 11px 13px; cursor: pointer; background: #fff;
  font: inherit; text-align: left; transition: border-color .12s, background .12s;
}
.choice:hover { border-color: var(--brand); }
.choice .ci {
  flex: none; width: 26px; height: 26px; border-radius: 50%;
  background: #ece1cd; color: var(--ink-soft);
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: .85rem;
}
.choice .ct { flex: 1; }
.choice.selected { border-color: var(--brand); background: var(--brand-soft); }
.choice.selected .ci { background: var(--brand); color: #fff; }
.choice.correct { border-color: var(--ok); background: var(--ok-soft); }
.choice.correct .ci { background: var(--ok); color: #fff; }
.choice.wrong { border-color: var(--ng); background: var(--ng-soft); }
.choice.wrong .ci { background: var(--ng); color: #fff; }
.choice.locked { cursor: default; }
.choice.locked:hover { border-color: inherit; }

.result-tag { font-weight: 700; margin: 14px 0 8px; font-size: 1.05rem; }
.result-tag.ok { color: var(--ok); }
.result-tag.ng { color: var(--ng); }
.expl {
  background: #faf4e7; border: 1px solid var(--line); border-left: 4px solid var(--brand);
  border-radius: 8px; padding: 13px 15px; margin-top: 6px;
  white-space: pre-wrap; font-size: .92rem; color: var(--ink);
}
.expl-label { font-size: .76rem; color: var(--brand-dark); font-weight: 700; margin-bottom: 4px; }

/* ---- 用語検索 ---- */
.searchbox { margin-top: 12px; }
.sb-label { font-size: .78rem; color: var(--ink-soft); }
.kw-wrap { display: flex; flex-wrap: wrap; gap: 7px; margin-top: 7px; }
a.kw {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: .82rem; padding: 5px 11px;
  border: 1px solid var(--line); border-radius: 99px;
  background: #fff; color: var(--brand-dark); text-decoration: none;
  max-width: 100%;
}
a.kw:hover { background: var(--brand-soft); border-color: var(--brand); }
a.kw .kw-ic { font-size: .8rem; flex: none; }
a.kw .kw-tx { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
a.kw.kw-block {
  justify-content: center; width: 100%;
  padding: 10px 14px; font-weight: 700; font-size: .9rem;
  border-color: var(--brand); background: var(--brand-soft);
}
a.kw.kw-block:hover { background: var(--brand); color: #fff; }

/* ---- 結果画面 ---- */
.score-big { text-align: center; padding: 8px 0 4px; }
.score-big .pct { font-family: var(--serif); font-size: 3rem; font-weight: 800; color: var(--brand-dark); line-height: 1; }
.score-big .frac { color: var(--ink-soft); margin-top: 6px; }
.review-item {
  display: flex; gap: 10px; align-items: baseline;
  padding: 8px 4px; border-bottom: 1px solid var(--line); font-size: .88rem;
}
.review-item .ri-mark { font-weight: 700; flex: none; }
.review-item .ri-mark.ok { color: var(--ok); }
.review-item .ri-mark.ng { color: var(--ng); }
.review-item .ri-link { color: var(--brand-dark); cursor: pointer; text-decoration: underline; }

/* ---- 画像ズーム ---- */
.lightbox {
  position: fixed; inset: 0; background: rgba(0,0,0,.82);
  display: flex; align-items: center; justify-content: center; z-index: 100; padding: 16px;
  cursor: zoom-out;
}
.lightbox img { max-width: 100%; max-height: 100%; border-radius: 6px; background:#fff; }

.empty { text-align: center; color: var(--ink-soft); padding: 24px; }

/* ---- 下部固定アクションバー（スマホ向け） ---- */
.action-bar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  background: rgba(252, 248, 238, .96);
  -webkit-backdrop-filter: blur(6px); backdrop-filter: blur(6px);
  border-top: 1px solid var(--line);
  box-shadow: 0 -2px 12px rgba(0, 0, 0, .07);
  padding: 10px 16px calc(10px + env(safe-area-inset-bottom, 0px));
}
.action-bar-inner { max-width: 760px; margin: 0 auto; }
.action-bar .btn { box-shadow: 0 2px 8px rgba(31, 110, 95, .3); }
/* 固定バーに隠れないよう本文に余白 */
body.has-actionbar #app { padding-bottom: 96px; }
body.has-actionbar { padding-bottom: env(safe-area-inset-bottom, 0px); }

@media (max-width: 560px) {
  #app { padding: 14px 12px 8px; }
  .card { padding: 14px; margin-bottom: 12px; }
  .stat .num { font-size: 1.3rem; }
  .stat { padding: 10px 6px; }
  .stem { font-size: 1rem; }
  .menu-grid { gap: 9px; }
  .mode-btn { padding: 11px 12px; gap: 2px; }
  .mode-btn .mb-title { font-size: .9rem; }
  .mode-btn .mb-desc { font-size: .7rem; }
  h2.section-title { margin-bottom: 12px; }
  .app-header { padding: 14px 16px; }
  .app-header h1 { font-size: 1.05rem; }
}
