*,
*::before,
*::after {
  box-sizing: border-box;
}

:root {
  --bg: #0b0d10;
  --fg: #f4f6f8;
  --fg-muted: rgba(244, 246, 248, 0.88);
  --accent: #5eb1ff;
  --danger: #ffb4b4;
  --overlay-pad: max(0.75rem, env(safe-area-inset-left, 0px));
  --overlay-pad-r: max(0.75rem, env(safe-area-inset-right, 0px));
  --overlay-pad-b: max(0.85rem, env(safe-area-inset-bottom, 0px));
  --overlay-pad-t: max(0.5rem, env(safe-area-inset-top, 0px));
  --touch-min: 44px;
  --radius: 0.7rem;
}

html {
  margin: 0;
  width: 100%;
  height: 100%;
  height: 100dvh;
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  width: 100%;
  min-height: 100%;
  min-height: 100dvh;
  min-height: -webkit-fill-available;
  overflow: hidden;
  background: var(--bg);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  color: var(--fg);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  overscroll-behavior: none;
  position: fixed;
  inset: 0;
}

#app {
  position: relative;
  width: 100%;
  height: 100%;
  height: 100dvh;
  height: -webkit-fill-available;
}

#ar-container {
  position: absolute;
  inset: 0;
  overflow: hidden;
  background: #000;
}

#ar-container canvas,
#ar-container video {
  display: block;
  max-width: 100%;
}

/* Videos inyectados por MindAR / AR.js */
video {
  -webkit-playsinline: true;
  object-fit: cover;
}

#overlay {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 20;
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
  max-height: min(48vh, 22rem);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding-top: var(--overlay-pad-t);
  padding-left: var(--overlay-pad);
  padding-right: var(--overlay-pad-r);
  padding-bottom: var(--overlay-pad-b);
  background: linear-gradient(transparent, rgba(8, 10, 14, 0.92) 28%);
  pointer-events: none;
}

#overlay > * {
  pointer-events: auto;
}

.lib-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.lib-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.4rem 0.75rem;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.12);
  border: 1px solid rgba(255, 255, 255, 0.18);
  font-size: clamp(0.7rem, 2.6vw, 0.8rem);
  letter-spacing: 0.02em;
  text-transform: uppercase;
}

.compat-chip {
  font-size: clamp(0.65rem, 2.4vw, 0.75rem);
  color: var(--fg-muted);
  opacity: 0.9;
}

#help-text,
#status-text {
  margin: 0;
  font-size: clamp(0.82rem, 3.2vw, 0.95rem);
  line-height: 1.35;
  color: var(--fg-muted);
  max-width: 40rem;
}

#status-text.is-error {
  color: var(--danger);
}

#status-text:empty,
#help-text:empty {
  display: none;
}

.btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

button,
.btn {
  appearance: none;
  -webkit-appearance: none;
  border: 0;
  border-radius: var(--radius);
  padding: 0.75rem 1rem;
  font: inherit;
  font-size: clamp(0.9rem, 3.4vw, 1rem);
  font-weight: 600;
  cursor: pointer;
  color: var(--bg);
  background: var(--fg);
  min-height: var(--touch-min);
  min-width: var(--touch-min);
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}

button:active,
.btn:active {
  transform: translateY(1px);
  opacity: 0.92;
}

button.secondary {
  background: rgba(255, 255, 255, 0.14);
  color: var(--fg);
  border: 1px solid rgba(255, 255, 255, 0.22);
}

button.active {
  background: var(--accent);
  color: #041018;
}

#btn-switch,
#btn-fallback {
  width: 100%;
  background: var(--fg);
  color: var(--bg);
}

.btn-row #btn-switch {
  width: auto;
  flex: 1 1 auto;
}

#btn-logs {
  flex: 0 0 auto;
}

#btn-fallback.hidden,
#webxr-controls.hidden,
#compat-banner.hidden,
#log-panel.hidden {
  display: none !important;
}

#log-panel {
  position: absolute;
  left: var(--overlay-pad);
  right: var(--overlay-pad-r);
  top: max(0.5rem, env(safe-area-inset-top, 0px));
  z-index: 40;
  max-height: min(42vh, 16rem);
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  padding: 0.5rem;
  border-radius: var(--radius);
  background: rgba(5, 8, 12, 0.92);
  border: 1px solid rgba(255, 255, 255, 0.16);
  pointer-events: auto;
}

.log-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.log-toolbar strong {
  font-size: 0.8rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

#log-body {
  margin: 0;
  flex: 1 1 auto;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.68rem;
  line-height: 1.35;
  white-space: pre-wrap;
  word-break: break-word;
  color: #c8f5c8;
  min-height: 4rem;
}

body.logs-open #overlay {
  max-height: min(36vh, 16rem);
}

#compat-banner {
  margin: 0;
  padding: 0.55rem 0.7rem;
  border-radius: var(--radius);
  background: rgba(255, 180, 180, 0.14);
  border: 1px solid rgba(255, 180, 180, 0.35);
  color: #ffd6d6;
  font-size: clamp(0.78rem, 3vw, 0.88rem);
  line-height: 1.35;
}

#webxr-controls {
  width: 100%;
}

#webxr-controls button {
  flex: 1 1 calc(50% - 0.25rem);
}

/* Landscape: overlay más compacto para no tapar el tracking */
@media (orientation: landscape) and (max-height: 500px) {
  #overlay {
    max-height: 42vh;
    gap: 0.35rem;
    padding-top: 0.35rem;
    background: linear-gradient(transparent, rgba(8, 10, 14, 0.94) 40%);
  }

  #help-text {
    display: none;
  }

  button,
  .btn {
    min-height: 40px;
    padding: 0.55rem 0.8rem;
  }
}

/* Tablets / desktop: más aire y ancho máximo de controles */
@media (min-width: 768px) {
  #overlay {
    left: 50%;
    right: auto;
    transform: translateX(-50%);
    width: min(28rem, calc(100% - 2rem));
    max-height: 40vh;
    border-radius: 1rem 1rem 0 0;
  }
}

/* MindAR: video a pantalla completa */
body.mindar-mode video {
  position: absolute !important;
  top: 0 !important;
  left: 0 !important;
  width: 100% !important;
  height: 100% !important;
  object-fit: cover !important;
  z-index: 1 !important;
}

body.mindar-mode #ar-container canvas {
  z-index: 2;
  position: absolute !important;
  top: 0 !important;
  left: 0 !important;
}

/*
 * AR.js: NO estirar el video a cover.
 * El tamaño lo fija arToolkitSource.onResizeElement() para que
 * coincida con el canvas de tracking; si no, la cámara “baila” y no hay cubo.
 */
body.arjs-mode video {
  z-index: 1 !important;
  object-fit: fill !important;
  background: #000;
}

body.arjs-mode #ar-container canvas {
  z-index: 2 !important;
  position: absolute !important;
}

/* Three.js ARButton */
#ARButton {
  position: absolute !important;
  left: 50% !important;
  top: 40% !important;
  bottom: auto !important;
  transform: translate(-50%, -50%) !important;
  z-index: 15 !important;
  border-radius: var(--radius) !important;
  padding: 0.85rem 1.2rem !important;
  font: inherit !important;
  font-weight: 600 !important;
  border: 0 !important;
  background: var(--fg) !important;
  color: var(--bg) !important;
  opacity: 1 !important;
  min-height: var(--touch-min) !important;
  min-width: 10rem !important;
  -webkit-appearance: none !important;
}

@supports not (height: 100dvh) {
  html,
  #app {
    height: 100%;
  }

  body {
    min-height: 100%;
  }
}
