:root {
    color-scheme: dark;
    --bg: #0d1014;
    --panel: #171b21;
    --panel-2: #1f252d;
    --line: #2d3540;
    --line-soft: rgba(255, 255, 255, 0.08);
    --text: #f4f7fb;
    --muted: #9da8b8;
    --brand: #16b8c6;
    --brand-2: #47d18c;
    --danger: #ff5c5c;
    --warn: #f6b84b;
    --shadow: 0 22px 70px rgba(0, 0, 0, 0.42);
    --radius: 8px;
}

* {
    box-sizing: border-box;
}

html,
body {
    min-height: 100%;
}

body {
    margin: 0;
    background:
        linear-gradient(135deg, rgba(22, 184, 198, 0.12), transparent 34%),
        linear-gradient(315deg, rgba(71, 209, 140, 0.09), transparent 32%),
        var(--bg);
    color: var(--text);
    font-family: Inter, "Segoe UI", Roboto, Arial, sans-serif;
    letter-spacing: 0;
}

button,
input,
select,
textarea {
    font: inherit;
}

button {
    min-height: 38px;
    border: 1px solid var(--line);
    border-radius: 7px;
    background: var(--panel-2);
    color: var(--text);
    cursor: pointer;
    transition: background 0.16s, border-color 0.16s, transform 0.16s;
}

button:hover {
    border-color: rgba(22, 184, 198, 0.75);
    background: #27303a;
}

button:active {
    transform: translateY(1px);
}

button:disabled {
    cursor: not-allowed;
    opacity: 0.55;
}

input,
select,
textarea {
    width: 100%;
    border: 1px solid var(--line);
    border-radius: 7px;
    background: #10141a;
    color: var(--text);
    padding: 10px 12px;
    outline: none;
}

textarea {
    resize: vertical;
}

input:focus,
select:focus,
textarea:focus {
    border-color: var(--brand);
    box-shadow: 0 0 0 3px rgba(22, 184, 198, 0.13);
}

.app-shell {
    width: min(1400px, calc(100vw - 28px));
    height: calc(100dvh - 28px);
    margin: 14px auto;
    display: flex;
    flex-direction: column;
    gap: 14px;
    overflow: hidden;
}

.topbar,
.panel,
.modal-box {
    border: 1px solid var(--line-soft);
    background: rgba(23, 27, 33, 0.92);
    box-shadow: var(--shadow);
    backdrop-filter: blur(16px);
}

.topbar {
    min-height: 72px;
    border-radius: var(--radius);
    padding: 12px 14px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
}

.brand {
    display: flex;
    align-items: center;
    gap: 12px;
    min-width: 0;
}

.brand-mark {
    width: 42px;
    height: 42px;
    border-radius: 8px;
    display: grid;
    place-items: center;
    background: linear-gradient(145deg, var(--brand), var(--brand-2));
    color: #061012;
    font-weight: 900;
}

.brand h1 {
    margin: 0;
    font-size: clamp(1.2rem, 2.2vw, 1.75rem);
    line-height: 1.05;
}

.brand p {
    margin: 3px 0 0;
    color: var(--muted);
    font-size: 0.88rem;
}

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

.icon-btn,
.primary-btn,
.danger-btn,
.ghost-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 9px 12px;
    font-weight: 800;
}

.icon-btn svg,
.primary-btn svg,
.danger-btn svg,
.ghost-btn svg {
    width: 18px;
    height: 18px;
    flex: 0 0 auto;
}

.primary-btn {
    border-color: rgba(22, 184, 198, 0.65);
    background: var(--brand);
    color: #061012;
}

.primary-btn:hover {
    background: #35c8d3;
    color: #061012;
}

.danger-btn {
    border-color: rgba(255, 92, 92, 0.55);
    background: rgba(255, 92, 92, 0.15);
    color: #ffdada;
}

.danger-btn:hover {
    background: rgba(255, 92, 92, 0.25);
    border-color: var(--danger);
}

.ghost-btn {
    background: transparent;
}

.pill {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    min-height: 38px;
    padding: 8px 11px;
    border: 1px solid rgba(71, 209, 140, 0.45);
    border-radius: 7px;
    background: rgba(71, 209, 140, 0.11);
    color: #b7f8d2;
    font-weight: 800;
}

.chat-layout {
    flex: 1;
    display: grid;
    grid-template-columns: 1fr;
    grid-template-rows: 100%;
    overflow: hidden;
    min-height: 0;
    gap: 14px;
}

.panel {
    border-radius: var(--radius);
    min-width: 0;
}

.sidebar {
    padding: 14px;
    display: flex;
    overflow-y: auto;
    flex-direction: column;
    gap: 14px;
}

.settings-group {
    display: grid;
    gap: 9px;
}

.settings-group label {
    color: #c7d0dd;
    font-size: 0.82rem;
    font-weight: 800;
}

.range-row {
    display: grid;
    grid-template-columns: 1fr 52px;
    gap: 8px;
    align-items: center;
}

.conversation-panel {
    display: flex;
    flex-direction: column;
    height: 100%;
    min-height: 0;
    min-width: 0;
    overflow: hidden;
}

