/* Core Sidebar Styles - Clean and Consolidated */
/* apps/core/static/core/css/sidebar.css */

/* ============================================
   CSS CUSTOM PROPERTIES
   ============================================ */
:root {
    --sidebar-width: 16rem;
    --navbar-height: 3.5rem;
    --sidebar-z-index: 65; /* raised so it always sits at top */
    --overlay-z-index: 64; /* just beneath sidebar, above nav & page */
    
    /* Colors */
    --sidebar-bg: #ffffff;
    --sidebar-text: #374151;
    --sidebar-text-hover: #111827;
    --sidebar-hover-bg: #f9fafb;
    --sidebar-focus-bg: #f3f4f6;
    --sidebar-icon-color: #6b7280;
    --sidebar-border: #e5e7eb;
    --sidebar-active-bg: #eff6ff;
    --sidebar-active-text: #2563eb;
    
    /* Dark mode colors */
    --sidebar-dark-bg: #181818;
    --sidebar-dark-text: #fff;
    --sidebar-dark-hover-bg: #414141;
    --sidebar-dark-border: #ffffff26;
}

/* ============================================
   SIDEBAR CORE STRUCTURE
   ============================================ */
#sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: var(--sidebar-width);
    /* height: 100vh; */
    z-index: var(--sidebar-z-index);
    background-color: var(--sidebar-bg);
    border-right: 1px solid var(--sidebar-border);
    transition: transform 0.3s ease;
}

/* Sidebar transform states */
.sidebar-transform {
    transform: translateX(-100%);
    transition: transform 0.3s ease;
}

.sidebar-transform.active,
.sidebar-transform.open,
.sidebar-transform.sidebar-show {
    transform: translateX(0);
}

/* Disable transitions for initial paint */
.sidebar-instant {
    transition: none !important;
}

/* ============================================
   SIDEBAR OVERLAY
   ============================================ */
#sidebar-overlay,
.sidebar-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: var(--overlay-z-index);
    background: #0d0d0d80; /* palette scrim */
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition: opacity 0.25s ease, visibility 0.25s ease;
    inset: 0; /* ensure full viewport coverage */
}

/* Fallback: show overlay whenever body indicates sidebar open (mobile only) */
@media (max-width: 767px) {
  body.sidebar-open #sidebar-overlay,
  body.sidebar-open-mobile #sidebar-overlay {
      opacity: 1;
      visibility: visible;
      pointer-events: auto;
  }
}

/* Guarantee stacking order */
#sidebar-overlay {
    z-index: calc(var(--sidebar-z-index) - 1) !important;
}

/* Optional: only darken area outside sidebar on larger screens */
@media (min-width: 768px) {
    body.sidebar-open #sidebar-overlay {
        display: block !important;
        left: var(--sidebar-width);
        width: calc(100% - var(--sidebar-width));
    }
}

/* On mobile keep full screen overlay */
@media (max-width: 767px) {
    body.sidebar-open-mobile #sidebar-overlay {
        left: 0;
        width: 100%;
    }
}

/* Prevent background scroll & ensure overlay visible when sidebar open */
body.sidebar-open, body.sidebar-open-mobile {
    overflow: hidden;
}

/* ============================================
   SIDEBAR HEADER
   ============================================ */
