/* ==========================================================================
   1. VARIABLES Y RESET BASE (ESTÉTICA iOS / HUMAN INTERFACE GUIDELINES)
   ========================================================================== */
:root {
  --bg-app: #0B0B0E;
  --bg-viewport: #16161B;
  --bg-dock: rgba(24, 24, 28, 0.94);
  --bg-surface-glass: rgba(34, 34, 38, 0.78);
  --bg-sheet: rgba(28, 28, 30, 0.9);
  --bg-control: rgba(118, 118, 128, 0.24);
  --bg-control-active: rgba(255, 255, 255, 0.18);

  --text-primary: #F5F5F7;
  --text-secondary: rgba(235, 235, 245, 0.65);
  --text-tertiary: rgba(235, 235, 245, 0.38);

  --ios-blue: #0A84FF;
  --ios-green: #30D158;
  --ios-amber: #FF9F0A;
  --ios-red: #FF453A;
  --hairline: rgba(255, 255, 255, 0.14);

  /* Proporción estándar ISO/IEC 7810 ID-1: 85.60mm x 53.98mm (~1.58577) */
  --id1-aspect: 85.60 / 53.98;
  --id1-ratio-num: 1.5857725;

  --font-system: -apple-system, BlinkMacSystemFont, "SF Pro Display", "SF Pro Text", system-ui, sans-serif;
}

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: var(--bg-app);
  color: var(--text-primary);
  font-family: var(--font-system);
  -webkit-font-smoothing: antialiased;
  touch-action: none;
  overscroll-behavior: none;
  position: fixed;
  inset: 0;
}

/* ==========================================================================
   2. ESTRUCTURA PRINCIPAL: VISORES ID-1 PERMANENTES + HUECO INFERIOR DE MENÚ
   ========================================================================== */
.app-shell {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  width: 100%;
  height: 100%;
  padding-top: env(safe-area-inset-top);
}

/* Contenedor de los dos visores ID-1 unidos sin huecos para capturas limpias */
.comparison-stage {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 2px 4px;
  overflow: hidden;
}

@media (orientation: portrait) {
  .comparison-stage {
    flex-direction: column;
    justify-content: flex-start;
  }

  /* Cada mitad mantiene SIEMPRE la proporción ID-1 exacta (85.60 : 53.98) */
  .viewport {
    width: min(100%, calc((100dvh - 168px - env(safe-area-inset-top) - env(safe-area-inset-bottom)) * 0.5 * var(--id1-ratio-num)));
    aspect-ratio: var(--id1-aspect);
  }
}

@media (orientation: landscape) {
  .comparison-stage {
    flex-direction: row;
    justify-content: center;
  }

  .viewport {
    width: min(calc(50% - 2px), calc((100dvh - 132px - env(safe-area-inset-top) - env(safe-area-inset-bottom)) * var(--id1-ratio-num)));
    aspect-ratio: var(--id1-aspect);
  }
}

/* ==========================================================================
   3. VISORES ID-1 Y ESTADOS DE DICTAMEN
   ========================================================================== */
