:root {
  --ink: #0d0d0d;
  --panel: rgba(13, 13, 13, 0.86);
  --line: rgba(255, 255, 255, 0.12);
  --text: #f3f2ef;
  --muted: #a3a29d;
  --brand: #78b829;
  --brand-soft: rgba(120, 184, 41, 0.18);
  --radius: 14px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--ink);
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

#scene { position: fixed; inset: 0; }
#scene canvas { display: block; touch-action: none; cursor: grab; }

button { font: inherit; }
.muted { color: var(--muted); }

/* ---------- Intro ---------- */
.intro {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 16px;
  background: radial-gradient(ellipse at center, rgba(13, 13, 13, 0.7), rgba(13, 13, 13, 0.94));
  transition: opacity 0.35s ease;
}
.intro.hidden { opacity: 0; pointer-events: none; }
.intro-card {
  width: min(460px, 100%);
  padding: 28px 26px 24px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 20px;
  backdrop-filter: blur(12px);
  text-align: center;
}
.intro-logo { width: 120px; height: auto; }
.intro-kicker {
  margin: 14px 0 4px;
  font-size: 11px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--brand);
}
.intro h1 { margin: 0 0 16px; font-size: 22px; font-weight: 600; line-height: 1.25; }
.intro-help {
  margin: 0 0 20px;
  padding: 0;
  list-style: none;
  display: grid;
  gap: 8px;
  font-size: 13.5px;
  color: #d6d4cf;
  text-align: left;
}
.intro-help li { padding: 8px 12px; background: rgba(255, 255, 255, 0.04); border-radius: 10px; }
.intro-help b { color: var(--brand); }
kbd {
  display: inline-block;
  min-width: 22px;
  margin-right: 3px;
  padding: 1px 5px;
  font: 600 11px/1.6 inherit;
  text-align: center;
  color: var(--text);
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid var(--line);
  border-radius: 5px;
}
.btn-primary {
  width: 100%;
  padding: 13px 18px;
  font-size: 15px;
  font-weight: 600;
  color: #0d0d0d;
  background: var(--brand);
  border: none;
  border-radius: 12px;
  cursor: pointer;
}
.btn-primary:disabled { opacity: 0.6; cursor: progress; }
.is-touch .only-desktop, :root:not(.is-touch) .only-touch { display: none; }

/* ---------- Barra superior ---------- */
.topbar {
  position: fixed;
  top: 12px;
  left: 12px;
  right: 12px;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 10px;
  pointer-events: none;
}
.topbar > * { pointer-events: auto; }
.logo {
  height: 40px;
  width: auto;
  padding: 8px 12px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  backdrop-filter: blur(8px);
  box-sizing: content-box;
}
.rooms {
  display: flex;
  gap: 4px;
  padding: 4px;
  margin: 0 auto;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  backdrop-filter: blur(8px);
}
.rooms button, .chip {
  padding: 8px 12px;
  font-size: 12.5px;
  color: var(--text);
  background: transparent;
  border: 1px solid transparent;
  border-radius: 9px;
  cursor: pointer;
  white-space: nowrap;
}
.rooms button:hover { background: rgba(255, 255, 255, 0.08); }
.rooms button.active { color: #0d0d0d; background: var(--brand); }
.rooms-select { display: none; }
.top-actions { display: flex; gap: 6px; }
.chip {
  background: var(--panel);
  border-color: var(--line);
  backdrop-filter: blur(8px);
}
.chip:hover { border-color: var(--brand); }
.chip.icon { width: 36px; padding: 8px 0; font-weight: 700; }

/* ---------- Etiqueta de habitación y tooltip ---------- */
.room-label {
  position: fixed;
  left: 50%;
  bottom: 22px;
  transform: translateX(-50%);
  padding: 8px 16px;
  font-size: 12.5px;
  letter-spacing: 0.06em;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 999px;
  pointer-events: none;
}
.room-label b { color: var(--brand); font-weight: 600; }
.room-label { white-space: nowrap; }
.tooltip {
  position: fixed;
  z-index: 20;
  padding: 7px 11px;
  font-size: 12.5px;
  line-height: 1.35;
  background: var(--panel);
  border: 1px solid var(--brand);
  border-radius: 10px;
  pointer-events: none;
  transform: translate(14px, 14px);
  white-space: nowrap;
}
.tooltip small { display: block; color: var(--muted); font-size: 11px; }

/* ---------- Minimapa ---------- */
.minimap {
  position: fixed;
  left: 12px;
  bottom: 12px;
  z-index: 5;
  width: 210px;
  height: 150px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  cursor: crosshair;
}

/* ---------- Joystick (sólo táctil) ---------- */
.joystick {
  display: none;
  position: fixed;
  left: 22px;
  bottom: calc(26px + var(--safe-b));
  z-index: 6;
  width: 120px;
  height: 120px;
  border-radius: 50%;
  background: rgba(13, 13, 13, 0.45);
  border: 1px solid rgba(255, 255, 255, 0.22);
  touch-action: none;
}
.joystick .knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 52px;
  height: 52px;
  margin: -26px 0 0 -26px;
  border-radius: 50%;
  background: rgba(120, 184, 41, 0.85);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.4);
}

