:root {
    color-scheme: dark;
    --bg: #070708;
    --sidebar: #0c0c0e;
    --panel: #111114;
    --panel-2: #17171b;
    --panel-3: #1f1f25;
    --border: #2a2a31;
    --border-soft: rgba(255, 255, 255, 0.075);
    --text: #f4f4f6;
    --muted: #a8a6b0;
    --faint: #77757f;
    --accent: #ff3030;
    --accent-2: #ff4a4a;
    --accent-soft: rgba(255, 48, 48, 0.11);
    --code: #050506;
    --green: #a7dc7a;
    --sidebar-width: 260px;
    --topbar-height: 52px;
    --composer-max: 768px;
    --message-max: 768px;
}

* {
    box-sizing: border-box;
    scrollbar-width: thin;
    scrollbar-color: #4a4a54 transparent;
}

*::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

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

*::-webkit-scrollbar-thumb {
    background: #44444d;
    border-radius: 999px;
}

html,
body {
    height: 100%;
}

body {
    margin: 0;
    overflow: hidden;
    background:
        radial-gradient(circle at 28% 32%, rgba(255, 48, 48, 0.1), transparent 32%),
        var(--bg);
    color: var(--text);
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

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

button {
    border: 0;
    cursor: pointer;
}

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

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* ── App shell ── */

.app-shell {
    display: grid;
    grid-template-columns: var(--sidebar-width) minmax(0, 1fr);
    height: 100vh;
    min-height: 0;
    transition: grid-template-columns 220ms ease;
}

.nav-hidden .app-shell {
    grid-template-columns: 0 minmax(0, 1fr);
}

.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.55);
    z-index: 40;
    opacity: 0;
    pointer-events: none;
    transition: opacity 220ms ease;
}

.sidebar-overlay.visible {
    opacity: 1;
    pointer-events: auto;
}

/* ── Sidebar ── */

.sidebar {
    position: relative;
    z-index: 50;
    min-width: 0;
    height: 100vh;
    height: 100dvh;
    border-right: 1px solid var(--border);
    background: var(--sidebar);
    padding: 12px;
    display: grid;
    grid-template-rows: auto auto minmax(0, 1fr) auto;
    gap: 10px;
    overflow: hidden;
    transition: opacity 180ms ease, transform 220ms ease;
}

.nav-hidden .sidebar {
    pointer-events: none;
    opacity: 0;
    transform: translateX(-16px);
}

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

.sidebar-close {
    display: none;
}

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

.brand-mark {
    display: grid;
    place-items: center;
    width: 28px;
    height: 28px;
    border-radius: 8px;
    background: linear-gradient(135deg, #ff2020, #ff4a4a);
    color: #fff;
    font-weight: 900;
    font-size: 13px;
    flex-shrink: 0;
}

.brand strong {
    display: block;
    color: #ff3b3b;
    font-size: 14px;
    font-weight: 700;
}

.brand span,
.hint,
.history-item small,
.lab-profile span,
.composer-disclaimer {
    color: var(--muted);
    font-size: 12px;
}

.new-chat-btn {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    min-height: 40px;
    padding: 0 14px;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--panel);
    color: var(--text);
    font-size: 14px;
    font-weight: 600;
    transition: background 150ms ease, border-color 150ms ease;
}

.new-chat-btn:hover {
    background: var(--panel-2);
    border-color: rgba(255, 48, 48, 0.4);
}

.new-chat-btn i {
    font-size: 15px;
    color: var(--accent);
}

.history-section {
    min-height: 0;
    display: grid;
    grid-template-rows: auto minmax(0, 1fr);
    gap: 6px;
    overflow: hidden;
}

.history-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 4px;
    color: var(--faint);
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.history-head span {
    display: flex;
    align-items: center;
    gap: 6px;
}