.sidebar-header {
    position: sticky;
    top: 0;
    height: var(--navbar-height);
    background: var(--sidebar-bg);
    border-bottom: 1px solid var(--sidebar-border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.5rem;
    z-index: 1;
}

.sidebar-logo {
    font-family: 'NichromeDark', sans-serif;
    font-size: 1.875rem;
    font-weight: normal;
    letter-spacing: 0.025em;
    color: #000000;
    text-decoration: none;
    padding-left: 0.25rem;
    transition: opacity 0.2s ease;
}

.sidebar-logo:hover {
    text-decoration: none;
}

.sidebar-logo span {
    display: inline;
}

/* ============================================
   SIDEBAR NAVIGATION
   ============================================ */
.sidebar-nav {
    padding: 0rem;
    overflow-y: auto;
    height: calc(100vh - var(--navbar-height));
}

/* Base link styles */
.sidebar-link,
.sidebar-link-chat,
.sidebar-item a,
.sidebar-item button {
    display: flex;
    align-items: center;
    width: 100%;
    /* padding: 0.75rem; */
    color: var(--sidebar-text);
    text-decoration: none;
    font-family: 'Lexend', sans-serif;
    font-size: 0.875rem;
    font-weight: 400 !important;
    background: transparent;
    border: none;
    border-radius: 0.5rem;
    cursor: pointer;
    text-align: left;
    transition: all 0.2s ease-in-out;
}

/* Chat-specific link styling */
.sidebar-link-chat {
    padding: 0.4rem 0.75rem;
}

/* Dedicated styling for the primary CTA at top of sidebar */
#sidebar .sidebar-item .sidebar-primary-action,
#sidebar .sidebar-primary-action {
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    gap: 0.15rem;
    width: 100%;
    padding: 0.65rem 0.85rem !important;
    border: 1px solid #d1d5db !important;
    border-radius: 0.5rem !important;
    background-color: #ffffff !important;
    color: #1f2937 !important;
    text-decoration: none !important;
    font-family: 'Lexend', sans-serif;
    font-size: 0.875rem;
    font-weight: 400;
    transition: background-color 0.2s ease, border-color 0.2s ease, color 0.2s ease;
}

#sidebar .sidebar-item .sidebar-primary-action:hover,
#sidebar .sidebar-item .sidebar-primary-action:focus,
#sidebar .sidebar-primary-action:hover,
#sidebar .sidebar-primary-action:focus {
    background-color: #f3f4f6 !important;
    border-color: #cbd5d5 !important;
    color: #111827 !important;
    outline: none;
}

#sidebar .sidebar-item .sidebar-primary-action__icon,
#sidebar .sidebar-primary-action__icon {
    width: 1.125rem;
    height: 1.125rem;
    color: inherit;
}

#sidebar .sidebar-item .sidebar-primary-action .nav-label,
#sidebar .sidebar-primary-action .nav-label {
    margin: 0;
    color: inherit;
    font-weight: 400;
}

body.dark-mode #sidebar .sidebar-item .sidebar-primary-action,
body.dark-mode #sidebar .sidebar-primary-action {
    background-color: #212121 !important;
    border-color: #383838 !important;
    color: #f5f5f5 !important;
}

body.dark-mode #sidebar .sidebar-item .sidebar-primary-action:hover,
body.dark-mode #sidebar .sidebar-item .sidebar-primary-action:focus,
body.dark-mode #sidebar .sidebar-primary-action:hover,
body.dark-mode #sidebar .sidebar-primary-action:focus {
    background-color: #2c2c2c !important;
    border-color: #4b4b4b !important;
    color: #ffffff !important;
}

/* ============================================
   CRITICAL: CROSS-DEVICE VISIBILITY GUARANTEE
   Ensures primary action button renders on ALL devices and browsers
   ============================================ */

/* Mobile devices (phones, small tablets) */
@media (max-width: 767px) {
    #sidebar .sidebar-item[data-component="primary-cta"],
    #sidebar .sidebar-item .sidebar-primary-action,
    #sidebar .sidebar-primary-action {
        display: flex !important;
        visibility: visible !important;
        opacity: 1 !important;
        position: relative !important;
        pointer-events: auto !important;
    }
    
    /* Ensure parent container is visible */
    #sidebar .sidebar-item[data-component="primary-cta"] {
        display: block !important;
    }
}

/* Tablet and desktop devices */
@media (min-width: 768px) {
    #sidebar .sidebar-item[data-component="primary-cta"],
    #sidebar .sidebar-item .sidebar-primary-action,
    #sidebar .sidebar-primary-action {
        display: flex !important;
        visibility: visible !important;
        opacity: 1 !important;
        pointer-events: auto !important;
    }
    
    /* Ensure parent container is visible */
    #sidebar .sidebar-item[data-component="primary-cta"] {
        display: block !important;
    }
}

/* Fallback for any viewport size - last resort */
.sidebar-primary-action[data-action="new-chat"] {
    display: flex !important;
    visibility: visible !important;
    opacity: 1 !important;
}

/* Hover states */
.sidebar-link:hover,
.sidebar-link-chat:hover,
.sidebar-item a:hover,
.sidebar-item button:hover {
    background-color: var(--sidebar-hover-bg);
    color: var(--sidebar-text-hover);
    text-decoration: none;
}

