/* ============================================
   ANA — Layout: Sidebar, Topbar, Page Shell
   Fixed sidebar + topbar, scrollable content
   ============================================ */

/* App Shell */
.app-shell {
    display: flex;
    height: 100vh;
    width: 100vw;
    overflow: hidden;
}

/* ---- Sidebar ---- */
.sidebar {
    width: var(--sidebar-width);
    min-width: var(--sidebar-width);
    height: 100vh;
    background: var(--color-shell);
    display: flex;
    flex-direction: column;
    position: fixed;
    top: 0;
    left: 0;
    z-index: var(--z-sidebar);
    overflow: hidden;
}

.sidebar-brand {
    height: var(--topbar-height);
    display: flex;
    align-items: center;
    padding: 0 var(--space-lg);
    border-bottom: 1px solid var(--color-shell-border);
    gap: var(--space-sm);
    flex-shrink: 0;
}

.sidebar-brand-logo {
    width: 28px;
    height: 28px;
    border-radius: var(--radius-sm);
    object-fit: contain;
}

.sidebar-brand-logo-placeholder {
    width: 28px;
    height: 28px;
    border-radius: var(--radius-sm);
    background: var(--color-accent);
    display: flex;
    align-items: center;
    justify-content: center;
    font-family: var(--font-display);
    font-weight: var(--weight-bold);
    font-size: var(--text-sm);
    color: var(--color-text-on-primary);
    flex-shrink: 0;
}

.sidebar-brand-name {
    font-family: var(--font-display);
    font-weight: var(--weight-bold);
    font-size: var(--text-md);
    color: var(--color-shell-text-active);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Nav */
.sidebar-nav {
    flex: 1;
    padding: var(--space-md) 0;
    overflow-y: auto;
}

.sidebar-nav-section {
    padding: var(--space-xs) var(--space-lg);
    margin-top: var(--space-lg);
}

.sidebar-nav-section:first-child {
    margin-top: 0;
}

.sidebar-nav-section-label {
    font-size: var(--text-xs);
    font-weight: var(--weight-semibold);
    color: var(--color-shell-text);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    padding-bottom: var(--space-sm);
}

.sidebar-nav-item {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    padding: 8px var(--space-lg);
    color: var(--color-shell-text);
    font-size: var(--text-sm);
    font-weight: var(--weight-medium);
    cursor: pointer;
    transition: all var(--transition-fast);
    border-left: 3px solid transparent;
    user-select: none;
}

.sidebar-nav-item:hover {
    color: var(--color-shell-text-active);
    background: var(--color-shell-hover);
}

.sidebar-nav-item.active {
    color: var(--color-shell-text-active);
    background: var(--color-shell-active);
    border-left-color: var(--color-accent);
}

.sidebar-nav-item svg {
    width: 18px;
    height: 18px;
    flex-shrink: 0;
    stroke-width: 1.75;
}

.sidebar-nav-item .nav-badge {
    margin-left: auto;
    background: var(--color-accent);
    color: var(--color-text-on-primary);
    font-size: 10px;
    font-weight: var(--weight-bold);
    padding: 1px 6px;
    border-radius: var(--radius-full);
    min-width: 18px;
    text-align: center;
}

/* Sidebar Footer */
.sidebar-footer {
    padding: var(--space-md) var(--space-lg);
    border-top: 1px solid var(--color-shell-border);
    flex-shrink: 0;
    position: relative;
}

.sidebar-footer .dropdown-menu {
    top: auto;
    bottom: calc(100% + 4px);
    left: var(--space-lg);
    right: var(--space-lg);
}

.sidebar-user {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    padding: var(--space-sm);
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background var(--transition-fast);
}

.sidebar-user:hover {
    background: var(--color-shell-hover);
}

.sidebar-user-avatar {
    width: 30px;
    height: 30px;
    border-radius: var(--radius-full);
    background: var(--color-shell-active);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: var(--text-xs);
    font-weight: var(--weight-semibold);
    color: var(--color-shell-text-active);
    flex-shrink: 0;
}

.sidebar-user-info {
    flex: 1;
    min-width: 0;
}

.sidebar-user-name {
    font-size: var(--text-sm);
    font-weight: var(--weight-medium);
    color: var(--color-shell-text-active);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.sidebar-user-role {
    font-size: var(--text-xs);
    color: var(--color-shell-text);
}

/* ---- Main Area ---- */
.main-area {
    flex: 1;
    margin-left: var(--sidebar-width);
    display: flex;
    flex-direction: column;
    height: 100vh;
    overflow: hidden;
}

/* ---- Topbar ---- */
.topbar {
    height: var(--topbar-height);
    min-height: var(--topbar-height);
    background: var(--color-shell);
    display: flex;
    align-items: center;
    padding: 0 var(--space-xl);
    gap: var(--space-lg);
    position: sticky;
    top: 0;
    z-index: var(--z-topbar);
    border-bottom: 1px solid var(--color-shell-border);
}

.topbar-title {
    font-family: var(--font-display);
    font-size: var(--text-lg);
    font-weight: var(--weight-bold);
    color: var(--color-shell-text-active);
    white-space: nowrap;
}

.topbar-search {
    flex: 1;
    max-width: 400px;
    position: relative;
}

.topbar-search-input {
    width: 100%;
    height: 32px;
    background: var(--color-shell-hover);
    border: 1px solid var(--color-shell-border);
    border-radius: var(--radius-sm);
    padding: 0 var(--space-sm) 0 32px;
    font-family: var(--font-primary);
    font-size: var(--text-sm);
    color: var(--color-shell-text-active);
    outline: none;
    transition: border-color var(--transition-fast);
}

.topbar-search-input::placeholder {
    color: var(--color-shell-text);
}

.topbar-search-input:focus {
    border-color: var(--color-accent);
}

.topbar-search-icon {
    position: absolute;
    left: 9px;
    top: 50%;
    transform: translateY(-50%);
    width: 15px;
    height: 15px;
    color: var(--color-shell-text);
    pointer-events: none;
}

.topbar-actions {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    margin-left: auto;
}

.topbar-action-btn {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-sm);
    background: none;
    border: none;
    color: var(--color-shell-text);
    cursor: pointer;
    transition: all var(--transition-fast);
    position: relative;
}

.topbar-action-btn:hover {
    background: var(--color-shell-hover);
    color: var(--color-shell-text-active);
}

.topbar-action-btn svg {
    width: 18px;
    height: 18px;
}

.topbar-action-btn .notification-dot {
    position: absolute;
    top: 5px;
    right: 5px;
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--color-accent);
}

