:root {
  --bg: #f6f3ed;
  --surface: #ffffff;
  --surface-alt: #faf7f1;
  --border: #e6dfd1;
  --ink: #2a2118;
  --ink-soft: #6b6055;
  --brand-dark: #241a13;
  --brand-dark-2: #2f2318;
  --gold: #b8863b;
  --gold-light: #e8cd98;
  --green: #3f7a52;
  --green-bg: #e7f2ea;
  --red: #ad4038;
  --red-bg: #f8e9e7;
  --amber: #b57a1f;
  --amber-bg: #faf0dd;
  --blue: #3a6ea5;
  --blue-bg: #e8f0f8;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(36, 26, 19, 0.06), 0 4px 16px rgba(36, 26, 19, 0.06);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}

a { color: inherit; text-decoration: none; }
button { font-family: inherit; }

.app-shell {
  display: flex;
  min-height: 100vh;
}

/* ---------- Sidebar ---------- */

.sidebar {
  width: 232px;
  flex-shrink: 0;
  background: linear-gradient(180deg, var(--brand-dark), var(--brand-dark-2));
  color: #f1e8da;
  display: flex;
  flex-direction: column;
  padding: 22px 16px;
  position: sticky;
  top: 0;
  height: 100vh;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 8px 26px;
}

.brand-mark {
  width: 34px;
  height: 34px;
  border-radius: 8px;
  background: linear-gradient(135deg, var(--gold-light), var(--gold));
  color: #241a13;
  font-weight: 700;
  font-size: 17px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.brand-text { display: flex; flex-direction: column; line-height: 1.2; }
.brand-text strong { font-size: 15.5px; letter-spacing: 0.02em; }
.brand-text span { font-size: 11.5px; color: #cbb995; }

.nav { display: flex; flex-direction: column; gap: 2px; margin-top: 6px; }

.nav a {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 8px;
  color: #d9cbb2;
  font-size: 13.5px;
  font-weight: 500;
  transition: background 0.12s ease, color 0.12s ease;
}

.nav a:hover { background: rgba(255,255,255,0.06); color: #f1e8da; }

.nav a.active {
  background: rgba(232, 205, 152, 0.14);
  color: var(--gold-light);
}

.nav-icon { font-size: 17px; width: 20px; text-align: center; opacity: 0.9; line-height: 1; }

.sidebar-footer {
  margin-top: auto;
  font-size: 11px;
  color: #9c8d76;
  padding: 8px;
}

/* ---------- Main ---------- */

.main {
  flex: 1;
  min-width: 0;
  padding: 28px 34px 60px;
}

.view { max-width: 1180px; }

.page-header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  margin-bottom: 22px;
  gap: 16px;
  flex-wrap: wrap;
}

.page-header h1 {
  font-size: 22px;
  margin: 0 0 4px;
  letter-spacing: -0.01em;
}

.page-header p {
  margin: 0;
  color: var(--ink-soft);
  font-size: 13px;
}

.header-actions { display: flex; gap: 8px; }

/* ---------- Buttons ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 9px 15px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--ink);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.12s ease;
}

.btn:hover { border-color: #d8cbb0; background: var(--surface-alt); }

.btn-primary {
  background: var(--brand-dark);
  border-color: var(--brand-dark);
  color: #f1e8da;
}
.btn-primary:hover { background: var(--brand-dark-2); }

.btn-gold {
  background: linear-gradient(135deg, var(--gold-light), var(--gold));
  border-color: var(--gold);
  color: #241a13;
}

.btn-danger { color: var(--red); }
.btn-danger:hover { background: var(--red-bg); border-color: #e3bdb8; }

.btn-sm { padding: 6px 11px; font-size: 12px; }
.btn-icon { padding: 7px 9px; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ---------- Cards / grid ---------- */

.grid { display: grid; gap: 16px; }
.grid-4 { grid-template-columns: repeat(4, 1fr); }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.grid-2 { grid-template-columns: repeat(2, 1fr); }

@media (max-width: 980px) {
  .grid-4 { grid-template-columns: repeat(2, 1fr); }
  .grid-3 { grid-template-columns: repeat(2, 1fr); }
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
  box-shadow: var(--shadow);
}

.stat-card .stat-label {
  font-size: 12px;
  color: var(--ink-soft);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.stat-card .stat-value {
  font-size: 26px;
  font-weight: 700;
  margin-top: 6px;
  letter-spacing: -0.01em;
}

.stat-card .stat-sub {
  font-size: 12px;
  color: var(--ink-soft);
  margin-top: 4px;
}

.card-title {
  font-size: 14px;
  font-weight: 700;
  margin: 0 0 14px;
}

/* ---------- Tables ---------- */

table { width: 100%; border-collapse: collapse; }

th {
  text-align: left;
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--ink-soft);
  font-weight: 700;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
}

td {
  padding: 12px;
  border-bottom: 1px solid var(--border);
  font-size: 13.5px;
  vertical-align: middle;
}

tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--surface-alt); }
tbody tr.clickable { cursor: pointer; }

.table-wrap {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  overflow-x: auto;
}

.empty-state {
  text-align: center;
  padding: 50px 20px;
  color: var(--ink-soft);
}
.empty-state strong { display: block; color: var(--ink); margin-bottom: 4px; font-size: 14px; }

/* ---------- Search / toolbar ---------- */

.toolbar {
  display: flex;
  gap: 10px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

.search-input {
  flex: 1;
  min-width: 200px;
}

input[type="text"], input[type="email"], input[type="number"], input[type="date"], input[type="search"], input[type="password"], select, textarea {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 13.5px;
  font-family: inherit;
  color: var(--ink);
  background: var(--surface);
}

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--gold);
  box-shadow: 0 0 0 3px rgba(184, 134, 59, 0.15);
}

