/* La transición de color solo se activa mientras el usuario cambia el tema a mano
   (clase agregada/quitada por el botón ☀️/🌙) — nunca durante la carga inicial de la página. */
html.theme-transitioning,
html.theme-transitioning * {
  transition: background-color 200ms ease, border-color 200ms ease, color 200ms ease !important;
}

/* Paleta del modo oscuro tomada de la maqueta acordada ("Bazar A&R Operations").
   Son negros neutros, no azules: el azul marino de antes peleaba con el acento
   naranja y hacia que el tema se viera a medio migrar. */
:root {
  --bg: #0D0E13;
  --bg-soft: #1D1F2A;
  --panel: #16171F;
  --border: #262835;
  --text: #F4F5FA;
  --muted: #8A8FA3;
  --primary: #FF7A45;
  --primary-rgb: 255, 122, 69;
  --primary-strong: #E85F2A;
  --primary-warm: #FFB24D;
  --primary-soft: rgba(255, 122, 69, 0.14);
  --surface-item: rgba(var(--surface-rgb), 0.5);
  --surface-rgb: 22, 23, 31;
  --border-rgb: 38, 40, 53;
  --ok: #34D399;
  --warn: #FFB24D;
  --danger: #FF5C7A;
  --human: #FF7A45;
  --ai: #34D399;
  --on-primary: #2b1305;
}

