/* ==================== TOKENS ==================== */

:root,
[data-theme="light"] {
    --primary: #dc2626;
    --primary-hover: #b91c1c;
    --primary-soft: rgba(220, 38, 38, 0.12);
    --success: #16a34a;
    --warning: #d97706;
    --error: #dc2626;

    --bg-body: #f5f7fb;
    --bg-surface: #ffffff;
    --bg-surface-alt: #f8fafc;
    --bg-input: #ffffff;
    --bg-container: var(--bg-surface);

    --text-primary: #111827;
    --text-secondary: #4b5563;
    --text-muted: #6b7280;

    --border-color: #e5e7eb;
    --border-strong: #d1d5db;
    --primary-color: var(--primary);

    --shadow-sm: 0 1px 2px rgba(17, 24, 39, 0.06);
    --shadow-md: 0 8px 24px rgba(17, 24, 39, 0.08);
}

[data-theme="dark"] {
    --primary: #ef4444;
    --primary-hover: #dc2626;
    --primary-soft: rgba(239, 68, 68, 0.15);
    --success: #22c55e;
    --warning: #f59e0b;
    --error: #f87171;

    --bg-body: #0f172a;
    --bg-surface: #111827;
    --bg-surface-alt: #1f2937;
    --bg-input: #111827;
    --bg-container: var(--bg-surface);

    --text-primary: #f9fafb;
    --text-secondary: #d1d5db;
    --text-muted: #9ca3af;

    --border-color: #374151;
    --border-strong: #4b5563;
    --primary-color: var(--primary);

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.35);
    --shadow-md: 0 10px 26px rgba(0, 0, 0, 0.35);
}

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

html,
body {
    width: 100%;
}

body {
    font-family: "Inter", sans-serif;
    background: radial-gradient(circle at 0% 0%, var(--primary-soft) 0, transparent 45%), var(--bg-body);
    color: var(--text-primary);
    min-height: 100vh;
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
}

:focus-visible {
    outline: none;
    box-shadow: 0 0 0 2px var(--bg-body), 0 0 0 4px var(--primary);
    border-radius: 8px;
}

.skip-link {
    position: absolute;
    top: -48px;
    left: 12px;
    z-index: 1000;
    background: var(--primary);
    color: #fff;
    text-decoration: none;
    padding: 8px 12px;
    border-radius: 8px;
    font-weight: 700;
}

.skip-link:focus-visible {
    top: calc(8px + env(safe-area-inset-top));
}

/* ==================== HEADER ==================== */

.header {
    height: 72px;
    padding: env(safe-area-inset-top) calc(24px + env(safe-area-inset-right)) 0 calc(24px + env(safe-area-inset-left));
    background: var(--bg-surface);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    position: sticky;
    top: 0;
    z-index: 100;
    box-shadow: var(--shadow-sm);
}

.header-left,
.header-right {
    display: flex;
    align-items: center;
    gap: 12px;
}

.logo {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 1.2rem;
    font-weight: 800;
    letter-spacing: -0.02em;
}

.user-info {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 12px;
    border: 1px solid var(--border-color);
    border-radius: 12px;
    background: var(--bg-surface-alt);
}

.user-avatar {
    width: 38px;
    height: 38px;
    border-radius: 999px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-weight: 700;
    background: linear-gradient(135deg, var(--primary), var(--primary-hover));
}

.user-details {
    display: flex;
    flex-direction: column;
}

.user-name {
    font-size: 0.86rem;
    font-weight: 700;
    line-height: 1.2;
}

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

.theme-toggle,
.btn-logout {
    min-height: 44px;
    border-radius: 10px;
    border: 1px solid var(--border-color);
    background: var(--bg-surface-alt);
    color: var(--text-primary);
    cursor: pointer;
    transition: 0.2s ease;
}

.theme-toggle {
    width: 44px;
    font-size: 1rem;
}

.theme-toggle:hover {
    border-color: var(--border-strong);
    transform: translateY(-1px);
}

.btn-logout {
    padding: 0 14px;
    font-weight: 700;
}

.btn-logout:hover {
    background: var(--primary);
    color: #fff;
    border-color: var(--primary);
}

/* ==================== LAYOUT ==================== */