label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--ink-soft);
  margin-bottom: 5px;
}

.field { margin-bottom: 14px; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

/* ---------- Badges ---------- */

.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 9px;
  border-radius: 100px;
  font-size: 11.5px;
  font-weight: 700;
  white-space: nowrap;
}

.badge-retail { background: var(--blue-bg); color: var(--blue); }
.badge-wholesale { background: var(--gold-light); color: #7a5620; }
.badge-low { background: var(--red-bg); color: var(--red); }
.badge-ok { background: var(--green-bg); color: var(--green); }
.badge-order { background: var(--green-bg); color: var(--green); }
.badge-visit { background: var(--blue-bg); color: var(--blue); }

/* A quieter tag than .badge — this marks where a row came from (manual entry
   vs. a file import), not a status that needs attention, so it stays small
   and low-contrast rather than competing with real alerts. */
.source-tag {
  display: inline-flex;
  align-items: center;
  padding: 2px 7px;
  border-radius: 100px;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  white-space: nowrap;
}
.source-tag-manual { background: var(--blue-bg); color: var(--blue); }
.source-tag-import { background: var(--surface-alt); color: var(--ink-soft); border: 1px solid var(--border); }

/* ---------- Historial de actividad (customer detail) ---------- */

.activity-timeline { display: flex; flex-direction: column; gap: 26px; }

.activity-month-label {
  font-size: 12.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--ink-soft);
  margin-bottom: 10px;
  padding-left: 2px;
}

.activity-entries { display: flex; flex-direction: column; gap: 10px; }

.activity-entry {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 4px solid var(--gold-light);
  border-radius: 8px;
  padding: 14px 16px;
}

/* Two-color band, flips with every quarter, so a long history reads as
   alternating blocks at a glance without having to check every date. */
.activity-entry.band-b { background: var(--surface-alt); border-left-color: var(--blue); }

.activity-entry-top {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 8px;
}

.activity-date { font-size: 12.5px; color: var(--ink-soft); font-weight: 600; }

.activity-amount { margin-left: auto; font-weight: 700; font-variant-numeric: tabular-nums; }

