:root {
  --bg: #080b18;
  --panel: rgba(19, 25, 48, .82);
  --line: rgba(155, 190, 255, .16);
  --text: #f6f5ff;
  --muted: #a9b0cb;
  --violet: #9d7cff;
  --cyan: #62e7ff;
  --pink: #ff77c8;
  --gold: #ffd369;
  --green: #8cff98;
  --red: #ff667f;
  --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);
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  min-height: 100%;
  background: radial-gradient(circle at 50% -20%, #1c2050 0, #0b1023 32%, var(--bg) 70%);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  overscroll-behavior: none;
}
body { overflow-x: hidden; }
body.in-game { overflow: hidden; position: fixed; inset: 0; width: 100%; height: 100%; }
.hidden { display: none !important; }

.aurora { position: fixed; border-radius: 50%; filter: blur(90px); opacity: .13; pointer-events: none; }
.a1 { width: 480px; height: 480px; background: var(--violet); left: -250px; top: 120px; }
.a2 { width: 520px; height: 520px; background: var(--cyan); right: -320px; bottom: -100px; }

.topbar {
  height: 72px;
  padding: 0 clamp(18px, 4vw, 58px);
  display: flex;
  align-items: center;
  gap: 20px;
  border-bottom: 1px solid var(--line);
  position: relative;
  z-index: 3;
}
.brand { display: flex; gap: 12px; align-items: center; color: white; text-decoration: none; letter-spacing: .12em; }
.brand-mark { width: 38px; height: 38px; display: grid; place-items: center; border: 1px solid var(--violet); border-radius: 12px; box-shadow: 0 0 25px rgba(157, 124, 255, .35); color: var(--cyan); }
.brand b { display: block; font-size: 14px; }
.brand small { display: block; color: var(--muted); font-size: 9px; margin-top: 2px; }
.room-badge { margin-left: auto; padding: 8px 12px; border: 1px solid var(--line); border-radius: 99px; color: var(--muted); font-size: 12px; }
.room-badge strong { color: var(--cyan); letter-spacing: .15em; }
.icon-button { background: transparent; border: 1px solid var(--line); width: 42px; height: 42px; padding: 0; }

