:root {
  --green: #059669;
  --green-dark: #047857;
  --green-soft: #D1FAE5;
  --blue: #0284C7;
  --amber: #D97706;
  --red: #DC2626;
  --violet: #7C3AED;
  --ink: #0F172A;
  --muted: #475569;
  --bg: #F4F7F6;
  --arcade: 'Press Start 2P', 'Courier New', monospace;
  --round: 'Fredoka', 'Trebuchet MS', system-ui, sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }

body {
  font-family: var(--round);
  color: var(--ink);
  background: linear-gradient(180deg, #ECFDF5 0%, var(--bg) 56%, #E0F2FE 100%);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

/* Top navigation bar */
.bar {
  flex: 0 0 auto;
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 0 14px;
  background: #FFFFFF;
  border-bottom: 2px solid var(--green-soft);
  z-index: 40;
}
.bar a.back {
  color: var(--green-dark);
  font-weight: 700;
  text-decoration: none;
  font-size: 0.9rem;
  white-space: nowrap;
}
.bar .ttl {
  font-weight: 700;
  letter-spacing: 0.04em;
  color: var(--ink);
  font-size: clamp(0.82rem, 2.8vw, 1.02rem);
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.bar-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}
.bar button {
  font-family: var(--round);
  border: 2px solid var(--green-soft);
  background: #FFFFFF;
  border-radius: 999px;
  font-size: 1rem;
  padding: 4px 11px;
  cursor: pointer;
}
.bar button:active { transform: scale(0.95); }

/* Main landscape viewport wrapper */
.wrap {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 8px 10px 6px;
}

.stage-shell {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  max-width: 960px;
  min-height: 0;
}

.stage {
  position: relative;
  width: min(100%, 920px, calc((100dvh - 50px - 42px - 84px) * 800 / 480));
  aspect-ratio: 800 / 480;
  border-radius: 18px;
  overflow: hidden;
  box-shadow: 0 12px 32px rgba(15, 23, 42, 0.24);
  border: 3px solid #FFFFFF;
  background: #0F172A;
}

canvas#game {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

/* Modal overlays (Intro, Victory/Game Over, Daily & All-Time Top 10) */
.ui {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 12px;
  background: rgba(15, 23, 42, 0.64);
  pointer-events: none;
  z-index: 20;
}
.ui.hidden { display: none; }
.ui > * { pointer-events: auto; }

.panel {
  width: 100%;
  max-width: 680px;
  max-height: 100%;
  overflow-y: auto;
  background: rgba(255, 255, 255, 0.97);
  border: 3px solid #0F172A;
  border-radius: 18px;
  padding: 13px 16px 14px;
  box-shadow: 0 8px 0 rgba(15, 23, 42, 0.28);
  text-align: center;
  touch-action: pan-y;
}
.panel.board { max-width: 560px; }
.panel.narrow { max-width: 540px; }

.panel h1, .panel h2 {
  margin: 0 0 3px;
  line-height: 1.08;
  color: var(--ink);
}
.panel h1 { font-size: clamp(1.35rem, 3.6vw, 1.9rem); }
.panel h2 { font-size: clamp(1.12rem, 3.2vw, 1.48rem); }
.panel .sub {
  color: var(--muted);
  font-weight: 600;
  margin: 0 0 8px;
  font-size: 0.84rem;
  line-height: 1.28;
}

/* Hero Picker Cards */
.hero-label {
  font-family: var(--arcade);
  font-size: 0.56rem;
  color: var(--green-dark);
  margin: 4px 0 6px;
  letter-spacing: 0.06em;
}
.hero-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 8px;
}
.hero-card {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  padding: 7px 6px 8px;
  background: #F8FAFC;
  border: 2.5px solid #CBD5E1;
  border-radius: 13px;
  cursor: pointer;
  font-family: var(--round);
  color: var(--ink);
  transition: transform 0.1s ease, border-color 0.12s ease, background 0.12s ease;
}
.hero-card:hover { transform: translateY(-2px); border-color: #64748B; }
.hero-card.selected {
  background: #ECFDF5;
  border-color: var(--green);
  box-shadow: 0 0 0 3px rgba(5, 150, 105, 0.22), 0 4px 0 var(--green-dark);
}
.hero-card.selected::after {
  content: '✓';
  position: absolute;
  top: 5px;
  right: 6px;
  width: 19px;
  height: 19px;
  border-radius: 50%;
  background: var(--green);
  color: #FFFFFF;
  font-size: 0.72rem;
  font-weight: 700;
  display: grid;
  place-items: center;
}
.hero-card svg {
  width: 56px;
  height: 52px;
  display: block;
}
.hero-card strong { font-size: 0.84rem; line-height: 1.1; }
.hero-card span { font-size: 0.69rem; color: var(--muted); font-weight: 600; line-height: 1.15; }

/* Rules Legend */
.legend {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 6px;
  background: #F8FAFC;
  border: 2px solid var(--green-soft);
  border-radius: 12px;
  padding: 7px 9px;
  font-size: 0.73rem;
  font-weight: 600;
  text-align: left;
  margin-bottom: 9px;
  line-height: 1.24;
}
.legend b.r { color: var(--red); }
.legend b.g { color: var(--green-dark); }
.legend b.v { color: var(--violet); }
.legend b.a { color: var(--amber); }

/* Victory celebration banner inside #overUI */
.victory-banner {
  display: none;
  background: linear-gradient(135deg, #FEF3C7 0%, #ECFDF5 100%);
  border: 2.5px solid #059669;
  border-radius: 14px;
  padding: 8px 10px;
  margin-bottom: 8px;
}
.ui.is-victory .victory-banner { display: flex; align-items: center; gap: 10px; text-align: left; }
.ui.is-victory .panel { border-color: #047857; background: linear-gradient(180deg, #FFFFFF 0%, #F0FDF4 100%); }
.victory-banner svg { flex: 0 0 auto; width: 96px; height: 68px; border-radius: 10px; border: 2px solid #0F172A; }
.victory-banner h3 { margin: 0 0 2px; font-size: 0.92rem; color: #065F46; }
.victory-banner p { margin: 0; font-size: 0.76rem; color: #334155; font-weight: 600; line-height: 1.25; }

/* Buttons & Rows */
.btn {
  font-family: var(--round);
  font-weight: 700;
  border: 3px solid #0F172A;
  border-radius: 13px;
  cursor: pointer;
  padding: 8px 16px;
  font-size: 0.94rem;
  box-shadow: 0 4px 0 #0F172A;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
}
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 #0F172A; }
.btn.primary { background: var(--green); color: #FFFFFF; font-size: 1.06rem; padding: 9px 24px; }
.btn.alt { background: #FFFFFF; color: var(--ink); font-size: 0.84rem; padding: 7px 12px; }
.row { display: flex; gap: 7px; flex-wrap: wrap; justify-content: center; margin-top: 6px; }
.hint { font-size: 0.72rem; color: var(--muted); margin: 7px 0 0; font-weight: 600; }

/* Stats strip */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin: 7px 0; }
.stat { background: #F0FDF4; border: 2px solid var(--green-soft); border-radius: 10px; padding: 5px 6px; }
.stat.big { background: #FEF3C7; border-color: #FCD34D; }
.stat small { display: block; color: var(--muted); font-size: 0.62rem; font-weight: 700; text-transform: uppercase; }
.stat strong { font-family: var(--arcade); font-size: 0.78rem; color: var(--ink); }
.stat.big strong { font-size: 0.92rem; }

/* Shared Daily & All-Time Leaderboard + Save Score Form */
label.nm { display: block; font-family: var(--arcade); font-size: 0.52rem; margin: 6px 0 3px; color: var(--green-dark); }
input#nameInput {
  font-family: var(--arcade);
  font-size: 0.9rem;
  text-transform: uppercase;
  text-align: center;
  letter-spacing: 0.12em;
  width: 100%;
  padding: 7px 6px;
  border: 3px solid var(--green);
  border-radius: 11px;
  background: #FFFFFF;
  color: var(--ink);
  outline: none;
  user-select: text;
  -webkit-user-select: text;
}
input#nameInput:focus, input#localityInput:focus { box-shadow: 0 0 0 4px rgba(5, 150, 105, 0.25); }
.loc-row { display: flex; gap: 6px; align-items: stretch; }
input#localityInput {
  flex: 1 1 auto;
  min-width: 0;
  font-family: var(--round);
  font-weight: 600;
  font-size: 0.86rem;
  text-align: center;
  padding: 6px 10px;
  border: 2.5px solid #94A3B8;
  border-radius: 11px;
  background: #FFFFFF;
  color: var(--ink);
  outline: none;
  user-select: text;
  -webkit-user-select: text;
}
.geo-btn {
  flex: 0 0 auto;
  font-family: var(--round);
  font-weight: 700;
  font-size: 0.82rem;
  padding: 0 10px;
  border: 2.5px solid var(--green);
  border-radius: 11px;
  background: #ECFDF5;
  color: var(--green-dark);
  cursor: pointer;
}
.geo-btn:disabled { opacity: 0.55; cursor: wait; }
.geo-pill {
  display: inline-block;
  font-size: 0.7rem;
  font-weight: 600;
  color: #065F46;
  background: #ECFDF5;
  border: 1.5px solid #A7F3D0;
  border-radius: 999px;
  padding: 2px 10px;
  margin: 4px 0 2px;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.err { color: var(--red); font-weight: 700; font-size: 0.76rem; min-height: 1.05em; margin: 3px 0; }

/* Daily / All-Time Period Tabs */
.period-tabs {
  display: inline-flex;
  gap: 5px;
  background: #F1F5F9;
  border: 2px solid #CBD5E1;
  border-radius: 999px;
  padding: 3px;
  margin: 4px 0 4px;
}
.period-btn {
  font-family: var(--round);
  font-weight: 700;
  font-size: 0.78rem;
  padding: 4px 13px;
  border-radius: 999px;
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}
.period-btn.on {
  background: var(--green);
  color: #FFFFFF;
  box-shadow: 0 2px 4px rgba(5, 150, 105, 0.25);
}

.board-pills { display: flex; flex-wrap: wrap; justify-content: center; gap: 5px; margin: 2px 0; }
.reset-pill {
  display: inline-block;
  font-size: 0.68rem;
  font-weight: 700;
  color: #92400E;
  background: #FEF3C7;
  border: 1.5px solid #FCD34D;
  border-radius: 999px;
  padding: 2px 9px;
  white-space: nowrap;
}
.reset-pill.alltime {
  color: #5B21B6;
  background: #EDE9FE;
  border-color: #C4B5FD;
}
.area-filter {
  margin: 5px 0 4px;
  padding: 5px 7px;
  background: #F8FAFC;
  border: 2px solid var(--green-soft);
  border-radius: 11px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.area-chips { display: flex; flex-wrap: wrap; justify-content: center; gap: 4px; }
.area-chip {
  font-family: var(--round);
  font-size: 0.7rem;
  font-weight: 700;
  padding: 3px 9px;
  border-radius: 999px;
  border: 1.5px solid #CBD5E1;
  background: #FFFFFF;
  color: #334155;
  cursor: pointer;
  line-height: 1.2;
}
.area-chip.on { background: var(--green); border-color: var(--green-dark); color: #FFFFFF; }
.area-controls { display: grid; grid-template-columns: 1fr 1fr; gap: 5px; }
.area-controls select, .area-controls input {
  width: 100%;
  min-width: 0;
  font-family: var(--round);
  font-size: 0.76rem;
  font-weight: 600;
  padding: 4px 8px;
  border: 1.5px solid #CBD5E1;
  border-radius: 8px;
  background: #FFFFFF;
  color: var(--ink);
  outline: none;
  user-select: text;
  -webkit-user-select: text;
}
.area-controls select:focus, .area-controls input:focus { border-color: var(--green); box-shadow: 0 0 0 3px rgba(5, 150, 105, 0.18); }

table.scores { width: 100%; border-collapse: collapse; margin: 5px 0; font-family: var(--arcade); font-size: clamp(0.48rem, 1.9vw, 0.64rem); }
table.scores th { color: var(--muted); font-size: 0.5rem; text-align: left; padding: 3px 5px; border-bottom: 2px solid var(--green-soft); }
table.scores td { padding: 4px 5px; text-align: left; border-bottom: 1px dashed #E2E8F0; vertical-align: middle; }
table.scores td.sc, table.scores th.sc { text-align: right; white-space: nowrap; }
table.scores td.pl { max-width: 240px; }
table.scores .p-nm { line-height: 1.2; }
table.scores .p-loc { font-family: var(--round); font-size: 0.7rem; font-weight: 600; color: #475569; margin-top: 1px; line-height: 1.1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
table.scores tr.me td { background: #FEF3C7; color: #92400E; font-weight: 700; }
table.scores tr.me .p-loc { color: #92400E; }
table.scores tr.gap td { text-align: center; color: var(--muted); border-bottom: none; padding: 2px; }
.src { font-size: 0.7rem; font-weight: 600; color: var(--muted); margin: 2px 0 3px; }

/* Ergonomic Arcade / Mobile Touch Controls Bar */
.controls {
  width: min(100%, 920px);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding: 2px 4px;
}
.ctrl-group {
  display: flex;
  align-items: center;
  gap: 8px;
}
.controls button {
  height: 54px;
  min-width: 68px;
  padding: 0 14px;
  border-radius: 15px;
  border: 3px solid #0F172A;
  font-family: var(--round);
  font-weight: 700;
  color: var(--ink);
  box-shadow: 0 4px 0 #0F172A;
  cursor: pointer;
  touch-action: manipulation;
}
.controls button:active,
.controls button.pressed {
  transform: translateY(2px);
  box-shadow: 0 1px 0 #0F172A;
}
.controls .mv {
  width: 74px;
  background: #FFFFFF;
  font-size: 1.35rem;
}
.controls .jump {
  background: #38BDF8;
  color: #0F172A;
  font-size: 0.96rem;
}
.controls .sweep {
  background: #FACC15;
  color: #0F172A;
  font-size: 0.96rem;
}

footer {
  flex: 0 0 auto;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 0 12px;
  font-size: 0.68rem;
  color: #64748B;
  line-height: 1.2;
}
footer a { color: var(--green-dark); font-weight: 700; text-decoration: none; }

/* Mobile Portrait: full-width crisp 800x480 stage + roomy touch deck below + spacious full-viewport overlays! */
@media (max-width: 700px) {
  .wrap { justify-content: flex-start; padding: 8px 6px 6px; gap: 10px; }
  .stage { width: 100%; border-radius: 14px; }
  .ui { position: fixed; inset: 50px 0 0 0; padding: 12px; }
  .legend { grid-template-columns: 1fr; }
  .hero-grid { gap: 6px; }
  .hero-card svg { width: 46px; height: 42px; }
  .controls { gap: 8px; padding: 6px 2px; }
  .controls button { height: 60px; min-width: 58px; padding: 0 12px; }
  .controls .mv { width: 66px; }
  .controls .jump, .controls .sweep { font-size: 0.88rem; }
  .victory-banner { flex-direction: column; text-align: center; }
}

/* Mobile Landscape / Short viewports: full-height stage with sleek translucent side touch pads */
@media (max-height: 540px) {
  .bar { height: 42px; }
  footer { display: none; }
  .wrap { padding: 4px 6px; }
  .stage {
    width: min(100vw - 12px, calc((100dvh - 42px - 10px) * 800 / 480));
    border-radius: 12px;
  }
  .ui { position: fixed; inset: 42px 0 0 0; padding: 8px; }
  .controls {
    position: fixed;
    left: 10px;
    right: 10px;
    bottom: 8px;
    width: auto;
    pointer-events: none;
    z-index: 15;
  }
  .controls button {
    pointer-events: auto;
    height: 46px;
    opacity: 0.88;
    font-size: 0.86rem;
  }
  .controls .mv { width: 56px; font-size: 1.15rem; }
}
