/* General Styles */
html, body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    height: 100%;
    margin: 0;
    padding: 0;
    overflow: hidden;
}

body {
    background-color: #f8f9fa;
    margin: 0;
    padding: 0;
    height: 100vh;
    /* Safari mobile fix - use CSS variable set by JS */
    height: calc(var(--vh, 1vh) * 100);
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

html {
    /* Safari mobile fix */
    height: 100%;
}

/* Navbar */
nav.navbar {
    flex-shrink: 0;
    flex-grow: 0;
}

/* Add light padding to navbar for spacing */
nav.navbar .container-fluid {
    padding-left: 1rem !important;
    padding-right: 1rem !important;
}

/* Override Bootstrap container-fluid padding for main content */
.container-fluid.vh-100 {
    padding-left: 0 !important;
    padding-right: 0 !important;
}

/* Override Bootstrap row margins */
.row.flex-grow-1 {
    margin-left: 0 !important;
    margin-right: 0 !important;
}

/* Main container */
main {
    flex: 1 1 auto;
    overflow: hidden;
    min-height: 0;
    position: relative;
}

.container-fluid.vh-100 {
    height: 100% !important;
    max-height: 100% !important;
    display: flex !important;
    flex-direction: column !important;
    overflow: hidden !important;
    padding: 0 !important;
    margin: 0 !important;
}

.container-fluid .row {
    flex: 1 1 auto;
    min-height: 0;
    max-height: 100%;
    overflow: hidden;
    margin: 0 !important;
    padding: 0 !important;
}

@media (max-width: 768px) {
    .container-fluid .row {
        height: 100%;
        min-height: 100%;
    }
}

/* Chat area */
.col-md-9.col-lg-10.d-flex.flex-column {
    height: 100%;
    min-height: 0;
    max-height: 100%;
    overflow: hidden;
    display: flex !important;
    flex-direction: column !important;
}

/* Sidebar Styles */
#sidebar {
    max-width: 300px;
    background-color: #f8f9fa;
    height: 100%;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

#sessionsList {
    flex: 1;
    overflow-y: auto;
    min-height: 0;
}

.session-item {
    padding: 12px 16px;
    cursor: pointer;
    border-left: 3px solid transparent;
    transition: all 0.2s;
    border-bottom: 1px solid #e9ecef;
}

.session-item:hover {
    background-color: #e9ecef;
}

.session-item.active {
    background-color: #e7f1ff;
    border-left-color: #0d6efd;
}

.session-item-title {
    font-weight: 500;
    font-size: 0.95rem;
    margin-bottom: 4px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    color: #212529;
}

.session-item-date {
    font-size: 0.8rem;
    color: #6c757d;
}

/* Messages Area */
#messagesArea {
    flex: 1;
    background-color: #ffffff;
    overflow-y: auto;
    min-height: 0;
}

.message {
    margin-bottom: 10px;
    animation: fadeIn 0.3s;
    display: flex;
}

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

.message-user {
    justify-content: flex-end;
}

.message-assistant,
.message-system {
    justify-content: flex-start;
}

.message-bubble {
    max-width: 70%;
    padding: 8px 12px;
    border-radius: 12px;
    position: relative;
}

.message-user .message-bubble {
    background-color: #0d6efd;
    color: white;
}

.message-assistant .message-bubble {
    background-color: #f8f9fa;
    border: 1px solid #dee2e6;
    color: #212529;
}

.message-system .message-bubble {
    background-color: #fff3cd;
    border: 1px solid #ffc107;
    color: #856404;
    max-width: 85%;
}

/* Error system messages with ❌ emoji */
.message-system .message-bubble:has(.message-content:first-line:contains("❌")) {
    background-color: #f8d7da;
    border: 1px solid #f5c2c7;
    color: #842029;
}

.message-header {
    font-size: 0.8rem;
    margin-bottom: 6px;
    font-weight: 600;
    opacity: 0.9;
}

.message-content {
    word-wrap: break-word;
    line-height: 1.5;
}

.message-timestamp {
    font-size: 0.75rem;
    margin-top: 6px;
    opacity: 0.7;
}

