* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  user-select: none;
  touch-action: manipulation;
}

:root {
  --bg: #0a1628;
  --panel: rgba(10, 22, 40, 0.92);
  --accent: #3dd6ff;
  --accent-dim: #1a8fb8;
  --danger: #ff4d6d;
  --gold: #ffd166;
  --text: #e8f4ff;
  --muted: #7a9bb8;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --control-deck-h: 158px;
  --control-deck-gap: 8px;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg);
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  color: var(--text);
  -webkit-user-select: none;
  user-select: none;
}

body {
  display: flex;
  justify-content: center;
}

#app {
  position: relative;
  width: min(100%, 520px);
  height: 100%;
  max-height: 100dvh;
  margin: 0 auto;
  overflow: hidden;
  touch-action: none;
  background: var(--bg);
  box-shadow: 0 0 0 1px rgba(61, 214, 255, 0.08);
}

#game-stage {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  overflow: hidden;
  z-index: 0;
  background: radial-gradient(circle at 50% 20%, rgba(30, 60, 90, 0.35), var(--bg) 68%);
  transition: bottom 0.22s ease;
}

#app.deck-active #game-stage {
  bottom: calc(var(--control-deck-h) + var(--control-deck-gap) + var(--safe-bottom));
}

#game {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  -webkit-user-drag: none;
  user-select: none;
}

#hud {
  position: absolute;
  top: max(8px, env(safe-area-inset-top, 0px));
  left: 8px;
  right: 8px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  pointer-events: none;
  z-index: 10;
}

.hud-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.hud-item {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--panel);
  border: 1px solid rgba(61, 214, 255, 0.25);
  border-radius: 10px;
  padding: 6px 10px;
  font-size: 14px;
  font-weight: 600;
  backdrop-filter: blur(8px);
  min-width: 0;
}

.hud-label {
  font-size: 16px;
  line-height: 1;
}

.air-wrap {
  flex: 1;
  min-width: 140px;
}

.depth-wrap {
  flex-shrink: 0;
}

.bar {
  flex: 1;
  height: 10px;
  background: rgba(255, 255, 255, 0.12);
  border-radius: 5px;
  overflow: hidden;
  min-width: 60px;
}

.bar-fill {
  height: 100%;
  width: 100%;
  border-radius: 5px;
  transition: width 0.35s ease-out, background 0.4s ease;
}

.bar-fill.air {
  background: linear-gradient(90deg, #3dd6ff, #6ee7ff);
}

.bar-fill.air.low {
  background: linear-gradient(90deg, #ff9f1c, #ff4d6d);
}

.bar-fill.air.critical {
  background: #ff4d6d;
  animation: pulse 0.5s infinite alternate;
}

@keyframes pulse {
  from { opacity: 1; }
  to { opacity: 0.5; }
}

.control-deck {
  position: absolute;
  left: 8px;
  right: 8px;
  bottom: var(--safe-bottom);
  height: var(--control-deck-h);
  z-index: 25;
  pointer-events: none;
  border-radius: 16px 16px 0 0;
  overflow: hidden;
}

.control-deck.hidden {
  display: none;
}

.control-deck-plate {
  position: absolute;
  inset: 0;
  background:
    linear-gradient(180deg, rgba(61, 214, 255, 0.24) 0%, rgba(61, 214, 255, 0.05) 10%, transparent 28%),
    repeating-linear-gradient(
      90deg,
      rgba(255, 255, 255, 0.025) 0 1px,
      transparent 1px 18px
    ),
    linear-gradient(180deg, #1a3048 0%, #0f1c30 52%, #08111f 100%);
  border-top: 2px solid rgba(61, 214, 255, 0.42);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.07),
    0 -10px 36px rgba(0, 0, 0, 0.48);
}

.control-deck-plate::before,
.control-deck-plate::after {
  content: '';
  position: absolute;
  top: 10px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 35%, rgba(255, 255, 255, 0.35), rgba(255, 255, 255, 0.06) 45%, rgba(0, 0, 0, 0.35) 100%);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.08);
}

.control-deck-plate::before { left: 12px; }
.control-deck-plate::after { right: 12px; }

.control-deck-inner {
  position: relative;
  height: 100%;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto 54px;
  gap: 6px;
  padding: 5px 8px;
  align-items: center;
}

.control-deck-feed {
  justify-content: center;
}

