/* ============================================
   FLORERÍA BACKOFFICE — Modern Design System 2025
   ============================================
   Features: Container Queries, clamp(), oklch(),
   View Transitions, CSS Layers, scroll-driven
   ============================================ */

@import url('https://fonts.googleapis.com/css2?family=Inter:ital,opsz,wght@0,14..32,300..800;1,14..32,300..800&display=swap');
/* Preconnect for faster font load */
/* Ensure font swap so text is never invisible while Inter loads */

/* ─── CSS Layers (cascade control) ─────────────────── */
@layer reset, base, layout, components, utilities, themes, responsive;

/* ─── Design Tokens ─────────────────────────────────── */
@layer base {
  :root {
    /* ── Color Palette (oklch for better perceptual uniformity) ── */
    --clr-brand: oklch(0.52 0.23 15);
    /* FloresYa crimson */
    --clr-brand-light: oklch(0.62 0.23 15);
    --clr-brand-dark: oklch(0.40 0.23 15);
    --clr-brand-glow: oklch(0.52 0.23 15 / 0.15);

    /* ── Neutrals (dark theme defaults) ── */
    --bg-base: oklch(0.14 0.01 255);
    --bg-surface: oklch(0.17 0.015 255);
    --bg-card: oklch(0.19 0.015 255);
    --bg-elevated: oklch(0.22 0.015 255);
    --border: oklch(0.28 0.015 255 / 0.8);

    /* ── Semantic ── */
    --clr-danger: oklch(0.70 0.19 20);
    --clr-warning: oklch(0.83 0.17 85);
    --clr-success: oklch(0.72 0.17 160);
    --clr-info: oklch(0.72 0.14 250);

    /* ── Text ── */
    --text-primary: oklch(0.97 0.005 250);   /* ↑ slightly brighter primary */
    --text-secondary: oklch(0.75 0.02 250);  /* ↑ clearer secondary */
    --text-muted: oklch(0.62 0.02 250);      /* ↑ was 0.55: WCAG AA safer */

    /* ── Spacing — fluid scale ── */
    --space-xs: clamp(4px, 0.4vw, 6px);
    --space-sm: clamp(8px, 0.8vw, 12px);
    --space-md: clamp(12px, 1.2vw, 20px);
    --space-lg: clamp(20px, 2vw, 32px);
    --space-xl: clamp(28px, 3vw, 48px);

    /* ── Typography — fluid scale ── */
    --text-xs: clamp(11px, 1vw, 12px);       /* ↑ more legible small text */
    --text-sm: clamp(12px, 1.1vw, 13px);
    --text-base: clamp(13px, 1.2vw, 15px);   /* ↑ slightly larger base */
    --text-md: clamp(14px, 1.4vw, 16px);     /* NEW: intermediate step for section subheadings */
    --text-lg: clamp(16px, 1.5vw, 19px);     /* ↑ clearer heading tier */
    --text-xl: clamp(20px, 2vw, 26px);
    --text-2xl: clamp(24px, 3vw, 34px);

    /* ── Shape ── */
    --radius-sm: 6px;
    --radius: 10px;
    --radius-lg: 14px;
    --radius-xl: 20px;

    /* ── Motion ── */
    --ease-spring: cubic-bezier(0.34, 1.56, 0.64, 1);
    --ease-in-out: cubic-bezier(0.4, 0, 0.2, 1);
    --duration-fast: 150ms;
    --duration-normal: 250ms;
    --duration-slow: 400ms;

    /* ── Layout ── */
    --sidebar-w: clamp(200px, 20vw, 240px);

    /* ── Shadows ── */
    --shadow-sm: 0 2px 8px oklch(0 0 0 / 0.3);
    --shadow-md: 0 8px 24px oklch(0 0 0 / 0.35);
    --shadow-lg: 0 20px 60px oklch(0 0 0 / 0.5);

    /* Compat aliases */
    --accent: var(--clr-brand);
    --accent-dark: var(--clr-brand-dark);
    --accent-glow: var(--clr-brand-glow);
    --danger: var(--clr-danger);
    --warning: var(--clr-warning);
    --info: var(--clr-info);
    --transition: var(--duration-normal) var(--ease-in-out);
    --primary: var(--clr-brand);
  }
}

/* ─── Reset ─────────────────────────────────────────── */
@layer reset {

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

  html {
    -webkit-text-size-adjust: 100%;
    scrollbar-width: thin;
    scrollbar-color: var(--bg-elevated) transparent;
    scroll-behavior: smooth;
  }

  ::-webkit-scrollbar {
    width: 6px;
    height: 6px;
  }

  ::-webkit-scrollbar-track {
    background: transparent;
  }

  ::-webkit-scrollbar-thumb {
    background: var(--bg-elevated);
    border-radius: 99px;
  }

  ::-webkit-scrollbar-thumb:hover {
    background: var(--border);
  }

  img,
  video {
    display: block;
    max-width: 100%;
  }

  button {
    font: inherit;
  }

  a {
    color: inherit;
  }
}

/* ─── Base ──────────────────────────────────────────── */
@layer base {
  body {
    font-family: 'Inter', system-ui, sans-serif;
    /* cv02: alternate '1', cv03: alternate '6'/'9', cv04: alt '4', cv11: single-storey 'a', ss01: open digits, ss03: alt 'r' */
    font-feature-settings: "cv02", "cv03", "cv04", "cv11", "ss01", "ss03", "tnum";
    background: var(--bg-base);
    color: var(--text-primary);
    min-height: 100svh;
    font-size: var(--text-base);
    line-height: 1.6;  /* ↑ 1.5 → 1.6 for better long-text readability */
    -webkit-font-smoothing: antialiased;
    text-rendering: optimizeLegibility;
  }

  /* View Transitions API */
  @media (prefers-reduced-motion: no-preference) {
    ::view-transition-old(root) {
      animation: 200ms ease-out both fade-slide-out;
    }

    ::view-transition-new(root) {
      animation: 250ms ease-in both fade-slide-in;
    }
  }
}

/* ─── Login Page ─────────────────────────────────────── */
@layer layout {
  #login-page {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100svh;
    padding: var(--space-md);
    background:
      radial-gradient(ellipse 60% 40% at 65% 35%, oklch(0.52 0.23 15 / 0.12) 0%, transparent 70%),
      radial-gradient(ellipse 40% 60% at 20% 80%, oklch(0.55 0.18 250 / 0.06) 0%, transparent 70%),
      var(--bg-base);
  }

  .login-card {
    container-type: inline-size;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-xl);
    padding: clamp(28px, 6vw, 48px) clamp(20px, 5vw, 40px);
    width: 100%;
    max-width: 420px;
    box-shadow: var(--shadow-lg);
    animation: fadeUp var(--duration-slow) var(--ease-spring);
  }

  .login-logo {
    text-align: center;
    margin-bottom: var(--space-lg);
  }

  .login-logo .logo-icon {
    font-size: 48px;
    display: block;
    margin-bottom: 8px;
    filter: drop-shadow(0 0 16px var(--clr-brand));
  }

  .login-logo .logo-img {
    width: clamp(160px, 50%, 220px);
    object-fit: contain;
    margin: 0 auto 8px;
    border-radius: 10px;
  }

  .login-logo h1 {
    font-size: var(--text-xl);
    font-weight: 700;
    color: var(--text-primary);
  }

  .login-logo p {
    font-size: var(--text-xs);
    color: var(--text-muted);
    margin-top: 4px;
  }
}

