:root {
  --bg: #f7f7f8;
  --surface: #ffffff;
  --border: #dcdce0;
  --text: #1a1a1c;
  --muted: #6b6b73;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --ok: #157347;
  --warn: #a15c00;
  --error: #b42318;
  --accent-weak: #e8effe;   /* 選択済みの行を塗る色 */
  --radius: 12px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-contrast="high"]) {
    --bg: #131316;
    --surface: #1d1d21;
    --border: #34343b;
    --text: #ececf0;
    --muted: #9c9ca6;
    --accent: #5b8cff;
    --accent-text: #10131a;
    --ok: #4ade80;
    --warn: #fbbf24;
    --error: #ff8177;
    --accent-weak: #1e2a44;
  }
}

/* 屋外モード。
   DHS/MICS のタブレット調査でも直射日光下の可読性が課題として報告されている。
   屋外では実効コントラスト比が 2:1 まで落ちることがあり、その状態では
   グレーの階調はほぼ消える。純黒×純白まで振り切って、細い罫線もやめる。
   ダークではなくライトにするのは、照り返しの下では白地に黒の方が読めるため
   （暗い背景は反射光に沈む）。輝度を上げるぶん電池は食うので、常時ではなく
   現場が必要なときだけ切り替える前提。 */
:root[data-contrast="high"] {
  --bg: #ffffff;
  --surface: #ffffff;
  --border: #000000;
  --text: #000000;
  --muted: #000000;
  --accent: #0000cc;
  --accent-text: #ffffff;
  --accent-weak: #d6d6ff;
  --ok: #006400;
  --warn: #8a4b00;
  --error: #c00000;
}
:root[data-contrast="high"] .choice,
:root[data-contrast="high"] input,
:root[data-contrast="high"] select,
:root[data-contrast="high"] textarea,
:root[data-contrast="high"] .scale-btn { border-width: 2px; }
:root[data-contrast="high"] .hint,
:root[data-contrast="high"] .field-help { color: #000; }

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Noto Sans JP", sans-serif;
  font-size: 17px;               /* iOS で入力時に自動ズームしない下限 */
  line-height: 1.6;
  -webkit-text-size-adjust: 100%;
  padding-bottom: env(safe-area-inset-bottom);
}

