:root {
  --ink: #f4f4ff;
  --muted: #aab6d8;
  --panel: rgba(21, 25, 42, 0.92);
  --panel-2: rgba(36, 43, 70, 0.94);
  --line: rgba(123, 232, 223, 0.34);
  --teal: #48e5df;
  --blue: #66a9ff;
  --purple: #aa72ff;
  --gold: #ffc857;
  --pink: #ff79cf;
  --danger: #ff6287;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --mobile-control-rail: 132px;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
  background:
    radial-gradient(circle at 20% 20%, rgba(72, 229, 223, 0.15), transparent 28%),
    radial-gradient(circle at 82% 12%, rgba(170, 114, 255, 0.16), transparent 30%),
    linear-gradient(135deg, #0e1222 0%, #151b30 48%, #101522 100%);
  color: var(--ink);
  font-family: ui-monospace, "SFMono-Regular", "Cascadia Mono", "Liberation Mono", monospace;
  -webkit-user-select: none;
  user-select: none;
}

button,
input {
  font: inherit;
}

button {
  touch-action: manipulation;
}

#game-shell {
  position: relative;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  isolation: isolate;
  overflow: hidden;
}

#gameCanvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  background: #11182c;
}

#game-ui {
  position: absolute;
  inset: 0;
  pointer-events: auto;
}

#hud {
  position: absolute;
  top: calc(14px + var(--safe-top));
  right: calc(14px + var(--safe-right));
  z-index: 4;
  width: min(390px, calc(100vw - 28px));
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: stretch;
  pointer-events: none;
}

#hud.is-collapsed {
  width: auto;
}

.hud-toolbar {
  display: flex;
  justify-content: flex-end;
  pointer-events: none;
}

.hud-toggle {
  pointer-events: auto;
  display: inline-grid;
  min-width: 44px;
  min-height: 44px;
  place-items: center;
  border: 1px solid rgba(255, 255, 255, 0.16);
  border-radius: 8px;
  color: #f7ffff;
  background: linear-gradient(180deg, rgba(35, 45, 74, 0.96), rgba(14, 19, 34, 0.96));
  box-shadow:
    0 10px 24px rgba(0, 0, 0, 0.30),
    inset 0 1px 0 rgba(255, 255, 255, 0.12);
  cursor: pointer;
}

.hud-toggle:hover {
  border-color: rgba(255, 200, 87, 0.72);
  color: #fff7d7;
}

.hud-toggle-collapsed {
  display: inline-flex;
  width: auto;
  min-height: 44px;
  gap: 8px;
  align-items: center;
  padding: 4px 10px 4px 5px;
  font-weight: 700;
}

.hud-card,
.panel {
  border: 2px solid rgba(124, 244, 235, 0.32);
  background: linear-gradient(180deg, rgba(36, 44, 72, 0.95), rgba(18, 23, 40, 0.95));
  box-shadow:
    0 14px 50px rgba(0, 0, 0, 0.34),
    inset 0 1px 0 rgba(255, 255, 255, 0.10);
  backdrop-filter: blur(8px);
}

.hud-card {
  min-height: 64px;
  width: 100%;
  padding: 10px;
  border-radius: 8px;
  pointer-events: auto;
}

.hud-title {
  display: flex;
  gap: 10px;
  align-items: center;
  margin-bottom: 8px;
}

.brand-mark {
  width: 34px;
  height: 34px;
  border: 2px solid rgba(255, 200, 87, 0.75);
  border-radius: 8px;
  display: grid;
  place-items: center;
  color: var(--gold);
  text-shadow: 0 0 10px rgba(255, 200, 87, 0.72);
}

.brand-mark-mini {
  width: 28px;
  height: 28px;
  border-radius: 7px;
  font-size: 12px;
}

.brand-copy strong {
  display: block;
  color: #ffffff;
  letter-spacing: 0;
  font-size: 15px;
}

.brand-copy span {
  display: block;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.25;
}

