/* Shoelace: graph paper, ink and a red pencil. Mobile first. */
:root {
  --paper: #fbfaf5;
  --grid: rgba(42, 92, 170, 0.07);
  --ink: #1d2a4d;
  --muted: #5b6482;
  --line: #d9dbe4;
  --accent: #d1495b;
  --mine: #2a6fdb;
  --theirs: #8a5cd6;
  --ok: #23845a;
  --warn: #b7791f;
  --radius: 14px;
  --tap: 52px;
  --tabbar: 64px;
  --topbar: 52px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, monospace;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
  background: var(--paper);
  -webkit-text-size-adjust: 100%;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; }
body { overscroll-behavior: none; }
[hidden] { display: none !important; }
a { color: var(--mine); }
button, input { font: inherit; color: inherit; }

.paper {
  background-color: var(--paper);
  background-image:
    linear-gradient(var(--grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: 22px 22px;
}

/* ---------- Layout ---------- */
#app { position: fixed; inset: 0; display: flex; flex-direction: column; }

.topbar {
  height: var(--topbar);
  display: flex; align-items: center; gap: 10px;
  padding: 0 14px;
  background: var(--ink); color: #fff;
  z-index: 1000;
  flex: none;
}
.brand { display: flex; align-items: center; gap: 8px; color: #fff; text-decoration: none; font-weight: 700; letter-spacing: 0.02em; font-size: 1.1rem; }
.brand img { border-radius: 7px; background: var(--paper); }
.dev-badge { background: var(--accent); color: #fff; font-size: 0.72rem; font-weight: 800; padding: 3px 7px; border-radius: 6px; letter-spacing: 0.06em; }
.score-chip { margin-left: auto; font-family: var(--mono); font-size: 0.95rem; background: rgba(255,255,255,0.12); padding: 5px 10px; border-radius: 999px; }

/* Its own stacking context: Leaflet gives its controls z-index 1000, which would otherwise poke through pages and sheets. */
#map { flex: 1; min-height: 0; background: #e8e6df; isolation: isolate; }

.tabbar {
  flex: none;
  height: calc(var(--tabbar) + var(--safe-b));
  padding-bottom: var(--safe-b);
  display: grid; grid-template-columns: repeat(4, 1fr);
  background: #fff; border-top: 1px solid var(--line);
  z-index: 1000;
}
.tabbar a {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--muted); text-decoration: none; font-size: 0.75rem; font-weight: 600;
}
.tabbar svg { width: 26px; height: 26px; }
.tabbar a.active { color: var(--ink); }
.tabbar a.active svg { color: var(--accent); }

/* Full-screen pages over the map (fields, board, report, help). */
.page {
  position: absolute; left: 0; right: 0;
  top: var(--topbar); bottom: calc(var(--tabbar) + var(--safe-b));
  overflow-y: auto; -webkit-overflow-scrolling: touch;
  z-index: 900;
}
.page-inner { max-width: 760px; margin: 0 auto; padding: 18px 16px 72px; } /* room to scroll the last line clear of the signature */
.page h1 { font-size: 1.5rem; margin: 4px 0 14px; }
.page h2 { font-size: 1.1rem; margin: 26px 0 10px; }

/* ---------- Map HUD ---------- */
.map-hud { position: absolute; right: 12px; bottom: calc(var(--tabbar) + var(--safe-b) + 14px); z-index: 800; display: flex; flex-direction: column; align-items: flex-end; gap: 10px; pointer-events: none; }
.map-hud > * { pointer-events: auto; }
.fab {
  width: var(--tap); height: var(--tap); border-radius: 50%;
  border: none; background: #fff; color: var(--ink);
  box-shadow: 0 2px 10px rgba(0,0,0,0.25);
  display: grid; place-items: center; cursor: pointer;
}
.status:empty { display: none; }
.status {
  max-width: min(320px, calc(100vw - 24px));
  background: rgba(29, 42, 77, 0.92); color: #fff;
  font-size: 0.86rem; padding: 8px 12px; border-radius: 10px;
}
.status button { margin-left: 8px; background: none; border: 1px solid rgba(255,255,255,0.6); color: #fff; border-radius: 8px; padding: 3px 8px; cursor: pointer; }

.link-banner {
  position: absolute; left: 12px; right: 12px; top: calc(var(--topbar) + 10px); z-index: 850;
  background: var(--accent); color: #fff; border-radius: 12px; padding: 10px 12px;
  display: flex; align-items: center; gap: 10px; font-weight: 600;
  box-shadow: 0 3px 12px rgba(0,0,0,0.2);
}
.link-banner span { flex: 1; }
.link-banner button { background: #fff; color: var(--accent); border: none; border-radius: 8px; padding: 8px 12px; font-weight: 700; cursor: pointer; }

/* The coach: one line on top of the map saying what to do next. */
.coach {
  position: absolute; left: 56px; right: 12px; top: calc(var(--topbar) + 10px); z-index: 840;
  background: #fff; color: var(--ink); border: 2px solid var(--ink); border-radius: 12px;
  padding: 9px 12px; font-size: 0.95rem; line-height: 1.35;
  display: flex; align-items: flex-start; gap: 10px;
  box-shadow: 0 3px 12px rgba(0,0,0,0.18);
}
.coach .step { flex: none; font-family: var(--mono); font-weight: 700; color: var(--accent); }
.coach span { flex: 1; }
.coach .mine { color: var(--mine); font-weight: 700; }
.coach button { flex: none; background: none; border: none; font-size: 1.3rem; line-height: 1; color: var(--muted); cursor: pointer; padding: 0 2px; }

/* The dedication: a small ink pill in the bottom-left corner, its triangle drawn once in red pencil. */
.signature {
  position: absolute; left: 12px; bottom: calc(var(--tabbar) + var(--safe-b) + 14px); z-index: 920;
  margin: 0; padding: 6px 13px 6px 9px; display: flex; align-items: center; gap: 8px;
  background: linear-gradient(135deg, #1d2a4d, #2c3d6b); color: #e9ecf7;
  border: 1px solid rgba(255,255,255,0.14); border-radius: 999px;
  box-shadow: 0 3px 12px rgba(29,42,77,0.35), inset 0 1px 0 rgba(255,255,255,0.12);
  font-family: var(--mono); font-size: 0.74rem; letter-spacing: 0.03em; white-space: nowrap;
  pointer-events: none; user-select: none;
}
.signature b {
  font-weight: 800;
  background: linear-gradient(90deg, #ff8a7a, #d1495b 45%, #f2c14e);
  -webkit-background-clip: text; background-clip: text; color: transparent;
}
.signature .c { opacity: 0.6; }
.signature svg { flex: none; overflow: visible; }
.signature .tri { fill: rgba(209,73,91,0.22); stroke: #ff7b6e; stroke-width: 1.8; stroke-linejoin: round; stroke-dasharray: 56; stroke-dashoffset: 56; animation: sig-draw 1.6s 0.4s ease-out forwards; }
.signature circle { fill: #fff; stroke: #d1495b; stroke-width: 1.2; }
@keyframes sig-draw { to { stroke-dashoffset: 0; } }
@media (prefers-reduced-motion: reduce) { .signature .tri { animation: none; stroke-dashoffset: 0; } }

/* ---------- Map markers ---------- */
.node-icon { background: none; border: none; }
.node-pin {
  transform: translate(-50%, -50%);
  position: absolute; left: 50%; top: 50%;
  min-width: 46px; height: 30px; padding: 0 7px;
  display: grid; place-items: center;
  font-family: var(--mono); font-weight: 700; font-size: 14px;
  color: var(--ink); background: #fff;
  border: 2.5px solid var(--ink); border-radius: 9px;
  box-shadow: 0 1px 4px rgba(0,0,0,0.3);
  white-space: nowrap;
}
.node-pin.captured { background: var(--mine); border-color: var(--mine); color: #fff; }
.node-pin.in-range { border-color: var(--accent); box-shadow: 0 0 0 4px rgba(209,73,91,0.35); }
.node-pin.selected { outline: 3px solid var(--accent); outline-offset: 2px; }
.node-pin.dim { opacity: 0.55; }

.player-dot { width: 18px; height: 18px; border-radius: 50%; background: var(--accent); border: 3px solid #fff; box-shadow: 0 0 0 2px var(--accent), 0 2px 6px rgba(0,0,0,0.4); }
.player-dot.teleported { background: var(--warn); box-shadow: 0 0 0 2px var(--warn), 0 2px 6px rgba(0,0,0,0.4); }

/* ---------- Bottom sheet ---------- */
.sheet {
  position: absolute; left: 0; right: 0; bottom: calc(var(--tabbar) + var(--safe-b));
  max-height: min(78%, 640px); overflow-y: auto;
  background: #fff; border-radius: 18px 18px 0 0;
  box-shadow: 0 -6px 24px rgba(0,0,0,0.18);
  z-index: 950; padding: 18px 18px 22px;
  animation: sheet-in 0.18s ease-out;
}
@keyframes sheet-in { from { transform: translateY(24px); opacity: 0; } to { transform: none; opacity: 1; } }
.sheet-close { position: absolute; right: 8px; top: 6px; width: 44px; height: 44px; border: none; background: none; font-size: 28px; color: var(--muted); cursor: pointer; }
.sheet h2 { margin: 0 40px 4px 0; font-size: 1.2rem; }
.sheet .kind { color: var(--muted); font-size: 0.9rem; margin: 0 0 12px; }

.bignum { font-family: var(--mono); font-size: 2.6rem; font-weight: 800; line-height: 1; letter-spacing: 0.02em; }
.node-head { display: flex; align-items: center; gap: 16px; margin-bottom: 12px; }
.node-head .bignum { background: var(--paper); border: 2px solid var(--ink); border-radius: 12px; padding: 8px 12px; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; touch-action: manipulation; }
.node-head.captured .bignum { background: var(--mine); border-color: var(--mine); color: #fff; }

.meta { display: flex; flex-wrap: wrap; gap: 6px 14px; color: var(--muted); font-size: 0.92rem; margin: 6px 0 14px; }
.meta b { color: var(--ink); }

/* ---------- Controls ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: var(--tap); padding: 0 20px;
  border-radius: 12px; border: 2px solid var(--ink);
  background: var(--ink); color: #fff; font-weight: 700; font-size: 1.02rem;
  cursor: pointer; text-decoration: none;
}
.btn.block { display: flex; width: 100%; }
.btn.secondary { background: #fff; color: var(--ink); }
.btn.accent { background: var(--accent); border-color: var(--accent); }
.btn.ghost { background: none; border-color: transparent; color: var(--muted); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-row { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 12px; }
.btn-row .btn { flex: 1; }

.field { display: block; margin: 0 0 14px; }
.field span { display: block; font-size: 0.86rem; font-weight: 600; color: var(--muted); margin-bottom: 5px; }
.field input {
  width: 100%; min-height: var(--tap); padding: 10px 14px;
  border: 2px solid var(--line); border-radius: 12px; background: #fff; font-size: 1.1rem;
}
.field input:focus { outline: none; border-color: var(--mine); }
.answer input { font-family: var(--mono); font-size: 1.5rem; text-align: center; letter-spacing: 0.04em; }

.note { font-size: 0.92rem; color: var(--muted); }
.error-msg { color: var(--accent); font-weight: 600; margin: 8px 0; }
.ok-msg { color: var(--ok); font-weight: 700; margin: 8px 0; }

/* ---------- Puzzle ---------- */
.puzzle-prompt { font-size: 1.25rem; line-height: 1.4; margin: 6px 0 16px; font-weight: 600; }
.hint { margin: -4px 0 12px; }
.hint summary { cursor: pointer; color: var(--mine); font-weight: 600; min-height: 32px; }
.explain { background: var(--paper); border-left: 4px solid var(--mine); padding: 10px 12px; border-radius: 8px; font-family: var(--mono); font-size: 0.92rem; line-height: 1.5; margin: 10px 0; overflow-wrap: anywhere; }
.celebrate { font-size: 1.3rem; font-weight: 800; color: var(--ok); }

/* ---------- Toasts ---------- */
.toasts { position: absolute; left: 0; right: 0; top: calc(var(--topbar) + 10px + var(--coach-h, 0px)); z-index: 1100; display: flex; flex-direction: column; align-items: center; gap: 8px; pointer-events: none; }
.toast { pointer-events: auto; background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 12px; box-shadow: 0 4px 14px rgba(0,0,0,0.25); max-width: calc(100vw - 24px); font-weight: 600; animation: sheet-in 0.18s ease-out; }
.toast.error { background: var(--accent); }
.toast.good { background: var(--ok); }
.toast a { color: #fff; }

/* ---------- Lists, cards ---------- */
.card { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; }
.list { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.list a.card { display: flex; align-items: center; gap: 14px; text-decoration: none; color: var(--ink); }
.list .tri { flex: none; width: 46px; height: 46px; }
.list summary.card { display: flex; align-items: center; gap: 14px; cursor: pointer; list-style: none; }
.list summary.card::-webkit-details-marker { display: none; }
.list summary.card::after { content: "›"; flex: none; margin-left: -4px; color: var(--muted); font-size: 1.4rem; line-height: 1; transition: transform 0.15s; }
.list details[open] > summary.card { border-color: var(--mine); }
.list details[open] > summary.card::after { transform: rotate(90deg); }
.shape-num { flex: none; width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center; background: #e3ecfb; color: var(--mine); font-family: var(--mono); font-weight: 800; }
.list details > .list { margin: 8px 0 4px 18px; padding-left: 12px; border-left: 2px solid var(--line); }
.list .grow { flex: 1; min-width: 0; }
.list .title, .list .sub { display: block; }
.list .title { font-weight: 700; font-family: var(--mono); }
.list .sub { color: var(--muted); font-size: 0.86rem; margin-top: 2px; }
.list .score { font-family: var(--mono); font-weight: 800; font-size: 1.1rem; text-align: right; }
.tags { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.tag { font-size: 0.72rem; font-weight: 700; background: #fde8eb; color: var(--accent); padding: 2px 7px; border-radius: 999px; }
.empty { color: var(--muted); text-align: center; padding: 30px 10px; }

table.board { width: 100%; border-collapse: collapse; background: #fff; border-radius: var(--radius); overflow: hidden; border: 1px solid var(--line); }
table.board th, table.board td { padding: 12px 10px; text-align: right; border-bottom: 1px solid var(--line); }
table.board th:first-child, table.board td:first-child { text-align: left; }
table.board th { font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); background: var(--paper); }
table.board td { font-family: var(--mono); }
table.board td:first-child { font-family: inherit; font-weight: 700; }
table.board tr:last-child td { border-bottom: none; }

/* ---------- Auth ---------- */
.auth { max-width: 420px; margin: 0 auto; padding: 28px 18px 40px; }
.auth .logo { display: flex; align-items: center; gap: 12px; margin-bottom: 8px; }
.auth .logo h1 { margin: 0; font-size: 2rem; }
.auth .tagline { color: var(--muted); margin: 0 0 22px; line-height: 1.45; }
.auth .switch { margin-top: 16px; text-align: center; }
.auth .switch button { background: none; border: none; color: var(--mine); font-weight: 600; cursor: pointer; min-height: 44px; }

/* ---------- Field Report ---------- */
.report { font-size: 1rem; line-height: 1.5; }
.report-head { display: grid; grid-template-columns: 1fr auto; gap: 4px 16px; align-items: end; border-bottom: 2px solid var(--ink); padding-bottom: 12px; margin-bottom: 6px; }
.report-head h1 { margin: 0; font-family: var(--mono); font-size: 1.35rem; }
.report-head .by { color: var(--muted); font-size: 0.9rem; }
.report-head .total { grid-row: span 2; text-align: right; }
.report-head .total .bignum { color: var(--accent); font-size: 2.2rem; }
.report-head .total small { display: block; color: var(--muted); font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.06em; }
.report section { margin-top: 18px; }
.report h2 { font-size: 1rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin: 0 0 8px; display: flex; align-items: baseline; gap: 8px; }
.report h2 .step { font-family: var(--mono); background: var(--ink); color: #fff; border-radius: 6px; padding: 0 7px; font-size: 0.85rem; }
.report .lead { margin: 0 0 8px; }
.report-grid { display: grid; gap: 16px; }
.math { overflow-x: auto; overflow-y: hidden; padding: 4px 0; -webkit-overflow-scrolling: touch; }
.math .katex-display { margin: 0.4em 0; }
.math-fallback { font-family: var(--mono); white-space: pre-wrap; }
table.nums { border-collapse: collapse; width: 100%; font-family: var(--mono); font-size: 0.9rem; }
table.nums th, table.nums td { padding: 6px 8px; text-align: right; border-bottom: 1px solid var(--line); white-space: nowrap; }
table.nums th:first-child, table.nums td:first-child { text-align: left; }
table.nums th { font-family: system-ui, sans-serif; font-size: 0.75rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.table-wrap { overflow-x: auto; }
.diagram { width: 100%; max-width: 360px; height: auto; display: block; margin: 0 auto; }
.diagram text { font-family: var(--mono); }
.bonus-list { list-style: none; padding: 0; margin: 0; display: grid; gap: 8px; }
.bonus-list li { display: grid; grid-template-columns: 30px 1fr auto; gap: 8px; align-items: start; padding: 10px 12px; border-radius: 10px; background: #fff; border: 1px solid var(--line); }
.bonus-list li.hit { border-color: var(--ok); background: #eef8f2; }
.bonus-list .mark { font-size: 1.2rem; line-height: 1.3; text-align: center; }
.bonus-list .name { font-weight: 700; }
.bonus-list .detail { color: var(--muted); font-size: 0.9rem; }
.bonus-list .mult { font-family: var(--mono); font-weight: 800; }
.bonus-list li:not(.hit) .mult { color: var(--muted); font-weight: 600; }
.pct { font-family: var(--mono); font-weight: 700; }

/* ---------- Help ---------- */
.help ol { padding-left: 1.3em; }
.help li { margin: 0 0 10px; line-height: 1.5; }
.help .rules { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; }
.help .rules .card b { display: block; font-family: var(--mono); font-size: 1.3rem; }
.invite-code { font-family: var(--mono); font-size: 1.6rem; font-weight: 800; letter-spacing: 0.1em; text-align: center; padding: 12px; background: var(--paper); border: 2px dashed var(--ink); border-radius: 12px; margin: 10px 0; user-select: all; }

.leaflet-container { font: inherit; }
.leaflet-control-attribution { font-size: 11px; }

/* A switch row: large tap target, native checkbox restyled as a pill. */
.toggle { display: flex; align-items: center; gap: 14px; margin: 10px 0 0; padding: 12px 14px; background: #fff; border: 1px solid var(--line); border-radius: var(--radius); cursor: pointer; min-height: var(--tap); }
.toggle input { appearance: none; flex: none; width: 50px; height: 30px; margin: 0; border-radius: 999px; background: var(--line); position: relative; cursor: pointer; transition: background 0.2s; }
.toggle input::after { content: ""; position: absolute; top: 3px; left: 3px; width: 24px; height: 24px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,0.3); transition: transform 0.2s; }
.toggle input:checked { background: var(--ok); }
.toggle input:checked::after { transform: translateX(20px); }
.toggle input:disabled { opacity: 0.6; }
.toggle .note { font-size: 0.85rem; }
[data-slot=invite] + .toggle { margin-top: 18px; }

/* ---------- Wider screens: side panel instead of a bottom sheet ---------- */
@media (min-width: 900px) {
  :root { --tabbar: 0px; }
  #app { display: grid; grid-template-columns: 220px 1fr; grid-template-rows: var(--topbar) 1fr; }
  .topbar { grid-column: 1 / -1; }
  .tabbar {
    grid-row: 2; grid-column: 1;
    height: auto; padding: 14px 10px; display: flex; flex-direction: column; gap: 4px;
    border-top: none; border-right: 1px solid var(--line);
  }
  .tabbar a { flex-direction: row; justify-content: flex-start; gap: 12px; padding: 10px 12px; border-radius: 10px; font-size: 0.98rem; }
  .tabbar a.active { background: var(--paper); }
  #map { grid-row: 2; grid-column: 2; }
  .page { left: 220px; bottom: 0; }
  .sheet { left: auto; right: 16px; bottom: 16px; top: auto; width: 400px; border-radius: 16px; max-height: calc(100% - var(--topbar) - 32px); }
  .map-hud { bottom: 20px; right: 20px; }
  #app.sheet-open .map-hud { right: 436px; }
  .link-banner { left: 236px; right: auto; max-width: 560px; }
  .coach { left: 276px; right: auto; max-width: 560px; }
  .toasts { left: 220px; }
  .signature { left: 16px; bottom: 16px; z-index: 1010; }
  .report-grid.two { grid-template-columns: 1fr 1fr; align-items: start; }
}

/* ---------- Print: Field Reports for a classroom ---------- */
@media print {
  html, body { overflow: visible; height: auto; }
  #app { position: static; display: block; }
  .topbar, .tabbar, #map, .map-hud, .sheet, .toasts, .link-banner, .coach, .signature, .no-print { display: none !important; }
  .page { position: static; overflow: visible; }
  .page-inner { max-width: none; padding: 0; }
  .paper { background: #fff; }
  .report section { break-inside: avoid; }
}
