/* =========================================================
   Apple のシステムカラー（ダーク／ライト）
   ========================================================= */
:root {
  --bg:         #000000;
  --fill:       #1C1C1E;
  --fill-press: #3A3A3C;
  --sheet:      #1C1C1E;
  --group:      #2C2C2E;
  --label:      #FFFFFF;
  --label-2:    rgba(235, 235, 245, 0.6);
  --label-3:    rgba(235, 235, 245, 0.3);
  --label-2-hc: rgba(235, 235, 245, 0.8);
  --label-3-hc: rgba(235, 235, 245, 0.55);
  --separator:  rgba(84, 84, 88, 0.65);
  --red:        #FF453A;
  --green:      #30D158;
  --tint:       #0A84FF;
  --seg-bg:     rgba(118, 118, 128, 0.24);
  --seg-on:     #636366;
  --scrim:      rgba(0, 0, 0, 0.55);
  color-scheme: dark;

  --font: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Noto Sans JP", "Roboto", system-ui, sans-serif;
  --ease-sheet: cubic-bezier(.32, .72, 0, 1);
}

/* ライト：明示的に選んだとき */
:root[data-theme="light"] {
  --bg: #FFFFFF; --fill: #F2F2F7; --fill-press: #E5E5EA; --sheet: #F2F2F7; --group: #FFFFFF;
  --label: #000000;
  --label-2: rgba(60, 60, 67, 0.6);  --label-3: rgba(60, 60, 67, 0.3);
  --label-2-hc: rgba(60, 60, 67, 0.85); --label-3-hc: rgba(60, 60, 67, 0.6);
  --separator: rgba(60, 60, 67, 0.29);
  --red: #FF3B30; --green: #34C759; --tint: #007AFF;
  --seg-bg: rgba(118, 118, 128, 0.12); --seg-on: #FFFFFF;
  --scrim: rgba(0, 0, 0, 0.3);
  color-scheme: light;
}
/* ライト：「自動」でOSがライトのとき */
@media (prefers-color-scheme: light) {
  :root:not([data-theme]) {
    --bg: #FFFFFF; --fill: #F2F2F7; --fill-press: #E5E5EA; --sheet: #F2F2F7; --group: #FFFFFF;
    --label: #000000;
    --label-2: rgba(60, 60, 67, 0.6);  --label-3: rgba(60, 60, 67, 0.3);
    --label-2-hc: rgba(60, 60, 67, 0.85); --label-3-hc: rgba(60, 60, 67, 0.6);
    --separator: rgba(60, 60, 67, 0.29);
    --red: #FF3B30; --green: #34C759; --tint: #007AFF;
    --seg-bg: rgba(118, 118, 128, 0.12); --seg-on: #FFFFFF;
    --scrim: rgba(0, 0, 0, 0.3);
    color-scheme: light;
  }
}

/* 「コントラストを上げる」設定 */
@media (prefers-contrast: more) {
  body { --label-2: var(--label-2-hc); --label-3: var(--label-3-hc); }
  .key { border: 1px solid var(--separator); }
}

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

html, body { height: 100%; background: var(--bg); color: var(--label); }
body {
  font-family: var(--font);
  font-variant-numeric: tabular-nums;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  user-select: none; -webkit-user-select: none;
  overscroll-behavior: none;
  overflow: hidden;
}
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
button:focus-visible { outline: 2px solid var(--tint); outline-offset: 2px; }

/* =========================================================
   プレイ画面
   ========================================================= */
.screen {
  height: 100vh; height: 100dvh;
  display: grid;
  grid-template-rows: auto auto 1fr auto 44%; /* ナビ・選択・数字・帯・入力 */
  gap: 10px;
  /* 上端：ステータスバー（ダイナミックアイランド）の下に余白を足し、
     iOSが画面上端にかけるぼかし効果とボタンが重ならないようにする */
  padding: calc(env(safe-area-inset-top, 0px) + 12px) 12px calc(env(safe-area-inset-bottom, 0px) + 12px);
}