/* ─── Forms ──────────────────────────────────────────── */
@layer components {
  .form-group {
    margin-bottom: var(--space-md);
  }

  .form-group label {
    display: block;
    font-size: var(--text-sm);
    font-weight: 500;
    color: var(--text-secondary);
    margin-bottom: 6px;
  }

  .form-group label.required::after {
    content: " *";
    color: var(--clr-brand);
  }

  .form-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: var(--space-md);
  }

  .form-group input,
  .form-group select,
  .form-group textarea,
  .input-field {
    width: 100%;
    padding: 10px 14px;
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text-primary);
    font-family: inherit;
    font-size: var(--text-base);
    outline: none;
    transition:
      border-color var(--duration-fast) var(--ease-in-out),
      box-shadow var(--duration-fast) var(--ease-in-out);
    -webkit-appearance: none;
    appearance: none;
  }

  .form-group input:focus,
  .form-group select:focus,
  .form-group textarea:focus,
  .input-field:focus {
    border-color: var(--clr-brand);
    box-shadow: 0 0 0 3px var(--clr-brand-glow);
  }

  .form-group input::placeholder {
    color: var(--text-muted);
  }

  .form-group select option {
    background: var(--bg-elevated);
  }

  .login-error {
    background: oklch(0.70 0.19 20 / 0.12);
    border: 1px solid oklch(0.70 0.19 20 / 0.3);
    border-radius: var(--radius);
    color: var(--clr-danger);
    padding: 10px 14px;
    font-size: var(--text-sm);
    margin-bottom: var(--space-md);
    display: none;
  }

  /* ── Buttons ── */
  .btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 10px 18px;
    border-radius: var(--radius);
    font-family: inherit;
    font-size: var(--text-sm);
    font-weight: 600;
    cursor: pointer;
    border: 1px solid transparent;
    transition: all var(--duration-normal) var(--ease-in-out);
    text-decoration: none;
    white-space: nowrap;
    user-select: none;
  }

  .btn:active {
    scale: 0.97;
  }

  .btn-primary {
    background: var(--clr-brand);
    color: #fff;
    border-color: var(--clr-brand);
    width: 100%;
  }

  .btn-primary:hover {
    background: var(--clr-brand-light);
    box-shadow: 0 0 20px var(--clr-brand-glow), 0 4px 12px oklch(0 0 0 / 0.25);
  }

  .btn-secondary {
    background: var(--bg-elevated);
    color: var(--text-primary);
    border-color: var(--border);
    width: auto;
  }

  .btn-secondary:hover {
    border-color: var(--clr-brand);
    color: var(--clr-brand);
  }

  .btn-danger {
    background: oklch(0.70 0.19 20 / 0.12);
    color: var(--clr-danger);
    border-color: oklch(0.70 0.19 20 / 0.3);
    width: auto;
  }

  .btn-danger:hover {
    background: oklch(0.70 0.19 20 / 0.22);
  }

  .btn-sm {
    padding: 7px 14px;          /* ↑ better tap target, was 6px/12px */
    font-size: var(--text-xs);
    min-height: 32px;           /* ensure 32px min tap height on mobile */
  }

  .btn-icon {
    padding: 7px;
    border-radius: var(--radius-sm);
    aspect-ratio: 1;
    width: auto;
  }

  /* ── Badges ── */
  .badge {
    display: inline-flex;
    align-items: center;
    padding: 3px 10px;
    border-radius: 99px;
    font-size: var(--text-xs);
    font-weight: 600;
    letter-spacing: 0.02em;
  }

  .badge-material {
    background: oklch(0.83 0.17 85 / 0.12);
    color: var(--clr-warning);
  }

  .badge-salario {
    background: oklch(0.72 0.14 250 / 0.12);
    color: var(--clr-info);
  }

  .badge-renta {
    background: oklch(0.70 0.14 285 / 0.12);
    color: oklch(0.70 0.14 285);
  }

  .badge-operacion {
    background: oklch(0.75 0.18 50 / 0.12);
    color: oklch(0.75 0.18 50);
  }

  .badge-inversion {
    background: oklch(0.72 0.17 160 / 0.12);
    color: var(--clr-success);
  }

  .badge-publicidad {
    background: oklch(0.72 0.18 335 / 0.12);
    color: oklch(0.72 0.18 335);
  }

  .badge-luz {
    background: oklch(0.88 0.18 90 / 0.12);
    color: oklch(0.88 0.18 90);
  }

  .badge-pagina {
    background: oklch(0.72 0.02 250 / 0.12);
    color: var(--text-secondary);
  }

  .badge-default {
    background: oklch(0.55 0.02 250 / 0.12);
    color: var(--text-muted);
  }

  .badge-success {
    background: oklch(0.72 0.17 160 / 0.15);
    color: var(--clr-success);
    border: 1px solid oklch(0.72 0.17 160 / 0.3);
  }

  .badge-pagina {
    background: oklch(0.70 0.14 285 / 0.15);
    color: oklch(0.70 0.14 285);
    border: 1px solid oklch(0.70 0.14 285 / 0.3);
  }
}

/* ─── App Layout ─────────────────────────────────────── */
@layer layout {
  #app {
    display: none;
    min-height: 100svh;
  }

  /* ── Sidebar ── */
  .sidebar {
    position: fixed;
    inset-block: 0;
    inset-inline-start: 0;
    width: var(--sidebar-w);
    height: 100svh;
    background: var(--bg-surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    z-index: 100;
    overflow-y: auto;
    overscroll-behavior: contain;
    transition: transform var(--duration-normal) var(--ease-in-out);
    /* glass effect in supporting browsers */
    backdrop-filter: blur(0);
  }

  .sidebar-header {
    padding: var(--space-lg) var(--space-md) var(--space-md);
    border-bottom: 1px solid var(--border);
  }

  .sidebar-logo {
    display: flex;
    align-items: center;
    gap: 10px;
  }

  .sidebar-logo .logo-img {
    width: clamp(110px, 12vw, 148px);
    object-fit: contain;
    border-radius: var(--radius-sm);
  }

  .sidebar-logo .logo-text {
    font-size: var(--text-base);
    font-weight: 700;
  }

  .sidebar-logo .logo-sub {
    font-size: var(--text-xs);
    color: var(--text-muted);
  }

  .sidebar-nav {
    padding: var(--space-md) var(--space-sm);
    flex: 1;
  }

  .nav-section-title {
    font-size: var(--text-xs);
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.12em;
    color: var(--text-secondary); /* ↑ was text-muted: more visible */
    padding: var(--space-md) 10px var(--space-xs);  /* ↑ more top breathing room */
    opacity: 0.8;
  }

  .nav-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 9px 12px;   /* comfortable tap target */
    border-radius: var(--radius);
    color: var(--text-secondary);
    cursor: pointer;
    transition: all var(--duration-fast) var(--ease-in-out);
    margin-bottom: 3px;  /* ↑ a bit more room between items */
    font-size: var(--text-sm);
    font-weight: 500;
    border: 1px solid transparent;
    position: relative;
  }

  .nav-item:hover {
    background: var(--bg-elevated);
    color: var(--text-primary);
  }

  .nav-item.active {
    background: var(--clr-brand-glow);
    color: var(--clr-brand);
    border-color: oklch(0.52 0.23 15 / 0.25);
  }

  .nav-item.active::before {
    content: '';
    position: absolute;
    inset-block: 20%;
    inset-inline-start: 0;
    width: 4px;                     /* ↑ thicker and more visible */
    background: var(--clr-brand);
    border-radius: 0 4px 4px 0;
    box-shadow: 2px 0 8px var(--clr-brand-glow);
  }

  .nav-item .nav-icon {
    font-size: 16px;
    width: 20px;
    text-align: center;
    flex-shrink: 0;
  }

  .sidebar-footer {
    padding: var(--space-md) var(--space-sm);
    border-top: 1px solid var(--border);
  }

  .user-card {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px;
    border-radius: var(--radius);
    background: var(--bg-elevated);
  }

  .user-avatar {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--clr-brand), var(--clr-brand-dark));
    display: grid;
    place-items: center;
    font-size: var(--text-sm);
    font-weight: 700;
    color: #fff;
    flex-shrink: 0;
  }

  .user-info {
    flex: 1;
    min-width: 0;
  }

  .user-name {
    font-size: var(--text-sm);
    font-weight: 600;
  }

  .user-role {
    font-size: var(--text-xs);
    color: var(--text-muted);
  }

  .btn-logout {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-muted);
    font-size: 16px;
    display: grid;
    place-items: center;
    padding: 4px;
    border-radius: var(--radius-sm);
    transition: color var(--duration-fast) var(--ease-in-out), background var(--duration-fast) var(--ease-in-out);
  }

  .btn-logout:hover {
    color: var(--clr-danger);
    background: oklch(0.70 0.19 20 / 0.1);
  }

  /* ── Main Content ── */
  .main-content {
    margin-left: var(--sidebar-w);
    min-height: 100svh;
    display: flex;
    flex-direction: column;
    width: calc(100% - var(--sidebar-w));
    max-width: 100vw;
    overflow-x: hidden;
  }

  .topbar {
    padding: var(--space-md) var(--space-lg);
    border-bottom: 1px solid var(--border);
    background: oklch(from var(--bg-surface) l c h / 0.85);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    display: flex;
    align-items: center;
    gap: var(--space-md);
    position: sticky;
    top: 0;
    z-index: 10;
    width: 100%;
  }

  .topbar-title {
    font-size: var(--text-lg);
    font-weight: 700;
    letter-spacing: -0.02em;  /* ↑ tighten large title for crispness */
  }

  .topbar-subtitle {
    font-size: var(--text-xs);
    color: var(--text-muted);
    margin-top: 2px;
    font-weight: 400;
  }

  .topbar-actions {
    margin-left: auto;
    display: flex;
    gap: var(--space-sm);
  }

  .page-content {
    padding: var(--space-lg);
    flex: 1;
    width: 100%;
    min-width: 0;
  }

  /* ── Views ── */
  .view {
    display: none;
  }

  .view.active {
    display: block;
    animation: fadeIn var(--duration-normal) var(--ease-in-out);
  }
}