.history-list {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.history-item {
    width: 100%;
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 9px 10px;
    border: 1px solid transparent;
    border-radius: 8px;
    background: transparent;
    color: var(--muted);
    text-align: left;
    transition: background 150ms ease, color 150ms ease;
}

.history-item:hover,
.history-item.active {
    color: var(--text);
    background: var(--accent-soft);
    border-color: rgba(255, 48, 48, 0.2);
}

.history-item i {
    font-size: 14px;
    flex-shrink: 0;
    opacity: 0.7;
}

.history-item-body {
    min-width: 0;
    flex: 1;
}

.history-item span {
    display: block;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 13px;
    font-weight: 500;
}

.history-item small {
    display: block;
    margin-top: 1px;
    font-size: 11px;
}

.history-empty {
    color: var(--muted);
    font-size: 13px;
    padding: 12px 8px;
    text-align: center;
}

.sidebar-bottom {
    flex-shrink: 0;
    display: grid;
    gap: 4px;
    border-top: 1px solid var(--border);
    padding-top: 8px;
    background: var(--sidebar);
}

.lab-settings {
    border-top: 0;
    padding-top: 0;
}

.lab-settings summary {
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    color: var(--muted);
    font-size: 13px;
    font-weight: 500;
    list-style: none;
    padding: 6px 4px;
    border-radius: 8px;
    transition: background 150ms ease;
}

.lab-settings summary::-webkit-details-marker {
    display: none;
}

.lab-settings summary:hover {
    background: var(--panel);
    color: var(--text);
}

.field {
    display: grid;
    gap: 6px;
    margin-top: 8px;
    padding: 0 4px;
}

label {
    color: var(--muted);
    font-size: 11px;
    font-weight: 600;
}

input,
select,
textarea {
    width: 100%;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--panel);
    color: var(--text);
    outline: none;
}

input,
select {
    height: 36px;
    padding: 0 12px;
}

textarea {
    padding: 12px;
    line-height: 1.5;
    resize: none;
}

input:focus,
select:focus,
textarea:focus {
    border-color: rgba(255, 48, 48, 0.68);
    box-shadow: 0 0 0 3px rgba(255, 48, 48, 0.1);
}

.sidebar-footer {
    padding-top: 0;
}

.sidebar-footer-link {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 10px;
    border-radius: 8px;
    color: var(--muted);
    font-size: 13px;
    text-decoration: none;
    transition: background 150ms ease, color 150ms ease;
}

.sidebar-footer-link:hover {
    background: var(--panel);
    color: var(--text);
}

/* ── Buttons ── */

.primary-btn,
.secondary-btn,
.ghost-btn {
    min-height: 40px;
    padding: 0 16px;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--panel);
    color: var(--text);
    font-weight: 600;
}

.primary-btn:hover,
.secondary-btn:hover {
    border-color: rgba(255, 48, 48, 0.52);
}

.icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border: 1px solid transparent;
    border-radius: 8px;
    background: transparent;
    color: var(--muted);
    font-size: 17px;
    flex-shrink: 0;
    transition: background 150ms ease, color 150ms ease, border-color 150ms ease;
}

.icon-btn:hover {
    background: var(--panel-2);
    color: var(--text);
    border-color: var(--border);
}

.ghost-icon-btn {
    width: 28px;
    height: 28px;
    font-size: 14px;
}

/* ── Chat panel ── */

.chat-panel {
    min-width: 0;
    height: 100vh;
    height: 100dvh;
    min-height: 0;
    display: grid;
    grid-template-rows: var(--topbar-height) minmax(0, 1fr) auto;
    position: relative;
}

.topbar {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 0 16px;
    border-bottom: 1px solid var(--border-soft);
    background: rgba(7, 7, 8, 0.85);
    backdrop-filter: blur(12px);
}

.topbar-center {
    flex: 1;
    display: flex;
    justify-content: center;
    min-width: 0;
}

.mode-select {
    width: auto;
    max-width: 220px;
    height: 34px;
    padding: 0 32px 0 12px;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: var(--panel);
    color: var(--text);
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 16 16' fill='%23a8a6b0'%3E%3Cpath d='M4.5 6L8 9.5 11.5 6'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
}

.mode-select:focus {
    border-color: rgba(255, 48, 48, 0.5);
    box-shadow: none;
}

.topbar-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

