/* ==========================================================================
   Design tokens
   ========================================================================== */
:root {
    --sidebar-width: 264px;
    --radius-sm: 0.5rem;
    --radius-md: 0.85rem;
    --radius-lg: 1.1rem;
    --transition: 160ms ease;

    --bg: #f5f6f9;
    --surface: #ffffff;
    --surface-alt: #f8f9fb;
    --border: #e7e9ee;
    --text: #1a1f2b;
    --text-muted: #6b7280;
    --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.05);
    --shadow-md: 0 4px 16px rgba(16, 24, 40, 0.08);
    --shadow-lg: 0 12px 32px rgba(16, 24, 40, 0.12);

    --sidebar-bg: #101828;
}

:root[data-theme="dark"] {
    --bg: #0e1117;
    --surface: #161b26;
    --surface-alt: #1c2230;
    --border: #2a3140;
    --text: #e6e9f0;
    --text-muted: #9aa3b5;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 20px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 16px 40px rgba(0, 0, 0, 0.5);
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --bg: #0e1117;
        --surface: #161b26;
        --surface-alt: #1c2230;
        --border: #2a3140;
        --text: #e6e9f0;
        --text-muted: #9aa3b5;
        --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
        --shadow-md: 0 4px 20px rgba(0, 0, 0, 0.4);
        --shadow-lg: 0 16px 40px rgba(0, 0, 0, 0.5);
    }
}

/* ==========================================================================
   Base
   ========================================================================== */
body {
    background: var(--bg);
    color: var(--text);
    font-family: "Inter", "Segoe UI", system-ui, -apple-system, sans-serif;
    transition: background var(--transition), color var(--transition);
}