/* Focus states */
.sidebar-link:focus,
.sidebar-link-chat:focus,
.sidebar-item a:focus,
.sidebar-item button:focus {
    outline: none;
    background-color: var(--sidebar-focus-bg);
}

/* Active states */
.sidebar-link.active {
    background-color: var(--sidebar-active-bg);
    color: var(--sidebar-active-text);
}

/* ============================================
   NAVIGATION ICONS AND LABELS
   ============================================ */
.nav-icon {
    flex: 0 0 1.25rem;
    width: 1.25rem;
    height: 1.25rem;
    margin-right: 0.75rem;
    color: var(--sidebar-icon-color);
}

.nav-label {
    font-family: 'Lexend', sans-serif;
    font-size: 0.875rem;
    font-weight: 300;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* ============================================
   TOGGLE BUTTONS
   ============================================ */
.sidebar-toggle-btn,
.sidebar-collapse-btn {
    display: none;
    background: transparent;
    border: none;
    padding: 0.5rem;
    border-radius: 0.375rem;
    color: var(--sidebar-icon-color);
    cursor: pointer;
    transition: all 0.2s ease-in-out;
}

.sidebar-toggle-btn:hover,
.sidebar-collapse-btn:hover {
    background-color: var(--sidebar-focus-bg);
    color: var(--sidebar-text-hover);
}

/* ============================================
   SPECIAL SECTIONS
   ============================================ */
#recent-chats-section {
    margin-bottom: 1rem;
}

#recent-chats-loading {
    padding: 0.75rem;
    font-size: 0.875rem;
    color: var(--sidebar-icon-color);
}

/* ============================================
   BUTTON OVERRIDES
   ============================================ */
.sidebar-btn {
    box-shadow: none;
}

.sidebar-btn .nav-icon {
    flex: 0 0 1.125rem;
}

.sidebar-btn .nav-label {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Remove accent button styling in sidebar */
#sidebar .btn-accent,
#sidebar button.btn-accent {
    background: transparent !important;
    color: var(--sidebar-text) !important;
    border-radius: 0.5rem !important;
}

#sidebar .btn-accent:hover,
#sidebar button.btn-accent:hover {
    background-color: var(--sidebar-hover-bg) !important;
    color: var(--sidebar-text-hover) !important;
}

/* ============================================
   DARK MODE STYLES
   ============================================ */
body.dark-mode #sidebar {
    background-color: var(--sidebar-dark-bg);
    color: var(--sidebar-dark-text);
    border-right-color: var(--sidebar-dark-border);
}

body.dark-mode .sidebar-header,
body.dark-mode .sidebar-nav {
    background-color: var(--sidebar-dark-bg);
    border-color: var(--sidebar-dark-border);
}

body.dark-mode .sidebar-logo {
    color: var(--sidebar-dark-text);
}

body.dark-mode .sidebar-logo span:first-child {
    color: var(--sidebar-dark-text) !important;
}

body.dark-mode .sidebar-logo span:last-child {
    color: #FF6C42 !important;
}

body.dark-mode .sidebar-link,
body.dark-mode .sidebar-link-chat,
body.dark-mode .sidebar-item a,
body.dark-mode .sidebar-item button,
body.dark-mode .nav-label {
    color: var(--sidebar-dark-text);
    font-weight: 300;
}

body.dark-mode .sidebar-link:hover,
body.dark-mode .sidebar-link-chat:hover,
body.dark-mode .sidebar-item a:hover,
body.dark-mode .sidebar-item button:hover,
body.dark-mode .sidebar-link:focus,
body.dark-mode .sidebar-link-chat:focus,
body.dark-mode .sidebar-item a:focus,
body.dark-mode .sidebar-item button:focus {
    background-color: var(--sidebar-dark-hover-bg);
    color: var(--sidebar-dark-text);
}

body.dark-mode .nav-icon,
body.dark-mode #sidebar svg {
    color: var(--sidebar-dark-text);
    fill: currentColor;
}

body.dark-mode .sidebar-toggle-btn,
body.dark-mode .sidebar-collapse-btn {
    color: var(--sidebar-dark-text);
}

