/* 色の部品（13番 ⑧-4）。画面の各所はこの名前で色を指定し、直接の色は書かない。
   テーマは部品の値の組を差し替えるだけ：ノーマル（初期）／ダーク／（後で）ファンシー。
   html の data-theme："auto"＝スマホの設定に合わせる、"normal"、"dark" */
:root {
  --bg: #f6f5f2;
  --surface: #ffffff;
  --surface-sub: #efede8;
  --text: #22201c;
  --text-sub: #6b665d;
  --border: #dcd8cf;
  --primary: #2f6fd6;        /* ＋ボタン・主な操作 */
  --primary-text: #ffffff;
  --win: #1f9d55;            /* 獲得 */
  --win-text: #ffffff;
  --giveup: #8a8478;         /* あきらめ */
  --giveup-text: #ffffff;
  --mark: #2f6fd6;           /* マスタ由来の候補の目印（青い星） */
  --focus: #f0a020;
  --alert: #c8402f;          /* 注意（記録し忘れ） */
  color-scheme: light;
}

/* ダーク：明示的に選んだ時 */
:root[data-theme="dark"] {
  --bg: #15171b;
  --surface: #1f2228;
  --surface-sub: #2a2e35;
  --text: #ecebe8;
  --text-sub: #a6a29a;
  --border: #3a3f47;
  --primary: #5b93ee;
  --primary-text: #0d1117;
  --win: #3cc47a;
  --win-text: #0d1117;
  --giveup: #8e8a82;
  --giveup-text: #0d1117;
  --mark: #7aa8f5;
  --focus: #f0b040;
  --alert: #f07a68;
  color-scheme: dark;
}

/* ダーク：「スマホの設定に合わせる」でスマホがダークの時（上と同じ値） */
@media (prefers-color-scheme: dark) {
  :root[data-theme="auto"] {
    --bg: #15171b;
    --surface: #1f2228;
    --surface-sub: #2a2e35;
    --text: #ecebe8;
    --text-sub: #a6a29a;
    --border: #3a3f47;
    --primary: #5b93ee;
    --primary-text: #0d1117;
    --win: #3cc47a;
    --win-text: #0d1117;
    --giveup: #8e8a82;
    --giveup-text: #0d1117;
    --mark: #7aa8f5;
    --focus: #f0b040;
  --alert: #f07a68;
    color-scheme: dark;
  }
}

/* ファンシーは後で :root[data-theme="fancy"] { ... } を足す */

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  touch-action: manipulation; /* ダブルタップでの拡大を止める（指2本の拡大はそのまま） */
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Hiragino Sans", "Noto Sans JP", sans-serif;
  font-size: 16px;
  line-height: 1.5;
  /* iPhone の上下の切り欠き・ホームバーを避ける */
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
  margin-bottom: 16px;
}
.card h2 { margin: 0 0 8px; font-size: 16px; }
.card p { margin: 4px 0; }
.sub { color: var(--text-sub); font-size: 14px; }

/* 親指で押せる大きさ（高さ48px以上） */
.btn {
  min-height: 48px;
  padding: 0 16px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-sub);
  color: var(--text);
  font-size: 16px;
  font-weight: 600;
}
.btn-primary { background: var(--primary); color: var(--primary-text); border-color: transparent; }
.btn-win { background: var(--win); color: var(--win-text); border-color: transparent; }
.btn-giveup { background: var(--giveup); color: var(--giveup-text); border-color: transparent; }
.btn:focus-visible, select:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

.row { display: flex; gap: 8px; flex-wrap: wrap; }
.row > .btn { flex: 1 1 0; }

select {
  min-height: 48px;
  width: 100%;
  font-size: 16px;
  padding: 0 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}

.mark { color: var(--mark); }

/* ---------- お店の画面（段階2、13番 ⑧-2） ---------- */

main { padding: 12px 16px 120px; max-width: 560px; margin: 0 auto; }

/* 来店の帯：上に固定 */
.visit-bar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0 8px;
  padding: 4px 12px 8px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