/* ---- Sub Menu ---- */
.submenu {
    height: var(--submenu-height);
    min-height: var(--submenu-height);
    background: var(--color-surface);
    border-bottom: 1px solid var(--color-border);
    display: flex;
    align-items: center;
    padding: 0 var(--space-xl);
    gap: var(--space-xl);
    position: sticky;
    top: var(--topbar-height);
    z-index: var(--z-submenu);
}

.submenu-item {
    font-size: var(--text-sm);
    font-weight: var(--weight-medium);
    color: var(--color-text-muted);
    cursor: pointer;
    padding: var(--space-sm) 0;
    border-bottom: 2px solid transparent;
    transition: all var(--transition-fast);
    white-space: nowrap;
    user-select: none;
}

.submenu-item:hover {
    color: var(--color-text);
}

.submenu-item.active {
    color: var(--color-accent);
    border-bottom-color: var(--color-accent);
}

.submenu-right {
    margin-left: auto;
    display: flex;
    align-items: center;
    gap: var(--space-sm);
}

/* ---- Page Content ---- */
.page-content {
    flex: 1;
    overflow-y: auto;
    padding: var(--space-xl);
    background: var(--color-bg);
}

.page-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: var(--space-xl);
}

.page-header h1 {
    font-size: var(--text-2xl);
}

.page-header-actions {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
}

/* ---- Login Layout ---- */
.login-page {
    width: 100vw;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--color-shell);
}

.login-card {
    width: 100%;
    max-width: 380px;
    background: var(--color-surface);
    border-radius: var(--radius-lg);
    padding: var(--space-3xl) var(--space-2xl);
    box-shadow: var(--shadow-lg);
}

.login-brand {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--space-md);
    margin-bottom: var(--space-2xl);
}

.login-brand-logo {
    width: 48px;
    height: 48px;
    border-radius: var(--radius-md);
    background: var(--color-accent);
    display: flex;
    align-items: center;
    justify-content: center;
}

.login-brand-title {
    font-family: var(--font-display);
    font-size: var(--text-xl);
    font-weight: var(--weight-bold);
    color: var(--color-text);
}

.login-brand-subtitle {
    font-size: var(--text-sm);
    color: var(--color-text-muted);
}

/* ---- Sidebar Collapse Toggle ---- */
.sidebar-collapse-btn {
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-sm);
    background: none;
    border: none;
    color: var(--color-shell-text);
    cursor: pointer;
    transition: all var(--transition-fast);
    margin-left: auto;
    flex-shrink: 0;
}