:root[data-theme="light"] {
  --bg: #DBD8D0;
  --bg-soft: #F3F1EA;
  --panel: #FFFFFF;
  --border: #DDD9CD;
  --text: #232420;
  --muted: #6b6a60;
  --primary: #EA5A24;
  --primary-rgb: 234, 90, 36;
  --primary-strong: #C94A1B;
  --primary-warm: #F09A3C;
  --primary-soft: rgba(234, 90, 36, 0.10);
  --surface-item: rgba(35, 33, 25, 0.045);
  --surface-rgb: 241, 238, 229;
  --border-rgb: 214, 209, 194;
  --ok: #17925F;
  --warn: #C77A12;
  --danger: #D13A5C;
  --human: #c2540f;
  --ai: #17925F;
  --on-primary: #fff7f0;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

/* Casillas, radios y deslizadores son controles del navegador: sin esto Chrome los
   pinta con el azul del sistema y desentonan con toda la paleta. */
input[type="checkbox"],
input[type="radio"],
input[type="range"],
progress {
  accent-color: var(--primary);
}

body {
  font-family: 'Plus Jakarta Sans', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

:root[data-theme="light"] body {
  background:
    radial-gradient(1100px 620px at 8% -12%, rgba(255, 255, 255, 0.55), transparent 55%),
    radial-gradient(900px 700px at 100% 115%, rgba(35, 33, 25, 0.06), transparent 50%),
    var(--bg);
}

:root[data-theme="light"] .sidebar {
  background: #FFFFFF;
  box-shadow: 1px 0 0 var(--border);
}

:root[data-theme="light"] header {
  background: rgba(255, 255, 255, 0.88);
}

:root[data-theme="light"] .search-results {
  background: #FFFFFF;
  box-shadow: 0 14px 40px rgba(35, 33, 25, 0.18);
}

:root[data-theme="light"] .panel,
:root[data-theme="light"] .modal-box {
  box-shadow: 0 6px 24px rgba(35, 33, 25, 0.08);
}

/* Tipografia de 3 capas: Fraunces para titulos, Plus Jakarta Sans para el cuerpo,
   Space Grotesk para cifras (KPIs, montos, dias del calendario). */
h1, h2, h3,
.panel-title-row h3,
.brand .name {
  font-family: 'Fraunces', Georgia, serif;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.dashboard-kpi strong,
.dashboard-kpi-delta,
.dashboard-cal-cell .day,
.dashboard-meta-item strong,
.stat .value,
.goal-top strong {
  font-family: 'Space Grotesk', system-ui, sans-serif;
  font-weight: 700;
}

body.chat-tab-active {
  overflow: hidden;
}

.app-shell {
  display: grid;
  grid-template-columns: 210px 1fr;
  min-height: 100vh;
  /* La altura la manda la ventana, no el contenido: asi el ultimo panel siempre
     termina en el borde inferior y no queda cortado a media fila. */
  height: 100vh;
}

.sidebar {
  border-right: 1px solid var(--border);
  background: rgba(var(--surface-rgb), 0.9);
  padding: 1.25rem 1rem;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}

.brand {
  display: flex;
  gap: 0.75rem;
  align-items: center;
  margin-bottom: 1.25rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--border);
}

.logo {
  width: 42px;
  height: 42px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  background: linear-gradient(180deg, rgba(var(--primary-rgb), 0.25), rgba(var(--primary-rgb), 0.08));
  border: 1px solid rgba(var(--primary-rgb), 0.35);
  font-size: 19px;
  font-weight: 700;
  line-height: 1;
  color: var(--primary);
}

.logo-wrap {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
}

.brand-logo {
  width: 42px;
  height: 42px;
  object-fit: contain;
  border-radius: 12px;
  border: 1px solid rgba(var(--primary-rgb), 0.35);
  background: rgba(var(--primary-rgb), 0.08);
  padding: 4px;
}

.brand-logo-mobile {
  width: 24px;
  height: 24px;
  object-fit: contain;
  border-radius: 8px;
  background: rgba(var(--primary-rgb), 0.12);
  padding: 2px;
}

h1 {
  font-size: 0.95rem;
  font-weight: 700;
}

.subtitle {
  color: var(--muted);
  font-size: 0.72rem;
}

.tabs {
  display: grid;
  gap: 0.35rem;
}

.tab-group {
  display: grid;
  gap: 0.35rem;
  margin-bottom: 0.6rem;
}

.tab-group-title {
  color: var(--muted);
  font-size: 0.66rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  padding: 0.35rem 0.25rem 0.1rem;
}

.tab {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  text-align: left;
  border: 1px solid transparent;
  background: transparent;
  color: var(--muted);
  padding: 0.5rem 0.65rem;
  border-radius: 9px;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 180ms ease;
}

.tab-icon {
  width: 17px;
  height: 17px;
  flex-shrink: 0;
  opacity: 0.85;
}

.tab:hover {
  color: var(--text);
  border-color: rgba(var(--border-rgb), 0.5);
  background: rgba(var(--surface-rgb), 0.45);
}

.tab.active {
  color: var(--primary);
  background: var(--primary-soft);
  border-color: transparent;
}

.tab.active .tab-icon {
  opacity: 1;
}

.sidebar-whatsapp {
  margin-top: 1rem;
  padding-top: 0.9rem;
  border-top: 1px solid var(--border);
}

.sidebar-wa-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.3rem 0.25rem;
  font-size: 0.78rem;
  color: var(--muted);
}

.sidebar-wa-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.sidebar-wa-dot.on { background: #22c55e; }
.sidebar-wa-dot.off { background: var(--muted); }
.sidebar-wa-dot.warn { background: #f59e0b; }

.sidebar-wa-qr {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.4rem;
  padding: 0.75rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: rgba(var(--surface-rgb), 0.5);
  text-align: center;
}

.sidebar-wa-qr-title {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text);
  margin: 0;
}

.sidebar-wa-qr img {
  border-radius: 8px;
  background: #fff;
  padding: 6px;
}

.sidebar-wa-qr .hint {
  font-size: 0.7rem;
  margin: 0;
}

/* Interruptor rápido "Rut activa/pausada" (debajo del bloque de WhatsApp en el
   sidebar): pausa el agente de inmediato sin desconectar WhatsApp ni perder el
   registro de conversaciones — a diferencia de desvincular el numero. */
.sidebar-ai-toggle {
  margin-top: 0.6rem;
  padding-top: 0.6rem;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.sidebar-ai-toggle-label {
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
  min-width: 0;
}

.sidebar-ai-toggle-title {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text);
}

.sidebar-ai-toggle-state {
  font-size: 0.68rem;
  color: var(--muted);
}

.toggle-switch {
  position: relative;
  display: inline-block;
  width: 40px;
  height: 22px;
  flex-shrink: 0;
}

.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-switch-track {
  position: absolute;
  inset: 0;
  background: var(--border);
  border-radius: 999px;
  cursor: pointer;
  transition: background-color 150ms ease;
}

.toggle-switch-track::before {
  content: '';
  position: absolute;
  width: 16px;
  height: 16px;
  left: 3px;
  top: 3px;
  background: var(--panel);
  border-radius: 50%;
  transition: transform 150ms ease;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

.toggle-switch input:checked + .toggle-switch-track {
  background: var(--ai);
}

.toggle-switch input:checked + .toggle-switch-track::before {
  transform: translateX(18px);
}

.toggle-switch input:disabled + .toggle-switch-track {
  opacity: 0.5;
  cursor: wait;
}

.workspace {
  display: grid;
  grid-template-rows: auto 1fr;
  grid-template-columns: minmax(0, 1fr);
  min-width: 0;
  /* Sin esto la fila 1fr crece con su contenido en vez de ceder y dejar que
     el area de trabajo haga su propio scroll. */
  min-height: 0;
}

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  padding: 1rem 1.5rem;
  border-bottom: 1px solid var(--border);
  background: rgba(var(--surface-rgb), 0.75);
  backdrop-filter: blur(8px);
  position: sticky;
  top: 0;
  z-index: 4;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  min-width: 0;
}

.icon-btn {
  display: none;
  align-items: center;
  justify-content: center;
  min-width: 40px;
  height: 40px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: rgba(var(--surface-rgb), 0.55);
  color: var(--text);
  font-size: 1.3rem;
  line-height: 1;
  cursor: pointer;
  /* El glifo ☰ trae de fabrica mas espacio en blanco debajo que arriba dentro
     de su propia caja de texto — sin esto, centrar por flexbox centra esa
     caja, no la tinta, y el icono se ve corrido hacia abajo. */
  transform: translateY(-0.08em);
}

.theme-toggle-btn {
  min-width: 40px;
  height: 40px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg-soft);
  color: var(--text);
  font-size: 1rem;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: none;
}

.theme-toggle-btn:hover {
  border-color: var(--primary);
  background: var(--primary-soft);
}

.header-mobile-brand {
  display: none;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.85rem;
  white-space: nowrap;
}

.mini-logo {
  width: 24px;
  height: 24px;
  border-radius: 8px;
  display: grid;
  place-items: center;
  background: rgba(var(--primary-rgb), 0.15);
}

.breadcrumb {
  color: var(--muted);
  font-size: 0.8rem;
}

.header-subtitle {
  margin-top: 0.2rem;
  font-size: 0.82rem;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

#btn-profile {
  display: none;
}

.search-wrap {
  position: relative;
}

.pill {
  margin-left: 0.3rem;
  display: inline-grid;
  place-items: center;
  min-width: 20px;
  height: 20px;
  border-radius: 999px;
  font-size: 0.72rem;
  background: rgba(var(--primary-rgb), 0.2);
  color: var(--primary);
  border: 1px solid rgba(var(--primary-rgb), 0.4);
}

.search {
  width: min(420px, 38vw);
  min-height: 44px;
  border: 1px solid var(--border);
  background: var(--bg-soft);
  color: var(--text);
  border-radius: 12px;
  padding: 0 0.8rem;
}

.search-results {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  width: min(520px, 70vw);
  max-height: 420px;
  overflow: auto;
  border: 1px solid rgba(var(--border-rgb), 0.9);
  border-radius: 12px;
  background: rgba(var(--surface-rgb), 0.97);
  box-shadow: 0 14px 40px rgba(2, 6, 23, 0.45);
  padding: 0.45rem;
  z-index: 12;
}

.search-group {
  margin-bottom: 0.45rem;
}

.search-group-title {
  color: var(--muted);
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 0.35rem 0.45rem;
}

.search-item {
  width: 100%;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text);
  text-align: left;
  border-radius: 10px;
  padding: 0.45rem;
  cursor: pointer;
  display: grid;
  gap: 0.15rem;
}

