:root {
  --bg-deep: #071017;
  --panel-bg: #0d1a24;
  --panel-border: #203646;
  --steel-light: #8ea5b5;
  --text-main: #e6f0f5;
  --text-muted: #7f97a8;
  --accent-amber: #f0a830;
  --accent-cyan: #38d9e8;
  --accent-green: #2fe088;
  --accent-red: #f2545b;
}

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

html, body {
  height: 100%;
  background: radial-gradient(circle at 50% 0%, #102432 0%, var(--bg-deep) 70%);
  color: var(--text-main);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", Roboto, Helvetica, Arial, sans-serif;
  overflow: hidden;
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

.mono {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-variant-numeric: tabular-nums;
}

.app-shell {
  max-width: 1000px;
  height: 100vh;
  height: 100dvh;
  margin: 0 auto;
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 10px;
}

/* HUD Bar */
.hud-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: rgba(13, 26, 36, 0.92);
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  padding: 8px 14px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.38);
}

.brand-block {
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-width: 128px;
}

.deck-tag {
  font-size: 10px;
  letter-spacing: 0.16em;
  color: var(--accent-amber);
  font-weight: 700;
}

.game-title {
  font-size: 16px;
  letter-spacing: 0.08em;
  font-weight: 800;
  color: var(--text-main);
  line-height: 1.1;
}

.telemetry-group {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  justify-content: center;
  flex-wrap: wrap;
}

.metric-card {
  background: rgba(7, 16, 23, 0.72);
  border: 1px solid #1a2f3d;
  border-radius: 7px;
  padding: 5px 10px;
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 96px;
}

.metric-card.warning {
  border-color: var(--accent-amber);
  box-shadow: inset 0 0 12px rgba(240, 168, 48, 0.14);
}

.metric-card.danger {
  border-color: var(--accent-red);
  box-shadow: inset 0 0 14px rgba(242, 84, 91, 0.25);
  animation: pulse-danger 0.8s ease-in-out infinite;
}