.viewport {
  position: relative;
  overflow: hidden;
  background-color: var(--bg-viewport);
  touch-action: none;
  border-radius: 3.7% / 5.88%; /* Curvatura oficial ISO ID-1 */
  border: 2.5px solid rgba(255, 255, 255, 0.12);
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

/* Resalte sutil del visor seleccionado para calibrar cuando las guías están activas */
.app-shell[data-guides="on"] .viewport.is-calibrating-target {
  box-shadow: 0 0 0 1px rgba(10, 132, 255, 0.45);
}

/* Dictamen: Coincidente (Verde continuo en ambos visores) */
.app-shell[data-verdict="match"] .viewport {
  border-color: var(--ios-green);
  border-style: solid;
}

/* Dictamen: Divergente (Verde continuo en Maestro, Rojo discontinuo en Esclavo) */
.app-shell[data-verdict="divergent"] .viewport--master {
  border-color: var(--ios-green);
  border-style: solid;
}

.app-shell[data-verdict="divergent"] .viewport--slave {
  border-color: var(--ios-red);
  border-style: dashed;
}

/* Etiquetas discretas: Maestro - Indubitado (abajo-izquierda) y Esclavo - Dubitado (arriba-izquierda) */
.viewport__badge {
  position: absolute;
  left: 10px;
  z-index: 25;
  display: inline-flex;
  align-items: center;
  padding: 3px 9px;
  border-radius: 999px;
  background: var(--bg-surface-glass);
  backdrop-filter: blur(14px) saturate(180%);
  -webkit-backdrop-filter: blur(14px) saturate(180%);
  border: 0.5px solid var(--hairline);
  font-size: 10.5px;
  font-weight: 600;
  letter-spacing: 0.01em;
  color: var(--text-secondary);
  pointer-events: none;
  transition: opacity 0.18s ease;
}

.viewport--master .viewport__badge {
  bottom: 8px;
}

.viewport--slave .viewport__badge {
  top: 8px;
}

.app-shell[data-labels="off"] .viewport__badge {
  opacity: 0;
}

/* Botón Cambiar foto (oculto salvo si se activa desde el menú) */
.viewport__replace-btn {
  position: absolute;
  right: 10px;
  z-index: 25;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--bg-surface-glass);
  backdrop-filter: blur(14px) saturate(180%);
  -webkit-backdrop-filter: blur(14px) saturate(180%);
  border: 0.5px solid var(--hairline);
  color: var(--text-primary);
  font-family: inherit;
  font-size: 11px;
  font-weight: 500;
  cursor: pointer;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.18s ease, transform 0.12s ease;
}

.viewport--master .viewport__replace-btn {
  bottom: 8px;
}

.viewport--slave .viewport__replace-btn {
  top: 8px;
}

.app-shell[data-replace="on"] .viewport.has-image .viewport__replace-btn {
  opacity: 1;
  pointer-events: auto;
}

.viewport__replace-btn:active {
  transform: scale(0.95);
}

/* ==========================================================================
   4. CAPAS DE TRANSFORMACIÓN GPU (CÁMARA ESQUINA -> PANZOOM -> WARP -> ROTACIÓN)
   ========================================================================== */
.viewport__camera {
  position: absolute;
  inset: 0;
  transform-origin: 0 0;
  will-change: transform;
  transition: transform 0.2s cubic-bezier(0.22, 1, 0.36, 1);
}

.viewport__camera.is-corner-zooming {
  transition: transform 0.14s cubic-bezier(0.22, 1, 0.36, 1);
}

.viewport__panzoom {
  position: absolute;
  inset: 0;
  transform-origin: center center;
  will-change: transform;
  pointer-events: none;
}

.viewport__warp {
  position: absolute;
  inset: 0;
  transform-origin: 0 0;
  will-change: transform;
  pointer-events: none;
}

.viewport__rotator {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  transform-origin: center center;
  will-change: transform;
  pointer-events: none;
}

.viewport__image {
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
  display: none;
  -webkit-user-drag: none;
}

.viewport.has-image .viewport__image {
  display: block;
}

/* ==========================================================================
   5. CUADRÍCULA ID-1 Y ESQUINAS INTERACTIVAS CON ZOOM
   ========================================================================== */
.id1-guide {
  position: absolute;
  inset: 0;
  z-index: 18;
  pointer-events: none;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 3.7% / 5.88%;
}

/* Retícula de tercios y ejes centrales ID-1 */
.id1-guide::before {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  border: 1.5px solid rgba(255, 255, 255, 0.38);
  background:
    linear-gradient(to right, transparent calc(50% - 0.5px), rgba(255, 255, 255, 0.16) calc(50% - 0.5px), rgba(255, 255, 255, 0.16) calc(50% + 0.5px), transparent calc(50% + 0.5px)),
    linear-gradient(to bottom, transparent calc(50% - 0.5px), rgba(255, 255, 255, 0.16) calc(50% - 0.5px), rgba(255, 255, 255, 0.16) calc(50% + 0.5px), transparent calc(50% + 0.5px));
  pointer-events: none;
  transition: opacity 0.2s ease;
}