/* 1段目：お店の名前と設定、2段目：両替（iPhone の幅でも店名が切れないように） */
.visit-bar .store-btn { order: 1; flex: 1 1 calc(100% - 104px); }
.visit-bar .icon-btn { order: 2; }
.visit-bar .exchange { order: 3; flex: 1 1 100%; justify-content: flex-end; }
/* 投入の合計と、手元の残り見込み（S-4） */
.visit-bar .expected { order: 4; flex: 1 1 100%; margin: 0; text-align: right; }
.expected strong { color: var(--text); }
.store-btn {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 44px;
  text-align: left;
  border: none;
  background: none;
  color: var(--text);
  font-size: 16px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding: 0 4px;
}
.exchange { display: flex; align-items: center; gap: 6px; white-space: nowrap; }
.icon-btn {
  min-width: 44px;
  min-height: 44px;
  border: none;
  background: none;
  color: var(--text-sub);
  font-size: 20px;
}
.btn-small { min-height: 40px; padding: 0 10px; font-size: 14px; }
.net-status {
  margin: 0;
  padding: 6px 16px;
  font-size: 13px;
  background: var(--surface-sub);
  color: var(--text-sub);
}

.hint { color: var(--text-sub); text-align: center; margin: 24px 8px; }

/* 開いている挑戦の行 */
.attempt {
  display: block;
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  margin-bottom: 10px;
  color: var(--text);
}
.attempt.active { padding: 14px; border-width: 2px; border-color: var(--primary); }
.attempt.active .row { margin-top: 10px; }
/* ＋を連打していて指がずれても確定しないよう、獲得・あきらめの段は間を空ける（S-12） */
.attempt.active .row-decide { margin-top: 22px; }
.taps { text-align: center; margin: 0; }
.attempt-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.name-btn {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 44px;
  text-align: left;
  border: none;
  background: none;
  color: var(--text);
  font-size: 17px;
  font-weight: 700;
  padding: 0;
  overflow-wrap: anywhere;
  display: flex;
  align-items: flex-start;
  gap: 4px;
}
/* 長い商品名は2行で切る（行を切り替えてもボタンの位置が大きくずれないように） */
.name-text {
  min-width: 0;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
}
.name-btn .edit-mark { flex: none; }
.name-btn.empty { color: var(--text-sub); font-weight: 600; }
.chip {
  min-height: 36px;
  padding: 0 12px;
  border-radius: 18px;
  border: 1px solid var(--border);
  background: var(--surface-sub);
  color: var(--text);
  font-size: 14px;
  white-space: nowrap;
}
.step-chip { display: block; margin: 10px auto 0; color: var(--text-sub); }
/* 10円刻みの時は目立たせる（S-13） */
.step-chip.step-small { color: var(--text); border-color: var(--focus); background: var(--surface); font-weight: 700; }
.amount { text-align: center; font-size: 44px; font-weight: 800; margin: 6px 0 0; font-variant-numeric: tabular-nums; }
.btn-big { min-height: 72px; font-size: 26px; }
.row > .btn-big:first-child { flex: 0 0 30%; }

.attempt.collapsed {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 52px;
  padding: 0 14px;
  text-align: left;
  font-size: 16px;
}
.collapsed-name { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.btn-new {
  display: block;
  width: 100%;
  min-height: 56px;
  margin: 4px 0 20px;
  border-style: dashed;
  background: var(--surface);
}

/* 今日確定した行（小さく） */
.section-title { font-size: 14px; color: var(--text-sub); margin: 0 0 6px; }
.closed-list { list-style: none; margin: 0; padding: 0; }
.closed-row {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  min-height: 44px;
  padding: 0 10px;
  border: none;
  border-bottom: 1px solid var(--border);
  background: none;
  color: var(--text);
  font-size: 15px;
  text-align: left;
}
.badge { font-size: 12px; font-weight: 700; padding: 2px 8px; border-radius: 10px; white-space: nowrap; }
.badge-win { background: var(--win); color: var(--win-text); }
.badge-giveup { background: var(--giveup); color: var(--giveup-text); }

/* 取り消しの表示 */
.toast {
  position: fixed;
  left: 12px;
  right: 12px;
  bottom: calc(16px + env(safe-area-inset-bottom));
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 10px;
  max-width: 536px;
  margin: 0 auto;
  padding: 10px 10px 10px 16px;
  border-radius: 12px;
  background: var(--text);
  color: var(--bg);
  font-size: 15px;
}
.toast span { flex: 1 1 auto; }
.toast-btn {
  min-height: 44px;
  padding: 0 14px;
  border: 2px solid var(--bg);
  border-radius: 10px;
  background: none;
  color: var(--bg);
  font-size: 15px;
  font-weight: 700;
}

/* 下から出る選択画面
   高さは見えている画面の75%まで（上の暗い所を押して閉じやすく）。タイトルと ✕ の段は固定し、中身だけスクロール */
.sheet {
  width: 100%;
  max-width: 560px;
  max-height: 75vh;
  max-height: 75dvh;
  margin: auto auto 0;
  padding: 12px 16px calc(20px + env(safe-area-inset-bottom));
  border: none;
  border-radius: 16px 16px 0 0;
  background: var(--surface);
  color: var(--text);
  overflow: hidden;
}
/* 中身の高さ＝シートの上限から、上下の余白とタイトルの段（44px）を引いた分。
   flex で伸び縮みさせる書き方は iPhone の Safari で中身が高さ0に潰れたため使わない（11/2 実機） */
#sheet-body {
  max-height: calc(75vh - 76px - env(safe-area-inset-bottom));
  max-height: calc(75dvh - 76px - env(safe-area-inset-bottom));
  overflow-y: auto;
  overscroll-behavior: contain;
}
.sheet::backdrop { background: rgba(0, 0, 0, 0.45); }
/* 開いた時に ✕ へ自動で当たる枠は、指で使う時は出さない（キーボードで移動した時は出す） */
.sheet:focus, #sheet-close:focus:not(:focus-visible) { outline: none; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; min-height: 44px; }
.sheet-head h2 { margin: 0; font-size: 18px; }
.sheet-sub { font-size: 15px; margin: 16px 0 6px; }
.choice-list { display: flex; flex-direction: column; gap: 6px; margin: 8px 0; }
.choice {
  min-height: 48px;
  padding: 0 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-sub);
  color: var(--text);
  font-size: 16px;
  text-align: left;
}
.choice.selected { border: 2px solid var(--primary); }
.choice-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 8px 0 12px; }
.input-row { display: flex; gap: 8px; margin: 6px 0; }
.text-input {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 48px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font-size: 16px; /* 16px 未満だと iPhone が勝手に拡大する */
}
.btn-link { display: block; margin-top: 12px; border: none; background: none; color: var(--primary); min-height: 44px; font-size: 15px; }