/* ─── KPI Cards ──────────────────────────────────────── */
@layer components {
  .kpi-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(200px, 100%), 1fr));
    gap: var(--space-md);
    margin-bottom: var(--space-lg);
    width: 100%;
  }

  /* Container query makes KPI responsive to its parent, not viewport */
  .kpi-card {
    container-type: inline-size;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: var(--space-md);
    position: relative;
    overflow: hidden;
    transition:
      transform var(--duration-normal) var(--ease-spring),
      box-shadow var(--duration-normal) var(--ease-in-out);
  }

  .kpi-card:hover {
    transform: translateY(-3px);
    box-shadow: 0 12px 32px oklch(0 0 0 / 0.3), 0 0 0 1px var(--border);
  }

  .kpi-card::before {
    content: '';
    position: absolute;
    inset: 0 0 auto 0;
    height: 3px;
    background: linear-gradient(90deg, var(--kpi-color, var(--clr-brand)), transparent 60%);
  }

  /* Subtle glow on hover using kpi-color */
  .kpi-card::after {
    content: '';
    position: absolute;
    inset: 0;
    background: radial-gradient(ellipse 80% 60% at 10% 10%, var(--kpi-color, transparent), transparent 70%);
    opacity: 0;
    transition: opacity var(--duration-normal) var(--ease-in-out);
    pointer-events: none;
  }

  .kpi-card:hover::after {
    opacity: 0.05;
  }

  .kpi-label {
    font-size: var(--text-xs);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.06em;       /* ↓ was 0.08em: less condensed */
    color: var(--text-secondary); /* ↑ was text-muted: higher contrast */
    margin-bottom: 6px;
  }

  .kpi-value {
    font-size: clamp(18px, 4cqi, 26px);
    /* container-query-based fluid size */
    font-weight: 700;
    color: var(--kpi-color, var(--text-primary));
    font-variant-numeric: tabular-nums;
    line-height: 1.1;
  }

  .kpi-icon {
    position: absolute;
    inset-block-start: var(--space-md);
    inset-inline-end: var(--space-md);
    font-size: 24px;              /* ↑ slightly larger */
    opacity: 0.35;                /* ↑ was 0.2: more visible at rest */
    transition: opacity var(--duration-normal) var(--ease-in-out),
                transform var(--duration-normal) var(--ease-spring);
  }

  .kpi-card:hover .kpi-icon {
    opacity: 0.65;                /* ↑ was 0.4 */
    transform: scale(1.12);       /* subtle pop on hover */
  }

  .kpi-sub {
    font-size: var(--text-xs);
    color: var(--text-muted);
    margin-top: 6px;
  }
}

/* ─── Table ──────────────────────────────────────────── */
@layer components {
  .table-container {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    overflow: hidden;
    width: 100%;
  }

  .table-scroll {
    overflow-x: auto;
    overscroll-behavior-x: contain;
    -webkit-overflow-scrolling: touch;
    /* Fade indicator on right edge when content overflows */
    background:
      linear-gradient(to right, var(--bg-card) 10%, transparent) left/24px 100% no-repeat,
      linear-gradient(to left, var(--bg-card) 10%, transparent) right/24px 100% no-repeat,
      linear-gradient(to right, oklch(0.28 0.015 255 / 0.4), transparent) left/24px 100% no-repeat,
      linear-gradient(to left, oklch(0.28 0.015 255 / 0.4), transparent) right/24px 100% no-repeat;
    background-attachment: local, local, scroll, scroll;
  }

  .table-header {
    padding: var(--space-md) var(--space-md);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    flex-wrap: wrap;
  }

  .table-header h3 {
    font-size: var(--text-md);
    font-weight: 700;             /* ↑ bolder section headers */
    letter-spacing: -0.01em;      /* subtle tightening for headings */
  }

  .table-header .spacer {
    flex: 1;
    min-width: var(--space-sm);
  }

  .search-box {
    position: relative;
  }

  .search-box input {
    padding: 8px 12px 8px 34px;
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text-primary);
    font-size: var(--text-sm);
    outline: none;
    width: clamp(160px, 20vw, 220px);
    transition: border-color var(--duration-fast), box-shadow var(--duration-fast);
    font-family: inherit;
  }

  .search-box input:focus {
    border-color: var(--clr-brand);
    box-shadow: 0 0 0 3px var(--clr-brand-glow);
    width: clamp(160px, 25vw, 260px);
  }

  .search-box .search-icon {
    position: absolute;
    left: 10px;
    top: 50%;
    translate: 0 -50%;
    color: var(--text-muted);
    font-size: 14px;
    pointer-events: none;
  }

  .filter-select {
    padding: 8px 12px;
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text-primary);
    font-size: var(--text-sm);
    font-family: inherit;
    outline: none;
    cursor: pointer;
    transition: border-color var(--duration-fast);
    -webkit-appearance: none;
    appearance: none;
  }

  .filter-select:focus {
    border-color: var(--clr-brand);
  }

  .filters-panel {
    display: flex;
    align-items: flex-end;
    gap: var(--space-sm);
    flex-wrap: wrap;
    padding: var(--space-md);
    border-bottom: 1px solid var(--border);
  }

  .filter-group {
    display: flex;
    flex-direction: column;
    gap: 4px;
  }

  .filter-label {
    font-size: var(--text-xs);
    font-weight: 500;
    color: var(--text-muted);
  }

  /* ── Data Table ── */
  .data-table {
    width: 100%;
    border-collapse: collapse;
  }

  .data-table thead th {
    padding: 12px 16px;           /* ↑ from 11px */
    text-align: left;
    font-size: var(--text-xs);
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;       /* ↓ was 0.07em: tight uppercase is harder to read */
    color: var(--text-secondary); /* ↑ was text-muted: more readable headers */
    border-bottom: 1px solid var(--border);
    background: var(--bg-surface);
    white-space: nowrap;
    position: sticky;
    top: 0;
    z-index: 1;
  }

  .data-table .sortable {
    cursor: pointer;
    user-select: none;
  }

  .data-table .sortable:hover {
    color: var(--clr-brand);
  }

  .data-table tbody tr {
    border-bottom: 1px solid oklch(from var(--border) l c h / 0.5);
    transition: background var(--duration-fast) var(--ease-in-out);
  }

  .data-table tbody tr:last-child {
    border-bottom: none;
  }

  .data-table tbody tr:hover {
    background: var(--bg-elevated);
  }

  .data-table tbody td {
    padding: 13px 16px;           /* ↑ was 11px: more comfortable rows */
    font-size: var(--text-sm);
    color: var(--text-primary);
    font-variant-numeric: tabular-nums; /* numbers align in columns */
  }

  .amount {
    font-weight: 600;
    font-variant-numeric: tabular-nums;
  }

  .amount-neg {
    color: var(--clr-danger);
  }

  /* ── Table Utils ── */
  .table-footer {
    padding: var(--space-sm) var(--space-md);
    border-top: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: var(--space-sm);
  }

  .table-info {
    font-size: var(--text-xs);
    color: var(--text-muted);
  }

  .pagination {
    display: flex;
    gap: 4px;
  }

  .page-btn {
    width: 32px;
    height: 32px;
    border-radius: var(--radius-sm);
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    color: var(--text-secondary);
    cursor: pointer;
    font-size: var(--text-sm);
    display: grid;
    place-items: center;
    transition: all var(--duration-fast) var(--ease-in-out);
    font-family: inherit;
  }

  .page-btn:hover {
    border-color: var(--clr-brand);
    color: var(--clr-brand);
  }

  .page-btn.active {
    background: var(--clr-brand);
    color: #fff;
    border-color: var(--clr-brand);
  }

  .page-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
  }

  /* ── States ── */
  .table-empty,
  .table-loading {
    text-align: center;
    padding: clamp(32px, 5vw, 56px) var(--space-md);
    color: var(--text-muted);
  }

  .table-empty .empty-icon {
    font-size: 40px;
    margin-bottom: 12px;
    display: block;
  }

  .spinner {
    width: 28px;
    height: 28px;
    border: 3px solid var(--border);
    border-top-color: var(--clr-brand);
    border-radius: 50%;
    animation: spin 0.7s linear infinite;
    margin: 0 auto 12px;
  }
}

