:root {
  --bg: #0b0d10;
  --panel: rgba(15, 18, 24, 0.9);
  --panel-border: rgba(255, 255, 255, 0.1);
  --text: #e6e9ee;
  --muted: #9aa3ad;
  --accent: #ffd500;
  --accent-text: #1e2a38;
  --play: #3ddc84;
  --danger: #ff5f5f;
  --font: 'Trebuchet MS', 'Lucida Sans', Arial, sans-serif;
  --palette-width: 240px;
}

* {
  box-sizing: border-box;
}

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

#view {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

/* --- Editor ---------------------------------------------------------------- */
.topbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 10;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--panel-border);
  backdrop-filter: blur(6px);
}

.topbar h1 {
  margin: 0 8px 0 0;
  font-size: 16px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  white-space: nowrap;
}

.topbar input,
.topbar select {
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--panel-border);
  color: var(--text);
  border-radius: 8px;
  padding: 8px 10px;
  font: inherit;
  font-size: 14px;
  min-width: 0;
}

#roomName {
  flex: 1 1 160px;
}

#authorName {
  flex: 0 1 130px;
}

.floor-label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--muted);
}

.topbar-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-left: auto;
}

.topbar-status {
  flex-basis: 100%;
  font-size: 12px;
  color: var(--muted);
}

button {
  font: inherit;
  font-size: 14px;
  font-weight: 700;
  border: none;
  border-radius: 10px;
  padding: 9px 14px;
  cursor: pointer;
  background: var(--accent);
  color: var(--accent-text);
  touch-action: manipulation;
}

button.secondary {
  background: rgba(255, 255, 255, 0.1);
  color: var(--text);
}

button.play {
  background: var(--play);
  color: #0b2a18;
}

button:disabled {
  opacity: 0.5;
  cursor: default;
}

.palette-panel {
  position: fixed;
  top: 98px;
  bottom: 0;
  left: 0;
  width: var(--palette-width);
  z-index: 10;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px;
  background: var(--panel);
  border-right: 1px solid var(--panel-border);
  overflow-y: auto;
}

.palette-tools {
  display: flex;
  gap: 6px;
}

.palette-tools button {
  flex: 1;
  background: rgba(255, 255, 255, 0.1);
  color: var(--text);
}

.palette-tools button.is-active {
  background: var(--danger);
  color: #fff;
}

.palette-group h3 {
  margin: 6px 0 4px;
  font-size: 11px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
}

.palette-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}

.piece {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 8px 4px;
  background: rgba(255, 255, 255, 0.06);
  color: var(--text);
  border: 2px solid transparent;
  font-weight: 600;
}

.piece.is-active {
  border-color: var(--accent);
  background: rgba(255, 213, 0, 0.15);
}

.piece-emoji {
  font-size: 24px;
  line-height: 1;
}

.piece-name {
  font-size: 11px;
}

.hint {
  margin: auto 0 0;
  font-size: 12px;
  color: var(--muted);
}

/* --- Juego ----------------------------------------------------------------- */
#playHud {
  position: fixed;
  inset: 0;
  z-index: 10;
  pointer-events: none;
}

.crosshair {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 6px;
  height: 6px;
  margin: -3px 0 0 -3px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.85);
  box-shadow: 0 0 6px rgba(0, 0, 0, 0.8);
}

.keys-hud {
  position: absolute;
  top: 14px;
  left: 14px;
  padding: 8px 12px;
  border-radius: 10px;
  background: var(--panel);
  font-size: 16px;
  font-weight: 700;
}

.keys-hud:empty {
  display: none;
}

.timer-hud {
  position: absolute;
  top: 14px;
  left: 50%;
  transform: translateX(-50%);
  padding: 8px 14px;
  border-radius: 10px;
  background: var(--panel);
  font-size: 16px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.exit-play {
  position: absolute;
  top: 14px;
  right: 14px;
  pointer-events: auto;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--panel-border);
  cursor: pointer;
}

/* Viñeta de sangre: un golpe rojo al ser visto, y se queda al ser pillado. */
.blood-vignette {
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0;
  background:
    radial-gradient(ellipse at center, rgba(120, 0, 0, 0) 45%, rgba(110, 0, 0, 0.55) 80%, rgba(60, 0, 0, 0.9) 100%);
  transition: opacity 0.25s;
}

