:root {
  --bg-0: #0a0b12;
  --bg-1: #12141f;
  --bg-2: #1a1d2b;
  --line: #2a2e42;
  --text: #eef0fb;
  --text-dim: #8b90ac;
  --accent: #00e5ff;
  --accent-2: #ff2d78;
  --accent-3: #ffd23f;
  --accent-4: #7b5cff;
  --accent-5: #3ce68a;
  --danger: #ff4d5e;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

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

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg-0);
  color: var(--text);
  font-family: "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
  overscroll-behavior: none;
  touch-action: none;
  user-select: none;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  max-width: 640px;
  margin: 0 auto;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
  background:
    radial-gradient(circle at 20% 0%, rgba(123, 92, 255, 0.15), transparent 55%),
    radial-gradient(circle at 80% 100%, rgba(0, 229, 255, 0.12), transparent 55%),
    var(--bg-0);
}

/* ---------- Topbar ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  flex-shrink: 0;
}

.brand {
  font-weight: 800;
  letter-spacing: 0.5px;
  font-size: 15px;
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--text);
}

.brand-dot {
  width: 10px; height: 10px;
  border-radius: 3px;
  background: linear-gradient(135deg, var(--accent), var(--accent-4));
  box-shadow: 0 0 10px var(--accent);
  display: inline-block;
}

.stat-group {
  display: flex;
  gap: 8px;
  margin-left: auto;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.stat {
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 4px 10px;
  text-align: center;
  min-width: 54px;
}

.stat-label {
  display: block;
  font-size: 9px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-dim);
}

.stat-value {
  display: block;
  font-size: 16px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.stat.combo { border-color: var(--accent-3); }
.stat.combo .stat-value { color: var(--accent-3); }

.icon-btn {
  width: 34px; height: 34px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--bg-2);
  color: var(--text);
  font-size: 16px;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
}
.icon-btn:active { background: var(--line); }

/* ---------- Board area ---------- */
.board-area {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 4px 8px;
  min-height: 0;
  overflow: hidden;
}

.side-panel {
  display: flex;
  flex-direction: column;
  gap: 10px;
  align-items: center;
  width: 58px;
  flex-shrink: 0;
}

.next-box, .best-box {
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 6px;
  width: 100%;
  text-align: center;
}

.panel-label {
  display: block;
  font-size: 8px;
  text-transform: uppercase;
  color: var(--text-dim);
  margin-bottom: 4px;
  letter-spacing: 0.5px;
}

#nextCanvas {
  width: 46px;
  height: 46px;
  display: block;
  margin: 0 auto;
}

#bestValue {
  font-weight: 700;
  font-size: 14px;
  color: var(--accent-3);
}

.booster-btn {
  width: 48px; height: 48px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--bg-2);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  position: relative;
  cursor: pointer;
  color: var(--text);
}
.booster-btn.active {
  border-color: var(--accent);
  box-shadow: 0 0 12px rgba(0,229,255,0.5);
}
.booster-btn:disabled { opacity: 0.35; }
.booster-icon { font-size: 18px; line-height: 1; }
.booster-count {
  position: absolute;
  bottom: -4px; right: -4px;
  background: var(--accent-2);
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  border-radius: 8px;
  min-width: 16px;
  padding: 1px 4px;
}

.hint-btn { margin-top: auto; }

.board-wrap {
  position: relative;
  height: min(100%, calc((min(100vw, 640px) - 148px) / 0.6));
  aspect-ratio: 9 / 15;
  max-height: 100%;
  border-radius: var(--radius);
  overflow: hidden;
  border: 1px solid var(--line);
  box-shadow: 0 10px 40px rgba(0,0,0,0.5), inset 0 0 0 1px rgba(255,255,255,0.02);
  background: var(--bg-1);
}

#gameCanvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}

/* ---------- Overlays ---------- */
.overlay {
  position: absolute;
  inset: 0;
  background: rgba(6,7,12,0.82);
  backdrop-filter: blur(2px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}

.overlay[hidden] {
  display: none;
}

.overlay-card {
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 22px 24px;
  text-align: center;
  width: 82%;
  max-width: 260px;
}

.overlay-card h2 {
  margin: 0 0 8px;
  font-size: 20px;
  background: linear-gradient(135deg, var(--accent), var(--accent-4));
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

.overlay-card p { color: var(--text-dim); font-size: 13px; margin: 4px 0 14px; }
.overlay-card .sub { margin-top: -4px; }

.btn {
  display: block;
  width: 100%;
  padding: 11px;
  margin-top: 8px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--bg-1);
  color: var(--text);
  font-weight: 700;
  font-size: 14px;
  cursor: pointer;
}
.btn.primary {
  background: linear-gradient(135deg, var(--accent), var(--accent-4));
  border: none;
  color: #06070c;
}
.btn:active { transform: scale(0.98); }

/* ---------- Bottom controls ---------- */
.controls {
  display: flex;
  gap: 8px;
  padding: 8px 12px 12px;
  flex-shrink: 0;
}

.ctrl-btn {
  flex: 1;
  height: 46px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--bg-2);
  color: var(--text);
  font-size: 17px;
  cursor: pointer;
}
.ctrl-btn:active { background: var(--line); transform: scale(0.96); }
#btnPause { flex: 0.7; font-size: 13px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  bottom: calc(78px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-2);
  border: 1px solid var(--accent);
  color: var(--text);
  padding: 8px 16px;
  border-radius: 20px;
  font-size: 13px;
  font-weight: 600;
  box-shadow: 0 4px 20px rgba(0,0,0,0.5);
  z-index: 20;
  animation: toast-in 0.25s ease-out;
}

@keyframes toast-in {
  from { opacity: 0; transform: translate(-50%, 8px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}

@media (max-width: 380px) {
  .side-panel { width: 46px; }
  .booster-btn { width: 40px; height: 40px; }
  .stat { min-width: 44px; padding: 3px 6px; }
  .board-wrap { height: min(100%, calc((100vw - 124px) / 0.6)); }
}

@media (min-height: 750px) {
  .board-wrap { max-height: 78vh; }
}
