:root {
  --bg: #0b0c14;
  --panel: #151726;
  --panel-2: #1d2036;
  --ink: #e9ecff;
  --muted: #8b90b5;
  --accent: #ff4d6d;
  --p0: #ff4d6d;
  --p1: #4d96ff;
  --p2: #46d97a;
  --p3: #ffd23f;
}

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

#app { height: 100vh; height: 100dvh; display: flex; align-items: center; justify-content: center; }

body.controller-mode #app {
  height: calc(100dvh - 174px - env(safe-area-inset-bottom));
}
body.controller-mode #board-wrap,
body.controller-mode #board {
  max-height: calc(100dvh - 174px - env(safe-area-inset-bottom));
}

#board-wrap {
  position: relative;
  max-width: 100vw;
  max-height: 100vh;
}

#board {
  display: block;
  width: auto;
  height: auto;
  max-width: 100vw;
  max-height: 100vh;
  max-height: 100dvh;
  background: #05060c;
  border-radius: 10px;
  box-shadow: 0 20px 60px rgba(0,0,0,.55), inset 0 0 0 1px rgba(255,255,255,.05);
  touch-action: none;
}
@media (pointer: fine) {
  #board { cursor: grab; }
  #board.anchoring { cursor: grabbing; }
}
@media (min-width: 900px) {
  #board { max-height: 94vh; max-height: 94dvh; }
}

/* ---- HUD ---- */
#hud {
  position: absolute;
  top: 10px; left: 10px; right: 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  pointer-events: none;
  font-size: 13px;
}
#reveal-row { display: flex; align-items: center; gap: 8px; }
#reveal-label { color: var(--muted); letter-spacing: .12em; font-size: 11px; }
#reveal-bar {
  flex: 0 1 200px; height: 8px;
  background: rgba(255,255,255,.12);
  border-radius: 5px; overflow: hidden;
}
#reveal-fill {
  height: 100%; width: 0%;
  background: linear-gradient(90deg, var(--p2), var(--p1));
  transition: width .15s linear;
}
#reveal-pct { font-variant-numeric: tabular-nums; color: var(--ink); }

#scores {
  list-style: none; margin: 0; padding: 0;
  display: flex; flex-wrap: wrap; gap: 6px;
}
#scores li {
  display: flex; align-items: center; gap: 6px;
  background: rgba(10,12,22,.6);
  border: 1px solid rgba(255,255,255,.06);
  padding: 3px 8px; border-radius: 999px;
  backdrop-filter: blur(4px);
  font-size: 12px;
}
#scores .chip { width: 10px; height: 10px; border-radius: 50%; }
#scores .nm { font-weight: 600; }
#scores .sc { color: var(--muted); font-variant-numeric: tabular-nums; }
#scores .me { outline: 1px solid rgba(255,255,255,.35); }
#scores .dead { opacity: .45; }

#room-line { color: var(--muted); font-size: 12px; }
#room-line b { color: var(--ink); letter-spacing: .15em; }
#copy-link,
#photo-btn {
  pointer-events: auto;
  margin-left: 6px;
  background: var(--panel-2); color: var(--ink);
  border: 1px solid rgba(255,255,255,.08);
  border-radius: 6px; padding: 2px 8px; font-size: 11px; cursor: pointer;
}
#copy-link:hover,
#photo-btn:hover { background: #262a45; }

/* ---- Popped toast ---- */
#toast {
  position: absolute;
  top: 42%; left: 50%;
  transform: translate(-50%, -50%);
  background: rgba(255, 77, 109, .92);
  color: #fff; font-weight: 700; font-size: 15px;
  padding: 10px 16px; border-radius: 12px;
  box-shadow: 0 8px 26px rgba(255,77,109,.4);
  pointer-events: none;
  text-align: center; line-height: 1.35;
  animation: toastpop .18s ease-out;
  z-index: 12;
}
@keyframes toastpop {
  from { transform: translate(-50%, -50%) scale(.8); opacity: 0; }
  to   { transform: translate(-50%, -50%) scale(1); opacity: 1; }
}

/* ---- Game over ---- */
#over {
  position: absolute; inset: 0;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  background: rgba(6,7,14,.72);
  backdrop-filter: blur(3px);
  border-radius: 10px;
  text-align: center;
}
#over h2 { margin: 0 0 12px; font-size: 28px; letter-spacing: .05em; }
#ranking { list-style: none; margin: 0 0 12px; padding: 0; }
#ranking li {
  display: flex; align-items: center; gap: 10px;
  padding: 5px 14px; font-size: 16px;
}
#ranking .chip { width: 12px; height: 12px; border-radius: 50%; }
#ranking .rank { color: var(--muted); width: 22px; text-align: right; }
#ranking .pts { color: var(--muted); }
#next-round { color: var(--muted); }