.blood-vignette.is-flash {
  opacity: 1;
  transition: opacity 0.05s;
}

.blood-vignette.is-caught {
  opacity: 1;
  animation: blood-pulse 1.6s ease-in-out infinite;
}

@keyframes blood-pulse {
  0%, 100% { opacity: 0.85; }
  50% { opacity: 1; }
}

/* La voz de la mansión: subtítulo abajo, en cursiva, con sombra. */
.subtitle {
  position: absolute;
  left: 50%;
  bottom: 12%;
  transform: translateX(-50%);
  max-width: min(720px, 86vw);
  padding: 10px 18px;
  border-radius: 12px;
  background: rgba(0, 0, 0, 0.55);
  color: #e8dcc8;
  font-size: 18px;
  font-style: italic;
  line-height: 1.35;
  text-align: center;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.9);
  letter-spacing: 0.02em;
}

.subtitle[hidden] {
  display: none;
}

.voice-toggle {
  position: absolute;
  top: 14px;
  right: 190px;
  pointer-events: auto;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--panel-border);
  padding: 8px 12px;
  font-size: 16px;
}

.voice-toggle.is-off {
  opacity: 0.5;
}

@media (pointer: coarse) {
  .subtitle {
    bottom: 32%;
    font-size: 16px;
  }
}

/* Aviso de lo que se puede usar (puerta, interruptor), bajo la mirilla. */
.prompt {
  position: absolute;
  left: 50%;
  top: 56%;
  transform: translateX(-50%);
  padding: 8px 14px;
  border-radius: 10px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  font-size: 14px;
  font-weight: 700;
  white-space: nowrap;
}

.prompt[hidden] {
  display: none;
}

.prompt::before {
  content: "E · ";
  color: var(--accent);
}

@media (pointer: coarse) {
  .prompt {
    display: none;
  }
}

.use-button {
  position: absolute;
  right: 24px;
  bottom: 40px;
  padding: 18px 26px;
  border-radius: 50px;
  font-size: 16px;
  background: var(--accent);
  color: var(--accent-text);
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.5);
  touch-action: none;
}

.use-button[hidden] {
  display: none;
}

#touchControls {
  position: fixed;
  inset: 0;
  z-index: 9;
}

#touchControls[hidden],
#playHud[hidden],
#editorUi[hidden],
#message[hidden] {
  display: none;
}

.joystick {
  position: absolute;
  left: 24px;
  bottom: 24px;
  width: 140px;
  height: 140px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.08);
  border: 2px solid rgba(255, 255, 255, 0.2);
  touch-action: none;
}

.joystick-knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.55);
  transform: translate(-50%, -50%);
  pointer-events: none;
}

.look-area {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  width: 55%;
  touch-action: none;
}

@media (pointer: fine) {
  #touchControls {
    display: none;
  }
}

/* En tactil los avisos suben para no tapar el joystick. */
@media (pointer: coarse) {
  body.is-playing .toast {
    bottom: 190px;
  }
}

.message {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  background: rgba(0, 0, 0, 0.6);
}

.message-card {
  text-align: center;
  padding: 24px 28px;
  border-radius: 16px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
}

.message-card h2 {
  margin: 0 0 8px;
}

.message-card.is-caught h2 {
  color: var(--danger);
}

.message-actions {
  display: flex;
  gap: 8px;
  justify-content: center;
  margin-top: 12px;
}

.message-extra {
  text-align: left;
  min-width: 260px;
}

.message-extra:empty {
  display: none;
}

.score-form {
  display: flex;
  gap: 6px;
  margin: 8px 0;
}

.score-form input {
  flex: 1;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--panel-border);
  color: var(--text);
  border-radius: 8px;
  padding: 8px 10px;
  font: inherit;
}

.score-list {
  margin: 8px 0 0;
  padding: 0;
  list-style: none;
}

.score-list li {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 4px 0;
  border-bottom: 1px solid var(--panel-border);
  font-size: 14px;
}

.score-list li.is-you {
  color: var(--accent);
}

