:root {
    --primary: #2563eb;
    --primary-dark: #1e40af;
    --primary-soft: #dbeafe;
    --primary-softer: #eff6ff;

    --bg-from: #f8fafc;
    --bg-to: #e2e8f0;
    --surface: #ffffff;
    --surface-2: #f9fafb;
    --border: #e5e7eb;
    --text: #1f2937;
    --text-muted: #4b5563;
    --text-faint: #9ca3af;

    --success: #10b981;
    --success-dark: #059669;
    --success-bg: #f0fdf4;
    --success-border: #bbf7d0;
    --error: #ef4444;
    --error-dark: #dc2626;
    --error-bg: #fef2f2;
    --error-border: #fecaca;
    --warning: #f59e0b;

    --diff-add-bg: #dcfce7;
    --diff-add-text: #166534;
    --diff-del-bg: #fee2e2;
    --diff-del-text: #991b1b;

    --shadow-sm: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
    --shadow-lg: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
}

[data-theme="dark"] {
    --primary: #3b82f6;
    --primary-dark: #2563eb;
    --primary-soft: #1e3a5f;
    --primary-softer: #172554;

    --bg-from: #0b1120;
    --bg-to: #111827;
    --surface: #1f2937;
    --surface-2: #111827;
    --border: #374151;
    --text: #f3f4f6;
    --text-muted: #d1d5db;
    --text-faint: #6b7280;

    --success-bg: #052e1b;
    --success-border: #065f46;
    --error-bg: #3b0d0d;
    --error-border: #7f1d1d;

    --diff-add-bg: #14532d;
    --diff-add-text: #bbf7d0;
    --diff-del-bg: #7f1d1d;
    --diff-del-text: #fecaca;

    --shadow-sm: 0 4px 6px -1px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 20px 25px -5px rgba(0, 0, 0, 0.5);
    color-scheme: dark;
}

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

/* Garante que o atributo hidden vença regras de display das classes */
[hidden] {
    display: none !important;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    background: linear-gradient(135deg, var(--bg-from) 0%, var(--bg-to) 100%);
    background-attachment: fixed;
    min-height: 100vh;
    color: var(--text);
    line-height: 1.6;
}

.container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 20px;
}

/* ---------- Header ---------- */
.header {
    position: relative;
    background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
    color: #fff;
    padding: 22px 120px;
    border-radius: 20px;
    text-align: center;
    margin-bottom: 20px;
    box-shadow: var(--shadow-lg);
}

.header h1 {
    font-size: 2rem;
    font-weight: 800;
    margin-bottom: 8px;
    letter-spacing: -0.025em;
}

.header p {
    font-size: 1rem;
    opacity: 0.9;
}

.theme-toggle {
    position: absolute;
    top: 16px;
    right: 16px;
    background: rgba(255, 255, 255, 0.15);
    border: 1px solid rgba(255, 255, 255, 0.3);
    color: #fff;
    border-radius: 999px;
    width: 42px;
    height: 42px;
    font-size: 18px;
    cursor: pointer;
    transition: background 0.2s ease;
}

.theme-toggle.logout-btn {
    right: 68px;
}

.theme-toggle:hover {
    background: rgba(255, 255, 255, 0.3);
}

/* ---------- Layout ---------- */
.main-grid {
    display: grid;
    grid-template-columns: 420px 1fr;
    gap: 30px;
    margin-bottom: 30px;
}

.sidebar,
.card {
    background: var(--surface);
    border-radius: 16px;
    padding: 30px;
    box-shadow: var(--shadow-sm);
    border: 1px solid var(--border);
}

.sidebar {
    height: fit-content;
}

.section-title {
    font-size: 1.4rem;
    font-weight: 700;
    color: var(--text);
    margin-bottom: 20px;
    display: flex;
    align-items: center;
    gap: 12px;
    flex-wrap: wrap;
}