/* ナビゲーションバー */
.nav {
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  min-height: 44px;
}
.nav-title { font-size: 17px; font-weight: 600; color: var(--label); }
.text-btn {
  justify-self: start;
  min-height: 44px; padding: 0 4px;
  font-size: 17px; color: var(--tint);
}
.text-btn--done { justify-self: end; font-weight: 600; }
.text-btn--strong { font-weight: 600; }
.text-btn:disabled { color: var(--label-3); cursor: default; }
.icon-btn {
  justify-self: end;
  width: 44px; height: 44px;
  display: grid; place-items: center;
  color: var(--tint);
}
.nav-actions { justify-self: end; display: flex; margin-right: -6px; }
.text-btn:active:not(:disabled), .icon-btn:active { opacity: .5; }

/* ゲームとサイズの選択 */
.controls {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  width: 100%; max-width: 560px; justify-self: center;
}
.controls .seg { max-width: 360px; }
/* iOSのポップアップボタン風：青い文字＋上下の山形。タップでサイズ選択シートを開く */
.popup-btn {
  min-height: 44px; padding: 0 8px;
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 15px; color: var(--tint);
}
.popup-btn:active { opacity: .5; }

/* スコア */
.board {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  min-height: 0; text-align: center;
}
.forecast { font-size: 15px; color: var(--label-2); }
.forecast-num { font-size: 28px; font-weight: 500; color: var(--label); margin-left: 6px; }
.total {
  font-size: clamp(96px, min(36vw, 24dvh), 260px);
  font-weight: 500; line-height: 1; letter-spacing: -0.03em;
}
.last { font-size: 17px; color: var(--label-2); min-height: 22px; }
/* 8ラウンド修行の「ここで終了」 */
.end-btn { justify-self: auto; margin-top: 2px; font-size: 15px; }

/* ラウンドごとの得点 */
.strip {
  list-style: none; width: 100%; max-width: 560px; justify-self: center;
  display: grid; grid-template-columns: repeat(8, 1fr);
  text-align: center;
}
.strip li { font-size: 15px; line-height: 1.3; color: var(--label-3); }
.strip li.done { color: var(--label-2); }
.strip li.current { color: var(--label); font-weight: 600; }
.strip .bd { display: block; height: 10px; font-size: 7px; line-height: 10px; letter-spacing: 1px; color: var(--red); }
/* 8ラウンド修行：成功したラウンドの印は緑 */
.strip--sh .bd { color: var(--green); }

/* 入力パッド */
.pad {
  display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr; gap: 10px;
  width: 100%; max-width: 640px; justify-self: center;
}
.key {
  background: var(--fill); border-radius: 22px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px;
}
.key .num { font-size: clamp(52px, 16vw, 96px); font-weight: 400; line-height: 1; }
.key .dots { display: flex; gap: 6px; height: 10px; }
.key .dots i { width: 10px; height: 10px; border-radius: 50%; background: var(--red); }
.key.hit { background: var(--fill-press); }
/* 8ラウンド修行：○と×の2つを横に並べる */
.pad--two { grid-template-columns: 1fr 1fr; grid-template-rows: 1fr; }
.key-label { font-size: 15px; color: var(--label-2); height: 10px; line-height: 10px; }

/* =========================================================
   シート（結果・設定 共通）
   ========================================================= */
.scrim {
  position: fixed; inset: 0; background: var(--scrim);
  opacity: 0; visibility: hidden;
  transition: opacity .3s ease, visibility 0s .3s;
}
.scrim.open { opacity: 1; visibility: visible; transition: opacity .3s ease, visibility 0s; }

.sheet {
  position: fixed; left: 0; right: 0; bottom: 0;
  top: calc(env(safe-area-inset-top, 0px) + 20px);
  max-width: 640px; margin: 0 auto;
  background: var(--sheet);
  border-radius: 14px 14px 0 0;
  display: flex; flex-direction: column;
  padding: 0 16px calc(env(safe-area-inset-bottom, 0px) + 16px);
  transform: translateY(100%); visibility: hidden;
  transition: transform .4s var(--ease-sheet), visibility 0s .4s;
}
.sheet.open { transform: none; visibility: visible; transition: transform .4s var(--ease-sheet), visibility 0s; }
.sheet:focus { outline: none; }

