/*
 * Critical CSS - Above the fold styles only
 * Includes: CSS variables, body, header, loading states
 * All other styles are loaded asynchronously
 */
:root {
  /* Paleta inspirada en Waze: fondo claro verdoso, texto azul oscuro y acento azul cielo */
  --bg: #f3f8f2;
  --text: #056a82;
  --accent: #5487A4;
  /* Altura preferida del estado colapsado del bottom sheet (dinámica) */
  --collapsed-min: 128px;
  /* Posición y tamaño del header (ajustables) */
  --hdr-top: 0px;   /* la barra llega hasta arriba */
  --hdr-h: 36px;    /* altura del header */
  --offline-h: 0px; /* altura del banner offline cuando esté visible */
  /* Altura dinámica del banner inferior (instalación) para desplazar la UI */
  --banner-offset: 0px;
  /* Ajuste adicional solo en iOS instalado para despejar la barra del sistema */
  --ios-bottom-bump: 0px;
  /* Superficies ligeramente más oscuras para diferenciar bloques (Guardados) */
  --surface: #eaf1e8;        /* ~5-7% más oscuro que --bg */
  --surface-item: #e4ede1;   /* un paso más oscuro para filas internas */
  /* Reservas para centrar overlays entre header y bottom sheet sin reflow */
  --geo-safe-top: calc(var(--hdr-top, 0px) + var(--hdr-h, 56px) + var(--offline-h, 0px));
  --geo-safe-bottom: calc(var(--banner-offset, 0px) + var(--collapsed-min, 128px));
}

.hidden {
  display: none !important;
}

@media (prefers-color-scheme: dark) {
  :root {
    /* Modo oscuro Waze: fondo gris oscuro, texto gris claro y acento azul cielo */
    --bg: #3c444c;
    --text: #bbb8b2;
    --accent: #5487A4;
    --surface: #2f363d;       /* ligeramente más oscuro que el fondo */
    --surface-item: #293038;   /* items un poco más oscuros aún */
  }
}

[data-theme="light"] {
  --bg: #ffffff;
  --text: #1a1a1a;
  --surface: #f3f3f3;       /* card más oscuro que el fondo blanco */
  --surface-item: #ededed;   
}

[data-theme="dark"] {
  --bg: #121212;
  --text: #eaeaea;
  --surface: #0f0f0f;       
  --surface-item: #0c0c0c;
}

html, body {
  margin: 0;
  padding: 0;
  font-family: system-ui, -apple-system, Segoe UI, Roboto, Ubuntu, Cantarell, Noto Sans, Helvetica Neue, Arial, sans-serif;
  background-color: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

/* Critical header styles */
header {
  position: fixed;
  top: var(--hdr-top, 0px);
  left: 0;
  right: 0;
  z-index: 600;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 56px;
  padding: 0 12px;
  box-sizing: border-box;
  background: var(--bg);
  border-bottom: 1px solid rgba(0,0,0,0.06);
  box-shadow: 0 2px 8px rgba(0,0,0,0.06);
}

html[data-theme="dark"] header { 
  border-bottom-color: rgba(255,255,255,0.12); 
  box-shadow: 0 2px 8px rgba(0,0,0,0.35); 
}

header .app-title { 
  display:flex; 
  align-items: center; 
  gap: 10px; 
  margin: 0; 
  font-size: 1.05rem; 
  line-height: 1; 
  white-space: nowrap; 
  flex: 1 1 auto; 
  min-width: 0; 
  overflow: hidden; 
}

header .app-title .app-logo { 
  width: 25px; 
  height: 25px; 
  display: inline-block; 
  flex: 0 0 auto; 
  vertical-align: middle; 
  image-rendering: -webkit-optimize-contrast; 
  aspect-ratio: 1 / 1;
}

#app-title-name { 
  cursor: pointer; 
  -webkit-tap-highlight-color: transparent; 
  user-select: none; 
  -webkit-user-select: none; 
  -ms-user-select: none; 
}

header .app-title .name { 
  font-weight: 800; 
  overflow: visible; 
  text-overflow: clip; 
  flex: 0 0 auto; 
}

