:root {
    --bg: #f7f7f8;
    --bg-elev: #ffffff;
    --fg: #1a1a1a;
    --fg-muted: #6b7280;
    --border: #e5e7eb;
    --accent: #4f46e5;
    --accent-hover: #4338ca;
    --user-bubble: #eef2ff;
    --assistant-bubble: #ffffff;
    --error-bg: #fef2f2;
    --error-fg: #b91c1c;
    --tool-bg: #f0fdf4;
    --tool-border: #86efac;
    --radius: 12px;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0f1117;
        --bg-elev: #1a1d29;
        --fg: #e6e8ee;
        --fg-muted: #9ca3af;
        --border: #2b2f3a;
        --accent: #6366f1;
        --accent-hover: #818cf8;
        --user-bubble: #1e2536;
        --assistant-bubble: #1a1d29;
        --error-bg: #2a1414;
        --error-fg: #fca5a5;
        --tool-bg: #14251c;
        --tool-border: #166534;
    }
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
                 "Helvetica Neue", Arial, sans-serif;
    background: var(--bg);
    color: var(--fg);
    font-size: 15px;
    line-height: 1.55;
}

.app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    max-width: 960px;
    margin: 0 auto;
}

/* ─── Topbar ────────────────────────────────────────────── */
.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 20px;
    border-bottom: 1px solid var(--border);
    background: var(--bg-elev);
    position: sticky;
    top: 0;
    z-index: 10;
}
.brand { font-weight: 600; font-size: 16px; }
.brand span { margin-left: 6px; }

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

.badge {
    font-size: 12px;
    padding: 3px 10px;
    border-radius: 999px;
    background: var(--user-bubble);
    color: var(--accent);
    font-weight: 500;
}

.icon-btn {
    border: 1px solid var(--border);
    background: transparent;
    color: var(--fg);
    padding: 6px 10px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 15px;
}
.icon-btn:hover { background: var(--bg); }

/* ─── Config panel ──────────────────────────────────────── */
.config-panel {
    background: var(--bg-elev);
    border-bottom: 1px solid var(--border);
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 14px;
    max-height: 70vh;
    overflow-y: auto;
}
.config-panel.hidden { display: none; }
.config-panel h2 {
    margin: 0;
    font-size: 15px;
    font-weight: 600;
    color: var(--fg-muted);
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.field {
    display: flex;
    flex-direction: column;
    gap: 4px;
    font-size: 13px;
}
.field > span {
    color: var(--fg-muted);
    font-weight: 500;
}
.field em { font-style: normal; opacity: 0.7; }

.field input[type="text"],
.field input[type="password"],
.field select {
    padding: 9px 12px;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: var(--bg);
    color: var(--fg);
    font-family: inherit;
    font-size: 14px;
}
.field input:focus, .field select:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.15);
}
.field .hint {
    font-size: 12px;
    color: var(--accent);
    text-decoration: none;
}

.field input[type="range"] { width: 100%; }

.config-actions {
    display: flex;
    gap: 8px;
    justify-content: flex-end;
    margin-top: 4px;
}

.btn-primary {
    background: var(--accent);
    color: #fff;
    border: none;
    padding: 9px 18px;
    border-radius: 8px;
    font-weight: 500;
    font-size: 14px;
    cursor: pointer;
    font-family: inherit;
}
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); }
.btn-primary:disabled { opacity: 0.4; cursor: not-allowed; }

.btn-secondary {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--fg);
    padding: 9px 14px;
    border-radius: 8px;
    font-size: 14px;
    cursor: pointer;
    font-family: inherit;
}
.btn-secondary:hover { background: var(--bg); }

.config-status {
    font-size: 13px;
    padding: 8px 12px;
    border-radius: 8px;
    display: none;
}
.config-status.success { background: var(--tool-bg); color: var(--tool-border); display: block; }
.config-status.error   { background: var(--error-bg); color: var(--error-fg); display: block; }

.note {
    font-size: 12px;
    color: var(--fg-muted);
    border-top: 1px solid var(--border);
    padding-top: 12px;
    margin: 4px 0 0 0;
}
.note code {
    background: var(--bg);
    padding: 1px 5px;
    border-radius: 4px;
    font-size: 11px;
}

/* ─── Chat ──────────────────────────────────────────────── */
#chat {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.empty-state {
    text-align: center;
    padding: 60px 20px;
    color: var(--fg-muted);
    max-width: 600px;
    margin: 0 auto;
}
.empty-state h2 {
    font-size: 22px;
    font-weight: 600;
    color: var(--fg);
    margin: 0 0 8px 0;
}
.suggestions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    justify-content: center;
    margin-top: 24px;
}
.suggestions button {
    background: var(--bg-elev);
    border: 1px solid var(--border);
    color: var(--fg);
    padding: 8px 14px;
    border-radius: 999px;
    cursor: pointer;
    font-size: 13px;
    font-family: inherit;
}
.suggestions button:hover { border-color: var(--accent); color: var(--accent); }