.search-item:hover {
  border-color: rgba(var(--primary-rgb), 0.45);
  background: rgba(var(--primary-rgb), 0.08);
}

.search-item small {
  color: var(--muted);
  font-size: 0.72rem;
}

.search:focus,
.chat-input input:focus,
.inline-form input:focus,
.modal-box input:focus,
.modal-box textarea:focus,
select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(var(--primary-rgb), 0.16);
}

.field-wrap input:focus,
.field-wrap textarea:focus,
.field-wrap select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(var(--primary-rgb), 0.16);
}

main {
  overflow: auto;
  padding: 1rem 1.2rem;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

body.chat-tab-active main {
  overflow: hidden;
}

.tab-content { display: none; }
.tab-content.active {
  display: block;
  flex: 1;
  min-height: 0;
}

.dashboard-grid {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 1rem;
}

.dashboard-grid-secondary {
  margin-top: 1rem;
  grid-template-columns: 1fr 1fr;
}

.dashboard-v3 {
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: auto 1fr 1.15fr;
  gap: 1rem;
  min-width: 0;
  min-height: 0;
  height: 100%;
}

/* Las rejillas ceden altura y cada panel scrollea por dentro si hace falta. */
.dashboard-v3 > .dashboard-mid-grid,
.dashboard-v3 > .dashboard-bottom-grid {
  min-height: 0;
}

.panel.dashboard-head-panel {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.42rem 0.7rem;
}

.panel.dashboard-head-panel h2 {
  margin-bottom: 0.12rem;
}

.dashboard-head-copy .hint {
  margin: 0;
}

.dashboard-head-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.dashboard-period {
  display: inline-flex;
  gap: 0.4rem;
}

/* Resumen "Pedidos / Ventas totales / Comisión" del Dashboard: un solo panel
   dividido por rayas finas (no tres tarjetas sueltas). Todo el bloque
   (etiqueta+variación, cifra, detalle) arranca pegado al borde izquierdo de
   su columna — el icono es chico y va en línea junto a la etiqueta, nunca
   como una columna aparte que empuje el texto hacia la derecha y le quite
   ancho al detalle de abajo. */
.summary-kpis {
  display: flex;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--panel);
  overflow: hidden;
  min-width: 0;
}

