@layer base {
  *, *::before, *::after { box-sizing: border-box; }
  html { -webkit-text-size-adjust: 100%; }
  body {
    margin: 0;
    background: var(--muted);
    color: var(--foreground);
    font-family: var(--font-sans);
    font-size: 0.875rem;
    -webkit-font-smoothing: antialiased;
  }
  [hidden] { display: none !important; }
  h1, h2, h3 { text-wrap: balance; }
}

@layer app {
  :root {
    --pad: 1.5rem;
    --header-h: 4rem;
    --chart-h: 260px;
    --map-h: clamp(380px, 58vh, 560px);
    --sidebar-w: 236px;
  }
  html.side-collapsed { --sidebar-w: 72px; }

  /* Sidebar igual que la v1 */
  aside {
    position: fixed; inset: 0 auto 0 0; z-index: 40;
    display: flex; flex-direction: column; width: var(--sidebar-w);
    padding: 21px 13px calc(20px + env(safe-area-inset-bottom));
    background: #00457F; color: #fff; overflow-x: hidden; overflow-y: auto;
    transition: width .2s ease;
  }
  .side-brand { display: flex; align-items: flex-start; gap: 10px; padding: 0 9px 18px; font-size: 14px; font-weight: 720; }
  .side-brand-main { display: block; min-width: 0; }
  .side-mark { display: block; height: 46px; overflow: hidden; }
  .side-logo { display: block; height: 46px; width: auto; max-width: none; }
  .side-close { display: none; place-items: center; width: 30px; height: 30px; margin-left: auto; padding: 0; background: none; border: 1px solid rgba(255,255,255,.35); border-radius: 6px; color: #fff; }
  .side-label { margin: 16px 9px 7px; color: rgba(255,255,255,.62); font-size: 10px; font-weight: 750; letter-spacing: .08em; text-transform: uppercase; white-space: nowrap; }
  .side-item, .side-collapse {
    position: relative; display: flex; align-items: center; gap: 10px; width: 100%;
    padding: 8px 12px; background: none; border: 0; border-radius: 8px;
    color: rgba(255,255,255,.82); font-size: 13px; font-weight: 500; letter-spacing: -0.01em;
    text-align: left; text-decoration: none; cursor: pointer;
  }
  .side-item svg, .side-collapse svg { width: 18px; height: 18px; flex: none; }
  .side-item:hover, .side-collapse:hover { background: rgba(255,255,255,.12); color: #fff; }
  .side-item.active { background: rgba(255,255,255,.16); color: #fff; font-weight: 600; }
  .side-text { min-width: 0; }
  .side-collapse { margin-top: auto; }
  .side-foot { margin-top: 8px; padding: 15px 6px 0; border-top: 1px solid rgba(255,255,255,.28); color: #fff; font-size: 12.5px; line-height: 1.45; }
  .side-legal { display: block; margin-top: 8px; font-size: 11px; line-height: 1.45; }
  .side-terms {
    padding: 0; background: none; border: 0; color: #fff; font: inherit; font-size: 11px;
    text-decoration: underline; text-underline-offset: 2px; cursor: pointer;
  }
  .side-terms:hover { color: #fff; }

  .terms-dialog {
    width: min(42rem, calc(100vw - 2rem)); max-height: min(82dvh, 44rem);
    margin: auto; padding: 0; overflow: hidden;
    border: 1px solid var(--border); border-radius: var(--radius-lg);
    background: var(--background); color: var(--foreground); box-shadow: var(--shadow-lg);
  }
  .terms-dialog[open] { display: flex; flex-direction: column; }
  .terms-dialog::backdrop { background: rgba(9, 9, 11, .5); }
  .terms-dialog .card-header { position: relative; flex: none; z-index: 1; background: var(--background); padding: 1.25rem 1.25rem 0.75rem; }
  .terms-dialog .card-title { font-size: 1rem; padding-right: 2rem; }
  .terms-close {
    position: absolute; top: 0.85rem; right: 0.85rem;
    display: grid; place-items: center; width: 2rem; height: 2rem; padding: 0;
    border: 1px solid var(--border); border-radius: var(--radius-md);
    background: var(--background); color: var(--foreground); cursor: pointer;
  }
  .terms-close:hover { background: var(--accent); }
  .terms-body {
    overflow: auto; flex: 1 1 auto; min-height: 0; padding: 0 1.25rem 1.5rem;
    font-size: 0.8125rem; line-height: 1.6;
  }
  .terms-kicker { margin: 0 0 0.25rem; color: var(--muted-foreground); font-size: 0.75rem; font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase; }
  .terms-version { margin: 0 0 0.75rem; color: var(--muted-foreground); }
  .terms-body h3 { margin: 1.15rem 0 0.4rem; font-size: 0.8125rem; letter-spacing: 0.02em; }
  .terms-body p { margin: 0.45rem 0; }
  .terms-body ul { margin: 0.35rem 0 0.6rem; padding-left: 1.15rem; }
  .terms-body li + li { margin-top: 0.3rem; }
  .cc-badge { display: block; width: 9.75rem; height: auto; margin: 0.75rem 0 0.25rem; color: var(--foreground); }
  html.side-collapsed .side-text,
  html.side-collapsed .side-foot { display: none; }
  html.side-collapsed .side-label {
    height: 1px; margin: 12px 8px; padding: 0; overflow: hidden;
    background: rgba(255,255,255,.28); color: transparent;
  }
  html.side-collapsed .side-brand { justify-content: center; padding-inline: 0; }
  html.side-collapsed .side-mark { width: auto; height: auto; overflow: visible; }
  .side-logo-icon { display: none; height: 36px; width: auto; }
  html.side-collapsed .side-logo-full { display: none; }
  html.side-collapsed .side-logo-icon { display: block; }
  html.side-collapsed .side-item,
  html.side-collapsed .side-collapse { justify-content: center; padding-inline: 0; }
  .side-tip {
    position: fixed; z-index: 60; translate: 0 -50%;
    padding: 0.35rem 0.6rem; border-radius: 6px;
    background: #27272a; color: #fff; border: 1px solid #3f3f46;
    font-size: 12px; font-weight: 500; line-height: 1.2; white-space: nowrap;
    pointer-events: none; box-shadow: 0 6px 18px rgba(0,0,0,.28);
  }
  .backdrop { position: fixed; inset: 0; z-index: 35; background: rgba(9,9,11,.5); opacity: 0; visibility: hidden; transition: opacity .2s, visibility .2s; }
  .menu-btn { display: none; place-items: center; flex: none; width: 36px; height: 36px; padding: 0; background: #fff; border: 1px solid var(--border); border-radius: 6px; color: var(--foreground); }
  .menu-btn:hover { background: var(--accent); }

  .app { display: flex; flex-direction: column; height: 100dvh; max-height: 100dvh; margin-left: var(--sidebar-w); min-width: 0; overflow: hidden; container: main / inline-size; transition: margin-left .2s ease; }
  .app-header {
    position: relative; flex: none; z-index: 20;
    display: flex; align-items: center; gap: 0.75rem;
    height: calc(var(--header-h) + env(safe-area-inset-top));
    padding: 0 var(--pad);
    padding-top: env(safe-area-inset-top);
    background: color-mix(in oklch, var(--background) 92%, transparent);
    border-bottom: 1px solid var(--border);
    backdrop-filter: blur(10px);
  }
  .header-title { flex: 1; min-width: 0; }
  .header-title h1 { margin: 0; font-size: 1rem; font-weight: 600; letter-spacing: -0.01em; line-height: 1.3; }
  .header-title p {
    margin: 0.125rem 0 0; color: var(--muted-foreground); font-size: 0.75rem;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  }
  .header-right { display: flex; align-items: center; gap: 0.5rem; flex: none; }
  #fecha .fecha-short { display: none; }

  .filters-dock {
    position: sticky; top: 0; z-index: 15; flex: none; min-width: 0; width: auto;
    margin: 0 calc(var(--pad) * -1) -1rem; padding: 1rem var(--pad);
    background: var(--muted);
  }
  .page { flex: 1; min-height: 0; min-width: 0; overflow: auto; display: flex; flex-direction: column; gap: 1rem; padding: 0 var(--pad) 2rem; }
  .filters, .filters-body { min-width: 0; width: 100%; }

  /* Filtros: una sola barra, se pliega cuando no entra */
  .filters-toggle {
    display: none; width: 100%; align-items: center; gap: 0.5rem;
    padding: 0.875rem 1rem; border: 0; background: none; font: inherit; font-weight: 600; cursor: pointer;
    color: var(--foreground);
  }
  .filters-toggle .chev { margin-left: auto; width: 1rem; height: 1rem; color: var(--muted-foreground); transition: rotate 200ms; }
  .filters.open .filters-toggle .chev { rotate: 180deg; }
  .filters-body {
    display: flex; flex-wrap: wrap; align-items: end; gap: 0.875rem 1rem;
    padding: 1rem 1.25rem;
  }
  .filters-body > .field { flex: 1 1 0; min-width: 0; }
  .filters .label { font-size: 0.75rem; color: var(--muted-foreground); }
  .filter-actions { flex: none; display: flex; align-items: center; }
  .filter-actions .btn { height: 2.5rem; }
  .combo { position: relative; }
  .combo .input { padding-right: 2rem; }
  .combo-clear {
    position: absolute; top: 50%; right: 0.375rem; translate: 0 -50%;
    display: grid; place-items: center; width: 1.5rem; height: 1.5rem; padding: 0;
    border: 0; border-radius: var(--radius-sm); background: transparent; color: var(--muted-foreground); cursor: pointer;
    font-size: 1rem; line-height: 1;
  }
  .combo-clear:hover { background: var(--accent); color: var(--foreground); }

  .grid { display: grid; grid-template-columns: repeat(12, minmax(0, 1fr)); gap: 1rem; }
  .grid > * { min-width: 0; }
  .grid > .kpi { grid-column: span 3; }
  .span6 { grid-column: span 6; }
  .span12 { grid-column: span 12; }
  .card { box-shadow: var(--shadow-xs); }

  /* Mapa al 80% y los totales al 20%, en stock y en leche */
  .territory {
    grid-column: span 12;
    display: grid;
    grid-template-columns: minmax(0, 4fr) minmax(11.5rem, 1fr);
    gap: 1rem;
    align-items: stretch;
  }
  .territory:not(:has(.kpi-rail:not([hidden]))) { grid-template-columns: minmax(0, 1fr); }
  .map-card { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
  .map-card .card-content { flex: 1; display: flex; flex-direction: column; min-height: 0; padding: 0 1.25rem 1rem; }
  .territory #map { flex: 1; width: 100%; height: auto; min-height: var(--map-h); }

  .kpi-rail { display: flex; flex-direction: column; gap: 0.75rem; min-width: 0; min-height: 0; }
  .kpi-rail .kpi { flex: 1 1 0; display: flex; flex-direction: column; min-height: 0; }
  .kpi-rail .card-content { flex: 1; display: flex; align-items: center; padding: 0.875rem 1rem; }

  .kpi .card-content { position: relative; }
  .kpi .statistic { min-width: 0; width: 100%; }
  .kpi .statistic-title { padding-right: 2.25rem; }
  .kpi .statistic-value {
    font-size: clamp(1.05rem, 11cqi, 1.7rem);
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
    letter-spacing: -0.03em;
  }
  .kpi .statistic-description { display: block; margin-top: 0.25rem; line-height: 1.4; }
  .kpi-icon {
    position: absolute; top: 0.75rem; right: 0.75rem;
    display: grid; place-items: center; width: 1.75rem; height: 1.75rem;
    border-radius: var(--radius-md);
    background: color-mix(in oklch, var(--accent-c, var(--primary)) 14%, transparent);
    color: var(--accent-c, var(--primary));
  }
  .kpi-icon svg { width: 0.95rem; height: 0.95rem; }

  .card-header {
    flex-direction: row; align-items: baseline; justify-content: space-between; flex-wrap: wrap;
    gap: 0.25rem 0.75rem; padding: 1.25rem 1.25rem 0;
  }
  .card-title { font-size: 0.9375rem; }
  .card-description { font-size: 0.8125rem; }
  .card-content { padding: 1rem 1.25rem 1.25rem; }
  .card-footer { padding: 0 1.25rem 1rem; color: var(--muted-foreground); font-size: 0.75rem; line-height: 1.45; }
  .chart-box { position: relative; height: var(--chart-h); overflow: hidden; }
  .chart-box > canvas { display: block; width: 100%; height: 100%; }
  .chart-tools { margin-left: auto; display: flex; align-items: center; gap: 0.5rem; }
  #map .maplibregl-canvas { width: 100% !important; height: 100% !important; }
  .map-layers { position: relative; padding: 0; background: transparent; box-shadow: none; }
  .map-layers-btn {
    display: grid; place-items: center; width: 29px; height: 29px; padding: 0;
    border: 0; border-radius: 4px; background: #fff; color: #333;
    box-shadow: 0 0 0 2px rgba(0,0,0,.1); cursor: pointer;
  }
  .map-layers-btn svg { width: 18px; height: 18px; }
  .map-layers-btn:hover, .map-layers-btn[aria-expanded="true"] { background: #f4f4f5; }
  .map-layers-panel {
    position: absolute; top: 0; right: calc(100% + 8px); min-width: 12.5rem;
    padding: 0.45rem 0.65rem 0.5rem;
    background: color-mix(in oklch, var(--popover) 96%, transparent);
    border-radius: var(--radius-md); box-shadow: var(--shadow-md);
    color: var(--foreground); font: 0.75rem/1.35 var(--font-sans);
  }
  .map-layers-title {
    margin: 0 0 0.2rem; color: var(--muted-foreground); font-weight: 600;
    font-size: 0.6875rem; letter-spacing: 0.05em; text-transform: uppercase;
  }
  .map-layers label { display: flex; align-items: center; gap: 0.4rem; padding: 0.12rem 0; cursor: pointer; white-space: nowrap; }
  .map-layers input { margin: 0; accent-color: #00457F; }

  .statistic-trend { font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
  .statistic-trend[data-trend="up"] { color: #16a34a; }
  .statistic-trend[data-trend="down"] { color: #dc2626; }
  .statistic-trend[data-trend="flat"] { color: var(--muted-foreground); }

  .map-tip .maplibregl-popup-content {
    padding: 0.5rem 0.75rem; border: 1px solid var(--border); border-radius: var(--radius-md);
    background: var(--popover); color: var(--popover-foreground); box-shadow: var(--shadow-md);
    font: 0.8125rem/1.4 var(--font-sans);
  }
  .map-tip .maplibregl-popup-tip { display: none; }
  .map-tip strong { display: block; font-weight: 600; }
  .map-tip span { color: var(--muted-foreground); }
  #map { position: relative; border-radius: var(--radius-lg); overflow: hidden; border: 1px solid var(--border); }
  .map-legends {
    position: absolute; left: 0.75rem; bottom: 0.75rem; z-index: 5;
    display: flex; flex-direction: column;
    max-width: min(20rem, 78%); max-height: calc(100% - 1.5rem);
    background: color-mix(in oklch, var(--popover) 96%, transparent);
    border: 1px solid var(--border); border-radius: var(--radius-md); box-shadow: var(--shadow-md);
    font-size: 0.75rem; line-height: 1.6; overflow: hidden;
  }
  .map-legends:not(:has(.map-legend:not([hidden]))) { display: none; }
  .map-legends-toggle {
    display: flex; align-items: center; gap: 0.35rem; flex: none;
    padding: 0.4rem 0.7rem; border: 0; background: transparent;
    color: var(--muted-foreground); font: inherit; font-size: 0.6875rem;
    font-weight: 600; letter-spacing: 0.05em; text-transform: uppercase;
    text-align: left; cursor: pointer;
  }
  .map-legends-toggle:hover { background: color-mix(in oklch, var(--accent) 55%, transparent); }
  .map-legends-toggle svg { width: 0.9rem; height: 0.9rem; margin-left: auto; transition: transform 0.15s ease; }
  .map-legends[data-collapsed="true"] .map-legends-toggle svg { transform: rotate(-90deg); }
  .map-legends-body { min-height: 0; overflow-y: auto; padding: 0 0.75rem 0.55rem; }
  .map-legends[data-collapsed="true"] .map-legends-body { display: none; }
  .map-legend + .map-legend { margin-top: 0.375rem; padding-top: 0.375rem; border-top: 1px solid var(--border); }
  .map-legend .lg-title { color: var(--muted-foreground); font-weight: 600; font-size: 0.6875rem; text-transform: uppercase; letter-spacing: 0.05em; }
  .map-legend .sw { display: inline-block; width: 0.875rem; height: 0.625rem; margin-right: 0.375rem; border-radius: 2px; border: 1px solid oklch(0 0 0 / 0.12); vertical-align: middle; }
  .map-legend .lg-row { display: flex; align-items: center; gap: 0.4rem; }
  .map-legend .lg-marker { flex: none; width: 1rem; height: 1rem; }

  .empty {
    display: grid; place-items: center; min-height: 8rem; padding: 1rem;
    border: 1px dashed var(--border); border-radius: var(--radius-lg);
    color: var(--muted-foreground); text-align: center;
  }
  .g3-wrap { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.2fr); gap: 1.25rem; align-items: center; }
  .g3-wrap .table-cell:nth-child(n+2), .g3-wrap .table-head:nth-child(n+2) { text-align: right; font-variant-numeric: tabular-nums; }
  .dot { display: inline-block; width: 0.625rem; height: 0.625rem; margin-right: 0.5rem; border-radius: 2px; vertical-align: middle; }
  .tipo-link, .tick-link { color: inherit; text-decoration: none; cursor: default; }
  .tipo-link:hover, .tick-link:hover { color: #2563eb; }
  .tick-links { position: absolute; inset: 0; pointer-events: none; z-index: 2; }
  .tick-link {
    position: absolute; transform: translateY(-50%);
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
    font-size: 10px; line-height: 1.15; pointer-events: auto;
  }
  #st_g3_table tr { cursor: pointer; }
  #st_g3_table tr[data-selected] { background: var(--accent); }

  .app-footer {
    margin: auto calc(var(--pad) * -1) -2rem; padding: 1rem var(--pad) calc(1rem + env(safe-area-inset-bottom));
    border-top: 1px solid var(--border); background: var(--background);
    color: var(--muted-foreground); font-size: 0.75rem; text-align: center;
  }

  #login { position: fixed; inset: 0; z-index: 100; display: grid; place-items: center; padding: 1.25rem; background: var(--muted); }
  #login img { display: block; height: 2rem; width: auto; }
  #login .card { width: 100%; max-width: 22rem; }
  #login .input { margin-bottom: 0.75rem; }
  #login-err { min-height: 1.25rem; margin-top: 0.5rem; color: var(--destructive); font-size: 0.8125rem; }

  /* El ancho útil es el del contenido: colapsar el menú vuelve a abrir el mapa al lado */
  @container main (max-width: 1040px) {
    .grid > .kpi { grid-column: span 6; }
  }
  @container main (max-width: 920px) {
    .territory { grid-template-columns: minmax(0, 1fr); }
    .kpi-rail { order: -1; flex-direction: row; flex-wrap: wrap; }
    .kpi-rail .kpi { flex: 1 1 12rem; }
    .territory #map { min-height: var(--map-h); }
  }
  @container main (max-width: 720px) {
    .span6 { grid-column: span 12; }
    .g3-wrap { grid-template-columns: minmax(0, 1fr); }
    .grid > .kpi { grid-column: span 6; }
  }
  @container main (max-width: 460px) {
    .grid > .kpi, .kpi-rail .kpi { flex-basis: 100%; }
    .grid > .kpi { grid-column: span 12; }
    .kpi-icon { display: none; }
    .kpi .statistic-title { padding-right: 0; }
    .kpi .statistic-value { font-size: 1.45rem; }
  }

  @media (max-width: 1050px) {
    .app { margin-left: 0; }
    aside { width: min(280px, 84vw); transform: translateX(-100%); transition: transform .22s ease; }
    body.nav-open aside { transform: none; box-shadow: 0 0 40px rgba(0,0,0,.35); }
    body.nav-open .backdrop { opacity: 1; visibility: visible; }
    .menu-btn, .side-close { display: grid; }
    .side-collapse { display: none; }
    html.side-collapsed .side-text,
    html.side-collapsed .side-foot { display: revert; }
    html.side-collapsed .side-label { height: auto; margin: 16px 9px 7px; padding: 0; overflow: visible; background: none; color: rgba(255,255,255,.62); }
    html.side-collapsed .side-brand { justify-content: flex-start; padding-inline: 9px; }
    html.side-collapsed .side-mark { width: auto; height: 46px; overflow: hidden; }
    html.side-collapsed .side-logo-full { display: block; }
    html.side-collapsed .side-logo-icon { display: none; }
    html.side-collapsed .side-item { justify-content: flex-start; padding: 8px 12px; }
  }
  @media (max-width: 767px) {
    :root { --pad: 1rem; }
    .filters-toggle { display: flex; }
    .filters-body { display: none; border-top: 1px solid var(--border); }
    .filters.open .filters-body:not([hidden]) { display: flex; }
    .filter-actions { width: 100%; justify-content: flex-end; }
  }
  @media (max-width: 520px) {
    :root { --chart-h: 220px; --map-h: clamp(280px, 52vh, 400px); }
    #fecha { font-size: 0.6875rem; white-space: nowrap; }
    #fecha .fecha-full { display: none; }
    #fecha .fecha-short { display: inline; }
    .map-legends { left: 0.5rem; bottom: 0.5rem; max-width: min(17rem, 82%); font-size: 0.75rem; line-height: 1.6; }
    .map-legends-body { padding: 0 0.6rem 0.45rem; }
    .filters-body > .field { flex-basis: 100%; }
    .page { padding-top: 0.875rem; gap: 0.75rem; }
    .grid { gap: 0.75rem; }
  }
  @media (prefers-reduced-motion: reduce) {
    aside, .app, .backdrop, .filters-toggle .chev { transition: none; }
  }
}
