:root {
  --green: #059669;
  --green-dark: #047857;
  --green-soft: #D1FAE5;
  --blue: #0284C7;
  --amber: #D97706;
  --red: #DC2626;
  --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) 60%, #E0F2FE 100%);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
.bar {
  flex: 0 0 auto;
  height: 52px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 0 14px;
  background: #FFFFFF;
  border-bottom: 2px solid var(--green-soft);
}
.bar a.back { color: var(--green-dark); font-weight: 700; text-decoration: none; font-size: 0.95rem; }
.bar .ttl { font-weight: 700; letter-spacing: 0.08em; color: var(--ink); font-size: 1rem; }
.bar button {
  font-family: var(--round);
  border: 2px solid var(--green-soft);
  background: #FFFFFF;
  border-radius: 999px;
  font-size: 1.05rem;
  padding: 4px 12px;
  cursor: pointer;
}
.wrap {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px 0 4px;
}
.stage {
  position: relative;
  width: min(100vw, calc((100dvh - 52px - 44px) * 480 / 760));
  aspect-ratio: 480 / 760;
  border-radius: 18px;
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(15, 23, 42, 0.18);
  border: 3px solid #FFFFFF;
  background: #DFF4FF;
}
canvas#game { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.ui { position: absolute; inset: 0; display: flex; flex-direction: column; pointer-events: none; }
.ui.hidden { display: none; }
.ui > * { pointer-events: auto; }

