:root {
    --sidebar-width: 260px;
    --header-height: 64px;
    --primary-color: #4f46e5;
    --sidebar-bg: #1e293b;
    --sidebar-hover: #334155;
    --touch-target: 44px;
    --mobile-padding: 1rem;
    --tablet-padding: 1.25rem;
    --desktop-padding: 1.5rem;
    --sticky-actions-height: 72px;
    --transition-fast: 0.2s ease;
    --transition-sidebar: 0.3s ease;
    --shadow-card: 0 1px 3px rgba(0, 0, 0, 0.08);
    --border-radius: 0.75rem;
}

*,
*::before,
*::after {
    box-sizing: border-box;
}

html {
    -webkit-text-size-adjust: 100%;
    scroll-behavior: smooth;
}

html, body {
    height: 100%;
    margin: 0;
    overflow-x: hidden;
}

body {
    font-size: 1rem;
    line-height: 1.5;
    color: #1e293b;
}

/* Skip link */
.skip-link {
    position: absolute;
    top: -100px;
    left: 1rem;
    z-index: 2000;
    padding: 0.5rem 1rem;
    background: var(--primary-color);
    color: #fff;
    border-radius: 0.375rem;
    text-decoration: none;
}

.skip-link:focus {
    top: 1rem;
}

/* App Layout */
.app-wrapper {
    display: flex;
    min-height: 100vh;
    min-height: 100dvh;
}

.sidebar {
    width: var(--sidebar-width);
    background: var(--sidebar-bg);
    color: #e2e8f0;
    flex-shrink: 0;
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh;
    height: 100dvh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    z-index: 1040;
    transition: transform var(--transition-sidebar);
    display: flex;
    flex-direction: column;
}

.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid #334155;
    flex-shrink: 0;
}

.sidebar-close {
    color: #94a3b8;
    padding: 0.5rem;
    margin-right: 0.5rem;
    min-width: var(--touch-target);
    min-height: var(--touch-target);
    display: flex;
    align-items: center;
    justify-content: center;
}

.sidebar-close:hover {
    color: #fff;
}

.sidebar-brand {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 1.25rem 1.5rem;
    font-size: 1.25rem;
    font-weight: 700;
    flex: 1;
    min-width: 0;
}

.sidebar-brand i {
    font-size: 1.5rem;
    color: var(--primary-color);
    flex-shrink: 0;
}

.sidebar-brand-logo {
    max-height: 32px;
    max-width: 32px;
    object-fit: contain;
    border-radius: 4px;
    flex-shrink: 0;
}

.sidebar-brand-text {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.sidebar-nav {
    padding: 1rem 0;
    flex: 1;
}

.sidebar-nav .nav-link {
    color: #94a3b8;
    padding: 0.75rem 1.5rem;
    min-height: var(--touch-target);
    display: flex;
    align-items: center;
    gap: 0.75rem;
    border-left: 3px solid transparent;
    transition: all var(--transition-fast);
    text-decoration: none;
}

.sidebar-nav .nav-link:hover:not(.disabled) {
    color: #fff;
    background: var(--sidebar-hover);
}

.sidebar-nav .nav-link.active {
    color: #fff;
    background: var(--sidebar-hover);
    border-left-color: var(--primary-color);
}

.sidebar-nav .nav-link.disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

.sidebar-nav .nav-section {
    padding: 0.75rem 1.5rem 0.25rem;
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: #64748b;
}

.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.5);
    z-index: 1035;
    opacity: 0;
    transition: opacity var(--transition-sidebar);
}

.sidebar-overlay.show {
    display: block;
    opacity: 1;
}

.app-content {
    flex: 1;
    margin-left: var(--sidebar-width);
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    min-height: 100dvh;
    background: #f1f5f9;
    min-width: 0;
}

