:root {
    --bg: #0d0d0d;
    --sidebar: #151515;
    --accent: #3b82f6;
    --text: #f0f0f0;
    --msg-user: #222;
    --border: #333;
}

.light-mode {
    --bg: #ffffff;
    --sidebar: #f8f9fa;
    --accent: #0056b3;
    --text: #1a1a1a;
    --msg-user: #f1f3f5;
    --border: #dee2e6;
}

html,
body {
    height: 100%;
    width: 100%;
    margin: 0;
    padding: 0;
    overflow: hidden !important;
    /* Force no scroll on body */
    position: fixed;
    /* Lock body in place */
    top: 0;
    left: 0;
}

body {
    background: var(--bg);
    color: var(--text);
    font-family: 'Inter', sans-serif;
    transition: 0.3s;
    display: flex;
    flex-direction: column;
}

a {
    text-decoration: none;
    color: inherit;
}

/* Layout Grid para Apps con Sidebar */
.layout {
    display: flex;
    height: 100%;
    width: 100%;
    overflow: hidden;
    position: relative;
    /* Changed from fixed to relative as body is now fixed */
}

.main-content-area {
    padding: 24px;
}

@media (max-width: 991.98px) {
    .main-content-area {
        padding: 12px;
    }
}

/* Sidebar Common */
.sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: 260px;
    height: 100%;
    background: var(--sidebar);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    z-index: 250000 !important;
    /* Nivel altisimo */
    transition: transform 0.4s cubic-bezier(0.19, 1, 0.22, 1);
    box-shadow: 4px 0 30px rgba(0, 0, 0, 0.5);
}

.sidebar.active {
    transform: translateX(0) !important;
    visibility: visible !important;
}

.sidebar.collapsed {
    transform: translateX(-260px);
}

@media (max-width: 991.98px) {
    .sidebar {
        transform: translateX(-105%);
        box-shadow: 10px 0 30px rgba(0, 0, 0, 0.8) !important;
    }
}

.brand {
    padding: 20px;
    font-weight: 900;
    color: var(--accent);
    text-align: center;
    font-size: 1.2rem;
    border-bottom: 1px solid var(--border);
    letter-spacing: 1px;
}

/* Estilos de botones especiales */
.btn-incognito-active {
    color: #00d4ff !important;
    text-shadow: 0 0 10px rgba(0, 212, 255, 0.5);
    transform: scale(1.1);
}

.nav-link-custom {
    display: block;
    padding: 12px 20px;
    color: var(--text);
    opacity: 0.7;
    transition: 0.2s;
    border-left: 3px solid transparent;
    cursor: pointer;
    position: relative;
    z-index: 250002;
}

.nav-link-custom:hover,
.nav-link-custom.active {
    opacity: 1;
    background: rgba(255, 255, 255, 0.05);
    border-left-color: var(--accent);
    color: var(--accent);
}

.nav-link-custom i {
    margin-right: 10px;
    width: 20px;
    text-align: center;
}

.main {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    height: 100%;
    /* Force 100% of parent, not viewport directly, to avoid overflow issues */
    position: relative;
    margin-left: 260px;
    /* PC Sidebar space */
    transition: margin-left 0.3s;
    overflow: hidden;
    /* El scroll lo tendra el chat-view */
}

@media (max-width: 991.98px) {
    .main {
        margin-left: 0 !important;
    }
}

.header {
    position: fixed !important;
    top: auto !important;
    /* Detach from top */
    bottom: 0 !important;
    /* Attach to bottom */
    right: 0;
    left: 260px;
    height: 60px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    border-top: 1px solid var(--border);
    /* Border top instead of bottom */
    border-bottom: none;
    background: var(--bg);
    z-index: 150000;
    transition: left 0.3s;
    box-shadow: 0 -4px 12px rgba(0, 0, 0, 0.4);
    /* Shadow upwards */
    will-change: left;
}

.header h5 {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 150px;
}

@media (max-width: 991.98px) {
    .header {
        left: 0 !important;
    }
}

.main-content-area {
    padding-top: 10px;
    /* Remove top padding since header is gone */
    padding-bottom: 200px;
    /* Increased padding for Bottom Bar + Header */
    height: 100%;
    /* Fix: Was 100vh which pushed content off screen */
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    /* Default for non-chat pages */
}

/* En el Chat, el scroll lo maneja Chat View especificamente */
body:has(#chat-flow) .main-content-area {
    overflow: hidden !important;
}

/* Chat Specifics */
#history-list {
    max-height: 55vh;
    overflow-y: auto;
    padding-right: 5px;
    margin-bottom: 20px;
}

#history-list::-webkit-scrollbar {
    width: 3px;
}