.app-shell[data-guides="off"] .id1-guide::before {
  opacity: 0;
}

/* Tiradores interactivos en las 4 esquinas curvas del ID-1 */
.corner-handle {
  position: absolute;
  width: 44px;
  height: 44px;
  z-index: 22;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.18s ease, transform 0.15s ease;
  touch-action: none;
  cursor: grab;
}

/* Solo activos cuando hay imagen cargada y las guías ID-1 están visibles */
.app-shell[data-guides="on"] .viewport.has-image .corner-handle {
  opacity: 1;
  pointer-events: auto;
}

/* Arco visual siguiendo la esquina curva del ID-1 */
.corner-handle::before {
  content: "";
  position: absolute;
  width: 22px;
  height: 22px;
  border-color: var(--ios-blue);
  border-style: solid;
  border-width: 0;
  filter: drop-shadow(0 1px 3px rgba(0, 0, 0, 0.65));
  transition: border-color 0.15s ease, transform 0.15s ease;
}

/* Modo distorsión activa: esquinas en color ámbar para distinguir perspectiva libre */
.viewport[data-distortion="on"] .corner-handle::before {
  border-color: var(--ios-amber);
}

.corner-handle.is-dragging::before {
  border-color: var(--ios-green);
  transform: scale(1.08);
}

.corner-handle--tl {
  top: 0;
  left: 0;
}
.corner-handle--tl::before {
  top: 2px;
  left: 2px;
  border-top-width: 3.5px;
  border-left-width: 3.5px;
  border-top-left-radius: 12px;
}

.corner-handle--tr {
  top: 0;
  right: 0;
}
.corner-handle--tr::before {
  top: 2px;
  right: 2px;
  border-top-width: 3.5px;
  border-right-width: 3.5px;
  border-top-right-radius: 12px;
}

.corner-handle--br {
  bottom: 0;
  right: 0;
}
.corner-handle--br::before {
  bottom: 2px;
  right: 2px;
  border-bottom-width: 3.5px;
  border-right-width: 3.5px;
  border-bottom-right-radius: 12px;
}

.corner-handle--bl {
  bottom: 0;
  left: 0;
}
.corner-handle--bl::before {
  bottom: 2px;
  left: 2px;
  border-bottom-width: 3.5px;
  border-left-width: 3.5px;
  border-bottom-left-radius: 12px;
}

/* Estado vacío centrado en el marco ID-1 */
.viewport__empty {
  position: relative;
  z-index: 20;
  pointer-events: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 14px 20px;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.04);
  border: 1px dashed rgba(255, 255, 255, 0.2);
  color: var(--text-secondary);
  cursor: pointer;
  transition: background-color 0.15s ease, transform 0.12s ease;
  text-align: center;
  max-width: min(250px, 84%);
}

.viewport__empty:active {
  background: rgba(255, 255, 255, 0.09);
  transform: scale(0.98);
}

.viewport.has-image .viewport__empty {
  display: none;
}

.viewport__empty-icon {
  width: 30px;
  height: 30px;
  stroke: var(--ios-blue);
  stroke-width: 1.75;
  fill: none;
  flex-shrink: 0;
}

.viewport__empty-title {
  font-size: 13.5px;
  font-weight: 600;
  color: var(--text-primary);
}

.viewport__empty-subtitle {
  font-size: 11.5px;
  line-height: 1.3;
  color: var(--text-secondary);
}

/* ==========================================================================
   6. DOCK INFERIOR DE AJUSTES Y MENÚ (EN EL HUECO INFERIOR)
   ========================================================================== */