.activity-entry-detail { font-size: 13.5px; color: var(--ink); line-height: 1.7; }

.stat-card-hero .stat-value-hero {
  font-size: 34px;
  font-weight: 700;
  margin-top: 6px;
  letter-spacing: -0.01em;
}

/* ---------- Brand sections (inventory) ---------- */

.brand-section { margin-bottom: 16px; }
.brand-section-header {
  padding: 12px 16px;
  font-size: 13px;
  font-weight: 700;
  border-bottom: 1px solid var(--border);
  background: var(--surface-alt);
}

/* A brand section rendered as <details>/<summary> (collapsed by default,
   e.g. "Sin marca") — same look as the plain div version, plus a cursor and
   a rotating disclosure arrow. */
summary.brand-section-header {
  cursor: pointer;
  list-style: none;
  display: flex;
  align-items: center;
  gap: 8px;
}
summary.brand-section-header::-webkit-details-marker { display: none; }
summary.brand-section-header::before {
  content: '▸';
  font-size: 10px;
  color: var(--ink-soft);
  transition: transform 0.12s ease;
}
details[open] > summary.brand-section-header::before { transform: rotate(90deg); }
details.brand-section table { border-top: 1px solid var(--border); }

/* ---------- Visit / quick entry screen ---------- */

.visit-wrap { display: flex; flex-direction: column; gap: 14px; max-width: 560px; }

.selected-pill {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 14px;
}

.picker-results {
  margin-top: 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}
.picker-results:empty { display: none; }

.picker-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}
.picker-item:last-child { border-bottom: none; }
.picker-item:hover, .picker-item:active { background: var(--surface-alt); }
.picker-item span { font-size: 12px; }

.picker-empty { padding: 12px 14px; color: var(--ink-soft); font-size: 13px; }

.cart { margin-top: 12px; display: flex; flex-direction: column; gap: 8px; }

.cart-row {
  display: grid;
  grid-template-columns: 1fr auto auto auto;
  align-items: center;
  gap: 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
}

.qty-stepper { display: flex; align-items: center; gap: 8px; }
.qty-btn {
  width: 30px; height: 30px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  line-height: 1;
}
.qty-btn:hover { background: var(--surface-alt); }
.qty-value { min-width: 20px; text-align: center; font-weight: 700; }

.cart-row-price { font-weight: 700; font-variant-numeric: tabular-nums; }

.cart-total {
  display: flex;
  justify-content: space-between;
  margin-top: 10px;
  padding: 12px 14px;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-weight: 700;
}

.promo-split { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.promo-split .field { margin-bottom: 0; }

/* ---------- Tags ---------- */

.tag-chip-row { display: flex; flex-wrap: wrap; gap: 8px; min-height: 26px; }

.tag-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--gold-light);
  color: #7a5620;
  padding: 5px 6px 5px 11px;
  border-radius: 100px;
  font-size: 12px;
  font-weight: 600;
}