/* ---------- Panel de texturas ---------- */
.panel {
  position: fixed;
  top: 70px;
  right: 12px;
  bottom: 12px;
  z-index: 15;
  width: 360px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 16px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 18px;
  backdrop-filter: blur(12px);
  transform: translateX(calc(100% + 24px));
  opacity: 0;
  transition: transform 0.25s ease, opacity 0.25s ease;
  pointer-events: none;
}
.panel.open { transform: none; opacity: 1; pointer-events: auto; }
.panel-grab { display: none; }
.panel-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 10px; }
.panel-kicker {
  margin: 0 0 2px;
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: 0.14em;
  color: var(--brand);
}
.panel h2 { margin: 0; font-size: 19px; font-weight: 600; }
.icon-btn {
  flex: none;
  width: 32px;
  height: 32px;
  color: var(--text);
  background: rgba(255, 255, 255, 0.08);
  border: none;
  border-radius: 9px;
  cursor: pointer;
}
.icon-btn:hover { background: rgba(255, 255, 255, 0.16); }
.current {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px;
  background: rgba(255, 255, 255, 0.04);
  border-radius: 12px;
  font-size: 13px;
}
.current img { width: 44px; height: 44px; object-fit: cover; border-radius: 8px; background: #222; }
.current > div { display: grid; flex: 1; min-width: 0; }
.current .muted { font-size: 11px; }
.current strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.link-btn { padding: 4px 6px; font-size: 12px; color: var(--brand); background: none; border: none; cursor: pointer; }
.check { display: flex; align-items: center; gap: 8px; font-size: 12.5px; color: #d6d4cf; cursor: pointer; }
.check input { accent-color: var(--brand); width: 16px; height: 16px; }
.scale-row { display: flex; align-items: center; gap: 10px; font-size: 12.5px; }
.scale-row input { flex: 1; accent-color: var(--brand); }
.scale-row span { min-width: 54px; text-align: right; font-variant-numeric: tabular-nums; }
.search {
  width: 100%;
  padding: 9px 12px;
  font: inherit;
  font-size: 13px;
  color: var(--text);
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--line);
  border-radius: 10px;
  outline: none;
}
.search:focus { border-color: var(--brand); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips button {
  padding: 5px 10px;
  font-size: 11.5px;
  color: #d6d4cf;
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 999px;
  cursor: pointer;
}
.chips button.active { color: #0d0d0d; background: var(--brand); border-color: var(--brand); }
.chips button small { opacity: 0.7; }
.swatches {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  align-content: start;
  gap: 8px;
  padding-right: 2px;
}
.swatch {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 5px;
  padding: 5px;
  font-size: 11px;
  line-height: 1.2;
  color: #d6d4cf;
  text-align: center;
  background: rgba(255, 255, 255, 0.04);
  border: 1.5px solid var(--line);
  border-radius: 12px;
  cursor: pointer;
  transition: border-color 0.15s, transform 0.1s;
}
.swatch img { width: 100%; aspect-ratio: 1; object-fit: cover; border-radius: 8px; display: block; background: #222; }
.swatch:hover { border-color: rgba(120, 184, 41, 0.6); transform: translateY(-1px); }
.swatch.active { border-color: var(--brand); box-shadow: 0 0 0 1px var(--brand); color: var(--text); }
.swatch.loading::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 22px;
  height: 22px;
  margin: -20px 0 0 -11px;
  border: 2px solid var(--brand);
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
.swatch .tag {
  position: absolute;
  top: 9px;
  left: 9px;
  padding: 1px 6px;
  font-size: 9.5px;
  font-weight: 700;
  letter-spacing: 0.06em;
  color: #0d0d0d;
  background: var(--brand);
  border-radius: 999px;
}
.api-status { margin: 0; font-size: 11.5px; color: var(--muted); }
.api-status.error { color: #e9a07a; }
.empty { grid-column: 1 / -1; padding: 18px 6px; font-size: 12.5px; color: var(--muted); text-align: center; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  top: 74px;
  z-index: 40;
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  padding: 10px 16px;
  font-size: 13px;
  background: var(--panel);
  border: 1px solid var(--brand);
  border-radius: 12px;
}

/* ---------- Táctil / móvil ---------- */
.is-touch .joystick { display: block; }
.is-touch .minimap { left: auto; right: 12px; bottom: calc(12px + var(--safe-b)); width: 150px; height: 107px; }
.is-touch .room-label { bottom: auto; top: 66px; }

@media (max-width: 900px) {
  .rooms { display: none; }
  .rooms-select {
    display: block;
    flex: 1;
    min-width: 0;
    padding: 9px 10px;
    font: inherit;
    font-size: 13px;
    color: var(--text);
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 10px;
  }
  .logo { height: 28px; padding: 6px 9px; }
}

@media (max-width: 680px) {
  .panel {
    top: auto;
    left: 0;
    right: 0;
    bottom: 0;
    width: auto;
    height: min(52vh, 460px);
    padding: 10px 14px calc(12px + var(--safe-b));
    border-radius: 18px 18px 0 0;
    transform: translateY(100%);
  }
  .panel-grab { display: block; width: 40px; height: 4px; margin: 0 auto; border-radius: 4px; background: rgba(255, 255, 255, 0.25); }
  .swatches { grid-template-columns: repeat(4, 1fr); gap: 6px; }
  .swatch { font-size: 10px; padding: 4px; }
  .panel h2 { font-size: 17px; }
  .chip:not(.icon) { padding: 8px 9px; font-size: 11.5px; }
  .intro h1 { font-size: 19px; }
}

.link-chip { display: inline-flex; align-items: center; text-decoration: none; font-weight: 600; }
