/* ============================================================
   Toast Sales Analyzer - Stylesheet
   Diseño moderno con Flexbox + CSS Grid, dark/light mode
   ============================================================ */

:root {
  --bg:           #f5f7fb;
  --bg-elev:     #ffffff;
  --bg-soft:     #eef2f9;
  --fg:          #0f172a;
  --fg-muted:    #64748b;
  --primary:     #ff6a3d;
  --primary-700: #d94a1e;
  --accent:      #2563eb;
  --success:     #16a34a;
  --warning:     #ca8a04;
  --danger:      #dc2626;
  --border:      #e2e8f0;
  --shadow:      0 1px 2px rgba(15,23,42,.06), 0 4px 16px rgba(15,23,42,.06);
  --radius:      12px;
  --radius-sm:   8px;
  --header-h:    64px;
  --sidebar-w:   240px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

[data-theme="dark"] {
  --bg:        #0b1220;
  --bg-elev:   #111a2e;
  --bg-soft:   #182241;
  --fg:        #e5e7eb;
  --fg-muted:  #94a3b8;
  --border:    #1f2a44;
  --shadow:    0 1px 2px rgba(0,0,0,.4), 0 4px 16px rgba(0,0,0,.35);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  font-family: var(--font);
  font-size: 14.5px;
  background: var(--bg);
  color: var(--fg);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.login-wrap {
  min-height: 100vh;
  display: grid;
  place-items: center;
  background: radial-gradient(circle at 20% 10%, #ff6a3d33 0%, transparent 40%),
              radial-gradient(circle at 80% 90%, #2563eb33 0%, transparent 45%),
              var(--bg);
  padding: 24px;
}
.login-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 40px 32px;
  width: 100%;
  max-width: 400px;
}
.login-card h1 { margin: 0 0 4px; font-size: 22px; display: flex; align-items: center; gap: 10px; }
.login-card .sub { color: var(--fg-muted); margin-bottom: 24px; font-size: 13px; }

.app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  grid-template-rows: var(--header-h) 1fr;
  grid-template-areas: "side header" "side main";
  min-height: 100vh;
}
.header {
  grid-area: header;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 20px;
  gap: 16px;
  position: sticky;
  top: 0;
  z-index: 10;
}
.header h2 { font-size: 17px; margin: 0; font-weight: 600; }
.header .spacer { flex: 1; }
.user-pill {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 12px;
  background: var(--bg-soft);
  border-radius: 999px;
  font-size: 13px;
}
.user-pill .role-badge {
  background: var(--primary);
  color: #fff;
  border-radius: 6px;
  padding: 2px 8px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .3px;
}

.sidebar {
  grid-area: side;
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  padding: 20px 14px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.sidebar .brand {
  display: flex; align-items: center; gap: 10px;
  padding: 6px 10px 18px;
  font-weight: 700;
  font-size: 16px;
}
.sidebar .brand-mark {
  width: 32px; height: 32px;
  background: linear-gradient(135deg, var(--primary), var(--primary-700));
  border-radius: 8px;
  display: grid; place-items: center; color: #fff;
}
.sidebar nav a {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  color: var(--fg);
  font-weight: 500;
  text-decoration: none;
  transition: background .15s, color .15s;
}
.sidebar nav a:hover { background: var(--bg-soft); }
.sidebar nav a.active {
  background: linear-gradient(135deg, var(--primary), var(--primary-700));
  color: #fff;
  box-shadow: var(--shadow);
}
.sidebar nav a.admin-only::after {
  content: "ADMIN";
  font-size: 9px;
  background: var(--accent);
  color: #fff;
  padding: 2px 5px;
  border-radius: 4px;
  margin-left: auto;
  letter-spacing: .5px;
}

.main { grid-area: main; padding: 24px; overflow-x: hidden; }

.card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
  margin-bottom: 20px;
}
.card-header { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; }
.card-header h3 { margin: 0; font-size: 16px; font-weight: 600; }
.card-header .actions { margin-left: auto; display: flex; gap: 8px; }

.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 16px; }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); }

.kpi {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  box-shadow: var(--shadow);
}
.kpi .label { font-size: 12px; color: var(--fg-muted); text-transform: uppercase; letter-spacing: .5px; }
.kpi .value { font-size: 26px; font-weight: 700; }