/* ---- Fixed mobile controller ---- */
#touch-controller {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  height: calc(164px + env(safe-area-inset-bottom));
  padding: 14px 26px calc(12px + env(safe-area-inset-bottom));
  display: flex;
  align-items: center;
  justify-content: space-between;
  background:
    linear-gradient(180deg, rgba(255,255,255,.08), transparent 2px),
    linear-gradient(150deg, #347fa8, #245d86);
  border-top: 3px solid rgba(255,255,255,.2);
  box-shadow: 0 -12px 32px rgba(0,0,0,.38), inset 0 1px 0 rgba(255,255,255,.14);
  z-index: 14;
  touch-action: none;
}
#dpad {
  position: relative;
  width: 132px; height: 132px;
  flex: 0 0 132px;
  display: grid;
  grid-template-columns: 32px 44px 32px;
  grid-template-rows: 32px 44px 32px;
  padding: 8px;
  border-radius: 50%;
  background: #f4dfbd;
  border: 4px solid #392d2a;
  box-shadow: 0 7px 0 #19263b, 0 10px 20px rgba(0,0,0,.35), inset 0 0 0 3px rgba(255,255,255,.55);
  touch-action: none;
}
.dpad-key,
.dpad-center {
  width: 100%; height: 100%;
  margin: 0; padding: 0;
  border: 0;
  background: #3b2b2b;
  color: #f4dfbd;
  font-size: 18px;
  line-height: 1;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}
.dpad-key { cursor: pointer; box-shadow: inset 2px 2px 0 rgba(255,255,255,.06); }
.dpad-key.up { grid-column: 2; grid-row: 1; border-radius: 8px 8px 0 0; }
.dpad-key.left { grid-column: 1; grid-row: 2; border-radius: 8px 0 0 8px; }
.dpad-center { grid-column: 2; grid-row: 2; }
.dpad-key.right { grid-column: 3; grid-row: 2; border-radius: 0 8px 8px 0; }
.dpad-key.down { grid-column: 2; grid-row: 3; border-radius: 0 0 8px 8px; }
.dpad-key.active {
  background: #df4f4f;
  color: #fff6dc;
  box-shadow: inset 0 3px 8px rgba(0,0,0,.32);
}
.controller-mark {
  display: flex;
  align-items: center;
  gap: 7px;
  margin-right: 10px;
  color: rgba(8,21,37,.82);
  font-weight: 900;
  font-size: 18px;
  letter-spacing: .16em;
  transform: rotate(-8deg);
}
.controller-mark i {
  display: block;
  width: 7px; height: 22px;
  border-radius: 2px;
  background: rgba(8,21,37,.76);
}

/* ---- Floating mouse drag indicator ---- */
#anchor-control {
  position: fixed;
  left: 0; top: 0;
  width: 116px; height: 116px;
  transform: translate(-50%, -50%);
  z-index: 15;
  pointer-events: none;
}
.anchor-base,
.anchor-knob {
  position: absolute;
  left: 50%; top: 50%;
  border-radius: 50%;
  transform: translate(-50%, -50%);
}
.anchor-base {
  width: 100%; height: 100%;
  background: rgba(20,23,39,.48);
  border: 2px solid rgba(255,255,255,.3);
  backdrop-filter: blur(6px);
  box-shadow: 0 8px 28px rgba(0,0,0,.35), inset 0 0 30px rgba(255,255,255,.04);
}
.anchor-knob {
  width: 48px; height: 48px;
  background: rgba(255,77,109,.92);
  border: 2px solid rgba(255,255,255,.72);
  box-shadow: 0 5px 16px rgba(0,0,0,.42), 0 0 18px rgba(255,77,109,.32);
}
#joy-hint {
  position: fixed;
  left: 50%; transform: translateX(-50%);
  bottom: calc(174px + env(safe-area-inset-bottom));
  width: max-content;
  max-width: calc(100vw - 32px);
  padding: 7px 11px;
  border-radius: 999px;
  background: rgba(10,12,22,.6);
  color: var(--muted); font-size: 12px; letter-spacing: .03em;
  z-index: 15; pointer-events: none; opacity: .8;
  text-shadow: 0 1px 3px rgba(0,0,0,.8);
}

