:root {
    /* Improved Color Palette - Better Visibility */
    --primary: #6366F1;
    /* Indigo 500 - More vibrant */
    --primary-hover: #4F46E5;
    /* Indigo 600 */
    --primary-light: #A5B4FC;
    /* Indigo 300 */
    --secondary: #22D3EE;
    /* Cyan 400 */
    --accent: #F59E0B;
    /* Amber 500 - For highlights */

    --bg-body: #0C0F1A;
    /* Darker blue-black */
    --bg-card: #171B2D;
    /* Dark card background */
    --bg-elevated: #1E2340;
    /* Elevated surfaces */
    --bg-input: #0F1222;
    /* Input backgrounds */

    --text-main: #F8FAFC;
    /* Slate 50 - Brighter white */
    --text-secondary: #CBD5E1;
    /* Slate 300 */
    --text-muted: #94A3B8;
    /* Slate 400 */
    --border: #2D3555;
    /* Softer border */
    --border-light: #3D4766;
    /* Lighter border for hover */

    --success: #10B981;
    /* Emerald 500 */
    --warning: #F59E0B;
    /* Amber 500 */
    --error: #EF4444;
    /* Red 500 */

    --radius: 12px;
    --radius-lg: 16px;
    --shadow-sm: 0 2px 4px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.5);

    --font-sans: 'Inter', system-ui, -apple-system, sans-serif;

    /* Gradients */
    --gradient-brand: linear-gradient(135deg, #6366F1 0%, #22D3EE 100%);
    --gradient-header: linear-gradient(180deg, #171B2D 0%, #0C0F1A 100%);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: var(--font-sans);
    background-color: var(--bg-body);
    color: var(--text-main);
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

/* ==================== HEADER ==================== */
.app-header {
    background: var(--gradient-header);
    border-bottom: 1px solid var(--border);
    padding: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    position: sticky;
    top: 0;
    z-index: 100;
    box-shadow: var(--shadow-md);
}

.header-top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1rem 1.5rem;
    max-width: 1000px;
    width: 100%;
    box-sizing: border-box;
}

.brand {
    display: flex;
    align-items: center;
    gap: 0.875rem;
}

.logo {
    font-size: 1.5rem;
    background: var(--gradient-brand);
    padding: 0.5rem;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 12px rgba(99, 102, 241, 0.3);
}

.brand-text {
    display: flex;
    flex-direction: column;
}

.app-header h1 {
    font-size: 1.35rem;
    font-weight: 700;
    color: var(--text-main);
    letter-spacing: -0.025em;
    line-height: 1.2;
}

.brand-tagline {
    font-size: 0.75rem;
    color: var(--text-muted);
    font-weight: 400;
}

/* ==================== TABS ==================== */
.tabs-wrapper {
    width: 100%;
    border-top: 1px solid var(--border);
    background: rgba(0, 0, 0, 0.2);
    display: flex;
    justify-content: center;
}

.mode-switch {
    display: flex;
    gap: 0.25rem;
    padding: 0.5rem 1.5rem 0;
    max-width: 1000px;
    width: 100%;
    box-sizing: border-box;
}

.tab-btn {
    background: transparent;
    border: none;
    padding: 0.875rem 1.5rem;
    border-radius: 12px 12px 0 0;
    font-size: 0.9rem;
    font-weight: 500;
    color: var(--text-muted);
    cursor: pointer;
    transition: all 0.2s ease;
    position: relative;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.tab-btn .tab-icon {
    font-size: 1.1rem;
}

.tab-btn:hover {
    color: var(--text-main);
    background: rgba(255, 255, 255, 0.05);
}

.tab-btn.active {
    background: var(--bg-card);
    color: var(--primary-light);
    font-weight: 600;
    border: 1px solid var(--border);
    border-bottom: none;
    margin-bottom: -1px;
}

.tab-btn.active::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 3px;
    background: var(--gradient-brand);
    border-radius: 3px 3px 0 0;
}

/* ==================== MAIN CONTAINER ==================== */
.container {
    max-width: 1000px;
    margin: 2rem auto;
    padding: 0 1.5rem;
    flex: 1;
    width: 100%;
}

.tab-panel {
    display: none;
    animation: fadeIn 0.3s ease;
}

.tab-panel.active {
    display: block;
}

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

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* ==================== CARDS ==================== */
.card {
    background: var(--bg-card);
    border-radius: var(--radius-lg);
    padding: 2rem;
    box-shadow: var(--shadow-lg);
    border: 1px solid var(--border);
    margin-bottom: 2rem;
}

.card h2 {
    font-size: 1.5rem;
    margin-bottom: 0.5rem;
    color: var(--text-main);
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.card h2 .section-icon {
    font-size: 1.75rem;
}

.card p.muted {
    color: var(--text-secondary);
    margin-bottom: 1.75rem;
    font-size: 0.95rem;
}

/* ==================== FORMS ==================== */
.form-grid {
    display: grid;
    gap: 1.5rem;
}

.form-grid-2 {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1.5rem;
}

.grid-3 {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 1.5rem;
}

.field {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

label {
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--text-main);
}

input[type="text"],
input[type="number"],
input[type="file"] {
    padding: 0.75rem 1rem;
    border-radius: 10px;
    border: 2px solid var(--border);
    font-size: 0.95rem;
    transition: border-color 0.2s, box-shadow 0.2s, background 0.2s;
    background: var(--bg-input);
    color: var(--text-main);
    width: 100%;
}

input[type="text"]::placeholder,
input[type="number"]::placeholder {
    color: var(--text-muted);
}

input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 4px rgba(99, 102, 241, 0.2);
    background: var(--bg-input);
}

input[type="file"] {
    padding: 0.625rem;
    background: var(--bg-input);
    color: var(--text-muted);
}

input[type="file"]::file-selector-button {
    background: var(--primary);
    border: none;
    color: white;
    padding: 0.5rem 1rem;
    border-radius: 8px;
    cursor: pointer;
    margin-right: 1rem;
    font-weight: 500;
    transition: background 0.2s;
}

input[type="file"]::file-selector-button:hover {
    background: var(--primary-hover);
}

.help {
    font-size: 0.8rem;
    color: var(--text-muted);
}

/* ==================== BUTTONS ==================== */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.75rem 1.5rem;
    border-radius: 10px;
    border: 2px solid var(--border);
    background: var(--bg-elevated);
    color: var(--text-main);
    font-weight: 600;
    font-size: 0.95rem;
    cursor: pointer;
    transition: all 0.2s;
    gap: 0.5rem;
}

