:root {
  --rojo: #E11B17;
  --rojo-700: #C0140F;
  --rojo-900: #8E0E0A;
  --rojo-soft: #FDECEB;
  --tinta: #161719;
  --gris-900: #1F2430;
  --gris-600: #5A6273;
  --gris-400: #9AA1B1;
  --gris-200: #E6E9EF;
  --gris-100: #F1F3F7;
  --bg: #F4F6FA;
  --blanco: #FFFFFF;
  --verde: #16A34A;
  --sombra: 0 10px 30px -12px rgba(22, 27, 40, .18);
  --sombra-sm: 0 2px 10px -4px rgba(22, 27, 40, .14);
  --radio: 18px;
  --maxw: 480px;
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

html, body {
  font-family: "Roboto", system-ui, sans-serif;
  background: var(--bg);
  color: var(--tinta);
  -webkit-font-smoothing: antialiased;
}

.app {
  max-width: var(--maxw);
  margin: 0 auto;
  min-height: 100vh;
  background: var(--bg);
  position: relative;
  box-shadow: 0 0 60px -20px rgba(0, 0, 0, .25);
}

/* ---------- Topbar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  padding-top: calc(14px + env(safe-area-inset-top));
  background: linear-gradient(135deg, var(--rojo) 0%, var(--rojo-700) 100%);
  color: #fff;
  box-shadow: var(--sombra-sm);
}

.brand { display: flex; align-items: center; gap: 10px; }
.brand__mark {
  width: 34px; height: 34px;
  display: grid; place-items: center;
  background: #fff; color: var(--rojo);
  font-weight: 900; font-size: 20px;
  border-radius: 10px;
  font-style: italic;
}
.brand__name { font-weight: 900; font-size: 19px; letter-spacing: -.3px; }
.brand__name span { font-weight: 300; }

.user-chip {
  display: flex; align-items: center; gap: 8px;
  background: rgba(255, 255, 255, .16);
  border: none; color: #fff;
  padding: 6px 12px 6px 6px;
  border-radius: 999px;
  font: inherit; font-size: 13px; font-weight: 500;
  cursor: pointer;
}
.user-chip__avatar {
  width: 26px; height: 26px; border-radius: 50%;
  background: #fff; color: var(--rojo);
  display: grid; place-items: center; font-weight: 700; font-size: 13px;
}

/* ---------- Content ---------- */
.content {
  padding: 18px 16px calc(96px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.hero { padding: 6px 4px 0; }
.hero__eyebrow {
  text-transform: uppercase;
  letter-spacing: 1.4px;
  font-size: 11px; font-weight: 700;
  color: var(--rojo);
}
.hero__title { font-size: 26px; font-weight: 900; letter-spacing: -.6px; margin-top: 2px; }
.hero__sub { color: var(--gris-600); font-size: 14px; margin-top: 4px; line-height: 1.4; }

/* ---------- Card ---------- */
.card {
  background: var(--blanco);
  border-radius: var(--radio);
  box-shadow: var(--sombra);
  padding: 18px;
  border: 1px solid rgba(230, 233, 239, .8);
}
.card__head {
  display: flex; align-items: flex-start; justify-content: space-between;
  gap: 12px; margin-bottom: 14px;
}
.card__title { font-size: 16px; font-weight: 700; letter-spacing: -.2px; }
.card__sub { font-size: 12.5px; color: var(--gris-400); margin-top: 2px; }

/* ---------- Query ---------- */
.query__row { display: flex; gap: 10px; align-items: flex-end; }
.field { flex: 1; display: flex; flex-direction: column; gap: 6px; }
.field__label {
  font-size: 12px; font-weight: 600; color: var(--gris-600);
  text-transform: uppercase; letter-spacing: .5px;
}
.field__input {
  font: inherit; font-size: 15px; font-weight: 500;
  padding: 12px 14px;
  border: 1.5px solid var(--gris-200);
  border-radius: 12px;
  background: var(--gris-100);
  color: var(--tinta);
  width: 100%;
  transition: border-color .15s, background .15s;
}
.field__input:focus { outline: none; border-color: var(--rojo); background: #fff; }

/* Selector de fecha: texto dd-mm-aaaa + calendario nativo encima */
.datepicker { position: relative; }
.datepicker #fecha-display { padding-right: 42px; cursor: pointer; }
.datepicker__native {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  opacity: 0; cursor: pointer;
  border: none; background: none;
}
.datepicker__native::-webkit-calendar-picker-indicator { width: 100%; cursor: pointer; }
.datepicker__icon {
  position: absolute; right: 14px; top: 50%; transform: translateY(-50%);
  color: var(--rojo); pointer-events: none;
}
.datepicker:focus-within #fecha-display { border-color: var(--rojo); background: #fff; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  font: inherit; font-weight: 700; font-size: 14px;
  border: none; border-radius: 12px;
  padding: 13px 16px;
  cursor: pointer;
  transition: transform .08s ease, box-shadow .15s, background .15s;
  white-space: nowrap;
}
.btn:active { transform: scale(.97); }
.btn--primary {
  background: linear-gradient(135deg, var(--rojo) 0%, var(--rojo-700) 100%);
  color: #fff;
  box-shadow: 0 8px 18px -8px rgba(225, 27, 23, .7);
}
.btn--primary:disabled { opacity: .6; cursor: default; }
.query__hint { font-size: 11.5px; color: var(--gris-400); margin-top: 10px; }
.query__hint.demo { color: #B45309; }
.query__hint.erp { color: var(--verde); }

/* ---------- KPIs ---------- */
.kpis {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
.kpi {
  background: var(--blanco);
  border-radius: 16px;
  padding: 15px;
  box-shadow: var(--sombra-sm);
  border: 1px solid rgba(230, 233, 239, .8);
  position: relative;
  overflow: hidden;
}
.kpi--hero {
  grid-column: 1 / -1;
  background: linear-gradient(135deg, var(--gris-900) 0%, #2C3344 100%);
  color: #fff;
  border: none;
}
.kpi--hero::after {
  content: ""; position: absolute; right: -30px; top: -30px;
  width: 120px; height: 120px; border-radius: 50%;
  background: radial-gradient(circle, rgba(225,27,23,.45), transparent 70%);
}
.kpi__label { font-size: 11.5px; font-weight: 600; color: var(--gris-400); text-transform: uppercase; letter-spacing: .6px; }
.kpi--hero .kpi__label { color: rgba(255,255,255,.7); }
.kpi__value { font-size: 22px; font-weight: 900; letter-spacing: -.5px; margin-top: 6px; }
.kpi--hero .kpi__value { font-size: 30px; }
.kpi__delta { display: inline-flex; align-items: center; gap: 4px; font-size: 12.5px; font-weight: 700; margin-top: 8px; }
.kpi__delta.up { color: var(--verde); }
.kpi__delta.down { color: var(--rojo); }
.kpi--hero .kpi__delta.up { color: #4ADE80; }
.kpi--hero .kpi__delta.down { color: #FCA5A5; }
.kpi__ref { font-size: 12px; font-weight: 500; color: rgba(255, 255, 255, .55); margin-top: 6px; }

/* ---------- Chart ---------- */
.legend { display: flex; gap: 14px; flex-shrink: 0; }
.legend__item { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--gris-600); font-weight: 500; }
.dot { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }
.dot--rojo { background: var(--rojo); }
.dot--gris { background: #475569; }
.chart-wrap { position: relative; height: 320px; }

/* ---------- Ranking ---------- */
.rank { list-style: none; display: flex; flex-direction: column; }
.rank__item {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 2px;
  border-bottom: 1px solid var(--gris-100);
}
.rank__item:last-child { border-bottom: none; }
.rank__pos {
  width: 26px; height: 26px; flex-shrink: 0;
  display: grid; place-items: center;
  border-radius: 8px; font-size: 12px; font-weight: 800;
  background: var(--gris-100); color: var(--gris-600);
}
.rank__item:nth-child(1) .rank__pos { background: var(--rojo); color: #fff; }
.rank__main { flex: 1; min-width: 0; }
.rank__name { font-size: 14px; font-weight: 600; }
.rank__bar { height: 5px; border-radius: 3px; background: var(--gris-100); margin-top: 6px; overflow: hidden; }
.rank__bar i { display: block; height: 100%; background: linear-gradient(90deg, var(--rojo), var(--rojo-700)); border-radius: 3px; }
.rank__right { text-align: right; flex-shrink: 0; }
.rank__val { font-size: 14px; font-weight: 800; }
.rank__pct { font-size: 12px; font-weight: 700; margin-top: 2px; }
.rank__pct.up { color: var(--verde); }
.rank__pct.down { color: var(--rojo); }

/* ---------- Inventarios ---------- */
.btn--block { width: 100%; margin-top: 12px; }
.query__range { display: flex; gap: 10px; }
.query__range .field { flex: 1; }

.inv-grid { display: grid; grid-template-columns: 1fr; gap: 14px; }

.invc {
  background: var(--blanco);
  border-radius: var(--radio);
  box-shadow: var(--sombra-sm);
  border: 1px solid rgba(230, 233, 239, .85);
  padding: 16px;
}
.invc__head { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.invc__name { font-size: 15px; font-weight: 700; letter-spacing: -.2px; }
.invc__badge {
  font-size: 11px; font-weight: 700; color: var(--gris-600);
  background: var(--gris-100); padding: 4px 9px; border-radius: 999px;
  white-space: nowrap; text-transform: lowercase;
}
.invc__cobrow { display: flex; align-items: baseline; justify-content: space-between; margin-top: 14px; }
.invc__coblabel { font-size: 11px; font-weight: 600; color: var(--gris-400); text-transform: uppercase; letter-spacing: .5px; }
.invc__cob { font-size: 20px; font-weight: 900; letter-spacing: -.5px; }
.invc__cob.ok { color: var(--verde); }
.invc__cob.warn { color: #D97706; }
.invc__cob.bad { color: var(--rojo); }

.invc__bar { height: 7px; border-radius: 4px; background: var(--gris-100); margin-top: 8px; overflow: hidden; }
.invc__bar i { display: block; height: 100%; border-radius: 4px; transition: width .5s ease; }
.invc__bar i.ok { background: linear-gradient(90deg, #16A34A, #22C55E); }
.invc__bar i.warn { background: linear-gradient(90deg, #D97706, #F59E0B); }
.invc__bar i.bad { background: linear-gradient(90deg, var(--rojo-700), var(--rojo)); }

.invc__vals { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; margin-top: 10px; }
.invc__vals strong { font-size: 15px; font-weight: 800; }
.invc__vals .muted { font-size: 12px; color: var(--gris-400); font-weight: 500; }

.invc__mods {
  display: flex; align-items: center; justify-content: space-between;
  margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--gris-100);
  font-size: 12.5px; color: var(--gris-600); font-weight: 500;
}
.invc__modval { font-weight: 800; color: var(--tinta); }

.invc__stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 12px; }
.invc__stats > div {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  background: var(--gris-100); border-radius: 12px; padding: 9px 4px;
}
.invc__stats small { font-size: 10px; color: var(--gris-400); font-weight: 600; text-transform: uppercase; letter-spacing: .4px; }
.st { font-size: 18px; font-weight: 900; }
.st--ok { color: var(--verde); }
.st--warn { color: #D97706; }
.st--bad { color: var(--rojo); }

@media (min-width: 540px) { .inv-grid { grid-template-columns: 1fr 1fr; } }

/* ---------- Estados ---------- */
.state { display: flex; flex-direction: column; align-items: center; gap: 14px; padding: 40px 0; color: var(--gris-600); }
.spinner {
  width: 38px; height: 38px; border-radius: 50%;
  border: 3.5px solid var(--gris-200); border-top-color: var(--rojo);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.empty { text-align: center; padding: 30px 10px; color: var(--gris-400); font-size: 14px; }

/* ---------- Tabbar ---------- */
.tabbar {
  position: fixed;
  bottom: 0; left: 50%; transform: translateX(-50%);
  width: 100%; max-width: var(--maxw);
  display: flex;
  background: rgba(255, 255, 255, .92);
  backdrop-filter: saturate(180%) blur(14px);
  border-top: 1px solid var(--gris-200);
  padding: 8px 6px calc(8px + env(safe-area-inset-bottom));
  z-index: 40;
}
.tab {
  flex: 1; min-width: 0;
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  background: none; border: none; cursor: pointer;
  color: var(--gris-400);
  font: inherit; font-size: 9.5px; font-weight: 600;
  padding: 4px 2px;
  transition: color .15s;
}
.tab svg { width: 21px; height: 21px; flex-shrink: 0; }
.tab span {
  max-width: 100%;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  line-height: 1.1;
}
.tab.is-active { color: var(--rojo); }

/* fade-in */
[hidden] { display: none !important; }
.fade-in { animation: fade .35s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

@media (max-width: 360px) {
  .query__row { flex-direction: column; align-items: stretch; }
  .btn--primary { width: 100%; }
}