/* Code Blocks */
.code-block {
    background-color: #282c34;
    color: #abb2bf;
    padding: 12px;
    border-radius: 6px;
    overflow-x: auto;
    margin: 8px 0;
    font-family: 'Courier New', Courier, monospace;
    font-size: 0.9rem;
}

.inline-code {
    background-color: #f1f3f5;
    color: #c7254e;
    padding: 2px 6px;
    border-radius: 3px;
    font-family: 'Courier New', Courier, monospace;
    font-size: 0.9em;
}

.message-user .inline-code {
    background-color: rgba(255, 255, 255, 0.2);
    color: #ffffff;
}

.list-item {
    margin: 2px 0;
    padding-left: 8px;
    line-height: 1.4;
}

.message-content {
    line-height: 1.5;
}

.message-content br {
    display: block;
    content: "";
    margin: 4px 0;
}

.message-content strong {
    font-weight: 600;
    color: #2c3e50;
}

.message-assistant .message-content strong {
    color: #1976d2;
}

/* Result Table */
.result-table {
    margin-top: 12px;
    max-height: 400px;
    overflow: auto;
    border-radius: 6px;
    border: 1px solid #dee2e6;
}

.result-table table {
    font-size: 0.9rem;
    margin-bottom: 0;
}

.result-table th {
    position: sticky;
    top: 0;
    background-color: #343a40;
    color: white;
    z-index: 10;
}

/* Typing Indicator */
.typing-indicator {
    display: flex;
    align-items: center;
    padding: 8px 0;
}

.typing-indicator span {
    height: 8px;
    width: 8px;
    background-color: #6c757d;
    border-radius: 50%;
    display: inline-block;
    margin-right: 4px;
    animation: typing 1.4s infinite;
}

.typing-indicator span:nth-child(2) {
    animation-delay: 0.2s;
}

.typing-indicator span:nth-child(3) {
    animation-delay: 0.4s;
    margin-right: 0;
}

@keyframes typing {
    0%, 60%, 100% {
        transform: translateY(0);
    }
    30% {
        transform: translateY(-10px);
    }
}

/* Message Input */
#messageInputArea {
    flex-shrink: 0;
    flex-grow: 0;
    border-top: 1px solid #dee2e6;
    padding: 1rem;
    background-color: #ffffff;
    margin-top: auto;
    width: 100%;
    position: relative;
    z-index: 10;
}

#messageInputArea textarea {
    border: 1px solid #dee2e6;
    transition: border-color 0.2s;
}

#messageInputArea textarea:focus {
    border-color: #0d6efd;
    box-shadow: 0 0 0 0.2rem rgba(13, 110, 253, 0.25);
}

#sendBtn {
    min-width: 100px;
}

/* Desktop font size reduction (5% smaller) */
@media (min-width: 769px) {
    html, body {
        font-size: 15.2px; /* 16px * 0.95 = 15.2px */
    }
    
    .session-item-title {
        font-size: 0.9025rem; /* 0.95rem * 0.95 */
    }
    
    .session-item-date {
        font-size: 0.76rem; /* 0.8rem * 0.95 */
    }
    
    .message-bubble {
        padding: 7.6px 11.4px; /* 8px * 0.95, 12px * 0.95 */
    }
    
    .message-header {
        font-size: 0.76rem; /* 0.8rem * 0.95 */
        margin-bottom: 5.7px;
    }
    
    .message-timestamp {
        font-size: 0.7125rem; /* 0.75rem * 0.95 */
        margin-top: 5.7px;
    }
    
    .code-block {
        padding: 11.4px; /* 12px * 0.95 */
        font-size: 0.855rem; /* 0.9rem * 0.95 */
        margin: 7.6px 0;
    }
    
    .inline-code {
        padding: 1.9px 5.7px; /* 2px * 0.95, 6px * 0.95 */
        font-size: 0.855em; /* 0.9em * 0.95 */
    }
    
    .result-table table {
        font-size: 0.855rem; /* 0.9rem * 0.95 */
    }
    
    .btn-sm {
        padding: 0.2375rem 0.475rem; /* 0.25rem * 0.95, 0.5rem * 0.95 */
        font-size: 0.83125rem; /* 0.875rem * 0.95 */
    }
    
    #errorBanner .alert-heading {
        font-size: 1.045rem; /* 1.1rem * 0.95 */
    }
    
    #errorBanner p {
        font-size: 0.855rem; /* 0.9rem * 0.95 */
    }
    
    #errorBanner .btn {
        font-size: 0.83125rem; /* 0.875rem * 0.95 */
        padding: 0.35625rem 0.7125rem;
    }
    
    .grouped-message-content {
        font-size: 0.855rem; /* 0.9rem * 0.95 */
    }
    
    .grouped-message-timestamp {
        font-size: 0.665rem; /* 0.7rem * 0.95 */
    }
}

