/* 色はトークンで持ち、ライト／ダークを切り替える（テーマ設定は後の段階で足す） */
:root {
  --bg: #f4f1ea;
  --surface: #ffffff;
  --text: #1c1c1c;
  --muted: #6b6b6b;
  --accent: #14532d;
  --accent-text: #ffffff;
  --border: #ddd6c8;
  --ok: #15803d;
  --ng: #b91c1c;
  --pos: #1d4ed8;   /* プラスの pt・収支 */
  --neg: #c2410c;   /* マイナスの pt・収支 */
  --chip-on: #dcfce7;
  --warn-bg: #fef3c7;
  --tabbar-h: 56px;
  /* グラフの色（プラス／マイナスの棒、累計の線）。色覚の違いでも見分けられることを検証済み */
  --chart-pos: #1d4ed8;
  --chart-neg: #c2410c;
  --chart-line: #3f3f3f;
  --chart-grid: #ebe6db;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1a14;
    --surface: #18261e;
    --text: #ece8de;
    --muted: #9aa59e;
    --accent: #2f8f57;
    --accent-text: #ffffff;
    --border: #2a3a31;
    --ok: #4ade80;
    --ng: #f87171;
    --pos: #7cb3ff;
    --neg: #fb9a6c;
    --chip-on: #1f4d33;
    --warn-bg: #4a3b12;
    --chart-pos: #4a8ae8;
    --chart-neg: #d9703d;
    --chart-line: #d6dcd8;
    --chart-grid: #243229;
  }
}

* {
  box-sizing: border-box;
  /* ダブルタップでの拡大を止める（タップの反応も速くなる）。2 本指での拡大とスクロールは残す */
  touch-action: manipulation;
}
[hidden] { display: none !important; } /* button などの display 指定に負けないように */

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Noto Sans JP", sans-serif;
  -webkit-text-size-adjust: 100%;
}

a { color: var(--accent); }

/* ---- 上の帯。全画面起動時はノッチに食い込まないよう safe-area 分の余白を取る ---- */
.appbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--accent);
  color: var(--accent-text);
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
}
.appbar h1 { margin: 0; font-size: 18px; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.appbar-back {
  background: transparent;
  color: var(--accent-text);
  padding: 4px 8px 4px 0;
  font-size: 16px;
}
.sync { font-size: 12px; opacity: 0.9; white-space: nowrap; }
/* 見た目は帯に溶け込ませる。文字は 16px（iOS が選ぶときに拡大しないように） */
.group-switch {
  width: auto;
  max-width: 40%;
  flex: none;
  padding: 3px 6px;
  background: rgba(255, 255, 255, 0.15);
  color: var(--accent-text);
  border-color: rgba(255, 255, 255, 0.45);
  border-radius: 6px;
}
.group-switch option { color: var(--text); background: var(--surface); }
.sync.ng { background: var(--ng); color: #fff; padding: 2px 6px; border-radius: 6px; opacity: 1; }

/* ---- 下のタブ ---- */
.tabbar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
  z-index: 10;
}
.tabbar a {
  flex: 1;
  height: var(--tabbar-h);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  text-decoration: none;
  font-size: 14px;
}
.tabbar a.active { color: var(--accent); font-weight: bold; }

main {
  padding: 12px 16px calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 24px);
  display: grid;
  gap: 12px;
}
main > div { display: grid; gap: 12px; min-width: 0; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
  min-width: 0;
}
.card h2 { margin: 0 0 10px; font-size: 15px; }

.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 12px; margin: 0 0 12px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; word-break: break-all; }

/* ---- 入力 ---- */
.field { display: grid; gap: 4px; margin-bottom: 10px; min-width: 0; }
.field span { color: var(--muted); font-size: 13px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }

/* iOS は 16px 未満の入力欄でフォーカス時に勝手に拡大するため 16px にする（入力欄の文字は 16px 未満にしない） */
input, select, textarea {
  font: inherit;
  font-size: 16px;
  padding: 9px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  width: 100%;
  min-width: 0;
}
input:disabled { opacity: 0.4; }
input.invalid { border-color: var(--ng); outline: 1px solid var(--ng); }

