:root {
  color-scheme: dark;
  --bg: #181115;
  --surface: rgba(24, 18, 22, 0.94);
  --surface-strong: rgba(30, 22, 26, 0.97);
  --border: rgba(242, 192, 87, 0.36);
  --text: #faf4eb;
  --muted: #c7b5a1;
  --gold: #f6c453;
  --amber: #f08a38;
  --emerald: #3be3a4;
  --cyan: #5ce8ff;
  --crimson: #ff5c77;
  font-family: "Avenir Next", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", system-ui, -apple-system, sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}

body {
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

[hidden] {
  display: none !important;
}

#mine-canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

.topbar {
  position: fixed;
  z-index: 12;
  top: max(10px, env(safe-area-inset-top));
  left: max(12px, env(safe-area-inset-left));
  right: max(12px, env(safe-area-inset-right));
  display: flex;
  align-items: center;
  gap: 8px;
  pointer-events: none;
}

.topbar a,
.topbar button {
  pointer-events: auto;
}

.home-btn {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background: rgba(24, 18, 22, 0.88);
  border: 1px solid var(--border);
  display: grid;
  place-items: center;
}

.home-btn img {
  display: block;
  border-radius: 6px;
}

.wordmark {
  font-size: 13px;
  font-weight: 900;
  letter-spacing: 0.14em;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.75);
}

.top-actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 6px;
}