/* Responsive Styles */
@media (max-width: 768px) {
    /* Mobile app-like adjustments */
    body {
        -webkit-overflow-scrolling: touch;
        -webkit-tap-highlight-color: rgba(0,0,0,0);
    }
    
    /* Disable text selection for better app feel */
    .session-item, .btn, .navbar-brand {
        -webkit-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        user-select: none;
    }
    
    /* Compact navbar on mobile */
    nav.navbar .container-fluid {
        padding: 0.5rem !important;
    }
    
    nav.navbar {
        min-height: 48px;
    }
    
    .navbar-brand {
        font-size: 1rem;
        padding: 0.25rem 0.5rem;
    }
    
    .navbar-brand i {
        font-size: 1.1rem;
    }
    
    /* Compact navbar dropdowns */
    .nav-link {
        padding: 0.4rem 0.6rem;
        font-size: 0.9rem;
    }
    
    .dropdown-menu {
        font-size: 0.9rem;
    }
    
    /* Hide sidebar by default on mobile */
    #sidebar {
        position: fixed;
        left: -100%;
        top: 48px;
        z-index: 1000;
        height: calc(100vh - 48px);
        width: 80%;
        max-width: 280px;
        transition: left 0.3s ease-in-out;
        background-color: #ffffff;
        box-shadow: 2px 0 10px rgba(0,0,0,0.3);
        border-right: 1px solid #dee2e6;
    }

    #sidebar.show {
        left: 0;
    }
    
    /* Compact sidebar elements */
    #sidebar .p-3 {
        padding: 0.75rem !important;
    }
    
    #sidebar .border-bottom {
        border-color: #e9ecef !important;
    }
    
    /* New session button on mobile */
    #sidebar #newSessionBtn {
        background-color: #0d6efd;
        border-color: #0d6efd;
        color: #ffffff;
        font-size: 0.875rem;
        padding: 0.5rem 0.75rem;
        font-weight: 500;
        box-shadow: 0 2px 4px rgba(13, 110, 253, 0.2);
    }
    
    #sidebar #newSessionBtn:hover {
        background-color: #0b5ed7;
        border-color: #0a58ca;
        box-shadow: 0 3px 6px rgba(13, 110, 253, 0.3);
    }
    
    /* Search input on mobile */
    #sidebar #sessionSearch {
        font-size: 0.875rem;
        border: 1px solid #ced4da;
        background-color: #f8f9fa;
    }
    
    #sidebar #sessionSearch:focus {
        background-color: #ffffff;
        border-color: #0d6efd;
        box-shadow: 0 0 0 0.15rem rgba(13, 110, 253, 0.15);
    }
    
    .session-item {
        padding: 10px 12px;
        background-color: #ffffff;
        border-bottom: 1px solid #f0f0f0;
    }
    
    .session-item:hover {
        background-color: #f8f9fa;
    }
    
    .session-item.active {
        background-color: #e7f1ff;
        border-left-color: #0d6efd;
        border-left-width: 3px;
        border-left-style: solid;
    }
    
    .session-item-title {
        font-size: 0.875rem;
        color: #212529;
        font-weight: 500;
    }
    
    .session-item.active .session-item-title {
        color: #0d6efd;
        font-weight: 600;
    }
    
    .session-item-date {
        font-size: 0.75rem;
        color: #6c757d;
    }
    
    #sessionsList {
        background-color: #ffffff;
    }

    /* Compact messages area */
    #messagesArea {
        padding: 0.75rem !important;
        flex: 1 1 auto !important;
        min-height: 0 !important;
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch;
    }
    
    .message {
        margin-bottom: 8px;
    }
    
    .message-bubble {
        max-width: 85%;
        padding: 6px 10px;
        font-size: 0.9rem;
    }
    
    .message-header {
        font-size: 0.75rem;
        margin-bottom: 4px;
    }
    
    .message-content {
        font-size: 0.9rem;
        line-height: 1.4;
    }
    
    .message-timestamp {
        font-size: 0.7rem;
        margin-top: 4px;
    }
    
    /* Compact code blocks */
    .code-block {
        padding: 8px;
        font-size: 0.8rem;
        margin: 6px 0;
    }
    
    /* Compact typing indicator on mobile */
    .typing-indicator {
        padding: 4px 0;
    }
    
    .typing-indicator span {
        height: 6px;
        width: 6px;
        margin-right: 3px;
    }
    
    .typing-indicator span:nth-child(3) {
        margin-right: 0;
    }
    
    @keyframes typing {
        0%, 60%, 100% {
            transform: translateY(0);
        }
        30% {
            transform: translateY(-6px);
        }
    }
    
    /* Compact session title bar */
    #sessionTitleBar {
        padding: 0.75rem !important;
        flex-shrink: 0 !important;
        flex-grow: 0 !important;
        flex-basis: auto !important;
        width: 100%;
    }
    
    #sessionTitleBar h5 {
        font-size: 1rem;
    }
    
    #sessionTitleBar .btn-sm {
        padding: 0.2rem 0.4rem;
        font-size: 0.8rem;
    }

    /* Compact message input */
    #messageInputArea {
        padding: 0.75rem !important;
        /* Add safe area for iOS home indicator with fallback */
        padding-bottom: 0.75rem !important;
        padding-bottom: calc(0.75rem + env(safe-area-inset-bottom, 0px)) !important;
        flex-shrink: 0 !important;
        flex-grow: 0 !important;
        width: 100%;
        background-color: #ffffff;
        border-top: 1px solid #dee2e6;
        position: relative;
        bottom: 0;
        left: 0;
        right: 0;
    }
    
    #messageInputArea textarea {
        min-height: 36px;
        /* Prevent iOS zoom on focus */
        font-size: 16px !important;
    }
    
    #sendBtn {
        min-width: 70px;
        font-size: 0.85rem;
        padding: 0.375rem 0.5rem;
    }
    
    .form-text {
        font-size: 0.75rem;
        margin-top: 0.5rem !important;
    }
    
    /* Ensure chat column takes proper height on Safari */
    .col-md-9.col-lg-10 {
        height: 100% !important;
        width: 100%;
        max-width: 100%;
        display: flex !important;
        flex-direction: column !important;
        overflow: hidden;
        position: relative;
    }
    
    /* Ensure messages area takes available space */
    .col-md-9.col-lg-10 #messagesArea {
        flex: 1 1 0;
        overflow-y: auto;
        overflow-x: hidden;
        min-height: 0;
        max-height: 100%;
        margin-bottom: 0;
        -webkit-overflow-scrolling: touch;
        order: 1;
    }
    
    /* Force message input to be visible and at bottom */
    .col-md-9.col-lg-10 #messageInputArea {
        flex: 0 0 auto;
        margin-top: 0;
        order: 999;
    }
    
    /* Force session title bar at top */
    .col-md-9.col-lg-10 #sessionTitleBar {
        flex: 0 0 auto;
        order: -1;
    }
    
    /* Sidebar backdrop overlay */
    body::after {
        content: '';
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.4);
        opacity: 0;
        pointer-events: none;
        transition: opacity 0.3s ease-in-out;
        z-index: 999;
        display: none;
    }
    
    body.sidebar-open::after {
        display: block;
        opacity: 1;
        pointer-events: auto;
    }
    
    /* Prevent body scroll when sidebar is open on mobile */
    body.sidebar-open {
        overflow: hidden;
    }
    
    /* Increase sidebar z-index when open */
    #sidebar.show {
        z-index: 1000;
    }
    
    /* Sidebar header styling on mobile */
    #sidebar .border-bottom:first-child {
        background-color: #f8f9fa;
    }
    
    /* Welcome message on mobile */
    #messagesArea #welcomeMessage {
        padding: 1rem;
    }
    
    #messagesArea #welcomeMessage i {
        font-size: 2.5rem !important;
    }
    
    #messagesArea #welcomeMessage h4 {
        font-size: 1rem;
        margin-bottom: 0.5rem !important;
    }
    
    #messagesArea #welcomeMessage p {
        font-size: 0.85rem;
        margin-bottom: 0.75rem !important;
    }
    
    /* Mobile welcome buttons */
    #mobileWelcomeButtons {
        margin-top: 1rem !important;
    }
    
    #mobileWelcomeButtons .btn {
        font-size: 0.875rem;
        padding: 0.4rem 0.75rem;
        border-radius: 6px;
    }
    
    #mobileWelcomeButtons .btn i {
        font-size: 0.9rem;
    }
}