.app-header {
    height: var(--header-height);
    min-height: var(--header-height);
    background: #fff;
    border-bottom: 1px solid #e2e8f0;
    display: flex;
    align-items: center;
    padding: 0 var(--desktop-padding);
    position: sticky;
    top: 0;
    z-index: 1020;
    gap: 0.5rem;
}

.sidebar-toggle {
    color: #334155;
    padding: 0.5rem;
    min-width: var(--touch-target);
    min-height: var(--touch-target);
    display: flex;
    align-items: center;
    justify-content: center;
    text-decoration: none;
    border: none;
    background: transparent;
}

.sidebar-toggle:hover {
    color: var(--primary-color);
}

.header-title {
    flex: 1;
    margin-left: 0.25rem;
    min-width: 0;
}

.header-title h5 {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 1rem;
}

.header-actions .user-menu {
    border: 1px solid #e2e8f0;
    border-radius: 0.5rem;
    min-height: var(--touch-target);
    padding: 0.375rem 0.75rem;
}

.main-content {
    padding: var(--desktop-padding);
    flex: 1;
    max-width: 100%;
}

/* Page header */
.crm-page-header {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    align-items: flex-start;
    gap: 1rem;
    margin-bottom: 1.5rem;
}

.crm-page-header__text {
    flex: 1;
    min-width: 0;
}

.crm-page-header__title {
    margin-bottom: 0.25rem;
    font-size: 1.25rem;
    font-weight: 600;
}

.crm-page-header__actions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    align-items: center;
}

/* Breadcrumbs */
.crm-breadcrumb {
    font-size: 0.8125rem;
    margin-bottom: 0.75rem;
}

.crm-breadcrumb .breadcrumb {
    margin-bottom: 0;
    padding: 0;
    background: transparent;
}

.crm-breadcrumb .breadcrumb-item a {
    color: #64748b;
    text-decoration: none;
}

.crm-breadcrumb .breadcrumb-item a:hover {
    color: var(--primary-color);
}

/* Dashboard Stats */
.stat-card {
    background: #fff;
    border-radius: var(--border-radius);
    padding: 1.25rem;
    display: flex;
    align-items: center;
    gap: 1rem;
    box-shadow: var(--shadow-card);
    height: 100%;
    transition: transform var(--transition-fast), box-shadow var(--transition-fast);
}

.stat-card:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.stat-icon {
    width: 52px;
    height: 52px;
    border-radius: var(--border-radius);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.375rem;
    flex-shrink: 0;
}