::selection { background: color-mix(in srgb, var(--app-primary, #2563eb) 25%, transparent); }

a { text-decoration: none; }

/* ==========================================================================
   Auth screen
   ========================================================================== */
.auth-bg {
    background: linear-gradient(135deg, #101828 0%, #1d4ed8 100%);
    position: relative;
    overflow: hidden;
}

.auth-bg::before,
.auth-bg::after {
    content: "";
    position: absolute;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.06);
    pointer-events: none;
}

.auth-bg::before { width: 480px; height: 480px; top: -180px; right: -140px; }
.auth-bg::after { width: 360px; height: 360px; bottom: -160px; left: -100px; }

.auth-card {
    width: 100%;
    max-width: 400px;
    border-radius: var(--radius-lg);
    position: relative;
    z-index: 1;
    animation: fadeUp 400ms ease;
}

/* ==========================================================================
   Shell / layout
   ========================================================================== */
.app-shell {
    display: flex;
    min-height: 100vh;
}

.sidebar {
    width: var(--sidebar-width);
    background: var(--sidebar-bg);
    flex-shrink: 0;
    transition: background var(--transition);
}

.sidebar-brand {
    color: #fff;
    font-weight: 700;
    font-size: 1.1rem;
    letter-spacing: -0.01em;
    padding: 1.35rem 1.25rem 1rem;
    align-items: center;
}

.sidebar-nav {
    padding: 0.5rem 0.85rem;
    gap: 3px;
}

.sidebar-nav .nav-link {
    color: rgba(255, 255, 255, 0.68);
    border-radius: var(--radius-sm);
    padding: 0.6rem 0.85rem;
    font-size: 0.9rem;
    font-weight: 500;
    position: relative;
    transition: background var(--transition), color var(--transition), padding-left var(--transition);
}

.sidebar-nav .nav-link i {
    width: 20px;
    display: inline-block;
    margin-right: 8px;
    font-size: 1rem;
}

.sidebar-nav .nav-link:hover {
    background: rgba(255, 255, 255, 0.08);
    color: #fff;
    padding-left: 1rem;
}

.sidebar-nav .nav-link.active {
    background: color-mix(in srgb, var(--app-primary, #2563eb) 18%, transparent);
    color: #fff;
    font-weight: 600;
}

.sidebar-nav .nav-link.active::before {
    content: "";
    position: absolute;
    left: -0.85rem;
    top: 50%;
    transform: translateY(-50%);
    width: 3px;
    height: 60%;
    border-radius: 0 3px 3px 0;
    background: var(--app-primary, #2563eb);
}

.sidebar-footer {
    margin-top: auto;
    border-top: 1px solid rgba(255, 255, 255, 0.08);
}

.avatar-circle {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 0.85rem;
    background: var(--app-primary, #2563eb);
    color: #fff;
    flex-shrink: 0;
}

.main {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
}

.topbar {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 0.6rem 0;
    transition: background var(--transition), border-color var(--transition);
}

.topbar .navbar-brand { color: var(--text); font-weight: 700; }

.content {
    flex: 1;
    animation: fadeUp 260ms ease;
}

/* ==========================================================================
   Cards / surfaces
   ========================================================================== */
.stat-card,
.table-responsive-card {
    background: var(--surface);
    border-radius: var(--radius-lg);
    border: 1px solid var(--border);
    box-shadow: var(--shadow-sm);
    color: var(--text);
    transition: background var(--transition), border-color var(--transition), box-shadow var(--transition), transform var(--transition);
}

.stat-card:hover {
    box-shadow: var(--shadow-md);
    transform: translateY(-2px);
}

.stat-card .stat-icon {
    width: 46px;
    height: 46px;
    border-radius: var(--radius-md);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.3rem;
    flex-shrink: 0;
}

.table-responsive-card { padding: 1.25rem; }

/* ==========================================================================
   Tables
   ========================================================================== */
.table { color: var(--text); }
.table > :not(caption) > * > * { background: transparent; color: inherit; border-bottom-color: var(--border); }
.table-light, .table thead.table-light th { background: var(--surface-alt); color: var(--text-muted); border-color: var(--border); font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.03em; font-weight: 600; }
.table-hover > tbody > tr:hover > * { background: var(--surface-alt); }

/* Below md, data tables collapse into stacked cards instead of scrolling
   horizontally - each <td> becomes a labeled row using its data-label. */
@media (max-width: 767.98px) {
    .table-mobile-cards.table { border-collapse: separate; border-spacing: 0 0.6rem; margin-bottom: -0.6rem; }
    .table-mobile-cards thead { display: none; }
    .table-mobile-cards, .table-mobile-cards tbody, .table-mobile-cards tr { display: block; width: 100%; }
    .table-mobile-cards tr {
        border: 1px solid var(--border);
        border-radius: var(--radius-md);
        background: var(--surface);
        overflow: hidden;
    }
    .table-mobile-cards td {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 0.75rem;
        text-align: right;
        padding: 0.5rem 0.85rem !important;
        border: 0 !important;
        border-bottom: 1px solid var(--border) !important;
    }
    .table-mobile-cards td:last-child { border-bottom: 0 !important; }
    .table-mobile-cards td::before {
        content: attr(data-label);
        font-weight: 600;
        font-size: 0.72rem;
        text-transform: uppercase;
        letter-spacing: 0.03em;
        color: var(--text-muted);
        text-align: left;
        flex-shrink: 0;
    }
    .table-mobile-cards td:not([data-label])::before,
    .table-mobile-cards td[data-label=""]::before { content: none; }
    .table-mobile-cards td:not([data-label]),
    .table-mobile-cards td[data-label=""] { justify-content: center; text-align: center; }
    .table-mobile-cards .btn-group { flex-wrap: wrap; justify-content: flex-end; }
}

/* ==========================================================================
   Forms / inputs
   ========================================================================== */
.form-control, .form-select {
    background-color: var(--surface);
    border-color: var(--border);
    color: var(--text);
    border-radius: var(--radius-sm);
    transition: border-color var(--transition), box-shadow var(--transition);
}

.form-control:focus, .form-select:focus {
    background-color: var(--surface);
    color: var(--text);
    border-color: var(--app-primary, #2563eb);
    box-shadow: 0 0 0 0.2rem color-mix(in srgb, var(--app-primary, #2563eb) 18%, transparent);
}

.form-control::placeholder { color: var(--text-muted); opacity: .7; }
.form-label { font-weight: 600; font-size: 0.85rem; }
.form-text, .text-muted { color: var(--text-muted) !important; }

/* ==========================================================================
   Buttons
   ========================================================================== */
.btn { border-radius: var(--radius-sm); font-weight: 600; font-size: 0.88rem; transition: transform var(--transition), box-shadow var(--transition), background var(--transition), filter var(--transition); }
.btn:active { transform: scale(0.97); }
.btn-light { background: var(--surface-alt); border-color: var(--border); color: var(--text); }
.btn-light:hover { background: var(--border); color: var(--text); }

/* ==========================================================================
   Modals / dropdowns
   ========================================================================== */
.modal-content, .dropdown-menu {
    background: var(--surface);
    color: var(--text);
    border-color: var(--border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
}

.modal-header, .modal-footer { border-color: var(--border); }
.dropdown-item { color: var(--text); }
.dropdown-item:hover, .dropdown-item:focus { background: var(--surface-alt); color: var(--text); }
.dropdown-item-text { color: var(--text-muted); }
.unread-notif { background: var(--surface-alt); }

/* ==========================================================================
   Badges / status
   ========================================================================== */
.badge-status-active { background-color: #16a34a; }
.badge-status-expired { background-color: #dc2626; }
.badge-status-cancelled { background-color: #6b7280; }
.badge-pay-paid { background-color: #16a34a; }
.badge-pay-due { background-color: #d97706; }

.days-left-danger { color: #ef4444; font-weight: 600; }
.days-left-warning { color: #f59e0b; font-weight: 600; }
.days-left-ok { color: #22c55e; font-weight: 600; }

/* ==========================================================================
   Global quick search
   ========================================================================== */
.quick-search { position: relative; }

.quick-search input {
    width: 260px;
}

.quick-search-results {
    position: absolute;
    top: calc(100% + 6px);
    left: 0;
    width: 340px;
    max-height: 380px;
    overflow-y: auto;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
    z-index: 1050;
}

.quick-search-results .qs-item {
    display: block;
    padding: 0.6rem 0.9rem;
    color: var(--text);
    border-bottom: 1px solid var(--border);
}

@media (max-width: 767.98px) {
    .quick-search input { width: 100%; }
    .quick-search-results { width: 100%; max-width: 100%; }
}

.quick-search-results .qs-item:last-child { border-bottom: 0; }
.quick-search-results .qs-item:hover { background: var(--surface-alt); }
.quick-search-results .qs-section { padding: 0.4rem 0.9rem; font-size: 0.7rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-muted); background: var(--surface-alt); }

/* ==========================================================================
   Skeleton loaders
   ========================================================================== */
.skeleton {
    display: inline-block;
    height: 0.9em;
    width: 100%;
    border-radius: 0.35em;
    background: linear-gradient(90deg, var(--surface-alt) 25%, var(--border) 37%, var(--surface-alt) 63%);
    background-size: 400% 100%;
    animation: skeleton-shimmer 1.4s ease infinite;
}

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

/* ==========================================================================
   Empty states
   ========================================================================== */
.empty-state {
    text-align: center;
    padding: 3rem 1rem;
    color: var(--text-muted);
}

.empty-state i { font-size: 2.4rem; opacity: 0.5; display: block; margin-bottom: 0.75rem; }
.empty-state p { margin: 0; }

/* ==========================================================================
   Misc
   ========================================================================== */
.theme-toggle {
    width: 38px;
    height: 38px;
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    transition: background var(--transition), transform var(--transition);
}

.theme-toggle:hover { background: var(--surface-alt); transform: rotate(15deg); }

@keyframes fadeUp {
    from { opacity: 0; transform: translateY(6px); }
    to { opacity: 1; transform: translateY(0); }
}

@media (max-width: 991.98px) {
    .sidebar { --bs-offcanvas-width: 264px; }
    .quick-search input { width: 160px; }
}
