:root {
  --bg: #14170f;
  --surface: #1e2318;
  --surface-2: #2a3121;
  --line: #3a4330;
  --text: #e8ead9;
  --muted: #9da38a;
  --accent: #c9a227;
  --accent-ink: #1a1505;
  --seeker: #d9452b;
  --hider: #3d8bd9;
  --referee: #c9a227;
  --danger: #e0533a;
  --ok: #5fbf5a;
  --radius: 10px;
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, monospace;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 15px/1.4 var(--font); }
body { overscroll-behavior: none; -webkit-tap-highlight-color: transparent; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--danger); min-height: 1.4em; }

button, input, select { font: inherit; color: inherit; }
button {
  background: var(--surface-2); border: 1px solid var(--line); border-radius: 8px; padding: 8px 12px; cursor: pointer;
}
button:hover { border-color: var(--muted); }
button:disabled { opacity: 0.5; cursor: default; }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 700; }
button.danger { background: transparent; border-color: var(--danger); color: var(--danger); }
button.small { padding: 4px 8px; font-size: 13px; }
input, select {
  background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 9px 10px; min-width: 0;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.screen { position: fixed; inset: 0; }

/* Home */
#home { overflow: auto; background: radial-gradient(ellipse at top, #2b331f, var(--bg) 70%); }
.home-inner { max-width: 440px; margin: 0 auto; padding: max(32px, env(safe-area-inset-top)) 16px 32px; display: grid; gap: 14px; }
.brand { font: 900 44px/1 var(--mono); letter-spacing: 0.3em; margin: 0; text-align: center; color: var(--accent); }
.tagline { text-align: center; margin: -6px 0 8px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.15em; font-size: 12px; }
.field { display: grid; gap: 4px; font-size: 12px; text-transform: uppercase; letter-spacing: 0.1em; color: var(--muted); }
.field input { font-size: 16px; text-transform: none; letter-spacing: 0; color: var(--text); }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; display: grid; gap: 10px; }
.card h2 { margin: 0; font-size: 13px; text-transform: uppercase; letter-spacing: 0.12em; color: var(--muted); }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row > input { flex: 1; }
.inline { display: flex; gap: 8px; align-items: center; color: var(--muted); }
.code-input { font: 700 22px var(--mono); letter-spacing: 0.3em; text-transform: uppercase; text-align: center; }
.resume-item { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 6px 0; border-top: 1px solid var(--line); }
.resume-item:first-child { border-top: 0; }

