/* ==========================================================================
   CONTROL QR IN/OUT — Global Base Styles + Mobile-First Utilities
   ========================================================================== */

/* ── Reset & Box Model ───────────────────────────────────────────────────── */
*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

/* ── Root Variables ──────────────────────────────────────────────────────── */
:root {
    --primary:        #17add1;
    --primary-dark:   #0e7490;
    --primary-glow:   rgba(23, 173, 209, 0.25);
    --indigo:         #6366f1;
    --indigo-dark:    #4f46e5;
    --bg:             #f8fafc;
    --bg-card:        #ffffff;
    --text-main:      #1e293b;
    --text-muted:     #64748b;
    --border:         #e5e7eb;
    --radius:         12px;
    --radius-sm:      8px;
    --shadow-sm:      0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.04);
    --shadow-md:      0 4px 16px rgba(0,0,0,.08), 0 2px 6px rgba(0,0,0,.04);
    --shadow-lg:      0 8px 32px rgba(0,0,0,.12), 0 4px 12px rgba(0,0,0,.06);

    /* Safe area para notch/Dynamic Island */
    --safe-top:    env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-left:   env(safe-area-inset-left, 0px);
    --safe-right:  env(safe-area-inset-right, 0px);
}

/* ── Base Typography & Body ──────────────────────────────────────────────── */
html {
    -webkit-text-size-adjust: 100%;
    scroll-behavior: smooth;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;
    color: var(--text-main);
    background: var(--bg);
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* ── PWA Touch Optimizations ────────────────────────────────────────────── */
/* Elimina el delay de 300ms en tap en mobile */
a, button, input, select, textarea, [role="button"] {
    touch-action: manipulation;
}

/* Evita zoom automático en iOS al hacer focus en inputs < 16px */
input, select, textarea {
    font-size: 16px;
}

/* Tap highlight personalizado */
a, button {
    -webkit-tap-highlight-color: rgba(0,0,0,0.04);
}

/* ── Tables: always responsive ──────────────────────────────────────────── */
table {
    border-collapse: collapse;
    width: 100%;
}

.table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    border-radius: var(--radius);
}

/* ── Base Elements ──────────────────────────────────────────────────────── */
a {
    text-decoration: none;
    color: inherit;
}

button {
    cursor: pointer;
    border: none;
    background: none;
    font-family: inherit;
}

input:focus, select:focus, textarea:focus {
    outline: none;
}

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

/* ── Responsive Utilities ───────────────────────────────────────────────── */
.container {
    width: 100%;
    max-width: 1280px;
    margin: 0 auto;
    padding: 0 16px;
}

/* Grid helpers */
.grid-responsive {
    display: grid;
    grid-template-columns: 1fr;
    gap: 16px;
}

@media (min-width: 640px) {
    .grid-responsive { grid-template-columns: repeat(2, 1fr); }
    .container { padding: 0 20px; }
}

@media (min-width: 1024px) {
    .grid-responsive { grid-template-columns: repeat(3, 1fr); }
    .container { padding: 0 32px; }
}

/* Stack on mobile, row on desktop */
.flex-wrap-mobile {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    align-items: center;
}

/* Card base */
.card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-sm);
}

/* ── Mobile Padding Safe Area ───────────────────────────────────────────── */
.pb-safe {
    padding-bottom: calc(16px + var(--safe-bottom));
}

.pt-safe {
    padding-top: calc(16px + var(--safe-top));
}

/* ── Inline Nav Mobile Overrides ────────────────────────────────────────── */
/* Para páginas con nav inline (billing, company-admin, super-admin, etc.) */
nav.site-nav {
    background: #fff;
    border-bottom: 1px solid var(--border);
    padding: 0 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 60px;
    position: sticky;
    top: 0;
    z-index: 100;
    gap: 12px;
}

nav.site-nav .nav-logo {
    font-size: 16px;
    font-weight: 700;
    color: var(--text-main);
    white-space: nowrap;
    flex-shrink: 0;
}

nav.site-nav .nav-links-inline {
    display: flex;
    align-items: center;
    gap: 4px;
    flex-wrap: wrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    -ms-overflow-style: none;
    flex: 1;
    justify-content: flex-end;
}

nav.site-nav .nav-links-inline::-webkit-scrollbar { display: none; }

nav.site-nav .nav-links-inline a {
    font-size: 13px;
    color: var(--text-muted);
    padding: 6px 10px;
    border-radius: 8px;
    white-space: nowrap;
    transition: all 0.2s;
    font-weight: 500;
}

nav.site-nav .nav-links-inline a:hover,
nav.site-nav .nav-links-inline a.active {
    color: var(--primary-dark);
    background: rgba(23,173,209,0.08);
}