/* ---- Lobby ---- */
#lobby {
  position: fixed; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: rgba(6,7,14,.92);
  z-index: 20;
  padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
  overflow: auto;
}
.panel {
  width: min(440px, 94vw);
  background: linear-gradient(180deg, var(--panel), #10111d);
  border: 1px solid rgba(255,255,255,.08);
  border-radius: 16px;
  padding: 26px 26px 20px;
  box-shadow: 0 30px 80px rgba(0,0,0,.6);
}
.panel h1 {
  margin: 0 0 4px; font-size: 34px; letter-spacing: .06em;
  background: linear-gradient(90deg, var(--p0), var(--p3), var(--p2), var(--p1));
  -webkit-background-clip: text; background-clip: text; color: transparent;
}
.tag { color: var(--muted); margin: 0 0 18px; line-height: 1.4; }
.panel label { display: block; font-size: 13px; color: var(--muted); margin: 12px 0 4px; }
.panel .hint { color: #5a5f80; }
.panel input {
  width: 100%; padding: 13px 12px; font-size: 16px; /* >=16px avoids iOS zoom-on-focus */
  background: var(--panel-2); color: var(--ink);
  border: 1px solid rgba(255,255,255,.1); border-radius: 10px;
  outline: none;
}
.panel input:focus { border-color: var(--p1); }
#join {
  width: 100%; margin-top: 18px; padding: 13px;
  font-size: 16px; font-weight: 700; letter-spacing: .08em;
  color: #0b0c14; cursor: pointer;
  background: linear-gradient(90deg, var(--p0), var(--p3));
  border: none; border-radius: 10px;
}
#join:hover { filter: brightness(1.07); }
#lobby-msg { min-height: 18px; margin: 10px 0 0; color: var(--accent); font-size: 13px; text-align: center; }
.notype { margin: 8px 0 0; color: var(--muted); font-size: 12px; text-align: center; line-height: 1.4; }
.notype b { color: var(--ink); }
.how { margin-top: 16px; color: var(--muted); font-size: 12.5px; line-height: 1.5; }
.how b { color: var(--ink); }
.how ul { margin: 6px 0 0; padding-left: 18px; }
.how kbd {
  background: var(--panel-2); border: 1px solid rgba(255,255,255,.12);
  border-radius: 5px; padding: 0 5px; font-size: 11px; color: var(--ink);
}
.how .spark { color: var(--accent); font-weight: 700; }

.panel .ghost {
  width: 100%; margin-top: 10px; padding: 11px;
  font-size: 14px; font-weight: 600;
  color: var(--ink); cursor: pointer;
  background: var(--panel-2);
  border: 1px solid rgba(255,255,255,.12); border-radius: 10px;
}
.panel .ghost:hover { background: #262a45; }

/* ---- Background photo manager ---- */
#photos {
  position: fixed; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: rgba(6,7,14,.94);
  z-index: 30;
  padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
  overflow: auto;
  touch-action: pan-y;
}
.photos-panel { max-height: 100%; display: flex; flex-direction: column; padding: 20px; }
.photos-head { display: flex; align-items: center; justify-content: space-between; }
.photos-head h2 { margin: 0; font-size: 20px; letter-spacing: .04em; }
#photos-close {
  background: var(--panel-2); color: var(--ink); cursor: pointer;
  border: 1px solid rgba(255,255,255,.1); border-radius: 8px;
  width: 32px; height: 32px; font-size: 14px; line-height: 1;
}
#photos-close:hover { background: #262a45; }
.photos-tag { color: var(--muted); font-size: 12.5px; line-height: 1.45; margin: 8px 0 14px; }

#photos-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(92px, 1fr));
  gap: 10px;
  overflow-y: auto;
  min-height: 96px;
  padding-bottom: 4px;
}
.photo-tile {
  position: relative;
  aspect-ratio: 5 / 7;
  border-radius: 10px; overflow: hidden;
  background: var(--panel-2);
  border: 2px solid rgba(255,255,255,.08);
  cursor: pointer; padding: 0;
}
.photo-tile img { width: 100%; height: 100%; object-fit: cover; display: block; }
.photo-tile.current { border-color: var(--p2); box-shadow: 0 0 0 3px rgba(70,217,122,.22); }
.photo-tile .used {
  position: absolute; left: 4px; bottom: 4px;
  background: var(--p2); color: #07240f;
  font-size: 10px; font-weight: 800; letter-spacing: .04em;
  padding: 2px 6px; border-radius: 999px;
}
.photo-tile .del {
  position: absolute; top: 4px; right: 4px;
  width: 24px; height: 24px; line-height: 1;
  border-radius: 50%; cursor: pointer;
  background: rgba(8,9,16,.72); color: #fff;
  border: 1px solid rgba(255,255,255,.22);
  font-size: 12px; padding: 0;
}
.photo-tile .del:hover { background: var(--accent); }
.photo-tile.pattern {
  display: flex; align-items: center; justify-content: center;
  color: var(--muted); font-size: 12px; font-weight: 600; text-align: center;
  background: linear-gradient(135deg, #6a3f8f, #2f6ea8 45%, #2f9c76);
}
.photo-tile.pattern span { color: #fff; text-shadow: 0 1px 4px rgba(0,0,0,.6); }
#photos-empty { grid-column: 1 / -1; color: var(--muted); font-size: 12.5px; align-self: center; }

#photos-add {
  display: block; position: relative; margin-top: 14px; padding: 12px;
  text-align: center; font-size: 14px; font-weight: 700;
  color: #0b0c14; cursor: pointer;
  background: linear-gradient(90deg, var(--p1), var(--p2));
  border-radius: 10px;
}
#photos-add:hover { filter: brightness(1.07); }
#photos-add input { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }
#photos.busy #photos-add { opacity: .55; pointer-events: none; }
#photos-msg { min-height: 18px; margin: 10px 0 0; font-size: 12.5px; text-align: center; color: var(--muted); }
#photos-msg.err { color: var(--accent); }

.hidden { display: none !important; }
