:root {
  --purple-deep: #2d1b46;
  --purple-dark: #190e29;
  --purple-mid: #53336e;
  --cream: #faf6ef;
  --cream-warm: #f1e8dc;
  --gold: #f5b335;
  --gold-bright: #ffd966;
  --teal: #38d9c0;
  --coral: #eb5e4c;
  --ink: #2a1c36;
  --muted: #766685;
}

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

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: radial-gradient(circle at 50% 20%, #3d2459 0%, #160c24 100%);
  font-family: "Noto Sans TC", system-ui, -apple-system, sans-serif;
  color: var(--ink);
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}

#app {
  width: 100%;
  height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Mobile-first portrait shell (full-bleed on phones, sleek phone viewport on desktop) */
#game-shell {
  position: relative;
  width: 100%;
  height: 100%;
  max-width: 500px;
  max-height: 960px;
  overflow: hidden;
  touch-action: none;
  background: #271742;
}

@media (min-width: 540px) {
  #game-shell {
    height: min(94dvh, 880px);
    border-radius: 28px;
    border: 2.5px solid rgba(255, 255, 255, 0.16);
    box-shadow: 0 28px 80px rgba(0, 0, 0, 0.55);
  }
}

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

.hidden {
  display: none !important;
}

/* Compact Mobile HUD */
#hud {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  padding: max(12px, env(safe-area-inset-top)) 12px 6px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 6px;
  pointer-events: none;
  z-index: 12;
}

.hud-left,
.hud-right {
  display: flex;
  align-items: center;
  gap: 6px;
}

.hud-pill {
  background: rgba(25, 14, 41, 0.72);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 13px;
  padding: 5px 9px;
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 56px;
  backdrop-filter: blur(8px);
  box-shadow: 0 4px 14px rgba(10, 4, 20, 0.25);
}

.hud-stage {
  align-items: flex-start;
  min-width: 92px;
}

.hud-label {
  font-size: 9px;
  letter-spacing: 0.06em;
  opacity: 0.76;
  line-height: 1.1;
}

#hud-stage-name {
  font-size: 12px;
  font-weight: 900;
  color: var(--gold-bright);
  line-height: 1.25;
  white-space: nowrap;
}

#hud-timer-val,
#hud-score-val,
#hud-combo-val {
  font-size: 14px;
  font-weight: 900;
  line-height: 1.25;
  font-variant-numeric: tabular-nums;
}

.hud-combo.fever-on {
  background: linear-gradient(135deg, rgba(245, 158, 11, 0.94), rgba(235, 94, 76, 0.94));
  border-color: rgba(255, 230, 140, 0.75);
  animation: pulse-pop 0.42s ease-in-out infinite alternate;
}

.icon-btn {
  pointer-events: auto;
  width: 36px;
  height: 36px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.18);
  background: rgba(25, 14, 41, 0.78);
  color: #fff;
  font-size: 16px;
  display: grid;
  place-items: center;
  cursor: pointer;
  backdrop-filter: blur(8px);
  transition: transform 0.1s ease;
}

.icon-btn:active {
  transform: scale(0.9);
}

/* Toast Banner */
.toast-banner {
  position: absolute;
  top: max(68px, calc(env(safe-area-inset-top) + 56px));
  left: 50%;
  transform: translateX(-50%);
  z-index: 15;
  pointer-events: none;
  padding: 7px 16px;
  border-radius: 999px;
  background: linear-gradient(135deg, rgba(245, 179, 53, 0.96), rgba(255, 217, 102, 0.96));
  color: #2a1836;
  font-size: 12px;
  font-weight: 900;
  letter-spacing: 0.05em;
  box-shadow: 0 8px 24px rgba(245, 179, 53, 0.42);
  animation: toast-in 0.25s cubic-bezier(0.22, 1, 0.36, 1);
  white-space: nowrap;
}

/* Mobile-First Thumb Controls Bar */
#controls-bar {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 8px 18px max(22px, env(safe-area-inset-bottom));
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  pointer-events: none;
  z-index: 14;
}