.deck-feed {
  display: flex;
  flex-direction: column;
  gap: 3px;
  width: 100%;
}

.deck-field {
  display: grid;
  grid-template-columns: 46px minmax(0, 1fr);
  gap: 5px;
  align-items: center;
}

.deck-field-label {
  font-size: 8px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: rgba(110, 231, 255, 0.52);
  line-height: 1.15;
}

.deck-line {
  min-width: 0;
  padding: 3px 7px;
  border-radius: 7px;
  background: rgba(0, 0, 0, 0.22);
  border: 1px solid rgba(61, 214, 255, 0.14);
  font-size: 11px;
  font-weight: 600;
  line-height: 1.25;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.deck-field-status .deck-line {
  font-size: 12px;
  background: rgba(0, 0, 0, 0.3);
  border-color: rgba(61, 214, 255, 0.26);
}

.deck-line-alert {
  border-color: rgba(255, 77, 109, 0.3);
  background: rgba(255, 77, 109, 0.1);
  color: #ffc8d2;
}

.deck-line-alert.deck-line-empty {
  color: rgba(255, 200, 210, 0.35);
  border-color: rgba(255, 77, 109, 0.16);
  background: rgba(255, 77, 109, 0.04);
}

.deck-line-alert.urgent {
  border-color: rgba(255, 77, 109, 0.68);
  color: #ffc8d2;
  animation: pulse 0.45s infinite alternate;
}

.control-deck-drive {
  display: flex;
  align-items: center;
  justify-content: center;
}

.control-deck-items {
  align-items: center;
  justify-content: center;
}

#joystick {
  position: relative;
  width: 100px;
  height: 100px;
  pointer-events: auto;
  flex-shrink: 0;
}

.joystick-base {
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background: rgba(8, 12, 18, 0.55);
  border: 3px solid rgba(255, 255, 255, 0.58);
  box-shadow:
    inset 0 0 24px rgba(255, 255, 255, 0.06),
    0 4px 16px rgba(0, 0, 0, 0.35);
  touch-action: none;
}

.joystick-cross {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.joystick-cross::before,
.joystick-cross::after {
  content: '';
  position: absolute;
  background: rgba(255, 255, 255, 0.28);
  border-radius: 1px;
}

.joystick-cross::before {
  left: 50%;
  top: 18%;
  width: 2px;
  height: 64%;
  transform: translateX(-50%);
}

.joystick-cross::after {
  top: 50%;
  left: 18%;
  height: 2px;
  width: 64%;
  transform: translateY(-50%);
}

.joystick-knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  transform: translate(-50%, -50%);
  background: rgba(255, 255, 255, 0.76);
  border: 2px solid rgba(255, 255, 255, 0.92);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.32);
  pointer-events: none;
}

.joystick.active .joystick-base {
  border-color: rgba(110, 231, 255, 0.82);
  background: rgba(8, 12, 18, 0.68);
}

.joystick.active .joystick-knob {
  background: rgba(255, 255, 255, 0.92);
}

.deck-label {
  display: block;
  margin-bottom: 4px;
  font-size: 8px;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: rgba(110, 231, 255, 0.52);
  text-align: center;
}

.control-deck-zone {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.item-bar {
  display: flex;
  flex-direction: column;
  gap: 5px;
  pointer-events: auto;
}

.item-btn {
  pointer-events: auto;
  position: relative;
  width: 46px;
  height: 46px;
  border: none;
  border-radius: 12px;
  background: linear-gradient(165deg, rgba(22, 42, 68, 0.96), rgba(8, 16, 28, 0.98));
  border: 2px solid rgba(61, 214, 255, 0.24);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.05),
    0 4px 12px rgba(0, 0, 0, 0.28);
  cursor: pointer;
  transition: transform 0.1s, border-color 0.15s, opacity 0.15s;
}

.item-btn:active:not(:disabled) {
  transform: scale(0.92);
}

.item-btn:disabled {
  opacity: 0.35;
  cursor: default;
}

.item-btn.item-bomb:not(:disabled) {
  border-color: rgba(255, 77, 109, 0.45);
}

.item-btn.item-turbine:not(:disabled) {
  border-color: rgba(110, 231, 255, 0.45);
}

.item-btn.item-air:not(:disabled) {
  border-color: rgba(46, 204, 113, 0.45);
}

.item-icon {
  font-size: 24px;
  line-height: 1;
}