/* ---------- Tabs ---------- */
.tabs {
    display: flex;
    flex-wrap: wrap;
    background: var(--surface);
    border: 1px solid var(--border);
    box-shadow: var(--shadow-sm);
    border-radius: 14px;
    padding: 6px;
    margin-bottom: 20px;
    gap: 4px;
}

.tab {
    flex: 1 1 auto;
    white-space: nowrap;
    padding: 11px 16px;
    border: none;
    background: transparent;
    border-radius: 8px;
    font-weight: 600;
    font-size: 15px;
    cursor: pointer;
    transition: all 0.2s ease;
    color: var(--text-muted);
}

.tab.active {
    background: var(--primary);
    color: #fff;
    box-shadow: 0 2px 4px rgba(37, 99, 235, 0.2);
}

.tab:hover:not(.active) {
    background: var(--primary-soft);
    color: var(--primary);
}

.tab-description {
    color: var(--text-muted);
    font-size: 14px;
    margin: -8px 0 18px;
}

.optional {
    font-weight: 400;
    color: var(--text-faint);
    font-size: 12px;
}

.row-2 {
    display: flex;
    gap: 12px;
}

.row-2 > .input-group {
    flex: 1;
    min-width: 0;
}

textarea.textarea-small {
    min-height: 80px;
}

.sidebar-footer {
    display: flex;
    align-items: flex-end;
    gap: 12px;
    flex-wrap: wrap;
    margin-top: 20px;
    padding-top: 16px;
    border-top: 1px solid var(--border);
}

.sidebar-footer .input-group {
    margin-bottom: 0;
    flex: 1;
    min-width: 160px;
}

.sidebar-footer label {
    font-size: 12px;
    color: var(--text-muted);
    margin-bottom: 4px;
}

.sidebar-footer select {
    padding: 8px 10px;
    font-size: 14px;
}

.shortcut-hint {
    font-size: 12px;
    color: var(--text-muted);
    padding-bottom: 8px;
}

/* ---------- Forms ---------- */
.api-config {
    background: var(--primary-softer);
    border: 2px solid var(--primary-soft);
    border-radius: 12px;
    padding: 20px;
    margin-bottom: 25px;
}

.api-config .input-group:last-child {
    margin-bottom: 0;
}

.input-group {
    margin-bottom: 20px;
}

.input-group[hidden] {
    display: none;
}

label {
    display: block;
    font-weight: 600;
    color: var(--text);
    margin-bottom: 8px;
    font-size: 14px;
}

input,
textarea,
select {
    width: 100%;
    padding: 12px 14px;
    border: 2px solid var(--border);
    border-radius: 10px;
    font-size: 15px;
    font-family: inherit;
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
    background: var(--surface);
    color: var(--text);
}

input:focus,
textarea:focus,
select:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}

textarea {
    min-height: 220px;
    resize: vertical;
}

.textarea-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
    margin-top: 6px;
    font-size: 12px;
    color: var(--text-muted);
}

.char-counter.warn {
    color: var(--warning);
    font-weight: 600;
}

.char-counter.over {
    color: var(--error);
    font-weight: 700;
}

.link-btn {
    background: none;
    border: none;
    color: var(--primary);
    cursor: pointer;
    font-size: 12px;
    font-weight: 600;
    padding: 0;
}

.link-btn:hover {
    text-decoration: underline;
}

.model-info {
    font-size: 12px;
    color: var(--text-muted);
    margin-top: 6px;
    font-style: italic;
}

kbd {
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-bottom-width: 2px;
    border-radius: 4px;
    padding: 0 5px;
    font-size: 11px;
    font-family: inherit;
}

/* ---------- Buttons ---------- */
.btn {
    background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
    color: #fff;
    border: none;
    padding: 15px 24px;
    border-radius: 12px;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
    width: 100%;
}

.btn:hover:not(:disabled) {
    transform: translateY(-1px);
    box-shadow: 0 10px 25px rgba(37, 99, 235, 0.3);
}

.btn:disabled {
    background: var(--text-faint);
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}

.btn-small {
    width: auto;
    padding: 9px 16px;
    font-size: 13px;
    border-radius: 10px;
}