@keyframes pulse-danger {
  0%, 100% { border-color: var(--accent-red); }
  50% { border-color: #ff989c; }
}

.metric-label {
  font-size: 9px;
  letter-spacing: 0.13em;
  color: var(--text-muted);
  font-weight: 700;
}

.metric-value {
  font-size: 15px;
  font-weight: 700;
  color: var(--text-main);
  line-height: 1.1;
}

.metric-card.warning .metric-value { color: var(--accent-amber); }
.metric-card.danger .metric-value { color: #ff777d; }

.relay-pips {
  display: flex;
  align-items: center;
  gap: 5px;
}

.pip {
  font-size: 10px;
  font-weight: 800;
  padding: 2px 5px;
  border-radius: 4px;
  background: #142633;
  color: #607c8f;
  border: 1px solid #223b4d;
  transition: all 0.2s ease;
}

.pip.powered {
  background: rgba(47, 224, 136, 0.2);
  color: var(--accent-green);
  border-color: var(--accent-green);
  box-shadow: 0 0 8px rgba(47, 224, 136, 0.35);
}

.hull-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
}

.hull-bar {
  width: 66px;
  height: 9px;
  background: #142430;
  border: 1px solid #253d4e;
  border-radius: 4px;
  overflow: hidden;
}

.hull-fill {
  height: 100%;
  width: 100%;
  background: linear-gradient(90deg, #27c784, #4ceea3);
  transition: width 0.2s ease, background 0.2s ease;
}

.hull-fill.mid {
  background: linear-gradient(90deg, #d88f21, #f5be49);
}

.hull-fill.low {
  background: linear-gradient(90deg, #c9343b, #ff6269);
}

.boost-bar {
  width: 68px;
  height: 9px;
  background: #142430;
  border: 1px solid #253d4e;
  border-radius: 4px;
  overflow: hidden;
  margin-top: 3px;
}

.boost-fill {
  height: 100%;
  width: 100%;
  background: linear-gradient(90deg, #1b9bb8, #44e6f5);
  transition: width 0.08s linear;
}

.boost-fill.draining {
  background: linear-gradient(90deg, #27c784, #7afff0);
}

.action-group {
  display: flex;
  align-items: center;
  gap: 8px;
}

.hud-btn {
  border: 1px solid var(--panel-border);
  border-radius: 7px;
  padding: 7px 12px;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.04em;
  cursor: pointer;
  transition: transform 0.1s ease, background 0.15s ease, border-color 0.15s ease;
}

.hud-btn:active {
  transform: translateY(1px);
}

.hud-btn.primary {
  background: linear-gradient(180deg, #e59b25 0%, #c67b13 100%);
  color: #0b141a;
  border-color: #f6bc52;
}

.hud-btn.primary:hover {
  background: linear-gradient(180deg, #f2aa36 0%, #d48619 100%);
}

.hud-btn.secondary {
  background: #132533;
  color: var(--steel-light);
  border-color: #264053;
}

.hud-btn.secondary:hover {
  background: #1a3142;
  color: var(--text-main);
}

.hud-btn.large {
  padding: 10px 24px;
  font-size: 14px;
  letter-spacing: 0.06em;
}

/* Stage */
.stage-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 10;
  max-height: calc(100vh - 156px);
  background: #061017;
  border: 2px solid #253c4d;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.55);
}

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

.status-banner {
  background: rgba(11, 23, 33, 0.9);
  border: 1px solid rgba(56, 217, 232, 0.3);
  color: #d6edf4;
  font-size: 12px;
  font-weight: 600;
  padding: 5px 14px;
  border-radius: 7px;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  letter-spacing: 0.02em;
  transition: border-color 0.2s ease, color 0.2s ease;
}

.status-banner.alert {
  border-color: rgba(242, 84, 91, 0.8);
  color: #ffc5c8;
}

.status-banner.success {
  border-color: rgba(47, 224, 136, 0.85);
  color: #c9ffe2;
}

/* Overlay */
.overlay {
  position: absolute;
  inset: 0;
  background: rgba(5, 13, 19, 0.78);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 10;
  transition: opacity 0.2s ease;
}

.overlay.hidden {
  display: none;
}

.overlay-card {
  width: 100%;
  max-width: 470px;
  background: linear-gradient(180deg, #10212e 0%, #0a1620 100%);
  border: 1px solid #2b485d;
  border-radius: 12px;
  padding: 24px 26px;
  text-align: center;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.6);
}

.overlay.win .overlay-card {
  border-color: var(--accent-green);
  box-shadow: 0 0 36px rgba(47, 224, 136, 0.2);
}

.overlay.lose .overlay-card {
  border-color: var(--accent-red);
  box-shadow: 0 0 36px rgba(242, 84, 91, 0.22);
}

.overlay-badge {
  display: inline-block;
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.16em;
  padding: 4px 10px;
  border-radius: 999px;
  background: rgba(240, 168, 48, 0.16);
  color: var(--accent-amber);
  border: 1px solid rgba(240, 168, 48, 0.4);
  margin-bottom: 10px;
}

.overlay.win .overlay-badge {
  background: rgba(47, 224, 136, 0.16);
  color: var(--accent-green);
  border-color: rgba(47, 224, 136, 0.45);
}

.overlay.lose .overlay-badge {
  background: rgba(242, 84, 91, 0.16);
  color: #ff7e84;
  border-color: rgba(242, 84, 91, 0.45);
}

.overlay-title {
  font-size: 24px;
  letter-spacing: 0.06em;
  margin-bottom: 10px;
  color: #f1f7fa;
}

.overlay-desc {
  font-size: 13.5px;
  line-height: 1.55;
  color: #b7ccd9;
  margin-bottom: 18px;
}

.overlay-desc strong {
  color: #ffffff;
}

.overlay-stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 18px;
}

.overlay-stats.hidden {
  display: none;
}

.stat-item {
  background: rgba(7, 16, 23, 0.72);
  border: 1px solid #1f3545;
  border-radius: 8px;
  padding: 10px 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.stat-label {
  font-size: 10px;
  letter-spacing: 0.14em;
  color: var(--text-muted);
  font-weight: 700;
}

.stat-val {
  font-size: 18px;
  font-weight: 800;
  color: var(--text-main);
}

.stat-val.rank-s {
  color: #ffd644;
  text-shadow: 0 0 12px rgba(255, 214, 68, 0.45);
}

.stat-val.rank-a {
  color: var(--accent-cyan);
}

.stat-val.rank-b {
  color: var(--accent-green);
}

.stat-val.rank-c {
  color: var(--accent-amber);
}

.overlay-controls-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  margin-bottom: 20px;
  text-align: left;
  background: rgba(6, 14, 20, 0.6);
  border: 1px solid #1b303f;
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 12px;
  color: #b4cad8;
}

.overlay-controls-grid.hidden {
  display: none;
}

.overlay-controls-grid > div {
  display: flex;
  align-items: center;
  gap: 5px;
  flex-wrap: wrap;
}

.overlay-actions {
  display: flex;
  justify-content: center;
}

.overlay-hint {
  margin-top: 10px;
  font-size: 11px;
  color: var(--text-muted);
}

/* Footer controls bar */
.controls-bar {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 20px;
  flex-wrap: wrap;
  font-size: 12px;
  color: var(--text-muted);
  padding: 2px 6px;
}

kbd {
  display: inline-block;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11px;
  font-weight: 700;
  color: #dcebf3;
  background: #152836;
  border: 1px solid #2d4c61;
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 1px 5px;
  margin: 0 1px;
}

/* Mobile Touch Controls (hidden on desktop mouse screens) */
.touch-controls {
  display: none;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 6px 12px;
  touch-action: none;
}

.joy-base {
  position: relative;
  width: 122px;
  height: 122px;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(22, 46, 62, 0.72) 0%, rgba(10, 22, 31, 0.88) 72%);
  border: 2px solid #2a475c;
  box-shadow: inset 0 0 18px rgba(0, 0, 0, 0.55), 0 6px 18px rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: none;
  flex-shrink: 0;
}

.joy-base.active {
  border-color: var(--accent-cyan);
  box-shadow: inset 0 0 18px rgba(56, 217, 232, 0.18), 0 0 14px rgba(56, 217, 232, 0.25);
}

.joy-ring {
  position: absolute;
  inset: 14px;
  border-radius: 50%;
  border: 1px dashed rgba(142, 165, 181, 0.28);
  pointer-events: none;
}

.joy-knob {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #f5be52 0%, #cf841a 75%);
  border: 2px solid #ffe09e;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5);
  pointer-events: none;
  will-change: transform;
}

.touch-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  touch-action: none;
}

.touch-btn {
  width: 74px;
  height: 74px;
  border-radius: 50%;
  border: 2px solid #2b4a5e;
  background: rgba(15, 32, 44, 0.9);
  color: var(--text-main);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  cursor: pointer;
  touch-action: none;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.42);
  transition: transform 0.08s ease, border-color 0.15s ease, opacity 0.15s ease;
}

.touch-btn-main {
  font-size: 14px;
  font-weight: 800;
  line-height: 1.1;
}

.touch-btn-sub {
  font-size: 10px;
  color: var(--text-muted);
  font-weight: 600;
}

.touch-btn.boost {
  border-color: rgba(56, 217, 232, 0.65);
  background: radial-gradient(circle at 50% 30%, rgba(28, 86, 108, 0.92), rgba(11, 29, 40, 0.95));
}

.touch-btn.boost .touch-btn-main {
  color: #7df3ff;
}

.touch-btn.boost.cooldown {
  opacity: 0.52;
  border-color: #294657;
}

.touch-btn.overdrive {
  border-color: rgba(47, 224, 136, 0.6);
  background: radial-gradient(circle at 50% 30%, rgba(20, 74, 52, 0.9), rgba(10, 26, 24, 0.95));
}

.touch-btn.overdrive .touch-btn-main {
  color: #7affbe;
}

.touch-btn.active {
  transform: scale(0.93);
  box-shadow: 0 0 18px rgba(56, 217, 232, 0.4);
}

.touch-btn.overdrive.active {
  box-shadow: 0 0 18px rgba(47, 224, 136, 0.45);
}

/* Mobile & Touch Responsive Layout */
@media (max-width: 820px), (max-height: 460px), (pointer: coarse) {
  .controls-bar {
    display: none;
  }

  .touch-controls {
    display: flex;
  }

  .overlay {
    position: fixed;
    inset: 0;
    padding: 14px;
    z-index: 30;
  }

  .overlay-card {
    max-height: calc(100dvh - 28px);
    overflow-y: auto;
    padding: 18px 18px;
  }
}

@media (max-width: 820px) {
  .app-shell {
    justify-content: flex-start;
    padding: calc(8px + env(safe-area-inset-top)) 10px calc(10px + env(safe-area-inset-bottom));
    gap: 8px;
  }

  .hud-bar {
    flex-wrap: wrap;
    padding: 8px 10px;
    gap: 8px;
  }

  .brand-block {
    min-width: auto;
  }

  .game-title {
    font-size: 14px;
  }

  .action-group {
    margin-left: auto;
    gap: 6px;
  }

  .hud-btn {
    padding: 6px 10px;
    font-size: 11.5px;
  }

  .telemetry-group {
    order: 3;
    flex: 1 0 100%;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 6px;
  }

  .metric-card {
    min-width: 0;
    padding: 5px 8px;
  }

  .metric-value {
    font-size: 13.5px;
  }

  .hull-bar {
    width: auto;
    flex: 1;
  }

  .boost-bar {
    width: 100%;
    flex: none;
  }

  .status-banner {
    font-size: 11.5px;
    padding: 5px 10px;
    white-space: normal;
    line-height: 1.35;
  }

  .stage-wrap {
    max-height: none;
    flex-shrink: 0;
  }

  .touch-controls {
    flex: 1;
    min-height: 132px;
    max-height: 210px;
    padding: 6px 10px;
  }

  .overlay-title {
    font-size: 20px;
  }

  .overlay-desc {
    font-size: 13px;
    margin-bottom: 14px;
  }

  .overlay-controls-grid {
    grid-template-columns: 1fr;
    gap: 6px;
    font-size: 11.5px;
    margin-bottom: 14px;
  }
}

/* Compact Landscape Mobile (e.g. 844x390) */
@media (max-height: 520px) and (pointer: coarse), (max-height: 460px) {
  .app-shell {
    padding: 6px 10px;
    gap: 6px;
    justify-content: flex-start;
  }

  .hud-bar {
    flex-wrap: nowrap;
    padding: 4px 8px;
    gap: 8px;
  }

  .brand-block {
    display: none;
  }

  .telemetry-group {
    order: 0;
    flex: 1;
    display: flex;
    flex-wrap: nowrap;
    gap: 6px;
  }

  .metric-card {
    flex: 1;
    padding: 3px 7px;
    min-width: 0;
  }

  .metric-label {
    font-size: 8px;
  }

  .metric-value {
    font-size: 12px;
  }

  .status-banner {
    font-size: 11px;
    padding: 3px 8px;
    white-space: nowrap;
  }

  .stage-wrap {
    flex: 1;
    min-height: 0;
    width: auto;
    max-width: 100%;
    margin: 0 auto;
  }

  .touch-controls {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 8px;
    padding: 0 14px;
    pointer-events: none;
    z-index: 20;
    min-height: 0;
    max-height: none;
  }

  .joy-base,
  .touch-actions {
    pointer-events: auto;
    opacity: 0.84;
  }

  .joy-base {
    width: 96px;
    height: 96px;
  }

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

  .touch-btn {
    width: 62px;
    height: 62px;
  }

  .touch-btn-main {
    font-size: 12px;
  }

  .touch-btn-sub {
    display: none;
  }
}