/* ─── Modal ──────────────────────────────────────────── */
@layer components {
  .modal-overlay {
    position: fixed;
    inset: 0;
    background: oklch(0 0 0 / 0.7);
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
    z-index: 200;
    display: grid;
    place-items: center;
    padding: var(--space-md);
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--duration-normal) var(--ease-in-out);
  }

  .modal-overlay.open {
    opacity: 1;
    pointer-events: all;
  }

  .modal {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    width: 100%;
    max-width: 500px;
    box-shadow: var(--shadow-lg);
    translate: 0 20px;
    transition: translate var(--duration-normal) var(--ease-spring);
    max-height: 90svh;
    overflow-y: auto;
    overscroll-behavior: contain;
  }

  .modal-overlay.open .modal {
    translate: 0 0;
  }

  .modal-header {
    padding: var(--space-md) var(--space-lg) var(--space-sm);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    position: sticky;
    top: 0;
    background: var(--bg-card);
    z-index: 1;
  }

  .modal-header h3 {
    font-size: var(--text-base);
    font-weight: 700;
  }

  .modal-close {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-muted);
    font-size: 20px;
    display: grid;
    place-items: center;
    width: 32px;
    height: 32px;
    border-radius: var(--radius-sm);
    transition: all var(--duration-fast) var(--ease-in-out);
    line-height: 1;
    flex-shrink: 0;
  }

  .modal-close:hover {
    color: var(--text-primary);
    background: var(--bg-elevated);
  }

  .modal-body {
    padding: var(--space-md) var(--space-lg);
  }

  .modal-footer {
    padding: var(--space-sm) var(--space-lg) var(--space-md);
    border-top: 1px solid var(--border);
    display: flex;
    justify-content: flex-end;
    gap: var(--space-sm);
    position: sticky;
    bottom: 0;
    background: var(--bg-card);
  }
}

/* ─── Charts ─────────────────────────────────────────── */
@layer components {
  .chart-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(300px, 100%), 1fr));
    gap: var(--space-md);
    width: 100%;
  }

  .chart-card {
    container-type: inline-size;
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: var(--space-md);
    display: flex;
    flex-direction: column;
    min-width: 0;
  }

  .chart-card h3 {
    font-size: var(--text-sm);
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: var(--space-md);
  }

  .chart-wrapper {
    position: relative;
    height: clamp(200px, 30cqi, 280px);
    /* container-query-based height */
    width: 100%;
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
  }
}

/* ─── Global Loader ──────────────────────────────────── */
@layer components {
  .global-loader {
    position: fixed;
    inset: 0;
    background: oklch(0.12 0.02 255);
    z-index: 9999;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    gap: var(--space-md);
  }

  .loader-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0;
    text-align: center;
  }

  .loader-icon {
    font-size: clamp(36px, 5vw, 52px);
    display: inline-block;
    animation: rotateSlow 4s linear infinite;
  }

  .progress-bar-container {
    width: clamp(200px, 40vw, 280px);
    height: 6px;
    background: oklch(1 0 0 / 0.08);
    border-radius: 99px;
    margin-top: var(--space-lg);
    overflow: hidden;
  }

  .progress-bar-fill {
    height: 100%;
    width: 0%;
    background: linear-gradient(90deg, var(--clr-brand), oklch(0.70 0.14 285));
    border-radius: 99px;
    transition: width 0.4s var(--ease-in-out);
  }
}

/* ─── Toast Notifications ────────────────────────────── */
@layer components {
  #toast-container {
    position: fixed;
    bottom: var(--space-lg);
    right: var(--space-lg);
    z-index: 9999;
    display: flex;
    flex-direction: column;
    gap: var(--space-sm);
    pointer-events: none;
  }

  .toast {
    pointer-events: all;
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 11px 16px;
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: var(--text-sm);
    min-width: clamp(240px, 30vw, 300px);
    max-width: 340px;
    box-shadow: var(--shadow-md);
    animation: slideInRight var(--duration-normal) var(--ease-spring) both;
  }

  .toast-success {
    border-left: 3px solid var(--clr-brand);
  }

  .toast-error {
    border-left: 3px solid var(--clr-danger);
  }

  .toast-info {
    border-left: 3px solid var(--clr-info);
  }

  .toast-icon {
    font-size: 18px;
  }
}

/* ─── Controls ───────────────────────────────────────── */
@layer components {
  .hamburger {
    display: none;
    align-items: center;
    justify-content: center;
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-primary);
    font-size: 22px;
    padding: 6px;
    border-radius: var(--radius-sm);
    transition: background var(--duration-fast);
    flex-shrink: 0;
    line-height: 1;
  }

  .hamburger:hover {
    background: var(--bg-elevated);
  }

  .theme-toggle {
    background: none;
    border: 1px solid var(--border);
    cursor: pointer;
    color: var(--text-secondary);
    font-size: 17px;
    padding: 6px 10px;
    border-radius: var(--radius-sm);
    transition: all var(--duration-fast);
    flex-shrink: 0;
    line-height: 1;
    font-family: inherit;
  }

  .theme-toggle:hover {
    border-color: var(--clr-brand);
    color: var(--clr-brand);
  }

  .sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: oklch(0 0 0 / 0.6);
    z-index: 99;
    backdrop-filter: blur(3px);
    -webkit-backdrop-filter: blur(3px);
  }

  body.sidebar-open .sidebar-overlay {
    display: block;
  }
}

/* ─── Dashboard Filters ──────────────────────────────── */
@layer components {
  .filter-row {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: var(--space-sm);
    margin-bottom: var(--space-md);
    padding: var(--space-sm) var(--space-md);
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    width: 100%;
  }

  .filter-row h3 {
    font-size: var(--text-base);
    font-weight: 700;
    display: flex;
    align-items: center;
    gap: 8px;
  }
}

/* ─── Animations ─────────────────────────────────────── */
@layer utilities {
  @keyframes fadeUp {
    from {
      opacity: 0;
      translate: 0 16px;
    }

    to {
      opacity: 1;
      translate: 0 0;
    }
  }

  @keyframes fadeIn {
    from {
      opacity: 0;
    }

    to {
      opacity: 1;
    }
  }

  @keyframes slideInRight {
    from {
      opacity: 0;
      translate: 20px 0;
    }

    to {
      opacity: 1;
      translate: 0 0;
    }
  }

  @keyframes spin {
    to {
      rotate: 360deg;
    }
  }

  @keyframes rotateSlow {
    to {
      rotate: 360deg;
    }
  }

  @keyframes fade-slide-out {
    to {
      opacity: 0;
      translate: -8px 0;
    }
  }

  @keyframes fade-slide-in {
    from {
      opacity: 0;
      translate: 8px 0;
    }
  }
}

