:root {
  --bg: #f6f8fc;
  --bg-grad: radial-gradient(1200px 600px at 100% -10%, #eaf0fb 0%, rgba(234,240,251,0) 60%),
             radial-gradient(900px 500px at -10% 0%, #eef2fb 0%, rgba(238,242,251,0) 55%);
  --card: #ffffff;
  --ink: #0f172a;
  --muted: #64748b;
  --line: #e7ebf2;
  --brand: #2563eb;
  --brand-2: #4f46e5;
  --brand-ink: #1d4ed8;
  --up: #16a34a;
  --up-bg: #ecfdf3;
  --up-line: #bbf7d0;
  --down: #dc2626;
  --down-bg: #fef2f2;
  --down-line: #fecaca;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(15,23,42,.04), 0 6px 20px rgba(15,23,42,.05);
  --shadow-lg: 0 8px 30px rgba(15,23,42,.09);
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: "Inter", ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  background-image: var(--bg-grad);
  background-attachment: fixed;
  color: var(--ink);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* Topbar */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.7rem 1.4rem;
  background: rgba(255,255,255,.8);
  backdrop-filter: saturate(180%) blur(10px);
  border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 50;
}
.topbar .brand { font-weight: 800; font-size: 1.02rem; letter-spacing: -.01em;
                 display: inline-flex; align-items: center; gap: .55rem; }
.topbar .logo { flex-shrink: 0; }
.topbar nav { display: flex; align-items: center; gap: .6rem; }
.topbar .user { color: var(--muted); font-size: 0.85rem; padding: 0 .4rem;
                border-left: 1px solid var(--line); }
.container { max-width: 1180px; margin: 1.6rem auto; padding: 0 1.2rem; }

/* Layout con panel lateral de informes */
.layout { display: flex; gap: 1.3rem; align-items: flex-start; }
.main-col { flex: 1; min-width: 0; }
.reports-nav { width: 220px; flex-shrink: 0; position: sticky; top: 5rem; }
.reports-nav h3 { margin: 0 0 .7rem; font-size: .72rem; text-transform: uppercase;
                  letter-spacing: .08em; color: var(--muted); }
.rep-link { display: flex; align-items: center; padding: .6rem .7rem; border-radius: 10px;
            color: var(--ink); text-decoration: none; font-size: .92rem; font-weight: 500;
            transition: background .15s, color .15s; }
.rep-link:hover { background: #eef2fb; }
.rep-link.active { background: linear-gradient(135deg, var(--brand), var(--brand-2));
                   color: #fff; font-weight: 600; box-shadow: 0 4px 12px rgba(37,99,235,.25); }

/* Cards */
.card {
  background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 1.15rem 1.35rem; margin-bottom: 1.2rem;
  box-shadow: var(--shadow);
}
h1 { font-size: 1.55rem; font-weight: 800; letter-spacing: -.02em; margin: 0 0 .25rem; }
h2 { font-size: 1.02rem; font-weight: 700; letter-spacing: -.01em; margin: 0 0 .8rem;
     display: flex; align-items: center; gap: .4rem; }
.muted { color: var(--muted); }
.small { font-size: .78rem; }
.nowrap { white-space: nowrap; }

/* Botones */
.btn {
  background: linear-gradient(135deg, var(--brand), var(--brand-2)); color: #fff; border: 0;
  border-radius: 10px; padding: .58rem 1.15rem; font-size: .92rem; font-weight: 600;
  cursor: pointer; box-shadow: 0 2px 8px rgba(37,99,235,.25);
  transition: transform .12s, box-shadow .12s, opacity .12s;
}
.btn:hover { transform: translateY(-1px); box-shadow: 0 6px 16px rgba(37,99,235,.32); }
.btn:active { transform: translateY(0); }
.btn:disabled { opacity: .55; cursor: default; transform: none; box-shadow: none; }
.btn-ghost {
  color: var(--ink); text-decoration: none; font-size: .88rem; font-weight: 500;
  background: #fff; border: 1px solid var(--line); padding: .5rem .85rem; border-radius: 10px;
  transition: border-color .15s, background .15s;
}
.btn-ghost:hover { background: #f8fafc; border-color: #cbd5e1; }

/* Login */
.login-wrap { min-height: 82vh; display: grid; place-items: center; }
.login { width: min(380px, 92vw); box-shadow: var(--shadow-lg); }
.login label { display: block; margin: .8rem 0; font-size: .85rem; color: var(--muted); font-weight: 500; }
.login input, .filters input, .filters select {
  width: 100%; margin-top: .35rem; padding: .6rem .7rem;
  border: 1px solid var(--line); border-radius: 10px; font-size: 1rem;
  background: #fff; color: var(--ink); transition: border-color .15s, box-shadow .15s;
}
.login input:focus, .filters input:focus, .filters select:focus {
  outline: 0; border-color: var(--brand); box-shadow: 0 0 0 3px rgba(37,99,235,.14);
}
.login .btn { width: 100%; margin-top: .6rem; }
.alert { background: var(--down-bg); color: var(--down); padding: .65rem .85rem;
         border-radius: 10px; font-size: .9rem; border: 1px solid var(--down-line); }

/* Filtros */
.filters { display: flex; flex-wrap: wrap; gap: 1rem; align-items: end; }
.filters label { font-size: .74rem; text-transform: uppercase; letter-spacing: .04em;
                 color: var(--muted); font-weight: 600; }
.filters input, .filters select { width: auto; min-width: 150px; margin-top: .3rem; }
.filters .btn { height: 42px; }

/* KPIs */
.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 1rem; margin-bottom: 1.2rem; }
.kpis3 { grid-template-columns: repeat(3, 1fr); }
.kpi { display: flex; flex-direction: column; gap: .3rem; margin-bottom: 0; }
.kpi-val { font-size: 1.9rem; font-weight: 800; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
/* Tarjetas de Evolución: rótulo arriba, dato grande y proyección debajo. */
.kpis3 .kpi { gap: .1rem; }
.kpis3 .kpi-val { font-size: 2.2rem; line-height: 1.15; margin-bottom: .55rem; }
.kpi-val.up { color: var(--up); }
.kpi-val.down { color: var(--down); }
/* Rótulo de la tarjeta ("MES EN CURSO", "VARIACIÓN"...) y su rango de fechas. */
.kpi-head { color: var(--muted); font-size: .74rem; font-weight: 700;
            text-transform: uppercase; letter-spacing: .06em; }
.kpi-range { color: var(--muted); font-size: .72rem; margin-bottom: .35rem; }
.kpi-unit { font-size: .95rem; font-weight: 600; color: var(--muted); letter-spacing: 0; }
/* Proyección (y ritmo del periodo anterior): pastilla al pie de la tarjeta, un
   escalón por debajo del dato grande. Verde si cierra por encima, rojo si por
   debajo. margin-top:auto la pega abajo para que las tres queden alineadas. */
.kpi-pill { align-self: flex-start; margin-top: auto; padding: .3rem .65rem;
            border-radius: 999px; font-size: .8rem; font-weight: 700;
            background: #f1f5f9; color: var(--muted); border: 1px solid var(--line); }
.kpi-pill.up { background: var(--up-bg); color: var(--up); border-color: var(--up-line); }
.kpi-pill.down { background: var(--down-bg); color: var(--down); border-color: var(--down-line); }
/* Dentro de la tarjeta ya teñida, la pastilla va en blanco para no fundirse. */
.kpi.kpi-up .kpi-pill, .kpi.kpi-down .kpi-pill { background: #fff; }
.kpi-lbl { color: var(--muted); font-size: .8rem; }
.kpi.kpi-up { background: var(--up-bg); border-color: var(--up-line); }
.kpi.kpi-down { background: var(--down-bg); border-color: var(--down-line); }

/* Botones de periodo */
.period-toggle { display: flex; gap: .5rem; margin-left: auto; align-items: end; flex-wrap: wrap; }
.period-toggle a { text-decoration: none; font-size: .85rem; }

/* Barras horizontales (marketplace) */
table.bars td { border: 0; padding: .38rem .4rem; }
.bar-lbl { white-space: nowrap; font-size: .9rem; }
.bar-cell { width: 100%; }
.bar { display: inline-block; height: 15px; border-radius: 6px;
       background: linear-gradient(90deg,#60a5fa,#2563eb); min-width: 3px; }
.mk-row td { cursor: pointer; }
.mk-row .bar-lbl a { color: var(--ink); text-decoration: none; }
.mk-row:hover td { background: #f8fafc; }
.mk-row.active td { background: #eef4ff; }
.mk-row.active .bar-lbl a { color: var(--brand-ink); font-weight: 700; }

/* Nombre de producto truncado con tooltip */
.prod-name { max-width: 220px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.proj { color: var(--brand-ink); font-weight: 600; font-size: .72rem; }

/* Badge de variación (verde/rojo) */
.delta { display: inline-block; min-width: 40px; text-align: center;
         padding: .12rem .45rem; border-radius: 999px; font-size: .78rem; font-weight: 700;
         font-variant-numeric: tabular-nums; }
.delta.up { background: var(--up-bg); color: var(--up); }
.delta.down { background: var(--down-bg); color: var(--down); }
.delta.zero { background: #f1f5f9; color: #94a3b8; font-weight: 500; }

/* Encabezado con explicación al pasar el cursor */
th.hint { cursor: help; text-decoration: underline dotted; text-underline-offset: 3px; }

/* Tablas */
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 1.2rem; }
.grid2 > .card { margin-bottom: 0; }
.grid2 { margin-bottom: 1.2rem; }
table { width: 100%; border-collapse: collapse; font-size: .9rem; }
th, td { text-align: left; padding: .5rem .55rem; border-bottom: 1px solid var(--line); }
th { color: var(--muted); font-weight: 600; font-size: .74rem; text-transform: uppercase;
     letter-spacing: .03em; }
td.r, th.r { text-align: right; }
tbody tr:last-child td { border-bottom: 0; }

/* Rankings (marcas / referencias / marketplaces que suben-bajan) */
table.rank tbody tr { transition: background .12s; }
table.rank tbody tr:hover td { background: #f8fafc; }
.rank-name { font-weight: 600; }
table.rank td { vertical-align: middle; }
/* Fila pinchable: filtra el informe por esa marca / referencia / marketplace. */
table.rank tbody tr.row-link { cursor: pointer; }
table.rank tbody tr.active td { background: #eef4ff; }
table.rank tbody tr.active td:first-child { font-weight: 700; color: var(--brand-ink); }
/* Filas 11-20: ocultas hasta pulsar "Ver más". */
table.rank tbody tr.extra { display: none; }
table.rank.expanded tbody tr.extra { display: table-row; }
.more-btn { margin-top: .6rem; }

/* Reporte diario de ventas: día en azul y su desglose por familia debajo */
.rep-table { max-width: 520px; }
.rep-table .rep-day td { background: #DCE6F1; font-weight: 700; border-bottom: 1px solid #b8cce4; }
.rep-table .rep-total td { background: #4F81BD; color: #fff; font-weight: 700; }
.rep-table .rep-fam td { font-style: italic; color: #334155; font-size: .85rem; padding: .3rem .55rem; }
.rep-table .rep-fam-name { text-align: right; }
.rep-table .rep-total-fam td { background: #f8fafc; }
.rep-table .rep-vacio td { background: #f8fafc; color: var(--muted); font-weight: 600; }

/* Filtros activos (referencia / marketplace) con su botón de quitar */
.active-filters { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap;
                  margin: -.4rem 0 1rem; }
.chip { display: inline-flex; align-items: center; gap: .3rem; text-decoration: none;
        background: #eef4ff; color: var(--brand-ink); border: 1px solid #dbe4f5;
        border-radius: 999px; padding: .22rem .6rem; font-size: .82rem; font-weight: 600; }
.chip:hover { background: #e0eaff; }

/* Panel de análisis IA */
.ai-card { border-color: #dbe4f5;
           background: linear-gradient(180deg, #fbfcff 0%, #ffffff 40%); }
.ai-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem;
           flex-wrap: wrap; }
.ai-head h2 { margin: 0; }
.ai-panel { margin-top: .8rem; font-size: .93rem; }
.ai-panel h3, .ai-panel h4 { margin: .9rem 0 .3rem; font-size: 1rem; }
.ai-panel ul { margin: .3rem 0 .7rem; padding-left: 1.2rem; }
.ai-panel li { margin: .2rem 0; }
.ai-loading { position: relative; padding-left: 1.4rem; }
.ai-loading::before {
  content: ""; position: absolute; left: 0; top: .15rem; width: 14px; height: 14px;
  border: 2px solid #cbd5e1; border-top-color: var(--brand); border-radius: 50%;
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Ajustes */
.page-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 1rem; }
.sched td, .sched th { vertical-align: middle; }
.sched input[type=text], .sched input[type=number] {
  padding: .4rem .45rem; border: 1px solid var(--line); border-radius: 8px;
}
.w60 { width: 60px; } .w70 { width: 70px; }
.actions { margin-top: 1rem; }
.btn-mini {
  font-size: .78rem; padding: .32rem .6rem; border: 1px solid var(--line);
  background: #f8fafc; border-radius: 8px; cursor: pointer; font-weight: 500;
}
.btn-mini:hover { background: #eef2f7; }
.btn-mini.danger { color: var(--down); border-color: var(--down-line); }
.add-row { display: flex; flex-wrap: wrap; gap: 1rem; align-items: end; }
.add-row label { font-size: .8rem; color: var(--muted); display: flex; flex-direction: column; gap: .25rem; }
.add-row input { padding: .55rem .65rem; border: 1px solid var(--line); border-radius: 10px; }
.add-row h2 { width: 100%; }
.add-row .chk { flex-direction: row; align-items: center; gap: .4rem; }
.ok { color: var(--up); font-weight: 600; }
.ko { color: var(--down); font-weight: 600; }
.saved { background: var(--up-bg); color: var(--up); border: 1px solid var(--up-line);
         padding: .65rem .95rem; border-radius: 10px; margin-bottom: 1rem; font-weight: 600; }
table.kv td { border: 0; padding: .28rem .8rem .28rem 0; font-size: .92rem; }
table.kv td:first-child { color: var(--muted); }

/* Mapa de zona */
.map-wrap { width: 100%; max-width: 680px; margin: 0 auto; }
.es-map { width: 100%; height: auto; display: block; }
.es-map .region, .es-map .region-inset { fill: #eef2f7; stroke: #b8c6d8; stroke-width: .5; }
.es-map circle { transition: fill-opacity .15s; cursor: default; }
.es-map g:hover circle { fill-opacity: .95; }

@media (max-width: 900px) {
  .layout { flex-direction: column; }
  .reports-nav { width: 100%; position: static; }
}
@media (max-width: 800px) {
  .kpis, .kpis3 { grid-template-columns: repeat(2, 1fr); }
  .grid2 { grid-template-columns: 1fr; }
}

/* ===== Barra lateral (estilo GOODBUY) ===== */
:root {
  --side-bg1: #123659;
  --side-bg2: #0c2340;
  --side-ink: #b4c5d8;
  --side-ink-strong: #ffffff;
  --side-muted: #61799a;
  --side-active-bg: #1d4369;
  --side-hover: rgba(255,255,255,.06);
  --side-line: rgba(255,255,255,.09);
  --avatar: #ef9a2a;
}
.app-shell { display: flex; min-height: 100vh; }
.sidebar {
  width: 252px; flex-shrink: 0; box-sizing: border-box;
  background: linear-gradient(180deg, var(--side-bg1), var(--side-bg2));
  color: var(--side-ink);
  display: flex; flex-direction: column;
  padding: 1.5rem 1rem 1.1rem;
  position: sticky; top: 0; height: 100vh; overflow-y: auto;
}
.side-brand { display: block; padding: 0 .4rem; }
.side-logo-img { width: 152px; height: auto; display: block; }
.side-sub {
  margin: .55rem .5rem 0; font-size: .72rem; font-weight: 600;
  letter-spacing: .34em; color: #8ba1bc; text-transform: uppercase;
}
.side-nav { margin-top: 1.5rem; display: flex; flex-direction: column; gap: .12rem; flex: 1; }
.side-section {
  margin: 1.1rem .6rem .45rem; font-size: .68rem; font-weight: 700;
  letter-spacing: .11em; text-transform: uppercase; color: var(--side-muted);
}
.side-section:first-child { margin-top: 0; }
.side-link {
  display: flex; align-items: center; gap: .72rem;
  padding: .62rem .7rem; border-radius: 10px;
  color: var(--side-ink); text-decoration: none; font-size: .93rem; font-weight: 500;
  transition: background .14s, color .14s;
}
.side-link:hover { background: var(--side-hover); color: var(--side-ink-strong); }
.side-link.active {
  background: var(--side-active-bg); color: var(--side-ink-strong); font-weight: 600;
  box-shadow: inset 0 0 0 1px rgba(255,255,255,.08);
}
.side-ico {
  width: 18px; height: 18px; flex-shrink: 0; fill: none; stroke: currentColor;
  stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round;
}
.side-foot { margin-top: auto; padding-top: 1rem; border-top: 1px solid var(--side-line); }
.side-user { display: flex; align-items: center; gap: .6rem; padding: .3rem .3rem; }
.avatar {
  width: 36px; height: 36px; flex-shrink: 0; border-radius: 50%;
  background: var(--avatar); color: #fff; font-weight: 700; font-size: .82rem;
  display: grid; place-items: center; text-transform: uppercase;
}
.side-user-info { display: flex; flex-direction: column; line-height: 1.25; min-width: 0; }
.side-user-info strong { color: #fff; font-size: .9rem; font-weight: 600;
  text-transform: capitalize; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.side-user-info small { color: var(--side-muted); font-size: .76rem; }
.side-logout { margin-left: auto; color: var(--side-muted); display: inline-flex; padding: .3rem; }
.side-logout:hover { color: #fff; }

/* Zona de contenido */
.content { flex: 1; min-width: 0; }
.content-inner { max-width: 1180px; margin: 1.8rem auto; padding: 0 1.6rem; }

@media (max-width: 900px) {
  .app-shell { flex-direction: column; }
  .sidebar { width: 100%; height: auto; position: static; }
  .side-nav { gap: .1rem; }
  .content-inner { margin: 1.2rem auto; padding: 0 1rem; }
}

/* Cabecera de marca en el login */
.login-brand {
  background: linear-gradient(180deg, var(--side-bg1), var(--side-bg2));
  margin: -1.15rem -1.35rem 1rem; padding: 1.6rem 1.35rem 1.3rem;
  border-radius: var(--radius) var(--radius) 0 0; text-align: center;
}
.login-logo { width: 168px; height: auto; }
.login-sub { display: block; margin-top: .5rem; font-size: .72rem; font-weight: 600;
  letter-spacing: .34em; color: #8ba1bc; text-transform: uppercase; }