/* ---- 上部ステータスバー ---- */
.bar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: calc(8px + env(safe-area-inset-top)) 16px 8px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.bar-left { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.bar-right { display: flex; gap: 8px; align-items: center; }

.lang-select {
  font: inherit;
  font-size: 15px;
  min-height: 40px;
  padding: 0 8px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
}

.pill {
  font-size: 13px;
  font-weight: 600;
  padding: 3px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
  white-space: nowrap;
}
.pill[data-state="online"]  { color: var(--ok);   border-color: currentColor; }
.pill[data-state="offline"] { color: var(--warn); border-color: currentColor; }
.pill[data-state="syncing"] { color: var(--accent); border-color: currentColor; }
.pill-quiet { color: var(--muted); }

main { max-width: 720px; margin: 0 auto; padding: 20px 16px 64px; }

h1 { font-size: 22px; margin: 0 0 6px; line-height: 1.35; }
.hint { color: var(--muted); font-size: 14px; margin: 0 0 20px; }

/* ---- フォーム一覧 ---- */
.cards { list-style: none; margin: 0; padding: 0; display: grid; gap: 12px; }

.card {
  width: 100%;
  text-align: left;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  font: inherit;
  color: inherit;
  cursor: pointer;
}
.card:active { transform: scale(0.995); }
.card-title { font-weight: 700; display: block; margin-bottom: 2px; }
.card-desc { color: var(--muted); font-size: 14px; }

/* ---- ページ進行 ---- */
.progress {
  height: 6px;
  background: var(--border);
  border-radius: 999px;
  overflow: hidden;
  margin: 4px 0 6px;
}
.progress-bar {
  height: 100%;
  background: var(--accent);
  border-radius: 999px;
  transition: width 0.25s ease;
}
.page-count { margin: 0 0 20px; }

.page-title {
  font-size: 18px;
  margin: 0 0 20px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
}
.section-heading {
  font-size: 17px;
  margin: 34px 0 18px;
  padding-top: 18px;
  border-top: 1px solid var(--border);
}
.section-heading:first-child { margin-top: 0; padding-top: 0; border-top: 0; }
.field-note { color: var(--muted); font-size: 14px; margin: -8px 0 22px; }

/* 操作の75%は親指、49%が片手操作。主要ボタンは画面下部に置く。
   97問のフォームで毎回ページ末尾までスクロールさせるのは負担が大きいので
   下部に貼り付ける。fixed はiOSのキーボード表示で壊れやすいので sticky。 */
.nav {
  position: sticky;
  bottom: 0;
  z-index: 5;
  display: flex;
  gap: 10px;
  align-items: center;
  margin: 24px -16px 0;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
}
/* 画面下端はタップ精度が最も落ちる。上端42px/下端46pxが目安なので余裕を取る */
.nav .btn-primary { flex: 1; margin-top: 0; min-height: 56px; }
.nav .btn-ghost { min-height: 56px; }
.btn-submit { background: var(--ok); }

/* ---- 設問 ---- */
.field { margin-bottom: 26px; }
.field[hidden] { display: none; }

.field > label.q,
.field > legend.q {
  display: block;
  font-size: 18px;      /* 本文17pxより一段大きく。設問文が読みの起点になるため */
  font-weight: 600;
  line-height: 1.5;
  margin-bottom: 10px;
  padding: 0;
}
.req { color: var(--error); font-weight: 700; margin-left: 4px; }
.field-help { color: var(--muted); font-size: 14px; margin: -4px 0 8px; }

fieldset { border: 0; margin: 0; padding: 0; }

input[type="text"], input[type="email"], input[type="tel"],
input[type="number"], input[type="date"], textarea, select {
  width: 100%;
  padding: 12px;
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  -webkit-appearance: none;
  appearance: none;
}
select {
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%),
                    linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 20px) 50%, calc(100% - 14px) 50%;
  background-size: 6px 6px, 6px 6px;
  background-repeat: no-repeat;
  padding-right: 40px;
}
textarea { min-height: 110px; resize: vertical; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* 選択肢。
   Antoun (2017, US Census, n=61/1,161画面) の無作為割付実験より:
   アイコンを 2mm→6mm にすると誤タップが 29.7%→8.0% に下がる。
   一方でアイコンを消して「ボタンだけ」にするのは逆効果（誤タップ12.2%、
   好んだ人は5.1%のみ）。だから丸/四角は残したまま大きくする。
   行全体を押せるようにしても誤タップはさらに下がらないが、害もなく、
   選択時に色が変わる点は好まれた — なので色は変える。 */
.choice {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 56px;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  margin-bottom: 10px;
  cursor: pointer;
  /* 選択の色変化のみ。transform/opacity 以外は動かさない */
  transition: background-color 0.12s ease, border-color 0.12s ease;
}
.choice input {
  width: 26px;          /* ≒6.5mm。実験で効果が確認された 6mm を上回る */
  height: 26px;
  margin: 0;
  flex: none;
  accent-color: var(--accent);
}
.choice:has(input:checked) {
  border-color: var(--accent);
  background: var(--accent-weak);
}
.choice:has(input:checked) span { font-weight: 600; }

/* 評価・スケール */
.scale { display: flex; flex-wrap: wrap; gap: 8px; }
.scale-btn {
  min-width: 48px;
  min-height: 48px;
  padding: 0 12px;
  font: inherit;
  font-weight: 600;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  cursor: pointer;
}
.scale-btn[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}
.scale-ends { display: flex; justify-content: space-between; color: var(--muted); font-size: 13px; margin-top: 6px; }

.field-error { color: var(--error); font-size: 14px; margin-top: 6px; }
.field.invalid input, .field.invalid textarea, .field.invalid select { border-color: var(--error); }
.field.invalid .q { color: var(--error); }

/* 単位付き入力（Pesos / Hectare / % など） */
.input-row { display: flex; align-items: center; gap: 10px; }
.input-row input { flex: 1; min-width: 0; }
.suffix { font-weight: 600; color: var(--muted); white-space: nowrap; }

/* 自動計算欄は触れないことが見て分かるようにする */
input.computed {
  background: var(--bg);
  color: var(--muted);
  font-weight: 700;
}

/* 「その他」を選んだときだけ出る自由記述 */
.other-input { margin-top: 8px; }
.other-input[hidden] { display: none; }

/* ---- 署名 ---- */
.sig-box {
  border: 1px dashed var(--border);
  border-radius: 10px;
  background: #fff;             /* 署名は黒線で描くので下地は常に白 */
  overflow: hidden;
}
.sig-canvas { display: block; width: 100%; height: 180px; touch-action: none; cursor: crosshair; }

/* ---- 繰り返し入力（家族構成など） ---- */
.repeat-item {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  padding: 14px 14px 2px;
  margin-bottom: 12px;
}
.repeat-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 14px;
}
.btn-small { min-height: 32px; font-size: 14px; padding: 0 10px; }
.field-sub { margin-bottom: 16px; }
.field-sub .q { font-weight: 500; font-size: 15px; }

