/* === Reset y base === */
*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    /* Paleta TurboPos — Naranja corporativo + gris oscuro profesional */
    --color-primary: #e8590c;
    --color-primary-dark: #c2410c;
    --color-primary-light: #fed7aa;
    --color-primary-bg: #fff7ed;
    --color-accent: #ea580c;
    --color-bg: #f8fafc;
    --color-surface: #ffffff;
    --color-text: #1e293b;
    --color-text-light: #64748b;
    --color-text-muted: #94a3b8;
    --color-border: #e2e8f0;
    --color-border-light: #f1f5f9;
    --color-user-bubble: #e8590c;
    --color-assistant-bubble: #ffffff;
    --color-header-bg: #1e293b;
    --color-header-accent: #e8590c;
    --radius: 12px;
    --radius-sm: 8px;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
    --shadow-md: 0 2px 8px rgba(0, 0, 0, 0.08);
    --shadow-lg: 0 4px 24px rgba(0, 0, 0, 0.12);
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: linear-gradient(135deg, #1e293b 0%, #334155 100%);
    color: var(--color-text);
    height: 100dvh;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 16px;
}

/* === Contenedor principal === */
.chat-container {
    width: 100%;
    max-width: 780px;
    height: 100%;
    max-height: 920px;
    background: var(--color-surface);
    border-radius: 20px;
    box-shadow: 0 8px 40px rgba(0, 0, 0, 0.25);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    border: 1px solid rgba(255, 255, 255, 0.1);
}

/* === Header — Estilo TurboPos oscuro con acento naranja === */
.chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 20px;
    background: var(--color-header-bg);
    color: white;
    border-bottom: 3px solid var(--color-header-accent);
}

.header-info {
    display: flex;
    align-items: center;
    gap: 12px;
}

.header-icon {
    width: 40px;
    height: 40px;
    background: var(--color-primary);
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    line-height: 1;
    font-weight: 700;
    color: white;
    box-shadow: 0 2px 8px rgba(232, 89, 12, 0.4);
}

.chat-header h1 {
    font-size: 16px;
    font-weight: 700;
    letter-spacing: -0.01em;
}

.chat-header p {
    font-size: 12px;
    opacity: 0.7;
    font-weight: 400;
}

.header-status {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 12px;
    opacity: 0.9;
    font-weight: 500;
}

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #22c55e;
    box-shadow: 0 0 6px rgba(34, 197, 94, 0.5);
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

/* === Área de mensajes === */
.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 16px;
    scroll-behavior: smooth;
    background: var(--color-bg);
}

/* Scrollbar personalizado */
.chat-messages::-webkit-scrollbar {
    width: 6px;
}
.chat-messages::-webkit-scrollbar-track {
    background: transparent;
}
.chat-messages::-webkit-scrollbar-thumb {
    background: var(--color-border);
    border-radius: 3px;
}
.chat-messages::-webkit-scrollbar-thumb:hover {
    background: var(--color-text-muted);
}

/* === Mensajes === */
.message {
    display: flex;
    max-width: 88%;
    animation: messageIn 0.3s ease-out;
}

.message.user {
    align-self: flex-end;
}

.message.assistant {
    align-self: flex-start;
}

.message-bubble {
    padding: 12px 16px;
    border-radius: var(--radius);
    line-height: 1.6;
    font-size: 14px;
    box-shadow: var(--shadow);
}

.message.user .message-bubble {
    background: linear-gradient(135deg, var(--color-primary) 0%, var(--color-primary-dark) 100%);
    color: white;
    border-bottom-right-radius: 4px;
    box-shadow: 0 2px 8px rgba(232, 89, 12, 0.25);
}

.message.assistant .message-bubble {
    background: var(--color-assistant-bubble);
    color: var(--color-text);
    border: 1px solid var(--color-border);
    border-bottom-left-radius: 4px;
    box-shadow: var(--shadow-md);
}