/* ─── Light Theme ────────────────────────────────────── */
@layer themes {
  [data-theme="light"] {
    --bg-base: oklch(0.98 0.003 255);
    --bg-surface: oklch(1 0 0);
    --bg-card: oklch(1 0 0);
    --bg-elevated: oklch(0.96 0.005 255);
    --border: oklch(0.90 0.005 255);
    --text-primary: oklch(0.20 0.02 255);
    --text-secondary: oklch(0.40 0.02 255);
    --text-muted: oklch(0.55 0.015 255);  /* light theme: darker muted text for WCAG contrast */
    --clr-brand-glow: oklch(0.52 0.23 15 / 0.10);
    --shadow-sm: 0 2px 8px oklch(0 0 0 / 0.08);
    --shadow-md: 0 8px 24px oklch(0 0 0 / 0.1);
    --shadow-lg: 0 20px 60px oklch(0 0 0 / 0.15);
  }

  [data-theme="light"] .login-card {
    box-shadow: var(--shadow-lg);
  }

  [data-theme="light"] .sidebar {
    box-shadow: 2px 0 16px oklch(0 0 0 / 0.07);
  }

  [data-theme="light"] .global-loader {
    background: oklch(0.97 0.003 255);
  }

  [data-theme="light"] .global-loader h2,
  [data-theme="light"] #global-loader-text {
    color: var(--text-primary) !important;
  }

  [data-theme="light"] .topbar {
    background: oklch(1 0 0 / 0.9);
  }

  [data-theme="light"] .data-table tbody tr:hover {
    background: oklch(0.96 0.003 255);
  }
}

/* ─── Responsive ─────────────────────────────────────── */
@layer responsive {

  /* ── Tablet / Small desktop ≤ 1024px ── */
  @media (max-width: 1024px) {
    .page-content {
      padding: var(--space-md);
    }

    .chart-grid {
      grid-template-columns: repeat(auto-fit, minmax(min(280px, 100%), 1fr));
    }
  }

  /* ── Mobile ≤ 768px ──────────── */
  @media (max-width: 768px) {
    :root {
      --sidebar-w: 280px;
    }

    .hamburger {
      display: flex;
      min-width: 44px;
      min-height: 44px;
      font-size: 24px;
    }

    /* Sidebar off-canvas drawer */
    .sidebar {
      transform: translateX(-100%);
      z-index: 200;
      box-shadow: none;
      width: var(--sidebar-w);
      transition: transform var(--duration-normal) var(--ease-in-out);
    }

    body.sidebar-open .sidebar {
      transform: translateX(0);
      box-shadow: var(--shadow-lg);
    }

    .sidebar-overlay {
      transition: opacity var(--duration-normal) var(--ease-in-out);
      opacity: 0;
    }

    body.sidebar-open .sidebar-overlay {
      opacity: 1;
    }

    /* Full-width content */
    .main-content {
      margin-left: 0;
      width: 100%;
      max-width: 100vw;
    }

    .topbar {
      padding: 10px var(--space-md);
      gap: var(--space-sm);
    }

    .topbar-subtitle {
      display: none;
    }

    .topbar-title {
      font-size: var(--text-base);
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }

    .topbar-actions {
      gap: 4px;
    }

    .topbar-actions .btn,
    .topbar-actions .theme-toggle {
      min-width: 40px;
      min-height: 40px;
      padding: 6px 8px;
    }

    /* ── KPIs ── */
    .kpi-grid {
      grid-template-columns: repeat(auto-fit, minmax(min(140px, 100%), 1fr));
      gap: var(--space-sm);
    }

    .kpi-card {
      padding: 12px 14px;
    }

    .kpi-card:hover {
      transform: none;
      /* Disable hover on mobile to avoid stuck states */
    }

    .kpi-label {
      font-size: 10px;
      margin-bottom: 4px;
    }

    .kpi-value {
      font-size: clamp(16px, 4.5vw, 22px);
    }

    .kpi-sub {
      font-size: 10px;
      margin-top: 4px;
    }

    .kpi-icon {
      font-size: 18px;
      opacity: 0.15;
    }

    /* ── Filters Panel ── */
    .filters-panel {
      flex-direction: column;
      align-items: stretch;
      padding: var(--space-sm);
      gap: var(--space-sm);
    }

    .filters-panel .filter-group {
      width: 100%;
    }

    .filters-panel .btn {
      width: 100%;
      min-height: 44px;
      font-size: var(--text-base);
    }

    .search-box,
    .search-box input {
      width: 100% !important;
    }

    .search-box input:focus {
      width: 100% !important;
    }

    .filter-select {
      width: 100%;
      min-height: 40px;
    }

    /* ── Filter Row (Dashboard) ── */
    .filter-row {
      flex-direction: column;
      align-items: stretch;
      gap: var(--space-sm);
      padding: var(--space-sm);
    }

    .filter-row h3 {
      font-size: var(--text-sm);
    }

    .filter-row .filter-select {
      width: 100%;
    }

    /* ── Table ── */
    .table-container {
      border-radius: var(--radius);
    }

    .table-scroll {
      -webkit-overflow-scrolling: touch;
    }

    .data-table thead th {
      padding: 8px 10px;
      font-size: 10px;
      letter-spacing: 0.04em;
    }

    .data-table tbody td {
      padding: 10px;
      font-size: var(--text-sm);
    }

    .data-table .btn-icon {
      min-width: 36px;
      min-height: 36px;
      font-size: 16px;
    }

    /* Stack table-header controls */
    .table-header {
      flex-direction: column;
      align-items: stretch;
    }

    .table-header h3 {
      font-size: var(--text-base);
    }

    .table-header .spacer {
      display: none;
    }

    .table-header .btn {
      width: 100%;
      min-height: 44px;
    }

    /* ── Table Footer / Pagination ── */
    .table-footer {
      flex-direction: column;
      align-items: center;
      gap: var(--space-sm);
      padding: var(--space-sm);
    }

    .page-btn {
      min-width: 36px;
      min-height: 36px;
    }

    /* ── Charts ── */
    .chart-grid {
      grid-template-columns: 1fr;
      gap: var(--space-sm);
    }

    .chart-card {
      padding: var(--space-sm);
    }

    .chart-card h3 {
      font-size: var(--text-sm);
    }

    .chart-wrapper {
      height: clamp(180px, 40vw, 240px);
    }

    /* ── Login ── */
    .login-card {
      padding: 24px 18px;
    }

    /* ── Modals ── */
    .modal {
      max-width: calc(100vw - 16px);
      max-height: 90svh;
      border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    }

    .modal-overlay {
      align-items: flex-end;
      padding: 0;
    }

    .modal-overlay.open .modal {
      translate: 0 0;
    }

    .modal-header {
      padding: var(--space-sm) var(--space-md);
    }

    .modal-body {
      padding: var(--space-sm) var(--space-md);
    }

    .modal-footer {
      padding: var(--space-sm) var(--space-md);
      flex-direction: column;
    }

    .modal-footer .btn {
      width: 100%;
      min-height: 44px;
    }

    /* ── Form ── */
    .form-row {
      grid-template-columns: 1fr;
      gap: var(--space-sm);
    }

    .form-group input,
    .form-group select,
    .form-group textarea {
      min-height: 42px;
      font-size: 16px;
      /* Prevents iOS zoom on focus */
    }

    /* ── Toast ── */
    #toast-container {
      bottom: var(--space-md);
      left: var(--space-sm);
      right: var(--space-sm);
    }

    .toast {
      min-width: unset;
      max-width: 100%;
    }

    /* ── Buttons touch-friendly ── */
    .btn {
      min-height: 42px;
      padding: 10px 16px;
    }

    .btn-icon {
      min-width: 40px;
      min-height: 40px;
    }

    /* ── Proyecciones specific ── */
    .chart-card[style*="grid-column"] {
      grid-column: 1 / -1 !important;
    }

    /* ── Estado Mensual & Edo. Semana — Mobile Tables ── */
    #view-estado-mensual .filter-row,
    #view-edo-semana .filter-row {
      flex-direction: column;
      align-items: stretch;
      gap: var(--space-sm);
    }

    #view-estado-mensual .filter-row>div,
    #view-edo-semana .filter-row>div {
      margin-left: 0;
      width: 100%;
    }

    #view-edo-semana .filter-row>div {
      flex-direction: column;
    }

    #view-estado-mensual .filter-row h3,
    #view-edo-semana .filter-row h3 {
      font-size: var(--text-sm);
      margin: 0;
    }

    /* Sticky first column for accounting tables */
    #view-estado-mensual .data-table,
    #view-edo-semana .data-table {
      font-size: 11px;
    }

    #view-estado-mensual .data-table thead th:first-child,
    #view-estado-mensual .data-table tbody td:first-child,
    #view-edo-semana .data-table thead th:first-child,
    #view-edo-semana .data-table tbody td:first-child {
      position: sticky;
      left: 0;
      z-index: 2;
      background: var(--bg-card);
      box-shadow: 2px 0 8px rgba(0, 0, 0, .15);
    }

    #view-estado-mensual .data-table thead th:first-child,
    #view-edo-semana .data-table thead th:first-child {
      background: var(--bg-elevated);
      z-index: 3;
    }

    #view-estado-mensual .data-table thead th,
    #view-edo-semana .data-table thead th {
      padding: 6px 8px;
      font-size: 9px;
      white-space: nowrap;
    }

    #view-estado-mensual .data-table tbody td,
    #view-edo-semana .data-table tbody td {
      padding: 8px 6px;
      font-size: 11px;
      white-space: nowrap;
    }

    /* Scroll hint indicator */
    #view-estado-mensual .table-scroll,
    #view-edo-semana .table-scroll {
      position: relative;
    }

    #view-estado-mensual .table-scroll::after,
    #view-edo-semana .table-scroll::after {
      content: '← Deslizar →';
      position: absolute;
      top: 0;
      right: 8px;
      font-size: 10px;
      color: var(--text-muted);
      background: var(--bg-card);
      padding: 4px 8px;
      border-radius: 0 0 6px 6px;
      opacity: 0.7;
      pointer-events: none;
      z-index: 5;
    }

    /* Badge smaller on mobile */
    #view-edo-semana .badge {
      font-size: 10px;
      padding: 2px 6px;
    }

    /* ── Network Error Overlay ── */
    .network-error-overlay .modal {
      margin: var(--space-md);
    }
  }

  /* ── Small mobile ≤ 480px ── */
  @media (max-width: 480px) {
    .page-content {
      padding: 10px;
    }

    /* KPIs stack in 1 column on very small screens */
    .kpi-grid {
      grid-template-columns: 1fr 1fr;
      gap: 8px;
    }

    .kpi-card {
      padding: 10px 12px;
    }

    .kpi-value {
      font-size: clamp(14px, 4vw, 18px);
    }

    .kpi-label {
      font-size: 9px;
    }

    .kpi-sub {
      font-size: 9px;
    }

    .kpi-icon {
      font-size: 14px;
    }

    /* Topbar even tighter */
    .topbar {
      padding: 8px 10px;
    }

    .topbar-title {
      font-size: 13px;
    }

    /* Table cells tighter */
    .data-table thead th {
      padding: 6px 8px;
      font-size: 9px;
    }

    .data-table tbody td {
      padding: 8px;
      font-size: 12px;
    }

    /* Charts smaller */
    .chart-wrapper {
      height: clamp(160px, 35vw, 200px);
    }

    /* Search input no overflow */
    .search-box input,
    .search-box input:focus {
      width: 100% !important;
    }

    /* Sidebar slightly narrower */
    :root {
      --sidebar-w: 250px;
    }
  }

  /* ── Very small ≤ 360px ── */
  @media (max-width: 360px) {
    .kpi-grid {
      grid-template-columns: 1fr;
    }

    .topbar-title {
      font-size: 12px;
    }

    .modal {
      max-width: 100vw;
    }
  }

  /* ── Safe area insets (notched phones) ── */
  @supports (padding: env(safe-area-inset-bottom)) {
    @media (max-width: 768px) {
      .sidebar {
        padding-bottom: env(safe-area-inset-bottom);
      }

      .modal-footer {
        padding-bottom: calc(var(--space-md) + env(safe-area-inset-bottom));
      }

      #toast-container {
        bottom: calc(var(--space-md) + env(safe-area-inset-bottom));
      }
    }
  }

  /* ── Touch-specific: disable hover effects on touch devices ── */
  @media (hover: none) {
    .kpi-card:hover {
      transform: none;
      box-shadow: none;
    }

    .kpi-card:hover::after {
      opacity: 0;
    }

    .data-table tbody tr:hover {
      background: transparent;
    }

    .nav-item:hover {
      background: transparent;
    }

    .nav-item:active {
      background: var(--bg-elevated);
    }

    .btn:active {
      scale: 0.97;
    }
  }

  /* ── Landscape mobile ── */
  @media (max-height: 500px) and (orientation: landscape) {
    .kpi-grid {
      grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
    }

    .chart-wrapper {
      height: 180px;
    }

    .modal {
      max-height: 95svh;
    }
  }

  /* ── Print ── */
  @media print {

    .sidebar,
    .hamburger,
    .sidebar-overlay,
    .topbar-actions,
    #toast-container,
    .global-loader,
    .modal-overlay {
      display: none !important;
    }

    .main-content {
      margin-left: 0 !important;
    }

    .data-table thead th,
    .data-table tbody td {
      color: #000 !important;
      border-color: #ccc !important;
    }
  }
}