/* ---- ボタン ---- */
.btn-primary {
  width: 100%;
  min-height: 52px;
  margin-top: 8px;
  font: inherit;
  font-weight: 700;
  color: var(--accent-text);
  background: var(--accent);
  border: 0;
  border-radius: var(--radius);
  cursor: pointer;
}
.btn-ghost {
  min-height: 40px;
  padding: 0 14px;
  white-space: nowrap;   /* 「同期」「I-sync」が2行に折れないように */
  font: inherit;
  font-size: 15px;
  color: var(--text);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 10px;
  cursor: pointer;
}
.btn-back {
  font: inherit;
  color: var(--muted);
  background: none;
  border: 0;
  padding: 0 0 12px;
  cursor: pointer;
}

/* ---- 通知類 ---- */
.notice {
  border: 1px solid var(--border);
  border-left: 4px solid var(--muted);
  border-radius: 8px;
  padding: 14px 16px;
  margin-top: 24px;
  background: var(--surface);
}
.notice-error { border-left-color: var(--error); }
.notice p { font-size: 14px; color: var(--muted); }
.notice button { margin-right: 8px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 20;
  max-width: calc(100% - 32px);
  padding: 12px 18px;
  border-radius: 999px;
  background: var(--text);
  color: var(--bg);
  font-size: 15px;
  font-weight: 600;
  box-shadow: 0 6px 24px rgb(0 0 0 / 0.25);
}

.done { text-align: center; padding: 40px 0; }
.done-mark {
  width: 64px; height: 64px;
  margin: 0 auto 16px;
  display: grid; place-items: center;
  font-size: 32px;
  color: var(--accent-text);
  background: var(--ok);
  border-radius: 50%;
}
.done .btn-ghost { margin-top: 12px; }

.foot {
  max-width: 720px;
  margin: 0 auto;
  padding: 0 16px 32px;
  display: flex;
  justify-content: space-between;
  font-size: 13px;
  color: var(--muted);
}
.foot a { color: var(--muted); }

