:root {
  --bg: #14181f;
  --panel: #1f2530;
  --panel-border: #333c4d;
  --text: #eef1f6;
  --text-dim: #9aa4b6;
  --accent: #6a4c93;
  --accent-2: #3a86ff;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
}

.screen { width: 100%; height: 100%; }
.hidden { display: none !important; }

/* ---------- Onboarding ---------- */

#onboarding {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  min-height: 100vh;
}

.card {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  padding: 32px;
  max-width: 480px;
  width: 100%;
}

.card h1 { margin: 0 0 4px; font-size: 28px; }
.subtitle { color: var(--text-dim); margin: 0 0 24px; }

.step { margin-bottom: 24px; }
.step h2 { font-size: 15px; color: var(--text-dim); margin: 0 0 10px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; }

.chip-grid { display: flex; flex-wrap: wrap; gap: 8px; }

.chip {
  background: #2a3242;
  border: 1px solid var(--panel-border);
  color: var(--text);
  padding: 8px 16px;
  border-radius: 999px;
  cursor: pointer;
  font-size: 14px;
  transition: background 0.15s, border-color 0.15s;
}
.chip:hover { background: #333e52; }
.chip.selected {
  background: var(--accent);
  border-color: var(--accent);
}
.chip.disabled { opacity: 0.4; cursor: not-allowed; }

.avatar-preview-wrap { display: flex; align-items: center; gap: 16px; }
#avatar-canvas { background: #10141b; border-radius: 12px; border: 1px solid var(--panel-border); }
.hint { color: var(--text-dim); font-size: 13px; margin: 8px 0 0; }

#regenerate-avatar, #enter-button, #chat-form button, #chat-close {
  background: var(--accent);
  color: white;
  border: none;
  padding: 10px 18px;
  border-radius: 10px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
}
#regenerate-avatar:hover, #enter-button:hover { background: #7d5cae; }

#username-input, #chat-input {
  width: 100%;
  padding: 10px 14px;
  border-radius: 10px;
  border: 1px solid var(--panel-border);
  background: #10141b;
  color: var(--text);
  font-size: 15px;
}

#enter-button { width: 100%; padding: 14px; font-size: 16px; }

.error { color: #ff6b6b; font-size: 14px; }

/* honeypot: fuori schermo, non display:none (alcuni bot lo rilevano) */
.hp-field {
  position: absolute;
  left: -9999px;
  top: -9999px;
  height: 1px;
  width: 1px;
  overflow: hidden;
}

#captcha-grid {
  display: grid;
  grid-template-columns: repeat(3, 56px);
  gap: 8px;
  margin: 10px 0;
}
.captcha-cell {
  width: 56px;
  height: 56px;
  border-radius: 10px;
  cursor: pointer;
  border: 3px solid transparent;
  transition: transform 0.1s, border-color 0.1s;
}
.captcha-cell:hover { transform: scale(1.05); }
.captcha-cell.selected { border-color: white; }
#captcha-submit {
  background: var(--accent-2);
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 600;
}
#enter-button:disabled { opacity: 0.45; cursor: not-allowed; }

/* ---------- Creazione locale ---------- */

/* ---------- Barra strumenti (stile Gather) ---------- */

#toolbar {
  position: absolute;
  bottom: 12px;
  left: 12px;
  z-index: 15;
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(20, 24, 31, 0.92);
  border: 1px solid var(--panel-border);
  border-radius: 999px;
  padding: 6px 10px;
  box-shadow: 0 4px 14px rgba(0,0,0,0.4);
}
.tb-me {
  display: flex;
  align-items: center;
  gap: 7px;
  padding-right: 4px;
  border-right: 1px solid var(--panel-border);
}
#tb-avatar { border-radius: 50%; }
#tb-name {
  font-size: 13px;
  font-weight: 600;
  max-width: 90px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
#toolbar button {
  background: #2a3242;
  color: var(--text);
  border: none;
  border-radius: 999px;
  padding: 8px 13px;
  font-size: 15px;
  cursor: pointer;
}
#toolbar button:hover { background: var(--accent); }
#tb-count { font-size: 13px; font-weight: 600; }

