@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');

:root {
    --primary: #4f46e5;
    --primary-light: #6366f1;
    --primary-dark: #4338ca;
    --secondary: #0f172a;
    --background: #f1f5f9;
    --surface: #ffffff;
    --text-main: #1e293b;
    --text-muted: #64748b;
    --danger: #ef4444;
    --danger-hover: #dc2626;
    --success: #10b981;
    --border: #e2e8f0;
    --radius: 0.5rem;
    --radius-lg: 0.75rem;
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
    --font: 'Inter', system-ui, -apple-system, sans-serif;
    
    /* Sidebar specific */
    --sidebar-bg: #0f172a;
    --sidebar-text: #94a3b8;
    --sidebar-active: #1e293b;
    --sidebar-active-text: #ffffff;
}

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

body {
    width: 100vw;
    height: 100vh;
    font-family: var(--font);
    background-color: var(--background);
    color: var(--text-main);
    overflow: hidden; /* Prevents global scroll, delegates to main-content */
    -webkit-font-smoothing: antialiased;
}

h1, h2, h3, h4, strong {
    color: var(--secondary);
    font-weight: 600;
}

/* ========================================= */
/* GLOBAL APP WRAPPER                       */
/* ========================================= */
#auth-view {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 100vw;
    height: 100vh;
    background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
    position: absolute;
    top: 0; left: 0;
    z-index: 9999;
}

#app-view {
    display: flex;
    width: 100%;
    height: 100vh;
    overflow: hidden;
    background: var(--background);
}

.d-none { display: none !important; }

/* ========================================= */
/* SIDEBAR                                  */
/* ========================================= */
.sidebar {
    width: 260px;
    background: var(--sidebar-bg);
    color: var(--sidebar-text);
    display: flex;
    flex-direction: column;
    padding: 1.5rem 0;
    transition: all 0.3s ease;
    box-shadow: var(--shadow-md);
    z-index: 50;
    flex-shrink: 0;
    height: 100vh;
    overflow-y: auto;
}

.brand {
    padding: 0 1.5rem;
    margin-bottom: 2.5rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}
.brand ion-icon { font-size: 1.75rem; color: var(--primary-light); }
.brand h2 {
    color: white;
    font-size: 1.25rem;
    font-weight: 700;
    letter-spacing: -0.025em;
    margin: 0;
}
.brand h2 span { color: var(--primary-light); }

.nav-menu {
    list-style: none;
    flex: 1;
    padding: 0 1rem;
}
.nav-item { margin-bottom: 0.25rem; }
.nav-link {
    display: flex;
    align-items: center;
    padding: 0.75rem 1rem;
    color: var(--sidebar-text);
    text-decoration: none;
    font-weight: 500;
    border-radius: var(--radius);
    transition: all 0.2s ease;
    gap: 0.75rem;
    font-size: 0.95rem;
}
.nav-link:hover {
    background: var(--sidebar-active);
    color: var(--sidebar-active-text);
}
.nav-link.active {
    background: var(--primary);
    color: white;
    box-shadow: 0 4px 6px -1px rgba(79, 70, 229, 0.4);
}
.nav-link ion-icon { font-size: 1.25rem; }

/* ========================================= */
/* MAIN CONTENT & TOPBAR                    */
/* ========================================= */
.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    height: 100%;
    min-height: 100vh;
    overflow-y: auto;
    overflow-x: hidden;
    background: var(--background);
}

.topbar {
    background: var(--surface);
    padding: 0 2rem;
    height: 72px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 40;
    flex-shrink: 0;
}

.topbar .flex {
    display: flex;
    align-items: center;
    gap: 1rem;
    min-width: 0;
}

.topbar h1 {
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin: 0;
}

.user-info {
    display: flex;
    align-items: center;
    gap: 1rem;
    font-weight: 500;
    flex-shrink: 0;
}
.user-info span { color: var(--text-muted); }

/* ========================================= */
/* VIEWS & CARDS                            */
/* ========================================= */
.view-section {
    padding: 2rem;
    width: 100%;
    display: block;
}

.card {
    background: var(--surface);
    border-radius: var(--radius-lg);
    padding: 1.5rem 2rem;
    box-shadow: var(--shadow-sm);
    margin-bottom: 2rem;
    border: 1px solid var(--border);
}
.card h2 { margin: 0; }

/* ========================================= */
/* UTILITIES                                */
/* ========================================= */
.flex { display: flex; }
.gap-2 { gap: 0.5rem; }
.items-center { align-items: center; }
.text-center { text-align: center; }
.d-md-none { display: none; }

