:root {
  --bg: #030b16;
  --panel: #081a30;
  --ice: #e9f7ff;
  --blue: #29a5ff;
  --red: #ff4d56;
  --white: #f8fbff;
  --muted: #93abc4;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background:
    radial-gradient(circle at 50% -10%, #12385e 0, transparent 42%),
    linear-gradient(180deg, #071425 0%, var(--bg) 100%);
  color: var(--white);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

body {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 18px;
  overflow-x: hidden;
}

button { font: inherit; }

.game-shell {
  width: min(1180px, 100%);
}

.topbar,
.scoreboard,
.footer-controls {
  background: rgba(7, 23, 43, 0.93);
  border: 1px solid rgba(255,255,255,.09);
  box-shadow: 0 14px 35px rgba(0,0,0,.28);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-radius: 16px 16px 0 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.puck-logo {
  width: 46px;
  height: 46px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: #0e2948;
  font-size: 25px;
}

h1 {
  margin: 0;
  font-size: clamp(20px, 3vw, 30px);
  letter-spacing: .055em;
  font-weight: 900;
}

.brand p {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.scoreboard {
  display: grid;
  grid-template-columns: 1fr .8fr 1fr;
  gap: 12px;
  align-items: center;
  padding: 10px 16px 12px;
  border-top: 0;
}

.team {
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-areas: "name score" "controls score";
  align-items: center;
  gap: 0 14px;
}

.team-red {
  text-align: right;
  grid-template-columns: auto 1fr;
  grid-template-areas: "score name" "score controls";
}

.team-name {
  grid-area: name;
  font-weight: 900;
  letter-spacing: .08em;
  font-size: 14px;
}

.controls {
  grid-area: controls;
  color: var(--muted);
  font-size: 11px;
  letter-spacing: .13em;
  margin-top: 3px;
}

.score {
  grid-area: score;
  font-size: clamp(38px, 6vw, 62px);
  line-height: .95;
  font-weight: 950;
  min-width: 58px;
}

.team-blue .score { color: var(--blue); text-shadow: 0 0 24px rgba(41,165,255,.3); }
.team-red .score { color: var(--red); text-shadow: 0 0 24px rgba(255,77,86,.3); }

.game-status {
  text-align: center;
  color: #d6e8fb;
  font-size: 12px;
  font-weight: 800;
  letter-spacing: .06em;
}

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

.rink-wrap {
  position: relative;
  background: #07172b;
  border-left: 1px solid rgba(255,255,255,.09);
  border-right: 1px solid rgba(255,255,255,.09);
  overflow: hidden;
}

canvas {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 1100 / 620;
  background: var(--ice);
  touch-action: none;
}

.overlay {
  position: absolute;
  inset: 0;
  display: none;
  place-items: center;
  background: rgba(1, 8, 17, .55);
  backdrop-filter: blur(4px);
  padding: 20px;
}

.overlay.active { display: grid; }

.overlay-card {
  width: min(520px, 92%);
  text-align: center;
  background: rgba(5, 19, 36, .95);
  border: 1px solid rgba(255,255,255,.14);
  border-radius: 20px;
  padding: 28px;
  box-shadow: 0 24px 65px rgba(0,0,0,.42);
}

.overlay-card h2 {
  margin: 0 0 8px;
  font-size: clamp(28px, 5vw, 46px);
  text-transform: uppercase;
  letter-spacing: .03em;
}

.overlay-card p {
  margin: 0 auto 20px;
  max-width: 440px;
  color: #b8cce0;
  line-height: 1.55;
}

.primary-btn,
.small-btn {
  border: 0;
  cursor: pointer;
  font-weight: 850;
  color: #fff;
  transition: transform .12s ease, filter .12s ease;
}

.primary-btn:hover,
.small-btn:hover { transform: translateY(-1px); filter: brightness(1.12); }
.primary-btn:active,
.small-btn:active { transform: translateY(1px); }

.primary-btn {
  padding: 13px 22px;
  border-radius: 999px;
  background: linear-gradient(135deg, #177bd4, #24a9ff);
  box-shadow: 0 7px 25px rgba(27,148,233,.25);
}

.small-btn {
  padding: 9px 13px;
  border-radius: 10px;
  background: #102c4d;
  border: 1px solid rgba(255,255,255,.08);
}

.footer-controls {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 14px;
  padding: 11px 16px;
  border-radius: 0 0 16px 16px;
  color: #b9cee2;
  font-size: 13px;
}

.tip {
  margin: 10px 0 0;
  text-align: center;
  color: #6f89a2;
  font-size: 12px;
}


.mode-selector {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin: 4px auto 10px;
  max-width: 430px;
}

.mode-btn {
  border: 1px solid rgba(255,255,255,.13);
  border-radius: 13px;
  padding: 12px 10px;
  background: #0b2745;
  color: #c7daec;
  font-weight: 900;
  cursor: pointer;
  transition: transform .12s ease, border-color .12s ease, background .12s ease;
}

.mode-btn:hover { transform: translateY(-1px); }
.mode-btn.selected {
  background: linear-gradient(135deg, #177bd4, #24a9ff);
  border-color: rgba(255,255,255,.35);
  color: white;
  box-shadow: 0 7px 22px rgba(27,148,233,.22);
}

.mode-help {
  min-height: 19px;
  margin: 0 auto 16px !important;
  font-size: 12px;
  color: #8faac2 !important;
}

.ai-mode .touch-side-red { display: none; }
.ai-mode .touch-controls { justify-content: flex-start; }

@media (max-width: 700px) {
  body { padding: 8px; align-items: flex-start; }
  .topbar { padding: 9px 10px; }
  .puck-logo { display: none; }
  .scoreboard { padding: 8px; gap: 5px; }
  .team-name { font-size: 11px; }
  .controls { font-size: 9px; }
  .game-status { font-size: 9px; }
  .timer { font-size: 14px; }
  .footer-controls { font-size: 10px; padding: 8px; }
  .footer-controls .small-btn { padding: 7px 9px; }
}

/* Mobile / tablet touch controls */
.play-zone {
  position: relative;
}

.touch-side {
  display: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  touch-action: none;
}

.dpad {
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 5px;
}

.touch-btn,
.touch-player-label {
  display: grid;
  place-items: center;
  border-radius: 14px;
}

.touch-btn {
  border: 2px solid rgba(255,255,255,.48);
  background: rgba(6, 20, 37, .82);
  color: white;
  font-size: clamp(18px, 4vw, 30px);
  font-weight: 950;
  touch-action: none;
  cursor: pointer;
  padding: 0;
  box-shadow: 0 4px 12px rgba(0,0,0,.22);
}

.touch-btn:active,
.touch-btn.is-active {
  transform: scale(.92);
  background: rgba(255,255,255,.30);
}

.touch-side-blue .touch-btn { box-shadow: inset 0 0 18px rgba(41,165,255,.22), 0 4px 12px rgba(0,0,0,.22); }
.touch-side-red .touch-btn { box-shadow: inset 0 0 18px rgba(255,77,86,.22), 0 4px 12px rgba(0,0,0,.22); }
.touch-side-blue .touch-btn.is-active { background: rgba(41,165,255,.62); }
.touch-side-red .touch-btn.is-active { background: rgba(255,77,86,.62); }

.touch-player-label {
  grid-column: 2;
  grid-row: 2;
  background: rgba(5,16,31,.9);
  border: 1px solid rgba(255,255,255,.26);
  color: rgba(255,255,255,.9);
  font-weight: 950;
  font-size: clamp(15px, 3.2vw, 24px);
}

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

/* Computer mode never needs the red control pad. */
body.ai-mode .touch-side-red,
.play-zone.ai-mode .touch-side-red,
.rink-wrap.ai-mode ~ .touch-side-red {
  display: none !important;
}

.mode-selector {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin: 4px auto 10px;
  max-width: 430px;
}

.mode-btn {
  border: 1px solid rgba(255,255,255,.13);
  border-radius: 13px;
  padding: 12px 10px;
  background: #0b2745;
  color: #c7daec;
  font-weight: 900;
  cursor: pointer;
  transition: transform .12s ease, border-color .12s ease, background .12s ease;
}

.mode-btn:hover { transform: translateY(-1px); }
.mode-btn.selected {
  background: linear-gradient(135deg, #177bd4, #24a9ff);
  border-color: rgba(255,255,255,.35);
  color: white;
  box-shadow: 0 7px 22px rgba(27,148,233,.22);
}

.mode-help {
  min-height: 19px;
  margin: 0 auto 16px !important;
  font-size: 12px;
  color: #8faac2 !important;
}

@media (max-width: 700px) {
  body {
    padding: max(5px, env(safe-area-inset-top)) max(5px, env(safe-area-inset-right)) max(5px, env(safe-area-inset-bottom)) max(5px, env(safe-area-inset-left));
    align-items: flex-start;
  }
  .brand p { display: none; }
  .puck-logo { display: none; }
  .topbar { padding: 8px 10px; border-radius: 10px 10px 0 0; }
  h1 { font-size: clamp(18px, 6vw, 28px); }
  .scoreboard { padding: 5px 8px 7px; gap: 5px; }
  .score { font-size: clamp(30px, 9vw, 48px); min-width: 42px; }
  .team-name { font-size: 11px; }
  .controls { display: none; }
  .game-status { font-size: 9px; }
  .timer { font-size: 14px; }
  .overlay { padding: 10px; z-index: 5; }
  .overlay-card { padding: 18px 16px; }
  .overlay-card p { font-size: 13px; line-height: 1.4; margin-bottom: 14px; }
  .footer-controls { display: none; }
  .tip { display: none; }
}

/* Touch devices: controls live OUTSIDE the ice. */
@media (hover: none), (pointer: coarse), (max-width: 900px) {
  canvas { touch-action: none; }

  .play-zone {
    background: rgba(5, 18, 34, .96);
    border-left: 1px solid rgba(255,255,255,.09);
    border-right: 1px solid rgba(255,255,255,.09);
  }

  .touch-side {
    display: block;
    width: clamp(104px, 29vw, 150px);
    height: clamp(104px, 29vw, 150px);
    padding: 6px;
    margin: 7px auto;
  }

  /* Portrait: rink first, then both pads side-by-side underneath. */
  .play-zone {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-template-areas:
      "rink rink"
      "blue red";
    align-items: center;
    justify-items: center;
    gap: 0 8px;
    padding-bottom: 3px;
  }

  .rink-wrap {
    grid-area: rink;
    width: 100%;
  }

  .touch-side-blue { grid-area: blue; }
  .touch-side-red { grid-area: red; }

  body.ai-mode .play-zone {
    grid-template-areas:
      "rink rink"
      "blue blue";
  }
}

/* Landscape phones: control pad | rink | control pad. The rink is height-limited
   so all controls stay visible without covering the ice. */
@media (orientation: landscape) and (hover: none),
       (orientation: landscape) and (pointer: coarse),
       (orientation: landscape) and (max-height: 650px) {
  body {
    min-height: 100dvh;
    height: 100dvh;
    overflow: hidden;
    padding: 3px max(4px, env(safe-area-inset-right)) 3px max(4px, env(safe-area-inset-left));
    align-items: stretch;
  }

  .game-shell {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
  }

  .topbar {
    flex: 0 0 auto;
    padding: 4px 10px;
    min-height: 39px;
  }
  .puck-logo, .brand p { display: none; }
  h1 { font-size: 17px; }
  .small-btn { padding: 6px 10px; border-radius: 8px; }

  .scoreboard {
    flex: 0 0 auto;
    padding: 2px 10px 4px;
    min-height: 48px;
  }
  .score { font-size: 29px; min-width: 38px; }
  .team-name { font-size: 10px; }
  .controls { display: none; }
  .game-status { font-size: 8px; }
  .timer { font-size: 12px; margin-top: 1px; }

  .play-zone {
    flex: 1 1 auto;
    min-height: 0;
    display: grid;
    grid-template-columns: minmax(105px, 16vw) minmax(0, 1fr) minmax(105px, 16vw);
    grid-template-areas: "blue rink red";
    gap: 6px;
    align-items: center;
    justify-items: center;
    padding: 4px 6px;
    overflow: hidden;
  }

  .rink-wrap {
    grid-area: rink;
    width: auto;
    height: 100%;
    max-width: 100%;
    aspect-ratio: 1100 / 620;
    border: 1px solid rgba(255,255,255,.09);
  }

  canvas {
    width: 100%;
    height: 100%;
    object-fit: contain;
  }

  .touch-side {
    width: min(15vw, 125px);
    height: min(15vw, 125px);
    min-width: 96px;
    min-height: 96px;
    margin: 0;
    padding: 3px;
  }
  .dpad { gap: 4px; }
  .touch-btn, .touch-player-label { border-radius: 10px; }
  .touch-btn { font-size: 18px; }
  .touch-player-label { font-size: 15px; }

  .touch-side-blue { grid-area: blue; }
  .touch-side-red { grid-area: red; }

  body.ai-mode .play-zone {
    grid-template-columns: minmax(105px, 16vw) minmax(0, 1fr) minmax(105px, 16vw);
    grid-template-areas: "blue rink spacer";
  }

  .footer-controls, .tip { display: none; }
}


/* ---------- Online multiplayer ---------- */
.mode-selector {
  grid-template-columns: repeat(3, 1fr);
  max-width: 650px;
}

.online-panel {
  margin: 4px auto 16px;
  max-width: 560px;
  padding: 14px;
  border-radius: 14px;
  background: rgba(3, 14, 27, .72);
  border: 1px solid rgba(255,255,255,.10);
}

.online-panel[hidden] { display: none !important; }

.online-actions {
  display: flex;
  gap: 10px;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
}

.online-or {
  color: #7891a9;
  font-size: 12px;
  font-weight: 800;
  text-transform: uppercase;
}

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

#roomCodeInput {
  width: 138px;
  border: 1px solid rgba(255,255,255,.18);
  background: #06182d;
  color: white;
  border-radius: 10px;
  padding: 10px 12px;
  font: 900 16px/1 system-ui;
  text-transform: uppercase;
  letter-spacing: .18em;
  outline: none;
}

#roomCodeInput:focus {
  border-color: #29a5ff;
  box-shadow: 0 0 0 3px rgba(41,165,255,.15);
}

.online-status {
  margin-top: 12px;
  color: #9db6cf;
  font-size: 13px;
  line-height: 1.4;
}

.room-display {
  margin-top: 10px;
  color: #b9cee2;
  font-size: 12px;
  font-weight: 800;
  letter-spacing: .08em;
}

.room-display strong {
  display: inline-block;
  margin-left: 8px;
  color: #fff;
  font-size: 25px;
  letter-spacing: .20em;
}

body.online-mode .touch-side { display: none !important; }
body.online-mode.online-p1 .touch-side-blue { display: block !important; }
body.online-mode.online-p2 .touch-side-red { display: block !important; }

body.online-mode.online-p1 .play-zone,
body.online-mode.online-p2 .play-zone {
  grid-template-areas:
    "rink rink"
    "blue red";
}

body.online-mode.online-p1 .touch-side-red,
body.online-mode.online-p2 .touch-side-blue { visibility: hidden; }

.connection-badge {
  font-weight: 900;
}

@media (max-width: 700px) {
  .mode-selector {
    grid-template-columns: 1fr;
    gap: 7px;
  }
  .mode-btn { padding: 9px 8px; }
  .online-panel { padding: 10px; margin-bottom: 10px; }
  .online-actions { gap: 7px; }
}

@media (orientation: landscape) and (hover: none),
       (orientation: landscape) and (pointer: coarse),
       (orientation: landscape) and (max-height: 650px) {
  body.online-mode.online-p1 .play-zone {
    grid-template-columns: minmax(105px, 16vw) minmax(0, 1fr) minmax(105px, 16vw);
    grid-template-areas: "blue rink spacer";
  }
  body.online-mode.online-p2 .play-zone {
    grid-template-columns: minmax(105px, 16vw) minmax(0, 1fr) minmax(105px, 16vw);
    grid-template-areas: "spacer rink red";
  }
}