.btn-secondary {
    background: var(--surface-2);
    color: var(--text);
    border: 1px solid var(--border);
}

.btn-secondary:hover:not(:disabled) {
    background: var(--primary-soft);
    color: var(--primary);
    box-shadow: none;
}

.btn-secondary.active {
    background: var(--primary);
    color: #fff;
    border-color: var(--primary);
}

.btn-danger {
    background: var(--error);
}

.btn-danger:hover:not(:disabled) {
    background: var(--error-dark);
    box-shadow: 0 8px 20px rgba(239, 68, 68, 0.3);
}

.action-row {
    display: flex;
    gap: 10px;
}

.action-row .btn-cancel {
    flex: 0 0 auto;
    width: auto;
}

/* ---------- Output ---------- */
.output-container {
    margin-top: 0;
}

.output-toolbar {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 12px;
    align-items: center;
}

.output-toolbar .spacer {
    flex: 1;
}

.output-meta {
    font-size: 12px;
    color: var(--text-muted);
}

.output-area {
    background: var(--surface);
    border: 2px solid var(--border);
    border-radius: 12px;
    padding: 25px;
    min-height: 480px;
    overflow-y: auto;
    font-size: 15px;
    line-height: 1.7;
    overflow-wrap: anywhere;
}

.output-area.streaming::after {
    content: '▍';
    animation: blink 1s steps(1) infinite;
    color: var(--primary);
}

@keyframes blink {
    50% { opacity: 0; }
}

.output-area h1,
.output-area h2,
.output-area h3 {
    color: var(--text);
    margin: 0 0 15px 0;
}

.output-area h1 { font-size: 1.5rem; }
.output-area h2 { font-size: 1.3rem; }
.output-area h3 { font-size: 1.1rem; }

.output-area p {
    margin-bottom: 15px;
    color: var(--text);
}

.output-area strong {
    color: var(--primary-dark);
    font-weight: 600;
}

[data-theme="dark"] .output-area strong {
    color: #93c5fd;
}

.output-area ul,
.output-area ol {
    margin: 15px 0;
    padding-left: 25px;
}

.output-area li {
    margin-bottom: 8px;
}

.output-area hr {
    border: none;
    border-top: 1px solid var(--border);
    margin: 20px 0;
}

.output-area a {
    color: var(--primary);
}

.plain-output {
    white-space: pre-wrap;
    font-family: inherit;
    background: none;
    border: none;
    padding: 0;
}

/* Diff */
.diff-view {
    white-space: pre-wrap;
    line-height: 1.9;
}

.diff-view ins {
    background: var(--diff-add-bg);
    color: var(--diff-add-text);
    text-decoration: none;
    border-radius: 3px;
    padding: 1px 2px;
}

.diff-view del {
    background: var(--diff-del-bg);
    color: var(--diff-del-text);
    border-radius: 3px;
    padding: 1px 2px;
}

.diff-legend {
    display: flex;
    gap: 16px;
    font-size: 12px;
    color: var(--text-muted);
    margin-bottom: 14px;
    padding-bottom: 12px;
    border-bottom: 1px solid var(--border);
}

/* Loading */
.loading {
    display: none;
    text-align: center;
    padding: 40px;
}