.dashboard-container {
    display: grid;
    grid-template-columns: 250px minmax(0, 1fr);
    min-height: calc(100vh - 72px);
}

.sidebar {
    background: var(--bg-surface);
    border-right: 1px solid var(--border-color);
    padding: 16px 10px;
}

.nav-menu {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.nav-item {
    min-height: 44px;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 0 12px;
    border-radius: 10px;
    text-decoration: none;
    color: var(--text-secondary);
    font-weight: 600;
    border: 1px solid transparent;
    transition: 0.2s ease;
}

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

.nav-item.active {
    color: #fff;
    background: linear-gradient(135deg, var(--primary), var(--primary-hover));
    border-color: transparent;
}

.main-content {
    padding: 22px calc(22px + env(safe-area-inset-right)) 22px calc(22px + env(safe-area-inset-left));
}

.content-section {
    display: none;
}

.content-section.active {
    display: block;
    animation: fadeIn 0.18s ease-out;
}

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

/* ==================== BLOCKS ==================== */

.welcome-section {
    margin-bottom: 18px;
}

.welcome-section h1 {
    font-size: 1.5rem;
    font-weight: 800;
    margin-bottom: 4px;
}

.welcome-section p {
    color: var(--text-secondary);
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 12px;
    margin-bottom: 18px;
}

.stat-card {
    display: flex;
    align-items: center;
    gap: 12px;
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: 14px;
    padding: 14px;
    box-shadow: var(--shadow-sm);
}

.stat-icon {
    width: 40px;
    height: 40px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--primary-soft);
    font-size: 1.2rem;
}

.stat-label {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.stat-value {
    margin-top: 2px;
    font-size: 1.25rem;
    font-weight: 800;
}

.section-title {
    font-size: 1.3rem;
    font-weight: 800;
    margin-bottom: 14px;
}

.section-toolbar {
    display: flex;
    gap: 8px;
    align-items: center;
    flex-wrap: wrap;
    margin-bottom: 12px;
}

.section-toolbar input,
.section-toolbar select,
.section-toolbar textarea,
.form-field input,
.form-field select,
.form-field textarea {
    min-height: 42px;
    border: 1px solid var(--border-color);
    border-radius: 10px;
    background: var(--bg-input);
    color: var(--text-primary);
    padding: 0 12px;
    font-size: 0.92rem;
}

.section-toolbar input:focus,
.section-toolbar select:focus,
.form-field input:focus,
.form-field select:focus,
.form-field textarea:focus {
    border-color: var(--primary);
}

.section-toolbar input {
    flex: 1;
    min-width: 210px;
}

.module-card,
.info-section {
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: 14px;
    padding: 16px;
    margin-bottom: 14px;
    box-shadow: var(--shadow-sm);
}

.module-card h3,
.actions-section h2,
.info-section h2 {
    font-size: 1rem;
    font-weight: 800;
    margin-bottom: 12px;
}

.actions-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
    gap: 8px;
}

.action-btn,
.btn-action {
    min-height: 42px;
    border: 1px solid transparent;
    border-radius: 10px;
    cursor: pointer;
    font-weight: 700;
    font-size: 0.88rem;
    padding: 0 12px;
    transition: 0.2s ease;
}

.action-btn {
    background: var(--bg-surface-alt);
    color: var(--text-primary);
    border-color: var(--border-color);
}

.action-btn:hover {
    transform: translateY(-1px);
    border-color: var(--border-strong);
}

.btn-action {
    color: #fff;
    background: var(--primary);
}

.btn-action:hover {
    background: var(--primary-hover);
}

.btn-green { background: var(--success) !important; }
.btn-red { background: var(--error) !important; }
.btn-gray { background: #6b7280 !important; }

.btn-sm {
    min-height: 32px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 0 10px;
    background: var(--bg-surface-alt);
    color: var(--text-primary);
    cursor: pointer;
    font-size: 0.78rem;
    font-weight: 700;
}

.btn-sm:hover {
    border-color: var(--border-strong);
}

.info-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 8px 12px;
}

.info-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    padding: 10px 12px;
    border: 1px solid var(--border-color);
    border-radius: 10px;
    background: var(--bg-surface-alt);
}