.score-empty {
  color: var(--muted);
  font-size: 13px;
  margin: 8px 0 0;
}

/* --- Dialogo de carga ------------------------------------------------------ */
dialog {
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  background: #151920;
  color: var(--text);
  padding: 20px;
  width: min(520px, 92vw);
  max-height: 80vh;
}

dialog::backdrop {
  background: rgba(0, 0, 0, 0.6);
}

dialog h2 {
  margin: 0 0 12px;
  font-size: 18px;
}

.tabs {
  display: flex;
  gap: 6px;
  margin-bottom: 10px;
}

.tab {
  background: rgba(255, 255, 255, 0.08);
  color: var(--muted);
}

.tab.is-active {
  background: var(--accent);
  color: var(--accent-text);
}

.room-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 52vh;
  overflow-y: auto;
  margin-bottom: 12px;
}

.room-row {
  display: flex;
  gap: 6px;
}

.room-row .room-item {
  flex: 1;
}

.room-delete {
  flex: 0 0 auto;
  background: rgba(255, 95, 95, 0.15);
  color: var(--danger);
  font-size: 18px;
  padding: 0 14px;
}

.room-item {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  text-align: left;
  background: rgba(255, 255, 255, 0.06);
  color: var(--text);
  font-weight: 500;
}

.room-item span {
  font-size: 12px;
  color: var(--muted);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translate(-50%, 20px);
  z-index: 30;
  padding: 10px 16px;
  border-radius: 12px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  font-size: 14px;
  opacity: 0;
  transition: opacity 0.2s, transform 0.2s;
  pointer-events: none;
  max-width: 90vw;
  text-align: center;
}

.toast.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

/* --- Pantallas estrechas: paleta abajo, en horizontal ---------------------- */
@media (max-width: 760px) {
  .topbar h1 {
    display: none;
  }

  .topbar-actions {
    margin-left: 0;
    flex-basis: 100%;
  }

  .topbar-actions button {
    flex: 1;
    padding: 9px 6px;
  }

  .palette-panel {
    top: auto;
    bottom: 0;
    left: 0;
    right: 0;
    width: auto;
    max-height: 42vh;
    border-right: none;
    border-top: 1px solid var(--panel-border);
  }

  .palette-grid {
    grid-template-columns: repeat(auto-fill, minmax(72px, 1fr));
  }

  .hint {
    display: none;
  }
}

/* Diálogos del pasadizo y de la IA */
.dialog-text {
  margin: 0 0 12px;
  color: var(--muted);
  font-size: 14px;
}

.portal-fields {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 8px;
}

.portal-fields input,
.portal-fields select,
#iaPrompt {
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  color: var(--text);
  padding: 10px 12px;
  font: inherit;
}

.portal-fields input {
  width: 190px;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  font-family: ui-monospace, monospace;
}

.portal-fields select {
  flex: 1;
  min-width: 200px;
}

#iaPrompt {
  width: 100%;
  box-sizing: border-box;
  resize: vertical;
  margin-bottom: 8px;
}

#iaStatus {
  min-height: 1.4em;
}

#iaStatus:empty {
  display: none;
}

/* Cooperativo */
.coop-panel {
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  padding: 12px 14px;
  margin-bottom: 10px;
}

.coop-panel h3 {
  margin: 0 0 8px;
  font-size: 14px;
}

.coop-panel .hint {
  margin: 0 0 8px;
}

.coop-code {
  margin: 0 0 8px;
  font-size: 15px;
}

.coop-code strong {
  font-family: ui-monospace, monospace;
  font-size: 26px;
  letter-spacing: 0.18em;
  margin-left: 6px;
}

.coop-status {
  margin: 0;
  min-height: 1.4em;
  color: var(--accent);
  font-weight: 600;
}

.coop-hud {
  position: absolute;
  top: 12px;
  left: 50%;
  transform: translateX(-50%) translateY(38px);
  background: rgba(0, 0, 0, 0.55);
  color: #d8d2c4;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 12px;
  pointer-events: none;
}

.coop-hud:empty {
  display: none;
}

/* Las ayudas de los diálogos sí se ven en móvil (la de la paleta no cabe). */
@media (max-width: 760px) {
  dialog .hint {
    display: block;
  }
}