/* Game */
#map { position: absolute; inset: 0; }
#hud {
  position: absolute; top: max(10px, env(safe-area-inset-top)); left: 50%; transform: translateX(-50%);
  display: flex; gap: 10px; align-items: center; padding: 6px 12px; border-radius: 999px;
  background: rgba(20, 23, 15, 0.88); border: 1px solid var(--line); font: 600 14px var(--mono); white-space: nowrap; z-index: 5;
  max-width: calc(100vw - 24px); overflow: hidden;
}
.badge { padding: 2px 8px; border-radius: 999px; background: var(--surface-2); font-size: 11px; letter-spacing: 0.12em; }
.badge.hiding { background: var(--hider); color: #fff; }
.badge.seeking { background: var(--seeker); color: #fff; }
.badge.ended { background: var(--muted); color: #000; }
#banner {
  position: absolute; top: calc(max(10px, env(safe-area-inset-top)) + 44px); left: 50%; transform: translateX(-50%);
  background: var(--danger); color: #fff; padding: 6px 14px; border-radius: 8px; font-weight: 700; z-index: 5;
}
#map-buttons { position: absolute; right: 10px; top: calc(max(10px, env(safe-area-inset-top)) + 50px); display: grid; gap: 6px; z-index: 4; }
#map-buttons button { width: 42px; height: 42px; padding: 0; font-size: 18px; background: rgba(20, 23, 15, 0.9); }
#map-buttons button.on { border-color: var(--accent); color: var(--accent); }
#style-picker {
  position: absolute; right: 60px; top: calc(max(10px, env(safe-area-inset-top)) + 50px); z-index: 6;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 6px; display: grid; gap: 4px; min-width: 150px;
}
#style-picker button { text-align: left; }
#style-picker button.on { border-color: var(--accent); }

/* Panel: bottom sheet on phones, sidebar on desktops */
#panel {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 6; background: var(--surface); border-top: 1px solid var(--line);
  border-radius: 14px 14px 0 0; display: flex; flex-direction: column; height: 58vh; transition: height 0.2s;
  padding-bottom: env(safe-area-inset-bottom);
}
#panel.collapsed { height: calc(56px + env(safe-area-inset-bottom)); }
#panel.collapsed.has-actions { height: calc(116px + env(safe-area-inset-bottom)); }
#panel.collapsed .tab { display: none; }
#tabs { display: flex; gap: 6px; padding: 8px; flex: none; }
#tabs button { flex: 1; position: relative; }
#tabs button.active { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 700; }
.pill { background: var(--danger); color: #fff; border-radius: 999px; font-size: 11px; padding: 0 6px; margin-left: 4px; }
.tab { flex: 1; overflow: auto; padding: 4px 12px 12px; min-height: 0; }
#tab-chat { display: flex; flex-direction: column; overflow: hidden; }

@media (min-width: 900px) {
  #panel, #panel.collapsed { left: auto; top: 0; width: 400px; height: auto; border-radius: 0; border-top: 0; border-left: 1px solid var(--line); }
  #panel.collapsed .tab { display: revert; }
  #panel.collapsed .tab.hidden { display: none; }
  #panel.collapsed #tab-chat:not(.hidden) { display: flex; }
  #map { right: 400px; }
  #hud { left: calc((100vw - 400px) / 2); }
  #banner { left: calc((100vw - 400px) / 2); }
  #map-buttons, #style-picker { right: 410px; }
  #style-picker { right: 460px; }
}

/* Players */
.group h3 { margin: 12px 0 6px; font-size: 12px; text-transform: uppercase; letter-spacing: 0.12em; color: var(--muted); }
.player { display: flex; gap: 10px; align-items: center; padding: 8px 0; border-top: 1px solid var(--line); }
.player .who { flex: 1; min-width: 0; }
.player .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.player .meta { font-size: 12px; color: var(--muted); }
.player .actions { display: flex; gap: 4px; flex-wrap: wrap; justify-content: flex-end; }
.player.caught .name { text-decoration: line-through; opacity: 0.7; }
.avatar {
  width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center; font: 700 13px var(--mono); color: #fff; flex: none;
  border: 3px solid var(--role); position: relative;
}
.avatar.offline::after { content: ''; position: absolute; right: -3px; bottom: -3px; width: 10px; height: 10px; border-radius: 50%; background: var(--muted); border: 2px solid var(--surface); }
.role-seeker { --role: var(--seeker); }
.role-hider { --role: var(--hider); }
.role-referee { --role: var(--referee); }
.tag { font-size: 11px; padding: 1px 6px; border-radius: 4px; background: var(--surface-2); margin-left: 4px; }
.tag.warn { background: var(--danger); color: #fff; }

/* Markers */
.marker { display: flex; flex-direction: column; align-items: center; cursor: pointer; transition: opacity 0.5s; }
.marker .avatar { width: 30px; height: 30px; font-size: 11px; box-shadow: 0 2px 6px rgba(0, 0, 0, 0.5); }
.marker .label {
  margin-top: 2px; font: 600 11px var(--font); background: rgba(20, 23, 15, 0.85); color: var(--text); padding: 1px 6px; border-radius: 4px; white-space: nowrap;
}
.marker.me .avatar { outline: 3px solid rgba(255, 255, 255, 0.85); }
.marker.me .avatar::before {
  content: ''; position: absolute; inset: -8px; border-radius: 50%; border: 2px solid var(--role); animation: ping 2s infinite;
}
.marker.ghost .avatar { border-style: dashed; background: transparent !important; color: var(--role); }
.marker.caught { opacity: 0.45; }
@keyframes ping { from { transform: scale(0.8); opacity: 1; } to { transform: scale(1.8); opacity: 0; } }

/* Chat */
.seg { display: flex; gap: 4px; margin-bottom: 6px; flex: none; }
.seg button { flex: 1; padding: 5px; }
.seg button.active { border-color: var(--accent); color: var(--accent); }
#messages { flex: 1; overflow: auto; display: flex; flex-direction: column; gap: 6px; padding: 4px 0; }
.msg { max-width: 85%; background: var(--surface-2); padding: 6px 10px; border-radius: 10px; align-self: flex-start; overflow-wrap: anywhere; }
.msg.mine { align-self: flex-end; background: #3b4a26; }
.msg.system { align-self: center; background: transparent; color: var(--muted); font-size: 13px; text-align: center; }
.msg .from { font-size: 12px; font-weight: 700; color: var(--role, var(--muted)); }
.msg .time { font-size: 11px; color: var(--muted); margin-left: 6px; font-weight: 400; }
.msg .team { font-size: 10px; text-transform: uppercase; letter-spacing: 0.1em; color: var(--accent); margin-left: 6px; }
.msg img, .msg video { display: block; max-width: 100%; max-height: 260px; border-radius: 6px; margin-top: 4px; cursor: zoom-in; background: #000; }
#composer { display: flex; gap: 6px; flex: none; padding-top: 6px; }
#composer input { flex: 1; }
.attach { display: grid; place-items: center; width: 42px; border: 1px solid var(--line); border-radius: 8px; cursor: pointer; background: var(--surface-2); }
#upload-progress { height: 4px; background: var(--surface-2); border-radius: 2px; margin-top: 4px; overflow: hidden; flex: none; }
#upload-progress div { height: 100%; width: 0; background: var(--accent); transition: width 0.2s; }

/* Setup */
.setup { display: grid; gap: 12px; padding-top: 6px; }
.setup .card { background: var(--bg); }
.setup label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.setup label.check { display: flex; align-items: center; gap: 8px; color: var(--text); }
.setup .code-big { font: 800 34px var(--mono); letter-spacing: 0.25em; text-align: center; color: var(--accent); }
.setup .qr { width: 160px; height: 160px; background: #fff; border-radius: 8px; padding: 6px; justify-self: center; }
.setup .actions { display: flex; gap: 8px; flex-wrap: wrap; }
.setup .actions button { flex: 1; }
input[type=range] { padding: 0; accent-color: var(--accent); }

#toast {
  position: fixed; left: 50%; bottom: calc(80px + env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 20;
  background: #000d; color: #fff; padding: 10px 16px; border-radius: 10px; max-width: 90vw;
}
#lightbox { padding: 0; border: 0; background: transparent; max-width: 96vw; max-height: 96vh; }
#lightbox::backdrop { background: rgba(0, 0, 0, 0.9); }
#lightbox .content img, #lightbox .content video { max-width: 96vw; max-height: 92vh; display: block; }
#lightbox .close { position: fixed; top: 12px; right: 12px; z-index: 1; }
.maplibregl-ctrl-attrib { font-size: 10px; }
.lang { justify-self: end; width: 104px; margin-bottom: -10px; }
.lang button { font-size: 12px; }
.player { flex-wrap: wrap; }
.player .who { flex: 1 1 140px; }
.player .actions { margin-left: auto; }
.player select.small { padding: 4px 6px; font-size: 13px; }
.inline > span { white-space: nowrap; }
.whoami { display: flex; justify-content: space-between; align-items: center; gap: 8px; color: var(--muted); }
.whoami b { color: var(--text); }
#menu { display: grid; gap: 14px; }
#menu.hidden { display: none; }
#draw-bar {
  position: absolute; left: 50%; transform: translateX(-50%); bottom: calc(70px + env(safe-area-inset-bottom)); z-index: 7;
  background: rgba(20, 23, 15, 0.94); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px; display: grid; gap: 8px;
  width: min(440px, calc(100vw - 24px));
}
#draw-bar .row { flex-wrap: nowrap; }
#draw-bar .row button { flex: 1; }
@media (min-width: 900px) { #draw-bar { left: calc((100vw - 400px) / 2); bottom: 24px; } }
.vertex { width: 18px; height: 18px; border-radius: 50%; background: #fff; border: 3px solid var(--danger); cursor: grab; box-shadow: 0 1px 4px #0008; }
.vertex.first { background: var(--danger); }
.download-link { text-align: center; color: var(--accent); text-decoration: none; padding: 8px; border: 1px dashed var(--line); border-radius: var(--radius); }
#home a { color: var(--accent); }
#btn-panel { display: none; }
#btn-panel.alert { border-color: var(--danger); color: var(--danger); }
#panel.tucked { transform: translateY(100%); pointer-events: none; }
#panel { transition: height 0.2s, transform 0.2s; }
#banner { animation: alarm 1s steps(2, start) infinite; }
@keyframes alarm { 50% { background: #8c1d0e; } }
@media (max-width: 899px) { #btn-panel { display: block; } }
@media (min-width: 900px) { #panel.tucked { transform: none; pointer-events: auto; } }
#panel:not(.collapsed):not(.tucked) ~ #catch-dialog::backdrop, #code-dialog::backdrop { background: rgba(0, 0, 0, 0.7); }
#catch-dialog h2, #code-dialog h2 { margin: 0 0 6px; font-size: 16px; }
#catch-form { display: grid; gap: 10px; }
#catch-form .row button { flex: 1; }
#catch-code { font-size: 34px; width: 100%; }
#code-dialog { text-align: center; display: grid; gap: 10px; }
#code-dialog:not([open]) { display: none; }
.big-code { font: 900 72px/1 var(--mono); letter-spacing: 0.12em; color: var(--accent); padding: 12px 0; }
#toast { bottom: calc(140px + env(safe-area-inset-bottom)); text-align: center; }
#actions { display: flex; gap: 6px; padding: 8px 8px 0; flex: none; }
#actions button { flex: 1; padding: 12px; font-size: 17px; font-weight: 700; }
/* A hidden drawer still leaves the action row peeking out, so catching stays one tap away. */
#panel.tucked.has-actions { transform: translateY(calc(100% - 60px - env(safe-area-inset-bottom))); pointer-events: auto; }
#panel.tucked.has-actions #tabs { visibility: hidden; }
#btn-mycode { background: var(--hider); border-color: var(--hider); color: #fff; }
#toast { top: calc(max(10px, env(safe-area-inset-top)) + 100px); bottom: auto; }
@media (min-width: 900px) { #toast { left: calc((100vw - 400px) / 2); } }