.stat-primary .stat-icon { background: #eef2ff; color: var(--primary-color); }
.stat-success .stat-icon { background: #ecfdf5; color: #059669; }
.stat-info .stat-icon { background: #ecfeff; color: #0891b2; }
.stat-warning .stat-icon { background: #fffbeb; color: #d97706; }
.stat-danger .stat-icon { background: #fef2f2; color: #dc2626; }

.stat-info h3 {
    margin: 0;
    font-size: 1.5rem;
    font-weight: 700;
    line-height: 1.2;
}

.stat-info p {
    margin: 0;
    color: #64748b;
    font-size: 0.875rem;
}

.module-preview {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 1rem;
    background: #f8fafc;
    border-radius: 0.5rem;
    min-height: var(--touch-target);
    transition: background var(--transition-fast);
}

.module-preview:hover {
    background: #f1f5f9;
}

.module-preview i {
    font-size: 1.5rem;
    flex-shrink: 0;
}

/* Cards */
.card {
    border: none;
    border-radius: var(--border-radius);
    max-width: 100%;
}

.card-header {
    padding: 0.875rem 1.25rem;
    flex-wrap: wrap;
    gap: 0.5rem;
}

/* Filter panel */
.crm-filter-panel .form-label {
    font-size: 0.875rem;
    font-weight: 500;
    margin-bottom: 0.25rem;
}

.crm-filter-panel .form-control,
.crm-filter-panel .form-select {
    min-height: var(--touch-target);
}

.crm-filter-toggle-badge {
    position: absolute;
    top: -4px;
    right: -4px;
    width: 8px;
    height: 8px;
    background: var(--primary-color);
    border-radius: 50%;
}

/* Responsive tables — desktop */
.table-responsive {
    -webkit-overflow-scrolling: touch;
}

.crm-responsive-table thead th {
    position: sticky;
    top: 0;
    z-index: 1;
    background: #f8fafc;
    white-space: nowrap;
}

/* Mobile record cards (auto-enhanced tables) */
@media (max-width: 767.98px) {
    .crm-responsive-table {
        border: none;
    }

    .crm-responsive-table thead {
        display: none;
    }

    .crm-responsive-table tbody tr {
        display: block;
        margin-bottom: 0.75rem;
        padding: 0.875rem;
        background: #fff;
        border: 1px solid #e2e8f0;
        border-radius: 0.625rem;
        box-shadow: var(--shadow-card);
    }

    .crm-responsive-table tbody tr:last-child {
        margin-bottom: 0;
    }

    .crm-responsive-table tbody td {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        gap: 1rem;
        padding: 0.4rem 0;
        border: none;
        text-align: right !important;
    }

    .crm-responsive-table tbody td::before {
        content: attr(data-label);
        font-weight: 600;
        font-size: 0.8125rem;
        color: #64748b;
        text-align: left;
        flex-shrink: 0;
    }

    .crm-responsive-table tbody td[data-label=""]::before,
    .crm-responsive-table tbody td:not([data-label])::before {
        content: none;
    }

    .crm-responsive-table tbody td.crm-td-actions {
        justify-content: flex-end;
        padding-top: 0.75rem;
        margin-top: 0.25rem;
        border-top: 1px solid #f1f5f9;
    }

    .crm-responsive-table tbody td.crm-td-actions::before {
        content: none;
    }

    .crm-responsive-table .btn-group {
        flex-wrap: wrap;
        justify-content: flex-end;
    }

    .table-responsive:has(.crm-responsive-table) {
        overflow-x: visible;
    }
}

/* Permission matrix */
.permission-matrix td,
.permission-matrix th {
    vertical-align: middle;
    min-width: 90px;
}

.permission-matrix select {
    min-width: 110px;
}

.permission-list div {
    padding: 0.2rem 0;
    font-size: 0.875rem;
}

/* Forms — touch-friendly */
.form-control,
.form-select,
.btn {
    font-size: 1rem;
}

.form-control,
.form-select {
    min-height: var(--touch-target);
}

.btn {
    min-height: var(--touch-target);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.5rem 1rem;
}

.btn-sm {
    min-height: 36px;
    padding: 0.25rem 0.625rem;
    font-size: 0.875rem;
}

.btn-group-sm .btn {
    min-height: 36px;
}

textarea.form-control {
    min-height: auto;
}

/* Sticky form actions (mobile) */
.crm-form-sticky-actions {
    display: none;
}

.crm-form-with-sticky {
    padding-bottom: 0;
}

/* Detail page collapsible sections */
.crm-detail-section .card-header {
    cursor: pointer;
    user-select: none;
}

.crm-detail-section .card-header .bi-chevron-down {
    transition: transform var(--transition-fast);
}

.crm-detail-section.collapsed .card-header .bi-chevron-down {
    transform: rotate(-90deg);
}

.crm-detail-section.collapsed .card-body {
    display: none;
}

/* FAB */
.crm-fab {
    position: fixed;
    bottom: 1.25rem;
    right: 1.25rem;
    z-index: 1010;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    box-shadow: 0 4px 14px rgba(79, 70, 229, 0.4);
    display: none;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
}

/* Login Page */
.login-body {
    background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
    min-height: 100vh;
    min-height: 100dvh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
}

.login-wrapper {
    width: 100%;
    max-width: 420px;
}

.login-card {
    background: #fff;
    border-radius: 1rem;
    padding: 2rem;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
}

.login-logo {
    width: 64px;
    height: 64px;
    background: #eef2ff;
    border-radius: 1rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 2rem;
    color: var(--primary-color);
    margin-bottom: 1rem;
}

.login-title {
    font-size: 1.5rem;
    font-weight: 700;
}

.btn-login {
    padding: 0.75rem;
    font-weight: 600;
    min-height: var(--touch-target);
}

.login-footer {
    text-align: center;
}

/* Offcanvas filters */
.offcanvas.crm-filter-offcanvas {
    max-width: 100%;
}

.offcanvas.crm-filter-offcanvas .offcanvas-body .form-control,
.offcanvas.crm-filter-offcanvas .offcanvas-body .form-select,
.offcanvas.crm-filter-offcanvas .offcanvas-body .btn {
    min-height: var(--touch-target);
}

/* Pagination */
.pagination .page-link {
    min-width: var(--touch-target);
    min-height: var(--touch-target);
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Reduce motion */
@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;
    }
}

/* Tablet */
@media (max-width: 1024px) {
    .main-content {
        padding: var(--tablet-padding);
    }

    .app-header {
        padding: 0 var(--tablet-padding);
    }

    .stat-info h3 {
        font-size: 1.375rem;
    }
}

/* Mobile */
@media (max-width: 991.98px) {
    .sidebar {
        transform: translateX(-100%);
    }

    .sidebar.show {
        transform: translateX(0);
        box-shadow: 4px 0 24px rgba(0, 0, 0, 0.15);
    }

    .app-content {
        margin-left: 0;
    }

    body.sidebar-open {
        overflow: hidden;
    }
}

@media (max-width: 767.98px) {
    .main-content {
        padding: var(--mobile-padding);
        padding-bottom: calc(var(--mobile-padding) + env(safe-area-inset-bottom, 0px));
    }

    .app-header {
        padding: 0 var(--mobile-padding);
    }

    .header-title h5 {
        font-size: 0.9375rem;
    }

    .header-actions .user-menu span,
    .header-actions .user-menu .me-1 + text {
        max-width: 100px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .crm-page-header {
        flex-direction: column;
        align-items: stretch;
        margin-bottom: 1rem;
    }

    .crm-page-header__actions {
        width: 100%;
    }

    .crm-page-header__actions .btn {
        flex: 1;
    }

    .crm-page-header__actions .btn-group {
        width: 100%;
    }

    .dashboard-header h4 {
        font-size: 1.125rem;
    }

    .stat-card {
        padding: 1rem;
    }

    .stat-icon {
        width: 44px;
        height: 44px;
        font-size: 1.25rem;
    }

    .stat-info h3 {
        font-size: 1.25rem;
    }

    .card-header {
        flex-direction: column;
        align-items: flex-start !important;
    }

    .card-header .btn {
        width: 100%;
    }

    .crm-form-sticky-actions {
        display: flex;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        z-index: 1025;
        padding: 0.75rem 1rem;
        padding-bottom: calc(0.75rem + env(safe-area-inset-bottom, 0px));
        background: #fff;
        border-top: 1px solid #e2e8f0;
        box-shadow: 0 -4px 12px rgba(0, 0, 0, 0.08);
        gap: 0.5rem;
    }

    .crm-form-sticky-actions .btn {
        flex: 1;
    }

    .crm-form-with-sticky {
        padding-bottom: calc(var(--sticky-actions-height) + env(safe-area-inset-bottom, 0px));
    }

    .crm-form-with-sticky .crm-form-desktop-actions {
        display: none;
    }

    .crm-fab {
        display: flex;
    }

    .permission-matrix {
        font-size: 0.8125rem;
    }

    .permission-matrix select {
        min-width: 80px;
        font-size: 0.8125rem;
    }

    .login-card {
        padding: 1.5rem;
    }

    .module-preview {
        padding: 0.875rem;
    }
}

/* Desktop sticky table headers inside scrollable container */
@media (min-width: 768px) {
    .table-responsive .crm-responsive-table thead th {
        position: sticky;
        top: 0;
    }
}