.token-row,
.bonus-row,
.controls-row,
.inventory-grid,
.pack-grid,
.zone-grid,
.stat-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
}

.pill {
  display: inline-flex;
  align-items: center;
  min-height: 26px;
  gap: 6px;
  padding: 4px 8px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 6px;
  background: rgba(7, 11, 24, 0.45);
  color: var(--ink);
  white-space: nowrap;
}

.pill img {
  width: 18px;
  height: 18px;
  image-rendering: pixelated;
}

.pill strong {
  color: #ffffff;
}

.hint {
  color: var(--muted);
  font-size: 11px;
  line-height: 1.5;
}

.action-hint {
  color: var(--teal);
}

.tooltip {
  position: absolute;
  left: 50%;
  bottom: 110px;
  transform: translateX(-50%);
  max-width: min(560px, calc(100vw - 30px));
  padding: 9px 12px;
  border-radius: 8px;
  border: 2px solid rgba(255, 200, 87, 0.44);
  background: rgba(17, 20, 34, 0.94);
  color: #fff7dc;
  text-align: center;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.34);
}

.dialogue {
  position: absolute;
  left: 50%;
  bottom: calc(24px + var(--safe-bottom));
  transform: translateX(-50%);
  width: min(720px, calc(100vw - 28px));
  min-height: 54px;
  padding: 12px 16px;
  border: 2px solid rgba(116, 232, 225, 0.38);
  border-radius: 8px;
  background: rgba(18, 22, 38, 0.95);
  box-shadow: 0 16px 42px rgba(0, 0, 0, 0.36);
  color: #effcff;
}

#mobile-controls {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: none;
  pointer-events: none;
}

.mobile-joystick,
.mobile-action-dock,
.mobile-more-menu {
  pointer-events: auto;
}

.mobile-joystick {
  position: absolute;
  left: calc(16px + var(--safe-left));
  bottom: calc(16px + var(--safe-bottom));
  width: 112px;
  height: 112px;
  touch-action: none;
}

.mobile-joystick-base {
  position: relative;
  width: 100%;
  height: 100%;
  border: 2px solid rgba(124, 244, 235, 0.34);
  border-radius: 999px;
  background:
    radial-gradient(circle at 50% 50%, rgba(72, 229, 223, 0.18), transparent 42%),
    rgba(12, 17, 31, 0.62);
  box-shadow:
    0 14px 34px rgba(0, 0, 0, 0.36),
    inset 0 0 0 1px rgba(255, 255, 255, 0.07);
  backdrop-filter: blur(8px);
}

.mobile-joystick-knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 48px;
  height: 48px;
  margin: -24px 0 0 -24px;
  border: 2px solid rgba(255, 200, 87, 0.72);
  border-radius: 999px;
  background: linear-gradient(180deg, rgba(55, 71, 110, 0.96), rgba(24, 31, 55, 0.96));
  box-shadow:
    0 10px 22px rgba(0, 0, 0, 0.38),
    inset 0 1px 0 rgba(255, 255, 255, 0.16);
  transition: transform 90ms ease-out;
}

.mobile-action-dock {
  position: absolute;
  right: calc(16px + var(--safe-right));
  bottom: calc(16px + var(--safe-bottom));
  display: grid;
  grid-template-columns: repeat(3, minmax(58px, auto));
  gap: 8px;
}

.mobile-action-button {
  min-width: 58px;
  min-height: 48px;
  padding: 8px 10px;
  border: 1px solid rgba(118, 233, 225, 0.42);
  border-radius: 8px;
  color: #f6ffff;
  background: linear-gradient(180deg, rgba(35, 45, 74, 0.96), rgba(14, 19, 34, 0.96));
  box-shadow:
    0 10px 24px rgba(0, 0, 0, 0.30),
    inset 0 1px 0 rgba(255, 255, 255, 0.12);
  cursor: pointer;
}

.mobile-action-button.primary {
  grid-column: span 2;
  border-color: rgba(255, 200, 87, 0.62);
  background: linear-gradient(180deg, rgba(52, 116, 136, 0.98), rgba(40, 74, 96, 0.98));
}