/* ---- 管理画面 ---- */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { border-collapse: collapse; width: 100%; font-size: 14px; }
th, td { text-align: left; padding: 8px 12px; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { color: var(--muted); font-weight: 600; }
td.wrap { white-space: normal; min-width: 220px; }

.agg { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; margin-bottom: 12px; }
.agg h3 { margin: 0 0 10px; font-size: 16px; }
.agg-row { display: grid; grid-template-columns: minmax(80px, 1fr) 3fr auto; gap: 10px; align-items: center; margin-bottom: 6px; font-size: 14px; }
.agg-bar { background: var(--accent); height: 12px; border-radius: 6px; min-width: 2px; }
.agg-track { background: var(--border); height: 12px; border-radius: 6px; }
.agg-label { overflow: hidden; text-overflow: ellipsis; }
.stat { font-size: 28px; font-weight: 700; }

.toolbar { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; margin-bottom: 18px; }
.toolbar-item { display: flex; gap: 8px; align-items: center; font-size: 14px; color: var(--muted); }

.tabs { display: flex; gap: 6px; border-bottom: 1px solid var(--border); margin-bottom: 20px; }
.tab {
  font: inherit;
  font-size: 15px;
  min-height: 42px;
  padding: 0 14px;
  color: var(--muted);
  background: none;
  border: 0;
  border-bottom: 2px solid transparent;
  cursor: pointer;
}
.tab-active { color: var(--text); font-weight: 600; border-bottom-color: var(--accent); }

/* ---- 長い選択肢リストの絞り込み ----
   約300項目のリストでは、利用者は数回スクロールして諦め検索に切り替える、という
   観察がある。農機具44択のように長いものには検索欄を出す。 */
.opt-filter { position: relative; margin-bottom: 10px; }
.opt-filter input { padding-right: 44px; }
.opt-filter-clear {
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  width: 34px;
  height: 34px;
  font: inherit;
  color: var(--muted);
  background: none;
  border: 0;
  cursor: pointer;
}
.opt-count { color: var(--muted); font-size: 14px; margin: -2px 0 10px; }
.choice[hidden] { display: none; }

/* ---- 画面遷移 ----
   RAM 2GB以下の端末が4割という前提なので、compositor だけで動く
   transform と opacity しか使わない。layout も paint も起こさない。
   時間も 150ms に抑える（長いと低速端末で「引っかかった」ように見える）。 */
@keyframes page-in {
  from { opacity: 0; transform: translate3d(0, 8px, 0); }
  to   { opacity: 1; transform: translate3d(0, 0, 0); }
}
.page-enter { animation: page-in 0.15s ease-out both; }

/* トーストも同様に transform/opacity のみ */
@keyframes toast-in {
  from { opacity: 0; transform: translate3d(-50%, 12px, 0); }
  to   { opacity: 1; transform: translate3d(-50%, 0, 0); }
}
.toast { animation: toast-in 0.15s ease-out both; }

/* 動きを減らす設定の端末では完全に切る。
   低速端末での体感改善にもなる。 */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* 屋外モードのトグル。文字が長い言語でもバーが崩れないよう幅を抑える */
.btn-icon { padding: 0 10px; font-size: 16px; }
.btn-icon[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}

/* ================================================================ 分析タブ
   配色は検証済みパレットのスロット1・2。両モードで CVD・通常視・
   コントラストの全項目を通過している（CVD ΔE 24.7 / 通常視 33.6）。
   基準線は系列色を使わず地の文字色で引く — 色は系列のために取っておく。 */
:root {
  --an-income:  #2a78d6;
  --an-expense: #eb6834;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-contrast="high"]) { --an-income: #3987e5; --an-expense: #d95926; }
}

.an-section { margin-bottom: 44px; }
.an-h { font-size: 19px; font-weight: 600; margin: 0 0 4px; }
.an-sub { color: var(--muted); font-size: 14px; margin: 0 0 18px; }

/* 見出しの数字。2026年の潮流に従い、チャートより先に洞察を出す */
.an-hero {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 26px 28px;
  margin-bottom: 20px;
}
.an-hero-num {
  font-size: clamp(40px, 8vw, 56px);
  font-weight: 700;
  line-height: 1;
  margin: 0 0 8px;
  font-variant-numeric: tabular-nums;
}
.an-hero-sub { margin: 0; color: var(--muted); font-size: 15px; }
.an-hero-sub strong { color: var(--text); }

.an-note {
  border-left: 3px solid var(--an-expense);
  background: var(--surface);
  border-radius: 0 6px 6px 0;
  padding: 16px 20px;
  margin-bottom: 40px;
  font-size: 14.5px;
}
.an-note.calm { border-left-color: var(--muted); margin: 18px 0 0; }
.an-note p { margin: 6px 0 0; color: var(--muted); }

/* ---- 分布帯（1世帯1点） ---- */
.an-swarm {
  position: relative;
  height: 150px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}
.an-dot {
  position: absolute;
  width: 7px; height: 7px;
  margin: -3.5px 0 0 -3.5px;
  border-radius: 50%;
  background: var(--an-income);
  /* 重なっても数えられるよう、地の色で細く縁取る */
  box-shadow: 0 0 0 1.5px var(--surface);
}
.an-swarm-line { position: absolute; top: 0; bottom: 0; width: 1px; background: var(--muted); opacity: 0.55; }
.an-swarm-line-label {
  position: absolute; top: 6px; left: 5px;
  font-size: 11px; color: var(--muted); white-space: nowrap;
}

/* ---- 比較の帯 ---- */
.an-legend { display: flex; gap: 18px; flex-wrap: wrap; margin-bottom: 20px; font-size: 13.5px; color: var(--muted); }
.an-legend span { display: inline-flex; align-items: center; gap: 7px; }
.an-key { width: 12px; height: 12px; border-radius: 3px; flex: none; display: inline-block; }
.an-key-income  { background: var(--an-income); }
.an-key-expense { background: var(--an-expense); }
.an-key-bench   { width: 2px; height: 14px; border-radius: 0; background: var(--muted); }

.an-row { margin-bottom: 34px; }
.an-row-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; margin-bottom: 8px; }
.an-row-head h4 { font-size: 15px; font-weight: 600; margin: 0; }
.an-row-note { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }

.an-track {
  position: relative;
  height: 28px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 5px;
}
/* Wilson 区間は帯の背後に薄く */
.an-ci { position: absolute; top: 0; bottom: 0; opacity: 0.22; }
.an-ci-income  { background: var(--an-income); }
.an-ci-expense { background: var(--an-expense); }

.an-band {
  position: absolute; top: 5px; bottom: 5px;
  background: linear-gradient(90deg, var(--an-expense), var(--an-income));
  border-radius: 3px;
}
.an-cap { position: absolute; top: 2px; bottom: 2px; width: 3px; border-radius: 2px; }
.an-cap-income  { background: var(--an-income); }
.an-cap-expense { background: var(--an-expense); }

/* 基準は系列と重ならないよう地の色のリングを持たせる */
.an-bench {
  position: absolute; top: -5px; bottom: -5px; width: 2px;
  background: var(--text);
  box-shadow: 0 0 0 2px var(--bg);
}
.an-bench-label {
  position: absolute; top: 100%; margin-top: 5px;
  transform: translateX(-50%);
  font-size: 11.5px; color: var(--muted); white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

/* 実数は必ず出す。小標本では割合だけだと誤解を招く */
.an-vals { display: flex; gap: 20px; flex-wrap: wrap; margin-top: 26px; font-size: 13.5px; }
.an-chip { display: inline-flex; align-items: center; gap: 6px; font-variant-numeric: tabular-nums; }
.an-chip-label { color: var(--muted); }
.an-chip-n { color: var(--muted); }
.an-chip-ci { color: var(--muted); font-size: 12px; }

.an-scale {
  display: flex; justify-content: space-between;
  font-size: 12px; color: var(--muted); margin-top: 8px;
  font-variant-numeric: tabular-nums;
}

/* ---- 数字タイル・発見 ---- */
.an-tiles {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 1px; background: var(--border);
  border: 1px solid var(--border); border-radius: 8px; overflow: hidden;
  margin-bottom: 24px;
}
.an-tile { background: var(--surface); padding: 16px 18px; }
.an-tile dt { font-size: 12px; color: var(--muted); margin: 0 0 4px; }
.an-tile dd { margin: 0; font-size: 25px; font-weight: 700; line-height: 1.15; font-variant-numeric: tabular-nums; }
.an-tile dd small { font-size: 13px; font-weight: 400; color: var(--muted); }

.an-finding { margin-bottom: 20px; }
.an-finding h4 { font-size: 15px; font-weight: 600; margin: 0 0 4px; }
.an-finding p { margin: 0; color: var(--muted); font-size: 14.5px; }

.an-sources { font-size: 14px; color: var(--muted); margin: 0; padding-left: 1.2em; }
.an-sources li { margin-bottom: 5px; }

@media (max-width: 560px) {
  .an-row-head { flex-direction: column; gap: 2px; }
  .an-vals { gap: 10px; flex-direction: column; }
}
