:root {
  --bg: #071018;
  --panel: rgba(8, 16, 24, 0.94);
  --line: rgba(255, 255, 255, 0.08);
  --text: #e8eef4;
  --muted: #8aa0b3;
  --teal: #3ee0c3;
  --gold: #e4c36a;
  --rose: #ff6b73;
  --ok: #3ee0c3;
  --warn: #e4c36a;
  --bad: #ff6b73;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.45);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --dock-h: calc(56px + var(--safe-bottom));
  font-family: ui-sans-serif, system-ui, "Segoe UI", Ubuntu, sans-serif;
}

* { box-sizing: border-box; }
html, body {
  height: 100%;
  height: 100dvh;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  overflow-x: hidden;
  width: 100%;
  max-width: 100%;
  -webkit-text-size-adjust: 100%;
}
button, input, select {
  font: inherit;
  color: inherit;
  font-size: 16px;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
.hidden { display: none !important; }

.gate {
  min-height: 100%;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: calc(1rem + var(--safe-top)) 1rem calc(1rem + var(--safe-bottom));
  overflow: auto;
  background:
    radial-gradient(1200px 500px at 10% 0%, rgba(62, 224, 195, 0.12), transparent 50%),
    radial-gradient(900px 400px at 90% 100%, rgba(228, 195, 106, 0.08), transparent 45%),
    var(--bg);
}
.gate-card {
  width: min(440px, 100%);
  padding: 1.4rem;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: var(--shadow);
}
.eyebrow { letter-spacing: 0.16em; text-transform: uppercase; font-size: 0.7rem; color: var(--teal); margin: 0 0 0.6rem; }
.gate-card h1 { margin: 0 0 0.6rem; font-weight: 560; letter-spacing: -0.03em; font-size: 1.45rem; }
.lede, .muted, .hint { color: var(--muted); }
.gate-card label, .sheet-search {
  display: block;
  font-size: 0.8rem;
  color: var(--muted);
  margin: 1rem 0 0.4rem;
}
.gate-card input, .top-actions input, .panel select, .sheet-search input {
  width: 100%;
  background: rgba(255,255,255,0.04);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.75rem 0.8rem;
  outline: none;
}
.otp-actions { display: grid; gap: 0.55rem; margin-top: 1rem; }
#otpCode { letter-spacing: 0.45em; text-align: center; font-size: 1.35rem; }
.btn {
  background: rgba(255,255,255,0.06);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0.8rem 0.9rem;
  min-height: 44px;
  cursor: pointer;
  width: 100%;
}
.btn.primary { background: var(--teal); color: #06211c; border-color: transparent; font-weight: 600; }
.btn.ghost { background: transparent; width: auto; }
.btn:hover { filter: brightness(1.08); }

.app {
  height: 100%;
  height: 100dvh;
  width: 100%;
  max-width: 100%;
  overflow: hidden;
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: auto 1fr var(--dock-h);
  grid-template-areas:
    "top"
    "map"
    "dock";
}
.top {
  grid-area: top;
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  padding: calc(0.45rem + var(--safe-top)) 0.7rem 0.5rem;
  border-bottom: 1px solid var(--line);
  background: rgba(7, 16, 24, 0.94);
  z-index: 500;
  min-width: 0;
  max-width: 100%;
  overflow-x: hidden;
}
.brand { display: flex; gap: 0.7rem; align-items: center; }
.brand .dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--teal); box-shadow: 0 0 12px var(--teal); flex: none;
}
.brand small { display: block; color: var(--muted); font-size: 0.72rem; }
.kpis {
  display: flex;
  gap: 0.45rem;
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
  padding-bottom: 0.15rem;
  scrollbar-width: none;
  min-width: 0;
  max-width: 100%;
  width: 100%;
  overscroll-behavior-x: contain;
}
.kpis::-webkit-scrollbar { display: none; }
.kpi {
  min-width: 108px;
  flex: none;
  padding: 0.35rem 0.65rem;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(255,255,255,0.03);
}
.kpi b { display: block; font-size: 0.95rem; letter-spacing: -0.03em; }
.kpi span { color: var(--muted); font-size: 0.62rem; text-transform: uppercase; letter-spacing: 0.08em; }
.desktop-only { display: none; }
.top-actions { gap: 0.4rem; align-items: center; }
.top-actions input { width: 220px; }