.inline-form { display: flex; gap: 8px; margin-top: 10px; }
.inline-form input { flex: 1; }

/* ---- ボタン ---- */
button, .button {
  font: inherit;
  font-size: 16px;
  padding: 10px 16px;
  border: 0;
  border-radius: 8px;
  background: var(--accent);
  color: var(--accent-text);
  text-decoration: none;
  text-align: center;
  display: inline-block;
  cursor: pointer;
  white-space: nowrap;
}
button:disabled { opacity: 0.5; }
.secondary { background: transparent; color: var(--accent); border: 1px solid var(--accent); }
.danger { background: transparent; color: var(--ng); border: 1px solid var(--ng); }
.small { font-size: 14px; padding: 6px 10px; }
.block { display: block; width: 100%; }
.button-row { display: flex; gap: 8px; flex-wrap: wrap; }
.button-row > * { flex: 1; }

/* ---- 一覧 ---- */
.list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.list-item {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px;
  color: var(--text);
  text-decoration: none;
}
.list-main { flex: 1; min-width: 0; }
.list-sub { color: var(--muted); font-size: 13px; margin-top: 4px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.empty { color: var(--muted); text-align: center; padding: 24px 0; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.ok { color: var(--ok); }
.ng { color: var(--ng); }
.pos { color: var(--pos); }
.neg { color: var(--neg); }
.version { color: var(--muted); font-size: 12px; text-align: center; }

.result { margin: 10px 0 0; word-break: break-all; }
.result.ok { color: var(--ok); }
.result.ng { color: var(--ng); }

/* ---- 参加者の選択 ---- */
.picker { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 15px;
}
.chip.on { background: var(--chip-on); border-color: var(--accent); }
.badge {
  display: inline-block;
  min-width: 1.4em;
  margin-right: 4px;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 12px;
  line-height: 1.4em;
}

/* ---- 入力表 ---- */
.session-head { display: grid; gap: 4px; }
.head-row { display: flex; align-items: baseline; gap: 12px; }
.head-row > span { flex: 1; }
.head-row > a { white-space: nowrap; }
.card > p:first-child { margin-top: 0; }
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; border-radius: 12px; border: 1px solid var(--border); }
table.score {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  font-variant-numeric: tabular-nums;
}
table.score th, table.score td {
  padding: 9px 6px;
  text-align: right;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
table.score thead th {
  text-align: center;
  font-size: 13px;
  max-width: 5.5em;
  overflow: hidden;
  text-overflow: ellipsis;
}
table.score th:first-child { text-align: center; color: var(--muted); font-weight: normal; width: 3.2em; font-size: 13px; }
/* 5 人以上で横にスクロールしても、行の見出し（回・計・収支）は左に残す */
table.score th:first-child { position: sticky; left: 0; background: var(--surface); z-index: 1; }
table.score tr.warn th:first-child { background: var(--warn-bg); }
table.score td { min-width: 3.8em; font-size: 16px; }
table.score td.top { font-weight: bold; }
table.score tr.tap { cursor: pointer; }
table.score tr.warn { background: var(--warn-bg); }
table.score tfoot tr.total td { font-weight: bold; border-top: 2px solid var(--border); }
table.score tfoot tr.balance td { font-weight: bold; font-size: 17px; }
table.score td.small { font-size: 12px; }

/* ---- 1 人 1 行の入力（半荘の pt、チップ、ゲーム代） ---- */
.entry-list { display: grid; gap: 8px; }
.entry { display: flex; align-items: center; gap: 8px; }
.entry.off .entry-name { color: var(--muted); text-decoration: line-through; }
.entry-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.entry input { width: 7.5em; flex: none; text-align: right; }
.sign { width: 2.6em; padding-left: 0; padding-right: 0; }
.total-line { margin: 10px 0 0; font-size: 14px; }
.total-line.ok { color: var(--ok); }
.total-line.ng { color: var(--ng); }

/* ---- 履歴：絞り込みと対局カード ---- */
.filter { display: grid; gap: 8px; }
.filter-row { display: flex; gap: 8px; }
.filter-row > select { flex: 1; }
.range { display: flex; align-items: center; gap: 6px; }
.card-head { display: flex; align-items: baseline; gap: 8px; }
.card-title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.balances { display: flex; flex-wrap: wrap; gap: 2px 14px; margin-top: 6px; font-size: 14px; font-variant-numeric: tabular-nums; }

/* ---- 集計表：先頭列は名前 ---- */
table.summary th:first-child { text-align: left; color: var(--text); font-weight: bold; font-size: 14px; width: auto; max-width: 6em; overflow: hidden; text-overflow: ellipsis; }
table.summary thead th { font-size: 12px; color: var(--muted); }
table.summary td { min-width: 3em; font-size: 15px; }

/* ---- 画像で共有 ---- */
.share-image { width: 100%; border: 1px solid var(--border); border-radius: 8px; background: #fff; min-height: 120px; }

/* ---- ルール設定（対局作成画面） ---- */
input[type=checkbox] { width: auto; margin: 0 6px 0 0; transform: scale(1.3); }
.check-row { display: flex; align-items: center; gap: 10px; min-height: 44px; }
.check { display: flex; align-items: center; flex: 1; font-size: 15px; }
.check-row select { width: auto; flex: none; }
.uma-custom { margin-bottom: 6px; }
.rule-line { margin: 6px 0 10px; font-size: 13px; color: var(--muted); }
.rule-line.ng { color: var(--ng); }

/* ---- 半荘の入力：pt／素点の切り替え、席、pt の見込み ---- */
.segmented { display: flex; border: 1px solid var(--accent); border-radius: 8px; overflow: hidden; }
.segmented button { flex: 1; border-radius: 0; background: transparent; color: var(--accent); }
.segmented button.on { background: var(--accent); color: var(--accent-text); }
.entry-wrap { display: grid; gap: 2px; }
.entry .seat { width: 2.6em; padding-left: 0; padding-right: 0; }
.entry .unit { color: var(--muted); margin-left: -6px; }
.entry input[inputmode=numeric] { width: 5.5em; }
.preview { font-size: 13px; text-align: right; padding-right: 2px; min-height: 0; }
.preview:empty { display: none; }

/* ---- ルール画面 ---- */
.grid2 { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.tag { font-size: 12px; color: var(--muted); border: 1px solid var(--border); border-radius: 999px; padding: 1px 8px; white-space: nowrap; }
.save-as { margin: -4px 0 10px; text-align: right; }

/* ---- ホーム（個人成績） ---- */
.home { display: grid; gap: 12px; }
.home .card { margin: 0; }
.hero { text-align: center; }
.hero-value { font-size: 34px; font-weight: bold; font-variant-numeric: tabular-nums; margin: 2px 0 4px; }
.stat-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 10px 8px; text-align: center; min-width: 0; }
.stat-label { font-size: 12px; color: var(--muted); }
.stat-value { font-size: 18px; font-weight: bold; font-variant-numeric: tabular-nums; margin-top: 2px; white-space: nowrap; }
.score.ranks td { text-align: center; }

/* ---- ランキング ---- */
.rank-item { display: flex; align-items: center; gap: 12px; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 10px 14px; }
.rank-item.me { border-color: var(--accent); }
.place { width: 2em; text-align: center; font-weight: bold; font-size: 18px; color: var(--muted); font-variant-numeric: tabular-nums; }
.place-1, .place-2, .place-3 { color: var(--accent); }
.rank-name { font-weight: bold; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rank-value { font-size: 18px; font-weight: bold; font-variant-numeric: tabular-nums; white-space: nowrap; }
.rank-item.below { opacity: 0.55; }
.below-section { display: grid; gap: 8px; }
.below-head { margin: 4px 0 0; font-size: 13px; font-weight: normal; color: var(--muted); }
.inline { display: flex; align-items: center; gap: 8px; }
.narrow { width: 6em; flex: none; }
.no-hanchan { opacity: 0.7; border-style: dashed; }

/* ---- グラフ ---- */
.chart-wrap { display: grid; gap: 6px; }
.chart { width: 100%; height: auto; display: block; overflow: visible; }
.chart-grid { stroke: var(--chart-grid); stroke-width: 1; }
.chart-zero { stroke: var(--border); stroke-width: 1; }
.chart-tick { fill: var(--muted); font-size: 10px; font-variant-numeric: tabular-nums; }
.chart-bar.pos { fill: var(--chart-pos); }
.chart-bar.neg { fill: var(--chart-neg); }
.chart-line { fill: none; stroke: var(--chart-line); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart-dot { fill: var(--chart-line); stroke: var(--surface); stroke-width: 2; }
.chart-cursor { stroke: var(--muted); stroke-width: 1; opacity: 0.5; }
.chart-hit { fill: transparent; cursor: pointer; }
.chart-readout { margin: 0; font-size: 14px; text-align: center; font-variant-numeric: tabular-nums; }
.chart-legend { display: flex; gap: 14px; font-size: 12px; color: var(--muted); }
.chart-legend span { display: inline-flex; align-items: center; gap: 4px; }
.key-bar { display: inline-block; width: 6px; height: 12px; border-radius: 2px 2px 0 0; }
.key-bar.pos { background: var(--chart-pos); }
.key-bar.neg { background: var(--chart-neg); margin-right: 2px; }
.key-line { display: inline-block; width: 16px; height: 2px; background: var(--chart-line); border-radius: 1px; }
.chart-table summary { font-size: 13px; color: var(--accent); cursor: pointer; }
.chart-table table { margin-top: 6px; width: 100%; }

/* ---- 収支カレンダー ---- */
.calendar { display: grid; gap: 8px; }
.cal-head { display: flex; align-items: center; gap: 8px; }
.cal-title { flex: 1; text-align: center; }
.cal-head button { min-width: 44px; font-size: 18px; }
.cal-grid { width: 100%; border-collapse: collapse; table-layout: fixed; }
.cal-grid th { font-size: 11px; font-weight: normal; color: var(--muted); padding: 2px 0; }
.cal-grid th.sun { color: var(--neg); }
.cal-grid th.sat { color: var(--pos); }
.cal-grid td { height: 44px; padding: 1px; vertical-align: top; text-align: center; }
.cal-day { display: block; font-size: 12px; color: var(--muted); }
.cal-cell { width: 100%; height: 100%; padding: 2px 0; border: 1px solid var(--border); border-radius: 6px; background: var(--bg); color: var(--text); display: grid; align-content: start; gap: 1px; }
.cal-played .cal-day { color: var(--text); font-weight: bold; }
.cal-value { font-size: 10px; font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; letter-spacing: -0.02em; }
.cal-detail { min-height: 1.5em; font-size: 14px; text-align: center; display: flex; flex-wrap: wrap; justify-content: center; gap: 4px 10px; }
.opp-item { border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; display: grid; gap: 6px; }
.opp-values { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px; text-align: center; }
.opp-value .num { font-size: 16px; font-weight: bold; font-variant-numeric: tabular-nums; white-space: nowrap; }
.opp-item .list-sub { white-space: normal; margin-top: 0; }

/* ---- 新しい版の帯（タブバーのすぐ上） ---- */
.update-banner {
  position: fixed; left: 12px; right: 12px; z-index: 20;
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 10px);
  display: flex; align-items: center; gap: 10px;
  background: var(--surface); color: var(--text);
  border: 2px solid var(--accent); border-radius: 12px; padding: 10px 12px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25); font-size: 14px;
}
.update-banner span { flex: 1; min-width: 0; }
.version-check { display: grid; justify-items: center; gap: 4px; }
.stack { display: grid; gap: 12px; }

/* 本家からの繰り越し：本家通算・入力済み・繰り越しの比べ表。0 になっていない繰り越しを太字にする */
.carry-table { border-collapse: collapse; width: 100%; font-variant-numeric: tabular-nums; }
.carry-table th, .carry-table td { padding: 8px 6px; border-bottom: 1px solid var(--border); text-align: right; white-space: nowrap; font-size: 14px; }
.carry-table thead th { font-size: 12px; color: var(--muted); font-weight: normal; }
.carry-table tbody th { text-align: left; color: var(--muted); font-weight: normal; }
.carry-table td.left { font-weight: bold; }
.carry-fields { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }
.carry-fields .narrow { width: 100%; flex: 1; }
.tag.ng { color: var(--ng); border-color: var(--ng); }
.list-sub.ng { color: var(--ng); }
