*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    height: 100%;
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

body {
    font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    background: radial-gradient(circle at top, #2b2b2b 0, #050505 55%);
    color: #f9f9f9;
    min-height: 100vh;
    min-height: 100dvh;
    min-height: -webkit-fill-available;
    height: 100%;
    overflow: hidden;
    overflow-x: hidden;
}

.app-root {
    display: flex;
    min-height: 100vh;
    min-height: 100dvh;
    min-height: -webkit-fill-available;
    height: 100%;
    max-width: 100vw;
    position: relative;
}

.sidebar-backdrop {
    display: none;
}

.mobile-header {
    display: none;
}

.sidebar {
    width: 260px;
    flex-shrink: 0;
    background: #020202;
    color: #fff;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    padding: 24px 16px;
    border-right: 1px solid #202020;
}

.sidebar-logo img {
    max-width: 180px;
}

.sidebar-convos {
    flex: 1;
    margin-top: 40px;
    overflow-y: auto;
}

.sidebar-heading {
    font-size: 13px;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: #a3a3a3;
    margin-bottom: 12px;
}

#conversation-list {
    list-style: none;
}

#conversation-list li {
    padding: 10px 8px;
    border-radius: 8px;
    font-size: 14px;
    color: #e5e5e5;
    cursor: pointer;
    transition: background 0.15s ease;
}

#conversation-list li:hover,
#conversation-list li.active {
    background: #1f1f1f;
}

.new-chat-btn {
    margin-top: 16px;
    background: #ffd900;
    color: #000;
    border: none;
    border-radius: 6px;
    padding: 10px 14px;
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 600;
    cursor: pointer;
    box-shadow: 0 0 0 1px #ffd90022, 0 0 18px #ffd90033;
}

.new-chat-btn .plus {
    font-size: 18px;
}

.chat-main {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    position: relative;
    padding: 40px 60px 24px;
    padding-bottom: max(24px, env(safe-area-inset-bottom, 0px));
}

.chat-hero {
    text-align: center;
    margin-top: 40px;
    margin-bottom: 40px;
}

.chat-hero h1 {
    font-size: 40px;
    font-weight: 700;
    margin-bottom: 10px;
}

.chat-hero p {
    font-size: 18px;
    color: #d2d2d2;
}

.messages {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    padding: 0 80px;
}

.message-row {
    display: flex;
    margin-bottom: 18px;
}

.message-row.user {
    justify-content: flex-end;
}

.message-row.assistant {
    justify-content: flex-start;
}

.message-bubble {
    max-width: 80%;
    padding: 12px 16px;
    border-radius: 18px;
    font-size: 15px;
    line-height: 1.5;
    position: relative;
    word-wrap: break-word;
}

.citation {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: #ffd90022;
    color: #ffd900;
    border: 1px solid #ffd90044;
    border-radius: 4px;
    padding: 0 4px;
    margin-left: 2px;
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
    vertical-align: middle;
}

.citation:hover {
    background: #ffd900;
    color: #000;
}

.message-row.user .message-bubble {
    background: #181818;
    color: #f5f5f5;
    border-radius: 18px 18px 4px 18px;
}

.message-row.assistant .message-bubble {
    background: transparent;
    color: #f5f5f5;
}

.message-bubble a {
    color: #ffd900;
    text-decoration: underline;
    word-break: break-all;
}

.sources-accordion {
    margin-top: 12px;
    background: #0d0d0d;
    border-radius: 12px;
    border: 1px solid #1f1f1f;
    overflow: hidden;
    transition: all 0.2s ease;
}

.sources-accordion.open {
    border-color: #ffd90033;
    background: #111111;
}

.sources-toggle {
    width: 100%;
    background: transparent;
    border: none;
    color: #e5e5e5;
    font-size: 13px;
    font-weight: 500;
    padding: 10px 14px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    cursor: pointer;
    transition: background 0.2s ease;
}

.sources-toggle:hover {
    background: #181818;
    color: #ffd900;
}

.sources-toggle .chevron {
    font-size: 14px;
    color: #666;
    transition: transform 0.2s ease, color 0.2s ease;
}

.sources-toggle:hover .chevron {
    color: #ffd900;
}

.sources-accordion.open .sources-toggle .chevron {
    transform: rotate(180deg);
}

.sources-list {
    display: none;
    padding: 4px 14px 12px;
    border-top: 1px solid #1f1f1f;
    animation: slideDown 0.2s ease;
}