.item-count {
  position: absolute;
  top: -6px;
  right: -6px;
  min-width: 20px;
  height: 20px;
  padding: 0 5px;
  border-radius: 999px;
  background: var(--gold);
  color: #1a1208;
  font-size: 11px;
  font-weight: 800;
  display: flex;
  align-items: center;
  justify-content: center;
}

.market-tabs {
  display: flex;
  gap: 6px;
  margin-bottom: 10px;
  flex-shrink: 0;
}

.market-tabs.hidden {
  display: none;
}

.market-tab {
  flex: 1;
  border: 1px solid rgba(61, 214, 255, 0.2);
  background: rgba(0, 0, 0, 0.2);
  color: var(--muted);
  border-radius: 999px;
  padding: 7px 8px;
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
}

.market-tab.active {
  color: var(--text);
  border-color: rgba(61, 214, 255, 0.45);
  background: rgba(61, 214, 255, 0.12);
}

.market-balance {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 12px;
  margin-bottom: 10px;
  border-radius: 12px;
  background: rgba(255, 209, 102, 0.08);
  border: 1px solid rgba(255, 209, 102, 0.2);
  font-size: 14px;
}

.market-balance strong {
  color: var(--gold);
  font-size: 18px;
}

.market-section.hidden {
  display: none;
}

.upgrade-card,
.item-card {
  text-align: left;
  background: rgba(0, 0, 0, 0.22);
  border: 1px solid rgba(61, 214, 255, 0.15);
  border-radius: 14px;
  padding: 12px;
  margin-bottom: 8px;
}

.upgrade-head,
.item-head {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  align-items: flex-start;
  margin-bottom: 6px;
}

.upgrade-title,
.item-title {
  font-weight: 700;
  font-size: 14px;
}

.upgrade-track {
  font-size: 11px;
  color: var(--muted);
  margin-bottom: 8px;
}

.upgrade-levels {
  display: flex;
  gap: 4px;
  margin-bottom: 8px;
}

.level-pip {
  flex: 1;
  height: 5px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.08);
}

.level-pip.on {
  background: linear-gradient(90deg, var(--accent), #6ee7ff);
}

.level-pip.saw.on {
  background: linear-gradient(90deg, #ff6b9d, #b388ff);
}

.buy-btn {
  width: 100%;
  margin-top: 4px;
  border: none;
  border-radius: 12px;
  padding: 10px 12px;
  font-size: 13px;
  font-weight: 700;
  color: var(--bg);
  background: linear-gradient(135deg, var(--accent), var(--accent-dim));
  cursor: pointer;
}

.buy-btn:disabled {
  opacity: 0.45;
  cursor: default;
}

.buy-btn.maxed {
  background: rgba(255, 255, 255, 0.08);
  color: var(--muted);
}

.item-card .item-desc {
  font-size: 12px;
  color: var(--muted);
  line-height: 1.4;
  margin-bottom: 8px;
}

.item-stock {
  font-size: 12px;
  color: var(--accent);
  margin-bottom: 6px;
}

.upgrade-preview {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--muted);
}

.preview-dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.panel-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex-shrink: 0;
}

.action-btn.secondary {
  background: rgba(255, 255, 255, 0.08);
  color: var(--text);
  box-shadow: none;
  border: 1px solid rgba(61, 214, 255, 0.2);
}

@media (max-width: 360px) {
  :root {
    --control-deck-h: 148px;
    --control-deck-gap: 6px;
  }

  .control-deck {
    left: 6px;
    right: 6px;
  }

  .control-deck-inner {
    grid-template-columns: minmax(0, 1fr) auto 50px;
    gap: 5px;
    padding: 4px 6px;
  }

  #joystick {
    width: 92px;
    height: 92px;
  }

  .joystick-knob {
    width: 38px;
    height: 38px;
  }

  .item-btn {
    width: 40px;
    height: 40px;
  }

  .item-icon {
    font-size: 20px;
  }
}

.action-btn {
  border: none;
  border-radius: 16px;
  padding: 14px 18px;
  font-size: 15px;
  font-weight: 700;
  color: var(--bg);
  background: linear-gradient(135deg, var(--accent), var(--accent-dim));
  box-shadow: 0 4px 20px rgba(61, 214, 255, 0.35);
  cursor: pointer;
  white-space: nowrap;
  transition: transform 0.08s, opacity 0.2s;
}

.action-btn:active {
  transform: scale(0.95);
}