.mobile-action-button:active {
  transform: translateY(1px);
  border-color: rgba(255, 200, 87, 0.78);
}

.mobile-more-menu {
  position: absolute;
  right: 0;
  bottom: calc(100% + 8px);
  display: none;
  grid-template-columns: repeat(2, minmax(78px, 1fr));
  gap: 8px;
  padding: 8px;
  border: 2px solid rgba(124, 244, 235, 0.32);
  border-radius: 8px;
  background: linear-gradient(180deg, rgba(36, 44, 72, 0.95), rgba(18, 23, 40, 0.95));
  box-shadow: 0 14px 40px rgba(0, 0, 0, 0.34);
  backdrop-filter: blur(8px);
}

#mobile-controls.is-menu-open .mobile-more-menu {
  display: grid;
}

#mobile-controls.is-panel-open {
  display: none;
  visibility: hidden;
  pointer-events: none;
}

#orientation-gate {
  position: absolute;
  inset: 0;
  z-index: 8;
  display: none;
  place-items: center;
  padding: 24px;
  text-align: center;
  background: rgba(9, 13, 25, 0.78);
  backdrop-filter: blur(6px);
}

.orientation-message {
  width: min(320px, calc(100vw - 48px));
  padding: 18px;
  border: 2px solid rgba(124, 244, 235, 0.36);
  border-radius: 8px;
  background: linear-gradient(180deg, rgba(36, 44, 72, 0.96), rgba(18, 23, 40, 0.96));
  box-shadow: 0 16px 42px rgba(0, 0, 0, 0.36);
}

.orientation-message strong,
.orientation-message span {
  display: block;
}

.orientation-message strong {
  color: #fff;
  font-size: 18px;
}

.orientation-message span {
  margin-top: 8px;
  color: var(--muted);
  line-height: 1.4;
}

.hidden {
  display: none !important;
}

#panel-root {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  pointer-events: auto;
}

#panel-root:empty {
  pointer-events: none;
}

.panel {
  pointer-events: auto;
  width: min(760px, calc(100vw - 28px));
  max-height: min(720px, calc(100vh - 38px));
  overflow: auto;
  border-radius: 8px;
  padding: 18px;
}

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 14px;
  margin-bottom: 14px;
}

.panel-eyebrow {
  color: var(--teal);
  font-size: 12px;
  letter-spacing: 0;
  text-transform: uppercase;
}

.panel h2 {
  margin: 3px 0 4px;
  font-size: 24px;
  line-height: 1.1;
  letter-spacing: 0;
}

.panel p {
  margin: 0 0 10px;
  color: var(--muted);
  line-height: 1.45;
}

.close-btn {
  display: grid;
  width: 34px;
  height: 34px;
  place-items: center;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 6px;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.06);
  cursor: pointer;
}

.close-btn:hover {
  border-color: rgba(255, 121, 207, 0.65);
  color: #fff;
}

.panel-section {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid rgba(255, 255, 255, 0.11);
}

.stat-card,
.inventory-slot,
.pack-card {
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 8px;
  background: rgba(9, 13, 25, 0.44);
}

.stat-card {
  flex: 1 1 160px;
  padding: 10px;
}

.stat-card span {
  display: block;
  color: var(--muted);
  font-size: 11px;
}

.stat-card strong {
  display: block;
  margin-top: 3px;
  color: #fff;
  font-size: 18px;
}

.panel-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 14px;
}

.purr-btn {
  min-height: 34px;
  border: 1px solid rgba(118, 233, 225, 0.42);
  border-radius: 7px;
  padding: 7px 11px;
  color: #f6ffff;
  background: linear-gradient(180deg, rgba(52, 116, 136, 0.98), rgba(40, 74, 96, 0.98));
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.13);
  cursor: pointer;
}

.purr-btn:hover {
  border-color: rgba(255, 200, 87, 0.7);
  color: #fff7d7;
}