.chat-view {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
    /* Suavidad en iOS */
    padding-bottom: 220px !important;
    /* Ensure enough space for bottom elements */
    position: relative;
    scroll-behavior: auto;
    min-height: 0;
    height: 100%;
    overscroll-behavior-y: contain;
    overflow-anchor: none !important;
    /* ENABLE scroll anchoring to prevent jumps when ads load */
}

/* Anuncios */
.ad-slot-inline {
    width: 100%;
    margin: 25px 0;
    padding: 10px 0;
    border-top: 1px solid var(--border);
    border-bottom: 1px solid var(--border);
    background: rgba(127, 127, 127, 0.01);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    box-sizing: border-box;
    height: 140px;
    overflow: hidden;
    flex-shrink: 0;
    /* Pass scroll events through — never block the user */
    pointer-events: none;
    touch-action: pan-y;
}

.ad-slot-inline .ad-container {
    width: 100%;
    max-width: 100%;
    height: 100%;
    overflow: hidden !important;
    position: relative;
    background: transparent;
    display: block;
    text-align: center;
    /* Container also passes scroll, only the actual ad element is clickable */
    pointer-events: none;
    touch-action: pan-y;
}

/* Only the real AdSense element receives pointer events (so clicks work) */
.ad-slot-inline ins.adsbygoogle,
.ad-slot-inline .ad-label {
    pointer-events: auto;
    touch-action: pan-y;
}

@media (min-width: 992px) {
    .chat-view {
        padding: 30px 10%;
    }
}

.msg-box {
    margin-bottom: 35px;
    animation: fadeIn 0.3s ease;
    width: 100%;
}

.user-bubble {
    background: linear-gradient(135deg, #2a2a2a 0%, #1a1a1a 100%);
    padding: 15px 20px;
    border-radius: 18px 18px 2px 18px;
    width: 95%;
    margin-left: auto;
    border: 1px solid rgba(255, 255, 255, 0.05);
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
    position: relative;
}

.light-mode .user-bubble {
    background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
    border: 1px solid #dee2e6;
}

.msg-timestamp {
    font-size: 0.6rem;
    opacity: 0.4;
    margin-top: 5px;
    display: block;
    text-align: right;
}

/* Scrollbar Moderna */
::-webkit-scrollbar {
    width: 6px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: rgba(127, 127, 127, 0.2);
    border-radius: 10px;
}

::-webkit-scrollbar-thumb:hover {
    background: rgba(127, 127, 127, 0.4);
}

.msg-controls {
    position: absolute;
    top: 5px;
    right: 10px;
    display: flex;
    gap: 8px;
    opacity: 0.3;
    transition: 0.2s;
}

.msg-box:hover .msg-controls {
    opacity: 1;
}

.control-btn {
    background: transparent;
    border: none;
    color: var(--text);
    padding: 2px 5px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.85rem;
}

.control-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    color: var(--accent);
}

.ai-bubble {
    padding: 12px 18px;
    border-left: 4px solid var(--accent);
    background: rgba(255, 255, 255, 0.03);
    border-radius: 4px 18px 18px 18px;
    position: relative;
    width: 95%;
    margin-right: auto;
    border: 1px solid var(--border);
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}

@media (min-width: 992px) {

    .user-bubble,
    .ai-bubble {
        width: 85%;
        padding: 15px 20px;
    }
}

.ai-header {
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
    padding-bottom: 6px;
    margin-bottom: 10px;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.ai-content {
    line-height: 1.5;
    font-size: 0.95rem;
    word-wrap: break-word;
}

.ai-footer {
    margin-top: 12px;
    padding-top: 8px;
    border-top: 1px solid rgba(255, 255, 255, 0.08);
}

.edit-btn-trigger {
    opacity: 0.5;
    transition: 0.2s;
    font-weight: 600;
    text-transform: uppercase;
    font-size: 0.65rem;
    letter-spacing: 0.5px;
}

.msg-box:hover .edit-btn-trigger {
    opacity: 1;
    color: var(--accent) !important;
}

/* Modal Dark Adjustments */
.modal-content {
    background: var(--sidebar);
    border: 1px solid var(--border);
    color: var(--text);
}

.modal-header {
    border-bottom: 1px solid var(--border);
}

.modal-footer {
    border-top: 1px solid var(--border);
}

/* Estructura de Chat & Input */
.bottom-bar {
    position: fixed !important;
    bottom: 60px;
    /* Push up above the Header */
    left: 260px;
    right: 0;
    padding: 10px 40px 10px;
    /* Reduced bottom padding */
    background: transparent;
    z-index: 999;
}

@media (max-width: 991.98px) {
    .bottom-bar {
        left: 0;
        padding: 5px 10px 5px;
        bottom: 60px !important;
    }
}

.input-group-custom {
    max-width: 850px;
    margin: 0 auto;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 18px;
    padding: 8px;
    display: flex;
    align-items: flex-end;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.4);
}