@keyframes slideDown {
    from {
        opacity: 0;
        transform: translateY(-5px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.sources-accordion.open .sources-list {
    display: block;
}

.source-item {
    font-size: 12px;
    margin-bottom: 4px;
}

.typing-bubble {
    background: #181818;
    border-radius: 18px 18px 18px 4px;
}

.typing-indicator {
    display: flex;
    align-items: center;
    gap: 4px;
}

.typing-indicator span {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background-color: #ffd900;
    display: inline-block;
    animation: pulse 1.2s infinite ease-in-out;
}

.typing-indicator span:nth-child(2) {
    animation-delay: 0.15s;
}

.typing-indicator span:nth-child(3) {
    animation-delay: 0.3s;
}

@keyframes pulse {

    0%,
    80%,
    100% {
        transform: scale(0.6);
        opacity: 0.4;
    }

    40% {
        transform: scale(1);
        opacity: 1;
    }
}

.chat-input-wrapper {
    flex-shrink: 0;
    padding: 18px 80px 0;
    padding-bottom: max(0px, env(safe-area-inset-bottom, 0px));
}

.chat-input-inner {
    display: flex;
    align-items: flex-end;
    /* Align to bottom as it grows */
    background: #181818;
    border-radius: 999px;
    padding: 10px 10px 10px 20px;
    box-shadow: 0 0 0 1px #ffd90055, 0 0 25px #ffd90022;
    transition: border-radius 0.2s ease, min-height 0.2s ease;
    min-height: 64px;
}

.chat-input-inner textarea {
    flex: 1;
    background: transparent;
    border: none;
    resize: none;
    color: #f9f9f9;
    font-size: 15px;
    outline: none;
    line-height: 1.5;
    max-height: 120px;
    /* Approximately 4 lines of 22.5px + padding */
    overflow-y: hidden;
    padding: 12px 0;
    transition: height 0.1s ease;
    scrollbar-width: thin;
    scrollbar-color: #444 transparent;
}

.chat-input-inner textarea::-webkit-scrollbar {
    width: 6px;
}
.chat-input-inner textarea::-webkit-scrollbar-track {
    background: transparent;
}
.chat-input-inner textarea::-webkit-scrollbar-thumb {
    background: #444;
    border-radius: 3px;
}

.send-btn {
    flex-shrink: 0;
    touch-action: manipulation;
    width: 44px;
    height: 44px;
    background: #ffd900;
    border-radius: 999px;
    border: none;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: 0 0 0 1px #ffd90022, 0 0 18px #ffd90055;
}

.send-icon {
    font-size: 18px;
    color: #000;
}

.chat-footer-note {
    margin-top: 10px;
    font-size: 12px;
    color: #9d9d9d;
    text-align: center;
}

.sources {
    margin-top: 8px;
    font-size: 12px;
    color: #c5c5c5;
}

/* Base modal styles retained for iframe linking behavior */
.modal {
    display: none;
    position: fixed;
    z-index: 1000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    background-color: rgba(0, 0, 0, 0.8);
}

.modal-content {
    background-color: #1a1a1a;
    margin: 5% auto;
    padding: 20px;
    border: 1px solid #333;
    border-radius: 8px;
    width: 80%;
    height: 80%;
    position: relative;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

.close-btn {
    color: #aaa;
    position: absolute;
    top: 10px;
    right: 20px;
    font-size: 28px;
    font-weight: bold;
    cursor: pointer;
}

.close-btn:hover,
.close-btn:focus {
    color: #fff;
    text-decoration: none;
}

@media (max-width: 960px) {
    .sidebar-backdrop {
        display: none;
        position: fixed;
        inset: 0;
        z-index: 150;
        background: rgba(0, 0, 0, 0.55);
        -webkit-tap-highlight-color: transparent;
    }

    .app-root.sidebar-open .sidebar-backdrop {
        display: block;
    }

    .mobile-header {
        display: flex;
        align-items: center;
        gap: 12px;
        flex-shrink: 0;
        padding: 12px max(16px, env(safe-area-inset-right, 0px)) 12px max(16px, env(safe-area-inset-left, 0px));
        padding-top: max(12px, env(safe-area-inset-top, 0px));
        border-bottom: 1px solid #202020;
        background: #050505;
    }

    .mobile-header-title {
        font-weight: 700;
        font-size: 17px;
        letter-spacing: 0.02em;
    }

    .sidebar-toggle {
        flex-shrink: 0;
        width: 44px;
        height: 44px;
        display: flex;
        align-items: center;
        justify-content: center;
        border: 1px solid #333;
        border-radius: 10px;
        background: #141414;
        color: #f9f9f9;
        cursor: pointer;
        touch-action: manipulation;
        -webkit-tap-highlight-color: transparent;
    }

    .sidebar-toggle:focus-visible {
        outline: 2px solid #ffd900;
        outline-offset: 2px;
    }

    .hamburger {
        display: block;
        width: 18px;
        height: 2px;
        background: currentColor;
        border-radius: 1px;
        box-shadow: 0 -6px 0 currentColor, 0 6px 0 currentColor;
    }

    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        bottom: 0;
        width: min(288px, 88vw);
        max-width: 100%;
        z-index: 200;
        transform: translateX(-102%);
        transition: transform 0.22s ease, box-shadow 0.22s ease;
        padding-top: max(20px, env(safe-area-inset-top, 0px));
        padding-bottom: max(16px, env(safe-area-inset-bottom, 0px));
        padding-left: max(16px, env(safe-area-inset-left, 0px));
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    .app-root.sidebar-open .sidebar {
        transform: translateX(0);
        box-shadow: 12px 0 40px rgba(0, 0, 0, 0.55);
    }

    .chat-main {
        padding: 0;
        padding-bottom: max(12px, env(safe-area-inset-bottom, 0px));
    }

    .chat-hero {
        margin-top: 16px;
        margin-bottom: 20px;
        padding-left: max(16px, env(safe-area-inset-left, 0px));
        padding-right: max(16px, env(safe-area-inset-right, 0px));
    }

    .chat-hero h1 {
        font-size: clamp(1.35rem, 5.5vw, 2rem);
        line-height: 1.2;
        padding: 0 4px;
    }

    .chat-hero p {
        font-size: clamp(0.9rem, 3.8vw, 1rem);
        padding: 0 6px;
    }

    .messages {
        padding-left: max(16px, env(safe-area-inset-left, 0px));
        padding-right: max(16px, env(safe-area-inset-right, 0px));
    }

    .chat-input-wrapper {
        padding-left: max(16px, env(safe-area-inset-left, 0px));
        padding-right: max(16px, env(safe-area-inset-right, 0px));
        padding-top: 12px;
    }

    .message-bubble {
        max-width: 92%;
        font-size: 15px;
    }

    .chat-input-inner {
        min-height: 52px;
        padding: 8px 10px 8px 14px;
        border-radius: 22px;
    }

    .chat-input-inner textarea {
        font-size: 16px;
        padding: 10px 0;
    }

    .new-chat-btn {
        touch-action: manipulation;
        min-height: 44px;
    }
}

@media (max-width: 480px) {
    .chat-hero h1 {
        font-size: 1.35rem;
    }

    .send-btn {
        width: 40px;
        height: 40px;
    }

    .chat-footer-note {
        font-size: 11px;
        padding: 0 4px;
    }
}

/* Deletion UI Styles */
#conversation-list li {
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: relative;
    padding-right: 35px;
    /* Space for delete button */
}

.convo-title {
    flex: 1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.delete-convo-btn {
    position: absolute;
    right: 8px;
    opacity: 0.6;
    /* Subtly visible by default */
    background: #2a2a2a;
    border: none;
    color: #ffd900;
    cursor: pointer;
    font-size: 14px;
    width: 28px;
    height: 28px;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
    line-height: 1;
}

#conversation-list li:hover .delete-convo-btn,
#conversation-list li.active .delete-convo-btn {
    opacity: 1;
    background: #333;
}

.delete-convo-btn:hover {
    background: #ff4d4d !important;
    color: #fff !important;
}

.delete-modal-content {
    width: 400px;
    height: auto;
    margin: 15% auto;
    text-align: center;
}

.delete-modal-content h2 {
    margin-bottom: 16px;
}

.delete-modal-content p {
    color: #a3a3a3;
    margin-bottom: 24px;
}

.modal-actions {
    display: flex;
    justify-content: center;
    gap: 12px;
}

.btn-secondary {
    background: #333;
    color: #fff;
    border: none;
    padding: 10px 20px;
    border-radius: 6px;
    cursor: pointer;
    font-weight: 500;
}

.btn-secondary:hover {
    background: #444;
}

.btn-danger {
    background: #ff4d4d;
    color: #fff;
    border: none;
    padding: 10px 20px;
    border-radius: 6px;
    cursor: pointer;
    font-weight: 500;
}

.btn-danger:hover {
    background: #ff3333;
}

@media (max-width: 960px) {
    body:has(.app-root.sidebar-open) {
        overflow: hidden;
    }
}

@media (max-width: 640px) {
    .modal-content {
        width: calc(100vw - 24px);
        max-width: 100%;
        height: min(85dvh, calc(100dvh - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px) - 32px));
        margin: max(16px, env(safe-area-inset-top, 0px)) auto auto;
        padding: max(44px, env(safe-area-inset-top, 0px)) 16px 16px;
    }

    .close-btn {
        top: max(10px, env(safe-area-inset-top, 0px));
        right: max(16px, env(safe-area-inset-right, 0px));
    }

    .delete-modal-content {
        width: min(calc(100vw - 32px), 400px);
        margin: 25vh auto auto;
        padding-left: max(20px, env(safe-area-inset-left, 0px));
        padding-right: max(20px, env(safe-area-inset-right, 0px));
        padding-bottom: max(20px, env(safe-area-inset-bottom, 0px));
    }

    .modal-actions {
        flex-wrap: wrap;
    }

    .btn-secondary,
    .btn-danger {
        min-height: 44px;
        touch-action: manipulation;
    }
}