.panel {
  position: fixed;
  left: 0;
  right: 0;
  bottom: var(--dock-h);
  top: auto;
  max-height: min(68dvh, 520px);
  background: var(--panel);
  border-top: 1px solid var(--line);
  border-radius: 16px 16px 0 0;
  padding: 0.9rem 0.9rem 1.2rem;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  z-index: 600;
  transform: translateY(110%);
  transition: transform 0.22s ease;
  box-shadow: var(--shadow);
}
.panel.open { transform: translateY(0); }
.panel h2 {
  margin: 1rem 0 0.45rem;
  font-size: 0.68rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
  font-weight: 600;
}
.panel h2:first-child { margin-top: 0; }
.panel label { display: block; font-size: 0.78rem; color: var(--muted); margin: 0.55rem 0; }
.chk { display: flex; gap: 0.5rem; align-items: center; min-height: 44px; }
.hint { font-size: 0.75rem; line-height: 1.4; }

.presets { display: grid; gap: 0.4rem; }
.preset {
  text-align: left;
  border: 1px solid var(--line);
  background: rgba(255,255,255,0.03);
  border-radius: 12px;
  padding: 0.7rem 0.8rem;
  min-height: 44px;
  cursor: pointer;
  width: 100%;
}
.preset.on { border-color: var(--teal); box-shadow: inset 0 0 0 1px var(--teal); }
.preset b { display: block; font-size: 0.85rem; }
.preset span { color: var(--muted); font-size: 0.72rem; }

