/**
 * Common CSS - Base structural styles shared across all pages
 *
 * This file defines foundational classes for consistent UI components:
 * - Section containers (card-like boxes)
 * - Table wrappers
 * - Stats badges
 * - Flex layouts
 * - Filters sections
 * - Table toolbars
 *
 * Themes can override these by:
 * 1. Redefining the CSS variables (--common-*)
 * 2. Overriding the classes directly (theme CSS loads after this)
 *
 * Load order: Bootstrap -> common.css -> theme CSS
 */

/* ==========================================================
   CSS Variables - Base Theme (Dark)
   Themes override these variables to customize appearance
   ========================================================== */
:root {
    /* Backgrounds */
    --bg-primary: #36393f;
    --bg-secondary: #2f3136;
    --bg-tertiary: #202225;

    /* Text */
    --text-primary: #ffffff;
    --text-secondary: #b9bbbe;
    --text-muted: #72767d;

    /* Borders */
    --border-color: #40444b;
    --border-radius: 8px;

    /* Accent color */
    --accent: #7289da;
    --accent-hover: #5b6eae;

    /* Shadows */
    --shadow-soft: 0 2px 8px rgba(0, 0, 0, 0.2);
    --shadow-hover: 0 4px 12px rgba(0, 0, 0, 0.3);

    /* Status colors */
    --success: #43b581;
    --danger: #ed4245;
    --warning: #faa61a;
    --info: #00b0f4;
}

/* ==========================================================
   Section Container (Card-like boxes)
   ========================================================== */
.section-container {
    background: var(--bg-secondary);
    border-radius: var(--border-radius);
    padding: 25px;
    margin-bottom: 25px;
    box-shadow: var(--shadow-soft);
    transition: all 0.2s ease;
    border: 1px solid var(--border-color);
}

.section-container:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-hover);
}

/* Section Title (header inside section-container) */
.section-title {
    font-weight: 700;
    font-size: 1.1rem;
    color: var(--text-primary);
    border-bottom: 2px solid var(--border-color);
    padding-bottom: 14px;
    margin-bottom: 22px;
    display: flex;
    align-items: center;
    gap: 10px;
}

.section-title i {
    color: var(--accent);
}

/* ==========================================================
   Table Wrapper (contains tables with proper styling)
   ========================================================== */
.table-wrapper {
    border-radius: var(--border-radius);
    overflow-x: auto;
    border: 1px solid var(--border-color);
}

/* Sticky actions column - always visible on the right */
.th-actions,
.actions-cell {
    position: sticky;
    right: 0;
    z-index: 1;
    background-color: var(--bg-secondary);
    box-shadow: -2px 0 4px rgba(0, 0, 0, 0.1);
}

.table-hover tbody tr:hover .actions-cell {
    background-color: var(--bg-tertiary, var(--bg-secondary));
}

/* Table base styles inside wrapper */
.table-wrapper .table {
    margin: 0;
    background-color: var(--bg-secondary);
    color: var(--text-primary);
}

.table-wrapper .table th,
.table-wrapper .table td {
    border-color: var(--border-color);
    padding: 10px 12px;
    vertical-align: middle;
}

.table-wrapper .table thead th {
    background-color: var(--bg-tertiary);
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-secondary);
    border-bottom: 2px solid var(--border-color);
}

.table-wrapper .table tbody td {
    color: var(--text-primary);
    border-bottom: 1px solid var(--border-color);
}

.table-wrapper .table tbody tr:last-child td {
    border-bottom: none;
}

.table-wrapper .table tbody tr:hover {
    background-color: var(--bg-tertiary);
}

/* Compact icon-only table cells */
.role-cell,
.status-cell {
    text-align: center;
    width: 60px;
    white-space: nowrap;
}

.role-cell i,
.status-cell i {
    font-size: 1rem;
    cursor: help;
}

/* ==========================================================
   Stats Row (Summary statistics badges)
   ========================================================== */
.stats-row {
    display: flex;
    gap: 15px;
    margin-bottom: 25px;
    flex-wrap: wrap;
}

.stat-badge {
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    padding: 10px 18px;
    border-radius: 25px;
    font-size: 0.9rem;
    display: inline-flex;
    align-items: center;
    gap: 10px;
    box-shadow: var(--shadow-soft);
    color: var(--text-secondary);
}

.stat-badge i {
    color: var(--accent);
    font-size: 1rem;
}

.stat-badge strong {
    color: var(--accent);
}

/* Stat badge variants */
.stat-badge.stat-active,
.stat-badge.stat-public {
    background: rgba(67, 181, 129, 0.15);
    border-color: rgba(67, 181, 129, 0.3);
}

.stat-badge.stat-active strong,
.stat-badge.stat-public strong {
    color: var(--success);
}

.stat-badge.stat-expired,
.stat-badge.stat-private {
    background: rgba(237, 66, 69, 0.15);
    border-color: rgba(237, 66, 69, 0.3);
}

.stat-badge.stat-expired strong {
    color: var(--danger);
}