.status-pill {
    display: inline-flex;
    align-items: center;
    max-width: 180px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    padding: 0 10px;
    height: 28px;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: var(--panel);
    color: var(--faint);
    font-family: "Cascadia Code", Consolas, monospace;
    font-size: 11px;
}

/* ── Messages ── */

.messages {
    min-height: 0;
    overflow-y: auto;
    scroll-behavior: smooth;
}

.messages:has(.empty-state) {
    display: flex;
    align-items: center;
    justify-content: center;
}

.empty-state {
    width: min(100%, var(--message-max));
    margin: 0 auto;
    padding: 32px 24px 80px;
    text-align: center;
}

.empty-state .greeting-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 52px;
    height: 52px;
    margin-bottom: 20px;
    border-radius: 14px;
    background: var(--accent-soft);
    color: var(--accent);
    font-size: 24px;
}

.empty-state strong {
    display: block;
    font-size: 28px;
    font-weight: 600;
    margin-bottom: 8px;
    letter-spacing: -0.02em;
}

.empty-state > span {
    display: block;
    color: var(--muted);
    font-size: 16px;
    margin-bottom: 28px;
}

.suggestion-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
    max-width: 520px;
    margin: 0 auto;
}

.suggestion-chip {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    padding: 14px 16px;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: var(--panel);
    color: var(--text);
    font-size: 13px;
    text-align: left;
    line-height: 1.45;
    transition: background 150ms ease, border-color 150ms ease;
}

.suggestion-chip:hover {
    background: var(--panel-2);
    border-color: rgba(255, 48, 48, 0.35);
}

.suggestion-chip i {
    font-size: 16px;
    color: var(--accent);
    margin-top: 1px;
    flex-shrink: 0;
}

.message-row {
    width: 100%;
    padding: 20px 0;
    border-bottom: 1px solid transparent;
}

.message-row.user {
    background: transparent;
}

.message-row.assistant {
    background: transparent;
}

.message-inner {
    width: min(100%, var(--message-max));
    margin: 0 auto;
    padding: 0 24px;
    display: flex;
    gap: 16px;
    align-items: flex-start;
}

.message-row.user .message-inner {
    flex-direction: row-reverse;
}

.message-avatar {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border-radius: 999px;
    flex-shrink: 0;
    font-size: 14px;
    font-weight: 700;
}

.message-row.user .message-avatar {
    background: var(--accent-soft);
    color: var(--accent);
    border: 1px solid rgba(255, 48, 48, 0.25);
}

.message-row.assistant .message-avatar {
    background: var(--panel-2);
    color: var(--muted);
    border: 1px solid var(--border);
}

.message-content {
    min-width: 0;
    flex: 1;
    line-height: 1.7;
    font-size: 15px;
}

.message-row.user .message-content {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
}

.user-text {
    display: inline-block;
    max-width: 100%;
    padding: 10px 16px;
    border-radius: 18px 18px 4px 18px;
    background: rgba(52, 14, 17, 0.78);
    border: 1px solid rgba(255, 48, 48, 0.18);
    white-space: pre-wrap;
    word-break: break-word;
}

.message-actions {
    display: flex;
    gap: 6px;
    margin-top: 8px;
    opacity: 0;
    pointer-events: none;
    transition: opacity 150ms ease;
}

.message-row:hover .message-actions,
.message-row:focus-within .message-actions,
.message-actions:hover {
    opacity: 1;
    pointer-events: auto;
}

.message-row.user .message-actions {
    justify-content: flex-end;
}

.message-action-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    min-height: 30px;
    padding: 0 10px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--panel);
    color: var(--muted);
    font-size: 12px;
    font-weight: 500;
    transition: background 150ms ease, color 150ms ease, border-color 150ms ease;
}

.message-action-btn:hover {
    color: var(--text);
    background: var(--panel-2);
    border-color: rgba(255, 48, 48, 0.35);
}

.message-action-btn i {
    font-size: 13px;
}

.bubble {
    min-width: 0;
    line-height: 1.7;
}

.bubble p:first-child,
.bubble pre:first-child {
    margin-top: 0;
}

.bubble p:last-child,
.bubble pre:last-child {
    margin-bottom: 0;
}

