/* ============================================
   CTC DASH — Layout
   Reset, base, sidebar, footer, login,
   responsive breakpoints, and theme toggle
   ============================================ */


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

html {
    font-size: 15px;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    scroll-behavior: smooth;
    overflow-x: hidden;
}

body {
    font-family: var(--font-body);
    font-weight: 400;
    color: var(--text-primary);
    background: var(--surface-0);
    line-height: 1.6;
    min-height: 100vh;
    display: flex;
    overflow-x: hidden;
    width: 100%;
}

/* Grain overlay for texture */
.grain-overlay {
    position: fixed;
    inset: 0;
    pointer-events: none;
    z-index: 9999;
    opacity: var(--grain-opacity, 0.015);
    background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='noise'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23noise)'/%3E%3C/svg%3E");
    background-repeat: repeat;
    background-size: 256px 256px;
}


/* --- Container --- */
.container {
    max-width: var(--container-max);
    margin: 0 auto;
    padding: 0 2rem;
    width: 100%;
}


/* --- Sidebar --- */
.sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 260px;
    background: var(--nav-bg);
    backdrop-filter: blur(20px) saturate(1.3);
    -webkit-backdrop-filter: blur(20px) saturate(1.3);
    border-right: 1px solid var(--border-subtle);
    display: flex;
    flex-direction: column;
    z-index: 200;
    transition: width 0.25s cubic-bezier(0.4, 0, 0.2, 1);
    overflow-y: auto;
    overflow-x: hidden;
}

.sidebar-top {
    flex: 1;
    min-height: 0;
}

.sidebar-brand {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1rem 1.25rem;
    border-bottom: 1px solid var(--border-subtle);
}

.sidebar-brand-link {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    text-decoration: none;
    color: var(--text-primary);
}

.brand-icon {
    color: var(--accent);
    flex-shrink: 0;
}

.brand-text {
    font-family: var(--font-display);
    font-weight: 700;
    font-size: 1.2rem;
    letter-spacing: -0.02em;
    white-space: nowrap;
}

.brand-accent {
    color: var(--accent);
}

.sidebar-collapse-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    padding: 0;
    border: none;
    border-radius: 6px;
    background: transparent;
    color: var(--text-muted);
    cursor: pointer;
    transition: color 0.2s, background 0.2s;
    flex-shrink: 0;
}

.sidebar-collapse-btn:hover {
    color: var(--text-primary);
    background: var(--surface-3);
    box-shadow: none;
    transform: none;
}

/* Collapsed state */
.sidebar.collapsed {
    width: 64px;
}

.sidebar.collapsed .brand-text,
.sidebar.collapsed .sidebar-link-text,
.sidebar.collapsed .sidebar-section-header,
.sidebar.collapsed .sidebar-badge,
.sidebar.collapsed .sidebar-user-info,
.sidebar.collapsed .sidebar-theme-label {
    display: none;
}

.sidebar.collapsed .sidebar-section {
    border-top: none;
}

.sidebar.collapsed .sidebar-collapse-btn svg {
    transform: rotate(180deg);
}

.sidebar.collapsed .sidebar-brand {
    justify-content: center;
    padding: 1rem 0.5rem;
}

.sidebar.collapsed .sidebar-brand-link {
    justify-content: center;
}

.sidebar.collapsed .sidebar-link {
    justify-content: center;
    padding: 0.6rem;
}

.sidebar.collapsed .sidebar-theme {
    justify-content: center;
    padding: 0.5rem;
}

.sidebar.collapsed .sidebar-user {
    justify-content: center;
    padding: 0.75rem 0.5rem;
}

.sidebar.collapsed .sidebar-logout {
    margin-left: 0;
}

/* Sidebar navigation */
.sidebar-nav {
    display: flex;
    flex-direction: column;
    gap: 2px;
    padding: 0.5rem 0.75rem;
}

.sidebar-section {
    border-top: 1px solid var(--border-subtle);
}

.sidebar-section-header {
    font-family: var(--font-display);
    font-size: 0.68rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--text-muted);
    padding: 1rem 1rem 0.25rem;
    white-space: nowrap;
}