#people-panel {
  bottom: 64px;
  left: 12px;
  width: min(240px, 80vw);
  max-height: 45vh;
  overflow-y: auto;
  z-index: 14;
}
.person-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 4px;
  font-size: 14px;
}
.person-row .dot { width: 12px; height: 12px; border-radius: 50%; flex-shrink: 0; border: 2px solid rgba(255,255,255,0.4); }
.person-row .you { color: var(--text-dim); font-size: 12px; }

#create-locale-panel {
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 320px;
}
#create-locale-form label {
  display: block;
  font-size: 13px;
  color: var(--text-dim);
  margin: 10px 0 4px;
}
#create-locale-form input {
  width: 100%;
  padding: 8px 12px;
  border-radius: 8px;
  border: 1px solid var(--panel-border);
  background: #10141b;
  color: var(--text);
  font-size: 14px;
  margin-top: 4px;
}
#create-locale-form button[type="submit"] {
  width: 100%;
  margin-top: 14px;
  background: var(--accent);
  color: white;
  border: none;
  padding: 10px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 600;
}
/* ---------- Selettore città ---------- */

#active-cities { display: flex; flex-direction: column; gap: 8px; }
.city-card {
  background: #2a3242;
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  padding: 10px 14px;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.city-card:hover { background: #333e52; }
.city-card.selected { border-color: var(--accent); background: #32294a; }

#city-search { margin-bottom: 6px; }
#city-search-results { display: flex; flex-direction: column; gap: 4px; max-height: 200px; overflow-y: auto; }
.city-result {
  padding: 8px 12px;
  border-radius: 8px;
  background: #232b39;
  cursor: pointer;
  font-size: 14px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.city-result:hover { background: #333e52; }
.tag-small {
  font-size: 11px;
  background: #333c4d;
  color: var(--text-dim);
  padding: 2px 8px;
  border-radius: 999px;
}
.tag-small.new { background: var(--accent); color: white; }

/* ---------- Interno palazzo ---------- */

#palazzo-panel {
  /* ancorato in ALTO al centro: non copre mai il pad in basso a destra */
  top: 56px;
  left: 50%;
  transform: translateX(-50%);
  width: min(340px, 92vw);
  max-height: 55vh;
  overflow-y: auto;
  z-index: 10;
}

/* ---------- Dentro il locale ---------- */

#room-bar {
  position: absolute;
  top: 10px;
  left: 10px;
  right: 10px;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 10px;
  background: rgba(20, 24, 31, 0.92);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  padding: 8px 12px;
}
#room-exit {
  background: var(--accent);
  color: white;
  border: none;
  padding: 8px 14px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 600;
  flex-shrink: 0;
}
#room-bar .room-info {
  display: flex;
  flex-direction: column;
  min-width: 0;
}
#room-bar .room-info strong { font-size: 15px; }
#room-bar .room-info .hint {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#room-chat-bar {
  position: absolute;
  left: 10px;
  bottom: 12px;
  /* lascia libera la zona del pad a destra */
  width: calc(100% - 190px);
  max-width: 440px;
  z-index: 10;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
#room-log {
  font-size: 12px;
  color: var(--text);
  display: flex;
  flex-direction: column;
  gap: 2px;
  pointer-events: none;
}
#room-log .msg {
  background: rgba(20, 24, 31, 0.75);
  border-radius: 8px;
  padding: 3px 8px;
  width: fit-content;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
#room-log .msg strong { color: var(--accent-2); }
#room-chat-form { display: flex; gap: 6px; }
#room-chat-form input {
  flex: 1;
  min-width: 0;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--panel-border);
  background: rgba(16, 20, 27, 0.92);
  color: var(--text);
  font-size: 14px;
}
#room-chat-form button {
  background: var(--accent);
  border: none;
  border-radius: 10px;
  padding: 0 14px;
  font-size: 16px;
  cursor: pointer;
}

/* dentro un locale il bottone "crea" non serve */
#game.in-room #create-locale-button { display: none; }
#game.in-room #proximity-panel { display: none; }
/* la barra chat della stanza parte sopra la toolbar */
#game.in-room #room-chat-bar { bottom: 64px; }
.locale-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
}
.locale-row:hover { background: #2a3242; }
.locale-row .rank { color: var(--text-dim); width: 18px; text-align: right; }
.locale-row .dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }
.locale-row .locale-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin-top: 10px;
}
.pagination button {
  background: #333c4d;
  color: var(--text);
  border: none;
  padding: 4px 12px;
  border-radius: 6px;
  cursor: pointer;
}
.pagination button:disabled { opacity: 0.35; cursor: not-allowed; }