.msg {
    display: flex;
    flex-direction: column;
    gap: 6px;
    animation: fade-in 0.2s ease-out;
}
@keyframes fade-in {
    from { opacity: 0; transform: translateY(4px); }
    to   { opacity: 1; transform: translateY(0); }
}

.msg .role {
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--fg-muted);
}
.msg.user .role { align-self: flex-end; }

.msg .bubble {
    padding: 12px 16px;
    border-radius: var(--radius);
    max-width: 85%;
    overflow-wrap: break-word;
}
.msg.user .bubble {
    background: var(--user-bubble);
    align-self: flex-end;
    border: 1px solid var(--border);
}
.msg.assistant .bubble {
    background: var(--assistant-bubble);
    border: 1px solid var(--border);
    box-shadow: var(--shadow);
}

.msg.assistant .bubble p:first-child { margin-top: 0; }
.msg.assistant .bubble p:last-child  { margin-bottom: 0; }
.msg.assistant .bubble pre {
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 10px;
    overflow-x: auto;
    font-size: 13px;
}
.msg.assistant .bubble code {
    background: var(--bg);
    padding: 1px 5px;
    border-radius: 4px;
    font-size: 0.9em;
}
.msg.assistant .bubble pre code { background: none; padding: 0; }
.msg.assistant .bubble ul, .msg.assistant .bubble ol { padding-left: 22px; }
.msg.assistant .bubble table { border-collapse: collapse; margin: 8px 0; font-size: 13px; }
.msg.assistant .bubble th, .msg.assistant .bubble td {
    border: 1px solid var(--border);
    padding: 6px 10px;
    text-align: left;
}

.msg.assistant.error .bubble {
    background: var(--error-bg);
    color: var(--error-fg);
    border-color: var(--error-fg);
}

/* Traza de herramientas */
.trace {
    margin-top: 8px;
    font-size: 12px;
}
.trace summary {
    cursor: pointer;
    color: var(--fg-muted);
    padding: 6px 10px;
    border-radius: 6px;
    user-select: none;
}
.trace summary:hover { background: var(--bg); }
.trace summary::marker { content: "▸ "; }
.trace[open] summary::marker { content: "▾ "; }

.trace-items {
    margin-top: 6px;
    padding: 8px 12px;
    background: var(--tool-bg);
    border: 1px solid var(--tool-border);
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.trace-item {
    font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
    font-size: 11.5px;
    line-height: 1.5;
}
.trace-item .tool-name {
    color: var(--accent);
    font-weight: 600;
}
.trace-item .tool-args { color: var(--fg-muted); }
.trace-item .tool-preview {
    color: var(--fg-muted);
    white-space: pre-wrap;
    margin-top: 2px;
}
.trace-item.fail .tool-name { color: var(--error-fg); }

/* Typing indicator */
.typing {
    display: inline-flex;
    gap: 4px;
    padding: 12px 16px;
}
.typing span {
    width: 6px; height: 6px;
    border-radius: 50%;
    background: var(--fg-muted);
    animation: typing 1.4s infinite;
}
.typing span:nth-child(2) { animation-delay: 0.2s; }
.typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes typing {
    0%, 60%, 100% { opacity: 0.3; transform: translateY(0); }
    30%           { opacity: 1;   transform: translateY(-3px); }
}

/* ─── Input form ────────────────────────────────────────── */
.input-form {
    display: flex;
    gap: 10px;
    padding: 14px 20px 18px;
    border-top: 1px solid var(--border);
    background: var(--bg-elev);
    align-items: flex-end;
}
.input-form textarea {
    flex: 1;
    resize: none;
    max-height: 200px;
    min-height: 42px;
    padding: 11px 14px;
    border-radius: 10px;
    border: 1px solid var(--border);
    background: var(--bg);
    color: var(--fg);
    font-family: inherit;
    font-size: 15px;
    line-height: 1.4;
    overflow-y: auto;
}
.input-form textarea:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.15);
}
.input-form textarea:disabled { opacity: 0.5; }

/* ─── Responsive ────────────────────────────────────────── */
@media (max-width: 640px) {
    .topbar { padding: 10px 14px; }
    .messages { padding: 14px; }
    .input-form { padding: 10px 14px 14px; }
    .msg .bubble { max-width: 95%; }
    .badge { display: none; }
}