.sidebar-link {
    display: flex;
    align-items: center;
    gap: 0.65rem;
    padding: 0.55rem 0.75rem;
    border-radius: var(--radius-md);
    text-decoration: none;
    color: var(--text-secondary);
    font-family: var(--font-display);
    font-weight: 500;
    font-size: 0.88rem;
    letter-spacing: 0.01em;
    transition: all 0.15s ease;
    white-space: nowrap;
    position: relative;
}

.sidebar-link:hover {
    color: var(--text-primary);
    background: var(--surface-3);
}

.sidebar-link.active {
    color: var(--nav-active-color);
    background: var(--nav-active-bg);
    border: 1px solid var(--border-accent);
}

.sidebar-link svg {
    flex-shrink: 0;
    opacity: 0.7;
    width: 20px;
    height: 20px;
}

.sidebar-link:hover svg,
.sidebar-link.active svg {
    opacity: 1;
}

.sidebar-link-text {
    white-space: nowrap;
    overflow: hidden;
}

.sidebar-badge {
    margin-left: auto;
    min-width: 20px;
    height: 20px;
    padding: 0 6px;
    border-radius: 9999px;
    background: var(--status-danger);
    color: #fff;
    font-size: 0.68rem;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
    flex-shrink: 0;
}

/* Sidebar bottom area */
.sidebar-bottom {
    margin-top: auto;
    border-top: 1px solid var(--border-subtle);
    padding: 0.5rem 0.75rem 0.75rem;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.sidebar-theme {
    display: flex;
    align-items: center;
    gap: 0.65rem;
    padding: 0.55rem 0.75rem;
}

.sidebar-theme-label {
    font-family: var(--font-display);
    font-size: 0.88rem;
    font-weight: 500;
    color: var(--text-secondary);
    white-space: nowrap;
}

.sidebar-user {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    padding: 0.75rem;
    margin-top: 0.25rem;
    background: var(--surface-2);
    border-radius: var(--radius-md);
    border: 1px solid var(--border-subtle);
}

.sidebar-user-info {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    min-width: 0;
    overflow: hidden;
}

.sidebar-user-name {
    font-family: var(--font-display);
    font-size: 0.82rem;
    font-weight: 500;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.sidebar-logout {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border-radius: 6px;
    color: var(--text-muted);
    text-decoration: none;
    transition: color 0.2s, background 0.2s;
    flex-shrink: 0;
}

.sidebar-logout:hover {
    color: var(--grade-poor);
    background: rgba(239, 68, 68, 0.08);
}


/* --- Mobile header & sidebar overlay --- */
.mobile-header {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 56px;
    background: var(--nav-bg);
    backdrop-filter: blur(20px) saturate(1.3);
    -webkit-backdrop-filter: blur(20px) saturate(1.3);
    border-bottom: 1px solid var(--border-subtle);
    align-items: center;
    gap: 0.75rem;
    padding: 0 1rem;
    z-index: 150;
}

.mobile-menu-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    padding: 0;
    border: none;
    border-radius: 8px;
    background: transparent;
    color: var(--text-primary);
    cursor: pointer;
}

.mobile-menu-btn:hover {
    background: var(--surface-3);
    box-shadow: none;
    transform: none;
}

.mobile-brand {
    font-family: var(--font-display);
    font-weight: 700;
    font-size: 1.1rem;
    color: var(--text-primary);
    text-decoration: none;
}

.sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 190;
}

.sidebar-backdrop.active {
    display: block;
}


/* --- Main wrapper --- */
.main-wrapper {
    margin-left: 260px;
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    transition: margin-left 0.25s cubic-bezier(0.4, 0, 0.2, 1);
    min-width: 0;
    overflow-x: clip;
}

.main-wrapper.sidebar-collapsed {
    margin-left: 64px;
}


/* --- Module Top Navigation --- */
.module-topnav {
    background: var(--surface-1);
    border-bottom: 1px solid var(--border-subtle);
    position: sticky;
    top: 0;
    z-index: 100;
}

.topnav-tabs {
    display: flex;
    align-items: center;
    gap: 0.15rem;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    -ms-overflow-style: none;
}

.topnav-tabs::-webkit-scrollbar {
    display: none;
}