/* ---------- まとめ・これまでの来店（段階3） ---------- */

.btn-finish { display: block; width: 100%; min-height: 52px; margin-top: 24px; }
.badge-open { background: var(--surface-sub); color: var(--text-sub); border: 1px solid var(--border); }

.view-bar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 8px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.view-bar h1 { margin: 0; font-size: 18px; }

.summary-store { font-size: 18px; font-weight: 700; margin: 4px 0 12px; }
.kv-list { list-style: none; margin: 0 0 8px; padding: 0; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; }
.kv { display: flex; justify-content: space-between; gap: 12px; padding: 10px 14px; border-bottom: 1px solid var(--border); }
.kv:last-child { border-bottom: none; }
.kv-alert, .kv-alert strong { color: var(--alert); }
#summary-body .section-title { margin-top: 20px; }

.decide-row { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; padding: 6px 0; border-bottom: 1px solid var(--border); }
/* 狭い画面でボタンが縦書きにならないよう、名前を1段目、金額とボタンを2段目に */
.decide-row .collapsed-name { flex: 1 1 100%; }
.decide-row strong { margin-right: auto; white-space: nowrap; }
.decide-row .btn { white-space: nowrap; }

.visit-row {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 2px;
  width: 100%;
  min-height: 56px;
  padding: 0 10px;
  border: none;
  border-bottom: 1px solid var(--border);
  background: none;
  color: var(--text);
  font-size: 15px;
  text-align: left;
}
.visit-date { flex: 0 0 auto; font-weight: 700; }
.visit-line { display: flex; align-items: center; gap: 8px; width: 100%; }
.visit-total { margin-left: auto; white-space: nowrap; }
.closed-row strong, .attempt.collapsed strong { white-space: nowrap; }
/* 遊んだ人の名前は折り返さない（商品名の方を省略する） */
.attempt.collapsed > .sub, .closed-row > .sub:not(.collapsed-name) { flex: none; white-space: nowrap; }
.edit-mark { flex: none; color: var(--text-sub); }

/* ---------- はしご・残金の入力（13番 ⑨） ---------- */

.choice-2line { display: flex; flex-direction: column; align-items: flex-start; justify-content: center; gap: 2px; padding: 8px 14px; }
.remaining { margin-top: 8px; }
.amount-row { display: flex; align-items: center; gap: 8px; }
.amount-input { font-size: 28px; font-weight: 800; text-align: right; font-variant-numeric: tabular-nums; }
.yen { font-size: 20px; font-weight: 700; }
.remaining .row { margin-top: 10px; }
.btn-wide { display: block; width: 100%; margin-top: 10px; }

/* 確定した行：名前と ✎ をひとまとまりに（✎ が遊んだ人の前に離れて並ばないように） */
.name-wrap { flex: 1 1 auto; min-width: 0; display: flex; align-items: center; gap: 4px; }
.name-wrap .collapsed-name { flex: 0 1 auto; }