.tag-chip-remove {
  background: rgba(36, 26, 19, 0.12);
  border: none;
  color: inherit;
  width: 17px; height: 17px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 12px;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.tag-chip-remove:hover { background: rgba(36, 26, 19, 0.22); }

.tag-quick-add { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 12px; }
.tag-suggest-btn {
  background: var(--surface-alt);
  border-style: dashed;
  color: var(--ink-soft);
  font-weight: 600;
}

.tag-add-row { display: flex; gap: 8px; margin-top: 12px; }
.tag-add-row input { flex: 1; }

.tag-chip-row-sm { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.tag-chip-sm {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  color: var(--ink-soft);
  padding: 2px 8px;
  border-radius: 100px;
  font-size: 10.5px;
  font-weight: 600;
}
.tag-chip-more { color: var(--ink-soft); }

/* ---------- Pendientes ---------- */

.pendiente-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.pendiente-row:last-child { border-bottom: none; }
.pendiente-row.pendiente-resuelto { opacity: 0.6; }
.pendiente-main { font-size: 13px; max-width: 75%; }
.pendiente-actions { display: flex; gap: 6px; flex-shrink: 0; }

.badge-pendiente-deuda { background: var(--red-bg); color: var(--red); }
.badge-pendiente-pedido { background: var(--amber-bg); color: var(--amber); }
.badge-pendiente-otro { background: var(--surface-alt); color: var(--ink-soft); border: 1px solid var(--border); }

/* ---------- Envíos pendientes ---------- */

.badge-envio-pendiente { background: var(--amber-bg); color: var(--amber); }
.badge-envio-anulado { background: var(--red-bg); color: var(--red); }

/* ---------- Import ---------- */

.import-upload {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 30px;
  text-align: center;
  color: var(--ink-soft);
}
.import-upload input[type="file"] { max-width: 320px; margin: 10px auto 0; }

.import-summary { display: flex; flex-wrap: wrap; gap: 12px; margin-bottom: 18px; }
.import-summary .card { flex: 1; min-width: 140px; text-align: center; }
.import-summary .stat-value { font-size: 22px; font-weight: 700; }

.unmatched-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.unmatched-row:last-child { border-bottom: none; }
.unmatched-info { font-size: 13px; flex: 1; min-width: 220px; }
.unmatched-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.unmatched-actions select, .unmatched-actions input { width: auto; min-width: 160px; }
.unmatched-resolved { color: var(--green); font-weight: 600; font-size: 12.5px; }

.btn-save-visit {
  width: 100%;
  justify-content: center;
  padding: 14px;
  font-size: 15px;
}

.nav-cta {
  background: rgba(232, 205, 152, 0.12);
  border: 1px dashed rgba(232, 205, 152, 0.4);
  margin-bottom: 6px;
}

/* ---------- Modal ---------- */

.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(36, 26, 19, 0.45);
  display: flex; align-items: flex-start; justify-content: center;
  padding: 48px 20px;
  overflow-y: auto;
  z-index: 100;
}

.modal {
  background: var(--surface);
  border-radius: 14px;
  width: 100%;
  max-width: 560px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.25);
}

.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 18px 22px;
  border-bottom: 1px solid var(--border);
}
.modal-header h2 { font-size: 16px; margin: 0; }
.modal-close { background: none; border: none; font-size: 20px; cursor: pointer; color: var(--ink-soft); line-height: 1; padding: 4px; }
.modal-body { padding: 20px 22px; max-height: 65vh; overflow-y: auto; }
.modal-footer { padding: 16px 22px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 8px; }

.line-items { border: 1px solid var(--border); border-radius: 8px; overflow: hidden; margin-top: 6px; }
.line-item-row {
  display: grid;
  grid-template-columns: 1fr 70px 90px 32px;
  gap: 8px;
  padding: 8px 10px;
  align-items: center;
  border-bottom: 1px solid var(--border);
}
.line-item-row:last-child { border-bottom: none; }
.line-item-remove { background: none; border: none; color: var(--red); cursor: pointer; font-size: 15px; }
.line-items-total { display: flex; justify-content: space-between; padding: 10px 12px; font-weight: 700; background: var(--surface-alt); }

/* ---------- Toast ---------- */

.toast {
  position: fixed;
  bottom: 24px; right: 24px;
  background: var(--brand-dark);
  color: #f1e8da;
  padding: 11px 18px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  box-shadow: var(--shadow);
  z-index: 200;
}
.toast.error { background: var(--red); }

/* ---------- Reports ---------- */

.chart-card { padding: 18px 22px 8px; }
/* The chart SVGs (reports.js) carry a fixed pixel height attribute sized for
   their 640-unit-wide design viewBox — width already scales via width:100%,
   but without this, height stays literal instead of scaling down to match,
   so on any container narrower than ~640px the drawing shrinks to fit while
   the box around it doesn't, leaving it small and vertically stranded in a
   mostly-empty box. height:auto lets the browser derive it from the
   viewBox's own aspect ratio instead, same as the width. */