.stat-badge.stat-private strong {
    color: var(--text-muted);
}

.stat-badge.stat-password {
    background: rgba(108, 117, 125, 0.15);
    border-color: rgba(108, 117, 125, 0.3);
}

.stat-badge.stat-password strong {
    color: var(--text-muted);
}

/* ==========================================================
   Filters Section (special section-container for filters)
   ========================================================== */
.filters-section {
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius);
    padding: 20px;
    margin-bottom: 20px;
}

.filters-section .section-title {
    margin-bottom: 16px;
    padding-bottom: 12px;
}

.filters-section .section-title .btn {
    margin-left: auto;
}

/* ==========================================================
   Table Toolbar (Select all + Actions above table)
   ========================================================== */
.table-toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 16px;
    margin-bottom: 16px;
    padding: 12px 16px;
    background-color: var(--bg-tertiary);
    border-radius: var(--border-radius);
}

.toolbar-left {
    display: flex;
    align-items: center;
    gap: 12px;
}

.toolbar-left .form-check-label {
    color: var(--text-secondary);
    font-size: 0.9rem;
}

.toolbar-right {
    display: flex;
    align-items: center;
    gap: 12px;
}

/* ==========================================================
   Flex Layout (Responsive columns)
   ========================================================== */
.flex-row {
    display: flex;
    flex-wrap: wrap;
    gap: 20px;
}

.flex-column {
    flex: 1;
    min-width: 200px;
}

/* ==========================================================
   Table Actions (Buttons in table rows)
   ========================================================== */
.table-actions {
    white-space: nowrap;
}

.table-actions .btn {
    padding: 7px 11px;
    margin-left: 6px;
    border-radius: 6px;
    transition: all 0.2s;
}

.table-actions .btn:first-child {
    margin-left: 0;
}

.table-actions .btn:hover {
    transform: translateY(-2px);
}

/* ==========================================================
   Sortable Table Headers
   ========================================================== */
.table thead th.sortable {
    cursor: pointer;
    user-select: none;
    transition: background-color 0.2s ease;
}

.table thead th.sortable:hover {
    background-color: var(--bg-primary);
}

.table thead th.sortable::after {
    content: '';
    margin-left: 6px;
    opacity: 0.3;
}

.table thead th.sort-asc::after {
    content: '\25B2';
    opacity: 1;
    color: var(--accent);
}

.table thead th.sort-desc::after {
    content: '\25BC';
    opacity: 1;
    color: var(--accent);
}

/* ==========================================================
   Status Indicators
   ========================================================== */
.status-active {
    color: var(--success);
}

.status-expired {
    color: var(--danger);
}

.status-na {
    color: var(--text-muted, #6c757d);
    font-weight: 500;
}

.status-public,
.status-private {
    white-space: nowrap;
    font-size: 0.875rem;
}

.status-public {
    color: var(--success);
}

.status-private {
    color: var(--text-muted);
}

/* ==========================================================
   Responsive Adjustments
   ========================================================== */
@media (max-width: 992px) {
    .flex-column {
        min-width: calc(50% - 10px);
        flex: 0 0 calc(50% - 10px);
    }
}

@media (max-width: 768px) {
    .section-container {
        padding: 15px;
    }

    .section-title {
        font-size: 1rem;
        padding-bottom: 10px;
        margin-bottom: 15px;
    }

    .stats-row {
        gap: 10px;
    }

    .stat-badge {
        flex: 1;
        justify-content: center;
        padding: 8px 12px;
        font-size: 0.85rem;
    }

    .flex-column {
        min-width: 100%;
        flex: 0 0 100%;
    }

    .table-toolbar {
        flex-direction: column;
        align-items: stretch;
        gap: 12px;
    }

    .toolbar-left,
    .toolbar-right {
        justify-content: center;
    }

    .toolbar-right {
        flex-wrap: wrap;
    }

    .table-wrapper {
        border-radius: 6px;
    }

    /* Hide chat navigation arrows on mobile */
    .nav-menu {
        display: none !important;
    }
}

@media (max-width: 480px) {
    .section-container {
        padding: 12px;
        margin-bottom: 15px;
    }

    .filters-section .section-title {
        flex-direction: column;
        align-items: flex-start;
        gap: 10px;
    }

    .filters-section .section-title .btn {
        width: 100%;
        margin-left: 0;
    }

    .toolbar-right .btn {
        flex: 1;
    }
}

/* ==========================================================
   Generic Modal Styling
   ========================================================== */
.modal-content {
    background-color: var(--bg-secondary);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
}

.modal-header {
    background-color: var(--bg-tertiary);
    border-bottom: 1px solid var(--border-color);
}

.modal-title {
    color: var(--text-primary);
}

.modal-body {
    color: var(--text-secondary);
}

.modal-footer {
    background-color: var(--bg-tertiary);
    border-top: 1px solid var(--border-color);
}

.modal-header .btn-close {
    filter: var(--modal-close-filter, invert(1) grayscale(100%) brightness(200%));
}

#folderModal .btn-close,
#moveChatModal .btn-close {
    filter: var(--modal-close-filter, invert(1) grayscale(100%) brightness(200%));
}