.topnav-tab {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.8rem 1rem;
    font-family: var(--font-display);
    font-weight: 500;
    font-size: 0.84rem;
    letter-spacing: 0.01em;
    color: var(--text-muted);
    text-decoration: none;
    white-space: nowrap;
    border-bottom: 2px solid transparent;
    margin-bottom: -1px;
    transition: color 0.15s ease, border-color 0.15s ease, background 0.15s ease;
}

.topnav-tab:hover {
    color: var(--text-primary);
    background: var(--surface-hover);
}

.topnav-tab.active {
    color: var(--accent);
    border-bottom-color: var(--accent);
    font-weight: 600;
}

.topnav-tab svg {
    flex-shrink: 0;
    opacity: 0.6;
}

.topnav-tab:hover svg,
.topnav-tab.active svg {
    opacity: 1;
}

.topnav-badge {
    min-width: 18px;
    height: 18px;
    padding: 0 5px;
    border-radius: 9999px;
    background: var(--status-danger);
    color: #fff;
    font-size: 0.65rem;
    font-weight: 700;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
    flex-shrink: 0;
}

/* Topnav group labels & dividers */
.topnav-group-label {
    display: inline-flex;
    align-items: center;
    padding: 0 0.2rem;
    font-family: var(--font-display);
    font-size: 0.6rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-muted);
    white-space: nowrap;
    user-select: none;
}

.topnav-group-divider {
    width: 1px;
    height: 18px;
    background: var(--border-subtle);
    flex-shrink: 0;
    margin: 0 0.25rem;
    align-self: center;
}

/* Reports hub cards */
.hub-category {
    margin-bottom: 2rem;
}

.hub-category-header {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 1rem;
    padding-bottom: 0.5rem;
    border-bottom: 2px solid var(--cat, var(--accent));
}

.hub-category-header h3 {
    font-family: var(--font-display);
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--text-primary);
    margin: 0;
}

.hub-category-header svg {
    width: 20px;
    height: 20px;
    color: var(--cat, var(--accent));
    flex-shrink: 0;
}

.hub-cards {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 1rem;
}

.hub-card {
    display: flex;
    align-items: flex-start;
    gap: 1rem;
    padding: 1.25rem;
    background: var(--surface-1);
    border: 1px solid var(--border-subtle);
    border-left: 3px solid var(--cat, var(--accent));
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-card);
    text-decoration: none;
    color: inherit;
    position: relative;
    transition: border-color 0.2s ease, box-shadow 0.2s ease, transform 0.15s ease;
}

.hub-card:hover {
    border-color: var(--cat, var(--accent));
    border-left-color: var(--cat, var(--accent));
    box-shadow: var(--shadow-elevated);
    transform: translateY(-2px);
}

.hub-card-icon {
    flex-shrink: 0;
    width: 44px;
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--cat-glow, var(--accent-glow));
    border-radius: var(--radius-md);
}

.hub-card-icon svg {
    width: 22px;
    height: 22px;
    color: var(--cat, var(--accent));
}

.hub-card-body {
    flex: 1;
    min-width: 0;
}

.hub-card-body h4 {
    font-family: var(--font-display);
    font-size: 0.95rem;
    font-weight: 600;
    margin: 0 0 0.25rem;
    color: var(--text-primary);
}

.hub-card-body p {
    font-size: 0.82rem;
    color: var(--text-secondary);
    margin: 0;
    line-height: 1.5;
}

.hub-card-arrow {
    position: absolute;
    top: 1.25rem;
    right: 1.25rem;
    width: 18px;
    height: 18px;
    color: var(--cat, var(--accent));
    opacity: 0;
    transition: opacity 0.15s ease, transform 0.15s ease;
}

.hub-card:hover .hub-card-arrow {
    opacity: 0.8;
    transform: translateX(3px);
}

/* Sidebar module icon sizing */
.sidebar-module-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.sidebar-module-icon svg {
    width: 20px;
    height: 20px;
}


/* --- Main Content --- */
.main-content {
    flex: 1;
    padding: 2.5rem 0 4rem;
    min-width: 0;
    max-width: 100%;
}


/* --- Footer --- */
.site-footer {
    border-top: 1px solid var(--border-subtle);
    padding: 1.5rem 0;
}