@media (max-width: 576px) {
    .input-group-custom {
        border-radius: 12px;
        padding: 5px;
    }
}

/* CSS fix */

@media (max-width: 576px) {
    .main-content-area {
        padding: 10px 8px 10px !important;
    }

    .bottom-bar {
        padding: 5px 5px 15px !important;
    }

    .input-group-custom {
        border-radius: 12px !important;
        padding: 4px !important;
    }

    #main-input {
        font-size: 0.9rem !important;
        padding: 6px !important;
    }

    .btn-circle {
        width: 34px !important;
        height: 34px !important;
    }

    .ai-bubble,
    .user-bubble {
        max-width: 95% !important;
        margin-bottom: 20px !important;
    }

    /* Evitar que el sidebar rompa el layout si se queda a medias */
    body.sidebar-active {
        overflow: hidden;
    }

    .chat-view {
        padding-bottom: 280px !important;
    }
}

/* Overlay for Mobile Sidebar */
.sidebar-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.85);
    /* Mas oscuro para contraste */
    z-index: 240000 !important;
    /* Justo debajo del sidebar */
    backdrop-filter: blur(10px);
    display: none;
}

.sidebar-overlay.show {
    display: block !important;
    animation: fadeInOverlay 0.3s ease;
}

@keyframes fadeInOverlay {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

#search-bar {
    max-width: 850px;
    margin: 0 auto 10px;
    border: 1px solid var(--border) !important;
    border-radius: 12px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.4);
    animation: slideUp 0.3s ease;
}

@keyframes slideUp {
    from {
        transform: translateY(20px);
        opacity: 0;
    }

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

#main-input {
    background: transparent;
    border: none;
    color: var(--text);
    outline: none;
    flex-grow: 1;
    padding: 8px;
    resize: none;
    max-height: 150px;
}

.btn-circle {
    width: 38px;
    height: 38px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Auth Pages & Cards */
.auth-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
}

.auth-box {
    width: 100%;
    max-width: 400px;
    padding: 40px;
    background: var(--sidebar);
    border-radius: 20px;
    border: 1px solid var(--border);
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.3);
}

.form-control-dark {
    background: var(--bg);
    border: 1px solid var(--border);
    color: var(--text);
    padding: 12px;
    border-radius: 8px;
}

.form-control-dark:focus {
    background: var(--bg);
    color: var(--text);
    border-color: var(--accent);
    box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.2);
    outline: none;
}

/* Dashboard Cards */
.dash-card {
    background: var(--sidebar);
    border: 1px solid var(--border);
    border-radius: 15px;
    padding: 25px;
    height: 100%;
    transition: 0.3s;
}

.dash-card:hover {
    transform: translateY(-5px);
    border-color: var(--accent);
}

.stat-val {
    font-size: 2.5rem;
    font-weight: 800;
    color: var(--accent);
}

.stat-label {
    text-transform: uppercase;
    font-size: 0.8rem;
    letter-spacing: 1px;
    opacity: 0.7;
}

/* Animaciones */
@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(5px);
    }

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

/* Bootstrap Overrides */
.table {
    --bs-table-bg: transparent;
    --bs-table-color: var(--text);
    --bs-table-border-color: var(--border);
}

.btn-primary {
    background-color: var(--accent);
    border-color: var(--accent);
}

.btn-outline-primary {
    color: var(--accent);
    border-color: var(--accent);
}

.btn-outline-primary:hover {
    background-color: var(--accent);
    color: #fff;
}

/* Code Blocks & Copy Button */
.code-block-wrapper {
    position: relative;
    margin: 20px 0;
}

pre {
    background: #1e1e1e !important;
    padding: 1.2rem !important;
    border-radius: 12px;
    border: 1px solid var(--border);
    margin: 0 !important;
}

code {
    font-family: 'Fira Code', 'Consolas', monospace;
    font-size: 0.9rem;
}

.copy-btn {
    position: absolute;
    top: 10px;
    right: 10px;
    padding: 4px 10px;
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.1);
    color: #fff;
    border-radius: 6px;
    font-size: 0.75rem;
    cursor: pointer;
    transition: all 0.2s;
    backdrop-filter: blur(4px);
    opacity: 0;
}

.code-block-wrapper:hover .copy-btn {
    opacity: 1;
}

#chat-flow {
    max-width: 850px;
    margin: 0 auto;
    width: 100%;
}

/* Anuncios */


.ad-slot-inline .ad-label {
    font-size: 0.6rem;
    letter-spacing: 2px;
    opacity: 0.3;
    margin-bottom: 12px;
    font-weight: bold;
    text-transform: uppercase;
}

.ad-slot-inline .ad-container {
    width: 100%;
    max-width: 100%;
    min-height: 90px;
    max-height: 400px;
    overflow: hidden !important;
    position: relative;
    background: transparent;
    display: block;
    text-align: center;
}