/* ==========================================================
   Navbar
   ========================================================== */
.navbar {
    position: relative;
    z-index: 1030;
    padding: 0.4rem 1rem;
}

.navbar .navbar-brand {
    font-size: 1.25rem;
    letter-spacing: 0.3px;
    margin-right: 1rem;
}

/* User avatar in navbar (image or initials) */
.navbar-user-avatar {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    flex-shrink: 0;
    object-fit: cover;
    border: 2px solid var(--border-color, rgba(255,255,255,0.3));
}

/* Initials fallback */
.navbar-user-initials {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: var(--accent, #7289da);
    color: #fff !important;
    font-size: 0.85rem;
    font-weight: 700;
}

/* User name inherits color from .nav-link (themed) */
.navbar-user-name {
    font-weight: 500;
    font-size: 0.9rem;
}

/* Theme submenu needs to be above all page content */
.theme-submenu {
    z-index: 1060 !important;
}

/* Ensure dropdowns in navbar have high z-index */
.navbar .dropdown-menu {
    z-index: 1050;
}

/* ==========================================================
   Locked Conversations
   ========================================================== */
.conversation-locked .chat-name {
    opacity: 0.7;
    color: var(--text-muted, #72767d);
}

.conversation-locked .chat-menu > .fa-ellipsis-h {
    opacity: 0.7;
}

.chat-name i.fa-comment-slash {
    margin-right: 6px;
    color: var(--warning, #faa61a);
    font-size: 0.85em;
}

/* Locked conversation banner - centered above input */
#locked-conversation-banner {
    position: absolute;
    bottom: 120px;
    left: 50%;
    transform: translateX(-50%);
    max-width: 700px;
    width: calc(100% - 40px);
    z-index: 100;
    margin: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    white-space: nowrap;
    /* Solid background for visibility over messages */
    background: linear-gradient(135deg, #e65100 0%, #ff6d00 100%);
    border: 2px solid #bf360c;
    box-shadow: 0 4px 20px rgba(230, 81, 0, 0.4);
}

#locked-conversation-banner .api-keys-banner-content {
    justify-content: center;
    color: #fff;
    font-weight: 500;
}

#locked-conversation-banner .api-keys-banner-content i {
    color: #fff;
}

/* ==========================================================
   GranSabio Pipeline Panel (Chat Messages)
   ========================================================== */

.gransabio-panel {
    background: var(--bg-secondary, #2b2d31);
    border: 1px solid var(--border-color, #3f4147);
    border-radius: 8px;
    margin-bottom: 12px;
    overflow: hidden;
    font-size: 0.85rem;
}

.gransabio-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    cursor: pointer;
    user-select: none;
    background: var(--bg-tertiary, #232428);
}

.gransabio-header i:first-child {
    color: var(--accent, #5865f2);
}

.gransabio-title {
    flex: 1;
    font-weight: 600;
    color: var(--text-primary, #dbdee1);
}

.gransabio-title.gransabio-approved {
    color: var(--success, #3ba55c);
}

.gransabio-title.gransabio-failed {
    color: var(--danger, #ed4245);
}

.gransabio-toggle {
    color: var(--text-muted, #949ba4);
    transition: transform 0.2s;
}

.gransabio-panel.collapsed .gransabio-body {
    display: none;
}

.gransabio-panel.collapsed .gransabio-toggle {
    transform: rotate(-90deg);
}

.gransabio-body {
    padding: 8px 12px;
    max-height: 300px;
    overflow-y: auto;
}

.gransabio-line {
    padding: 3px 0;
    color: var(--text-secondary, #b5bac1);
    display: flex;
    align-items: center;
    gap: 6px;
    flex-wrap: wrap;
}

.gransabio-line i {
    width: 16px;
    text-align: center;
    color: var(--text-muted, #949ba4);
}

.gransabio-phase-generating i { color: var(--accent, #5865f2); }
.gransabio-phase-qa i { color: var(--info, #5bc0de); }
.gransabio-phase-scoring i { color: var(--warning, #faa61a); }
.gransabio-phase-editing i { color: #e67e22; }
.gransabio-phase-retry i { color: var(--warning, #faa61a); }
.gransabio-phase-gran_sabio i { color: #9b59b6; }

.gransabio-text {
    flex: 1;
    min-width: 0;
}

.gransabio-iter {
    font-size: 0.8rem;
    color: var(--text-muted, #949ba4);
    background: var(--bg-primary, #1e1f22);
    padding: 1px 6px;
    border-radius: 4px;
}

.gransabio-layer {
    font-size: 0.8rem;
    font-style: italic;
    color: var(--text-muted, #949ba4);
}

.gransabio-score-pass {
    color: var(--success, #3ba55c);
    font-weight: 600;
}

.gransabio-score-fail {
    color: var(--danger, #ed4245);
    font-weight: 600;
}