/* ============================================
   MARKDOWN DENTRO DE RESPUESTAS DEL ASISTENTE
   — Formato limpio, legible y espaciado
   ============================================ */

.message.assistant .message-bubble p {
    margin-bottom: 10px;
    line-height: 1.7;
}

.message.assistant .message-bubble p:last-child {
    margin-bottom: 0;
}

.message.assistant .message-bubble h1,
.message.assistant .message-bubble h2,
.message.assistant .message-bubble h3,
.message.assistant .message-bubble h4 {
    color: var(--color-text);
    line-height: 1.3;
    margin-top: 16px;
    margin-bottom: 8px;
}

.message.assistant .message-bubble h1:first-child,
.message.assistant .message-bubble h2:first-child,
.message.assistant .message-bubble h3:first-child,
.message.assistant .message-bubble h4:first-child {
    margin-top: 0;
}

.message.assistant .message-bubble h1 {
    font-size: 18px;
    font-weight: 700;
    padding-bottom: 6px;
    border-bottom: 2px solid var(--color-primary-light);
}

.message.assistant .message-bubble h2 {
    font-size: 16px;
    font-weight: 700;
    color: var(--color-primary-dark);
}

.message.assistant .message-bubble h3 {
    font-size: 15px;
    font-weight: 600;
}

/* Links en burbujas del asistente */
.message.assistant .message-bubble a {
    color: var(--color-primary);
    text-decoration: underline;
    word-break: break-all;
}

.message.assistant .message-bubble a:hover {
    color: var(--color-primary-light);
}

.message.assistant .message-bubble h4 {
    font-size: 14px;
    font-weight: 600;
    color: var(--color-text-light);
}

.message.assistant .message-bubble strong {
    font-weight: 600;
    color: var(--color-text);
}

.message.assistant .message-bubble em {
    font-style: italic;
    color: var(--color-text-light);
}

.message.assistant .message-bubble hr {
    border: none;
    border-top: 1px solid var(--color-border);
    margin: 12px 0;
}

/* Listas — con mejor espaciado e indicadores */
.message.assistant .message-bubble ul,
.message.assistant .message-bubble ol {
    margin: 10px 0;
    padding-left: 0;
    list-style: none;
}

.message.assistant .message-bubble ul li {
    position: relative;
    padding-left: 20px;
    margin-bottom: 6px;
    line-height: 1.6;
}

.message.assistant .message-bubble ul li::before {
    content: "";
    position: absolute;
    left: 4px;
    top: 9px;
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--color-primary);
}

.message.assistant .message-bubble ol {
    counter-reset: item;
}

.message.assistant .message-bubble ol li {
    position: relative;
    padding-left: 28px;
    margin-bottom: 6px;
    line-height: 1.6;
    counter-increment: item;
}

.message.assistant .message-bubble ol li::before {
    content: counter(item);
    position: absolute;
    left: 0;
    top: 1px;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: var(--color-primary);
    color: white;
    font-size: 11px;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Código inline */
.message.assistant .message-bubble code {
    background: var(--color-primary-bg);
    border: 1px solid var(--color-primary-light);
    padding: 1px 6px;
    border-radius: 4px;
    font-size: 13px;
    font-family: 'SF Mono', 'Fira Code', Consolas, 'Courier New', monospace;
    color: var(--color-primary-dark);
    font-weight: 500;
}

/* Bloques de código con header */
.message.assistant .message-bubble .code-block {
    margin: 12px 0;
    border-radius: var(--radius-sm);
    overflow: hidden;
    border: 1px solid var(--color-border);
}

.message.assistant .message-bubble .code-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 6px 12px;
    background: #1e293b;
    border-bottom: 1px solid #334155;
}