.bottom-dock {
  flex: 0 0 auto;
  width: 100%;
  background: var(--bg-dock);
  backdrop-filter: blur(24px) saturate(180%);
  -webkit-backdrop-filter: blur(24px) saturate(180%);
  border-top: 0.5px solid var(--hairline);
  padding: 8px 12px max(10px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 30;
}

.dock-topbar {
  display: flex;
  align-items: center;
  gap: 8px;
}

.dock-target-selector {
  flex: 1;
  grid-template-columns: 1fr 1fr !important;
}

.central-trigger {
  flex-shrink: 0;
  width: 36px;
  height: 34px;
  border-radius: 9px;
  border: 0.5px solid var(--hairline);
  background: var(--bg-control);
  color: var(--text-primary);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: transform 0.12s ease, background-color 0.15s ease;
}

.central-trigger:active {
  transform: scale(0.93);
  background: var(--bg-control-active);
}

.central-trigger svg {
  width: 20px;
  height: 20px;
}

.dock-tools {
  display: flex;
  flex-direction: column;
  gap: 7px;
}

.dock-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.rot-buttons {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

.tool-pill {
  appearance: none;
  border: 0.5px solid var(--hairline);
  background: var(--bg-control);
  color: var(--text-primary);
  font-family: inherit;
  font-size: 12px;
  font-weight: 500;
  padding: 5px 9px;
  border-radius: 7px;
  cursor: pointer;
  white-space: nowrap;
}

.tool-pill:active {
  background: var(--bg-control-active);
  transform: scale(0.96);
}

.tool-pill--subtle {
  font-size: 11.5px;
  color: var(--ios-blue);
}

.fine-rot-control {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}

.ios-slider {
  flex: 1;
  -webkit-appearance: none;
  appearance: none;
  height: 4px;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.2);
  outline: none;
  min-width: 60px;
}

.ios-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #FFFFFF;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.45);
  cursor: pointer;
}

.angle-readout {
  appearance: none;
  border: none;
  background: rgba(255, 255, 255, 0.08);
  color: var(--text-secondary);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11px;
  padding: 4px 6px;
  border-radius: 6px;
  min-width: 44px;
  text-align: center;
  cursor: pointer;
}

/* Selector de modo de distorsión en el dock */
.distortion-toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  font-size: 12px;
  color: var(--text-secondary);
  min-width: 0;
}

.distortion-toggle input {
  display: none;
}

.distortion-toggle__box {
  width: 18px;
  height: 18px;
  border-radius: 5px;
  border: 1.5px solid rgba(255, 255, 255, 0.35);
  background: transparent;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background-color 0.15s ease, border-color 0.15s ease;
}

.distortion-toggle input:checked + .distortion-toggle__box {
  background: var(--ios-amber);
  border-color: var(--ios-amber);
}

.distortion-toggle input:checked + .distortion-toggle__box::after {
  content: "";
  width: 5px;
  height: 9px;
  border: solid #000;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg) translate(-0.5px, -0.5px);
}

.distortion-toggle__text {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.distortion-toggle__text strong {
  color: var(--text-primary);
  font-weight: 600;
}

/* ==========================================================================
   7. ACTION SHEET ESTILO iOS Y MODAL DE CONFIRMACIÓN
   ========================================================================== */
.overlay-backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgba(0, 0, 0, 0.45);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.22s ease;
}

.overlay-backdrop.is-open {
  opacity: 1;
  pointer-events: auto;
}

.action-sheet {
  position: fixed;
  left: 50%;
  bottom: max(12px, env(safe-area-inset-bottom));
  transform: translate(-50%, calc(100% + 24px));
  z-index: 50;
  width: min(94vw, 400px);
  display: flex;
  flex-direction: column;
  gap: 8px;
  transition: transform 0.26s cubic-bezier(0.32, 0.72, 0, 1);
  pointer-events: none;
}

.action-sheet.is-open {
  transform: translate(-50%, 0);
  pointer-events: auto;
}

.sheet-group {
  background: var(--bg-sheet);
  backdrop-filter: blur(28px) saturate(190%);
  -webkit-backdrop-filter: blur(28px) saturate(190%);
  border: 0.5px solid var(--hairline);
  border-radius: 14px;
  overflow: hidden;
}

.sheet-header {
  padding: 12px 16px 8px;
  text-align: center;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  border-bottom: 0.5px solid var(--hairline);
}

.sheet-section {
  padding: 12px 16px;
  border-bottom: 0.5px solid var(--hairline);
}