label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .4px;
  color: var(--fg-muted);
  margin-bottom: 6px;
}
input[type=text], input[type=email], input[type=password], input[type=number],
input[type=date], input[type=tel], input[type=url], select, textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--fg);
  font-family: inherit;
  font-size: 14px;
  transition: border-color .15s, box-shadow .15s;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(255,106,61,.18);
}
textarea { min-height: 80px; resize: vertical; }
.field { margin-bottom: 14px; }
.row { display: flex; gap: 12px; flex-wrap: wrap; }
.row .field { flex: 1 1 200px; min-width: 0; }

.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 10px 16px;
  border: 1px solid var(--border);
  background: var(--bg-elev);
  color: var(--fg);
  border-radius: var(--radius-sm);
  font-weight: 600;
  font-size: 13.5px;
  cursor: pointer;
  transition: transform .08s, background .15s, border-color .15s;
}
.btn:hover { background: var(--bg-soft); }
.btn:active { transform: translateY(1px); }
.btn-primary { background: linear-gradient(135deg, var(--primary), var(--primary-700)); border-color: transparent; color: #fff; }
.btn-primary:hover { filter: brightness(1.07); }
.btn-accent { background: var(--accent); border-color: transparent; color: #fff; }
.btn-danger { background: var(--danger); border-color: transparent; color: #fff; }
.btn-ghost  { background: transparent; }
.btn-sm     { padding: 6px 10px; font-size: 12.5px; }

.table-wrap { overflow-x: auto; border-radius: var(--radius-sm); }
table.tbl {
  width: 100%;
  border-collapse: collapse;
  font-size: 13.5px;
}
table.tbl th, table.tbl td {
  padding: 10px 12px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
table.tbl th {
  font-weight: 600;
  font-size: 11.5px;
  color: var(--fg-muted);
  text-transform: uppercase;
  letter-spacing: .4px;
  background: var(--bg-soft);
}
table.tbl tr:hover td { background: var(--bg-soft); }
table.tbl td.num, table.tbl th.num { text-align: right; font-variant-numeric: tabular-nums; }
.badge {
  display: inline-block;
  padding: 3px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .3px;
}
.badge-success { background: #dcfce7; color: #166534; }
.badge-warning { background: #fef3c7; color: #92400e; }
.badge-danger  { background: #fee2e2; color: #991b1b; }
.badge-info    { background: #dbeafe; color: #1e40af; }
.badge-muted   { background: var(--bg-soft); color: var(--fg-muted); }

.rank-pill {
  display: inline-flex; align-items: center; justify-content: center;
  width: 28px; height: 28px;
  border-radius: 50%;
  font-weight: 700;
  font-size: 13px;
  color: #fff;
  background: var(--fg-muted);
}
.rank-1 { background: linear-gradient(135deg, #facc15, #ca8a04); }
.rank-2 { background: linear-gradient(135deg, #cbd5e1, #94a3b8); }
.rank-3 { background: linear-gradient(135deg, #f97316, #c2410c); }

.ai-panel {
  background: linear-gradient(135deg, #f0f9ff, #ede9fe);
  border: 1px solid #c7d2fe;
  border-radius: var(--radius);
  padding: 20px 24px;
}
[data-theme="dark"] .ai-panel {
  background: linear-gradient(135deg, #1e3a8a33, #4c1d9533);
  border-color: #3730a3;
}
.ai-panel h3 { margin: 0 0 10px; font-size: 16px; display: flex; gap: 8px; align-items: center; }
.ai-panel ul { padding-left: 22px; margin: 0; }
.ai-panel li { margin: 6px 0; line-height: 1.5; }

.page { display: none; }
.page.active { display: block; animation: fadeIn .25s; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.55);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.modal-backdrop.show { display: flex; }
.modal {
  background: var(--bg-elev);
  border-radius: var(--radius);
  padding: 24px;
  max-width: 760px;
  width: calc(100% - 40px);
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0,0,0,.3);
}
.modal h3 { margin-top: 0; }

.toast-box {
  position: fixed; bottom: 24px; right: 24px;
  display: flex; flex-direction: column; gap: 10px;
  z-index: 200;
}
.toast {
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  background: var(--bg-elev);
  border-left: 4px solid var(--primary);
  box-shadow: var(--shadow);
  font-size: 13px;
  min-width: 260px;
  animation: slideIn .25s;
}
.toast.success { border-left-color: var(--success); }
.toast.error   { border-left-color: var(--danger); }
.toast.warn    { border-left-color: var(--warning); }
@keyframes slideIn { from { transform: translateX(20px); opacity: 0; } to { transform: none; opacity: 1; } }

.muted { color: var(--fg-muted); }
.text-center { text-align: center; }
.text-right  { text-align: right; }
.flex { display: flex; gap: 8px; align-items: center; }
.flex.between { justify-content: space-between; }
.hidden { display: none !important; }
hr.sep { border: 0; border-top: 1px solid var(--border); margin: 18px 0; }

@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; grid-template-areas: "header" "main"; }
  .sidebar {
    position: fixed; top: 0; left: -100%;
    width: 240px; height: 100vh;
    z-index: 50;
    transition: left .25s;
  }
  .sidebar.open { left: 0; box-shadow: 0 0 40px rgba(0,0,0,.3); }
  .menu-toggle { display: inline-flex !important; }
}
.role-badge {
  display: inline-block;
  padding: 3px 8px;
  font-size: 10px;
  border-radius: 999px;
  background: var(--bg-soft);
  color: var(--fg-muted);
  text-transform: uppercase;
  font-weight: 700;
  letter-spacing: .5px;
}
.role-badge.role-admin   { background: #fee2e2; color: #991b1b; }
.role-badge.role-gerente { background: #fef3c7; color: #92400e; }
.role-badge.role-user    { background: #dbeafe; color: #1e40af; }
[data-theme="dark"] .role-badge.role-admin   { background: #7f1d1d33; color: #fca5a5; }
[data-theme="dark"] .role-badge.role-gerente { background: #78350f33; color: #fcd34d; }
[data-theme="dark"] .role-badge.role-user    { background: #1e3a8a33; color: #93c5fd; }

.menu-toggle { display: none; }

/* ============================================================
   SETUP WIZARD — tarjetas de conexión + switches
   ============================================================ */

/* Tarjetas de estado de conexión */
.conn-card {
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  transition: border-color .2s, box-shadow .2s;
}
.conn-card.ok  { border-color: var(--success); box-shadow: 0 0 0 3px rgba(22,163,74,.08); }
.conn-card.bad { border-color: var(--danger);  box-shadow: 0 0 0 3px rgba(220,38,38,.08); }
.conn-head {
  display: flex;
  align-items: center;
  gap: 12px;
}
.conn-head > div { flex: 1; min-width: 0; }
.conn-head strong { display: block; font-size: 14px; }
.conn-icon {
  font-size: 28px;
  line-height: 1;
  width: 44px; height: 44px;
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--bg-elev);
  border-radius: var(--radius-sm);
  flex-shrink: 0;
}
.conn-status { white-space: nowrap; }
.conn-body {
  font-size: 12.5px;
  padding: 8px 10px;
  background: var(--bg-elev);
  border-radius: var(--radius-sm);
  border: 1px dashed var(--border);
  word-break: break-word;
}

/* Filas de switch tipo "iOS" para elegir qué importar */
.switch-row {
  display: flex;
  align-items: flex-start;
  gap: 16px;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-elev);
  margin-bottom: 10px;
  cursor: pointer;
  transition: border-color .15s, background .15s, box-shadow .15s;
  user-select: none;
}
.switch-row:hover { border-color: var(--primary); }
.switch-row:has(input:checked) {
  border-color: var(--primary);
  background: linear-gradient(135deg, rgba(255,106,61,.06), rgba(37,99,235,.04));
}
[data-theme="dark"] .switch-row:has(input:checked) {
  background: linear-gradient(135deg, rgba(255,106,61,.12), rgba(37,99,235,.10));
}
.switch-row.compact { padding: 12px 16px; }
.switch-info { flex: 1; min-width: 0; }
.switch-title {
  font-weight: 600;
  font-size: 14.5px;
  margin-bottom: 4px;
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}
.switch-desc { font-size: 13px; line-height: 1.45; }
.switch-meta {
  margin-top: 8px;
  font-size: 12px;
  padding: 4px 8px;
  background: var(--bg-soft);
  border-radius: 4px;
  display: inline-block;
}

/* Toggle switch */
.switch {
  position: relative;
  display: inline-block;
  width: 46px;
  height: 26px;
  flex-shrink: 0;
  margin-top: 1px;
}
.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}
.switch .slider {
  position: absolute;
  inset: 0;
  cursor: pointer;
  background-color: var(--border);
  transition: background-color .25s;
  border-radius: 26px;
}
.switch .slider:before {
  content: "";
  position: absolute;
  width: 20px;
  height: 20px;
  left: 3px;
  bottom: 3px;
  background: #fff;
  border-radius: 50%;
  transition: transform .25s;
  box-shadow: 0 1px 3px rgba(0,0,0,.2);
}
.switch input:checked + .slider {
  background-color: var(--primary);
}
.switch input:checked + .slider:before {
  transform: translateX(20px);
}
.switch input:focus-visible + .slider {
  box-shadow: 0 0 0 3px rgba(255,106,61,.3);
}

/* Lista de vista previa en el modal */
.preview-list {
  list-style: none;
  padding: 0;
  margin: 12px 0;
}
.preview-list li {
  padding: 10px 14px;
  background: var(--bg-soft);
  border-radius: var(--radius-sm);
  margin-bottom: 8px;
  font-size: 14px;
}

/* Bloques de resultado de importación */
.result-block {
  background: var(--bg-soft);
  border-left: 4px solid var(--success);
  padding: 14px 16px;
  border-radius: var(--radius-sm);
  margin-bottom: 12px;
}
.result-block.error { border-left-color: var(--danger); }
.result-block h4 {
  margin: 0 0 8px;
  font-size: 14px;
}
.result-block ul {
  margin: 0;
  padding-left: 18px;
  font-size: 13.5px;
  line-height: 1.6;
}

@media (max-width: 700px) {
  .switch-row { flex-direction: row; }
  .conn-head { flex-wrap: wrap; }
  .conn-status { order: 3; flex-basis: 100%; }
}

/* ==========================================================
   PWA install button
   ========================================================== */
.pwa-install-btn {
  background: linear-gradient(135deg, var(--primary), var(--primary-700, #d94f2a));
  color: #fff !important;
  border: none;
  font-weight: 600;
  box-shadow: 0 2px 8px rgba(255, 106, 61, .35);
  transition: transform .15s, box-shadow .15s;
}
.pwa-install-btn:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(255, 106, 61, .45);
}
.pwa-install-btn:active { transform: translateY(0); }
.pwa-install-btn:disabled { opacity: .6; cursor: wait; }

/* Instrucciones iOS dentro del modal */
.ios-steps {
  padding-left: 20px;
  line-height: 1.7;
  margin: 12px 0;
}
.ios-steps li { margin-bottom: 8px; }
.ios-icon {
  display: inline-block;
  padding: 1px 6px;
  background: var(--bg-soft);
  border-radius: 4px;
  margin: 0 2px;
  font-size: 14px;
}

/* ==========================================================
   PWA / MOBILE — centrado, safe-area, touch targets
   ========================================================== */

/* Altura dinámica del viewport (mejor que vh con browser chrome variable) */
html, body {
  min-height: 100vh;
  min-height: 100dvh;
  overflow-x: hidden;
}

/* Sin tap-highlight feo en móviles */
* { -webkit-tap-highlight-color: transparent; }

/* Centra el contenido en pantallas anchas sin tocar el sidebar */
.main {
  max-width: 1400px;
  margin: 0 auto;
  width: 100%;
}

/* Inputs sin auto-zoom en iOS (necesita >= 16px) */
input[type="text"],
input[type="email"],
input[type="password"],
input[type="date"],
input[type="number"],
input[type="search"],
select,
textarea {
  font-size: 16px;
}

/* ===== Móvil / tablet en columna única ===== */
@media (max-width: 900px) {

  /* Header respeta notch + dynamic island */
  .header {
    padding-left:  max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right));
    padding-top:   env(safe-area-inset-top);
    gap: 10px;
  }
  .header h2 { font-size: 15px; }

  /* Main: padding reducido + safe-area horizontal */
  .main {
    padding: 14px;
    padding-left:  max(14px, env(safe-area-inset-left));
    padding-right: max(14px, env(safe-area-inset-right));
    padding-bottom: max(14px, env(safe-area-inset-bottom));
  }

  /* Cards más compactas */
  .card {
    padding: 14px;
    margin-bottom: 12px;
    border-radius: 10px;
  }
  .card-header {
    gap: 8px;
    flex-wrap: wrap;
    margin-bottom: 12px;
  }
  .card-header h3 { font-size: 14.5px; }
  .card-header .actions {
    margin-left: 0;
    width: 100%;
    flex-wrap: wrap;
  }
  .card-header .actions .btn { flex: 1 1 auto; }

  /* Sidebar fuera-de-pantalla con respeto a safe-area */
  .sidebar {
    width: min(280px, 85vw);
    padding-top:    max(20px, env(safe-area-inset-top));
    padding-bottom: max(20px, env(safe-area-inset-bottom));
    padding-left:   max(16px, env(safe-area-inset-left));
  }
  .sidebar.open + * + * + .header,
  .sidebar.open ~ .main { /* opcional: backdrop visual */ }

  /* Backdrop cuando el sidebar está abierto */
  .sidebar.open::before {
    content: "";
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.4);
    z-index: -1;
  }

  /* Botones: touch target mínimo 40px */
  .btn         { min-height: 40px; padding-left: 14px; padding-right: 14px; }
  .btn.btn-sm  { min-height: 36px; padding: 6px 12px; font-size: 13px; }

  /* User pill compacta */
  .user-pill { padding: 5px 10px; gap: 6px; font-size: 12px; }

  /* Forms: una columna en móvil */
  .row { display: block; }
  .row .field { width: 100%; margin-bottom: 12px; }

  /* Tablas con scroll horizontal suave (extiende al borde del card) */
  .table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin: 0 -14px;
    padding: 0 14px;
    /* Scroll snap suave en iOS */
    scroll-snap-type: x proximity;
  }
  .tbl { font-size: 13px; }
  .tbl th, .tbl td { padding: 8px 10px; white-space: nowrap; }

  /* KPIs en 2 columnas en lugar de auto-fit con minmax(220px) */
  .grid { grid-template-columns: repeat(2, 1fr); }
  .grid-2 { grid-template-columns: 1fr; }

  /* Toasts no chocan con el notch ni el home indicator */
  .toast-box {
    bottom: max(20px, env(safe-area-inset-bottom));
    left:   max(20px, env(safe-area-inset-left));
    right:  max(20px, env(safe-area-inset-right));
  }

  /* Modal scrolleable y centrado en safe-area */
  .modal { padding: max(16px, env(safe-area-inset-top)) 14px; }
  .modal-card {
    max-height: calc(100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom) - 40px);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* Setup wizard: switches en columna */
  .switch-row { padding: 12px; gap: 12px; }
}

/* ===== Pantallas muy pequeñas (iPhone SE, etc.) ===== */
@media (max-width: 480px) {
  .user-pill .user-name { display: none; }   /* solo el badge del rol */
  .header h2 { font-size: 14px; }
  .main { padding: 10px; }
  .card { padding: 12px; border-radius: 8px; }
  .grid { grid-template-columns: 1fr; }      /* KPIs en una sola columna */

  /* Login centrado total */
  .login-wrap {
    padding: 16px;
    padding-top:    max(16px, env(safe-area-inset-top));
    padding-bottom: max(16px, env(safe-area-inset-bottom));
  }
  .login-card { padding: 28px 20px; max-width: 100%; }
  .login-card h1 { font-size: 19px; }

  /* Botones de acción a ancho completo en wizards/forms */
  .card-header .actions .btn { width: 100%; }
}

/* ===== PWA standalone: ajustes para cuando corre instalado ===== */
@media (display-mode: standalone) {
  /* En PWA instalada, el header sí queda contra el status bar — añadimos
     padding-top extra equivalente al safe-area para que el título no quede
     pegado al notch */
  .header { padding-top: max(0px, env(safe-area-inset-top)); }
  body { user-select: none; -webkit-user-select: none; }
  /* Los inputs sí permiten selección */
  input, textarea, select, .selectable { user-select: text; -webkit-user-select: text; }
}

/* ===== Apaisado en móvil ===== */
@media (max-width: 900px) and (orientation: landscape) {
  .login-wrap { align-items: flex-start; padding-top: 20px; }
}