/* ─── Sidebar: Collapsible Nav Groups ────────────────── */
@layer components {
  .nav-group-toggle {
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: var(--text-xs);
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.1em;
    color: var(--text-muted);
    padding: var(--space-sm) 10px var(--space-xs);
    cursor: pointer;
    user-select: none;
    border-radius: var(--radius-sm);
    transition: background var(--duration-fast) var(--ease-in-out), color var(--duration-fast) var(--ease-in-out);
    margin-top: 4px;
  }

  .nav-group-toggle:hover {
    background: var(--bg-elevated);
    color: var(--text-primary);
  }

  .nav-group-toggle .chevron {
    font-size: 10px;
    transition: transform var(--duration-normal) var(--ease-spring);
    opacity: 0.5;
  }

  .nav-group.collapsed .chevron {
    transform: rotate(-90deg);
  }

  .nav-group-items {
    overflow: hidden;
    max-height: 600px;
    transition: max-height var(--duration-slow) var(--ease-in-out), opacity var(--duration-normal) var(--ease-in-out);
    opacity: 1;
  }

  .nav-group.collapsed .nav-group-items {
    max-height: 0;
    opacity: 0;
  }
}

/* ─── Modern Date Filter Toolbar ─────────────────────── */
@layer components {
  .filter-toolbar {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 14px 18px;
    margin-bottom: 20px;
  }

  .filter-toolbar-label {
    font-size: var(--text-xs);
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--text-muted);
    white-space: nowrap;
    margin-right: 4px;
  }

  .filter-toolbar .divider {
    width: 1px;
    height: 24px;
    background: var(--border);
    flex-shrink: 0;
  }

  .filter-select-pill {
    appearance: none;
    -webkit-appearance: none;
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: 99px;
    color: var(--text-primary);
    font-family: inherit;
    font-size: var(--text-sm);
    font-weight: 500;
    padding: 6px 32px 6px 14px;
    cursor: pointer;
    outline: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='%2394a3b8'%3E%3Cpath d='M4.5 6L8 9.5 11.5 6'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 10px center;
    background-size: 14px;
    transition: border-color var(--duration-fast) var(--ease-in-out), box-shadow var(--duration-fast) var(--ease-in-out);
    min-width: 110px;
  }

  .filter-select-pill:focus {
    border-color: var(--clr-brand);
    box-shadow: 0 0 0 3px var(--clr-brand-glow);
  }

  .filter-select-pill.active {
    border-color: var(--clr-brand);
    background-color: var(--clr-brand-glow);
    color: var(--clr-brand);
  }

  .filter-reset-btn {
    margin-left: auto;
    background: none;
    border: 1px solid transparent;
    border-radius: 99px;
    color: var(--text-muted);
    font-size: var(--text-xs);
    cursor: pointer;
    padding: 5px 12px;
    font-family: inherit;
    font-weight: 500;
    transition: all var(--duration-fast) var(--ease-in-out);
    white-space: nowrap;
  }

  .filter-reset-btn:hover {
    color: var(--clr-brand);
    border-color: var(--clr-brand);
    background: var(--clr-brand-glow);
  }
}

