:root {
  --bg: #070b1a;
  --panel: rgba(8, 13, 36, 0.9);
  --line: rgba(110, 150, 255, 0.28);
  --text: #e8eeff;
  --dim: #8d9bd0;
  --green: #4dff98;
  --orange: #ff6a3d;
  --gold: #ffc83d;
  --purple: #b36bff;
  --cyan: #22e0ff;
  --blue: #7f9bff;
  --font: system-ui, -apple-system, "Segoe UI", "Hiragino Sans", "Yu Gothic UI", "Noto Sans JP", Meiryo, sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  overscroll-behavior: none;
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

[hidden] { display: none !important; }

#app {
  height: 100vh;
  height: 100dvh;
  max-width: 720px;
  margin: 0 auto;
  padding: max(8px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right))
           max(8px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  grid-template-areas: "hud" "effects" "stage" "dpad";
  gap: 8px;
}

/* ---- HUD ---- */
#hud { grid-area: hud; display: flex; align-items: stretch; gap: 8px; }
.stat {
  flex: 1 1 0;
  min-width: 0;
  padding: 6px 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(16, 26, 74, 0.45);
  display: flex;
  flex-direction: column;
  justify-content: center;
}
.label { font-size: 10px; letter-spacing: 0.16em; color: var(--dim); }
.value { font-size: 22px; font-weight: 800; font-variant-numeric: tabular-nums; line-height: 1.15; }
.value.small { font-size: 14px; line-height: 1.7; letter-spacing: 0.06em; }
#score { color: var(--green); text-shadow: 0 0 12px rgba(77, 255, 152, 0.55); }
#best { color: var(--gold); }
.hud-buttons { display: flex; flex-direction: column; gap: 4px; }
.icon-btn {
  flex: 1;
  min-width: 78px;
  padding: 0 10px;
  font: 700 11px/1 var(--font);
  letter-spacing: 0.1em;
  color: var(--text);
  background: rgba(16, 26, 74, 0.6);
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
}
.icon-btn:disabled { opacity: 0.35; cursor: default; }
.icon-btn:not(:disabled):hover { border-color: var(--cyan); }

/* ---- 効果の残り時間 ---- */
#effects { grid-area: effects; display: flex; flex-wrap: wrap; gap: 6px; min-height: 26px; align-items: center; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 10px;
  border-radius: 999px;
  border: 1px solid currentColor;
  font: 700 12px/1.4 var(--font);
  letter-spacing: 0.06em;
  background: rgba(8, 13, 36, 0.7);
  font-variant-numeric: tabular-nums;
}
.chip::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: currentColor; box-shadow: 0 0 8px currentColor; }
.chip.empty { color: var(--dim); border-style: dashed; opacity: 0.55; }
.chip.empty::before { display: none; }

/* ---- 盤面 ---- */
#stage { grid-area: stage; position: relative; min-height: 0; min-width: 0; touch-action: none; }
#board { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; }
#canvas {
  display: block;
  border-radius: 12px;
  box-shadow: 0 0 0 1px var(--line), 0 0 40px rgba(60, 100, 255, 0.18);
  touch-action: none;
}

/* ---- オーバーレイ ---- */
#overlay { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; pointer-events: none; }
.panel {
  pointer-events: auto;
  width: min(100%, 380px);
  max-height: 100%;
  overflow-y: auto;
  padding: 18px 20px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--panel);
  backdrop-filter: blur(6px);
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 10px;
  text-align: center;
  animation: pop 0.2s ease-out;
}
.panel.bare { background: none; border: 0; backdrop-filter: none; width: auto; pointer-events: none; }
@keyframes pop { from { opacity: 0; transform: scale(0.96); } to { opacity: 1; transform: none; } }

h1 {
  margin: 0;
  font-size: clamp(34px, 9vw, 52px);
  line-height: 1;
  font-weight: 900;
  letter-spacing: 0.12em;
  background: linear-gradient(120deg, var(--green), var(--cyan));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 0 14px rgba(77, 255, 152, 0.45));
}
h2 { margin: 0; font-size: 28px; letter-spacing: 0.14em; font-weight: 900; }
#over-title { color: var(--orange); text-shadow: 0 0 16px rgba(255, 106, 61, 0.5); }
#over-title.clear { color: var(--green); text-shadow: 0 0 16px rgba(77, 255, 152, 0.5); }
.tagline, .note { margin: 0; font-size: 12px; color: var(--dim); line-height: 1.5; }