body.dark-mode .sidebar-toggle-btn:hover,
body.dark-mode .sidebar-collapse-btn:hover {
    background-color: var(--sidebar-dark-hover-bg);
    color: var(--sidebar-dark-text);
}

body.dark-mode #sidebar .btn-accent,
body.dark-mode #sidebar button.btn-accent {
    background: transparent !important;
    color: var(--sidebar-dark-text) !important;
    font-weight: 300;
}

body.dark-mode #sidebar .btn-accent:hover,
body.dark-mode #sidebar button.btn-accent:hover {
    background-color: var(--sidebar-dark-hover-bg) !important;
    color: var(--sidebar-dark-text) !important;
}

body.dark-mode .sidebar-overlay {
    background: rgba(13, 13, 13, 0.45);
}

body.dark-mode #sidebar .overflow-y-auto {
    scrollbar-width: thin;
    scrollbar-color: #414141 transparent;
    overflow-x: hidden;
}

body.dark-mode #sidebar .overflow-y-auto::-webkit-scrollbar {
    width: 8px;
}

body.dark-mode #sidebar .overflow-y-auto::-webkit-scrollbar-track {
    background: transparent;
}

body.dark-mode #sidebar .overflow-y-auto::-webkit-scrollbar-thumb {
    background-color: rgba(205, 205, 205, 0.28);
    border-radius: 8px;
    border: 2px solid transparent;
    background-clip: padding-box;
}

body.dark-mode #sidebar:hover .overflow-y-auto::-webkit-scrollbar-thumb {
    background-color: rgba(205, 205, 205, 0.45);
}

body.dark-mode .sidebar button {
    background-color: var(--dark-bg-tertiary);
    color: var(--dark-text-primary);
    border-color: var(--dark-border);
}

body.dark-mode .sidebar button:hover {
    background-color: var(--dark-accent-hover);
}

body.dark-mode #sidebar [style*="border: 1px solid #d1d5db"] {
    border-color: var(--dark-border) !important;
    background-color: var(--sidebar-dark-bg) !important;
}

body.dark-mode #dark-mode-switch {
    background-color: var(--dark-bg-tertiary) !important;
}

body.dark-mode #dark-mode-switch-knob {
    background-color: var(--dark-text-muted) !important;
}

/* Dark mode text colors */
body.dark-mode .text-gray-500,
body.dark-mode .text-gray-600,
body.dark-mode .text-gray-700 {
    color: var(--dark-text-primary) !important;
}

body.dark-mode .text-gray-800,
body.dark-mode .text-gray-900 {
    color: var(--sidebar-dark-text) !important;
}

/* ============================================
   RESPONSIVE DESIGN - DESKTOP
   ============================================ */
