:root {
  --bg: #111317;
  --panel: #191c22;
  --line: #2b3039;
  --text: #f3f1ea;
  --muted: #9aa2ad;
  --accent: #f2b134;
  --accent-ink: #1b1405;
  --danger: #e5484d;
  --font: system-ui, -apple-system, "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Yu Gothic UI", "Meiryo", sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}

body { overflow: hidden; }

button, input, textarea { font: inherit; color: inherit; }

.app {
  position: relative;
  display: grid;
  grid-template-rows: auto 1fr auto auto;
  height: 100vh;
  height: 100dvh;
  max-width: 560px;
  margin: 0 auto;
  padding: max(8px, env(safe-area-inset-top)) 12px max(8px, env(safe-area-inset-bottom));
  gap: 8px;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

/* HUD */
.hud {
  display: grid;
  grid-template-columns: 1.3fr 1fr 1fr auto;
  align-items: end;
  gap: 8px;
  border-bottom: 1px solid var(--line);
  padding-bottom: 8px;
}
.hud-cell { display: flex; flex-direction: column; min-width: 0; }
.label { font-size: 11px; letter-spacing: 0.14em; color: var(--muted); font-weight: 700; }
.value {
  font-size: clamp(22px, 6.5vw, 30px);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.value.small { font-size: clamp(16px, 4.6vw, 20px); color: var(--muted); }
.hud-time .value { color: var(--accent); }
.app.is-hurry .hud-time .value { color: var(--danger); }
.app.is-lastmove .hud-time .value { color: var(--danger); animation: pulse 0.5s ease-in-out infinite alternate; }
.hud-next canvas { width: 60px; height: 38px; display: block; }
@keyframes pulse { from { opacity: 1; } to { opacity: 0.45; } }

/* 盤面 */
.stage {
  position: relative;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
#board {
  display: block;
  touch-action: none;
  border-radius: 4px;
  box-shadow: 0 0 0 1px var(--line);
  cursor: pointer;
}
.toast {
  position: absolute;
  left: 50%;
  bottom: 14px;
  transform: translateX(-50%);
  padding: 8px 14px;
  background: rgba(10, 11, 14, 0.9);
  border: 1px solid var(--line);
  border-radius: 4px;
  font-size: 14px;
  font-weight: 700;
  white-space: nowrap;
  opacity: 0;
  transition: opacity 0.15s;
  pointer-events: none;
}
.toast.show { opacity: 1; }

/* 操作ボタン */
.controls { display: grid; grid-template-columns: 1fr 1.3fr 1fr; gap: 8px; }
.ctl {
  min-height: 52px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 6px;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
}
.ctl small { display: block; font-size: 11px; color: var(--muted); font-weight: 600; }
.ctl:active:not(:disabled) { background: #222630; }
.ctl:disabled { opacity: 0.4; cursor: default; }
.keys-hint { margin: 0; font-size: 11px; color: var(--muted); text-align: center; line-height: 1.5; }
@media (hover: none), (max-width: 480px) { .keys-hint { display: none; } }

.app[data-screen="title"] .hud,
.app[data-screen="title"] .controls { visibility: hidden; }

/* オーバーレイ画面 */
.screen {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(11, 12, 15, 0.94);
  z-index: 10;
  overflow-y: auto;
}
.screen[hidden] { display: none; }
.modal { z-index: 20; background: var(--bg); }
#screen-title { background: var(--bg); }

.panel {
  width: 100%;
  max-width: 380px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin: auto;
}
.panel.wide { max-width: 520px; }
.panel h2 { margin: 0; font-size: 24px; }
.panel h3 { margin: 0 0 4px; font-size: 15px; }

.logo {
  margin: 0 0 4px;
  display: flex;
  flex-direction: column;
  font-size: clamp(48px, 15vw, 72px);
  font-weight: 900;
  line-height: 0.92;
  letter-spacing: -0.02em;
}
.logo .accent { color: var(--accent); }
.tagline { margin: 0 0 8px; color: var(--muted); line-height: 1.6; font-size: 15px; }

.btn {
  min-height: 48px;
  padding: 10px 16px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 6px;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
}
.btn:hover { border-color: #3b4250; }
.btn:focus-visible, .ctl:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.primary:hover { background: #f6c25a; }
.btn.big { min-height: 60px; font-size: 20px; letter-spacing: 0.04em; }
.btn.ghost { background: transparent; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.muted { color: var(--muted); margin: 0; font-size: 14px; }
.note { margin: 0; color: var(--muted); font-size: 13px; line-height: 1.5; min-height: 1em; }

.records { display: grid; grid-template-columns: repeat(auto-fit, minmax(90px, 1fr)); gap: 8px; margin: 0; }
.records div { border-top: 1px solid var(--line); padding-top: 6px; }
.records dt { font-size: 12px; color: var(--muted); }
.records dd { margin: 0; font-size: 20px; font-weight: 800; font-variant-numeric: tabular-nums; }

.result-score { margin: 0; font-size: clamp(48px, 14vw, 64px); font-weight: 900; font-variant-numeric: tabular-nums; line-height: 1; }
.newbest { margin: 0; color: var(--accent); font-weight: 800; letter-spacing: 0.06em; }
.share-text {
  width: 100%;
  resize: none;
  background: #0c0d10;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 13px;
  line-height: 1.5;
  user-select: text;
  -webkit-user-select: text;
}

.rules { margin: 0; padding-left: 1.3em; line-height: 1.7; font-size: 15px; }
.rules li + li { margin-top: 2px; }
.howto { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; font-size: 14px; line-height: 1.6; }
.howto p { margin: 0; color: #d6d8dc; }
@media (max-width: 480px) { .howto { grid-template-columns: 1fr; } }

.setting { display: flex; flex-direction: column; gap: 8px; font-weight: 700; }
.setting.check { flex-direction: row; align-items: center; gap: 12px; min-height: 44px; cursor: pointer; }
.setting input[type="range"] { width: 100%; accent-color: var(--accent); height: 32px; }
.setting input[type="checkbox"] { width: 24px; height: 24px; accent-color: var(--accent); }

/* 横向きスマホなど背の低い画面: HUD を左、操作ボタンを右に置いて盤面の高さを確保する */
@media (orientation: landscape) and (max-height: 520px) {
  .app {
    max-width: none;
    grid-template-columns: minmax(120px, 1fr) auto minmax(120px, 1fr);
    grid-template-rows: 1fr;
    grid-template-areas: "hud stage controls";
    padding: max(6px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) max(6px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
    gap: 12px;
  }
  .hud {
    grid-area: hud;
    grid-template-columns: 1fr;
    align-content: center;
    align-items: start;
    border-bottom: 0;
    padding-bottom: 0;
    gap: 6px;
  }
  .stage { grid-area: stage; }
  .controls {
    grid-area: controls;
    grid-template-columns: 1fr;
    align-content: center;
  }
  .ctl { min-height: 48px; }
  .keys-hint { display: none; }
  .panel { gap: 8px; }
  .logo { font-size: 40px; flex-direction: row; gap: 0.3em; }
  .result-score { font-size: 40px; }
  .btn.big { min-height: 48px; font-size: 18px; }
}

@media (prefers-reduced-motion: reduce) {
  .app.is-lastmove .hud-time .value { animation: none; }
}