/* ---------- Pad direzionale (touch + mouse) ---------- */

#dpad {
  position: absolute;
  bottom: 16px;
  right: 16px;
  z-index: 30; /* SEMPRE sopra ogni pannello: le frecce non si coprono mai */
  width: 148px;
  height: 148px;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: 1fr 1fr 1fr;
  gap: 4px;
}
.dpad-btn {
  background: rgba(31, 37, 48, 0.9);
  border: 1px solid var(--panel-border);
  color: var(--text);
  font-size: 20px;
  border-radius: 10px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}
.dpad-btn:active { background: var(--accent); }
.dpad-up    { grid-column: 2; grid-row: 1; }
.dpad-left  { grid-column: 1; grid-row: 2; }
.dpad-right { grid-column: 3; grid-row: 2; }
.dpad-down  { grid-column: 2; grid-row: 3; }

@media (min-width: 900px) {
  /* su schermi grandi, con tastiera quasi certa, il pad resta ma più discreto */
  #dpad { opacity: 0.55; }
  #dpad:hover { opacity: 1; }
}

.color-row { display: flex; gap: 8px; margin-top: 4px; }
.color-swatch {
  width: 28px;
  height: 28px;
  border-radius: 8px;
  cursor: pointer;
  border: 3px solid transparent;
}
.color-swatch.selected { border-color: white; }

.piani-row { display: flex; gap: 8px; margin-top: 4px; }
.piano-card {
  flex: 1;
  background: #232b39;
  border: 2px solid var(--panel-border);
  border-radius: 10px;
  padding: 8px 6px;
  text-align: center;
  cursor: pointer;
  font-size: 13px;
}
.piano-card.selected { border-color: var(--accent); background: #32294a; }
.piano-card .price { font-weight: 700; font-size: 15px; }

.pay-button {
  display: block;
  width: 100%;
  margin-top: 10px;
  background: #2a9d8f;
  color: white;
  border: none;
  padding: 12px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 600;
  font-size: 14px;
  text-align: center;
  text-decoration: none;
}
.pay-button:hover { filter: brightness(1.1); }

/* ---------- Game ---------- */

#game { position: relative; overflow: hidden; }

#hud {
  position: absolute;
  top: 12px;
  left: 12px;
  z-index: 5;
  background: rgba(20, 24, 31, 0.85);
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  padding: 8px 14px;
  display: flex;
  gap: 16px;
  font-size: 14px;
}

#game {
  height: 100vh;
  height: 100dvh; /* mobile: esclude le barre del browser */
  background: #1a2f22;
}
#game-canvas {
  display: block;
  width: 100%;
  height: 100%;
}

#controls-hint {
  position: absolute;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 5;
  background: rgba(20, 24, 31, 0.85);
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  padding: 6px 12px;
  font-size: 12px;
  color: var(--text-dim);
  white-space: nowrap;
}
@media (max-width: 480px) {
  /* su schermi molto stretti il suggerimento testuale è ridondante col pad */
  #controls-hint { display: none; }
}

.panel {
  position: absolute;
  z-index: 5;
  background: rgba(20, 24, 31, 0.95);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  padding: 14px 16px;
}

#proximity-panel {
  top: 12px;
  right: 12px;
  width: 240px;
}
#proximity-panel h3 { margin: 0 0 6px; font-size: 16px; }
#proximity-panel .tag {
  display: inline-block;
  background: var(--accent-2);
  color: white;
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 999px;
  text-transform: uppercase;
  margin-bottom: 8px;
}
#proximity-panel p { margin: 4px 0; font-size: 13px; color: var(--text-dim); }

.chat {
  bottom: 12px;
  right: 12px;
  width: 280px;
  display: flex;
  flex-direction: column;
}
.chat-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}
#chat-close { padding: 2px 8px; border-radius: 6px; background: #333c4d; }
#chat-messages {
  height: 160px;
  overflow-y: auto;
  font-size: 13px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 8px;
}
#chat-messages .msg strong { color: var(--accent-2); }
#chat-form { display: flex; gap: 6px; }
#chat-form input { flex: 1; }
#chat-form button { padding: 8px 12px; font-size: 13px; }