.site-footer .container {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.footer-text {
    font-family: var(--font-display);
    font-size: 0.8rem;
    font-weight: 500;
    color: var(--text-muted);
    letter-spacing: 0.04em;
    text-transform: uppercase;
}

.footer-muted {
    opacity: 0.6;
}

.footer-divider {
    width: 1px;
    height: 12px;
    background: var(--border-default);
}


/* --- Login page --- */
.login-page {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    padding: 2rem;
    width: 100%;
}

.login-card {
    width: 100%;
    max-width: 380px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 2.5rem 2rem;
    box-shadow: 0 4px 24px rgba(0, 0, 0, 0.06);
}

.login-header {
    text-align: center;
    margin-bottom: 2rem;
}

.login-header svg {
    color: var(--accent);
    margin-bottom: 0.75rem;
}

.login-header h1 {
    font-family: 'Outfit', sans-serif;
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--text-primary);
    margin: 0;
}

.login-tagline {
    font-size: 0.85rem;
    color: var(--text-muted);
    margin-top: 0.35rem;
    font-weight: 400;
    letter-spacing: 0.02em;
}

.login-form label {
    display: block;
    font-size: 0.85rem;
    font-weight: 500;
    color: var(--text-secondary);
    margin-bottom: 1rem;
}

.login-form input {
    width: 100%;
    margin-top: 0.35rem;
    padding: 0.6rem 0.75rem;
    font-size: 0.9rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--bg);
    color: var(--text-primary);
    transition: border-color 0.2s;
}

.login-form input:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(0, 166, 81, 0.1);
}

.login-btn {
    width: 100%;
    padding: 0.7rem;
    margin-top: 0.5rem;
    font-size: 0.95rem;
    font-weight: 600;
    color: #fff;
    background: var(--accent);
    border: none;
    border-radius: 8px;
    cursor: pointer;
    transition: background 0.2s, transform 0.1s;
}

.login-btn:hover {
    background: var(--accent-deep);
}

.login-btn:active {
    transform: scale(0.98);
}

.login-error {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.65rem 0.85rem;
    margin-bottom: 1.25rem;
    font-size: 0.85rem;
    color: var(--grade-poor);
    background: rgba(239, 68, 68, 0.08);
    border: 1px solid rgba(239, 68, 68, 0.2);
    border-radius: 8px;
}