.summary-kpi {
  flex: 1 1 0%;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  padding: 1rem 1.25rem;
}

.summary-kpi:not(:first-child) {
  border-left: 1px solid var(--border);
}

.summary-kpi-icon {
  font-size: 1rem;
  flex: none;
}

.summary-kpi-top {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  min-width: 0;
}

.summary-kpi-label {
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 600;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.summary-kpi-value {
  color: var(--text);
  line-height: 1.15;
  letter-spacing: -0.02em;
  white-space: nowrap;
}

.summary-kpi-value.s1 { font-size: 2rem; }
.summary-kpi-value.s2 { font-size: 1.65rem; }
.summary-kpi-value.s3 { font-size: 1.35rem; }

.summary-kpi-detail {
  color: var(--muted);
  font-size: 0.78rem;
  line-height: 1.4;
}

.summary-kpi-detail b { color: var(--text); font-weight: 600; }

/* Cada "Etiqueta numero" (ej. "Envío RD$0") no se separa entre lineas — si no
   cabe, el bloque entero pasa a la siguiente linea, nunca deja el numero
   colgando solo abajo de su etiqueta. */
.summary-kpi-detail-item {
  white-space: nowrap;
}

/* Rejilla de 2 filas: la cifra manda y toma todo el ancho que necesite; la etiqueta y
   la variación van arriba, el detalle al costado de la cifra. */
.dashboard-kpi {
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--panel);
  padding: 0.9rem 1rem;
  min-height: 92px;
  min-width: 0;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  grid-template-rows: auto auto;
  align-items: center;
  column-gap: 0.85rem;
  row-gap: 0.3rem;
}

.dashboard-kpi-icon {
  grid-row: 1 / 3;
  width: 44px;
  height: 44px;
  border-radius: 13px;
  background: var(--primary-soft);
  display: grid;
  place-items: center;
  font-size: 1.25rem;
  flex: none;
}

.dashboard-kpi-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  min-width: 0;
}

.dashboard-kpi-label {
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 600;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.dashboard-kpi-delta {
  flex: none;
  font-size: 0.72rem;
  font-weight: 700;
  padding: 0.12rem 0.5rem;
  border-radius: 7px;
}

.dashboard-kpi-delta.up { color: var(--ok); background: rgba(16, 185, 129, 0.14); }
.dashboard-kpi-delta.down { color: var(--danger); background: rgba(239, 68, 68, 0.14); }

.dashboard-kpi-bottom {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.7rem;
  min-width: 0;
}

/* La cifra nunca se comprime: si falta espacio, cede el detalle de al lado. */
.dashboard-kpi strong {
  color: var(--text);
  line-height: 1.05;
  letter-spacing: -0.02em;
  white-space: nowrap;
  flex: 0 0 auto;
}

.dashboard-kpi strong.s1 { font-size: 2rem; }
.dashboard-kpi strong.s2 { font-size: 1.65rem; }
.dashboard-kpi strong.s3 { font-size: 1.35rem; }

.dashboard-kpi-detail {
  color: var(--muted);
  font-size: 0.74rem;
  line-height: 1.45;
  text-align: right;
  flex: 0 1 auto;
  min-width: 0;
}

.dashboard-kpi-detail b { color: var(--text); font-weight: 600; }

.header-title-row {
  display: flex;
  align-items: baseline;
  gap: 0.6rem;
  flex-wrap: wrap;
  margin-top: 0.15rem;
}

.header-title {
  font-size: 1.15rem;
  line-height: 1.2;
}

.header-today {
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 500;
}

.dashboard-mid-grid {
  display: grid;
  /* 40-30-30 arriba contra 30-30-40 abajo: las dos filas suman lo mismo pero sus
     divisiones caen en sitios distintos, y la pantalla deja de verse como una
     cuadricula rigida. Ademas cada panel recibe el ancho que de verdad necesita. */
  grid-template-columns: 40fr 30fr 30fr;
  gap: 1rem;
  min-width: 0;
}