.btn:hover {
    background: var(--border);
    border-color: var(--border-light);
    transform: translateY(-1px);
}

.btn.primary {
    background: var(--primary);
    border-color: var(--primary);
    color: white;
    box-shadow: 0 4px 12px rgba(99, 102, 241, 0.3);
}

.btn.primary:hover {
    background: var(--primary-hover);
    border-color: var(--primary-hover);
    box-shadow: 0 6px 16px rgba(99, 102, 241, 0.4);
}

.actions {
    display: flex;
    justify-content: flex-end;
    margin-top: 1rem;
    gap: 0.75rem;
}

.actions.span-2 {
    grid-column: span 2;
}

/* ==================== RESULTS ==================== */
.result-block {
    margin-top: 2rem;
    padding-top: 2rem;
    border-top: 1px solid var(--border);
}

.result-block h3 {
    font-size: 0.85rem;
    margin-bottom: 1rem;
    color: var(--primary-light);
    text-transform: uppercase;
    letter-spacing: 0.1em;
    font-weight: 700;
}

pre.code,
.answer {
    background: var(--bg-input);
    padding: 1.25rem;
    border-radius: 10px;
    border: 1px solid var(--border);
    font-family: 'JetBrains Mono', 'Menlo', 'Monaco', monospace;
    font-size: 0.9rem;
    line-height: 1.7;
    white-space: pre-wrap;
    color: var(--text-secondary);
    min-height: 80px;
    overflow-x: auto;
}

.answer {
    border-left: 4px solid var(--primary);
    background: linear-gradient(90deg, rgba(99, 102, 241, 0.1) 0%, var(--bg-input) 100%);
}

/* ==================== TOGGLE SWITCH ==================== */
.toggle-row {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    height: 100%;
}

.switch {
    position: relative;
    display: inline-block;
    width: 52px;
    height: 28px;
}

.switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: var(--border);
    transition: .3s;
    border-radius: 34px;
}