/* Left Thumb Virtual Joystick */
.joystick-zone {
  pointer-events: auto;
  width: 138px;
  height: 138px;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: none;
}

.joystick-base {
  position: relative;
  width: 124px;
  height: 124px;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(25, 14, 41, 0.42) 0%, rgba(25, 14, 41, 0.72) 100%);
  border: 2px solid rgba(255, 255, 255, 0.22);
  box-shadow: 0 10px 26px rgba(10, 4, 20, 0.38), inset 0 0 18px rgba(255, 217, 102, 0.08);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
}

.joy-tick {
  position: absolute;
  font-size: 12px;
  font-weight: 900;
  color: rgba(255, 255, 255, 0.34);
  pointer-events: none;
  transition: color 0.1s ease, transform 0.1s ease;
}

.joy-tick.active {
  color: var(--gold-bright);
  transform: scale(1.25);
}

.joy-ne { top: 14px; right: 18px; }
.joy-nw { top: 14px; left: 18px; }
.joy-se { bottom: 14px; right: 18px; }
.joy-sw { bottom: 14px; left: 18px; }

.joystick-knob {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background: linear-gradient(145deg, #ffd966 0%, #e89b24 100%);
  border: 2.5px solid rgba(255, 255, 255, 0.72);
  box-shadow: 0 6px 18px rgba(245, 179, 53, 0.5);
  pointer-events: none;
  transform: translate3d(0, 0, 0);
  transition: transform 0.06s ease-out;
}

.joystick-knob.dragging {
  transition: none;
}

/* Right Thumb Action Cluster */
.action-cluster {
  pointer-events: auto;
  display: flex;
  align-items: flex-end;
  gap: 12px;
  padding-bottom: 6px;
}

.action-btn {
  position: relative;
  overflow: hidden;
  border-radius: 50%;
  border: 2.5px solid rgba(255, 255, 255, 0.34);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  cursor: pointer;
  backdrop-filter: blur(8px);
  box-shadow: 0 10px 26px rgba(10, 4, 20, 0.42);
  transition: transform 0.08s ease, filter 0.12s ease;
  touch-action: none;
}

.action-btn:active,
.action-btn.pressed {
  transform: scale(0.9);
}

.btn-rotate {
  width: 74px;
  height: 74px;
  background: linear-gradient(145deg, rgba(255, 217, 102, 0.94), rgba(224, 142, 24, 0.94));
  color: #251332;
}

.btn-dash {
  width: 84px;
  height: 84px;
  background: linear-gradient(145deg, rgba(56, 217, 192, 0.95), rgba(24, 148, 152, 0.95));
  color: #0e242a;
}

.btn-dash.cooling {
  filter: saturate(0.4) brightness(0.75);
}

.action-icon {
  font-size: 26px;
  line-height: 1;
}

.action-label {
  font-size: 12px;
  font-weight: 900;
  letter-spacing: 0.03em;
}

.dash-cd-ring {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 0%;
  background: rgba(15, 10, 25, 0.48);
  pointer-events: none;
  transition: height 0.05s linear;
}

/* Overlays */
.overlay {
  position: absolute;
  inset: 0;
  z-index: 25;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
  background: radial-gradient(circle at 50% 30%, rgba(45, 27, 70, 0.68), rgba(18, 9, 32, 0.86));
  backdrop-filter: blur(8px);
}

.start-card,
.result-card {
  width: 100%;
  max-width: 410px;
  background: linear-gradient(180deg, #fffdf9 0%, #f5ece0 100%);
  border-radius: 26px;
  padding: 24px 20px 22px;
  text-align: center;
  box-shadow: 0 24px 60px rgba(10, 4, 22, 0.48), inset 0 1px 0 rgba(255, 255, 255, 0.9);
  border: 1px solid rgba(255, 255, 255, 0.6);
  animation: card-pop 0.32s cubic-bezier(0.22, 1, 0.36, 1);
}

.badge-tag,
.rank-badge {
  display: inline-block;
  padding: 5px 14px;
  border-radius: 999px;
  background: var(--purple-deep);
  color: var(--gold-bright);
  font-size: 11px;
  font-weight: 900;
  letter-spacing: 0.08em;
  margin-bottom: 10px;
}

.rank-gold {
  background: linear-gradient(135deg, #f59e0b, #f5b335);
  color: #2b1438;
}

.game-title {
  margin: 0 0 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  line-height: 1.15;
}

.title-accent {
  font-size: 32px;
  font-weight: 900;
  background: linear-gradient(135deg, #4f2a75, #9c4b8e, #e87a4a);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.title-sub-main {
  font-size: 19px;
  font-weight: 900;
  color: var(--ink);
  letter-spacing: 0.12em;
}

.game-sub,
.stage-clear-sub {
  margin: 0 0 16px;
  font-size: 13px;
  line-height: 1.6;
  color: var(--muted);
  font-weight: 500;
}

.feature-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 18px;
  text-align: left;
}

.feature-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 10px;
  border-radius: 14px;
  background: rgba(52, 32, 68, 0.06);
  border: 1px solid rgba(52, 32, 68, 0.08);
}

.feat-icon {
  font-size: 19px;
  flex-shrink: 0;
}

.feature-item strong {
  display: block;
  font-size: 12px;
  color: var(--ink);
}

.feature-item span {
  display: block;
  font-size: 10px;
  color: var(--muted);
  line-height: 1.3;
}

.result-title {
  margin: 0 0 8px;
  font-size: 23px;
  font-weight: 900;
  color: var(--ink);
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin: 14px 0 16px;
}

.stat-box {
  background: rgba(52, 32, 68, 0.07);
  border-radius: 14px;
  padding: 10px 6px;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.stat-label {
  font-size: 11px;
  color: var(--muted);
  font-weight: 700;
}

.stat-val {
  font-size: 17px;
  font-weight: 900;
  color: var(--purple-deep);
  font-variant-numeric: tabular-nums;
}

.new-record-pill {
  display: inline-block;
  margin-bottom: 14px;
  padding: 5px 14px;
  border-radius: 999px;
  background: #fff3c4;
  color: #b45309;
  font-size: 12px;
  font-weight: 900;
}

.result-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.btn-primary {
  width: 100%;
  border: none;
  border-radius: 16px;
  padding: 15px 20px;
  background: linear-gradient(135deg, #53336e 0%, #342044 100%);
  color: var(--gold-bright);
  font-family: inherit;
  font-size: 16px;
  font-weight: 900;
  letter-spacing: 0.06em;
  cursor: pointer;
  box-shadow: 0 10px 24px rgba(52, 32, 68, 0.34);
  transition: transform 0.1s ease;
}

.btn-primary:active {
  transform: scale(0.97);
}

.btn-secondary {
  width: 100%;
  border: 1.5px solid rgba(52, 32, 68, 0.22);
  border-radius: 14px;
  padding: 11px 16px;
  background: transparent;
  color: var(--purple-deep);
  font-family: inherit;
  font-size: 14px;
  font-weight: 800;
  cursor: pointer;
}

.btn-order {
  display: block;
  width: 100%;
  text-decoration: none;
  border-radius: 14px;
  padding: 12px 16px;
  background: linear-gradient(135deg, #f5b335, #e88e25);
  color: #291536;
  font-size: 14px;
  font-weight: 900;
  box-shadow: 0 8px 20px rgba(245, 179, 53, 0.35);
}

.best-line {
  margin-top: 12px;
  font-size: 12px;
  color: var(--muted);
}

.best-line strong {
  color: var(--purple-deep);
}

@keyframes pulse-pop {
  from { transform: scale(1); }
  to { transform: scale(1.06); }
}

@keyframes card-pop {
  from { opacity: 0; transform: translateY(14px) scale(0.96); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

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