.bubble pre {
    position: relative;
    overflow: auto;
    min-height: 80px;
    max-height: min(48vh, 460px);
    border: 1px solid var(--border);
    border-radius: 12px;
    background: var(--code);
    padding: 44px 14px 14px;
    margin: 12px 0;
    overscroll-behavior: contain;
}

.bubble code {
    font-family: "Cascadia Code", "Fira Code", Consolas, monospace;
    font-size: 13px;
}

.bubble pre code {
    color: var(--green);
}

.bubble :not(pre) > code {
    border: 1px solid var(--border);
    border-radius: 5px;
    background: var(--panel);
    padding: 2px 6px;
    font-size: 13px;
}

.code-toolbar {
    position: absolute;
    inset: 0 0 auto 0;
    height: 36px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 10px 0 52px;
    border-bottom: 1px solid var(--border);
    background: var(--panel-2);
    color: var(--muted);
    font-family: "Cascadia Code", Consolas, monospace;
    font-size: 11px;
}

.code-toolbar::before {
    content: "";
    position: absolute;
    left: 14px;
    top: 14px;
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #ff5f57;
    box-shadow: 14px 0 #ffbd2e, 28px 0 #28c840;
}

.code-actions {
    display: flex;
    gap: 4px;
}

.copy-code {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    min-height: 26px;
    padding: 0 10px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--panel);
    color: var(--muted);
    font-size: 11px;
    transition: color 150ms ease, border-color 150ms ease;
}

.copy-code:hover {
    color: var(--text);
    border-color: rgba(255, 48, 48, 0.4);
}

.attached-context-note {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    margin-top: 8px;
    padding: 6px 12px;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: var(--accent-soft);
    color: var(--muted);
    font-size: 12px;
}

/* ── Composer ── */

.composer-wrap {
    padding: 12px 16px 16px;
    background: linear-gradient(to top, var(--bg) 60%, transparent);
}

.composer {
    width: min(100%, var(--composer-max));
    margin: 0 auto;
}

.composer-box {
    border: 1px solid var(--border);
    border-radius: 20px;
    background: var(--panel);
    box-shadow: 0 4px 24px rgba(0, 0, 0, 0.25);
    overflow: hidden;
    transition: border-color 150ms ease, box-shadow 150ms ease;
}

.composer-box:focus-within {
    border-color: rgba(255, 48, 48, 0.45);
    box-shadow: 0 4px 32px rgba(255, 48, 48, 0.08);
}

.composer-inner {
    display: flex;
    align-items: flex-end;
    gap: 4px;
    padding: 8px 8px 8px 16px;
}

.composer textarea {
    flex: 1;
    min-height: 24px;
    max-height: 200px;
    border: 0;
    border-radius: 0;
    background: transparent;
    color: var(--text);
    font-size: 15px;
    line-height: 1.5;
    padding: 8px 0;
    resize: none;
    outline: none;
    box-shadow: none;
}

.composer textarea:focus {
    border-color: transparent;
    box-shadow: none;
}

.composer-toolbar {
    display: flex;
    align-items: center;
    gap: 2px;
    flex-shrink: 0;
}

.send-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border-radius: 999px;
    background: var(--accent);
    color: #fff;
    font-size: 17px;
    flex-shrink: 0;
    transition: background 150ms ease, transform 100ms ease, opacity 150ms ease;
}

.send-btn:hover:not(:disabled) {
    background: var(--accent-2);
}

.send-btn:active:not(:disabled) {
    transform: scale(0.94);
}

.send-btn:disabled {
    background: var(--panel-3);
    color: var(--faint);
}

.stop-btn:not(:disabled) {
    color: var(--accent);
}

.attachment-list {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    padding: 10px 14px 0;
}

.attachment-list:empty {
    display: none;
}

.attachment-item {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    max-width: 100%;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: var(--panel-2);
    color: var(--muted);
    padding: 4px 6px 4px 10px;
    font-size: 12px;
}

.attachment-item.skipped {
    border-color: rgba(255, 48, 48, 0.42);
}