.slider:before {
    position: absolute;
    content: "";
    height: 22px;
    width: 22px;
    left: 3px;
    bottom: 3px;
    background-color: white;
    transition: .3s;
    border-radius: 50%;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

input:checked+.slider {
    background: var(--gradient-brand);
}

input:checked+.slider:before {
    transform: translateX(24px);
}

/* ==================== TABLES ==================== */
.table-wrap {
    overflow-x: auto;
    border: 1px solid var(--border);
    border-radius: 10px;
}

table {
    width: 100%;
    border-collapse: collapse;
    text-align: left;
}

th,
td {
    padding: 0.875rem 1rem;
    border-bottom: 1px solid var(--border);
}

th {
    background: var(--bg-elevated);
    font-weight: 700;
    font-size: 0.85rem;
    color: var(--primary-light);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

td {
    font-size: 0.95rem;
    color: var(--text-main);
}

tr:last-child td {
    border-bottom: none;
}

tr:hover td {
    background: rgba(99, 102, 241, 0.05);
}

td.center {
    text-align: center;
}

/* ==================== FOOTER ==================== */
.app-footer {
    text-align: center;
    padding: 2rem;
    color: var(--text-muted);
    font-size: 0.875rem;
    background: var(--bg-card);
    border-top: 1px solid var(--border);
}

.app-footer strong {
    color: var(--primary-light);
}

.tiny {
    font-size: 0.8rem;
    opacity: 0.7;
    margin-top: 0.5rem;
}

/* ==================== DIVIDER ==================== */
.divider {
    height: 1px;
    background: linear-gradient(90deg, transparent, var(--border), transparent);
    margin: 2rem 0;
}

/* ==================== INFO SECTION ==================== */
.info-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 1.5rem;
    margin-top: 1.5rem;
}

.info-item {
    padding: 1.5rem;
    background: var(--bg-elevated);
    border-radius: 12px;
    border: 1px solid var(--border);
    transition: transform 0.2s, border-color 0.2s, box-shadow 0.2s;
}

.info-item:hover {
    transform: translateY(-4px);
    border-color: var(--primary);
    box-shadow: 0 8px 24px rgba(99, 102, 241, 0.15);
}

.info-item h4 {
    color: var(--primary-light);
    margin-bottom: 0.75rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 1.1rem;
}

.info-item p {
    color: var(--text-secondary);
    font-size: 0.9rem;
    line-height: 1.6;
}

.info-item strong {
    color: var(--text-main);
}

/* ==================== STATUS INDICATORS ==================== */
.status-badge {
    display: inline-flex;
    align-items: center;
    gap: 0.375rem;
    padding: 0.25rem 0.75rem;
    border-radius: 99px;
    font-size: 0.8rem;
    font-weight: 600;
}

.status-badge.success {
    background: rgba(16, 185, 129, 0.15);
    color: var(--success);
}

.status-badge.warning {
    background: rgba(245, 158, 11, 0.15);
    color: var(--warning);
}

.status-badge.error {
    background: rgba(239, 68, 68, 0.15);
    color: var(--error);
}

/* ==================== META INFO ==================== */
#chat-meta {
    padding: 0.75rem 1rem;
    background: var(--bg-elevated);
    border-radius: 8px;
    margin-top: 1rem;
    font-size: 0.875rem;
    border: 1px solid var(--border);
}

.small {
    font-size: 0.875rem;
}

/* ==================== TOKEN USAGE ==================== */
.token-usage {
    display: flex;
    align-items: center;
    gap: 1rem;
    margin-top: 1rem;
    padding: 0.75rem 1rem;
    background: var(--bg-elevated);
    border-radius: 10px;
    border: 1px solid var(--border);
    font-size: 0.85rem;
}

.token-usage.hidden {
    display: none;
}

.token-usage .token-label {
    color: var(--text-muted);
    font-weight: 500;
    display: flex;
    align-items: center;
    gap: 0.375rem;
}

.token-usage .token-label::before {
    content: '';
    display: inline-block;
    width: 8px;
    height: 8px;
    background: var(--primary);
    border-radius: 50%;
}

.token-stat {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.375rem 0.75rem;
    background: var(--bg-input);
    border-radius: 6px;
    border: 1px solid var(--border);
}