.segmented { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.segmented button {
  padding: 10px 0;
  font: 800 13px/1 var(--font);
  letter-spacing: 0.08em;
  color: var(--dim);
  background: rgba(16, 26, 74, 0.5);
  border: 1px solid var(--line);
  border-radius: 10px;
  cursor: pointer;
}
.segmented button[aria-checked="true"] {
  color: #04120a;
  background: var(--green);
  border-color: var(--green);
  box-shadow: 0 0 16px rgba(77, 255, 152, 0.45);
}

button.primary, button.secondary {
  padding: 13px 0;
  font: 900 16px/1 var(--font);
  letter-spacing: 0.18em;
  border-radius: 12px;
  cursor: pointer;
}
button.primary { color: #04120a; background: var(--green); border: 0; box-shadow: 0 0 22px rgba(77, 255, 152, 0.4); }
button.primary:hover { filter: brightness(1.08); }
button.secondary { color: var(--text); background: transparent; border: 1px solid var(--line); font-size: 13px; padding: 10px 0; }
button.secondary:hover { border-color: var(--cyan); }
button:focus-visible { outline: 2px solid var(--cyan); outline-offset: 2px; }

.howto { list-style: none; margin: 4px 0 0; padding: 0; font-size: 12px; color: var(--dim); line-height: 1.9; }
kbd {
  display: inline-block;
  min-width: 20px;
  padding: 0 5px;
  margin: 0 1px;
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 5px;
  color: var(--text);
  font: 700 11px/1.5 var(--font);
}
.nw { white-space: nowrap; margin: 0 4px; }
.tag { display: inline-block; padding: 0 8px; margin: 0 2px; border-radius: 999px; border: 1px solid currentColor; font-size: 11px; line-height: 1.6; }
.t-speed { color: var(--cyan); }
.t-slow { color: var(--blue); }
.t-bonus { color: var(--gold); }
.t-shield { color: var(--purple); }

#countdown-number {
  font-size: clamp(90px, 28vmin, 180px);
  font-weight: 900;
  line-height: 1;
  color: var(--text);
  text-shadow: 0 0 30px rgba(77, 255, 152, 0.7), 0 0 8px rgba(0, 0, 0, 0.8);
  animation: beat 1s ease-out infinite;
}
@keyframes beat { from { transform: scale(1.35); opacity: 0.2; } 25% { opacity: 1; } to { transform: scale(0.9); opacity: 0.85; } }

.badge {
  margin: 0;
  align-self: center;
  padding: 4px 14px;
  border-radius: 999px;
  font-weight: 900;
  font-size: 13px;
  letter-spacing: 0.12em;
  color: #1a1200;
  background: var(--gold);
  box-shadow: 0 0 20px rgba(255, 200, 61, 0.6);
  animation: glow 1s ease-in-out infinite alternate;
}
@keyframes glow { from { box-shadow: 0 0 8px rgba(255, 200, 61, 0.4); } to { box-shadow: 0 0 24px rgba(255, 200, 61, 0.9); } }
.result { display: flex; gap: 10px; margin: 0; }
.result > div { flex: 1; padding: 8px; border: 1px solid var(--line); border-radius: 10px; }
.result dt { font-size: 10px; letter-spacing: 0.16em; color: var(--dim); }
.result dd { margin: 0; font-size: 28px; font-weight: 800; font-variant-numeric: tabular-nums; }
#over-score { color: var(--green); }
#over-best { color: var(--gold); }

/* ---- 方向ボタン（タッチ端末・狭い画面で表示） ---- */
#dpad {
  grid-area: dpad;
  display: none;
  justify-self: center;
  width: min(240px, 62vw);
  aspect-ratio: 3 / 2;
  grid-template: "u u u" 1fr "l d r" 1fr / 1fr 1fr 1fr;
  gap: 8px;
}
#dpad .dir {
  font-size: 22px;
  color: var(--green);
  background: rgba(16, 26, 74, 0.6);
  border: 1px solid var(--line);
  border-radius: 14px;
  touch-action: none;
  cursor: pointer;
}
#dpad .dir:active { background: rgba(77, 255, 152, 0.25); border-color: var(--green); }
#dpad .up { grid-area: u; width: 33.4%; justify-self: center; }
#dpad .left { grid-area: l; }
#dpad .down { grid-area: d; }
#dpad .right { grid-area: r; }

.touch-only { display: none; }
@media (pointer: coarse), (max-width: 600px) {
  #dpad { display: grid; }
  .touch-only { display: list-item; }
  .pc-only { display: none; }
}

/* 横向きのスマートフォン: 盤面を左、方向ボタンを右に */
@media (orientation: landscape) and (max-height: 520px) {
  #app {
    max-width: none;
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-rows: auto auto minmax(0, 1fr);
    grid-template-areas: "hud hud" "effects effects" "stage dpad";
  }
  #dpad { align-self: center; width: min(210px, 30vw); }
  .value { font-size: 18px; }
  .panel { padding: 10px 14px; gap: 6px; }
  h1 { font-size: 28px; }
  .howto { display: none; }
}

@media (max-height: 640px) and (orientation: portrait) {
  #dpad { width: min(190px, 52vw); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; }
}