.chart-card svg { height: auto; display: block; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; margin-top: 10px; font-size: 12px; color: var(--ink-soft); }
.legend-swatch { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 5px; vertical-align: middle; }

.text-right { text-align: right; }
.text-muted { color: var(--ink-soft); }
.mono-num { font-variant-numeric: tabular-nums; }

.link-btn {
  background: none; border: none; color: var(--gold); font-weight: 600; cursor: pointer; font-size: 13px; padding: 0;
}

/* ---------- Mobile ---------- */

@media (max-width: 780px) {
  .app-shell { flex-direction: column; }

  .sidebar {
    position: sticky;
    top: 0;
    width: 100%;
    height: auto;
    flex-direction: row;
    align-items: center;
    padding: 10px 12px;
    gap: 10px;
    z-index: 60;
  }

  .brand { padding: 0; flex-shrink: 0; }
  .brand-text span { display: none; }

  .nav {
    flex-direction: row;
    flex: 1;
    overflow-x: auto;
    gap: 4px;
    margin-top: 0;
    -webkit-overflow-scrolling: touch;
    /* More nav items than fit on a phone screen, so the row scrolls — fade
       the trailing edge to hint there's more, since otherwise "Panel" or
       "Envíos pendientes" getting cut off reads as broken, not scrollable. */
    -webkit-mask-image: linear-gradient(to right, black calc(100% - 28px), transparent 100%);
    mask-image: linear-gradient(to right, black calc(100% - 28px), transparent 100%);
  }

  .nav a {
    white-space: nowrap;
    padding: 8px 10px;
    font-size: 12.5px;
  }

  .nav-cta { margin-bottom: 0; }

  .sidebar-footer { display: none; }

  .main { padding: 16px; }

  .grid-4, .grid-3, .grid-2 { grid-template-columns: 1fr; }

  .stat-card-hero { grid-column: span 1 !important; }

  /* Stat-card rows outside the .grid-* system (import previews, envíos
     pendientes) — same 1-column collapse as .grid-*, so cards never end up
     squeezed two-to-a-row with barely any breathing room. */
  .import-summary { flex-direction: column; }
  .import-summary .card { min-width: 0; }

  .page-header { align-items: flex-start; }

  .modal-overlay { padding: 0; align-items: stretch; }
  .modal { max-width: none; height: 100%; border-radius: 0; display: flex; flex-direction: column; }
  .modal-body { flex: 1; }

  .line-item-row { grid-template-columns: 1fr; }
  .cart-row { grid-template-columns: 1fr; justify-items: start; }

  table, .table-wrap { font-size: 13px; }

  /* Wide tables (6 columns in Clientes, 5-6 in Pedidos/Envíos pendientes)
     read poorly as a cramped horizontally-scrolling table on a phone — each
     row becomes its own stacked card instead, with the column header shown
     as a small label in front of its value (see .table-cards td::before).
     Opt-in via the .table-cards class so narrower tables (Inventario,
     Rutas) that already read fine as a plain table are untouched. */
  .table-wrap-cards { background: transparent; border: none; box-shadow: none; overflow: visible; }
  .table-cards thead { display: none; }
  .table-cards, .table-cards tbody { display: block; width: 100%; }
  .table-cards tbody tr {
    display: block;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 4px 14px;
    margin-bottom: 10px;
  }
  .table-cards tbody tr:last-child { margin-bottom: 0; }
  .table-cards tbody tr:hover { background: var(--surface); }
  .table-cards td {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 14px;
    padding: 8px 0;
    border-bottom: 1px solid var(--border);
    text-align: right;
  }
  .table-cards tr td:last-child { border-bottom: none; }
  .table-cards td[data-label]::before {
    content: attr(data-label);
    font-size: 10.5px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--ink-soft);
    text-align: left;
    flex-shrink: 0;
    padding-top: 1px;
  }
  .table-cards td.td-actions { justify-content: flex-end; }
}