.message.assistant .message-bubble .code-lang {
    font-size: 11px;
    font-weight: 600;
    color: #94a3b8;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.message.assistant .message-bubble .copy-btn {
    padding: 2px 10px;
    border: 1px solid #475569;
    border-radius: 4px;
    background: transparent;
    color: #94a3b8;
    font-size: 11px;
    cursor: pointer;
    font-family: inherit;
    transition: all 0.2s;
}

.message.assistant .message-bubble .copy-btn:hover {
    background: #334155;
    color: #e2e8f0;
    border-color: #64748b;
}

.message.assistant .message-bubble .copy-btn.copied {
    background: #22c55e;
    color: white;
    border-color: #22c55e;
}

.message.assistant .message-bubble .code-block pre {
    background: #0f172a;
    color: #e2e8f0;
    padding: 14px 16px;
    margin: 0;
    overflow-x: auto;
    font-size: 13px;
    line-height: 1.6;
    border-radius: 0;
}

.message.assistant .message-bubble .code-block pre code {
    background: none;
    border: none;
    padding: 0;
    color: inherit;
    font-size: inherit;
    font-weight: 400;
}

/* Pre sin wrapper (fallback) */
.message.assistant .message-bubble pre {
    background: #0f172a;
    color: #e2e8f0;
    padding: 14px 16px;
    border-radius: var(--radius-sm);
    margin: 10px 0;
    overflow-x: auto;
    font-size: 13px;
    line-height: 1.6;
}

.message.assistant .message-bubble pre code {
    background: none;
    border: none;
    padding: 0;
    color: inherit;
    font-weight: 400;
}

/* === Indicador de escritura === */
.typing-indicator {
    display: flex;
    align-items: center;
    gap: 5px;
    padding: 14px 18px;
    background: var(--color-assistant-bubble);
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    border-bottom-left-radius: 4px;
    box-shadow: var(--shadow);
    align-self: flex-start;
}

.typing-indicator span {
    width: 8px;
    height: 8px;
    background: var(--color-primary);
    border-radius: 50%;
    animation: bounce 1.4s infinite both;
    opacity: 0.6;
}

.typing-indicator span:nth-child(2) { animation-delay: 0.16s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.32s; }

.tool-indicator {
    display: inline-block;
    margin-top: 8px;
    padding: 4px 10px;
    font-size: 0.8rem;
    color: var(--color-primary);
    border: 1px solid var(--color-primary);
    border-radius: 12px;
    opacity: 0.8;
    animation: pulse-tool 1.5s infinite;
}

@keyframes pulse-tool {
    0%, 100% { opacity: 0.5; }
    50% { opacity: 1; }
}

/* === Input === */
.chat-input-area {
    padding: 14px 20px;
    border-top: 1px solid var(--color-border);
    background: var(--color-surface);
}

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

#messageInput {
    flex: 1;
    padding: 12px 18px;
    border: 2px solid var(--color-border);
    border-radius: 24px;
    font-size: 14px;
    outline: none;
    transition: border-color 0.2s, box-shadow 0.2s;
    font-family: inherit;
    background: var(--color-bg);
}

#messageInput:focus {
    border-color: var(--color-primary);
    box-shadow: 0 0 0 3px rgba(232, 89, 12, 0.1);
    background: white;
}

#messageInput:disabled {
    background: var(--color-border-light);
    color: var(--color-text-muted);
}

#sendButton {
    width: 44px;
    height: 44px;
    border: none;
    border-radius: 50%;
    background: var(--color-primary);
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s, transform 0.1s, box-shadow 0.2s;
    flex-shrink: 0;
    box-shadow: 0 2px 8px rgba(232, 89, 12, 0.3);
}

#sendButton:hover {
    background: var(--color-primary-dark);
    box-shadow: 0 4px 12px rgba(232, 89, 12, 0.4);
}

#sendButton:active {
    transform: scale(0.95);
}

#sendButton:disabled {
    background: var(--color-text-muted);
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}