.purr-btn.secondary {
  background: rgba(255, 255, 255, 0.07);
}

.purr-btn[disabled] {
  opacity: 0.55;
  cursor: not-allowed;
}

.inventory-slot {
  width: 84px;
  min-height: 90px;
  padding: 8px;
  text-align: center;
}

.inventory-slot img,
.pack-card img {
  width: 42px;
  height: 42px;
  image-rendering: pixelated;
}

.inventory-slot strong,
.pack-card strong {
  display: block;
  margin-top: 5px;
  color: #fff;
  font-size: 12px;
}

.pack-card {
  flex: 1 1 190px;
  padding: 12px;
}

.progress-shell {
  height: 18px;
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 5px;
  overflow: hidden;
  background: rgba(0, 0, 0, 0.28);
}

.progress-bar {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--teal), var(--blue), var(--purple));
}

.leader-table,
.treasury-table {
  width: 100%;
  border-collapse: collapse;
}

.leader-table th,
.leader-table td,
.treasury-table th,
.treasury-table td {
  padding: 8px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.10);
  text-align: left;
  font-size: 12px;
}

.leader-table th,
.treasury-table th {
  color: var(--teal);
  font-weight: 700;
}

.you-row {
  color: #fff7d7;
  background: rgba(255, 200, 87, 0.08);
}

@media (max-width: 780px) {
  #hud {
    top: calc(10px + var(--safe-top));
    right: calc(10px + var(--safe-right));
    width: min(340px, calc(100vw - 20px));
  }

  #hud.is-collapsed {
    width: auto;
  }

  .hud-bonuses {
    display: none;
  }

  .close-btn,
  .purr-btn {
    min-width: 44px;
    min-height: 44px;
  }

  .panel {
    padding: 14px;
  }

  .panel h2 {
    font-size: 20px;
  }
}

@media (hover: none) and (pointer: coarse) and (orientation: landscape),
  (max-width: 900px) and (max-height: 520px) {
  #mobile-controls {
    display: block;
  }

  #hud {
    top: calc(10px + var(--safe-top));
    right: calc(10px + var(--safe-right));
    width: auto;
    max-width: min(312px, calc(100vw - 180px));
    gap: 6px;
  }

  #hud:not(.is-collapsed) {
    width: min(312px, calc(100vw - 180px));
  }

  .hud-card {
    min-height: 0;
    padding: 8px;
  }

  .hud-bonuses {
    display: none;
  }

  .brand-copy span {
    display: none;
  }

  .token-row,
  .controls-row {
    gap: 5px;
  }

  .pill {
    min-height: 24px;
    padding: 3px 7px;
  }

  .tooltip {
    bottom: calc(var(--mobile-control-rail) + 54px + var(--safe-bottom));
    max-width: min(420px, calc(100vw - 260px));
  }

  .dialogue {
    bottom: calc(var(--mobile-control-rail) + var(--safe-bottom));
    width: min(520px, calc(100vw - 260px));
    min-height: 48px;
    padding: 10px 12px;
  }

  #panel-root {
    place-items: stretch;
    padding:
      calc(8px + var(--safe-top))
      calc(8px + var(--safe-right))
      calc(8px + var(--safe-bottom))
      calc(8px + var(--safe-left));
  }

  .panel {
    width: 100%;
    height: 100%;
    max-height: none;
    padding: 12px;
  }

  .panel-header {
    position: sticky;
    top: -12px;
    z-index: 1;
    padding-top: 4px;
    background: linear-gradient(180deg, rgba(36, 44, 72, 0.98), rgba(28, 35, 58, 0.96));
  }

  .close-btn,
  .purr-btn {
    min-width: 44px;
    min-height: 44px;
  }

  .leader-table,
  .treasury-table {
    min-width: 560px;
  }
}

@media (max-width: 780px) and (orientation: portrait) {
  #orientation-gate {
    display: grid;
  }

  #hud,
  #mobile-controls,
  #panel-root,
  .tooltip,
  .dialogue {
    display: none !important;
  }
}