.sheet-section:last-child {
  border-bottom: none;
}

.sheet-label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  margin-bottom: 8px;
}

/* Segmented Control iOS */
.segmented-control {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  padding: 2px;
  background: var(--bg-control);
  border-radius: 9px;
  gap: 2px;
}

.segmented-control__btn {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--text-primary);
  font-family: inherit;
  font-size: 12.5px;
  font-weight: 500;
  padding: 6px 6px;
  border-radius: 7px;
  cursor: pointer;
  transition: background-color 0.15s ease, color 0.15s ease;
}

.segmented-control__btn.is-active {
  background: rgba(255, 255, 255, 0.22);
  font-weight: 600;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.25);
}

.segmented-control__btn[data-verdict-option="match"].is-active {
  background: rgba(48, 209, 88, 0.28);
  color: #66E887;
}

.segmented-control__btn[data-verdict-option="divergent"].is-active {
  background: rgba(255, 69, 58, 0.28);
  color: #FF857D;
}

/* Fila con Toggle Switch iOS */
.sheet-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.sheet-row__title {
  font-size: 15px;
  font-weight: 400;
  color: var(--text-primary);
}

.ios-switch {
  position: relative;
  width: 51px;
  height: 31px;
  flex-shrink: 0;
}

.ios-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.ios-switch__slider {
  position: absolute;
  inset: 0;
  background-color: rgba(120, 120, 128, 0.36);
  border-radius: 31px;
  cursor: pointer;
  transition: background-color 0.2s ease;
}

.ios-switch__slider::before {
  content: "";
  position: absolute;
  width: 27px;
  height: 27px;
  left: 2px;
  top: 2px;
  background-color: #FFFFFF;
  border-radius: 50%;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
  transition: transform 0.2s cubic-bezier(0.32, 0.72, 0, 1);
}

.ios-switch input:checked + .ios-switch__slider {
  background-color: var(--ios-green);
}

.ios-switch input:checked + .ios-switch__slider::before {
  transform: translateX(20px);
}

/* Botones de acción en el Action Sheet */
.sheet-btn {
  width: 100%;
  padding: 14px 16px;
  border: none;
  background: transparent;
  font-family: inherit;
  font-size: 17px;
  font-weight: 400;
  color: var(--ios-blue);
  text-align: center;
  cursor: pointer;
}

.sheet-btn:active {
  background: var(--bg-control-active);
}

.sheet-btn--destructive {
  color: var(--ios-red);
}

.sheet-btn--cancel {
  font-weight: 600;
}

/* Modal de alerta/confirmación estilo iOS */
.ios-alert {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.94);
  z-index: 60;
  width: min(84vw, 280px);
  background: var(--bg-sheet);
  backdrop-filter: blur(28px) saturate(190%);
  -webkit-backdrop-filter: blur(28px) saturate(190%);
  border: 0.5px solid var(--hairline);
  border-radius: 14px;
  overflow: hidden;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.18s ease, transform 0.18s ease;
}

.ios-alert.is-open {
  opacity: 1;
  transform: translate(-50%, -50%) scale(1);
  pointer-events: auto;
}

.ios-alert__body {
  padding: 18px 16px 16px;
  text-align: center;
}

.ios-alert__title {
  font-size: 17px;
  font-weight: 600;
  margin-bottom: 6px;
  color: var(--text-primary);
}

.ios-alert__message {
  font-size: 13px;
  line-height: 1.38;
  color: var(--text-secondary);
}

.ios-alert__actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  border-top: 0.5px solid var(--hairline);
}

.ios-alert__btn {
  appearance: none;
  border: none;
  background: transparent;
  padding: 12px 8px;
  font-family: inherit;
  font-size: 17px;
  color: var(--ios-blue);
  cursor: pointer;
}

.ios-alert__btn:first-child {
  border-right: 0.5px solid var(--hairline);
  font-weight: 400;
}

.ios-alert__btn--destructive {
  color: var(--ios-red);
  font-weight: 600;
}

.ios-alert__btn:active {
  background: var(--bg-control-active);
}