/* ========================================= */
/* FORMS & INPUTS                           */
/* ========================================= */
.form-group {
    margin-bottom: 1.25rem;
    text-align: left;
}
.form-group label {
    display: block;
    margin-bottom: 0.5rem;
    font-weight: 500;
    font-size: 0.875rem;
    color: var(--secondary);
}
.form-control {
    width: 100%;
    padding: 0.625rem 0.875rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    outline: none;
    font-size: 0.95rem;
    font-family: var(--font);
    transition: all 0.2s;
    background: var(--surface);
    color: var(--secondary);
}
.form-control:hover { border-color: #cbd5e1; }
.form-control:focus {
    border-color: var(--primary-light);
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.15);
}
.form-control:disabled, .form-control[readonly] {
    background: #f1f5f9;
    cursor: not-allowed;
    color: var(--text-muted);
}
.form-control::placeholder { color: #94a3b8; }

/* ========================================= */
/* BUTTONS                                  */
/* ========================================= */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    padding: 0.625rem 1.25rem;
    border: none;
    border-radius: var(--radius);
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    font-family: var(--font);
}
.btn ion-icon { font-size: 1.1rem; }
.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-dark); box-shadow: var(--shadow-sm); }
.btn-secondary { background: var(--surface); color: var(--secondary); border: 1px solid var(--border); }
.btn-secondary:hover { background: #f8fafc; }
.btn-danger  { background: var(--danger); color: white; }
.btn-danger:hover  { background: var(--danger-hover); }
.btn-success { background: var(--success); color: white; }
.btn-success:hover { background: #059669; }
.btn-warning { background: #d97706; color: white; }
.btn-warning:hover { background: #b45309; }
.btn-sm { padding: 0.4rem 0.75rem; font-size: 0.85rem; }

/* ========================================= */
/* GRIDS                                    */
/* ========================================= */
.grid-2 { display: grid; grid-template-columns: repeat(2, 1fr); gap: 1.5rem; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1.5rem; }
.grid-4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 1.5rem; }

/* ========================================= */
/* TABLES                                   */
/* ========================================= */
.table-responsive {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    width: 100%;
}
.table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 1rem;
    background: var(--surface);
    font-size: 0.9rem;
}
.table th, .table td {
    padding: 1rem;
    text-align: left;
    border-bottom: 1px solid var(--border);
}
.table th {
    font-weight: 600;
    color: var(--text-muted);
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    background: #f8fafc;
    border-top: 1px solid var(--border);
}
.table tbody tr:hover { background-color: #f8fafc; }
.table tbody tr:last-child td { border-bottom: none; }
.table td .form-control, .order-item-row .form-control { padding: 0.35rem 0.5rem; }

/* ========================================= */
/* MODALS                                   */
/* ========================================= */
.modal-overlay {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(15, 23, 42, 0.6);
    backdrop-filter: blur(4px);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 999;
}
.modal-overlay.active { display: flex; }
.modal {
    background: var(--surface);
    width: 90%;
    max-width: 500px;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    animation: modalPop 0.3s cubic-bezier(0.16, 1, 0.3, 1);
    display: flex;
    flex-direction: column;
    max-height: 90vh;
}
@keyframes modalPop { 
    from { opacity: 0; transform: scale(0.95) translateY(10px); } 
    to { opacity: 1; transform: scale(1) translateY(0); } 
}
.modal-header {
    padding: 1.25rem 1.5rem;
    border-bottom: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: #f8fafc;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
}
.modal-header h3 { margin: 0; font-size: 1.1rem; }
.modal-body {
    padding: 1.5rem;
    overflow-y: auto;
}

/* ========================================= */
/* MISC (Toast, Badges, Login)              */
/* ========================================= */
.badge {
    background: #f1f5f9;
    color: var(--text-muted);
    padding: 0.25rem 0.5rem;
    border-radius: 9999px;
    font-size: 0.75rem;
    font-weight: 600;
}
.badge-success { background: #dcfce7; color: #166534; }
.badge-danger { background: #fee2e2; color: #991b1b; }
.badge-warning { background: #fef9c3; color: #854d0e; }
.badge-info    { background: #dbeafe; color: #1e40af; }
.badge-nulada  { background: #f1f5f9; color: #64748b; text-decoration: line-through; }

#toast {
    position: fixed;
    bottom: 2rem;
    right: 2rem;
    background: var(--secondary);
    color: white;
    padding: 1rem 1.5rem;
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
    z-index: 10000;
    transition: all 0.3s;
    transform: translateY(100px);
    opacity: 0;
    font-weight: 500;
}
#toast.show { transform: translateY(0); opacity: 1; }
#toast.error { background: var(--danger); }

.login-card {
    background: var(--surface);
    padding: 2.5rem;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    width: 100%;
    max-width: 420px;
    text-align: center;
}
.login-card h2 { margin-bottom: 0.5rem; font-size: 1.75rem; }
.login-card p { color: var(--text-muted); margin-bottom: 2rem; }

/* ========================================= */
/* ORDER ITEMS                              */
/* ========================================= */
.mobile-label {
    display: none;
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-muted);
    margin-bottom: 0.25rem;
}

.order-item-row {
    display: grid;
    grid-template-columns: 80px 1fr 85px 120px 80px 120px 44px;
    gap: 0.5rem;
    align-items: start;
    padding: 0.5rem 0;
    border-bottom: 1px solid var(--border);
}
.order-item-row:last-child { border-bottom: none; }

/* ========================================= */
/* TOTALS BOX                               */
/* ========================================= */
.totals-box {
    background: #f8fafc;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.25rem;
    height: fit-content;
}
.totals-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.5rem 0;
    border-bottom: 1px solid var(--border);
    font-size: 0.95rem;
}
.totals-row:last-child { border-bottom: none; }
.totals-row.grand-total {
    font-weight: 700;
    font-size: 1.1rem;
    color: var(--primary);
    padding-top: 0.75rem;
}

/* ========================================= */
/* RESPONSIVE DESIGN FOR MOBILE              */
/* ========================================= */
.hide-on-desktop { display: none; }


/* ========================================= */
/* MODAL FOOTER                             */
/* ========================================= */
.modal-footer {
    padding: 1rem 1.5rem;
    border-top: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: #f8fafc;
    border-radius: 0 0 var(--radius-lg) var(--radius-lg);
    gap: 0.5rem;
    flex-wrap: wrap;
}

/* ========================================= */
/* WHATSAPP RESEND RADIO OPTIONS            */
/* ========================================= */
.wa-radio-option {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    border: 2px solid var(--border);
    border-radius: var(--radius);
    padding: 0.75rem 1rem;
    cursor: pointer;
    transition: border-color 0.2s, background 0.2s;
    width: 100%;
}
.wa-radio-option:has(input:checked) {
    border-color: var(--primary);
    background: #eef2ff;
}
.wa-radio-option input[type="radio"] {
    accent-color: var(--primary);
    width: 16px;
    height: 16px;
    flex-shrink: 0;
    cursor: pointer;
}
.wa-radio-content {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}
.wa-radio-content > ion-icon {
    font-size: 1.25rem;
    color: var(--text-muted);
    flex-shrink: 0;
}

/* ========================================= */
/* Sidebar backdrop overlay */
.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 800;
    cursor: pointer;
}
.sidebar-overlay.active { display: block; }

@media (max-width: 768px) {
    .d-md-none { display: inline-flex !important; }
    .hide-on-mobile { display: none !important; }
    .btn-text-mobile-hide { display: none; }
    .mobile-label { display: block; }

    /* Sidebar */
    .sidebar {
        position: fixed;
        left: -100%;
        top: 0;
        bottom: 0;
        z-index: 900;
        box-shadow: 4px 0 10px rgba(0,0,0,0.1);
    }
    .sidebar.active { left: 0; }

    /* Topbar */
    .topbar { padding: 0 1rem; gap: 0.5rem; }
    .topbar h1 { font-size: 1rem; }

    /* Layout */
    .view-section { padding: 1rem; }
    .card { padding: 1rem; margin-bottom: 1rem; }

    /* Grids collapse to 1 column */
    .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; gap: 0.75rem; }

    /* Tables */
    .table-responsive { border: 1px solid var(--border); border-radius: var(--radius); }
    .table th, .table td { padding: 0.6rem 0.75rem; font-size: 0.82rem; }

    /* Order item rows — 2-col stacked card */
    .order-item-row {
        grid-template-columns: 1fr 1fr;
        gap: 0.5rem;
        padding: 0.75rem;
        border: 1px solid var(--border);
        border-radius: var(--radius);
        margin-bottom: 0.75rem;
        background: var(--surface);
    }
    .order-item-row > div:nth-child(1) { grid-column: 1; }       /* Código */
    .order-item-row > div:nth-child(2) { grid-column: 1 / -1; }  /* Descripción — full width */
    .order-item-row > div:nth-child(3) { grid-column: 1; }       /* Cantidad */
    .order-item-row > div:nth-child(4) { grid-column: 2; }       /* Precio */
    .order-item-row > div:nth-child(5) { grid-column: 1; }       /* Descuento */
    .order-item-row > div:nth-child(6) { grid-column: 2; }       /* Subtotal */
    .order-item-row > div:nth-child(7) { grid-column: 1 / -1; }  /* Botón eliminar */

    /* Totals */
    .totals-box { margin-top: 0; }

    /* Login */
    .login-card { padding: 1.5rem 1.25rem; margin: 0 1rem; }

    /* Toast */
    #toast { right: 1rem; left: 1rem; bottom: 1rem; text-align: center; }

    /* Touch-friendly buttons */
    .btn { min-height: 44px; }
    .btn-sm { min-height: 36px; }
}

/* ========================================= */
/* VISITS MODULE                            */
/* ========================================= */
.slot-taken {
    color: var(--text-muted);
    background: #f1f5f9;
}

.visit-detail-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.75rem 1.5rem;
}

.visit-detail-field label {
    font-size: 0.75rem;
    color: var(--text-muted);
    margin-bottom: 0.15rem;
    display: block;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.visit-detail-field p {
    margin: 0;
    font-weight: 500;
    color: var(--text-main);
}

.badge-programada {
    background: #dbeafe;
    color: #1e40af;
    padding: 0.2rem 0.6rem;
    border-radius: 99px;
    font-size: 0.75rem;
    font-weight: 600;
}

.badge-completada {
    background: #dcfce7;
    color: #166534;
    padding: 0.2rem 0.6rem;
    border-radius: 99px;
    font-size: 0.75rem;
    font-weight: 600;
}

.badge-cancelada {
    background: #f1f5f9;
    color: #64748b;
    text-decoration: line-through;
    padding: 0.2rem 0.6rem;
    border-radius: 99px;
    font-size: 0.75rem;
    font-weight: 600;
}

@media (max-width: 600px) {
    .visit-detail-grid {
        grid-template-columns: 1fr;
    }
}

/* ========================================= */
/* CUSTOMER HISTORY TABS                     */
/* ========================================= */
.cust-hist-tab {
    background: none;
    border: none;
    border-bottom: 3px solid transparent;
    padding: 0.5rem 1rem;
    font-size: 0.88rem;
    font-weight: 500;
    color: var(--text-muted);
    cursor: pointer;
    transition: color 0.15s, border-color 0.15s;
    white-space: nowrap;
}
.cust-hist-tab:hover { color: var(--primary); }
.cust-hist-tab.active { color: var(--primary); border-bottom-color: var(--primary); font-weight: 600; }

/* ========================================= */
/* PANEL GERENCIAL — KPI CARDS               */
/* ========================================= */
.gc-card {
    padding: 1rem 1.1rem;
    display: flex;
    align-items: center;
    gap: 0.85rem;
    min-width: 0;            /* permite que flex/grid encojan la card */
    overflow: hidden;
}
.gc-link {
    cursor: pointer;
    transition: box-shadow 0.15s, transform 0.1s;
}
.gc-link:hover {
    box-shadow: 0 4px 16px rgba(0,0,0,0.12);
    transform: translateY(-1px);
}
.gc-card-detail {
    flex-direction: column;
    align-items: stretch;
    gap: 0;
}
.gc-icon {
    width: 44px;
    height: 44px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}
.gc-icon ion-icon { font-size: 1.35rem; }
.gc-body {
    min-width: 0;            /* crítico: permite que el texto trunque */
    flex: 1;
    overflow: hidden;
}
.gc-value {
    font-size: clamp(1.25rem, 3.5vw, 1.9rem);
    font-weight: 700;
    line-height: 1.1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.gc-value-fin {
    font-size: clamp(1.05rem, 2.8vw, 1.55rem);
}
.gc-label {
    font-size: clamp(0.7rem, 1.5vw, 0.85rem);
    color: var(--text-muted);
    margin-top: 0.2rem;
    line-height: 1.25;
}
.gc-detail {
    font-size: 0.7rem;
    color: var(--text-muted);
    border-top: 1px solid var(--border);
    padding-top: 0.4rem;
    margin-top: 0.5rem;
    word-break: break-word;
    line-height: 1.4;
}

/* En móvil pequeño: reduce padding de cards */
@media (max-width: 480px) {
    .gc-card { padding: 0.8rem 0.9rem; gap: 0.6rem; }
    .gc-icon { width: 36px; height: 36px; border-radius: 8px; }
    .gc-icon ion-icon { font-size: 1.1rem; }
}

/* ========================================= */
/* SWITCH TOGGLE                            */
/* ========================================= */
.switch {
  position: relative;
  display: inline-block;
  width: 46px;
  height: 24px;
}
.switch input { 
  opacity: 0;
  width: 0;
  height: 0;
}
.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #cbd5e1;
  transition: .3s;
}
.slider:before {
  position: absolute;
  content: "";
  height: 18px;
  width: 18px;
  left: 3px;
  bottom: 3px;
  background-color: white;
  transition: .3s;
}
input:checked + .slider {
  background-color: var(--primary);
}
input:focus + .slider {
  box-shadow: 0 0 1px var(--primary);
}
input:checked + .slider:before {
  transform: translateX(22px);
}
.slider.round {
  border-radius: 24px;
}
.slider.round:before {
  border-radius: 50%;
}