.role-badge,
.status-active {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 26px;
    padding: 0 10px;
    border-radius: 999px;
    font-size: 0.78rem;
    font-weight: 700;
}

.role-badge {
    background: var(--primary-soft);
    color: var(--primary);
}

.status-active {
    background: rgba(22, 163, 74, 0.15);
    color: var(--success);
}

.products-meta {
    margin-bottom: 10px;
    color: var(--text-muted);
    font-size: 0.82rem;
    font-weight: 600;
}

/* ==================== TABLE ==================== */

.table-wrapper,
.cash-history-wrap,
#rep-sales-content,
#rep-stock-content {
    overflow-x: auto;
}

.data-table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: 12px;
}

.data-table thead th {
    background: var(--bg-surface-alt);
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.03em;
    font-size: 0.72rem;
    font-weight: 700;
    padding: 10px 12px;
    border-bottom: 1px solid var(--border-color);
    white-space: nowrap;
}

.data-table tbody td {
    padding: 10px 12px;
    border-bottom: 1px solid var(--border-color);
    font-size: 0.86rem;
    vertical-align: middle;
}

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

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

/* ==================== FORMS / MODAL ==================== */

.form-row {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    align-items: flex-end;
}

.form-field {
    display: flex;
    flex-direction: column;
    gap: 4px;
    flex: 1;
    min-width: 140px;
}

.form-field label {
    font-size: 0.76rem;
    font-weight: 700;
    color: var(--text-secondary);
}

.module-modal {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.7);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 900;
}

.module-modal-box {
    width: min(92vw, 560px);
    max-height: 90vh;
    overflow-y: auto;
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: 14px;
    padding: 20px;
    box-shadow: var(--shadow-md);
}

.modal-footer {
    display: flex;
    gap: 8px;
    justify-content: flex-end;
    margin-top: 12px;
}

/* ==================== HELPERS ==================== */

.empty-msg {
    text-align: center;
    color: var(--text-muted);
    padding: 22px;
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 22px;
    padding: 0 8px;
    border-radius: 999px;
    font-size: 0.72rem;
    font-weight: 700;
}

.badge-green { background: rgba(22, 163, 74, 0.16); color: #16a34a; }
.badge-red { background: rgba(220, 38, 38, 0.18); color: #ef4444; }
.badge-yellow { background: rgba(245, 158, 11, 0.18); color: #f59e0b; }
.badge-blue { background: rgba(37, 99, 235, 0.2); color: #60a5fa; }
.badge-gray { background: rgba(107, 114, 128, 0.2); color: #9ca3af; }

.green { color: var(--success); }
.red { color: var(--error); }
.yellow { color: var(--warning); }
.bold { font-weight: 700; }

.spinner {
    width: 16px;
    height: 16px;
    border: 2px solid rgba(156, 163, 175, 0.35);
    border-top-color: var(--primary);
    border-radius: 999px;
    display: inline-block;
    animation: spin 0.7s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* ==================== RESPONSIVE ==================== */

@media (max-width: 1200px) {
    .dashboard-container {
        grid-template-columns: 220px minmax(0, 1fr);
    }
}

@media (max-width: 980px) {
    .header {
        height: auto;
        padding: calc(10px + env(safe-area-inset-top)) calc(12px + env(safe-area-inset-right)) 10px calc(12px + env(safe-area-inset-left));
        flex-wrap: wrap;
    }

    .dashboard-container {
        grid-template-columns: 1fr;
    }

    .sidebar {
        border-right: none;
        border-bottom: 1px solid var(--border-color);
        padding: 8px;
    }

    .nav-menu {
        flex-direction: row;
        overflow-x: auto;
        padding-bottom: 2px;
    }

    .nav-item {
        white-space: nowrap;
    }

    .main-content {
        padding: 12px;
    }
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}

@media (max-width: 680px) {
    .user-info {
        padding: 6px 8px;
    }

    .user-details {
        display: none;
    }

    .stats-grid,
    .actions-grid,
    .info-grid {
        grid-template-columns: 1fr;
    }

    .section-toolbar input {
        min-width: 100%;
    }

    .modal-footer {
        flex-direction: column-reverse;
    }
}