.messages {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    scroll-behavior: auto; /* Cambiado a auto para un scroll instantáneo y preciso tras render */
    padding: 18px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    min-height: 7.5em; /* Mínimo 5 líneas de texto (5 * 1.5 line-height) */
}

.empty-state {
    min-height: 100%;
    display: grid;
    place-items: center;
    color: var(--muted);
    text-align: center;
    padding: 30px;
}

.empty-state strong {
    display: block;
    color: var(--text);
    margin-bottom: 6px;
    font-size: 1.1rem;
}

.message {
    max-width: min(820px, 92%);
    border: 1px solid var(--line-soft);
    border-radius: 8px;
    padding: 11px 13px;
    line-height: 1.5;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
}

.message.user {
    align-self: flex-start;
    background: rgba(22, 184, 198, 0.13);
    border-color: rgba(22, 184, 198, 0.35);
}

.message.assistant {
    align-self: flex-start;
    background: rgba(255, 255, 255, 0.045);
}

.message.waiting {
    min-width: 3ch;
}

.message.system {
    align-self: center;
    max-width: 720px;
    color: #ffe1a6;
    background: rgba(246, 184, 75, 0.11);
    border-color: rgba(246, 184, 75, 0.28);
}

.message.error {
    align-self: center;
    max-width: 720px;
    color: #ffbdc0;
    background: rgba(255, 92, 92, 0.11);
    border-color: rgba(255, 92, 92, 0.28);
}

.composer {
    flex-shrink: 0;
    border-top: 1px solid var(--line-soft);
    padding: 12px;
    display: grid;
    grid-template-columns: 1fr auto;
    align-items: center;
    gap: 10px;
}

.composer textarea {
    min-height: 54px;
    max-height: 180px;
}

.send-btn {
    width: 54px;
    height: 54px;
    padding: 0;
    border-radius: 8px;
    align-self: center;
}

.send-btn.is-stopping {
    background: rgba(255, 92, 92, 0.22);
    border-color: rgba(255, 92, 92, 0.75);
    color: #ffe1e1;
}

.send-btn.is-stopping:hover {
    background: rgba(255, 92, 92, 0.34);
    border-color: var(--danger);
}

.modal {
    position: fixed;
    inset: 0;
    z-index: 1000;
    display: none;
    align-items: center;
    justify-content: center;
    padding: 18px;
    background: rgba(0, 0, 0, 0.72);
    backdrop-filter: blur(7px);
}

#noticeModal { z-index: 2000; }

.modal.open {
    display: flex;
}

.modal-box {
    width: min(460px, 96vw);
    max-height: 90vh;
    overflow: auto;
    border-radius: 8px;
    padding: 20px;
}

.modal-box.wide {
    width: min(980px, 96vw);
}

.modal-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    margin-bottom: 14px;
}

.modal-head h2,
.modal-head h3 {
    margin: 0;
}

.modal-x {
    width: 38px;
    height: 38px;
    border-radius: 7px;
    padding: 0;
    font-size: 1.2rem;
}

.form-grid {
    display: grid;
    gap: 10px;
}

.status-text {
    min-height: 20px;
    color: #ffbdc0;
    font-size: 0.9rem;
}

.list {
    display: grid;
    gap: 8px;
}

.list-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
    padding: 10px;
    border: 1px solid var(--line-soft);
    border-radius: 7px;
    background: rgba(255, 255, 255, 0.035);
}

.list-row span {
    display: block;
    color: var(--muted);
    font-size: 0.84rem;
}

.list-row-actions {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-wrap: wrap;
    justify-content: flex-end;
}

.menu-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
    gap: 14px;
}

.menu-card {
    min-height: 154px;
    padding: 16px;
    text-align: left;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: stretch;
    border-radius: 8px;
}

.menu-card strong {
    display: block;
    font-size: 1.25rem;
    margin-bottom: 6px;
}

.menu-card span {
    color: var(--muted);
}

.login-modal {
    display: flex;
}

@media (max-width: 860px) {
    .chat-layout {
        grid-template-columns: 1fr;
    }

    .topbar {
        align-items: flex-start;
        flex-direction: column;
    }

    .actions {
        justify-content: flex-start;
    }
}

/* Botón de salida con ID de usuario */
.logout-btn {
    flex-direction: column;
    line-height: 1.1;
    padding: 5px 12px;
    height: auto;
}

.user-id-tag {
    font-size: 10px;
    opacity: 0.8;
    font-weight: 400;
}

/* Utilidades generales */
.hidden { display: none; }
.mt-10 { margin-top: 10px; }
.mt-12 { margin-top: 12px; }

/* Formatos de inputs específicos */
.code-input {
    text-transform: uppercase;
    letter-spacing: 2px;
}

.input-credits { max-width: 130px; }
.input-model-pull { max-width: 260px; }
.input-inline-credits { width: 92px; }

/* Reset de sidebar para el modal de ajustes */
.settings-sidebar {
    background: none;
    border: none;
    padding: 0;
}
