:root {
  /* Pixel HUD palette — from iOS DesignSystem.swift */
  --dp-hud-pink: rgb(255, 115, 168);
  --dp-hud-red: rgb(217, 41, 46);
  --dp-xp-yellow: rgb(255, 214, 51);
  --dp-xp-empty: rgb(92, 18, 33);
  --dp-pixel-outline: rgb(22, 22, 28);
  --dp-dot-on: rgb(89, 242, 115);
  --dp-slot-fill: rgb(99, 64, 41);
  --dp-slot-highlight: rgb(130, 84, 56);

  /* Themed UI palette */
  --dp-grass-green: rgb(102, 204, 77);
  --dp-sky-blue: rgb(77, 179, 255);
  --dp-earth-yellow: rgb(242, 191, 51);
  --dp-danger-red: rgb(230, 77, 51);
  --dp-field-green: rgb(140, 199, 92);

  /* Surfaces */
  --dp-bg-dark: rgb(20, 20, 31);
  --dp-bg-card: rgb(31, 31, 41);
  --dp-text-primary: #fff;
  --dp-text-secondary: rgba(255, 255, 255, 0.65);

  /* Spacing — 4pt grid */
  --dp-space-edge: 24px;
  --dp-space-row: 16px;
  --dp-space-inline: 12px;
  --dp-space-small: 8px;
  --dp-space-micro: 4px;

  /* Pixel unit */
  --dp-px-unit: 3px;

  color-scheme: dark;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
  background: var(--dp-bg-dark);
  color: var(--dp-text-primary);
  font-family: 'Courier New', monospace;
  min-height: 100dvh;
  overflow-x: hidden;
}

/* Scene canvas container */
.scene-wrap {
  position: relative;
  width: 100%;
  max-width: 512px;
  margin: 0 auto;
  aspect-ratio: 2 / 1;
}
.scene-wrap canvas {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
}
.scene-wrap .overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
}
.scene-wrap .overlay.interactive { pointer-events: auto; }

/* HUD layout */
.hud {
  max-width: 512px;
  margin: 0 auto;
  padding: 0 var(--dp-space-edge);
}
.hud-top {
  display: flex;
  align-items: center;
  gap: var(--dp-space-small);
  padding: var(--dp-space-small) 0;
}
.hud-bottom {
  padding: var(--dp-space-small) 0;
  display: flex;
  flex-direction: column;
  gap: var(--dp-space-small);
}

/* XP bar */
.xp-bar {
  position: relative;
  height: 12px;
  background: var(--dp-xp-empty);
  border: 2px solid var(--dp-pixel-outline);
  overflow: hidden;
  image-rendering: pixelated;
}
.xp-bar .fill {
  height: 100%;
  background: var(--dp-xp-yellow);
  transition: width 0.3s ease;
}

/* Pixel button */
.pixel-btn {
  border: none;
  cursor: pointer;
  font-family: inherit;
  font-size: 14px;
  letter-spacing: 1px;
  color: #fff;
  padding: 12px 24px;
  background: var(--dp-earth-yellow);
  border: 3px solid var(--dp-pixel-outline);
  text-transform: uppercase;
  transition: transform 0.08s, filter 0.08s;
  image-rendering: pixelated;
}
.pixel-btn:active {
  transform: scale(0.97);
  filter: brightness(0.85);
}
.pixel-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
.pixel-btn.start { background: var(--dp-earth-yellow); }
.pixel-btn.pause { background: var(--dp-hud-red); }
.pixel-btn.blue { background: var(--dp-sky-blue); }
.pixel-btn.pink { background: var(--dp-hud-pink); }

/* Scene picker */
.scene-picker {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--dp-space-small);
}
.scene-picker .scene-name {
  font-size: 13px;
  min-width: 100px;
  text-align: center;
  color: var(--dp-text-secondary);
}
.scene-picker button {
  background: none;
  border: 1px solid var(--dp-text-secondary);
  color: var(--dp-text-secondary);
  padding: 4px 10px;
  cursor: pointer;
  font-family: inherit;
  font-size: 14px;
}

/* Avatar */
.avatar {
  width: 40px;
  height: 40px;
  border: 2px solid var(--dp-pixel-outline);
  background: var(--dp-slot-fill);
  image-rendering: pixelated;
  flex-shrink: 0;
}

/* Level badge */
.level-badge {
  display: inline-block;
  font-size: 10px;
  padding: 1px 4px;
  background: var(--dp-hud-pink);
  color: #fff;
}

/* Stats row */
.stats-row {
  display: flex;
  align-items: center;
  gap: var(--dp-space-micro);
  font-size: 11px;
  color: var(--dp-text-secondary);
}

/* EXP bonus badge */
.exp-bonus {
  font-size: 9px;
  padding: 1px 4px;
  background: rgba(74, 222, 128, 0.9);
  color: #fff;
  white-space: nowrap;
  flex-shrink: 0;
}

