* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display", "Helvetica Neue", Arial, sans-serif;
    -webkit-tap-highlight-color: transparent;
}

:root {
    color-scheme: dark;
    overscroll-behavior: none;
    --bg: #000;
    --blue: #0a84ff;
    --gray-bubble: #262628;
    --text: #fff;
    --secondary: #98989f;
    --separator: rgba(255,255,255,.10);
    --ui-font-size: 14px;
    --sidebar-collapsed: 56px;
    --sidebar-expanded: 236px;
    --chat-max: 760px;
    --sb-w: var(--sidebar-collapsed);
}

body.sb-expanded { --sb-w: var(--sidebar-expanded); }

html, body {
    width: 100%;
    height: 100%;
    background: #000;
    color: var(--text);
    overflow: hidden;
    font-size: var(--ui-font-size);
}

button { cursor: pointer; font: inherit; }
button:disabled { cursor: default; }

:focus-visible {
    outline: 2px solid var(--blue);
    outline-offset: 1px;
}

/* ---------- SIDEBAR ---------- */
.app-sidebar {
    position: fixed;
    z-index: 50;
    inset: 0 auto 0 0;
    width: var(--sb-w);
    display: flex;
    flex-direction: column;
    padding: calc(12px + env(safe-area-inset-top)) 10px calc(12px + env(safe-area-inset-bottom));
    background: linear-gradient(180deg, #131316 0%, #0e0e10 100%);
    border-right: 1px solid rgba(255,255,255,.07);
    overflow: hidden;
    transition: width .18s ease;
}

.sidebar-top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 0 2px 14px;
}

.brand {
    min-width: 0;
    display: flex;
    align-items: center;
    gap: 10px;
    padding-left: 4px;
}