.token-stat .stat-label {
    color: var(--text-muted);
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.token-stat .stat-value {
    color: var(--primary-light);
    font-weight: 700;
    font-family: 'JetBrains Mono', 'Menlo', monospace;
}

.token-stat.input .stat-value {
    color: #22D3EE;
}

.token-stat.output .stat-value {
    color: #10B981;
}

.token-stat.total .stat-value {
    color: var(--primary-light);
}

/* Global Token Stats in Header */
.header-stats {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.global-token-btn {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 1rem;
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text-secondary);
    font-size: 0.8rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
}

.global-token-btn:hover {
    background: var(--border);
    color: var(--text-main);
}

.global-token-btn .token-icon {
    font-size: 1rem;
}

.global-token-btn .token-count {
    color: var(--primary-light);
    font-weight: 700;
    font-family: 'JetBrains Mono', 'Menlo', monospace;
}

/* Token Stats Modal/Panel */
.token-panel {
    position: fixed;
    top: 0;
    right: -400px;
    width: 380px;
    height: 100vh;
    background: var(--bg-card);
    border-left: 1px solid var(--border);
    box-shadow: -8px 0 24px rgba(0, 0, 0, 0.4);
    z-index: 200;
    transition: right 0.3s ease;
    overflow-y: auto;
}

.token-panel.show {
    right: 0;
}

.token-panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1.25rem 1.5rem;
    border-bottom: 1px solid var(--border);
    background: var(--bg-elevated);
}

.token-panel-header h3 {
    font-size: 1.1rem;
    color: var(--text-main);
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.token-panel-close {
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 1.5rem;
    cursor: pointer;
    padding: 0.25rem;
    line-height: 1;
    transition: color 0.2s;
}

.token-panel-close:hover {
    color: var(--text-main);
}

.token-panel-body {
    padding: 1.5rem;
}

.token-summary-card {
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 1.25rem;
    margin-bottom: 1.5rem;
}

.token-summary-card h4 {
    font-size: 0.8rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.08em;
    margin-bottom: 1rem;
}

.token-summary-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 1rem;
}

.token-summary-item {
    text-align: center;
}

.token-summary-item .value {
    font-size: 1.5rem;
    font-weight: 700;
    font-family: 'JetBrains Mono', 'Menlo', monospace;
    color: var(--primary-light);
    line-height: 1.2;
}

.token-summary-item .label {
    font-size: 0.75rem;
    color: var(--text-muted);
    margin-top: 0.25rem;
}

.token-summary-item.input .value {
    color: #22D3EE;
}

.token-summary-item.output .value {
    color: #10B981;
}

.token-by-type {
    margin-top: 1rem;
}

.token-type-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.875rem 1rem;
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: 10px;
    margin-bottom: 0.75rem;
}

.token-type-info {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.token-type-icon {
    font-size: 1.25rem;
}

.token-type-name {
    font-weight: 600;
    color: var(--text-main);
}

.token-type-calls {
    font-size: 0.8rem;
    color: var(--text-muted);
}

.token-type-count {
    font-family: 'JetBrains Mono', 'Menlo', monospace;
    font-weight: 700;
    color: var(--primary-light);
    font-size: 1rem;
}

.token-panel-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 199;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s;
}

.token-panel-overlay.show {
    opacity: 1;
    visibility: visible;
}

/* ==================== RESPONSIVE ==================== */
@media (max-width: 768px) {
    .header-top {
        padding: 0.875rem 1rem;
    }

    .app-header h1 {
        font-size: 1.1rem;
    }

    .brand-tagline {
        display: none;
    }

    .logo {
        font-size: 1.25rem;
        padding: 0.4rem;
    }

    .global-token-btn {
        padding: 0.4rem 0.75rem;
        font-size: 0.75rem;
    }

    .global-token-btn span:not(.token-count):not(.token-icon) {
        display: none;
    }

    .mode-switch {
        padding: 0.5rem 1rem 0;
        overflow-x: auto;
    }

    .tab-btn {
        padding: 0.625rem 0.875rem;
        font-size: 0.8rem;
        white-space: nowrap;
    }

    .form-grid-2 {
        grid-template-columns: 1fr;
    }

    .grid-3 {
        grid-template-columns: 1fr;
    }

    .actions.span-2 {
        grid-column: span 1;
    }

    .card {
        padding: 1.25rem;
    }

    .container {
        padding: 0 1rem;
        margin: 1.5rem auto;
    }

    .token-usage {
        flex-wrap: wrap;
        gap: 0.5rem;
    }
}