/* === Error toast === */
.error-toast {
    position: fixed;
    top: 20px;
    left: 50%;
    transform: translateX(-50%);
    background: #ef4444;
    color: white;
    padding: 12px 24px;
    border-radius: var(--radius-sm);
    font-size: 14px;
    font-weight: 500;
    box-shadow: var(--shadow-lg);
    z-index: 100;
    animation: slideDown 0.3s ease-out;
}

/* === Botones de categoría — Estilo TurboPos === */
.category-buttons {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    padding: 8px 0;
    align-self: flex-start;
    max-width: 88%;
    animation: messageIn 0.3s ease-out;
}

.category-btn {
    padding: 8px 18px;
    border: 2px solid var(--color-primary);
    border-radius: 22px;
    background: var(--color-surface);
    color: var(--color-primary);
    font-size: 13px;
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
    transition: all 0.2s;
    white-space: nowrap;
}

.category-btn:hover {
    background: var(--color-primary);
    color: white;
    box-shadow: 0 2px 8px rgba(232, 89, 12, 0.3);
    transform: translateY(-1px);
}

.category-btn:active {
    transform: scale(0.97);
}

/* === Notificación de cambio de tema === */
.topic-change-notice {
    text-align: center;
    padding: 6px 16px;
    font-size: 12px;
    font-weight: 500;
    color: var(--color-text-light);
    background: white;
    border: 1px solid var(--color-border);
    border-radius: 20px;
    align-self: center;
    animation: messageIn 0.3s ease-out;
}

/* === Animaciones === */
@keyframes messageIn {
    from { opacity: 0; transform: translateY(12px); }
    to { opacity: 1; transform: translateY(0); }
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

@keyframes bounce {
    0%, 80%, 100% { transform: scale(0); }
    40% { transform: scale(1); }
}

@keyframes slideDown {
    from { opacity: 0; transform: translate(-50%, -20px); }
    to { opacity: 1; transform: translate(-50%, 0); }
}

/* === Botón de adjuntar imagen === */
.attach-btn {
    width: 44px;
    height: 44px;
    border: none;
    border-radius: 50%;
    background: transparent;
    color: var(--color-text-muted);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: color 0.2s, background 0.2s;
    flex-shrink: 0;
}

.attach-btn:hover {
    color: var(--color-primary);
    background: var(--color-primary-bg);
}

.attach-btn:disabled {
    color: var(--color-border);
    cursor: not-allowed;
}

/* === Preview de imagen seleccionada === */
.image-preview {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 20px 0;
    position: relative;
}

.image-preview img {
    max-height: 80px;
    max-width: 120px;
    border-radius: var(--radius-sm);
    border: 2px solid var(--color-primary-light);
    object-fit: cover;
}

.remove-image-btn {
    width: 24px;
    height: 24px;
    border: none;
    border-radius: 50%;
    background: var(--color-text-muted);
    color: white;
    font-size: 16px;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
}

.remove-image-btn:hover {
    background: #ef4444;
}

/* === Imagen dentro de burbuja del usuario === */
.message.user .message-bubble img {
    max-width: 200px;
    max-height: 200px;
    border-radius: var(--radius-sm);
    display: block;
    margin-top: 8px;
    border: 1px solid rgba(255, 255, 255, 0.2);
}

/* === Drag & drop === */
.chat-container.drag-over {
    outline: 3px dashed var(--color-primary);
    outline-offset: -3px;
}

/* === Botón de corrección (modo corrector) === */
.correction-btn {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    margin-top: 8px;
    padding: 4px 12px;
    border: 1px solid var(--color-border);
    border-radius: 14px;
    background: var(--color-surface);
    color: var(--color-text-muted);
    font-size: 11px;
    cursor: pointer;
    transition: all 0.2s;
    font-family: inherit;
}

.correction-btn:hover {
    border-color: #ef4444;
    color: #ef4444;
    background: #fef2f2;
}

.correction-btn svg {
    width: 12px;
    height: 12px;
}

/* === Modal de corrección === */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 200;
    animation: fadeIn 0.2s ease-out;
}