@media (min-width: 768px) {
    /* Default desktop layout - sidebar visible */
    .sidebar-transform {
        transform: translateX(0);
    }
    
    .sidebar-overlay {
        display: none !important;
    }
    
    body {
        padding-left: var(--sidebar-width);
    }
    
    /* Scope to main top navbar only so inner navs (e.g., chat pane) are unaffected */
    nav[role="navigation"][aria-label="main navigation"] {
        width: calc(100% - var(--sidebar-width));
        margin-left: var(--sidebar-width);
    }

    /* Show collapse button on desktop */
    .sidebar-collapse-btn {
        display: block;
    }
    
    /* Hide mobile elements on desktop */
    #desktop-collapse-btn,
    #drawer-toggle {
        display: none;
    }
    
    #navbar-logo,
    #main-logo {
        display: none;
    }

    /* Main content area adjustments */
    section.flex-1 {
        margin-left: 0;
        transition: margin-left 0.3s ease;
    }

    /* Collapsed sidebar state */
    body.sidebar-collapsed,
    html.sidebar-collapsed body {
        padding-left: 0;
    }

    body.sidebar-collapsed nav[role="navigation"][aria-label="main navigation"],
    html.sidebar-collapsed nav[role="navigation"][aria-label="main navigation"] {
        width: 100%;
        margin-left: 0;
    }

    body.sidebar-collapsed section.flex-1,
    html.sidebar-collapsed section.flex-1 {
        margin-left: 0;
    }

    body.sidebar-collapsed #sidebar,
    html.sidebar-collapsed #sidebar {
        transform: translateX(-100%);
        width: 0;
        overflow: hidden;
        padding: 0;
    }

    /* Show elements when collapsed */
    body.sidebar-collapsed #desktop-collapse-btn,
    html.sidebar-collapsed #desktop-collapse-btn,
    body.sidebar-collapsed #navbar-logo,
    html.sidebar-collapsed #navbar-logo {
        display: block;
    }

    body.sidebar-collapsed #main-logo,
    html.sidebar-collapsed #main-logo {
        display: none;
    }

    /* Hide text in collapsed sidebar */
    body.sidebar-collapsed #sidebar .nav-label,
    html.sidebar-collapsed #sidebar .nav-label,
    body.sidebar-collapsed #sidebar .sidebar-logo,
    html.sidebar-collapsed #sidebar .sidebar-logo {
        display: none;
    }

    body.sidebar-collapsed #sidebar .sidebar-header,
    html.sidebar-collapsed #sidebar .sidebar-header,
    body.sidebar-collapsed #sidebar .sidebar-link,
    html.sidebar-collapsed #sidebar .sidebar-link,
    body.sidebar-collapsed #sidebar .sidebar-item button,
    html.sidebar-collapsed #sidebar .sidebar-item button {
        justify-content: center;
    }

    body.sidebar-collapsed #sidebar .sidebar-link .nav-icon,
    html.sidebar-collapsed #sidebar .sidebar-link .nav-icon {
        margin-right: 0;
    }

    body.sidebar-collapsed #sidebar .sidebar-item button,
    html.sidebar-collapsed #sidebar .sidebar-item button {
        font-size: 0; /* Hide text */
    }

    body.sidebar-collapsed #sidebar .sidebar-item button::before,
    html.sidebar-collapsed #sidebar .sidebar-item button::before {
        content: attr(data-icon);
        font-size: 1rem;
    }

    /* Shared desktop open state */
    body.sidebar-open,
    html.sidebar-open body {
        padding-left: var(--sidebar-width);
    }

    body.sidebar-open nav[role="navigation"][aria-label="main navigation"],
    html.sidebar-open nav[role="navigation"][aria-label="main navigation"] {
        width: calc(100% - var(--sidebar-width));
        margin-left: var(--sidebar-width);
    }

    body.sidebar-open #sidebar,
    html.sidebar-open #sidebar {
        transform: translateX(0);
        width: var(--sidebar-width);
    }
}

/* ============================================
   RESPONSIVE DESIGN - MOBILE
   ============================================ */
@media (max-width: 767px) {
    body {
        padding-left: 0;
    }
    
    /* Scope to main navbar only */
    nav[role="navigation"][aria-label="main navigation"] {
        margin-left: 0;
        width: 100%;
        z-index: 50;
    }
    
    .sidebar-toggle-btn {
        display: block;
    }
    
    .sidebar-collapse-btn, /* make visible on mobile */
    #desktop-collapse-btn,
    #navbar-logo {
        display: none;
    }
    
    /* Override: show the header collapse button inside sidebar on mobile */
    #sidebar .sidebar-collapse-btn {
        display: block;
    }
    
    #main-logo {
        display: block;
    }
}

/* ============================================
   CHAT AND MODAL STYLES
   ============================================ */
.recent-chat-item {
    transition: all 0.2s ease;
}

.recent-chat-item:hover {
    transform: translateX(2px);
}

.chat-search-input:focus {
    box-shadow: 0 0 0 3px rgba(255, 108, 66, 0.1);
}

/* ============================================
   MODAL ANIMATIONS
   ============================================ */
#all-chats-modal {
    backdrop-filter: blur(4px);
}

#all-chats-modal.flex {
    animation: modal-appear 0.2s ease-out;
}

@keyframes modal-appear {
    from {
        opacity: 0;
        transform: scale(0.95);
    }
    to {
        opacity: 1;
        transform: scale(1);
    }
}

/* ============================================
   LOADING ANIMATIONS
   ============================================ */
.chat-item-loading {
    background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
    background-size: 200% 100%;
    animation: loading-shimmer 1.5s infinite;
}

@keyframes loading-shimmer {
    0% {
        background-position: -200% 0;
    }
    100% {
        background-position: 200% 0;
    }
}