/* ── Admin Page Layout ──────────────────────────────────────────────────── */
.admin-main {
    max-width: 1280px;
    margin: 0 auto;
    padding: 20px 16px;
}

@media (min-width: 768px) {
    .admin-main { padding: 28px 24px; }
}

@media (min-width: 1024px) {
    .admin-main { padding: 32px; }
}

/* ── Stats Grid ─────────────────────────────────────────────────────────── */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
    margin-bottom: 20px;
}

@media (min-width: 768px) {
    .stats-grid { grid-template-columns: repeat(4, 1fr); gap: 16px; }
}

.stat-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    box-shadow: var(--shadow-sm);
}

.stat-card .stat-label {
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-muted);
    margin-bottom: 6px;
}

.stat-card .stat-value {
    font-size: 26px;
    font-weight: 800;
    color: var(--text-main);
    line-height: 1;
}

@media (min-width: 768px) {
    .stat-card .stat-value { font-size: 32px; }
}

/* ── Data Table ─────────────────────────────────────────────────────────── */
.data-table {
    width: 100%;
    font-size: 13px;
}

.data-table th {
    padding: 10px 14px;
    text-align: left;
    font-weight: 600;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.4px;
    color: var(--text-muted);
    background: #f8fafc;
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
}

.data-table td {
    padding: 12px 14px;
    border-bottom: 1px solid var(--border);
    color: var(--text-main);
    vertical-align: middle;
}

.data-table tr:last-child td { border-bottom: none; }
.data-table tbody tr:hover { background: #f8fafc; }

/* ── Buttons ────────────────────────────────────────────────────────────── */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 10px 18px;
    border-radius: var(--radius-sm);
    font-size: 14px;
    font-weight: 600;
    transition: all 0.15s ease;
    white-space: nowrap;
    cursor: pointer;
    border: none;
}

.btn-primary {
    background: var(--indigo);
    color: #fff;
    box-shadow: 0 2px 8px rgba(99,102,241,0.3);
}

.btn-primary:hover { background: var(--indigo-dark); transform: translateY(-1px); }

.btn-secondary {
    background: #f1f5f9;
    color: var(--text-main);
    border: 1px solid var(--border);
}

.btn-secondary:hover { background: #e2e8f0; }

.btn-danger {
    background: #fee2e2;
    color: #dc2626;
}

.btn-sm {
    padding: 6px 12px;
    font-size: 12px;
}

/* ── Modals ─────────────────────────────────────────────────────────────── */
.modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    backdrop-filter: blur(4px);
    display: flex;
    align-items: flex-end;
    justify-content: center;
    z-index: 1000;
    padding: 0;
}

@media (min-width: 640px) {
    .modal-backdrop {
        align-items: center;
        padding: 16px;
    }
}

.modal-box {
    background: #fff;
    width: 100%;
    max-width: 520px;
    border-radius: 24px 24px 0 0;
    overflow: hidden;
    box-shadow: 0 -8px 40px rgba(0,0,0,0.15);
    max-height: 90vh;
    overflow-y: auto;
}

@media (min-width: 640px) {
    .modal-box {
        border-radius: 20px;
        box-shadow: var(--shadow-lg);
    }
}

/* ── Form Elements ──────────────────────────────────────────────────────── */
.form-input {
    width: 100%;
    border: 1.5px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 11px 14px;
    font-size: 16px; /* Prevents iOS zoom */
    font-family: inherit;
    color: var(--text-main);
    background: #fff;
    transition: border-color 0.2s, box-shadow 0.2s;
    -webkit-appearance: none;
    appearance: none;
}

.form-input:focus {
    border-color: var(--indigo);
    box-shadow: 0 0 0 3px rgba(99,102,241,0.15);
}

.form-label {
    display: block;
    font-size: 13px;
    font-weight: 600;
    color: var(--text-main);
    margin-bottom: 6px;
}

/* ── Badges ─────────────────────────────────────────────────────────────── */
.badge {
    display: inline-flex;
    align-items: center;
    padding: 3px 9px;
    border-radius: 20px;
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.3px;
}