.sidebar-collapse-btn:hover {
    background: var(--color-shell-hover);
    color: var(--color-shell-text-active);
}

.sidebar-collapse-btn svg {
    width: 16px;
    height: 16px;
    transition: transform var(--transition-base);
}

/* ---- Collapsed Sidebar State ---- */
.app-shell.sidebar-collapsed .sidebar {
    width: var(--sidebar-width-collapsed);
    min-width: var(--sidebar-width-collapsed);
}

.app-shell.sidebar-collapsed .main-area {
    margin-left: var(--sidebar-width-collapsed);
}

.app-shell.sidebar-collapsed .sidebar-brand {
    padding: 0;
    justify-content: center;
}

.app-shell.sidebar-collapsed .sidebar-brand-name {
    display: none;
}

.app-shell.sidebar-collapsed .sidebar-collapse-btn {
    display: none;
}

.app-shell.sidebar-collapsed .sidebar-nav-section {
    padding: var(--space-xs) 0;
}

.app-shell.sidebar-collapsed .sidebar-nav-section-label {
    display: none;
}

.app-shell.sidebar-collapsed .sidebar-nav-item {
    padding: 10px 0;
    justify-content: center;
    border-left: 3px solid transparent;
    gap: 0;
}

.app-shell.sidebar-collapsed .sidebar-nav-item span {
    display: none;
}

.app-shell.sidebar-collapsed .sidebar-nav-item .nav-badge {
    display: none;
}

.app-shell.sidebar-collapsed .sidebar-nav-item svg {
    width: 20px;
    height: 20px;
}

.app-shell.sidebar-collapsed .sidebar-footer {
    padding: var(--space-sm);
}

.app-shell.sidebar-collapsed .sidebar-user {
    justify-content: center;
    padding: var(--space-sm);
}

.app-shell.sidebar-collapsed .sidebar-user-info,
.app-shell.sidebar-collapsed .sidebar-user > svg {
    display: none;
}

.app-shell.sidebar-collapsed .sidebar-user-avatar {
    width: 32px;
    height: 32px;
}

/* Collapsed dropdown menu needs to appear to the right */
.app-shell.sidebar-collapsed .sidebar-footer .dropdown-menu {
    left: var(--sidebar-width-collapsed);
    bottom: 0;
    top: auto;
}

/* Tooltip on hover for collapsed nav items */
.app-shell.sidebar-collapsed .sidebar-nav-item {
    position: relative;
}

.app-shell.sidebar-collapsed .sidebar-nav-item::after {
    content: attr(data-tooltip);
    position: absolute;
    left: calc(var(--sidebar-width-collapsed) - 3px);
    top: 50%;
    transform: translateY(-50%);
    background: var(--color-text);
    color: var(--color-text-inverse);
    font-size: var(--text-xs);
    font-weight: var(--weight-medium);
    padding: 4px 8px;
    border-radius: 4px;
    white-space: nowrap;
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--transition-fast);
    z-index: 200;
}

.app-shell.sidebar-collapsed .sidebar-nav-item:hover::after {
    opacity: 1;
}

/* Smooth transition for everything */
.sidebar,
.main-area,
.sidebar-brand,
.sidebar-nav-item,
.sidebar-nav-item span,
.sidebar-nav-section-label,
.sidebar-brand-name,
.sidebar-footer,
.sidebar-user-info {
    transition: all var(--transition-base);
}

/* ============================================
   Portal Shell (Client Role)
   Simpler layout: horizontal nav, no sidebar
   ============================================ */

.portal-shell {
    display: flex;
    flex-direction: column;
    height: 100vh;
    width: 100vw;
    overflow: hidden;
}

.portal-topbar {
    height: var(--topbar-height);
    min-height: var(--topbar-height);
    background: var(--color-shell);
    display: flex;
    align-items: center;
    padding: 0 var(--space-xl);
    gap: var(--space-xl);
    border-bottom: 1px solid var(--color-shell-border);
    flex-shrink: 0;
}

.portal-brand {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    flex-shrink: 0;
}

.portal-nav {
    display: flex;
    align-items: center;
    gap: var(--space-xs);
    flex: 1;
    justify-content: center;
}

.portal-nav-item {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 14px;
    border-radius: var(--radius-sm);
    font-size: var(--text-sm);
    font-weight: var(--weight-medium);
    color: var(--color-shell-text);
    cursor: pointer;
    transition: all var(--transition-fast);
    white-space: nowrap;
    user-select: none;
}