/* ─── KPI title/subtitle aliases ─────────────────────── */
@layer components {
  .kpi-title {
    font-size: var(--text-xs);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--text-muted);
    margin-bottom: 6px;
  }

  .kpi-subtitle {
    font-size: var(--text-xs);
    color: var(--text-muted);
    margin-top: 6px;
  }
}

/* ─── PWA / iOS Safe Area ─────────────────────────────── */
@supports (padding-top: env(safe-area-inset-top)) {

  /* Topbar respeta el notch / Dynamic Island */
  .topbar {
    padding-top: calc(10px + env(safe-area-inset-top));
  }

  /* Sidebar también necesita el top safe area */
  .sidebar {
    padding-top: env(safe-area-inset-top);
  }

  /* Contenido no quede detrás del bottom bar del iPhone */
  .main-content {
    padding-bottom: env(safe-area-inset-bottom);
  }

  /* En móvil la topbar se ajusta al tamaño del notch */
  @media (max-width: 768px) {
    .topbar {
      padding-top: calc(8px + env(safe-area-inset-top));
    }
  }

  @media (max-width: 480px) {
    .topbar {
      padding-top: calc(6px + env(safe-area-inset-top));
    }
  }
}

/* Zonas period pill buttons */
.zonas-period-btn.active {
  background: var(--accent) !important;
  color: #fff !important;
  border-color: var(--accent) !important;
}

/* ─── Table: Sticky First Column (all data tables on mobile) ──────────── */
/* Applied globally — first col sticks when table scrolls horizontally     */
@layer responsive {

  /* ── Tablet 769–1024: medium density tuning ── */
  @media (min-width: 769px) and (max-width: 1024px) {
    .data-table thead th {
      padding: 10px 12px;
      font-size: var(--text-xs);
    }

    .data-table tbody td {
      padding: 11px 12px;
      font-size: var(--text-sm);
    }

    /* kpi-container inside cards (Zonas, Notificaciones) tighter on tablet */
    .kpi-container {
      gap: var(--space-sm);
    }
  }

  /* ── Mobile ≤ 768px — table enhancements ── */
  @media (max-width: 768px) {

    /* ──────────────────────────────────────────
       kpi-container: make it behave like kpi-grid
       (used in Zonas, Notificaciones views)
    ─────────────────────────────────────────── */
    .kpi-container {
      grid-template-columns: repeat(auto-fit, minmax(min(140px, 100%), 1fr)) !important;
      gap: var(--space-sm) !important;
    }

    /* ──────────────────────────────────────────
       GLOBAL sticky first column for all tables
    ─────────────────────────────────────────── */
    .data-table thead th:first-child,
    .data-table tbody td:first-child {
      position: sticky;
      left: 0;
      z-index: 2;
      background: inherit;       /* inherit from tr/thead bg */
    }

    /* Override background for thead specifically */
    .data-table thead th:first-child {
      background: var(--bg-surface);
      z-index: 3;
      box-shadow: 2px 0 6px oklch(0 0 0 / 0.2);
    }

    .data-table tbody td:first-child {
      background: var(--bg-card);
      box-shadow: 2px 0 6px oklch(0 0 0 / 0.15);
    }

    /* Row hover: keep first cell background in sync */
    .data-table tbody tr:hover td:first-child {
      background: var(--bg-elevated);
    }

    /* ──────────────────────────────────────────
       GLOBAL horizontal scroll hint for all tables
    ─────────────────────────────────────────── */
    .table-scroll,
    .table-wrapper {
      position: relative;
    }

    .table-scroll::after,
    .table-wrapper::after {
      content: '← Deslizar →';
      position: absolute;
      top: 0;
      right: 6px;
      font-size: 9px;
      color: var(--text-muted);
      background: var(--bg-card);
      padding: 3px 8px;
      border-radius: 0 0 6px 6px;
      border: 1px solid var(--border);
      border-top: none;
      opacity: 0.75;
      pointer-events: none;
      z-index: 5;
      white-space: nowrap;
      letter-spacing: 0.03em;
    }

    /* Hide hint if table is narrow enough not to scroll (JS progressive enhancement) */
    .table-scroll.no-overflow::after,
    .table-wrapper.no-overflow::after {
      display: none;
    }

    /* ──────────────────────────────────────────
       Zonas: period filter pills scroll horizontally
       instead of wrapping onto multiple lines
    ─────────────────────────────────────────── */
    #view-zonas .table-header > div {
      width: 100%;
    }

    #view-zonas [style*="display:flex"][style*="gap:8px"] {
      overflow-x: auto;
      flex-wrap: nowrap !important;
      padding-bottom: 6px;
      -webkit-overflow-scrolling: touch;
      scrollbar-width: none; /* hide scrollbar on mobile */
    }

    #view-zonas [style*="display:flex"][style*="gap:8px"]::-webkit-scrollbar {
      display: none;
    }

    /* Zonas month selector full-width on mobile */
    #zonas-month {
      width: 100% !important;
      min-height: 38px;
    }

    /* ──────────────────────────────────────────
       View-specific: Zonas tables
    ─────────────────────────────────────────── */
    #view-zonas .data-table {
      font-size: 12px;
    }

    #view-zonas .data-table thead th {
      padding: 7px 8px;
      font-size: 9px;
      white-space: nowrap;
    }

    #view-zonas .data-table tbody td {
      padding: 9px 8px;
      white-space: nowrap;
    }

    /* Map full width on mobile */
    #zonas-map {
      height: 280px !important;
    }

    /* Top productos & best routes: 2-col grid on mobile */
    #top-productos-container {
      grid-template-columns: 1fr 1fr !important;
      gap: 8px !important;
      padding: 10px !important;
    }

    #best-routes-container {
      grid-template-columns: 1fr 1fr !important;
      gap: 8px !important;
    }

    /* ──────────────────────────────────────────
       View-specific: Notificaciones
    ─────────────────────────────────────────── */
    #view-notificaciones .data-table {
      font-size: 12px;
      min-width: 620px; /* force scroll on narrow screens */
    }

    #view-notificaciones .data-table thead th {
      padding: 7px 8px;
      font-size: 9px;
      white-space: nowrap;
    }

    #view-notificaciones .data-table tbody td {
      padding: 9px 8px;
      font-size: 12px;
      white-space: nowrap;
    }

    /* ──────────────────────────────────────────
       View-specific: Códigos catalog
    ─────────────────────────────────────────── */
    #view-codigos .data-table {
      font-size: 12px;
      min-width: 600px;
    }

    #view-codigos .data-table thead th {
      padding: 7px 8px;
      font-size: 9px;
      white-space: nowrap;
    }

    #view-codigos .data-table tbody td {
      padding: 9px 8px;
      white-space: nowrap;
    }

    /* ──────────────────────────────────────────
       View-specific: Marketing / Google Ads table
    ─────────────────────────────────────────── */
    #view-marketing .data-table {
      min-width: 700px;
    }

    #view-marketing .data-table thead th,
    #view-marketing .data-table tbody td {
      padding: 7px 10px;
      font-size: 11px;
      white-space: nowrap;
    }

    /* ──────────────────────────────────────────
       Table header actions: full-width on mobile
    ─────────────────────────────────────────── */
    .table-header {
      gap: 8px;
    }

    /* Buttons inside table headers that are NOT the primary action
       become full-width on narrow screens */
    .table-header .btn-secondary {
      flex: 1;
      min-height: 40px;
    }
  }

  /* ── Small mobile ≤ 480px ── */
  @media (max-width: 480px) {

    /* kpi-container: 2-column grid like kpi-grid */
    .kpi-container {
      grid-template-columns: 1fr 1fr !important;
    }

    /* Zonas productos/routes single col on very small */
    #top-productos-container,
    #best-routes-container {
      grid-template-columns: 1fr !important;
    }

    /* Very small: even tighter table cells */
    .data-table thead th {
      padding: 6px 6px;
      font-size: 9px;
    }

    .data-table tbody td {
      padding: 8px 6px;
      font-size: 11px;
    }
  }
}