.sheet-body {
  flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch;
  display: flex; flex-direction: column; gap: 28px;
  padding-top: 12px;
  user-select: text; -webkit-user-select: text;
}
/* 中身は縮めず、収まらないときはスクロールさせる */
.sheet-body > * { flex-shrink: 0; }
.sheet-body--center { justify-content: flex-start; gap: 20px; padding-top: 0; }
/* 中央寄せ：内容が収まるときは上下中央、あふれるときは上から表示（上が切れない） */
.sheet-body--center > :first-child { margin-top: auto; }
.sheet-body--center > :last-child  { margin-bottom: auto; }

.r-total {
  text-align: center;
  font-size: clamp(88px, min(30vw, 18dvh), 180px);
  font-weight: 500; line-height: 1; letter-spacing: -0.03em;
}

/* 結果：「達成」の文字は数字より小さくする */
.r-total--text { font-size: clamp(56px, 16vw, 96px); letter-spacing: 0; }
/* 結果：判定の根拠（直近の平均など） */
.r-note { text-align: center; margin-top: 6px; padding: 0 16px; font-size: 13px; line-height: 1.5; color: var(--label-2); }

/* 設定アプリ風のグループリスト */
.section-title  { font-size: 13px; font-weight: 400; color: var(--label-2); padding: 0 16px 7px; }
.section-footer { font-size: 13px; color: var(--label-2); padding: 7px 16px 0; line-height: 1.4; }

.group { list-style: none; background: var(--group); border-radius: 10px; overflow: hidden; }
.group > li {
  display: flex; justify-content: space-between; align-items: center;
  min-height: 44px; padding: 0 16px; font-size: 17px;
  position: relative;
}
.group > li + li::before {
  content: ""; position: absolute; top: 0; left: 16px; right: 0;
  height: 1px; transform: scaleY(.5); background: var(--separator);
}
.group .value { color: var(--label-2); }
.group > .row-control { padding: 8px 12px; }
.group > .row-text { display: block; padding: 11px 16px; font-size: 15px; line-height: 1.5; }
.row-tint { color: var(--tint); justify-content: center !important; }

/* セグメンテッドコントロール */
.seg {
  display: grid; grid-template-columns: repeat(3, 1fr);
  width: 100%; padding: 2px; border-radius: 9px; background: var(--seg-bg);
}
/* 選択肢が2つのとき（ゲームの切り替え） */
.seg--two { grid-template-columns: repeat(2, 1fr); }
.seg button { min-height: 32px; border-radius: 7px; font-size: 13px; font-weight: 500; }
.seg button[aria-checked="true"] {
  background: var(--seg-on); font-weight: 600;
  box-shadow: 0 3px 8px rgba(0, 0, 0, .12), 0 3px 1px rgba(0, 0, 0, .04);
}

.primary {
  min-height: 52px; margin-top: 16px;
  background: var(--tint); color: #FFFFFF; border-radius: 14px;
  font-size: 17px; font-weight: 600;
}
.primary:active { opacity: .7; }

/* 結果：自己ベスト */
.flight { text-align: center; margin-top: 8px; font-size: 20px; font-weight: 600; color: var(--label-2); }
.group .value--strong { color: var(--label); font-weight: 600; padding: 11px 0; line-height: 1.4; }
.badge { text-align: center; margin-top: 6px; font-size: 15px; font-weight: 600; color: var(--tint); }
.badge--error { color: var(--red); font-weight: 400; font-size: 13px; line-height: 1.5; }

