:root {
  --bg: #0b0e1a;
  --bg2: #121731;
  --panel: rgba(255, 255, 255, 0.05);
  --panel-border: rgba(255, 255, 255, 0.1);
  --text: #e8ecff;
  --muted: #8a93bd;
  --accent: #5ee7ff;
  --accent2: #ff5ec8;
  --gold: #ffd35e;
  --gap: 8px;
  --app-h: 100dvh;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: radial-gradient(120% 80% at 50% 0%, var(--bg2), var(--bg)) fixed;
  background-color: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", "Hiragino Sans", "Noto Sans JP", system-ui, 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;
}

button { font: inherit; color: inherit; touch-action: manipulation; }

#app {
  height: 100vh;
  height: var(--app-h);
  max-width: 820px;
  margin: 0 auto;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  grid-template-columns: minmax(0, 1fr);
  gap: var(--gap);
  padding:
    max(8px, env(safe-area-inset-top))
    max(8px, env(safe-area-inset-right))
    max(8px, env(safe-area-inset-bottom))
    max(8px, env(safe-area-inset-left));
}

/* ---------- HUD ---------- */
.hud {
  display: grid;
  grid-template-columns: minmax(0, 1.6fr) repeat(3, minmax(0, 1fr));
  gap: 6px;
}
.stat {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  padding: 4px 8px;
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.label {
  margin: 0;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.12em;
  color: var(--muted);
}
.value {
  font-size: clamp(15px, 4.2vw, 22px);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  line-height: 1.15;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.stat-chain .value { color: var(--accent); transition: transform 0.15s, color 0.15s; }
.stat-chain.active .value { color: var(--gold); transform: scale(1.18); }

/* ---------- play area ---------- */
.play {
  display: flex;
  gap: var(--gap);
  min-height: 0;
}
.field-wrap {
  flex: 1 1 0;
  overflow: hidden;
  min-width: 0;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.field-box {
  position: relative;
  border-radius: 6px;
  box-shadow: 0 0 0 2px rgba(94, 231, 255, 0.25), 0 10px 40px rgba(0, 0, 0, 0.5);
  overflow: hidden;
  container-type: inline-size;
}
#field { display: block; }

.side {
  flex: 0 0 clamp(78px, 21vw, 190px);
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  min-height: 0;
}
.panel {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  padding: 6px 8px;
}
.panel-small .value { font-size: clamp(14px, 3.6vw, 20px); display: block; }
#next { display: block; width: 100%; }
.side-buttons { display: flex; flex-direction: column; gap: 6px; }

.btn {
  appearance: none;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  border: 1px solid rgba(94, 231, 255, 0.45);
  background: rgba(94, 231, 255, 0.12);
  border-radius: 10px;
  padding: 10px 8px;
  min-height: 44px;
  font-weight: 800;
  letter-spacing: 0.08em;
  cursor: pointer;
}
.btn:hover:not(:disabled) { background: rgba(94, 231, 255, 0.22); }
.btn:disabled { opacity: 0.4; cursor: default; }
.btn-primary {
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  color: #0b0e1a;
  border: none;
  padding: 12px 36px;
  font-size: 18px;
}
.btn-small { min-height: 34px; padding: 6px 14px; }

.keys {
  margin: 0;
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 3px 8px;
  font-size: 12px;
  color: var(--muted);
}
.keys dt { font-weight: 700; color: var(--text); }
.keys dd { margin: 0; }

/* ---------- overlay ---------- */
.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 16px;
  text-align: center;
  background: rgba(8, 10, 22, 0.78);
  backdrop-filter: blur(3px);
}
.overlay[hidden] { display: none; }
.title {
  margin: 0;
  font-size: clamp(28px, 9vw, 44px);
  line-height: 1.05;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.title span { display: block; font-size: 0.62em; }
.overlay-msg { margin: 0; font-size: 14px; line-height: 1.6; color: var(--text); }
.overlay-hint { margin: 0; font-size: 12px; color: var(--muted); }

/* ---------- chain popup (presentation only) ---------- */
.chain-pop {
  position: absolute;
  left: 0;
  right: 0;
  top: 32%;
  text-align: center;
  pointer-events: none;
  font-weight: 900;
  font-style: italic;
  line-height: 1;
  opacity: 0;
  text-shadow: 0 0 12px rgba(94, 231, 255, 0.9), 0 3px 0 rgba(0, 0, 0, 0.6);
}
.chain-pop .big { display: block; white-space: nowrap; font-size: var(--pop-size, 34px); font-size: min(var(--pop-size, 34px), 15cqw); }
.chain-pop .sub { display: block; margin-top: 6px; font-size: 14px; color: var(--gold); }
.chain-pop.show { animation: pop var(--pop-dur, 900ms) cubic-bezier(0.2, 1.4, 0.4, 1) forwards; }
.chain-pop.tier-1 .big { color: #fff; }
.chain-pop.tier-2 .big { color: var(--accent); }
.chain-pop.tier-3 .big { color: var(--gold); text-shadow: 0 0 18px rgba(255, 211, 94, 0.95), 0 3px 0 #000; }
.chain-pop.tier-4 .big {
  background: linear-gradient(90deg, #ff5e5e, #ffd35e, #5eff8a, #5ee7ff, #b35eff, #ff5ec8);
  background-size: 200% 100%;
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  animation: rainbow 0.6s linear infinite;
  filter: drop-shadow(0 0 10px rgba(255, 94, 200, 0.9));
}
@keyframes pop {
  0% { opacity: 0; transform: scale(0.4) rotate(-6deg); }
  18% { opacity: 1; transform: scale(1.15) rotate(0); }
  30% { transform: scale(1); }
  75% { opacity: 1; transform: scale(1) translateY(0); }
  100% { opacity: 0; transform: scale(1.05) translateY(-24px); }
}
@keyframes rainbow { to { background-position: -200% 0; } }

/* ---------- touch controls ---------- */
.touch {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}
.tbtn {
  appearance: none;
  height: clamp(52px, 8.2vh, 76px);
  border-radius: 14px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.1), rgba(255, 255, 255, 0.04));
  font-size: 26px;
  font-weight: 700;
  touch-action: none;
}
.tbtn.pressed, .tbtn:active { background: rgba(94, 231, 255, 0.3); transform: scale(0.97); }
.tbtn-hard { background: linear-gradient(180deg, rgba(255, 94, 200, 0.3), rgba(255, 94, 200, 0.12)); }

/* PC (mouse): hide touch pad, show key help */
@media (hover: hover) and (pointer: fine) {
  .touch { display: none; }
}
@media (hover: none), (pointer: coarse) {
  .keys { display: none; }
}
@media (max-width: 520px) {
  .keys { display: none; }
  .btn { font-size: 12px; padding: 8px 4px; }
  .label { font-size: 9px; }
}

/* ---------- update toast ---------- */
.update-toast {
  position: fixed;
  left: 50%;
  bottom: calc(16px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 12px;
  background: #1c2350;
  border: 1px solid var(--accent);
  border-radius: 12px;
  font-size: 13px;
  z-index: 10;
}
.update-toast[hidden] { display: none; }
