:root {
  --bg: #0b1220;
  --panel: #121c30;
  --ink: #e8eefc;
  --muted: #a9b6d6;
  --accent: #7aa2ff;
  --danger: #ff6b7a;
  --good: #7dffb2;
  --vh: 1vh;
  --headerH: 56px;
}

* { box-sizing: border-box; }
html, body { height: 100%; }

body {
  margin: 0;
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
  color: var(--ink);
  background: radial-gradient(900px 400px at 10% 10%, #162447 0%, var(--bg) 60%);
  height: calc(var(--vh) * 100);
  overflow: hidden;
}

.topbar {
  position: relative;
  z-index: 100; /* keep header buttons clickable above overlays */
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 14px;
  border-bottom: 1px solid rgba(255,255,255,0.08);
  background: rgba(10, 16, 30, 0.65);
  backdrop-filter: blur(8px);
}

.title {
  font-weight: 800;
  letter-spacing: 0.5px;
  white-space: nowrap;
}

.hud {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  color: var(--muted);
  font-size: 14px;
  justify-content: center;
}
.hud .label {
  color: rgba(232,238,252,0.85);
  margin-right: 6px;
}
.hud .sub {
  color: rgba(232,238,252,0.55);
  margin-left: 6px;
  font-size: 12px;
}

.controls {
  display: flex;
  gap: 8px;
}
.controls .menu {
  display: none;
  width: 40px;
  padding: 8px 0;
}
.controls button {
  background: rgba(122,162,255,0.12);
  color: var(--ink);
  border: 1px solid rgba(122,162,255,0.35);
  padding: 8px 12px;
  border-radius: 10px;
  cursor: pointer;
}
.controls button:hover { background: rgba(122,162,255,0.18); }
.controls button.primary { background: rgba(122,162,255,0.24); border-color: rgba(122,162,255,0.60); }
.controls button.primary:hover { background: rgba(122,162,255,0.30); }
.controls button:disabled { opacity: 0.45; cursor: not-allowed; }

.layout {
  height: calc(var(--vh) * 100 - var(--headerH));
  display: grid;
  grid-template-columns: 1fr 360px;
  gap: 14px;
  padding: 14px;
}

/* JS-enforced mobile mode (more reliable than media queries alone) */
body.is-mobile .controls .menu { display: inline-flex; align-items: center; justify-content: center; }
body.is-mobile .layout { grid-template-columns: 1fr; padding: 8px; gap: 8px; }
body.is-mobile .topbar .hud { display: none; }
body.is-mobile .topbar .title { font-size: 16px; }
body.is-mobile .controls button { padding: 9px 10px; }
body.is-mobile .controls .primary { padding: 9px 12px; }
body.is-mobile .mobileFloat { display: inline-flex; }
body.is-mobile.playing { --headerH: 0px; }
body.is-mobile.playing .topbar { display: none; }

/* Mobile toast/level messages must NOT reflow the layout (typing friendly) */
body.is-mobile .toastRow{
  position: fixed;
  left: 10px;
  right: 10px;
  top: 10px;
  bottom: auto;
  padding: 8px 10px;
  border-top: none;
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 12px;
  background: rgba(18,28,48,0.86);
  backdrop-filter: blur(10px);
  z-index: 110; /* above canvas + keypad */
  pointer-events: none;
  flex-direction: column;
  gap: 4px;
  opacity: 0;
  transform: translateY(-6px);
  transition: opacity 160ms ease, transform 160ms ease;
}
body.is-mobile .toastRow.show{
  opacity: 1;
  transform: translateY(0);
}
body.is-mobile .toast,
body.is-mobile .levelNote{
  min-height: 0;
  font-size: 13px;
  line-height: 1.25;
}
body.is-mobile .levelNote{ opacity: 0.88; }

/* Prevent accidental scroll-jumps while playing on mobile */
body.is-mobile.playing { overflow: hidden; }

body.is-mobile .canvasWrap { padding: 8px; }
body.is-mobile .sidepanel { display: none; }
body.is-mobile .hud { font-size: 13px; }

body.is-mobile.mobile-side-open .sideBackdrop {
  display: block;
  position: fixed;
  left: 0;
  right: 0;
  top: var(--headerH);
  bottom: 0;
  background: rgba(0,0,0,0.45);
  backdrop-filter: blur(2px);
  z-index: 90;
}

body.is-mobile.mobile-side-open .sidepanel {
  display: block;
  position: fixed;
  left: 8px;
  right: 8px;
  top: calc(var(--headerH) + 8px);
  bottom: 8px;
  max-height: none;
  z-index: 95;
  overflow: auto;
}

body.is-mobile.mobile-side-open .sideTop {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  position: sticky;
  top: 0;
  padding: 8px 2px 10px;
  margin-bottom: 8px;
  background: rgba(18,28,48,0.92);
  border-bottom: 1px solid rgba(255,255,255,0.08);
  backdrop-filter: blur(8px);
}

.sideBackdrop {
  display: none;
}

.sideTop {
  display: none;
}

.gamepanel, .sidepanel {
  background: rgba(18,28,48,0.72);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 16px;
  box-shadow: 0 14px 40px rgba(0,0,0,0.35);
  overflow: hidden;
}

.gamepanel {
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.canvasWrap {
  position: relative;
  flex: 1;
  min-height: 0;
  padding: 12px;
}

#gameCanvas {
  width: 100%;
  height: 100%;
  display: block;
  border-radius: 14px;
  background: linear-gradient(#0a1020, #070c16);
  border: 1px solid rgba(255,255,255,0.08);
}

.overlay {
  position: absolute;
  inset: 12px;
  border-radius: 14px;
  background: rgba(0,0,0,0.45);
  backdrop-filter: blur(6px);
  display: grid;
  place-items: center;
  pointer-events: auto;
}
.overlay.hidden { display: none; }
.overlayCard {
  background: rgba(18,28,48,0.92);
  border: 1px solid rgba(255,255,255,0.10);
  border-radius: 14px;
  padding: 16px 16px;
  max-width: 360px;
  text-align: center;
}
.overlayTitle {
  font-weight: 900;
  letter-spacing: 0.4px;
  margin-bottom: 8px;
  font-size: 18px;
}
.overlayBody {
  color: rgba(232,238,252,0.78);
  font-size: 14px;
  line-height: 1.35;
}

.stormBadge {
  position: absolute;
  top: 18px;
  right: 18px;
  padding: 6px 10px;
  border-radius: 999px;
  background: rgba(255,255,255,0.10);
  border: 1px solid rgba(255,255,255,0.14);
  font-size: 12px;
  letter-spacing: 1.2px;
}
.stormBadge.hidden { display: none; }

.answerbar {
  position: sticky;
  bottom: 0;
  display: flex;
  gap: 10px;
  padding: 12px;
  background: rgba(12,18,32,0.75);
  border-top: 1px solid rgba(255,255,255,0.08);
}

.answerbar input {
  flex: 1;
  padding: 12px 12px;
  border-radius: 12px;
  border: 1px solid rgba(255,255,255,0.12);
  background: rgba(8,12,22,0.75);
  color: var(--ink);
  outline: none;
  font-size: 16px; /* iOS: prevent zoom */
}
.answerbar input[readonly] {
  caret-color: transparent;
}
.answerbar input:focus {
  border-color: rgba(122,162,255,0.55);
  box-shadow: 0 0 0 4px rgba(122,162,255,0.10);
}

.answerbar button {
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid rgba(255,255,255,0.12);
  background: rgba(122,162,255,0.18);
  color: var(--ink);
  cursor: pointer;
}
.answerbar button:hover { background: rgba(122,162,255,0.24); }

.keypad {
  display: none; /* shown on mobile */
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  padding: 12px;
  padding-top: 0;
  background: rgba(12,18,32,0.50);
  border-top: 1px solid rgba(255,255,255,0.06);
}
.keypad button {
  padding: 12px 0;
  border-radius: 12px;
  border: 1px solid rgba(255,255,255,0.12);
  background: rgba(255,255,255,0.06);
  color: rgba(232,238,252,0.92);
  font-weight: 800;
  cursor: pointer;
}
.keypad button.ok {
  background: rgba(125,255,178,0.10);
  border-color: rgba(125,255,178,0.25);
}
.keypad button.zero {
  grid-column: span 4;
}

.toastRow {
  display: flex;
  gap: 12px;
  padding: 10px 12px 12px;
  border-top: 1px solid rgba(255,255,255,0.06);
}

.toast {
  flex: 1;
  min-height: 18px;
  color: var(--muted);
  font-size: 14px;
}
.toast.good { color: var(--good); }
.toast.bad { color: var(--danger); }

.levelNote {
  flex: 1.2;
  color: rgba(232,238,252,0.78);
  font-size: 13px;
  min-height: 18px;
}

.sidepanel {
  padding: 12px;
  overflow: auto;
}

.card {
  background: rgba(12,18,32,0.35);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 14px;
  padding: 10px 12px;
  margin-bottom: 10px;
}
.card summary {
  cursor: pointer;
  font-weight: 800;
  color: rgba(232,238,252,0.92);
  list-style: none;
}
.card summary::-webkit-details-marker { display: none; }

.field {
  display: grid;
  gap: 6px;
  margin-top: 10px;
}
.field label {
  font-size: 12px;
  color: rgba(232,238,252,0.75);
}
.field input {
  padding: 10px 10px;
  border-radius: 12px;
  border: 1px solid rgba(255,255,255,0.12);
  background: rgba(8,12,22,0.65);
  color: var(--ink);
  outline: none;
  font-size: 14px;
}
.hint {
  font-size: 12px;
  color: rgba(232,238,252,0.55);
}

.check {
  display: flex;
  gap: 10px;
  align-items: center;
  margin-top: 10px;
  color: rgba(232,238,252,0.82);
  font-size: 13px;
}
.check input { transform: scale(1.1); }

.btn {
  margin-top: 10px;
  width: 100%;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid rgba(122,162,255,0.35);
  background: rgba(122,162,255,0.16);
  color: var(--ink);
  cursor: pointer;
  font-weight: 800;
}
.btn:hover { background: rgba(122,162,255,0.22); }

.meRow {
  margin-top: 10px;
  color: rgba(232,238,252,0.70);
  font-size: 13px;
}

.leaderboard {
  margin: 10px 0 0;
  padding-left: 20px;
}
.leaderboard li {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding: 4px 0;
  color: rgba(232,238,252,0.86);
}
.leaderboard .who { color: rgba(232,238,252,0.78); }
.leaderboard .score { color: rgba(232,238,252,0.92); font-weight: 900; }

.milestones { margin-top: 10px; color: rgba(232,238,252,0.70); font-size: 13px; line-height: 1.35; }

.exportLinks {
  display: grid;
  gap: 8px;
  margin-top: 10px;
}
.exportLinks a {
  color: rgba(232,238,252,0.88);
  text-decoration: none;
  background: rgba(255,255,255,0.06);
  border: 1px solid rgba(255,255,255,0.10);
  padding: 10px 10px;
  border-radius: 12px;
}
.exportLinks a:hover { background: rgba(255,255,255,0.10); }

.note {
  color: rgba(232,238,252,0.55);
  font-size: 12px;
  margin: 10px 2px 0;
}

body.reduced-motion #gameCanvas {
  /* slightly calmer feel */
  filter: saturate(0.95);
}

@media (max-width: 980px) {
  .controls .menu { display: inline-flex; align-items: center; justify-content: center; }

  .layout { grid-template-columns: 1fr; padding: 8px; gap: 8px; }
  .canvasWrap { padding: 8px; }

  /* Mobile: keep playfield as tall as possible.
     Hide the side panel by default and show it as an overlay when Menu is opened. */
  .sidepanel { display: none; }

  body.mobile-side-open .sideBackdrop {
    display: block;
    position: fixed;
    left: 0;
    right: 0;
    top: var(--headerH);
    bottom: 0;
    background: rgba(0,0,0,0.45);
    backdrop-filter: blur(2px);
    z-index: 49;
  }

  body.mobile-side-open .sidepanel {
    display: block;
    position: fixed;
    left: 8px;
    right: 8px;
    top: calc(var(--headerH) + 8px);
    bottom: 8px;
    max-height: none;
    z-index: 50;
    overflow: auto;
  }

  body.mobile-side-open .sideTop {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    position: sticky;
    top: 0;
    padding: 8px 2px 10px;
    margin-bottom: 8px;
    background: rgba(18,28,48,0.92);
    border-bottom: 1px solid rgba(255,255,255,0.08);
    backdrop-filter: blur(8px);
  }

  .sideTopTitle { font-weight: 900; letter-spacing: 0.6px; }
  .sideClose {
    background: rgba(255,255,255,0.06);
    border: 1px solid rgba(255,255,255,0.12);
    color: rgba(232,238,252,0.92);
    border-radius: 10px;
    padding: 8px 10px;
    cursor: pointer;
  }

  .keypad { display: grid; }
  .hud { font-size: 13px; }
}

@media (max-width: 420px) {
  .hud { gap: 10px; }
  .controls button { padding: 8px 10px; }
}


/* Mobile floating controls (maximizes playfield height by hiding header while playing) */

.mobileFloat {
  display: none;
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 20;
  gap: 8px;
  align-items: center;
  padding: 8px 10px;
  border-radius: 999px;
  background: rgba(10, 16, 30, 0.62);
  border: 1px solid rgba(255,255,255,0.10);
  backdrop-filter: blur(10px);
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}
.mobileFloat .mBtn{
  width: 34px;
  height: 34px;
  border-radius: 999px;
  border: 1px solid rgba(255,255,255,0.12);
  background: rgba(255,255,255,0.07);
  color: rgba(232,238,252,0.92);
  font-weight: 800;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.mobileFloat .mHud{
  display: inline-flex;
  gap: 8px;
  color: rgba(232,238,252,0.88);
  font-size: 13px;
  font-weight: 700;
  padding: 0 4px;
  white-space: nowrap;
}
.mobileFloat .mHudItem{
  color: rgba(232,238,252,0.72);
}


body.is-mobile .answerbar {
  padding: 10px 10px;
  gap: 8px;
}
body.is-mobile .answerbar input {
  padding: 10px 10px;
  border-radius: 12px;
  font-size: 16px;
}
body.is-mobile .keypad {
  padding: 8px 10px 10px;
  gap: 6px;
}
body.is-mobile .keypad button {
  padding: 10px 0;
  border-radius: 12px;
  font-size: 16px;
}
body.is-mobile .keypad button.zero {
  padding: 10px 0;
}


@media (max-height: 740px) {
  body.is-mobile .keypad button { padding: 8px 0; }
  body.is-mobile .keypad { gap: 5px; padding: 6px 10px 10px; }
  body.is-mobile .answerbar { padding: 8px 10px; }
  .mobileFloat { top: 8px; right: 8px; }
}
