:root {
  --bg: #0B0F24;
  --field: #0E1330;
  --panel: #151B3A;
  --panel-dark: #1D2552;
  --button: #1B2350;
  --line: #2F3A78;
  --line-faint: #232C5A;
  --text: #EEF1FF;
  --text-sub: #9AA5D6;
  --accent: #9DB4FF;
  --accent-shadow: #5F72C9;
  --warn: #FF6B6B;
  --record: #FFD166;
  --btn-shadow: #080B1C;

  --gutter: 16px;
  --gap: 8px;
  --side-w: 84px;
  --side-gap: 12px;
  --radius-btn: 20px;
  --press: 4px;

  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Hiragino Sans", "Noto Sans JP", sans-serif;
  /* スクロール・拡大・引っ張って更新・文字選択・長押しメニューを止める */
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}

body { position: fixed; inset: 0; }
#app { position: absolute; inset: 0; overflow: hidden; }
[hidden] { display: none !important; }
h1, h2, h3, p { margin: 0; }

/* ================= 画面の切りかえ（#app の data-screen で決まる） ================= */
.screen { position: absolute; inset: 0; display: none; }
#app[data-screen="title"] .screen-title,
#app[data-screen="settings"] .screen-settings { display: block; }
#app[data-screen="playing"] .screen-play,
#app[data-screen="paused"] .screen-play,
#app[data-screen="gameover"] .screen-play { display: grid; }

.screen-play,
.screen-settings {
  padding:
    calc(var(--safe-top) + 8px)
    calc(var(--safe-right) + var(--gutter))
    calc(var(--safe-bottom) + 12px)
    calc(var(--safe-left) + var(--gutter));
}

/* ================= 共通：ボタン ================= */
.btn {
  appearance: none;
  border: 1px solid var(--line-faint);
  border-radius: var(--radius-btn);
  background: var(--button);
  color: var(--text);
  box-shadow: 0 var(--press) 0 var(--btn-shadow);
  font: inherit;
  font-weight: 700;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 0;
  cursor: pointer;
  touch-action: none;
  transition: transform 80ms ease-out, box-shadow 80ms ease-out;
}
.btn svg {
  width: 30px;
  height: 30px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.6;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex-shrink: 0;
}
svg .fill, svg.fill { fill: currentColor; stroke: none; }
/* ゲームの操作ボタンは input.js が指ごとに is-pressed を付ける。メニューのボタンは :active */
.btn.is-pressed,
.btn:not([data-action]):active {
  transform: translateY(var(--press));
  box-shadow: 0 0 0 var(--btn-shadow);
}
.btn:focus-visible, .switch:focus-visible, .segmented button:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

.btn-accent { background: var(--accent); color: var(--bg); border-color: var(--accent); }
.btn-primary {
  background: var(--accent);
  color: var(--bg);
  border-color: #B9CAFF;
  box-shadow: 0 var(--press) 0 var(--accent-shadow);
}
.btn-primary:active { box-shadow: 0 0 0 var(--accent-shadow); }
.btn-icon { width: 44px; height: 44px; border-radius: 14px; }
.btn-icon svg { width: 22px; height: 22px; }

.btn-big {
  width: 100%;
  height: 64px;
  flex-direction: row;
  gap: 14px;
  font-size: 20px;
  letter-spacing: 0.12em;
  border-radius: 24px;
}
.btn-big svg { width: 22px; height: 22px; }
.btn-menu {
  height: 52px;
  flex-direction: row;
  gap: 10px;
  font-size: 16px;
  border-radius: 20px;
}
.btn-menu svg { width: 22px; height: 22px; }
.btn-full { width: 100%; }

/* ================= プレイ画面 ================= */
.screen-play {
  grid-template-rows: 48px minmax(0, 1fr) auto;
  grid-template-columns: minmax(0, 1fr);
  row-gap: 12px;
}