.attachment-item i {
    font-size: 13px;
    color: var(--accent);
}

.attachment-item span {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.attachment-item button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 22px;
    height: 22px;
    border: 0;
    border-radius: 999px;
    background: transparent;
    color: var(--muted);
    font-size: 14px;
    transition: background 150ms ease, color 150ms ease;
}

.attachment-item button:hover {
    background: var(--panel-3);
    color: var(--text);
}

.composer-disclaimer {
    margin: 8px 0 0;
    text-align: center;
    font-size: 11px;
    line-height: 1.4;
}

/* ── Login ── */

.login-shell {
    display: grid;
    place-items: center;
    min-height: 100vh;
    padding: 24px;
}

.login-card {
    width: min(100%, 390px);
    border: 1px solid var(--border);
    border-radius: 18px;
    background: linear-gradient(180deg, rgba(34, 9, 12, 0.98), var(--sidebar) 42%);
    box-shadow: 0 24px 80px rgba(0, 0, 0, 0.42);
    padding: 26px;
}

.login-brand {
    margin-bottom: 22px;
}

.login-card h1 {
    margin: 0 0 8px;
    font-size: 25px;
}

.login-card p {
    margin: 0 0 20px;
    color: var(--muted);
    line-height: 1.55;
}

.login-card label {
    display: block;
    margin-bottom: 8px;
}

.login-card input {
    margin-bottom: 14px;
}

.login-card .primary-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    width: 100%;
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
}

.login-error {
    border: 1px solid rgba(255, 48, 48, 0.4);
    border-radius: 10px;
    background: rgba(255, 48, 48, 0.1);
    color: #ffc6c6;
    padding: 10px 12px;
    margin-bottom: 14px;
    font-size: 14px;
}

/* ── Responsive ── */

@media (max-width: 900px) {
    .app-shell {
        grid-template-columns: 1fr;
    }

    .sidebar {
        position: fixed;
        top: 0;
        left: 0;
        width: var(--sidebar-width);
        height: 100dvh;
        padding-bottom: max(12px, env(safe-area-inset-bottom));
        transform: translateX(0);
        box-shadow: 4px 0 24px rgba(0, 0, 0, 0.4);
    }

    .nav-hidden .sidebar {
        transform: translateX(calc(-1 * var(--sidebar-width) - 8px));
        opacity: 1;
        pointer-events: none;
    }

    body:not(.nav-hidden) .sidebar-overlay {
        display: block;
    }

    body:not(.nav-hidden) .sidebar-overlay.visible {
        opacity: 1;
        pointer-events: auto;
    }

    .sidebar-close {
        display: inline-flex;
    }

    .nav-hidden .app-shell {
        grid-template-columns: 1fr;
    }

    .suggestion-grid {
        grid-template-columns: 1fr;
    }

    .chat-panel {
        height: 100dvh;
        grid-template-rows: var(--topbar-height) minmax(0, 1fr);
    }

    .messages {
        padding-bottom: 16px;
    }

    .composer-wrap {
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: 30;
        padding: 8px 12px max(10px, env(safe-area-inset-bottom));
        background: linear-gradient(to top, var(--bg) 75%, rgba(7, 7, 8, 0.95));
        border-top: 1px solid var(--border-soft);
    }

    .composer-disclaimer {
        display: none;
    }

    .empty-state {
        padding-bottom: 100px;
    }
}

@media (max-width: 640px) {
    .topbar {
        padding: 0 12px;
        gap: 8px;
    }

    .status-pill {
        display: none;
    }

    .mode-select {
        max-width: 160px;
        font-size: 12px;
    }

    .message-inner {
        padding: 0 16px;
        gap: 12px;
    }

    .empty-state strong {
        font-size: 22px;
    }

    .empty-state > span {
        font-size: 14px;
    }

    .composer-wrap {
        padding: 6px 10px max(8px, env(safe-area-inset-bottom));
    }

    .composer-inner {
        padding: 6px 6px 6px 12px;
    }

    .message-actions {
        opacity: 1;
        pointer-events: auto;
    }

    .message-action-btn span {
        display: none;
    }
}
