:root {
  --bg: #0b1020;
  --bg-2: #141a33;
  --panel: #1b2142;
  --text: #e8ecff;
  --muted: #9aa3d0;
  --accent: #ffb84d;
  --accent-2: #ff7e5f;
  --good: #5de0a9;
  --bad: #ff6b8a;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; height: 100%; overflow: hidden; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: radial-gradient(1200px 800px at 50% -10%, #23306a 0%, var(--bg) 60%);
  color: var(--text);
  user-select: none;
  touch-action: none;
}

#app { position: fixed; inset: 0; }

.screen {
  position: absolute;
  inset: 0;
  padding: 40px 24px;
  display: none;
  flex-direction: column;
  gap: 16px;
  overflow-y: auto;
  align-items: center;
}
.screen.active { display: flex; }
.screen > *:not(canvas) { max-width: 560px; width: 100%; box-sizing: border-box; }
.screen.game { padding: 0; gap: 0; align-items: stretch; }
.screen.game > * { max-width: none; }
#screen-menu { justify-content: center; }

h1 {
  font-size: clamp(44px, 12vw, 84px);
  margin: 24px 0 0;
  text-align: center;
  line-height: 0.95;
  letter-spacing: -1px;
  background: linear-gradient(180deg, #fff, #ffb84d 80%);
  -webkit-background-clip: text; background-clip: text;
  color: transparent;
  text-shadow: 0 6px 24px rgba(255, 184, 77, 0.25);
}
h2 { margin: 8px 0 0; font-size: 28px; }
.tagline { text-align: center; color: var(--muted); margin-top: 4px; }
.footer-note { text-align: center; color: var(--muted); font-size: 12px; margin-top: auto; }
.muted { color: var(--muted); }
.hint { color: var(--muted); font-size: 13px; min-height: 18px; }

.menu-buttons {
  display: flex; flex-direction: column; gap: 12px;
  max-width: 340px; width: 100%; margin: 24px auto 0;
}

.btn {
  appearance: none; border: 0; cursor: pointer;
  font: inherit; font-weight: 600;
  padding: 14px 18px;
  border-radius: 14px;
  background: var(--panel);
  color: var(--text);
  box-shadow: var(--shadow);
  transition: transform 0.08s ease, background 0.15s ease;
}
.btn:active { transform: translateY(1px) scale(0.99); }
.btn.primary { background: linear-gradient(180deg, var(--accent), var(--accent-2)); color: #22143a; }
.btn.ghost { background: transparent; box-shadow: none; color: var(--muted); }
.btn.big { padding: 16px 20px; font-size: 18px; }
.btn.small { padding: 8px 12px; font-size: 14px; }

.back {
  align-self: flex-start;
  background: transparent; border: 0; color: var(--muted); font: inherit;
  padding: 6px 10px; border-radius: 10px; cursor: pointer;
}
.back:hover { color: var(--text); background: rgba(255,255,255,0.06); }

.map-list { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; }
.map-card {
  background: var(--panel); border-radius: 14px; padding: 14px;
  border: 2px solid transparent; cursor: pointer;
  display: flex; flex-direction: column; gap: 4px;
}
.map-card.selected { border-color: var(--accent); }
.map-card h3 { margin: 0; font-size: 16px; }
.map-card p { margin: 0; color: var(--muted); font-size: 13px; }

.checkbox { display: flex; align-items: center; gap: 10px; cursor: pointer; }
.checkbox input { width: 20px; height: 20px; }

input[type="text"], #username-input {
  font: inherit; padding: 12px 14px; border-radius: 12px; border: 1px solid #2a325f;
  background: var(--bg-2); color: var(--text); outline: none;
}
input:focus { border-color: var(--accent); }

/* Game screen */
.hud {
  position: absolute; top: 0; left: 0; right: 0; z-index: 5;
  display: flex; align-items: center; gap: 10px;
  padding: calc(10px + env(safe-area-inset-top)) 12px 10px;
  background: linear-gradient(180deg, rgba(11,16,32,0.85), rgba(11,16,32,0));
  pointer-events: none;
}
.hud .back { pointer-events: auto; color: #fff; background: rgba(0,0,0,0.3); border-radius: 999px; padding: 6px 10px; }
.hud-info { display: flex; gap: 14px; margin-left: auto; pointer-events: auto; }
.hud-info > div { display: flex; flex-direction: column; align-items: flex-end; line-height: 1.1; }
.hud-info .label { font-size: 10px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.08em; }
.hud-info span + span { font-weight: 700; }

#game-canvas {
  position: absolute; inset: 0; width: 100%; height: 100%;
  display: block; background: transparent;
  touch-action: none;
}

.controls {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 5;
  display: flex; gap: 12px; padding: 12px calc(12px + env(safe-area-inset-right)) calc(12px + env(safe-area-inset-bottom)) calc(12px + env(safe-area-inset-left));
  background: linear-gradient(0deg, rgba(11,16,32,0.85), rgba(11,16,32,0));
}
.controls .btn { flex: 1; }
.controls .btn.drop { flex: 1.4; }

.toast {
  position: absolute; top: 80px; left: 50%; transform: translateX(-50%) translateY(-8px);
  background: rgba(0,0,0,0.7); color: #fff; padding: 8px 14px; border-radius: 999px;
  font-size: 14px; opacity: 0; transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 6; pointer-events: none;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* Modal */
.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,0.55);
  display: flex; align-items: center; justify-content: center; z-index: 10;
  padding: 20px;
}
.modal.hidden { display: none; }
.modal-inner {
  background: var(--panel); border-radius: 18px; padding: 22px;
  max-width: 360px; width: 100%; box-shadow: var(--shadow);
  text-align: center;
}
.modal-inner h2 { margin: 0 0 8px; }
.modal-buttons { display: flex; gap: 10px; margin-top: 18px; }
.modal-buttons .btn { flex: 1; }