.glass { background: linear-gradient(145deg, rgba(26, 32, 61, .9), rgba(12, 17, 35, .82)); border: 1px solid var(--line); box-shadow: 0 24px 70px rgba(0, 0, 0, .25); backdrop-filter: blur(14px); }
button, input { font: inherit; }
button { border: 1px solid var(--line); background: #171e3a; color: white; border-radius: 12px; padding: 12px 16px; cursor: pointer; transition: transform .18s, border-color .18s, background .18s; -webkit-tap-highlight-color: transparent; }
button:hover { transform: translateY(-1px); border-color: rgba(98, 231, 255, .48); }
button:active { transform: scale(.97); }
button:disabled { opacity: .5; cursor: default; transform: none; }
.primary { border: 0; background: linear-gradient(120deg, var(--violet), #7058df); box-shadow: 0 10px 30px rgba(112, 88, 223, .28); font-weight: 800; }
input { width: 100%; background: #0d1228; color: white; border: 1px solid var(--line); border-radius: 12px; padding: 14px; outline: none; font-size: 16px; }
input:focus { border-color: var(--cyan); box-shadow: 0 0 0 3px rgba(98, 231, 255, .08); }

.landing { min-height: calc(100vh - 72px); display: grid; grid-template-columns: minmax(0, 1.25fr) minmax(330px, .75fr); gap: 8vw; align-items: center; padding: 7vh clamp(22px, 7vw, 110px); }
.hero-copy h1 { font-size: clamp(48px, 7vw, 92px); line-height: .95; margin: 12px 0 26px; letter-spacing: -.055em; }
.hero-copy h1 em { font-style: normal; color: transparent; background: linear-gradient(90deg, var(--cyan), var(--violet), var(--pink)); background-clip: text; }
.lead { max-width: 690px; font-size: clamp(17px, 1.65vw, 23px); line-height: 1.55; color: var(--muted); }
.eyebrow { font-size: 11px; letter-spacing: .18em; color: var(--cyan); font-weight: 800; }
.feature-row { display: flex; flex-wrap: wrap; gap: 9px; margin-top: 32px; }
.feature-row span { border: 1px solid var(--line); background: rgba(14, 20, 41, .7); padding: 9px 12px; border-radius: 99px; font-size: 12px; }
.join-card { border-radius: 24px; padding: 30px; display: grid; gap: 12px; position: relative; }
.step { position: absolute; right: 24px; top: 20px; font-size: 48px; font-weight: 900; color: rgba(255, 255, 255, .045); }
.join-card label { font-weight: 800; }
.join-card small, .muted { color: var(--muted); line-height: 1.45; }
.join-row { display: grid; grid-template-columns: 1fr auto; gap: 8px; }
.join-row input { text-transform: uppercase; letter-spacing: .18em; font-weight: 900; }
.divider { height: 1px; background: var(--line); margin: 13px 0; position: relative; }
.divider span { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); background: #141a33; padding: 0 10px; color: var(--muted); font-size: 10px; }
.error { color: var(--red); min-height: 20px; margin: 0; }
.install-button { border-color: rgba(98, 231, 255, .36); background: rgba(98, 231, 255, .08); }

.lobby { min-height: calc(100vh - 72px); display: grid; grid-template-columns: minmax(360px, 650px) minmax(280px, 430px); gap: 28px; justify-content: center; align-items: center; padding: 50px 24px; }
.lobby-card, .rules-card { border-radius: 24px; padding: 34px; }
.lobby-card { text-align: center; }
.lobby-card h2 { font-size: 30px; }
.lobby-card h2 strong { display: block; font-size: clamp(54px, 9vw, 92px); letter-spacing: .18em; color: var(--cyan); text-shadow: 0 0 35px rgba(98, 231, 255, .25); }
.lobby-players { display: grid; grid-template-columns: repeat(2, 1fr); gap: 9px; margin: 24px 0; }
.lobby-player { padding: 13px; border: 1px solid var(--line); border-radius: 12px; text-align: left; display: flex; align-items: center; gap: 9px; }
.player-dot { width: 10px; height: 10px; border-radius: 50%; box-shadow: 0 0 14px currentColor; }
.lobby-player small { margin-left: auto; }
.rules-card li { margin: 14px 0; color: var(--muted); line-height: 1.45; }

.game { padding: 16px clamp(10px, 2vw, 28px) 26px; }
.hud { max-width: 1500px; margin: 0 auto 12px; border-radius: 16px; padding: 10px 16px; display: grid; grid-template-columns: repeat(4, minmax(90px, 1fr)) minmax(140px, 1.4fr); gap: 10px; }
.hud > div { border-right: 1px solid var(--line); padding: 3px 14px; }
.hud > div:last-child { border: 0; }
.hud small { display: block; color: var(--muted); font-size: 9px; text-transform: uppercase; letter-spacing: .12em; }
.hud strong { font-size: 18px; }
.meter { height: 8px; background: #080d1e; border-radius: 99px; margin-top: 8px; overflow: hidden; }
.meter i { display: block; width: 100%; height: 100%; background: linear-gradient(90deg, var(--pink), var(--green)); border-radius: inherit; transition: width .2s; }

.fullscreen-button, .mobile-info-button { display: none; }
.game-grid { max-width: 1500px; margin: auto; display: grid; grid-template-columns: 190px minmax(0, 1fr) 220px; gap: 12px; }
.side-panel, .right-panel { border-radius: 18px; padding: 15px; }
.side-panel h3, .right-panel h3 { font-size: 12px; letter-spacing: .1em; text-transform: uppercase; color: var(--muted); margin: 8px 0 12px; }
.tower-choice { width: 100%; display: grid; grid-template-columns: 34px 1fr; grid-template-rows: auto auto; text-align: left; margin-bottom: 8px; padding: 10px; }
.tower-choice span { grid-row: 1 / 3; font-size: 23px; }
.tower-choice b { font-size: 12px; }
.tower-choice small { color: var(--muted); font-size: 10px; }
.tower-choice.active { border-color: var(--cyan); background: rgba(98, 231, 255, .1); box-shadow: 0 0 0 1px rgba(98, 231, 255, .12) inset; }
.help { border-top: 1px solid var(--line); margin-top: 18px; padding-top: 16px; font-size: 11px; color: var(--muted); line-height: 1.5; }

.arena-wrap { position: relative; border-radius: 18px; overflow: hidden; min-width: 0; align-self: start; background: #080d20; }
.arena-wrap canvas { display: block; width: 100%; height: auto; aspect-ratio: 16 / 10; touch-action: none; user-select: none; -webkit-user-select: none; }
.wave-banner { position: absolute; left: 50%; top: 18%; transform: translateX(-50%); font-size: clamp(35px, 6vw, 75px); font-weight: 1000; letter-spacing: .02em; text-shadow: 0 0 28px var(--violet); animation: pop 1.6s both; pointer-events: none; }
.respawn { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); background: rgba(7, 10, 24, .88); border: 1px solid var(--pink); padding: 18px 25px; border-radius: 15px; font-size: 20px; pointer-events: none; }
.orientation-hint { display: none; }
.score-row { display: grid; grid-template-columns: 26px 1fr auto; gap: 5px; align-items: center; padding: 9px 6px; border-bottom: 1px solid var(--line); font-size: 11px; }
.score-row.me { background: rgba(157, 124, 255, .09); }
.score-rank { color: var(--muted); }
.score-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.score-value { color: var(--gold); font-weight: 900; }
.event-log { min-height: 150px; }
.event-item { padding: 8px 0; border-bottom: 1px solid var(--line); font-size: 10px; line-height: 1.35; color: var(--muted); }
.keymap { display: grid; gap: 6px; border-top: 1px solid var(--line); padding-top: 13px; margin-top: 12px; font-size: 9px; color: var(--muted); }
kbd { border: 1px solid var(--line); background: #080d1e; border-radius: 4px; padding: 2px 5px; color: white; }
.close-info-button { display: none; }
.mobile-controls { display: none; }

.modal { position: fixed; inset: 0; background: rgba(3, 5, 13, .78); backdrop-filter: blur(10px); z-index: 30; display: grid; place-items: center; padding: max(20px, var(--safe-top)) max(20px, var(--safe-right)) max(20px, var(--safe-bottom)) max(20px, var(--safe-left)); }
.modal-card { width: min(560px, 100%); max-height: 92dvh; overflow: auto; border-radius: 24px; padding: 30px; text-align: center; }
.modal-card h2 { font-size: 32px; margin: 8px; }
.modal-card > p:not(.eyebrow) { color: var(--muted); font-size: 17px; }
.options { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 24px; }
.options button { min-height: 58px; }
.trophy { font-size: 60px; color: var(--gold); }
.final-row { display: grid; grid-template-columns: 35px 1fr auto; padding: 12px; border-bottom: 1px solid var(--line); text-align: left; }
.end-card .primary { margin-top: 20px; }
.toast { position: fixed; left: 50%; bottom: max(24px, calc(var(--safe-bottom) + 12px)); transform: translateX(-50%); z-index: 40; background: #11182f; border: 1px solid var(--cyan); border-radius: 12px; padding: 12px 18px; box-shadow: 0 15px 55px black; font-weight: 700; font-size: 12px; max-width: calc(100vw - 30px); text-align: center; }
.toast.bad { border-color: var(--red); }

@keyframes pop {
  0% { opacity: 0; transform: translate(-50%, -20px) scale(.7); }
  25% { opacity: 1; transform: translate(-50%, 0) scale(1); }
  80% { opacity: 1; }
  100% { opacity: 0; transform: translate(-50%, -10px) scale(1.05); }
}

@media (max-width: 1050px) {
  .game-grid { grid-template-columns: 150px minmax(0, 1fr); }
  .right-panel { grid-column: 1 / -1; display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
  .right-panel h3:nth-of-type(2), .event-log { grid-column: 2; }
  .keymap { display: none; }
  .landing { gap: 35px; padding-left: 35px; padding-right: 35px; }
}

@media (max-width: 760px), (max-height: 520px) and (max-width: 1000px), (pointer: coarse) and (max-height: 900px) {
  html, body { width: 100%; min-height: 100%; }
  .topbar { height: 62px; padding: var(--safe-top) max(12px, var(--safe-right)) 0 max(12px, var(--safe-left)); }
  .brand b { font-size: 11px; }
  .landing { min-height: auto; display: block; padding: 38px max(16px, var(--safe-right)) calc(28px + var(--safe-bottom)) max(16px, var(--safe-left)); }
  .hero-copy h1 { font-size: clamp(43px, 13vw, 58px); }
  .lead { font-size: 16px; }
  .feature-row { margin-top: 22px; }
  .join-card { margin-top: 32px; padding: 22px; }
  .join-row { grid-template-columns: 1fr; }
  .join-row button, .join-card .primary { min-height: 52px; }
  .lobby { display: block; padding: 22px max(14px, var(--safe-right)) calc(26px + var(--safe-bottom)) max(14px, var(--safe-left)); }
  .lobby-card, .rules-card { padding: 24px 18px; }
  .rules-card { margin-top: 14px; }
  .lobby-players { grid-template-columns: 1fr; }

  body.in-game .topbar { display: none; }
  body.in-game main { position: fixed; inset: 0; width: 100%; height: 100%; }
  .game { position: fixed; inset: 0; width: 100%; height: 100%; padding: 0; background: #080b18; touch-action: none; }
  .game-grid { position: absolute; inset: 0; display: block; max-width: none; }
  .arena-wrap { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; border-radius: 0; box-shadow: none; backdrop-filter: none; }
  .arena-wrap canvas { width: 100%; height: 100%; aspect-ratio: auto; }

  .hud {
    position: fixed;
    z-index: 12;
    top: max(6px, var(--safe-top));
    left: max(6px, var(--safe-left));
    right: max(6px, var(--safe-right));
    margin: 0;
    padding: 5px 8px;
    grid-template-columns: repeat(4, 1fr);
    gap: 2px;
    border-radius: 13px;
    background: rgba(10, 15, 32, .72);
    backdrop-filter: blur(8px);
    box-shadow: 0 10px 35px rgba(0, 0, 0, .2);
  }
  .hud > div { padding: 1px 5px; text-align: center; }
  .hud small { font-size: 7px; letter-spacing: .08em; }
  .hud strong { font-size: 13px; white-space: nowrap; }
  .hp-box { grid-column: 1 / -1; border-right: 0 !important; display: grid; grid-template-columns: auto 1fr; align-items: center; gap: 8px; padding-top: 3px !important; }
  .meter { margin: 0; height: 6px; }

  .fullscreen-button, .mobile-info-button {
    display: grid;
    place-items: center;
    position: fixed;
    z-index: 14;
    top: calc(max(6px, var(--safe-top)) + 63px);
    width: 40px;
    height: 40px;
    padding: 0;
    border-radius: 50%;
    background: rgba(10, 15, 32, .74);
    backdrop-filter: blur(8px);
  }
  .fullscreen-button { right: max(8px, var(--safe-right)); font-size: 19px; }
  .fullscreen-button span { display: none; }
  .mobile-info-button { left: max(8px, var(--safe-left)); font-size: 17px; }

  .side-panel {
    position: fixed;
    z-index: 13;
    top: calc(max(6px, var(--safe-top)) + 62px);
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    gap: 5px;
    width: auto;
    padding: 4px;
    margin: 0;
    border-radius: 15px;
    background: rgba(10, 15, 32, .72);
    backdrop-filter: blur(8px);
  }
  .side-panel h3, .help { display: none; }
  .tower-choice {
    width: 63px;
    min-height: 42px;
    margin: 0;
    padding: 5px 7px;
    display: grid;
    grid-template-columns: 22px 1fr;
    grid-template-rows: 1fr;
    align-items: center;
    border-radius: 11px;
  }
  .tower-choice span { grid-row: auto; font-size: 18px; }
  .tower-choice b { font-size: 9px; }
  .tower-choice small { display: none; }

  .right-panel {
    display: block;
    position: fixed;
    z-index: 25;
    top: max(8px, var(--safe-top));
    left: max(8px, var(--safe-left));
    right: max(8px, var(--safe-right));
    bottom: max(8px, var(--safe-bottom));
    padding: 24px 18px;
    border-radius: 20px;
    overflow: auto;
    transform: translateY(115%);
    transition: transform .22s ease;
    background: rgba(9, 13, 29, .96);
  }
  .right-panel.open { transform: translateY(0); }
  .right-panel h3:nth-of-type(2), .event-log { grid-column: auto; }
  .close-info-button { display: block; position: absolute; right: 10px; top: 10px; width: 40px; height: 40px; padding: 0; border-radius: 50%; font-size: 24px; }
  .event-log { min-height: 80px; }
  .keymap { display: none; }

  .mobile-controls {
    display: flex;
    position: absolute;
    z-index: 11;
    left: max(12px, calc(var(--safe-left) + 8px));
    right: max(12px, calc(var(--safe-right) + 8px));
    bottom: max(10px, calc(var(--safe-bottom) + 7px));
    justify-content: space-between;
    align-items: flex-end;
    pointer-events: none;
  }
  .joystick, .actions { pointer-events: auto; }
  .joystick { width: 130px; display: grid; justify-items: center; gap: 3px; touch-action: none; user-select: none; }
  .joystick-ring {
    width: 110px;
    height: 110px;
    border-radius: 50%;
    position: relative;
    border: 2px solid rgba(98, 231, 255, .28);
    background: radial-gradient(circle, rgba(98, 231, 255, .12), rgba(8, 13, 32, .58) 64%);
    box-shadow: inset 0 0 30px rgba(0, 0, 0, .35), 0 10px 40px rgba(0, 0, 0, .24);
  }
  .joystick-ring::before, .joystick-ring::after { content: ""; position: absolute; background: rgba(255, 255, 255, .08); }
  .joystick-ring::before { width: 1px; top: 12px; bottom: 12px; left: 50%; }
  .joystick-ring::after { height: 1px; left: 12px; right: 12px; top: 50%; }
  .joystick-knob {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 52px;
    height: 52px;
    margin: -26px 0 0 -26px;
    border-radius: 50%;
    background: linear-gradient(145deg, rgba(157, 124, 255, .96), rgba(98, 231, 255, .82));
    border: 2px solid rgba(255, 255, 255, .65);
    box-shadow: 0 6px 24px rgba(0, 0, 0, .35);
    transform: translate3d(0, 0, 0);
    transition: transform .06s linear;
  }
  .joystick small { font-size: 8px; font-weight: 900; letter-spacing: .14em; color: rgba(255, 255, 255, .66); text-shadow: 0 1px 6px #000; }

  .actions { display: grid; grid-template-columns: 58px 68px; grid-template-rows: 58px 68px; gap: 7px; align-items: end; justify-items: end; }
  .action-button {
    width: 58px;
    height: 58px;
    padding: 0;
    border-radius: 50%;
    display: grid;
    place-items: center;
    align-content: center;
    gap: 1px;
    background: rgba(10, 15, 32, .74);
    backdrop-filter: blur(7px);
    box-shadow: 0 8px 28px rgba(0, 0, 0, .28);
    user-select: none;
    touch-action: manipulation;
  }
  .action-button span { font-size: 22px; line-height: 1; }
  .action-button small { font-size: 7px; font-weight: 900; text-transform: uppercase; letter-spacing: .04em; }
  .attack-action { width: 68px; height: 68px; grid-column: 2; grid-row: 2; border-color: rgba(255, 102, 127, .7); background: rgba(94, 20, 43, .76); }
  .attack-action span { font-size: 29px; }
  .interact-action { grid-column: 1; grid-row: 2; border-color: rgba(98, 231, 255, .62); }
  .dash-action { grid-column: 2; grid-row: 1; border-color: rgba(255, 211, 105, .54); }
  .action-button.pressed { transform: scale(.9); filter: brightness(1.35); }

  .wave-banner { top: 29%; font-size: clamp(34px, 13vw, 62px); }
  .respawn { font-size: 16px; }
  .orientation-hint {
    display: block;
    position: absolute;
    z-index: 8;
    top: calc(max(6px, var(--safe-top)) + 112px);
    left: 50%;
    transform: translateX(-50%);
    padding: 7px 10px;
    border-radius: 99px;
    background: rgba(7, 10, 24, .62);
    color: rgba(255, 255, 255, .72);
    font-size: 9px;
    white-space: nowrap;
    pointer-events: none;
    animation: hintFade 5s forwards;
  }
  .modal { padding: max(12px, var(--safe-top)) max(12px, var(--safe-right)) max(12px, var(--safe-bottom)) max(12px, var(--safe-left)); }
  .modal-card { border-radius: 18px; padding: 20px 16px; }
  .modal-card h2 { font-size: 24px; }
  .modal-card > p:not(.eyebrow) { font-size: 15px; }
  .options { grid-template-columns: 1fr; gap: 8px; margin-top: 16px; }
  .options button { min-height: 50px; font-size: 15px; }
  .toast { bottom: calc(max(10px, var(--safe-bottom)) + 137px); }
}

@media (max-width: 420px) and (orientation: portrait) {
  .joystick { width: 112px; }
  .joystick-ring { width: 96px; height: 96px; }
  .joystick-knob { width: 46px; height: 46px; margin: -23px 0 0 -23px; }
  .actions { grid-template-columns: 52px 62px; grid-template-rows: 52px 62px; gap: 6px; }
  .action-button { width: 52px; height: 52px; }
  .attack-action { width: 62px; height: 62px; }
  .tower-choice { width: 56px; }
  .tower-choice b { font-size: 8px; }
}

@media (max-height: 480px) and (orientation: landscape) {
  .hud { left: calc(max(6px, var(--safe-left)) + 46px); right: calc(max(6px, var(--safe-right)) + 46px); grid-template-columns: repeat(5, 1fr); }
  .hp-box { grid-column: auto; display: block; }
  .fullscreen-button, .mobile-info-button { top: max(7px, var(--safe-top)); }
  .side-panel { top: calc(max(6px, var(--safe-top)) + 54px); }
  .joystick-ring { width: 92px; height: 92px; }
  .joystick { width: 105px; }
  .joystick-knob { width: 44px; height: 44px; margin: -22px 0 0 -22px; }
  .actions { grid-template-columns: 50px 62px; grid-template-rows: 50px 62px; }
  .action-button { width: 50px; height: 50px; }
  .attack-action { width: 62px; height: 62px; }
  .orientation-hint { display: none; }
  .toast { bottom: calc(max(8px, var(--safe-bottom)) + 102px); }
}

@media (orientation: landscape) {
  .orientation-hint { display: none !important; }
}

@keyframes hintFade {
  0%, 60% { opacity: 1; }
  100% { opacity: 0; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; scroll-behavior: auto !important; }
}