/* Loading State */
.btn:disabled {
    cursor: not-allowed;
}

/* Scrollbar Styles */
#messagesArea::-webkit-scrollbar,
#sessionsList::-webkit-scrollbar,
.result-table::-webkit-scrollbar {
    width: 8px;
}

#messagesArea::-webkit-scrollbar-track,
#sessionsList::-webkit-scrollbar-track,
.result-table::-webkit-scrollbar-track {
    background: #f1f1f1;
}

#messagesArea::-webkit-scrollbar-thumb,
#sessionsList::-webkit-scrollbar-thumb,
.result-table::-webkit-scrollbar-thumb {
    background: #888;
    border-radius: 4px;
}

#messagesArea::-webkit-scrollbar-thumb:hover,
#sessionsList::-webkit-scrollbar-thumb:hover,
.result-table::-webkit-scrollbar-thumb:hover {
    background: #555;
}

/* Buttons */
.btn-sm {
    padding: 0.25rem 0.5rem;
    font-size: 0.875rem;
}

/* Admin pages override - allow normal scrolling */
main.admin-page {
    overflow: auto !important;
    height: auto !important;
    min-height: calc(100vh - 56px) !important;
}

/* Utilities */
.text-muted {
    color: #6c757d !important;
}

.fw-bold {
    font-weight: 700 !important;
}