.icon-btn,
.pill-btn {
  height: 36px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: rgba(24, 18, 22, 0.88);
  color: var(--text);
  font-weight: 800;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.icon-btn {
  width: 36px;
  font-size: 15px;
}

.icon-btn[aria-pressed="true"] {
  background: var(--emerald);
  color: #10241e;
  border-color: transparent;
}

.pill-btn {
  padding: 0 11px;
  font-size: 12px;
  gap: 4px;
}

.pill-btn.accent {
  background: linear-gradient(135deg, #f5a638, #e06c28);
  color: #1a0f08;
  border-color: transparent;
}

.hud {
  position: fixed;
  z-index: 5;
  top: max(52px, calc(env(safe-area-inset-top) + 44px));
  left: 50%;
  transform: translateX(-50%);
  width: min(460px, calc(100% - 20px));
  pointer-events: none;
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.hud-stats {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 5px;
  flex-wrap: wrap;
}

.hud-pill {
  padding: 3px 9px;
  border-radius: 999px;
  background: rgba(20, 15, 19, 0.86);
  border: 1px solid rgba(255, 255, 255, 0.14);
  font-size: 11px;
  font-weight: 850;
  font-variant-numeric: tabular-nums;
}

.hud-pill.depth {
  color: var(--cyan);
}

.hud-pill.act {
  color: var(--text);
  max-width: 48%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.hud-pill.coins {
  color: var(--gold);
}

.hud-pill.daily {
  color: #ffd166;
  border-color: rgba(255, 209, 102, 0.45);
}

.hud-bars {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 5px;
  background: rgba(18, 13, 17, 0.84);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 11px;
  padding: 5px 7px;
}

.bar-item {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 10px;
  font-weight: 800;
  color: var(--muted);
  min-width: 0;
}

.bar-item b {
  color: var(--text);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.bar-track {
  flex: 1;
  min-width: 24px;
  height: 7px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.12);
  overflow: hidden;
}

.bar-track i {
  display: block;
  height: 100%;
  width: 100%;
  transition: width 0.14s ease;
}

.fill-cargo { background: linear-gradient(90deg, #f59e0b, #fbbf24); }
.fill-fuel { background: linear-gradient(90deg, #38bdf8, #5ce8ff); }
.fill-hull { background: linear-gradient(90deg, #22c55e, #4ade80); }
.fill-low { background: linear-gradient(90deg, #ef4444, #f97316) !important; }

.goal-bar {
  background: rgba(24, 18, 22, 0.82);
  border: 1px solid rgba(246, 196, 83, 0.28);
  border-radius: 9px;
  padding: 4px 9px;
  font-size: 11px;
  font-weight: 750;
  color: #ffe7b8;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.radio-banner {
  pointer-events: auto;
  cursor: pointer;
  background: rgba(16, 29, 38, 0.94);
  border: 1.5px solid rgba(92, 232, 255, 0.5);
  border-radius: 12px;
  padding: 8px 11px;
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.45);
}

.radio-banner strong {
  display: block;
  font-size: 11px;
  color: var(--cyan);
  margin-bottom: 2px;
}

.radio-banner p {
  margin: 0;
  font-size: 12px;
  line-height: 1.38;
  color: var(--text);
}

.toast {
  position: fixed;
  z-index: 8;
  left: 50%;
  top: max(160px, calc(env(safe-area-inset-top) + 150px));
  transform: translateX(-50%);
  pointer-events: none;
  max-width: calc(100% - 28px);
  text-align: center;
}

.toast:not(:empty) {
  padding: 6px 13px;
  border-radius: 999px;
  background: rgba(24, 17, 14, 0.92);
  border: 1px solid var(--gold);
  color: #fff2d1;
  font-size: 12px;
  font-weight: 800;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.5);
}

/* Bottom Controls */
.controls-bar {
  position: fixed;
  z-index: 6;
  left: max(10px, env(safe-area-inset-left));
  right: max(10px, env(safe-area-inset-right));
  bottom: max(12px, env(safe-area-inset-bottom));
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 8px;
  pointer-events: none;
}

.dpad {
  pointer-events: auto;
  display: grid;
  grid-template-columns: repeat(3, 46px);
  grid-template-rows: repeat(3, 46px);
  gap: 4px;
  touch-action: none;
}

.dpad-btn {
  border-radius: 13px;
  border: 1.5px solid rgba(246, 196, 83, 0.4);
  background: rgba(24, 18, 22, 0.78);
  color: var(--text);
  font-size: 16px;
  font-weight: 900;
  cursor: pointer;
  display: grid;
  place-items: center;
  touch-action: none;
}

.dpad-btn.active,
.dpad-btn:active {
  background: var(--amber);
  color: #1c1008;
  border-color: transparent;
}

.dpad-btn.up { grid-column: 2; grid-row: 1; }
.dpad-btn.left { grid-column: 1; grid-row: 2; }
.dpad-btn.right { grid-column: 3; grid-row: 2; }
.dpad-btn.down { grid-column: 2; grid-row: 3; }

.right-controls {
  pointer-events: auto;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 6px;
  max-width: calc(100% - 154px);
}

.supply-dock {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 5px;
}

.sup-btn {
  position: relative;
  width: 44px;
  height: 44px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.22);
  background: rgba(24, 18, 22, 0.84);
  color: var(--text);
  font-size: 18px;
  cursor: pointer;
  display: grid;
  place-items: center;
}

.sup-btn:active {
  transform: scale(0.94);
}

.sup-btn .key-tag {
  position: absolute;
  top: 2px;
  left: 4px;
  font-size: 9px;
  font-weight: 800;
  color: var(--muted);
}

.sup-btn .qty {
  position: absolute;
  bottom: 2px;
  right: 4px;
  font-size: 10px;
  font-weight: 900;
  color: var(--gold);
}

.sup-btn.empty {
  opacity: 0.45;
}

.context-row {
  display: flex;
  align-items: center;
  gap: 6px;
}

.context-btn {
  height: 40px;
  padding: 0 13px;
  border-radius: 12px;
  border: none;
  background: linear-gradient(135deg, #f6c453, #f08a38);
  color: #1d1007;
  font-size: 12.5px;
  font-weight: 900;
  cursor: pointer;
  box-shadow: 0 6px 18px rgba(240, 138, 56, 0.35);
}

.winch-quick-btn {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  border: 1px solid rgba(255, 92, 119, 0.65);
  background: rgba(52, 16, 24, 0.9);
  color: #ffd3da;
  font-size: 17px;
  cursor: pointer;
}

@media (hover: hover) and (pointer: fine) and (min-width: 820px) {
  .dpad {
    opacity: 0.72;
  }
}

/* Overlays */
.overlay-root {
  position: fixed;
  z-index: 10;
  inset: 0;
  background: rgba(12, 8, 10, 0.66);
  backdrop-filter: blur(5px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: max(58px, env(safe-area-inset-top)) 12px max(16px, env(safe-area-inset-bottom));
  overflow-y: auto;
  touch-action: pan-y;
}

.overlay-card {
  width: min(540px, 100%);
  max-height: 84dvh;
  overflow-y: auto;
  background: var(--surface-strong);
  border: 1.5px solid var(--border);
  border-radius: 20px;
  padding: 18px 16px;
  box-shadow: 0 24px 54px rgba(0, 0, 0, 0.62);
}

.eyebrow {
  margin: 0 0 4px;
  font-size: 11px;
  font-weight: 900;
  letter-spacing: 0.14em;
  color: var(--gold);
  text-transform: uppercase;
}

.overlay-card h1,
.overlay-card h2 {
  margin: 0 0 6px;
  font-size: clamp(22px, 5.5vw, 32px);
  line-height: 1.1;
  font-weight: 900;
}

.sub {
  margin: 0 0 12px;
  font-size: 13px;
  line-height: 1.45;
  color: var(--muted);
}

.stat-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  margin-bottom: 14px;
}

.stat-box {
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 11px;
  padding: 7px 6px;
  text-align: center;
}

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

.stat-box b {
  font-size: 14px;
  color: var(--gold);
  font-variant-numeric: tabular-nums;
}

.btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}

.btn {
  flex: 1 1 130px;
  height: 44px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.16);
  background: rgba(255, 255, 255, 0.07);
  color: var(--text);
  font-size: 13.5px;
  font-weight: 850;
  cursor: pointer;
}

.btn.primary {
  background: linear-gradient(135deg, #f6c453, #e67e22);
  color: #1c1006;
  border: none;
}

.btn.cyan {
  background: linear-gradient(135deg, #38bdf8, #3be3a4);
  color: #0b2226;
  border: none;
}

.btn.danger {
  border-color: rgba(255, 92, 119, 0.5);
  color: #ffb8c4;
}

.tabs {
  display: flex;
  gap: 5px;
  overflow-x: auto;
  margin: 0 0 12px;
  padding-bottom: 2px;
}

.tab-btn {
  flex: 1 0 auto;
  padding: 7px 10px;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: rgba(255, 255, 255, 0.05);
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  cursor: pointer;
}

.tab-btn.active {
  background: var(--gold);
  color: #1c1106;
  border-color: transparent;
}

.list-col {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.item-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 11px;
  border-radius: 13px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.item-info {
  min-width: 0;
  flex: 1;
}

.item-info strong {
  display: block;
  font-size: 13.5px;
  margin-bottom: 2px;
}

.item-info small {
  display: block;
  font-size: 11.5px;
  color: var(--muted);
  line-height: 1.35;
}

.item-act {
  flex: 0 0 auto;
  padding: 7px 11px;
  border-radius: 10px;
  border: none;
  background: linear-gradient(135deg, #f6c453, #e67e22);
  color: #1c1006;
  font-size: 12px;
  font-weight: 900;
  cursor: pointer;
  white-space: nowrap;
}

.item-act:disabled {
  opacity: 0.42;
  cursor: default;
  background: rgba(255, 255, 255, 0.12);
  color: var(--muted);
}

.relic-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(112px, 1fr));
  gap: 7px;
  margin-top: 8px;
}

.relic-tile {
  padding: 8px 6px;
  border-radius: 11px;
  text-align: center;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.09);
  font-size: 11px;
}

.relic-tile.found {
  border-color: rgba(246, 196, 83, 0.55);
  background: rgba(246, 196, 83, 0.12);
}

.relic-tile .r-ico {
  font-size: 20px;
  display: block;
  margin-bottom: 3px;
}

.milestone-row {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 6px;
  margin-bottom: 8px;
}

.milestone-pill {
  padding: 6px 8px;
  border-radius: 9px;
  font-size: 11px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.1);
  color: var(--muted);
}

.milestone-pill.on {
  border-color: rgba(59, 227, 164, 0.55);
  color: var(--emerald);
}

.log-card {
  padding: 10px 12px;
  border-radius: 12px;
  background: rgba(16, 29, 38, 0.72);
  border: 1px solid rgba(92, 232, 255, 0.28);
}

.log-card strong {
  display: block;
  font-size: 12.5px;
  color: var(--cyan);
  margin-bottom: 3px;
}

.log-card p {
  margin: 0;
  font-size: 12px;
  line-height: 1.45;
  color: var(--text);
}

.guide-block {
  padding: 10px 12px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.09);
}

.guide-block h3 {
  margin: 0 0 4px;
  font-size: 13.5px;
  color: var(--gold);
}

.guide-block p {
  margin: 0;
  font-size: 12.5px;
  line-height: 1.48;
  color: var(--muted);
}

.controls-note {
  margin: 12px 0 0;
  font-size: 11px;
  color: var(--muted);
  text-align: center;
}

@media (max-width: 420px) {
  .dpad {
    grid-template-columns: repeat(3, 42px);
    grid-template-rows: repeat(3, 42px);
  }
  .sup-btn {
    width: 40px;
    height: 40px;
    font-size: 16px;
  }
  .stat-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}