/* Lobby / form UI */
.field { display: flex; flex-direction: column; gap: 6px; }
.field > span { color: var(--muted); font-size: 13px; }
.seg { display: flex; gap: 6px; background: var(--bg-2); padding: 4px; border-radius: 12px; }
.seg button {
  flex: 1; padding: 10px; background: transparent; border: 0; color: var(--muted);
  font: inherit; font-weight: 600; border-radius: 8px; cursor: pointer;
}
.seg button.on { background: var(--accent); color: #22143a; box-shadow: var(--shadow); }

.card {
  background: var(--panel); border-radius: 16px; padding: 16px;
  display: flex; flex-direction: column; gap: 12px;
  box-shadow: var(--shadow);
}
.card h3 { margin: 0; font-size: 18px; }

select, #priv-join-code {
  font: inherit; padding: 12px 14px; border-radius: 12px; border: 1px solid #2a325f;
  background: var(--bg-2); color: var(--text); outline: none;
  text-transform: uppercase; letter-spacing: 0.15em;
}
select { letter-spacing: 0; text-transform: none; }
select:focus, #priv-join-code:focus { border-color: var(--accent); }

.lobby-list { display: flex; flex-direction: column; gap: 10px; }
.lobby-row {
  background: var(--panel); border-radius: 12px; padding: 12px 14px;
  display: flex; align-items: center; gap: 12px;
}
.lobby-row .info { flex: 1; min-width: 0; }
.lobby-row .info .line1 { font-weight: 600; }
.lobby-row .info .line2 { color: var(--muted); font-size: 13px; }
.lobby-row .code { font-family: "SF Mono", ui-monospace, Menlo, monospace; font-weight: 700; }
.lobby-row .btn { padding: 8px 14px; }

.code {
  font-family: "SF Mono", ui-monospace, Menlo, monospace;
  letter-spacing: 0.18em; background: var(--bg-2); padding: 2px 10px; border-radius: 8px;
}

.player-list { display: flex; flex-direction: column; gap: 6px; }
.player-row {
  background: var(--panel); border-radius: 12px; padding: 10px 14px;
  display: flex; align-items: center; gap: 10px;
}
.player-row .host-badge {
  font-size: 11px; background: var(--accent); color: #22143a;
  padding: 2px 8px; border-radius: 999px; margin-left: auto;
}
.player-row.me { border: 1px solid rgba(255,184,77,0.5); }
.player-row .seat {
  width: 22px; height: 22px; display: inline-grid; place-items: center;
  background: var(--bg-2); border-radius: 50%; font-size: 12px; color: var(--muted);
}

.lobby-meta {
  display: flex; gap: 10px; flex-wrap: wrap; color: var(--muted); font-size: 13px;
}

.controls .btn:disabled, .btn:disabled {
  opacity: 0.45; cursor: not-allowed;
}

/* In-game chat */
.chat-toggle {
  position: absolute; top: 10px; right: 12px; z-index: 6;
  background: rgba(0,0,0,0.4); border: 0; color: #fff;
  width: 40px; height: 40px; border-radius: 50%;
  font-size: 20px; cursor: pointer; pointer-events: auto;
}
.chat-toggle:hover { background: rgba(0,0,0,0.6); }

.chat-panel {
  position: absolute; top: 60px; right: 12px; z-index: 7;
  width: min(320px, calc(100vw - 24px));
  max-height: 55vh;
  background: rgba(20, 25, 50, 0.96);
  border-radius: 14px; padding: 10px;
  display: flex; flex-direction: column; gap: 8px;
  box-shadow: var(--shadow);
  pointer-events: auto;
}
.chat-panel.hidden { display: none; }
.chat-head {
  display: flex; align-items: center; justify-content: space-between;
  font-size: 13px; color: var(--muted);
}
.chat-close {
  background: transparent; border: 0; color: var(--muted);
  font-size: 20px; cursor: pointer; padding: 0 6px;
}
.chat-messages {
  flex: 1; min-height: 120px; overflow-y: auto;
  display: flex; flex-direction: column; gap: 4px;
  padding: 4px; border-radius: 8px; background: rgba(0,0,0,0.3);
  font-size: 13px;
}
.chat-msg { line-height: 1.35; word-wrap: break-word; }
.chat-msg .name { color: var(--accent); font-weight: 600; margin-right: 6px; }
.chat-input { display: flex; gap: 6px; }
.chat-input input {
  flex: 1; padding: 8px 10px; font-size: 13px;
  background: var(--bg-2); color: var(--text);
  border: 1px solid #2a325f; border-radius: 8px; outline: none;
}
.chat-input input:focus { border-color: var(--accent); }
.chat-input button { padding: 8px 14px; font-size: 13px; }

.chat-toast {
  position: absolute; top: 60px; right: 60px; z-index: 5;
  background: rgba(0,0,0,0.7); color: #fff; padding: 6px 10px;
  border-radius: 8px; font-size: 12px; opacity: 0;
  transition: opacity 0.25s ease; pointer-events: none;
  max-width: 260px;
}
.chat-toast.show { opacity: 1; }