.portal-nav-item:hover {
    color: var(--color-shell-text-active);
    background: var(--color-shell-hover);
}

.portal-nav-item.active {
    color: var(--color-shell-text-active);
    background: var(--color-shell-active);
}

.portal-nav-item svg {
    flex-shrink: 0;
}

.portal-user {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    flex-shrink: 0;
    margin-left: auto;
}

.portal-content {
    flex: 1;
    overflow-y: auto;
    background: var(--color-bg);
}

.portal-content .page-content {
    padding: var(--space-xl);
    max-width: 1200px;
    margin: 0 auto;
}

/* ============================================
   Responsive Breakpoints
   ============================================ */

/* Tablet: sidebar collapses, portal nav wraps */
@media (max-width: 1024px) {
    /* Auto-collapse sidebar on tablet */
    .app-shell .sidebar {
        width: var(--sidebar-width-collapsed);
        min-width: var(--sidebar-width-collapsed);
    }

    .app-shell .main-area {
        margin-left: var(--sidebar-width-collapsed);
    }

    .app-shell .sidebar-brand {
        padding: 0;
        justify-content: center;
    }

    .app-shell .sidebar-brand-name,
    .app-shell .sidebar-collapse-btn,
    .app-shell .sidebar-nav-section-label,
    .app-shell .sidebar-nav-item span,
    .app-shell .sidebar-nav-item .nav-badge,
    .app-shell .sidebar-user-info,
    .app-shell .sidebar-user > svg {
        display: none;
    }

    .app-shell .sidebar-nav-item {
        padding: 10px 0;
        justify-content: center;
        border-left: 3px solid transparent;
        gap: 0;
    }

    .app-shell .sidebar-nav-section {
        padding: var(--space-xs) 0;
    }

    .app-shell .sidebar-footer {
        padding: var(--space-sm);
    }

    .app-shell .sidebar-user {
        justify-content: center;
        padding: var(--space-sm);
    }

    .app-shell .sidebar-user-avatar {
        width: 32px;
        height: 32px;
    }

    /* Portal: hide nav text labels on tablet */
    .portal-nav-item span {
        display: none;
    }

    .portal-nav-item {
        padding: 8px 10px;
    }

    /* Topbar search shrinks */
    .topbar-search {
        max-width: 240px;
    }
}

/* Mobile: overlay sidebar, stacked portal nav */
@media (max-width: 768px) {
    /* Admin shell: hide sidebar entirely on mobile */
    .app-shell .sidebar {
        display: none;
    }

    .app-shell .main-area {
        margin-left: 0;
    }

    /* Topbar adjustments */
    .topbar {
        padding: 0 var(--space-md);
        gap: var(--space-sm);
    }

    .topbar-search {
        max-width: none;
        flex: 1;
    }

    .topbar-title {
        font-size: var(--text-md);
    }

    /* Submenu scrollable */
    .submenu {
        padding: 0 var(--space-md);
        gap: var(--space-md);
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    .submenu-right {
        flex-shrink: 0;
    }

    /* Page content */
    .page-content {
        padding: var(--space-md);
    }

    /* Portal: stack nav below brand */
    .portal-topbar {
        flex-wrap: wrap;
        height: auto;
        min-height: auto;
        padding: var(--space-sm) var(--space-md);
        gap: var(--space-sm);
    }

    .portal-brand {
        flex: 1;
    }

    .portal-user {
        order: 2;
    }

    .portal-nav {
        order: 3;
        width: 100%;
        justify-content: flex-start;
        gap: var(--space-xs);
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        padding-bottom: var(--space-xs);
    }

    .portal-nav-item {
        font-size: var(--text-xs);
        padding: 6px 10px;
    }

    .portal-nav-item span {
        display: inline;
    }

    .portal-content .page-content {
        padding: var(--space-md);
    }

    /* Tables: horizontal scroll */
    .data-table-wrapper,
    table {
        display: block;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    /* Stat grids: 2 columns */
    .stat-grid {
        grid-template-columns: repeat(2, 1fr) !important;
    }

    /* Login card */
    .login-card {
        margin: var(--space-md);
        max-width: none;
    }
}

/* Small mobile */
@media (max-width: 480px) {
    .stat-grid {
        grid-template-columns: 1fr !important;
    }

    .portal-nav-item {
        font-size: 11px;
        padding: 5px 8px;
    }

    .topbar-title {
        font-size: var(--text-sm);
    }

    /* Search dropdown full width */
    .search-dropdown {
        position: fixed;
        left: var(--space-sm);
        right: var(--space-sm);
        top: var(--topbar-height);
    }
}