/* ─── Light theme: sticky column background fix ───────── */
[data-theme="light"] .data-table thead th:first-child {
  background: var(--bg-elevated);
}

[data-theme="light"] .data-table tbody td:first-child {
  background: var(--bg-card);
}

[data-theme="light"] .data-table tbody tr:hover td:first-child {
  background: oklch(0.96 0.003 255);
}

/* ─── Utilidades de layout ──────────────────────────────── */
@layer utilities {

  /* Flex helpers */
  .flex-between  { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: var(--space-md); }
  .flex-center   { display: flex; align-items: center; justify-content: center; }
  .flex-gap-sm   { display: flex; align-items: center; gap: var(--space-sm); }
  .flex-gap-md   { display: flex; align-items: center; gap: var(--space-md); }

  /* Page header estándar (usado en Zonas, Notificaciones, Productos, etc.) */
  .page-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 12px;
    margin-bottom: 20px;
  }
  .page-header h2 {
    margin: 0;
    font-size: var(--text-lg);
    font-weight: 700;
    letter-spacing: -0.02em;
  }
  .page-header p {
    margin: 4px 0 0;
    font-size: var(--text-xs);
    color: var(--text-muted);
  }

  /* Card de contenido con borde lateral de color */
  .border-success { border-left: 4px solid var(--clr-success) !important; }
  .border-danger  { border-left: 4px solid var(--clr-danger)  !important; }
  .border-warning { border-left: 4px solid var(--clr-warning) !important; }
  .border-brand   { border-left: 4px solid var(--clr-brand)   !important; }

  /* Chip de estado inline */
  .status-chip {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    border-radius: 99px;
    padding: 3px 10px;
    font-size: 11px;
    font-weight: 600;
  }
  .status-chip--success {
    background: oklch(0.72 0.17 160 / 0.15);
    border: 1px solid oklch(0.72 0.17 160 / 0.3);
    color: var(--clr-success);
  }
  .status-chip--warning {
    background: oklch(0.83 0.17 85 / 0.12);
    border: 1px solid oklch(0.83 0.17 85 / 0.3);
    color: var(--clr-warning);
  }
  .status-chip--info {
    background: oklch(0.72 0.14 250 / 0.12);
    border: 1px solid oklch(0.72 0.14 250 / 0.3);
    color: var(--clr-info);
  }
  .status-chip--danger {
    background: oklch(0.70 0.19 20 / 0.12);
    border: 1px solid oklch(0.70 0.19 20 / 0.3);
    color: var(--clr-danger);
  }

  /* ─── Skeleton loading ──────────────────────────────────── */
  .skeleton {
    background: linear-gradient(
      90deg,
      var(--bg-elevated) 25%,
      oklch(0.25 0.015 255) 50%,
      var(--bg-elevated) 75%
    );
    background-size: 200% 100%;
    animation: skeleton-shimmer 1.5s infinite;
    border-radius: var(--radius-sm);
  }

  @keyframes skeleton-shimmer {
    0%   { background-position: 200% 0; }
    100% { background-position: -200% 0; }
  }

  .skeleton-text {
    height: 14px;
    margin-bottom: 8px;
    border-radius: var(--radius-sm);
  }
  .skeleton-text--lg  { height: 24px; width: 60%; }
  .skeleton-text--sm  { height: 11px; width: 40%; }
  .skeleton-kpi       { height: 40px; width: 80%; margin: 8px auto 0; }

  /* ─── Empty state ───────────────────────────────────────── */
  .empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: var(--space-xl);
    color: var(--text-muted);
    text-align: center;
    gap: var(--space-sm);
  }
  .empty-state__icon {
    font-size: 40px;
    opacity: 0.5;
  }
  .empty-state__title {
    font-size: var(--text-base);
    font-weight: 600;
    color: var(--text-secondary);
  }
  .empty-state__body {
    font-size: var(--text-sm);
    max-width: 280px;
    line-height: 1.5;
  }

  /* ─── Auto-refresca badge ───────────────────────────────── */
  .auto-refresh-badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    background: oklch(0.72 0.17 160 / 0.12);
    border: 1px solid oklch(0.72 0.17 160 / 0.3);
    border-radius: 99px;
    padding: 2px 8px;
    font-size: 10px;
    color: var(--clr-success);
    vertical-align: middle;
  }

  /* ─── Sección header estándar dentro de tabla/card ─────── */
  .section-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 18px 10px;
    border-bottom: 1px solid var(--border);
  }
  .section-header h4 {
    margin: 0;
    font-size: var(--text-md);
    font-weight: 700;
  }
  .section-header p {
    margin: 3px 0 0;
    font-size: var(--text-xs);
    color: var(--text-muted);
  }

  /* ─── Cache label (zonas / productos) ──────────────────────── */
  .cache-label {
    display: none;
    font-size: 11px;
    color: var(--text-muted);
    font-style: italic;
  }

  /* ─── Zonas: collapsible sections ──────────────────────────── */
  .zonas-section--collapsed {
    display: none !important;
  }
  .zonas-toggle-btn {
    background: none;
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 2px 8px;
    font-size: 11px;
    color: var(--text-muted);
    cursor: pointer;
    transition: border-color 0.15s, color 0.15s;
  }
  .zonas-toggle-btn:hover {
    border-color: var(--brand);
    color: var(--brand);
  }

  /* ─── Zonas: Leaflet popup ──────────────────────────────────── */
  .zonas-popup {
    min-width: 180px;
    font-family: inherit;
  }
  .zonas-popup__title {
    font-weight: 700;
    font-size: 13px;
    margin-bottom: 8px;
    color: var(--text);
    border-bottom: 1px solid var(--border);
    padding-bottom: 6px;
  }
  .zonas-popup__table {
    width: 100%;
    border-collapse: collapse;
    font-size: 12px;
  }
  .zonas-popup__table td {
    padding: 3px 6px 3px 0;
    color: var(--text-secondary);
    vertical-align: top;
  }
  .zonas-popup__table td:last-child {
    font-weight: 600;
    color: var(--text);
    text-align: right;
    padding-left: 12px;
  }
  .zonas-popup__agent {
    margin-top: 8px;
    padding-top: 6px;
    border-top: 1px solid var(--border);
    font-size: 11px;
    color: var(--text-muted);
  }

  /* ─── Zonas: map zone labels ────────────────────────────────── */
  .zonas-map-label {
    background: rgba(15, 23, 42, 0.8);
    border: 1px solid rgba(255,255,255,0.15);
    border-radius: 4px;
    color: #e2e8f0;
    font-size: 11px;
    font-weight: 600;
    padding: 2px 6px;
    white-space: nowrap;
    pointer-events: none;
  }

  /* ─── Zonas: top-producto cards ─────────────────────────────── */
  .top-producto-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 14px 16px;
    display: flex;
    flex-direction: column;
    gap: 6px;
    transition: border-color 0.15s, transform 0.15s;
  }
  .top-producto-card:hover {
    border-color: var(--brand);
    transform: translateY(-1px);
  }
  .top-producto-card__rank {
    font-size: 10px;
    font-weight: 700;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
  }
  .top-producto-card__name {
    font-weight: 600;
    font-size: var(--text-sm);
    line-height: 1.3;
    color: var(--text);
  }
  .top-producto-card__qty {
    font-size: var(--text-xs);
    color: var(--text-muted);
  }
  .top-producto-card__revenue {
    font-weight: 700;
    font-size: var(--text-md);
    color: var(--clr-success);
    font-variant-numeric: tabular-nums;
  }

  /* ─── Zonas: zonas-roja row highlight ───────────────────────── */
  .zonas-roja-row {
    background: oklch(0.45 0.22 25 / 0.06);
  }
  .zonas-roja-row:hover {
    background: oklch(0.45 0.22 25 / 0.12);
  }
}