.action-btn.big {
  font-size: 18px;
  padding: 14px 28px;
  margin-top: 10px;
  flex-shrink: 0;
}

#overlay {
  position: absolute;
  inset: 0;
  background: rgba(5, 12, 24, 0.82);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 24px;
}

#overlay.hidden {
  display: none;
}

.panel {
  background: linear-gradient(160deg, rgba(18, 36, 64, 0.98), rgba(10, 22, 40, 0.98));
  border: 1px solid rgba(61, 214, 255, 0.3);
  border-radius: 20px;
  padding: 20px 20px 16px;
  max-width: 360px;
  width: 100%;
  max-height: calc(100dvh - 32px - var(--safe-bottom));
  display: flex;
  flex-direction: column;
  overflow: hidden;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}

.panel h2 {
  font-size: 22px;
  margin-bottom: 8px;
  color: var(--accent);
  flex-shrink: 0;
}

.panel p {
  font-size: 14px;
  line-height: 1.45;
  color: var(--muted);
  margin-bottom: 8px;
  flex-shrink: 0;
}

.panel p.hidden {
  display: none;
}

.panel-scroll {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  margin: 0 -4px;
  padding: 0 4px;
}

.panel-scroll::-webkit-scrollbar {
  width: 6px;
}

.panel-scroll::-webkit-scrollbar-thumb {
  background: rgba(61, 214, 255, 0.35);
  border-radius: 999px;
}

.loot-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.loot-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  font-size: 14px;
}

.loot-row span:last-child {
  color: var(--gold);
  font-weight: 700;
  flex-shrink: 0;
}

.loot-total {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid rgba(61, 214, 255, 0.2);
  text-align: right;
  font-size: 16px;
  color: var(--text);
}

.loot-empty {
  text-align: center;
  color: var(--muted);
  padding: 8px 0;
}

.stats {
  text-align: left;
  background: rgba(0, 0, 0, 0.25);
  border-radius: 12px;
  padding: 12px 14px;
  margin: 4px 0 8px;
  font-size: 14px;
  line-height: 1.7;
}

.stats.hidden {
  display: none;
}

.victory-stats {
  background: linear-gradient(160deg, rgba(26, 74, 110, 0.35), rgba(10, 22, 40, 0.55));
  border: 1px solid rgba(61, 214, 255, 0.25);
}

.victory-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 12px;
}

.stat-card {
  background: rgba(0, 0, 0, 0.22);
  border-radius: 10px;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.stat-card.highlight {
  grid-column: 1 / -1;
  background: linear-gradient(135deg, rgba(26, 74, 110, 0.55), rgba(61, 214, 255, 0.12));
  border: 1px solid rgba(61, 214, 255, 0.35);
}

.stat-card.gold {
  background: rgba(255, 209, 102, 0.08);
  border: 1px solid rgba(255, 209, 102, 0.18);
}

.stat-label {
  font-size: 11px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.stat-value {
  font-size: 18px;
  font-weight: 700;
  color: var(--text);
}

.stat-card.highlight .stat-value {
  font-size: 24px;
  color: var(--accent);
}

.stat-card.gold .stat-value {
  color: var(--gold);
}

.victory-ores-title {
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.victory-ores {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.victory-ore {
  display: flex;
  justify-content: space-between;
  font-size: 13px;
  padding: 6px 8px;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.18);
}

.victory-ore.muted {
  color: var(--muted);
  justify-content: center;
}

#toast {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: rgba(10, 22, 40, 0.95);
  border: 1px solid var(--accent);
  border-radius: 12px;
  padding: 12px 20px;
  font-weight: 600;
  z-index: 50;
  pointer-events: none;
  animation: toast-in 0.25s ease;
}

#app.deck-active #toast {
  top: auto;
  bottom: calc(var(--control-deck-h) + var(--control-deck-gap) + 24px + var(--safe-bottom));
  transform: translateX(-50%);
}

#toast.hidden {
  display: none;
}

@keyframes toast-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

@media (max-width: 360px) {
  .action-btn {
    padding: 12px 14px;
    font-size: 13px;
  }
}

body.test-mode #app::before {
  content: '⚡ TEST MODE';
  position: absolute;
  top: 8px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 30;
  padding: 4px 10px;
  border-radius: 999px;
  background: rgba(255, 209, 102, 0.92);
  color: #1a1208;
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.06em;
  pointer-events: none;
}