.topbar {
  display: grid;
  grid-template-columns: 44px 1fr 44px;
  align-items: center;
}
.score { text-align: center; line-height: 1; }
.score .label, .label {
  font-size: 11px;
  letter-spacing: 0.3em;
  color: var(--text-sub);
  font-weight: 700;
}
.score .value {
  margin-top: 4px;
  font-size: 30px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.play {
  position: relative;
  min-height: 0;
  min-width: 0;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: flex-start;
}
.stage { display: flex; gap: var(--side-gap); }
.field-frame {
  border: 1px solid var(--line-faint);
  border-radius: 14px;
  overflow: hidden;
  background: var(--field);
  line-height: 0;
}
.field-frame canvas { display: block; }

.side {
  width: var(--side-w);
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.side-label {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.25em;
  color: var(--text-sub);
  margin-bottom: -2px;
}
.panel {
  background: var(--panel);
  border: 1px solid var(--line-faint);
  border-radius: 14px;
}
.panel-hold { height: 68px; flex-shrink: 0; }
.panel-hold, .panel-next { position: relative; overflow: hidden; }
.panel-hold canvas, .panel-next canvas { position: absolute; inset: 0; display: block; }
.panel-next { flex: 1 1 216px; min-height: 120px; }
.stat {
  height: 52px;
  flex-shrink: 0;
  padding: 8px 10px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 4px;
  line-height: 1;
}
.stat .label { font-size: 10px; letter-spacing: 0.2em; }
.stat .value {
  font-size: 18px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}
.stat .value-small { font-size: 15px; }

/* 操作ボタン */
.controls { display: flex; flex-direction: column; gap: var(--gap); }
.row { display: flex; gap: var(--gap); }
.row-main .btn { height: 80px; }
.row-drop .btn { height: 60px; }
.row .btn { flex: var(--w) 1 0; min-width: 44px; }
.btn-hold span { font-size: 10px; letter-spacing: 0.15em; }
.btn-hold svg { width: 24px; height: 24px; }
.btn-wide { flex-direction: row; gap: 10px; font-size: 16px; }
.btn-wide svg { width: 24px; height: 24px; }
.btn-outline { border: 2px solid var(--accent); color: var(--accent); }

/* 左右を入れ替え：回転を左手、移動を右手に（いちばん外側に右回転） */
.controls.is-swapped [data-action="rotateCW"] { order: 1; }
.controls.is-swapped [data-action="rotateCCW"] { order: 2; }
.controls.is-swapped [data-action="hold"] { order: 3; }
.controls.is-swapped [data-action="left"] { order: 4; }
.controls.is-swapped [data-action="right"] { order: 5; }
.controls.is-swapped [data-action="hard"] { order: 1; }
.controls.is-swapped [data-action="soft"] { order: 2; }

/* ================= タイトル ================= */
.screen-title { background: var(--bg); }
.title-bg { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.title-body {
  position: relative;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: max(calc(var(--safe-top) + 40px), 17vh) 44px 0;
  gap: 18px;
}
.logo { display: flex; flex-direction: column; align-items: center; gap: 22px; width: 100%; }
.logo canvas { display: block; width: min(100%, 250px); }
.logo-kana {
  font-size: 19px;
  font-weight: 600;
  letter-spacing: 0.55em;
  margin-right: -0.55em;
  color: var(--text);
}
.best-pill {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 26px;
  border-radius: 999px;
  background: var(--panel);
  border: 1px solid var(--line-faint);
  line-height: 1;
}
.best-pill .trophy {
  width: 20px; height: 20px; fill: none; stroke: var(--record);
  stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round;
}
.best-pill .label { font-size: 11px; letter-spacing: 0.25em; }
.best-pill .value { font-size: 20px; font-weight: 800; font-variant-numeric: tabular-nums; }
.menu {
  width: 100%;
  max-width: 300px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  margin-top: 18px;
}
.menu-row { display: flex; gap: 8px; }
.menu-row .btn { flex: 1; height: 56px; }

/* ================= 設定 ================= */
.screen-settings {
  display: none;
  flex-direction: column;
  gap: 16px;
}
#app[data-screen="settings"] .screen-settings { display: flex; }
.page-title, .sheet-title {
  text-align: center;
  font-size: 20px;
  font-weight: 700;
}
.settings-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  touch-action: pan-y;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding-bottom: 8px;
}
.card {
  background: var(--panel);
  border: 1px solid var(--line-faint);
  border-radius: 22px;
  padding: 16px;
}
.card.list { padding: 0; }
.list .item { padding: 14px 16px; }
.list .item + .item { border-top: 1px solid var(--line-faint); }
.item { display: flex; align-items: center; gap: 14px; }
.item-icon {
  width: 40px; height: 40px; flex-shrink: 0;
  border-radius: 50%;
  background: var(--panel-dark);
  display: grid; place-items: center;
  color: var(--accent);
}
.item-icon svg {
  width: 20px; height: 20px; fill: none; stroke: currentColor;
  stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
}
.item-text { flex: 1; display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.item-title { font-size: 16px; font-weight: 700; }
.item-note { font-size: 12px; color: var(--text-sub); }
.card > .item-note { margin-top: 4px; }

.switch {
  appearance: none;
  flex-shrink: 0;
  width: 52px; height: 32px;
  border-radius: 999px;
  border: 0;
  padding: 3px;
  background: var(--panel-dark);
  cursor: pointer;
  touch-action: manipulation;
  /* 押せる範囲を44以上に広げる */
  position: relative;
}
.switch::before { content: ""; position: absolute; inset: -6px; }
.switch span {
  display: block;
  width: 26px; height: 26px;
  border-radius: 50%;
  background: var(--text-sub);
  transition: transform 150ms ease-out, background-color 150ms;
}
.switch[aria-checked="true"] { background: var(--accent); }
.switch[aria-checked="true"] span { transform: translateX(20px); background: var(--bg); }

.segmented {
  margin-top: 14px;
  display: flex;
  padding: 4px;
  border-radius: 18px;
  background: var(--bg);
  border: 1px solid var(--line-faint);
}
.segmented button {
  appearance: none;
  flex: 1;
  height: 44px;
  border: 0;
  border-radius: 14px;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
}
.segmented button[aria-pressed="true"] { background: var(--accent); color: var(--bg); }

.stepper { display: flex; align-items: center; gap: 6px; }
.btn-step { width: 44px; height: 44px; border-radius: 14px; box-shadow: none; }
.btn-step svg { width: 20px; height: 20px; }
.btn-step:disabled { opacity: 0.4; }
.stepper-value { width: 28px; text-align: center; font-size: 20px; font-weight: 800; font-variant-numeric: tabular-nums; }

.btn-danger {
  margin-top: 8px;
  height: 56px;
  flex-direction: row;
  gap: 10px;
  font-size: 16px;
  background: transparent;
  color: var(--warn);
  border: 2px solid var(--warn);
  box-shadow: none;
}
.btn-danger svg { width: 22px; height: 22px; }
.btn-danger-solid { height: 52px; background: var(--warn); color: var(--bg); border-color: var(--warn); box-shadow: 0 var(--press) 0 #9C3B3B; }
.danger-note { font-size: 12px; color: var(--text-sub); text-align: center; }
.version {
  margin-top: auto;
  padding-top: 24px;
  text-align: center;
  font-size: 11px;
  letter-spacing: 0.25em;
  color: var(--text-sub);
}

/* ================= 重ねて出す画面 ================= */
.overlay {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  padding: calc(var(--safe-top) + 16px) 24px calc(var(--safe-bottom) + 16px);
  background: rgba(8, 10, 26, 0.62);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
  z-index: 10;
}
.overlay.is-open { display: flex; }
#app[data-screen="paused"] #overlay-pause,
#app[data-screen="gameover"] #overlay-gameover { display: flex; }
#dialog-confirm { z-index: 20; }

.sheet {
  width: 100%;
  max-width: 300px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 30px;
  box-shadow: 0 6px 0 var(--btn-shadow);
  padding: 28px 24px 24px;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 12px;
}
.sheet-small { max-width: 300px; }
.sheet-badge {
  align-self: center;
  width: 56px; height: 56px;
  border-radius: 50%;
  background: var(--panel-dark);
  border: 1px solid var(--line);
  display: grid; place-items: center;
  color: var(--accent);
}
.sheet-badge svg { width: 22px; height: 22px; }
.sheet-title { font-size: 26px; }
.sheet-text { font-size: 14px; color: var(--text-sub); text-align: center; line-height: 1.6; margin-bottom: 4px; }
.sheet-stats {
  display: flex;
  justify-content: center;
  align-items: baseline;
  gap: 12px;
  margin-bottom: 8px;
}
.sheet-stats .value { font-size: 18px; font-weight: 800; font-variant-numeric: tabular-nums; margin-right: 8px; }

/* ゲームオーバー */
.go-title { display: flex; flex-direction: column; align-items: center; gap: 8px; }
.go-title canvas { display: block; width: 100%; }
.go-kana { font-size: 15px; font-weight: 600; color: var(--text-sub); letter-spacing: 0.25em; }
.record-badge {
  align-self: center;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 16px;
  border-radius: 999px;
  background: var(--record);
  color: var(--bg);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0.2em;
}
.record-badge svg { width: 15px; height: 15px; }
.go-score { text-align: center; line-height: 1; }
.go-score .value { font-size: 44px; font-weight: 800; margin: 8px 0 10px; font-variant-numeric: tabular-nums; }
.go-best { font-size: 13px; color: var(--text-sub); }
.go-best span { margin-left: 12px; font-variant-numeric: tabular-nums; }
.go-stats { display: flex; gap: 8px; margin: 6px 0 4px; }
.go-stat {
  flex: 1;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 14px;
  border-radius: 18px;
  background: var(--button);
  border: 1px solid var(--line-faint);
  font-size: 13px;
  color: var(--text-sub);
}
.go-stat .value { font-size: 20px; font-weight: 800; color: var(--text); font-variant-numeric: tabular-nums; }

/* 遊び方 */
.howto { max-width: 330px; }
.howto-cards { position: relative; }
.howto-card { display: none; flex-direction: column; gap: 10px; min-height: 250px; }
.howto-card.is-current { display: flex; }
.howto-card h3 { font-size: 17px; }
.howto-card p { font-size: 14px; line-height: 1.7; color: var(--text-sub); }
.howto-art {
  height: 96px;
  border-radius: 18px;
  background: var(--field);
  border: 1px solid var(--line-faint);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}
.howto-canvas { width: 100%; height: 100%; display: block; }
.key {
  width: 46px; height: 46px; border-radius: 14px;
  background: var(--button); border: 1px solid var(--line-faint);
  display: grid; place-items: center; color: var(--text);
}
.key svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2.6; stroke-linecap: round; stroke-linejoin: round; }
.key-accent { background: var(--accent); color: var(--bg); border-color: var(--accent); }
.howto-dots { display: flex; justify-content: center; gap: 8px; }
.howto-dots span { width: 8px; height: 8px; border-radius: 50%; background: var(--line); }
.howto-dots span.is-current { background: var(--accent); }
.howto-nav { display: flex; gap: 8px; }
.howto-nav .btn { flex: 1; height: 48px; flex-direction: row; font-size: 15px; }
.howto-nav .btn:disabled { opacity: 0.4; }

/* 横向きのとき（スマホだけ。PC の横長ウィンドウでは出さない） */
.rotate-notice {
  position: absolute;
  inset: 0;
  z-index: 30;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  background: var(--bg);
  color: var(--text);
  font-size: 18px;
  font-weight: 700;
}
.rotate-notice svg { width: 56px; height: 56px; fill: none; stroke: var(--accent); stroke-width: 1.8; stroke-linecap: round; }
@media (orientation: landscape) and (pointer: coarse) and (max-height: 600px) {
  .rotate-notice { display: flex; }
}

@media (prefers-reduced-motion: reduce) {
  .btn, .switch span { transition: none; }
}

/* 確認ダイアログの見出しは1行におさめる */
.sheet-small .sheet-title { font-size: 19px; }

/* 設定の中身は縮めず、入りきらないときはスクロールさせる（小さい画面向け） */
.settings-body > * { flex-shrink: 0; }

/* ================= 演出 ================= */
/* ハードドロップ：画面が2だけ下に、100ミリ秒ゆれる */
.stage.is-shake { animation: shake 100ms ease-out; }
@keyframes shake {
  0% { transform: translateY(0); }
  35% { transform: translateY(2px); }
  100% { transform: translateY(0); }
}
/* レベルアップ：LEVEL の数字が200ミリ秒ふくらむ */
.stat .value.is-bump { animation: bump 200ms ease-out; transform-origin: left center; }
@keyframes bump {
  0% { transform: scale(1); }
  45% { transform: scale(1.4); color: var(--record); }
  100% { transform: scale(1); }
}
@media (prefers-reduced-motion: reduce) {
  .stage.is-shake, .stat .value.is-bump { animation: none; }
}