/* Error Banner Styles */
#errorBanner {
    position: relative;
    margin-bottom: 0;
    border-left: 4px solid #dc3545;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

#errorBanner .alert-heading {
    font-size: 1.1rem;
    margin-bottom: 0.75rem;
    color: #842029;
}

#errorBanner p {
    margin-bottom: 0.5rem;
    font-size: 0.9rem;
    line-height: 1.5;
    word-break: break-word;
}

#errorBanner hr {
    margin: 0.75rem 0;
    opacity: 0.3;
}

#errorBanner .btn {
    font-size: 0.875rem;
    padding: 0.375rem 0.75rem;
}

#errorBanner .btn i {
    margin-right: 0.25rem;
}

/* Message Group Styles */
.message-group-container {
    max-width: 85% !important;
}

.message-group-header {
    cursor: pointer;
    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
}

.message-group-header:hover {
    opacity: 0.9;
}

.message-group-toggle {
    color: #856404;
    transition: transform 0.2s;
    min-width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.message-group-toggle:hover {
    color: #533f03;
    background-color: rgba(0, 0, 0, 0.05);
    border-radius: 4px;
}

.message-group-toggle i {
    font-size: 1rem;
    transition: transform 0.2s;
}

.message-group-content {
    margin-top: 8px;
}

.message-group-messages {
    padding: 0;
}

.grouped-message-item {
    padding: 8px;
    background-color: rgba(255, 255, 255, 0.5);
    border-radius: 6px;
    border: 1px solid rgba(255, 193, 7, 0.3);
}

.grouped-message-content {
    font-size: 0.9rem;
    line-height: 1.4;
    color: #664d03;
}

.grouped-message-timestamp {
    font-size: 0.7rem;
    margin-top: 4px;
    opacity: 0.7;
    color: #856404;
}

/* Mobile adjustments for message groups */
@media (max-width: 768px) {
    .message-group-container {
        max-width: 90% !important;
    }
    
    .message-group-header {
        font-size: 0.85rem;
    }
    
    .message-group-toggle {
        min-width: 20px;
        height: 20px;
    }
    
    .message-group-toggle i {
        font-size: 0.85rem;
    }
    
    .grouped-message-item {
        padding: 6px;
    }
    
    .grouped-message-content {
        font-size: 0.85rem;
    }
    
    .grouped-message-timestamp {
        font-size: 0.65rem;
    }
}

/* Favorites Styles */
.favorite-item {
    transition: background-color 0.2s;
}

.favorite-item:hover {
    background-color: #f8f9fa;
}

.favorite-description {
    font-weight: 500;
    color: #212529;
}

.favorite-edit-input {
    font-weight: 500;
}

/* Star button styles */
.btn-warning .bi-star-fill,
.btn-outline-warning:hover .bi-star {
    animation: starPulse 0.3s ease-in-out;
}

@keyframes starPulse {
    0%, 100% {
        transform: scale(1);
    }
    50% {
        transform: scale(1.2);
    }
}

.chat-favorite-btn {
    transition: all 0.2s;
}

.chat-favorite-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

/* Favorited state - keep outline style but with filled star */
.chat-favorite-btn.btn-warning {
    background-color: transparent !important;
    border-color: #ffc107 !important;
    color: #ffc107 !important;
}

.chat-favorite-btn.btn-warning:hover {
    background-color: #fff3cd !important;
    border-color: #ffc107 !important;
    color: #ffc107 !important;
}

.chat-favorite-btn .bi-star-fill {
    color: #ffc107;
}

.chat-favorite-btn.btn-warning .bi-star-fill {
    color: #ffc107;
    filter: drop-shadow(0 0 2px rgba(255, 193, 7, 0.5));
}

/* Favorites modal */
#favoritesModal .modal-body {
    max-height: 70vh;
    overflow-y: auto;
}