.badge-green  { background: #dcfce7; color: #166534; }
.badge-red    { background: #fee2e2; color: #991b1b; }
.badge-yellow { background: #fef9c3; color: #854d0e; }
.badge-blue   { background: #dbeafe; color: #1e40af; }
.badge-gray   { background: #f1f5f9; color: #475569; }

/* ── Section Header ─────────────────────────────────────────────────────── */
.section-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 10px;
    margin-bottom: 16px;
}

.section-title {
    font-size: 17px;
    font-weight: 700;
    color: var(--text-main);
}

/* ── Empty State ────────────────────────────────────────────────────────── */
.empty-state {
    text-align: center;
    padding: 48px 24px;
    color: var(--text-muted);
}

.empty-state .empty-icon {
    font-size: 40px;
    margin-bottom: 12px;
    opacity: 0.5;
}

.empty-state p {
    font-size: 14px;
}

/* ==========================================================================
   MOBILE-FIRST GLOBAL OVERRIDES (PWA & RESPONSIVE PERFECTION)
   ========================================================================== */

/* ── Prevent Mobile Zoom & Text Inflation ────────────────────────────────── */
@media screen and (max-width: 768px) {
    input, select, textarea, .form-input, .bar input, .form-group input {
        font-size: 16px !important; /* iOS strict requirement to avoid zoom */
    }
}

/* ── Fluid Main Containers ──────────────────────────────────────────────── */
@media (max-width: 640px) {
    main, .main-content {
        padding: 16px 12px !important;
        width: 100% !important;
    }

    .page-title {
        font-size: 20px !important;
    }

    .page-sub {
        font-size: 13px !important;
        margin-bottom: 20px !important;
    }

    .header-section, .header-actions {
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 12px !important;
        margin-bottom: 18px !important;
    }

    .header-section > div, .header-actions > div {
        width: 100%;
    }

    .header-section button, .header-actions button,
    .header-section .btn, .header-actions .btn {
        width: 100% !important;
        text-align: center !important;
        justify-content: center !important;
    }
}

/* ── Inline Navbars (Billing, Admin, Super Admin, Profiles) ──────────────── */
@media (max-width: 768px) {
    nav:not(.scanner-header) {
        padding: 0 14px !important;
        height: auto !important;
        min-height: 56px !important;
        flex-wrap: wrap !important;
        gap: 10px !important;
        padding-top: max(env(safe-area-inset-top, 0px), 8px) !important;
        padding-bottom: 8px !important;
    }

    nav:not(.scanner-header) .nav-right {
        width: 100% !important;
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch !important;
        padding-bottom: 4px !important;
        scrollbar-width: none !important;
        gap: 12px !important;
        justify-content: flex-start !important;
    }

    nav:not(.scanner-header) .nav-right::-webkit-scrollbar {
        display: none !important;
    }

    nav:not(.scanner-header) a {
        white-space: nowrap !important;
        font-size: 13px !important;
    }
}

/* ── Mobile Horizontal Scroll Tabs ──────────────────────────────────────── */
.tabs, .sub-tabs {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch !important;
    white-space: nowrap !important;
    scrollbar-width: none !important;
    -ms-overflow-style: none !important;
    display: flex !important;
    gap: 6px !important;
}

.tabs::-webkit-scrollbar, .sub-tabs::-webkit-scrollbar {
    display: none !important;
}

.tab {
    flex-shrink: 0 !important;
    padding: 10px 14px !important;
}

/* ── Filter Bars & Search Inputs ────────────────────────────────────────── */
@media (max-width: 640px) {
    .bar {
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 10px !important;
    }

    .bar input {
        width: 100% !important;
        min-width: 0 !important;
    }

    .bar button, .bar .btn {
        width: 100% !important;
    }
}

/* ── Modals: Mobile Bottom-Sheet or Centered Responsive ─────────────────── */
@media (max-width: 640px) {
    #modal-overlay, .modal-backdrop {
        padding: 0 !important;
        align-items: flex-end !important;
    }

    .modal-card, .modal-box {
        width: 100% !important;
        max-width: 100% !important;
        border-radius: 20px 20px 0 0 !important;
        padding: 22px 18px !important;
        max-height: 85vh !important;
        max-height: 85dvh !important;
    }

    .form-grid {
        grid-template-columns: 1fr !important;
        gap: 12px !important;
    }
}

/* ── Responsive Grids & Cards ───────────────────────────────────────────── */
@media (max-width: 640px) {
    .cards-grid, .plans, .usage-grid {
        grid-template-columns: 1fr !important;
        gap: 14px !important;
    }

    .stats-row {
        grid-template-columns: 1fr 1fr !important;
        gap: 10px !important;
    }

    .stat-card {
        padding: 14px !important;
    }

    .stat-val {
        font-size: 24px !important;
    }
}

/* ── Universal Responsive Tables ────────────────────────────────────────── */
.table-card, .plans-table, .admin-table, #scans-table-body, table {
    width: 100%;
}

.table-card, .plans-table-wrap, .admin-table-wrap {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch !important;
    margin-top: 16px !important;
    border-radius: 12px !important;
    box-shadow: 0 1px 3px rgba(0,0,0,0.06) !important;
}