.spinner {
    border: 3px solid var(--surface-2);
    border-top: 3px solid var(--primary);
    border-radius: 50%;
    width: 50px;
    height: 50px;
    animation: spin 1s linear infinite;
    margin: 0 auto 20px;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

.loading-text {
    color: var(--text-muted);
    font-weight: 500;
}

/* ---------- Status / messages ---------- */
.api-status {
    padding: 4px 12px;
    border-radius: 20px;
    font-size: 12px;
    font-weight: 600;
}

.api-status.connected {
    background: #dcfce7;
    color: #166534;
}

.api-status.disconnected {
    background: #fef2f2;
    color: #dc2626;
}

.api-status.warning {
    background: #fef3c7;
    color: #92400e;
}

.toast-container {
    position: fixed;
    bottom: 20px;
    right: 20px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    z-index: 1000;
    max-width: min(420px, calc(100vw - 40px));
}

.message {
    padding: 14px 16px;
    border-radius: 10px;
    font-weight: 500;
    box-shadow: var(--shadow-lg);
    animation: toast-in 0.2s ease;
}

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

.message.error {
    background: var(--error-bg);
    border: 2px solid var(--error-border);
    color: var(--error);
}

.message.success {
    background: var(--success-bg);
    border: 2px solid var(--success-border);
    color: var(--success);
}

.message.info {
    background: var(--primary-softer);
    border: 2px solid var(--primary-soft);
    color: var(--primary);
}

/* ---------- History ---------- */
.history-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 12px;
    margin-bottom: 20px;
}

.history-header .section-title {
    margin-bottom: 0;
}

.history-actions {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    align-items: center;
}

.history-actions input {
    width: 240px;
    padding: 8px 12px;
    font-size: 14px;
}

.history-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
    gap: 20px;
}

.history-item {
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-left: 4px solid var(--primary);
    padding: 18px;
    border-radius: 12px;
    transition: all 0.2s ease;
    display: flex;
    flex-direction: column;
}

.history-item:hover {
    background: var(--surface);
    box-shadow: var(--shadow-sm);
}

.history-item h4 {
    color: var(--primary);
    margin-bottom: 8px;
    font-size: 15px;
}

.history-item p {
    color: var(--text-muted);
    font-size: 13px;
    margin-bottom: 6px;
}

.history-item .history-preview {
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.history-item-actions {
    display: flex;
    gap: 8px;
    margin-top: auto;
    padding-top: 10px;
}

.history-item-actions button {
    background: var(--primary);
    color: #fff;
    border: none;
    padding: 7px 14px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 12px;
    font-weight: 600;
    transition: background 0.2s ease;
}

.history-item-actions button:hover {
    background: var(--primary-dark);
}

.history-item-actions button.danger {
    background: transparent;
    color: var(--error);
    border: 1px solid var(--error-border);
    margin-left: auto;
}

.history-item-actions button.danger:hover {
    background: var(--error-bg);
}

.empty-state {
    text-align: center;
    padding: 60px 20px;
    color: var(--text-faint);
}

.empty-state svg {
    width: 80px;
    height: 80px;
    margin-bottom: 20px;
    opacity: 0.5;
}

/* ---------- Misc markdown ---------- */
pre {
    background: var(--surface-2);
    padding: 15px;
    border-radius: 8px;
    overflow-x: auto;
    border: 1px solid var(--border);
}

code {
    background: var(--surface-2);
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 14px;
}

blockquote {
    border-left: 4px solid var(--primary);
    padding-left: 20px;
    margin: 20px 0;
    font-style: italic;
    color: var(--text-muted);
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
}

/* ---------- Responsivo ---------- */
@media (max-width: 1024px) {
    .main-grid {
        grid-template-columns: 1fr;
        gap: 20px;
    }
}

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

    .header {
        padding: 52px 16px 20px;
    }

    .theme-toggle.logout-btn {
        right: 56px;
    }

    .theme-toggle {
        top: 12px;
        right: 12px;
        width: 36px;
        height: 36px;
        font-size: 16px;
    }

    .header h1 {
        font-size: 1.6rem;
    }

    .output-area {
        min-height: 300px;
        padding: 18px;
    }

    .card,
    .sidebar {
        padding: 20px;
    }

    /* Abas numa linha só, rolando para o lado */
    .tabs {
        flex-wrap: nowrap;
        overflow-x: auto;
        scrollbar-width: none;
        -webkit-overflow-scrolling: touch;
    }

    .tabs::-webkit-scrollbar {
        display: none;
    }

    .tab {
        flex: 0 0 auto;
        padding: 10px 14px;
        font-size: 14px;
    }

    .row-2 {
        flex-direction: column;
        gap: 0;
    }

    .history-actions input {
        width: 100%;
    }

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