.seg { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 0.25rem; }
.seg button {
  font-size: 0.72rem;
  padding: 0.55rem 0.2rem;
  min-height: 44px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: transparent;
  cursor: pointer;
}
.seg button.on { background: var(--teal); color: #06211c; border-color: transparent; }

.legend {
  margin: 0 0 0.7rem;
  padding: 0.55rem 0.65rem;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(255,255,255,0.03);
}
.legend-bar {
  height: 8px;
  border-radius: 99px;
}
.legend-labels {
  display: flex;
  justify-content: space-between;
  margin-top: 0.3rem;
  font-size: 0.68rem;
  color: var(--muted);
}
.legend-cap {
  margin: 0.35rem 0 0;
  font-size: 0.72rem;
  color: var(--muted);
  line-height: 1.35;
}

.map-stack {
  grid-area: map;
  position: relative;
  min-height: 0;
  overflow: hidden;
  width: 100%;
}
.map { height: 100%; min-height: 0; overflow: hidden; width: 100%; }
.map-legend {
  position: absolute;
  left: 8px;
  bottom: 8px;
  z-index: 1100;
  max-width: min(280px, calc(100% - 56px));
  padding: 0.45rem 0.55rem;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(7, 16, 24, 0.88);
  box-shadow: var(--shadow);
  pointer-events: none;
}
.map-legend .legend-bar { height: 7px; }
.map-legend .legend-cap { font-size: 0.68rem; }
.attr { display: none; }

.dock {
  grid-area: dock;
  display: flex;
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
  overflow: hidden;
  padding: 0 0 var(--safe-bottom);
  background: rgba(7, 16, 24, 0.96);
  border-top: 1px solid var(--line);
  z-index: 800;
}
.dock button {
  flex: 1 1 0;
  min-width: 0;
  max-width: none;
  border: 0;
  background: transparent;
  color: var(--muted);
  min-height: 52px;
  font-size: 0.72rem;
  font-weight: 600;
  padding: 0 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.dock button.on { color: var(--teal); }

.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: var(--dock-h);
  background: var(--panel);
  border-top: 1px solid var(--line);
  border-radius: 16px 16px 0 0;
  padding: 0.9rem 0.9rem calc(0.9rem + var(--safe-bottom));
  z-index: 650;
  box-shadow: var(--shadow);
}
.sheet-row { display: grid; grid-template-columns: 1fr 1fr; gap: 0.5rem; margin-top: 0.8rem; }

.widget { margin-bottom: 0.9rem; }
.widget h3 { margin: 0 0 0.35rem; font-size: 0.78rem; }
.chips { display: flex; flex-wrap: wrap; gap: 0.35rem; }
.chip {
  border: 1px solid var(--line);
  background: transparent;
  border-radius: 999px;
  padding: 0.45rem 0.7rem;
  min-height: 40px;
  font-size: 0.78rem;
  cursor: pointer;
}
.chip.on { background: var(--teal); color: #06211c; border-color: transparent; }

.rank { font-size: 0.78rem; margin-bottom: 0.8rem; }
.rank-row {
  display: grid;
  grid-template-columns: 1.4rem 1fr auto;
  gap: 0.35rem;
  padding: 0.4rem 0;
  border-bottom: 1px solid var(--line);
  align-items: center;
}
.rank-name { display: flex; flex-direction: column; min-width: 0; }
.rank-name b { font-size: 0.8rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rank-name span, .rank-score { color: var(--muted); font-size: 0.7rem; }
.rank-score { font-variant-numeric: tabular-nums; }

.drawer {
  position: fixed;
  left: 0;
  right: 0;
  bottom: var(--dock-h);
  top: auto;
  width: 100%;
  max-height: min(78dvh, 640px);
  background: rgba(8, 16, 24, 0.96);
  border-top: 1px solid var(--line);
  border-radius: 16px 16px 0 0;
  z-index: 700;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  padding: 0.8rem 0.9rem 1.4rem;
  box-shadow: var(--shadow);
}
.drawer-close {
  display: block;
  width: 100%;
  min-height: 44px;
  margin: 0 0 0.8rem;
  background: rgba(255,255,255,0.06);
  border: 1px solid var(--line);
  border-radius: 12px;
  color: var(--text);
  cursor: pointer;
}
.badge {
  display: inline-block;
  padding: 0.2rem 0.55rem;
  border-radius: 99px;
  font-size: 0.72rem;
  font-weight: 650;
}
.badge.excelente { background: rgba(62,224,195,0.15); color: var(--ok); }
.badge.regular { background: rgba(228,195,106,0.15); color: var(--warn); }
.badge.mal { background: rgba(255,107,115,0.15); color: var(--bad); }
.ano-wrap {
  font-size: 0.7rem;
  color: var(--muted);
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}
.ano-wrap select, #anoInternoM {
  background: #12202c;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.55rem 0.65rem;
  color: #e8eef4;
  -webkit-text-fill-color: #e8eef4;
  color-scheme: dark;
  opacity: 1;
  min-width: 200px;
  min-height: 44px;
}
.ano-wrap select option, #anoInternoM option {
  color: #e8eef4;
  background: #12202c;
}
.search-hits {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  margin: 0.6rem 0 0.8rem;
  max-height: 40dvh;
  overflow: auto;
}
.search-hit {
  text-align: left;
  width: 100%;
  min-height: 44px;
  padding: 0.65rem 0.8rem;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: rgba(255,255,255,0.04);
  color: #e8eef4;
}
.badge.cerrada { background: rgba(62,224,195,0.15); color: var(--ok); }
.badge.en_curso { background: rgba(228,195,106,0.15); color: var(--warn); }
.badge.sin_asentar { background: rgba(109,123,136,0.2); color: #c5d0da; }
.statgrid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.55rem;
  margin: 0.9rem 0;
}
.stat {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0.55rem 0.65rem;
}
.stat b { display: block; font-size: 1.05rem; }
.stat span { color: var(--muted); font-size: 0.68rem; }

.leaflet-container {
  background: #071018;
  font: inherit;
  width: 100% !important;
  max-width: 100%;
}
.leaflet-control-attribution { display: none; }
.leaflet-top.leaflet-right { display: none; }
.leaflet-bottom { bottom: 8px; }
.leaflet-control-zoom a {
  width: 36px !important;
  height: 36px !important;
  line-height: 36px !important;
}

@media (min-width: 960px) {
  .app {
    grid-template-columns: 280px 1fr 260px;
    grid-template-rows: 72px 1fr 28px;
    grid-template-areas:
      "top top top"
      "left map right"
      "attr attr attr";
  }
  .top {
    flex-direction: row;
    align-items: center;
    padding: 0 1rem;
    gap: 1rem;
  }
  .desktop-only { display: flex; }
  .map-legend { display: none; }
  .dock, .sheet { display: none !important; }
  .panel {
    position: relative;
    left: auto;
    right: auto;
    bottom: auto;
    top: auto;
    max-height: none;
    transform: none;
    border-radius: 0;
    box-shadow: none;
    z-index: 400;
  }
  .panel.left { grid-area: left; border-right: 1px solid var(--line); border-top: 0; }
  .panel.right { grid-area: right; border-left: 1px solid var(--line); border-top: 0; }
  .btn.ghost { width: auto; min-width: 44px; }
  .top-actions .btn { width: auto; }
  .attr {
    display: flex;
    grid-area: attr;
    font-size: 0.68rem;
    color: var(--muted);
    padding: 0 0.8rem;
    align-items: center;
    border-top: 1px solid var(--line);
  }
  .drawer {
    top: 72px;
    right: 260px;
    left: auto;
    bottom: 28px;
    width: min(380px, 42vw);
    max-height: none;
    border-radius: 0;
    border-top: 0;
    border-left: 1px solid var(--line);
  }
  .otp-actions { grid-template-columns: 1fr 1fr; }
}