#favoritesModal .list-group-item {
    border-left: 3px solid transparent;
    transition: border-color 0.2s;
}

#favoritesModal .list-group-item:hover {
    border-left-color: #ffc107;
}

/* Toast notification positioning */
.toast {
    z-index: 9999;
}

/* Favorite button in Results page */
#favoriteBtn {
    transition: all 0.2s;
}

#favoriteBtn:hover {
    transform: scale(1.05);
}

#favoriteBtn .bi-star,
#favoriteBtn .bi-star-fill {
    font-size: 1.1rem;
}

/* Favorited state - keep outline style but with filled star */
#favoriteBtn.btn-warning {
    background-color: transparent !important;
    border-color: #ffc107 !important;
    color: #ffc107 !important;
}

#favoriteBtn.btn-warning:hover {
    background-color: #fff3cd !important;
    border-color: #ffc107 !important;
    color: #ffc107 !important;
}

#favoriteBtn.btn-warning .bi-star-fill {
    color: #ffc107;
    filter: drop-shadow(0 0 2px rgba(255, 193, 7, 0.5));
}

/* Loading state for favorite buttons while checking status */
.chat-favorite-btn[data-checking="true"] {
    opacity: 0.6;
    pointer-events: none;
}

.chat-favorite-btn[data-checking="true"] i {
    animation: checkingPulse 1s ease-in-out infinite;
}

@keyframes checkingPulse {
    0%, 100% {
        opacity: 0.4;
    }
    50% {
        opacity: 1;
    }
}

/* Footer Styles */
footer.footer {
    flex-shrink: 0;
    flex-grow: 0;
    background-color: #f8f9fa;
    border-top: 1px solid #dee2e6;
    margin-top: auto;
}

footer.footer a {
    color: #6c757d;
    text-decoration: none;
    transition: color 0.2s;
}

footer.footer a:hover {
    color: #0d6efd;
    text-decoration: underline;
}