/* Floating text animation */
@keyframes floatUp {
  from { transform: translateY(0); opacity: 1; }
  to   { transform: translateY(-60px); opacity: 0; }
}

/* Focus timer */
.focus-timer {
  font-size: 28px;
  text-align: center;
  letter-spacing: 2px;
  font-variant-numeric: tabular-nums;
}

/* Star rating */
.stars { letter-spacing: 2px; font-size: 16px; }
.stars .filled { color: var(--dp-xp-yellow); }
.stars .empty  { color: var(--dp-text-secondary); }

/* Music mini bar */
.music-bar {
  display: flex;
  align-items: center;
  gap: var(--dp-space-small);
  padding: var(--dp-space-small) var(--dp-space-inline);
  background: rgba(30, 30, 50, 0.9);
  border: 1px solid rgba(255,255,255,0.08);
  font-size: 12px;
}
.music-bar .scene-label {
  color: var(--dp-text-primary);
  flex: 1;
}
.music-bar .status {
  color: var(--dp-text-secondary);
  font-size: 9px;
}
.music-bar button {
  background: var(--dp-earth-yellow);
  border: 2px solid var(--dp-pixel-outline);
  color: #fff;
  padding: 4px 8px;
  cursor: pointer;
  font-family: inherit;
  font-size: 12px;
}

/* Modal overlay */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  z-index: 200;
  justify-content: center;
  align-items: center;
}
.modal-overlay.open { display: flex; }
.modal-content {
  background: var(--dp-bg-card);
  border: 2px solid var(--dp-pixel-outline);
  padding: var(--dp-space-row);
  max-width: 400px;
  width: 90%;
  max-height: 80vh;
  overflow-y: auto;
}
.modal-content h2 {
  font-size: 16px;
  margin-bottom: var(--dp-space-small);
}