.brand-mark {
    width: 28px;
    height: 28px;
    flex: 0 0 28px;
    display: grid;
    place-items: center;
    border-radius: 50%;
    background: radial-gradient(circle at 30% 25%, #7ad7ff 0%, #0a84ff 45%, #5e5ce6 100%);
    color: #fff;
    font-size: 14px;
    font-weight: 700;
    box-shadow: 0 0 0 1px rgba(255,255,255,.12) inset, 0 4px 14px rgba(10,132,255,.35);
}

.brand-name {
    font-size: 16px;
    font-weight: 600;
    letter-spacing: -.2px;
}

.sidebar-toggle {
    width: 36px;
    height: 36px;
    flex: 0 0 36px;
    display: grid;
    place-items: center;
    border: 0;
    border-radius: 10px;
    background: transparent;
    color: #98989f;
}

.sidebar-toggle:hover { background: rgba(255,255,255,.08); color: #fff; }

.sidebar-item {
    width: 100%;
    min-height: 40px;
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 0 11px;
    border: 0;
    border-radius: 11px;
    background: transparent;
    color: #b9b9bf;
    font-size: var(--ui-font-size);
    text-align: left;
    transition: background .12s ease, color .12s ease;
}

.sidebar-item:hover:not(:disabled) {
    background: rgba(255,255,255,.07);
    color: #fff;
}

.sidebar-item.active {
    background: rgba(10,132,255,.16);
    color: #fff;
}

.sidebar-item.disabled { opacity: .4; }

.sidebar-item small {
    margin-left: auto;
    padding: 2px 7px;
    border-radius: 99px;
    background: rgba(255,255,255,.07);
    color: #8e8e93;
    font-size: 10px;
}

.sidebar-nav {
    display: flex;
    flex-direction: column;
    gap: 3px;
}

.sidebar-spacer { flex: 1; }

.sidebar-bottom {
    padding-top: 10px;
    border-top: 1px solid rgba(255,255,255,.07);
}

.sidebar-icon {
    width: 20px;
    height: 20px;
    flex: 0 0 20px;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.7;
    stroke-linecap: round;
    stroke-linejoin: round;
    color: #8e8e93;
}

.sidebar-item.active .sidebar-icon { color: #4da3ff; }
.sidebar-item:hover:not(:disabled) .sidebar-icon { color: #fff; }

.sidebar-label {
    overflow: hidden;
    white-space: nowrap;
}

/* profile chip */
.profile-chip { min-height: 48px; padding: 0 8px; }

.avatar {
    width: 30px;
    height: 30px;
    flex: 0 0 30px;
    display: grid;
    place-items: center;
    border-radius: 50%;
    background: #2c2c30;
    color: #fff;
    font-size: 13px;
    font-weight: 600;
    text-transform: uppercase;
}

.avatar svg {
    width: 16px;
    height: 16px;
    fill: none;
    stroke: #98989f;
    stroke-width: 1.8;
    stroke-linecap: round;
}

.profile-chip.active .avatar { background: var(--blue); }

.chip-text {
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 1px;
}

.chip-name {
    overflow: hidden;
    text-overflow: ellipsis;
    color: #fff;
    font-weight: 600;
    line-height: 1.2;
}

.chip-sub {
    color: #77777d;
    font-size: 11px;
    line-height: 1.2;
}

/* collapsed rail (desktop only) */
@media (min-width: 701px) {
    body:not(.sb-expanded) .sidebar-label,
    body:not(.sb-expanded) .brand {
        display: none;
    }

    body:not(.sb-expanded) .sidebar-top { justify-content: center; }

    body:not(.sb-expanded) .sidebar-item {
        justify-content: center;
        padding: 0;
        gap: 0;
    }
}

.sidebar-backdrop {
    display: none;
}

/* ---------- SHELL ---------- */
.app-shell {
    position: fixed;
    top: env(safe-area-inset-top);
    right: 0;
    bottom: 0;
    left: var(--sb-w);
    transition: left .18s ease;
}

/* keyboard open: pin the app to the visual viewport so the chat resizes
   instead of the page being pushed up */
html.kb-open .app-shell {
    top: calc(env(safe-area-inset-top) + var(--vv-t, 0px));
    bottom: auto;
    height: calc(var(--vv-h, 100svh) - env(safe-area-inset-top));
}

/* ---------- CHAT ---------- */
.chat-container {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    background: #000;
}

/* every chat piece shares the same centered column */
.chat-header,
.chat-messages,
.input-area {
    padding-inline: max(14px, calc((100% - var(--chat-max)) / 2));
}

.chat-header {
    height: 54px;
    flex: 0 0 54px;
    display: flex;
    align-items: center;
    gap: 10px;
    border-bottom: 1px solid var(--separator);
    background: rgba(0,0,0,.88);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    z-index: 2;
}

.chat-title {
    font-size: var(--ui-font-size);
    line-height: 18px;
    font-weight: 600;
}

.chat-subtitle {
    margin-top: 1px;
    color: #8e8e93;
    font-size: 11px;
    line-height: 12px;
}

.menu-btn {
    display: none;
    width: 34px;
    height: 34px;
    flex: 0 0 34px;
    place-items: center;
    margin-left: -6px;
    border: 0;
    border-radius: 9px;
    background: transparent;
    color: #c7c7cc;
}

.menu-btn:active { background: rgba(255,255,255,.10); }

.menu-btn svg {
    width: 22px;
    height: 22px;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.8;
    stroke-linecap: round;
}

.chat-messages {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding-top: 12px;
    padding-bottom: 12px;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
}

.chat-messages::-webkit-scrollbar { display: none; }

.message {
    display: flex;
    width: 100%;
    margin: 2px 0;
    animation: messageIn .16s ease-out;
}

.message.bot { justify-content: flex-start; }
.message.user { justify-content: flex-end; }

/* pin the conversation to the bottom when it is shorter than the screen */
.chat-messages > :first-child { margin-top: auto; }

.time-divider {
    width: 100%;
    text-align: center;
    font-size: 12px;
    color: var(--secondary);
    margin: 14px 0 6px;
    flex-shrink: 0;
}

@keyframes messageIn {
    from { opacity: 0; transform: translateY(3px) scale(.99); }
    to { opacity: 1; transform: translateY(0) scale(1); }
}

.message-bubble {
    max-width: min(78%, 520px);
    min-height: 34px;
    padding: 7px 12px 8px;
    border-radius: 18px;
    font-size: var(--ui-font-size);
    line-height: 1.35;
    letter-spacing: -.05px;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
}

.message.bot .message-bubble { color: #fff; background: var(--gray-bubble); }
.message.user .message-bubble { color: #fff; background: var(--blue); }

.message.bot + .message.bot,
.message.user + .message.user { margin-top: 1px; }

.message.bot + .message.bot .message-bubble { border-top-left-radius: 5px; }
.message.bot:not(:has(+ .message.bot)) .message-bubble { border-bottom-left-radius: 18px; }
.message.bot:has(+ .message.bot) .message-bubble { border-bottom-left-radius: 5px; }

.message.user + .message.user .message-bubble { border-top-right-radius: 5px; }
.message.user:has(+ .message.user) .message-bubble { border-bottom-right-radius: 5px; }
.message.user:not(:has(+ .message.user)) .message-bubble { border-bottom-right-radius: 18px; }

/* typing bubble: created/removed by JS, never a chat message */
.message.typing-row { pointer-events: none; }

.typing-bubble {
    min-width: 52px;
    min-height: 34px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
    padding: 7px 11px 8px;
}

.typing-dot {
    width: 5px;
    height: 5px;
    border-radius: 50%;
    background: #a9a9ae;
    animation: typingBounce 1.15s infinite ease-in-out;
}

.typing-dot:nth-child(2) { animation-delay: .14s; }
.typing-dot:nth-child(3) { animation-delay: .28s; }

@keyframes typingBounce {
    0%, 60%, 100% { transform: translateY(0); opacity: .55; }
    30% { transform: translateY(-3px); opacity: 1; }
}

.input-area {
    flex: 0 0 auto;
    padding-top: 8px;
    padding-bottom: calc(16px + env(safe-area-inset-bottom));
    background: #000;
}

.input-container {
    width: 100%;
    min-height: 44px;
    display: flex;
    align-items: center;
    gap: 2px;
    padding: 4px 6px 4px 10px;
    border: 1px solid #3a3a3e;
    border-radius: 14px;
    background: #1c1c1f;
    transition: border-color .15s ease;
}

.input-container:focus-within { border-color: rgba(10,132,255,.65); }

.input-container input {
    flex: 1;
    min-width: 0;
    height: 34px;
    border: 0;
    outline: 0;
    background: transparent;
    color: #fff;
    padding: 0 6px;
    font-size: var(--ui-font-size);
    letter-spacing: -.05px;
}

.input-container input::placeholder { color: #8e8e93; }

.input-icon {
    flex: 0 0 32px;
    width: 32px;
    height: 32px;
    display: grid;
    place-items: center;
    border: 0;
    background: transparent;
    border-radius: 9px;
    color: #8e8e93;
}

.input-icon:hover { color: #fff; }
.input-icon:active { background: rgba(255,255,255,.10); }

.input-icon svg {
    width: 20px;
    height: 20px;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.8;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.send-btn {
    display: none;
    flex: 0 0 32px;
    width: 32px;
    height: 32px;
    place-items: center;
    border: 0;
    border-radius: 9px;
    background: var(--blue);
    color: #fff;
}

.send-btn:active { transform: scale(.94); }

.send-btn svg {
    width: 20px;
    height: 20px;
    fill: none;
    stroke: currentColor;
    stroke-width: 2.4;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.input-container.has-text .send-btn { display: grid; }
.input-container.has-text #mic-btn { display: none; }

/* ---------- REPLIES ---------- */
.reply-bar {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 6px;
    padding: 6px 6px 6px 10px;
    border-left: 3px solid var(--blue);
    border-radius: 8px;
    background: #1c1c1f;
}

.reply-bar[hidden] { display: none; }

.reply-bar-text {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 1px;
    font-size: 12px;
}

.reply-bar-text b { color: #4da3ff; font-weight: 600; }

.reply-bar-text span {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    color: #a8a8ae;
}

.reply-bar button {
    width: 26px;
    height: 26px;
    border: 0;
    border-radius: 7px;
    background: transparent;
    color: #8e8e93;
    font-size: 20px;
    line-height: 1;
}

.reply-bar button:hover { color: #fff; background: rgba(255,255,255,.08); }

.reply-quote {
    display: block;
    margin: 0 0 5px;
    padding: 4px 8px;
    border-left: 3px solid rgba(255,255,255,.55);
    border-radius: 6px;
    background: rgba(0,0,0,.2);
    font-size: 12px;
    line-height: 1.3;
    cursor: pointer;
}

.reply-quote .rq-who { display: block; font-weight: 600; opacity: .9; }

.reply-quote .rq-text {
    display: block;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    opacity: .75;
}

.message.flash .message-bubble { animation: flash 1.1s ease; }

@keyframes flash {
    0%, 100% { filter: none; }
    30% { filter: brightness(1.6); }
}

.msg-menu {
    position: fixed;
    z-index: 80;
    min-width: 150px;
    display: flex;
    flex-direction: column;
    padding: 5px;
    border: 1px solid #3a3a3e;
    border-radius: 12px;
    background: #222226;
    box-shadow: 0 10px 30px rgba(0,0,0,.6);
}

.msg-menu[hidden] { display: none; }

.msg-menu button {
    min-height: 38px;
    padding: 0 12px;
    border: 0;
    border-radius: 8px;
    background: transparent;
    color: #fff;
    font-size: var(--ui-font-size);
    text-align: left;
}

.msg-menu button:hover { background: rgba(255,255,255,.08); }

/* long-press should open our menu, not the native text-selection callout */
@media (pointer: coarse) {
    .message-bubble {
        -webkit-user-select: none;
        user-select: none;
        -webkit-touch-callout: none;
    }
}

/* ---------- FILE / PROFILE VIEW ---------- */
.file-view {
    position: absolute;
    inset: 0;
    display: none;
    flex-direction: column;
    background: #0b0b0d;
}

.file-view.open { display: flex; }

/* ---------- MOBILE ---------- */
@media (max-width: 700px) {
    :root { --sb-w: 0px; --ui-font-size: 16px; }
    body.sb-expanded { --sb-w: 0px; }

    .app-sidebar {
        width: 100%;
        transform: translateX(-100%);
        transition: transform .2s ease;
        box-shadow: none;
    }

    body.sb-drawer-open .app-sidebar {
        transform: none;
        box-shadow: 8px 0 30px rgba(0,0,0,.6);
    }

    .sidebar-backdrop {
        display: block;
        position: fixed;
        inset: 0;
        z-index: 40;
        background: rgba(0,0,0,.5);
        opacity: 0;
        pointer-events: none;
        transition: opacity .2s ease;
    }

    body.sb-drawer-open .sidebar-backdrop {
        opacity: 1;
        pointer-events: auto;
    }

    .menu-btn { display: grid; }

    .message-bubble { max-width: 84%; }

    /* tight above the home indicator: the full inset looks floaty */
    .input-area { padding-bottom: max(10px, calc(env(safe-area-inset-bottom) - 22px)); }
    /* while the keyboard is open the home indicator sits behind it, so no inset needed */
    html.kb-open .input-area { padding-bottom: 0; }
}

@media (prefers-reduced-motion: reduce) {
    .app-sidebar,
    .app-shell,
    .sidebar-backdrop { transition: none; }

    .message { animation: none; }
}