header .app-title .sub { 
  flex: 1 1 auto; 
  min-width: 0; 
  overflow: hidden; 
  text-overflow: ellipsis; 
  font-weight: 400; 
  opacity: 0.85; 
}

header .actions { 
  display:flex; 
  align-items:center; 
  gap: 0px; 
  flex: 0 0 auto; 
}

#theme-toggle + #menu-button { 
  margin-left: -10px; 
}

header .icon-btn { 
  width: 56px; 
  height: 56px; 
  display:inline-flex; 
  align-items:center; 
  justify-content:center; 
  border-radius: 12px; 
  background: transparent; 
  border: none; 
  color: var(--text); 
  cursor: pointer; 
  -webkit-appearance: none; 
  appearance: none; 
}

header .icon-btn:hover { 
  background: transparent; 
}

header .icon-btn:active { 
  background: transparent; 
  box-shadow: none; 
  transform: none; 
}

header .icon { 
  width: 48px; 
  height: 48px; 
  display:block; 
  shape-rendering: geometricPrecision; 
}

header .icon-btn,
header .icon-btn:focus,
header .icon-btn:active {
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  outline: none;
  user-select: none;
}

/* Critical loading bar */
#global-loading-bar {
  position: fixed;
  left: 0; 
  right: 0;
  top: calc(var(--hdr-top, 0px) + var(--hdr-h, 56px) + var(--offline-h, 0px));
  height: 3px;
  z-index: 1200;
  pointer-events: none;
  opacity: 0;
  transition: opacity 150ms linear;
}

#global-loading-bar.active { 
  opacity: 1; 
}

#global-loading-bar .bar {
  position: absolute; 
  left: 0; 
  top: 0; 
  height: 100%; 
  width: 100%;
  transform-origin: left center;
  transform: scaleX(0);
  transition: transform 240ms ease;
  background: var(--accent, #5487A4);
  box-shadow: 0 0 6px rgba(0,0,0,0.15);
}

html[data-theme="dark"] #global-loading-bar .bar {
  box-shadow: 0 0 6px rgba(0,0,0,0.45);
}

/* Ensure the hero map placeholder is responsive before async styles load */
#map-canvas,
.map-canvas {
  position: fixed;
  inset: 0;
  overflow: hidden;
  z-index: 0;
  background: radial-gradient(circle at 30% 20%, rgba(255,255,255,0.35), transparent 40%),
              radial-gradient(circle at 70% 80%, rgba(0,0,0,0.15), transparent 40%),
              #d9e7ff;
}

#map-canvas > .map-placeholder {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: block;
}

html[data-theme="dark"] .map-canvas,
html[data-theme="dark"] #map-canvas {
  background: radial-gradient(circle at 30% 20%, rgba(255,255,255,0.08), transparent 40%),
              radial-gradient(circle at 70% 80%, rgba(255,255,255,0.06), transparent 40%),
              #0f1113;
}

/* Aplicar el mismo filtro que el mapa real al placeholder SVG en modo oscuro */
html[data-theme="dark"] #map-canvas > .map-placeholder {
  filter: invert(0.9) hue-rotate(180deg) saturate(1.15) contrast(0.95) brightness(1.15);
}

#map-canvas .leaflet-container {
  background: transparent !important;
}


.geo-fallback {
  position: fixed;
  inset: 0;
  z-index: 1;
  box-sizing: border-box;
  pointer-events: none;
  display: flex;
  align-items: center;
  justify-content: center;
  padding-top: var(--geo-safe-top, 56px);
  padding-bottom: var(--geo-safe-bottom, 128px);
  opacity: 1;
  transition: opacity 180ms ease;
}

.geo-fallback.hidden,
html.geo-hide #geo-fallback {
  opacity: 0;
}

.geo-fallback .inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  pointer-events: auto;
  max-width: min(88vw, 420px);
  max-height: calc(100vh - var(--geo-safe-top, 56px) - var(--geo-safe-bottom, 128px));
  text-align: center;
}

.geo-fallback.hidden .inner,
html.geo-hide #geo-fallback .inner {
  pointer-events: none;
}