/* Gacha results grid */
.gacha-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--dp-space-small);
}
.gacha-effect-stage {
  position: relative;
  height: 168px;
  margin-bottom: var(--dp-space-small);
  overflow: hidden;
  background:
    linear-gradient(90deg, rgba(255, 255, 255, 0.035) 2px, transparent 2px) 0 0 / 16px 16px,
    linear-gradient(0deg, rgba(255, 255, 255, 0.03) 2px, transparent 2px) 0 0 / 16px 16px,
    radial-gradient(circle at 50% 52%, rgba(255, 214, 51, 0.18), transparent 28%),
    linear-gradient(180deg, rgba(77, 179, 255, 0.14), rgba(255, 115, 168, 0.08)),
    #11111a;
  border: 2px solid var(--dp-pixel-outline);
  box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.05);
}
.gacha-effect-stage canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
}
.gacha-effect-core {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 62px;
  height: 62px;
  transform: translate(-50%, -50%);
  border: 2px solid rgba(255, 255, 255, 0.72);
  background: linear-gradient(135deg, #ffd633, #ff73a8 48%, #4db3ff);
  box-shadow: 0 0 0 4px rgba(22, 22, 28, 0.9), 0 0 26px rgba(255, 214, 51, 0.7);
  image-rendering: pixelated;
}
.gacha-effect-core::before,
.gacha-effect-core::after {
  content: "";
  position: absolute;
  background: rgba(255, 255, 255, 0.78);
  box-shadow: 0 0 0 2px rgba(17, 17, 26, 0.82);
}
.gacha-effect-core::before {
  inset: 14px 20px;
}
.gacha-effect-core::after {
  left: 17px;
  right: 17px;
  top: 29px;
  height: 4px;
}
.gacha-effect-stage.playing .gacha-effect-core {
  animation: gacha-core-pop 720ms steps(8) infinite;
}
.gacha-effect-stage[data-effect="1"] .gacha-effect-core,
.gacha-effect-stage[data-effect="6"] .gacha-effect-core {
  transform: translate(-50%, -50%) rotate(45deg);
}
.gacha-effect-stage[data-effect="2"] .gacha-effect-core {
  width: 54px;
  height: 72px;
  background: linear-gradient(180deg, #4db3ff 0 42%, #ffd633 42% 48%, #ff73a8 48%);
}
.gacha-effect-stage[data-effect="3"] .gacha-effect-core {
  width: 84px;
  height: 44px;
  background: linear-gradient(90deg, #20283a, #4db3ff 54%, #ffd633);
}
.gacha-effect-stage[data-effect="4"] .gacha-effect-core,
.gacha-effect-stage[data-effect="7"] .gacha-effect-core {
  border-radius: 50%;
}
.gacha-effect-stage[data-effect="5"] .gacha-effect-core {
  background: linear-gradient(180deg, #ff7330, #ffd633 45%, #6b2b12);
}
.gacha-effect-stage[data-effect="8"] .gacha-effect-core {
  background: linear-gradient(180deg, #17202b, #31ffb6);
}
.gacha-effect-stage[data-effect="9"] .gacha-effect-core {
  background: linear-gradient(135deg, #0c0b10 0 48%, #ffd633 48% 55%, #7a4b12 55%);
}
.gacha-effect-label {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 8px;
  text-align: center;
  font-size: 11px;
  color: rgba(255, 255, 255, 0.82);
  text-shadow: 0 2px 0 var(--dp-pixel-outline);
  padding: 0 8px;
}
.gacha-effect-choices {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px;
  margin-bottom: var(--dp-space-small);
}
.gacha-effect-choice {
  min-height: 30px;
  padding: 4px 6px;
  background: rgba(255, 255, 255, 0.06);
  border: 2px solid rgba(255, 255, 255, 0.16);
  color: var(--dp-text-secondary);
  font-family: inherit;
  font-size: 10px;
  text-align: left;
  cursor: pointer;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.gacha-effect-choice.active {
  color: #fff;
  border-color: var(--dp-xp-yellow);
  background: rgba(255, 214, 51, 0.14);
}
.gacha-pending {
  grid-column: 1 / -1;
  min-height: 72px;
  display: grid;
  place-items: center;
  color: var(--dp-text-secondary);
  font-size: 12px;
}
.gacha-item {
  text-align: center;
  padding: var(--dp-space-micro);
  border: 2px solid #aaa;
  background: rgba(255, 255, 255, 0.04);
  transform-origin: 50% 100%;
  animation: gacha-card-reveal 520ms steps(7) both;
}
.gacha-item.common { background: rgba(255, 255, 255, 0.035); }
.gacha-item.uncommon { background: rgba(68, 170, 136, 0.11); }
.gacha-item.rare { background: rgba(68, 136, 255, 0.12); }
.gacha-item.epic { background: rgba(170, 68, 255, 0.14); box-shadow: 0 0 14px rgba(170, 68, 255, 0.22); }
.gacha-item.legendary { background: rgba(255, 170, 68, 0.16); box-shadow: 0 0 18px rgba(255, 170, 68, 0.32); }
.gacha-icon {
  width: 48px; height: 48px;
  margin: 0 auto var(--dp-space-micro);
  display: flex;
  align-items: center;
  justify-content: center;
  image-rendering: pixelated;
}
.gacha-icon img {
  max-width: 100%; max-height: 100%;
  object-fit: contain;
  image-rendering: pixelated;
}
.gacha-name { font-size: 10px; color: var(--dp-text-primary); }
.gacha-rarity { font-size: 9px; }

@keyframes gacha-core-pop {
  0% { filter: brightness(0.9); scale: 0.92; }
  50% { filter: brightness(1.35); scale: 1.08; }
  100% { filter: brightness(0.9); scale: 0.92; }
}

@keyframes gacha-card-reveal {
  0% { opacity: 0; transform: translateY(12px) scale(0.88) rotateX(70deg); filter: brightness(1.8); }
  62% { opacity: 1; transform: translateY(-3px) scale(1.04) rotateX(0deg); filter: brightness(1.35); }
  100% { opacity: 1; transform: translateY(0) scale(1) rotateX(0deg); filter: brightness(1); }
}

/* Inventory grid */
.inventory-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--dp-space-small);
}
.inventory-slot {
  width: 56px; height: 56px;
  background: var(--dp-slot-fill);
  border: 2px solid var(--dp-pixel-outline);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  position: relative;
}
.inventory-slot.equipped::after {
  content: '✓';
  position: absolute;
  top: 2px; right: 2px;
  font-size: 10px;
  color: var(--dp-dot-on);
}
.inventory-slot img {
  width: 40px; height: 40px;
  image-rendering: pixelated;
}
.item-bonus {
  position: absolute;
  bottom: 1px; left: 1px;
  font-size: 8px;
  padding: 0 2px;
  background: rgba(74, 222, 128, 0.85);
  color: #fff;
  line-height: 1.4;
  pointer-events: none;
}

/* Collection progress */
.collection-progress {
  display: flex;
  align-items: center;
  gap: var(--dp-space-small);
  font-size: 12px;
  margin-bottom: var(--dp-space-small);
}
.collection-progress .bar {
  flex: 1;
  height: 8px;
  background: var(--dp-xp-empty);
}
.collection-progress .bar .fill {
  height: 100%;
  background: var(--dp-dot-on);
}

/* Offline focus */
.duration-chips {
  display: flex;
  flex-wrap: wrap;
  gap: var(--dp-space-small);
  justify-content: center;
}
.duration-chip {
  padding: 8px 16px;
  border: 2px solid var(--dp-text-secondary);
  background: transparent;
  color: var(--dp-text-primary);
  cursor: pointer;
  font-family: inherit;
  font-size: 13px;
}
.duration-chip.selected {
  border-color: var(--dp-earth-yellow);
  background: rgba(242, 191, 51, 0.15);
}

/* Responsive */
@media (max-width: 540px) {
  .scene-wrap { max-width: 100vw; }
  .hud { padding: 0 var(--dp-space-inline); }
  .pixel-btn { padding: 10px 16px; font-size: 13px; }
}