/* Regla estricta para hijos de AdSense */
.ad-slot-inline .ad-container ins,
.ad-slot-inline .ad-container iframe {
    max-width: 100% !important;
    box-sizing: border-box !important;
    overflow: hidden !important;
}

/* Evitar que Google inyecte estilos que rompan el scroll */
.adsbygoogle {
    max-width: 100% !important;
    overflow: hidden !important;
    display: block !important;
}

@media (max-width: 768px) {
    .ad-slot-inline {
        margin: 20px 0;
        width: 100%;
    }
}

/* Boton Flotante de Scroll */
#quick-scroll-btn,
.floating-scroll-btn {
    position: fixed !important;
    bottom: 120px;
    right: 30px;
    width: 48px;
    height: 48px;
    background-color: var(--accent) !important;
    color: white !important;
    border-radius: 50% !important;
    display: flex !important;
    /* Forzar visibilidad */
    /* Se activa via JS */
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
    z-index: 200000 !important;
    border: 2px solid rgba(255, 255, 255, 0.4) !important;
    transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    opacity: 1 !important;
    visibility: visible !important;
}

/* Siempre visible */

@keyframes bounceIn {
    0% {
        transform: scale(0);
    }

    70% {
        transform: scale(1.1);
    }

    100% {
        transform: scale(1);
    }
}

#quick-scroll-btn:hover {
    transform: translateY(-15px);
    background-color: #2563eb !important;
}

@media (max-width: 991.98px) {
    #quick-scroll-btn {
        right: 20px;
        bottom: 155px;
        width: 45px;
        height: 45px;
        box-shadow: 0 5px 25px rgba(0, 0, 0, 0.7);
    }
}

/* AI Response Markdown Improvements */
.ai-content h1, .ai-content h2, .ai-content h3 {
    color: var(--accent);
    margin-top: 1.5rem;
    margin-bottom: 0.8rem;
    font-weight: 700;
}
.ai-content h1 { font-size: 1.5rem; border-bottom: 1px solid var(--border); padding-bottom: 0.5rem; }
.ai-content h2 { font-size: 1.3rem; }
.ai-content h3 { font-size: 1.1rem; }

.ai-content p { margin-bottom: 1.2rem; }

.ai-content ul, .ai-content ol {
    padding-left: 1.5rem;
    margin-bottom: 1.2rem;
}

.ai-content li { margin-bottom: 0.4rem; }

.ai-content code:not(pre code) {
    background: rgba(127, 127, 127, 0.15);
    color: var(--accent);
    padding: 0.2rem 0.4rem;
    border-radius: 4px;
    font-family: 'Fira Code', 'Consolas', monospace;
    font-size: 0.9em;
}

.ai-content blockquote {
    border-left: 4px solid var(--accent);
    padding: 0.8rem 1.2rem;
    background: rgba(127, 127, 127, 0.05);
    margin: 1.2rem 0;
    font-style: italic;
    color: var(--text);
    opacity: 0.9;
}

.ai-content table {
    width: 100%;
    border-collapse: collapse;
    margin: 1.5rem 0;
    font-size: 0.9rem;
    border-radius: 8px;
    overflow: hidden;
    border: 1px solid var(--border);
}

.ai-content th, .ai-content td {
    padding: 0.75rem 1rem;
    text-align: left;
    border: 1px solid var(--border);
}

.ai-content th {
    background: rgba(127, 127, 127, 0.1);
    font-weight: 700;
    color: var(--accent);
}

.ai-content tr:nth-child(even) {
    background: rgba(127, 127, 127, 0.03);
}

.ai-content hr {
    border: none;
    border-top: 1px solid var(--border);
    margin: 2rem 0;
    opacity: 0.5;
}

.ai-content img {
    max-width: 100%;
    border-radius: 12px;
    margin: 1rem 0;
    box-shadow: 0 4px 15px rgba(0,0,0,0.2);
}

/* Light mode adjustments for markdown */
.light-mode .ai-content code:not(pre code) {
    background: #f1f3f5;
    color: #0056b3;
}

.light-mode .ai-content blockquote {
    background: #f8f9fa;
}

.light-mode .ai-content th {
    background: #f1f3f5;
}

/* Botón expandir/colapsar mensajes largos */
.msg-expand-btn {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    margin-top: 10px;
    padding: 4px 14px;
    background: transparent;
    border: 1px solid var(--border);
    border-radius: 20px;
    color: var(--accent);
    font-size: 0.78rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s, border-color 0.2s;
    letter-spacing: 0.3px;
}

.msg-expand-btn:hover {
    background: rgba(59, 130, 246, 0.1);
    border-color: var(--accent);
}

.msg-expand-btn i {
    font-size: 0.8rem;
    transition: transform 0.25s ease;
}