/* --- Responsive --- */
@media (max-width: 768px) {
    .container {
        padding: 0 1rem;
        max-width: 100%;
    }

    /* Hide sidebar on mobile, show mobile header */
    .sidebar {
        transform: translateX(-100%);
        transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        width: 280px;
    }

    .sidebar.mobile-open {
        transform: translateX(0);
    }

    .sidebar.collapsed {
        width: 280px;
    }

    .sidebar.collapsed .brand-text,
    .sidebar.collapsed .sidebar-link-text,
    .sidebar.collapsed .sidebar-section-header,
    .sidebar.collapsed .sidebar-badge,
    .sidebar.collapsed .sidebar-user-info,
    .sidebar.collapsed .sidebar-theme-label {
        display: unset;
    }

    .sidebar.collapsed .sidebar-link,
    .sidebar.collapsed .sidebar-theme {
        justify-content: flex-start;
        padding: 0.55rem 0.75rem;
    }

    .sidebar.collapsed .sidebar-brand {
        justify-content: space-between;
        padding: 1rem 1.25rem;
    }

    .sidebar.collapsed .sidebar-brand-link {
        justify-content: flex-start;
    }

    .sidebar.collapsed .sidebar-user {
        justify-content: space-between;
        padding: 0.75rem;
    }

    .mobile-header {
        display: flex;
    }

    .sidebar-collapse-btn {
        display: none;
    }

    .main-wrapper {
        margin-left: 0;
        padding-top: 56px;
        width: 100%;
        max-width: 100vw;
    }

    .main-wrapper.sidebar-collapsed {
        margin-left: 0;
    }

    .module-topnav {
        top: 56px;
        overflow: hidden;
    }

    .topnav-tabs {
        padding: 0 0.5rem;
    }

    .topnav-tab {
        padding: 0.65rem 0.6rem;
        font-size: 0.78rem;
        gap: 0.25rem;
    }

    .topnav-tab svg {
        width: 14px;
        height: 14px;
    }

    .topnav-group-label {
        font-size: 0.55rem;
        padding: 0 0.1rem;
    }

    .topnav-group-divider {
        height: 14px;
        margin: 0 0.15rem;
    }

    .hub-cards {
        grid-template-columns: 1fr;
        gap: 0.75rem;
    }

    .grade-hero {
        grid-template-columns: 1fr;
    }

    h1 { font-size: 1.6rem; }
    h2 { font-size: 1.3rem; }

    .vehicle-hero h2 {
        font-size: 1.5rem;
    }

    .stat-value {
        font-size: 1.4rem;
    }

    fieldset {
        padding: 1.25rem;
    }

    .stat-row {
        grid-template-columns: 1fr 1fr;
    }

    .main-content {
        padding: 1.5rem 0 3rem;
    }

    /* Ensure tables don't overflow */
    .table-wrap {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
}

@media (max-width: 480px) {
    .stat-row {
        grid-template-columns: 1fr;
    }
}


/* --- Theme Toggle Switch --- */
.theme-toggle {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.theme-switch {
    position: relative;
    width: 52px;
    height: 28px;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    flex-shrink: 0;
}

.theme-switch input {
    display: none;
}

.theme-switch .switch-track {
    position: absolute;
    inset: 0;
    background: var(--surface-3);
    border: 1px solid var(--border-default);
    border-radius: 100px;
    transition: background 0.3s ease, border-color 0.3s ease;
}

.theme-switch:hover .switch-track {
    border-color: var(--border-strong);
}

.theme-switch .switch-thumb {
    position: absolute;
    top: 3px;
    left: 3px;
    width: 22px;
    height: 22px;
    background: var(--accent);
    border-radius: 50%;
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1), background 0.3s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 1px 3px rgba(0,0,0,0.2);
    z-index: 1;
}

.theme-switch input:checked ~ .switch-thumb {
    transform: translateX(24px);
}

.theme-switch .switch-icon {
    width: 14px;
    height: 14px;
    color: var(--text-inverse);
    flex-shrink: 0;
}

/* Sun icon (light mode) — visible when unchecked */
.theme-switch .icon-sun {
    display: block;
}

.theme-switch .icon-moon {
    display: none;
}

/* Moon icon (dark mode) — visible when checked */
.theme-switch input:checked ~ .switch-thumb .icon-sun {
    display: none;
}

.theme-switch input:checked ~ .switch-thumb .icon-moon {
    display: block;
}

/* Light mode specific: cards get subtle shadows */
[data-theme="light"] article,
[data-theme="light"] .card,
[data-theme="light"] .table-wrap,
[data-theme="light"] .stat-card,
[data-theme="light"] .filter-bar,
[data-theme="light"] .disposition-card,
[data-theme="light"] .score-card,
[data-theme="light"] .grade-hero-card,
[data-theme="light"] .summary-block,
[data-theme="light"] fieldset,
[data-theme="light"] .photo-completeness,
[data-theme="light"] .confidence-info {
    box-shadow: var(--shadow-card);
}

/* Light mode: topnav subtle shadow */
[data-theme="light"] .module-topnav {
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.04);
}

/* Light mode: grade badge text stays white */
[data-theme="light"] .grade-badge {
    color: #ffffff;
}

/* Light mode: analysis prompt gradient adjust */
[data-theme="light"] .analysis-prompt {
    background: linear-gradient(135deg, var(--brand-tint), var(--surface-1));
}

/* Light mode: select dropdown arrow darker */
[data-theme="light"] select {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%235a6359' stroke-width='2'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
}

/* Smooth transition for theme change */
html.theme-transition,
html.theme-transition *,
html.theme-transition *::before,
html.theme-transition *::after {
    transition: background-color 0.3s ease,
                color 0.3s ease,
                border-color 0.3s ease,
                box-shadow 0.3s ease,
                opacity 0.3s ease !important;
}

@media (max-width: 768px) {
    .theme-switch {
        width: 44px;
        height: 24px;
    }

    .theme-switch .switch-thumb {
        width: 18px;
        height: 18px;
    }

    .theme-switch input:checked ~ .switch-thumb {
        transform: translateX(20px);
    }

    .theme-switch .switch-icon {
        width: 11px;
        height: 11px;
    }
}