/* 行全体がボタンになっているリスト行 */
.group > .row-link { padding: 0; }
.row-link > button, .row-link > a {
  width: 100%; min-height: 44px; padding: 0 16px;
  display: flex; justify-content: space-between; align-items: center;
  font-size: 17px; text-align: left;
}
.row-link > button:active, .row-link > a:active { background: var(--fill-press); }
.row-link > a { color: var(--label); text-decoration: none; gap: 12px; padding-top: 10px; padding-bottom: 10px; }
.link-title { line-height: 1.4; }
.link-value { color: var(--label-2); }
.ext { color: var(--label-3); font-size: 15px; margin-left: 4px; flex-shrink: 0; }
.tag {
  display: inline-block; margin-left: 8px; padding: 1px 6px; vertical-align: 2px;
  border: 1px solid var(--separator); border-radius: 4px;
  font-size: 11px; color: var(--label-2);
}
.chev { color: var(--label-3); font-size: 22px; line-height: 1; }
/* サイズ選択：選んでいる行のチェックマーク */
.check { display: inline-flex; justify-content: center; width: 16px; margin-left: 12px; color: var(--tint); vertical-align: -2px; }
.destructive { color: var(--red); justify-content: center !important; }

/* =========================================================
   履歴シート
   ========================================================= */
#history-content { display: flex; flex-direction: column; gap: 28px; }
.empty { margin: auto 0; text-align: center; font-size: 15px; line-height: 1.6; color: var(--label-2); }

.group > .h-row { display: block; padding: 0; }
.h-line { display: flex; align-items: center; }
.h-main {
  flex: 1; min-width: 0; min-height: 60px; padding: 8px 12px 8px 16px;
  display: flex; align-items: center; gap: 12px; text-align: left;
}
.h-main:active { background: var(--fill-press); }
.h-text { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.h-date { font-size: 17px; }
.h-sub  { font-size: 13px; color: var(--label-2); }
.h-score { font-size: 28px; font-weight: 500; letter-spacing: -0.02em; }
.h-score--text { font-size: 17px; font-weight: 600; letter-spacing: 0; color: var(--tint); }
/* 8ラウンド修行の行は押しても何も起きないので、押した表示を出さない */
div.h-main:active { background: none; }
.h-main .chev { width: 12px; text-align: center; }
.h-main[aria-expanded="true"] .chev { transform: rotate(90deg); }
.h-del { display: none; min-height: 44px; padding: 0 16px; color: var(--red); font-size: 17px; }
#history.editing .h-del { display: block; }
#history.editing .h-main .chev { display: none; }
.h-strip { padding: 2px 16px 12px; max-width: none; }

/* ホーム画面から起動したとき（iOS 26以降）：
   iOSが画面の上端にかけるぼかし効果がボタンにかからないよう、上の余白を広げる */
/* 画面の低い機種（iPhone SEなど）：結果シートを1画面に収めやすくする */
@media (max-height: 700px) {
  .screen { grid-template-rows: auto auto 1fr auto 40%; }
  .total { font-size: clamp(64px, 15dvh, 120px); }
  .r-total { font-size: clamp(72px, 13dvh, 120px); }
  .sheet-body--center { gap: 14px; }
}

@media (display-mode: standalone) {
  .screen {
    padding-top: 32px;
    /* 下端：ホームに戻るスワイプ（画面下端から上へ）でボタンが押されないよう、
       最低36pxの余白を確保する */
    padding-bottom: max(calc(env(safe-area-inset-bottom, 0px) + 12px), 36px);
  }
  .sheet {
    top: 32px;
    padding-bottom: max(calc(env(safe-area-inset-bottom, 0px) + 16px), 36px);
  }
}

@media (prefers-reduced-motion: reduce) {
  .scrim, .scrim.open, .sheet, .sheet.open { transition: none; }
}

/* =========================================================
   横向き：縦向き専用のため、スマホを横にしたら案内で覆う
   （Androidは manifest の orientation で縦に固定される。
     iPhoneはWebから向きを固定できないので、この案内で代用する。
     iPadは高さに余裕があるので対象外＝縦向きのレイアウトのまま使える）
   ========================================================= */
.rotate-notice { display: none; }
@media (orientation: landscape) and (max-height: 560px) {
  .rotate-notice {
    position: fixed; inset: 0; z-index: 1000;
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px;
    background: var(--bg); color: var(--label-2);
    font-size: 15px; text-align: center;
  }
  .rotate-notice svg { color: var(--label); }
}