#geo-fallback-btn {
  background: #9aa0a6;
  color: #ffffff;
  border: none;
  border-radius: 999px;
  padding: 0.75rem 1.2rem;
  font-size: 1rem;
  box-shadow: 0 6px 16px rgba(0,0,0,0.25);
  cursor: pointer;
  white-space: nowrap;
}

#geo-fallback-note {
  font-size: 0.9rem;
  opacity: 0.85;
  color: var(--text);
  text-align: center;
}

/* Critical bottom sheet layout to avoid initial CLS before async styles load */
.bottom-sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: var(--banner-offset, 0px);
  background: var(--bg);
  border-top-left-radius: 16px;
  border-top-right-radius: 16px;
  box-shadow: 0 -6px 24px rgba(0,0,0,0.25);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  z-index: 5;
  height: clamp(104px, var(--collapsed-min, 128px), 156px);
  transition: height 320ms cubic-bezier(0.22, 1, 0.36, 1), bottom 320ms cubic-bezier(0.22, 1, 0.36, 1);
}
.bottom-sheet[data-state="collapsed"] {
  height: clamp(104px, var(--collapsed-min, 128px), 156px);
}
.bottom-sheet[data-state="half"] {
  height: calc(56vh - var(--banner-offset, 0px));
}
.bottom-sheet[data-state="expanded"] {
  height: calc(84vh - var(--nav-reserve, 0px) - var(--banner-offset, 0px));
}

.sheet-handle {
  display: flex;
  align-items: center;
  justify-content: center;
  padding-top: 8px;
  padding-bottom: 4px;
  cursor: grab;
  touch-action: none;
  position: relative;
}
.sheet-handle::before {
  content: "";
  position: absolute;
  left: -12px;
  right: -12px;
  top: -16px;
  bottom: -6px;
}
.sheet-grabber {
  width: 44px;
  height: 5px;
  border-radius: 999px;
  background: rgba(0,0,0,0.25);
}
.sheet-fixed {
  background: var(--bg);
  padding: 6px 0 8px;
  border-bottom: 1px solid rgba(0,0,0,0.08);
  box-shadow: 0 4px 10px -10px rgba(0,0,0,0.12);
  z-index: 2;
}
.sheet-fixed-inner {
  max-width: 640px;
  margin: 0 auto;
  padding: 0 1rem;
  width: 100%;
  box-sizing: border-box;
}
.sheet-fixed, .sheet-fixed * {
  cursor: grab;
}
.sheet-content {
  flex: 1 1 auto;
  overflow-y: hidden;
  overflow-x: hidden;
  padding: 0 1rem 1rem;
  box-sizing: border-box;
  touch-action: none;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}
.sheet-content[data-scroll-lock="expanded"] {
  overflow-y: auto;
  touch-action: pan-y;
}
.sheet-content.ios-scroll-auto {
  -webkit-overflow-scrolling: auto;
}
.converter-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: nowrap;
}
#converter label {
  display: block;
  margin-bottom: 0.4rem;
  text-align: center;
  font-weight: 600;
}
.converter-row input[type='text'] {
  flex: 1 1 auto;
  min-width: 0;
  padding: 0.75rem;
  font-size: 1rem;
  border-radius: 4px;
  border: 1px solid rgba(0,0,0,0.12);
  background-color: var(--bg);
  color: var(--text);
  box-sizing: border-box;
}
.converter-row button {
  flex: 0 0 auto;
  min-width: 5.5rem;
  padding: 0.75rem 1rem;
  font-size: 1rem;
  border-radius: 4px;
  border: none;
  color: #fff;
  background-color: var(--accent);
  cursor: pointer;
  transition: background-color 160ms ease;
}
.converter-row button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}
#history-list {
  list-style: none;
  padding: 0;
  margin: 0;
  min-height: var(--history-min-h, 0);
  contain: layout style paint;
}
#history-list:not(:empty) {
  min-height: 64px;
}
#history section {
  contain: layout style paint;
}
.more-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 48px;
  cursor: pointer;
  border-top: 1px solid rgba(0,0,0,0.06);
  background: var(--bg);
}

/* Ensure the drawer menu starts off-canvas to prevent layout jump */
.more-menu {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: var(--drawer-width, 320px);
  transform: translateX(100%);
  opacity: 0;
  pointer-events: none;
  display: flex;
}