/* Intro */
.intro { justify-content: space-between; }
.logo { text-align: center; padding-top: 12px; pointer-events: none; }
.logo h1 {
  margin: 0;
  font-family: var(--round);
  font-weight: 700;
  font-size: clamp(2rem, 9.5vw, 3.4rem);
  line-height: 1;
  letter-spacing: 0.02em;
  color: #FFFFFF;
  -webkit-text-stroke: 3px #0F172A;
  paint-order: stroke fill;
  text-shadow: 0 4px 0 rgba(15, 23, 42, 0.35);
  transform: rotate(-2deg);
}
.logo p {
  margin: 6px 0 0;
  font-size: clamp(0.7rem, 2.8vw, 0.9rem);
  font-weight: 600;
  color: #0F172A;
  background: rgba(255, 255, 255, 0.85);
  display: inline-block;
  padding: 2px 12px;
  border-radius: 999px;
}
.intro-bottom { padding: 0 12px 12px; display: flex; flex-direction: column; align-items: center; gap: 8px; }
.btn {
  font-family: var(--round);
  font-weight: 700;
  border: 3px solid #0F172A;
  border-radius: 14px;
  cursor: pointer;
  padding: 10px 22px;
  font-size: 1.1rem;
  box-shadow: 0 4px 0 #0F172A;
  transition: transform 0.08s ease, box-shadow 0.08s ease;
}
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 #0F172A; }
.btn.primary { background: var(--green); color: #FFFFFF; font-size: 1.35rem; padding: 12px 30px; }
.btn.alt { background: #FFFFFF; color: var(--ink); }
.btn.warn { background: #FEF3C7; color: #92400E; }
.btn.small { font-size: 0.9rem; padding: 7px 14px; }
a.btn { display: inline-flex; align-items: center; justify-content: center; line-height: 1.2; text-decoration: none; }
.row { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; }
.legend {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px 10px;
  background: rgba(255, 255, 255, 0.92);
  border: 2px solid var(--green-soft);
  border-radius: 12px;
  padding: 7px 10px;
  font-size: clamp(0.64rem, 2.5vw, 0.8rem);
  font-weight: 600;
  color: #1E293B;
  width: 100%;
}
.legend b.g { color: var(--green-dark); }
.legend b.r { color: var(--red); }
.hint { font-size: 0.72rem; color: #334155; background: rgba(255,255,255,.8); padding: 1px 10px; border-radius: 999px; }

/* Panels & Game Over */
.center { justify-content: center; align-items: center; padding: 14px; background: rgba(15, 23, 42, 0.25); }
.panel {
  width: 100%;
  max-height: 100%;
  overflow-y: auto;
  touch-action: pan-y;
  background: rgba(255, 255, 255, 0.97);
  border: 3px solid #0F172A;
  border-radius: 18px;
  padding: 12px 14px 14px;
  box-shadow: 0 8px 0 rgba(15, 23, 42, 0.25);
  text-align: center;
}
.panel h2 { margin: 0 0 4px; font-size: clamp(1.3rem, 6vw, 1.85rem); line-height: 1.05; }
.panel .quip { color: var(--muted); font-weight: 600; margin: 0 0 6px; font-size: 0.88rem; }
.stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: 5px; margin: 6px 0 8px; }
.stat { background: #F0FDF4; border: 2px solid var(--green-soft); border-radius: 10px; padding: 4px; }
.stat small { display: block; color: var(--muted); font-weight: 600; font-size: 0.64rem; text-transform: uppercase; letter-spacing: 0.05em; }
.stat strong { font-family: var(--arcade); font-size: 0.76rem; color: var(--ink); }
.stat.wide { grid-column: 1 / -1; }
.stat.big { grid-column: 1 / -1; background: #FEF3C7; border-color: #FCD34D; }
.stat.big strong { font-size: 1.1rem; }
label.nm { display: block; font-family: var(--arcade); font-size: 0.58rem; margin: 6px 0 4px; color: var(--green-dark); }
input#nameInput {
  font-family: var(--arcade);
  font-size: 1rem;
  text-transform: uppercase;
  text-align: center;
  letter-spacing: 0.12em;
  width: 100%;
  padding: 8px 6px;
  border: 3px solid var(--green);
  border-radius: 12px;
  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.92rem;
  text-align: center;
  padding: 7px 10px;
  border: 2.5px solid #94A3B8;
  border-radius: 12px;
  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.85rem;
  padding: 0 10px;
  border: 2.5px solid var(--green);
  border-radius: 12px;
  background: #ECFDF5;
  color: var(--green-dark);
  cursor: pointer;
}
.geo-btn:disabled { opacity: 0.55; cursor: wait; }
.geo-pill {
  display: inline-block;
  font-size: 0.72rem;
  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.78rem; min-height: 1.1em; margin: 4px 0; }
.newstk { font-weight: 700; font-size: 0.85rem; color: #92400E; background: #FEF3C7; border: 2px solid #FCD34D; border-radius: 10px; padding: 4px 8px; margin: 4px 0 8px; display: none; }

/* High scores */
.board h2 { font-size: clamp(1.15rem, 5.4vw, 1.55rem); }
.period-tabs {
  display: inline-flex;
  gap: 4px;
  padding: 3px;
  margin: 4px 0 3px;
  background: #F1F5F9;
  border: 2px solid #0F172A;
  border-radius: 999px;
}
.period-btn {
  font-family: var(--round);
  font-size: 0.78rem;
  font-weight: 700;
  padding: 4px 13px;
  border: none;
  border-radius: 999px;
  background: transparent;
  color: #334155;
  cursor: pointer;
  transition: background 0.12s ease, color 0.12s ease;
}
.period-btn.on {
  background: var(--green);
  color: #FFFFFF;
  box-shadow: 0 2px 0 #0F172A;
}
.board-pills {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 5px;
  margin: 2px 0 2px;
}
.reset-pill {
  display: inline-block;
  font-size: 0.7rem;
  font-weight: 700;
  color: #92400E;
  background: #FEF3C7;
  border: 1.5px solid #FCD34D;
  border-radius: 999px;
  padding: 2px 9px;
  margin: 4px 0 2px;
  white-space: nowrap;
}
.reset-pill.alltime {
  color: #1E40AF;
  background: #DBEAFE;
  border-color: #93C5FD;
}
.area-filter {
  margin: 6px 0 4px;
  padding: 6px 7px;
  background: #F8FAFC;
  border: 2px solid var(--green-soft);
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  gap: 5px;
}
.area-chips {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 4px;
}
.area-chip {
  font-family: var(--round);
  font-size: 0.72rem;
  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.78rem;
  font-weight: 600;
  padding: 5px 8px;
  border: 1.5px solid #CBD5E1;
  border-radius: 9px;
  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: 6px 0; font-family: var(--arcade); font-size: clamp(0.5rem, 2.5vw, 0.68rem); }
table.scores th { color: var(--muted); font-size: 0.52rem; text-align: left; padding: 4px 5px; border-bottom: 2px solid var(--green-soft); }
table.scores td { padding: 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.72rem; font-weight: 600; color: #475569; margin-top: 2px; 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.72rem; font-weight: 600; color: var(--muted); margin: 2px 0 4px; }

/* School quiz */
.ui.bottom { justify-content: flex-end; padding: 10px; }
.quiz { padding: 10px 12px 12px; max-height: 66%; overflow-y: auto; text-align: left; touch-action: pan-y; }
.q-top { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.q-prog { font-family: var(--arcade); font-size: 0.55rem; color: var(--green-dark); }
.q-cat { font-size: 0.7rem; font-weight: 700; background: #E0F2FE; color: #075985; border-radius: 999px; padding: 2px 10px; }
.q-text { font-size: clamp(0.95rem, 4.2vw, 1.15rem); font-weight: 700; margin: 6px 0 8px; line-height: 1.2; }
.q-opts { display: grid; gap: 6px; }
.q-opt { font-family: var(--round); font-weight: 600; font-size: clamp(0.8rem, 3.6vw, 0.98rem); text-align: left; padding: 8px 10px; border: 2.5px solid #CBD5E1; border-radius: 12px; background: #FFFFFF; color: var(--ink); cursor: pointer; line-height: 1.15; }
.q-opt:active { transform: translateY(1px); }
.q-opt.right { border-color: var(--green); background: #D1FAE5; }
.q-opt.wrong { border-color: var(--red); background: #FEE2E2; }
.q-opt[disabled] { cursor: default; }
.q-fb { font-size: 0.85rem; font-weight: 600; color: #1E293B; margin: 8px 0 4px; min-height: 1.2em; line-height: 1.2; }
.q-fb b.ok { color: var(--green-dark); }
.q-fb b.no { color: var(--red); }

/* Garage */
.garage { background: #FFFFFF; max-height: 100%; display: flex; flex-direction: column; padding: 10px 10px 12px; }
.g-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.g-head h2 { font-size: clamp(1.1rem, 5vw, 1.5rem); margin: 0; }
.g-caps { font-family: var(--arcade); font-size: 0.75rem; background: #FEF3C7; border: 2px solid #FCD34D; border-radius: 999px; padding: 5px 10px; }
.g-prev { align-self: center; width: 150px; height: 143px; margin: 4px 0; background: linear-gradient(180deg,#DFF4FF,#ECFDF5); border: 2px solid var(--green-soft); border-radius: 14px; }
.g-tabs { display: flex; gap: 8px; justify-content: center; margin: 4px 0 6px; }
.g-tabs .btn.on { background: var(--green); color: #FFFFFF; }
.g-body { overflow-y: auto; flex: 1 1 auto; min-height: 90px; text-align: left; padding-right: 2px; touch-action: pan-y; }
.g-set { font-family: var(--arcade); font-size: 0.5rem; color: var(--green-dark); margin: 8px 0 4px; }
.g-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.g-card { border: 2px solid var(--green-soft); background: #FFFFFF; border-radius: 12px; padding: 4px 3px 5px; text-align: center; cursor: pointer; font-family: var(--round); }
.g-card canvas { width: 54px; height: 60px; display: block; margin: 0 auto; }
.g-card .nm2 { font-size: 0.68rem; font-weight: 700; line-height: 1.05; min-height: 1.4em; color: var(--ink); }
.g-card .pr { font-family: var(--arcade); font-size: 0.5rem; margin-top: 2px; color: #92400E; }
.g-card.eq { border-color: var(--green); background: #ECFDF5; }
.g-card.locked { opacity: 0.55; cursor: default; }
.g-card.cant .pr { color: var(--red); }
.g-slots { display: flex; gap: 6px; justify-content: center; margin-bottom: 6px; }
.g-slot { width: 46px; height: 46px; border-radius: 50%; border: 3px dashed #94A3B8; background: #FFFFFF; font-size: 1.4rem; cursor: pointer; }
.g-slot.sel { border: 3px solid var(--green); background: #ECFDF5; }
.g-stk { font-size: 1.6rem; line-height: 1; }
.g-card.locked .g-stk { filter: grayscale(1); }
.g-card .hint2 { font-size: 0.55rem; color: var(--muted); font-weight: 600; line-height: 1.05; }

/* Touch & Footer */
.touch { position: absolute; left: 0; right: 0; bottom: 8px; display: none; justify-content: space-between; padding: 0 10px; pointer-events: none; }
.touch button { pointer-events: auto; width: 22%; max-width: 90px; aspect-ratio: 1; border-radius: 50%; border: 3px solid #0F172A; background: rgba(255,255,255,0.75); font-size: 1.6rem; font-weight: 700; color: var(--ink); }
.touch.on { display: flex; }
footer { flex: 0 0 auto; height: 44px; 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; }
@media (max-height: 600px) { footer { display: none; } .stage { width: min(100vw, calc((100dvh - 52px - 8px) * 480 / 760)); } }