.modal {
    background: var(--color-surface);
    border-radius: 16px;
    box-shadow: 0 16px 48px rgba(0, 0, 0, 0.25);
    width: 90%;
    max-width: 560px;
    max-height: 90vh;
    overflow-y: auto;
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid var(--color-border);
}

.modal-header h2 {
    font-size: 16px;
    font-weight: 700;
}

.modal-close {
    width: 32px;
    height: 32px;
    border: none;
    border-radius: 50%;
    background: transparent;
    font-size: 20px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--color-text-muted);
    transition: background 0.2s;
}

.modal-close:hover {
    background: var(--color-bg);
}

.modal-body {
    padding: 16px 20px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.modal-body label {
    font-size: 13px;
    font-weight: 600;
    color: var(--color-text-light);
    margin-top: 4px;
}

.modal-body textarea {
    width: 100%;
    padding: 10px 12px;
    border: 2px solid var(--color-border);
    border-radius: var(--radius-sm);
    font-size: 13px;
    font-family: inherit;
    resize: vertical;
    line-height: 1.6;
    transition: border-color 0.2s;
}

.modal-body textarea:focus {
    outline: none;
    border-color: var(--color-primary);
}

.modal-body textarea[readonly] {
    background: var(--color-bg);
    color: var(--color-text-light);
}

.modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    padding: 12px 20px 16px;
}

.modal-btn-cancel {
    padding: 8px 18px;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    background: var(--color-surface);
    color: var(--color-text);
    font-size: 13px;
    font-weight: 500;
    font-family: inherit;
    cursor: pointer;
    transition: background 0.2s;
}

.modal-btn-cancel:hover {
    background: var(--color-bg);
}

.modal-btn-save {
    padding: 8px 18px;
    border: none;
    border-radius: var(--radius-sm);
    background: var(--color-primary);
    color: white;
    font-size: 13px;
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
    transition: background 0.2s;
}

.modal-btn-save:hover {
    background: var(--color-primary-dark);
}

.modal-btn-save:disabled {
    background: var(--color-text-muted);
    cursor: not-allowed;
}

/* === Indicador modo corrector en header === */
.corrector-badge {
    display: inline-block;
    padding: 2px 8px;
    background: #ef4444;
    border-radius: 10px;
    font-size: 10px;
    font-weight: 700;
    margin-left: 8px;
    vertical-align: middle;
    letter-spacing: 0.02em;
}

/* === Responsive — Modo inmersivo móvil === */
@media (max-width: 640px) {
    body {
        padding: 0;
        background: var(--color-header-bg);
    }
    .chat-container {
        border-radius: 0;
        max-height: none;
        height: 100dvh;
        border: none;
    }
    .message {
        max-width: 94%;
    }
    .chat-messages {
        padding: 16px;
        padding-bottom: env(safe-area-inset-bottom, 16px);
        gap: 14px;
    }
    .message-bubble {
        font-size: 16px;
        padding: 14px 18px;
        line-height: 1.65;
    }
    .message.assistant .message-bubble p {
        line-height: 1.75;
    }
    .message.assistant .message-bubble h1 { font-size: 20px; }
    .message.assistant .message-bubble h2 { font-size: 18px; }
    .message.assistant .message-bubble h3 { font-size: 16px; }
    .message.assistant .message-bubble ul li,
    .message.assistant .message-bubble ol li {
        font-size: 16px;
        line-height: 1.65;
    }
    .message.assistant .message-bubble code {
        font-size: 14px;
    }
    .chat-header {
        padding: 14px 20px;
        padding-top: calc(14px + env(safe-area-inset-top, 0px));
    }
    .chat-input-area {
        padding: 12px 16px;
        padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
    }
    #messageInput {
        font-size: 16px;
        padding: 12px 16px;
    }
    .category-btn {
        font-size: 14px;
        padding: 10px 20px;
    }
}
