/* CodeBrain UI */
:root {
    --bg: #0d0d0d;
    --bg-2: #121212;
    --surface: #171717;
    --surface-2: #1f1f1f;
    --surface-3: #2a2a2a;
    --border: rgba(255, 255, 255, 0.08);
    --border-strong: rgba(255, 255, 255, 0.14);
    --text: #ececec;
    --text-2: #b4b4b4;
    --text-3: #8e8e8e;
    --accent: #10a37f;
    --accent-2: #0d8a6a;
    --accent-glow: rgba(16, 163, 127, 0.25);
    --success: #10a37f;
    --warning: #fbbf24;
    --danger: #ef4444;
    --user-bg: #2f2f2f;
    --radius-sm: 10px;
    --radius: 14px;
    --radius-lg: 18px;
    --radius-xl: 22px;
    --shadow: 0 12px 40px rgba(0, 0, 0, 0.55);
    --font: "Inter", system-ui, sans-serif;
    --mono: "JetBrains Mono", ui-monospace, monospace;
    --rail-w: 280px;
    --drawer-w: 300px;
    --top-h: 56px;
    --thread-w: 1040px;
    --thread-pad: 1.25rem;
    --ease: cubic-bezier(0.22, 1, 0.36, 1);
}

*, *::before, *::after { box-sizing: border-box; }

html, body {
    height: 100%;
    margin: 0;
    overflow-x: hidden;
}

body {
    font-family: var(--font);
    font-size: 15px;
    line-height: 1.55;
    color: var(--text);
    background: var(--bg);
    -webkit-font-smoothing: antialiased;
}

.app-backdrop {
    position: fixed;
    inset: 0;
    pointer-events: none;
    background: radial-gradient(ellipse 70% 50% at 50% -20%, rgba(16, 163, 127, 0.07), transparent);
    z-index: 0;
}

.app {
    position: relative;
    z-index: 1;
    display: grid;
    grid-template-columns: var(--rail-w) minmax(0, 1fr) var(--drawer-w);
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
}

.app.drawer-closed {
    grid-template-columns: var(--rail-w) 1fr 0;
}

.app.drawer-closed .drawer {
    opacity: 0;
    pointer-events: none;
    transform: translateX(100%);
}

a { color: var(--accent); text-decoration: none; }
a:hover { color: #a5b4fc; }

code {
    font-family: var(--mono);
    font-size: 0.88em;
}

.muted { color: var(--text-2); font-size: 0.9rem; }

.hidden { display: none !important; }

/* Buttons */
.icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border: none;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text-2);
    cursor: pointer;
    transition: background 0.15s var(--ease), color 0.15s;
}
.icon-btn:hover {
    background: var(--surface-2);
    color: var(--text);
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.4rem;
    padding: 0.55rem 1rem;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    background: var(--surface-2);
    color: var(--text);
    font: inherit;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s;
}
.btn:hover { background: var(--surface-3); border-color: var(--border-strong); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn-primary {
    background: var(--accent-2);
    border-color: transparent;
    color: #fff;
}
.btn-primary:hover { background: var(--accent); }

/* Rail (sidebar) */
.rail {
    display: flex;
    flex-direction: column;
    background: rgba(12, 12, 15, 0.85);
    backdrop-filter: blur(12px);
    border-right: 1px solid var(--border);
    min-height: 0;
    z-index: 20;
}

.rail-top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1rem 1rem 0.5rem;
}

.logo {
    display: flex;
    align-items: center;
    gap: 0.65rem;
    color: var(--text);
    text-decoration: none;
    font-weight: 600;
    font-size: 1.05rem;
    letter-spacing: -0.02em;
}
.logo:hover { color: var(--text); }
.logo-mark {
    width: 28px;
    height: 28px;
    border-radius: 8px;
    background: var(--accent);
    box-shadow: 0 0 16px var(--accent-glow);
}
.rail-close { display: none; }

.new-chat-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    margin: 0.5rem 0.75rem 0.75rem;
    padding: 0.65rem 1rem;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius);
    background: var(--surface-2);
    color: var(--text);
    font: inherit;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s;
}
.new-chat-btn:hover {
    background: var(--surface-3);
    border-color: var(--accent);
}

.search-wrap {
    position: relative;
    margin: 0 0.75rem 0.5rem;
}
.search-icon {
    position: absolute;
    left: 0.75rem;
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-3);
    pointer-events: none;
}
.search-input {
    width: 100%;
    padding: 0.55rem 0.75rem 0.55rem 2.25rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--text);
    font: inherit;
    font-size: 0.9rem;
    outline: none;
    transition: border-color 0.15s, box-shadow 0.15s;
}
.search-input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.15);
}

.chat-list {
    flex: 1;
    overflow-y: auto;
    padding: 0.25rem 0.5rem;
    min-height: 0;
}
.chat-list::-webkit-scrollbar { width: 6px; }
.chat-list::-webkit-scrollbar-thumb { background: var(--surface-3); border-radius: 3px; }

.chat-item-row {
    display: flex;
    align-items: stretch;
    gap: 0.15rem;
    margin-bottom: 2px;
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    transition: background 0.12s, border-color 0.12s;
}
.chat-item-row:hover { background: var(--surface-2); }
.chat-item-row:hover .chat-item-delete { opacity: 1; }
.chat-item-row.active {
    background: var(--surface-2);
    border-color: var(--border-strong);
}
.chat-item-open {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    text-align: left;
    padding: 0.65rem 0.35rem 0.65rem 0.75rem;
    border: none;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text);
    cursor: pointer;
    font: inherit;
}
.chat-item-delete {
    flex-shrink: 0;
    width: 2rem;
    margin: 0.35rem 0.35rem 0.35rem 0;
    border: none;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text-3);
    font-size: 1.25rem;
    line-height: 1;
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.12s, background 0.12s, color 0.12s;
}
.chat-item-row.active .chat-item-delete,
.chat-item-delete:focus-visible {
    opacity: 1;
}
.chat-item-delete:hover {
    background: rgba(239, 68, 68, 0.15);
    color: var(--danger);
}
@media (hover: none) {
    .chat-item-delete { opacity: 0.75; }
}
.chat-item-title {
    font-size: 0.9rem;
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 100%;
}
.chat-item-meta {
    font-size: 0.72rem;
    color: var(--text-3);
    margin-top: 0.15rem;
}

.rail-bottom {
    padding: 0.75rem;
    border-top: 1px solid var(--border);
}
.field-label {
    display: block;
    font-size: 0.72rem;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-3);
    margin-bottom: 0.35rem;
}
.select {
    width: 100%;
    padding: 0.5rem 0.65rem;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    font: inherit;
    font-size: 0.88rem;
    margin-bottom: 0.75rem;
    outline: none;
}
.select:focus { border-color: var(--accent); }

.rail-links {
    display: flex;
    flex-wrap: wrap;
    gap: 0.75rem;
    align-items: center;
}
.rail-link {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    font-size: 0.85rem;
    color: var(--text-2);
}
.rail-link:hover { color: var(--text); }
.rail-link-muted { color: var(--text-3); }

.rail-scrim {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.55);
    z-index: 15;
}

/* Stage (main) */
.stage {
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
    position: relative;
}

.top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    height: var(--top-h);
    padding: 0 1.25rem;
    border-bottom: 1px solid var(--border);
    background: rgba(7, 7, 8, 0.6);
    backdrop-filter: blur(8px);
    flex-shrink: 0;
}
.top-left {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    min-width: 0;
}
#rail-open { display: none; }
.top-titles { min-width: 0; }
.top-title {
    margin: 0;
    font-size: 1rem;
    font-weight: 600;
    letter-spacing: -0.02em;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.top-meta {
    font-size: 0.78rem;
    color: var(--text-3);
    margin-top: 0.1rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.top-actions {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex-shrink: 0;
}

.status-pill {
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    padding: 0.25rem 0.55rem;
    border-radius: 999px;
    border: 1px solid var(--border);
    color: var(--text-3);
    background: var(--surface);
}
.status-pill[data-state="streaming"] {
    color: var(--accent);
    border-color: rgba(99, 102, 241, 0.4);
    background: rgba(99, 102, 241, 0.1);
    animation: pulse-pill 1.5s ease infinite;
}
.status-pill[data-state="error"] {
    color: var(--danger);
    border-color: rgba(248, 113, 113, 0.35);
}
.status-pill[data-state="done"] {
    color: var(--success);
    border-color: rgba(52, 211, 153, 0.35);
}

@keyframes pulse-pill {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.65; }
}

.menu-wrap { position: relative; }
.dropdown {
    position: absolute;
    right: 0;
    top: calc(100% + 6px);
    min-width: 200px;
    padding: 0.35rem;
    border-radius: var(--radius);
    border: 1px solid var(--border-strong);
    background: var(--surface-2);
    box-shadow: var(--shadow);
    z-index: 50;
}
.dropdown button {
    display: block;
    width: 100%;
    text-align: left;
    padding: 0.55rem 0.75rem;
    border: none;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text);
    font: inherit;
    font-size: 0.88rem;
    cursor: pointer;
}
.dropdown button:hover:not(:disabled) { background: var(--surface-3); }
.dropdown button:disabled { opacity: 0.4; cursor: not-allowed; }

/* Thread */
.thread-wrap {
    flex: 1;
    overflow-y: auto;
    min-height: 0;
    scroll-behavior: smooth;
}
.thread-wrap::-webkit-scrollbar { width: 8px; }
.thread-wrap::-webkit-scrollbar-thumb { background: var(--surface-3); border-radius: 4px; }

.thread {
    width: 100%;
    max-width: var(--thread-w);
    margin: 0 auto;
    padding: 1.25rem var(--thread-pad) 2rem;
    box-sizing: border-box;
}

.agent-timeline {
    display: flex;
    flex-direction: column;
    gap: 0.85rem;
    width: 100%;
}

.agent-step {
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
    overflow: hidden;
}
.agent-step-label {
    padding: 0.4rem 0.75rem;
    font-size: 0.72rem;
    font-weight: 600;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--text-3);
    border-bottom: 1px solid var(--border);
    background: rgba(255, 255, 255, 0.02);
}
.step-flow {
    padding: 0.65rem 0.75rem 0.75rem;
    display: flex;
    flex-direction: column;
    gap: 0.55rem;
}

.step-preface {
    font-size: 0.84rem;
    line-height: 1.5;
    color: var(--text-3);
    white-space: pre-wrap;
    padding: 0.45rem 0.55rem;
    border-left: 2px solid var(--border-strong);
    background: rgba(255, 255, 255, 0.02);
    border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
}

.tools-running {
    margin: 0;
    font-size: 0.82rem;
    color: var(--text-3);
    font-style: italic;
}

.empty-state {
    text-align: center;
    padding: 3rem 1.5rem;
    animation: fade-up 0.4s var(--ease);
}
.empty-icon {
    width: 56px;
    height: 56px;
    margin: 0 auto 1rem;
    border-radius: 16px;
    background: linear-gradient(135deg, var(--surface-2), var(--surface-3));
    border: 1px solid var(--border);
    display: grid;
    place-items: center;
    color: var(--accent);
}
.empty-state h2 {
    margin: 0 0 0.5rem;
    font-size: 1.25rem;
    font-weight: 600;
}
.empty-state p {
    margin: 0;
    color: var(--text-2);
    max-width: 420px;
    margin-inline: auto;
    font-size: 0.95rem;
}
.empty-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    justify-content: center;
    margin-top: 1.25rem;
}
.chip {
    padding: 0.4rem 0.75rem;
    border-radius: 999px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text-2);
    font-size: 0.82rem;
    cursor: pointer;
    transition: border-color 0.15s, color 0.15s;
}
.chip:hover {
    border-color: var(--accent);
    color: var(--text);
}

@keyframes fade-up {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}

.turn {
    display: flex;
    gap: 0.85rem;
    margin-bottom: 1.75rem;
    animation: fade-up 0.35s var(--ease);
}
.turn-user { flex-direction: row-reverse; }

.avatar {
    flex-shrink: 0;
    width: 32px;
    height: 32px;
    border-radius: 10px;
    display: grid;
    place-items: center;
    font-size: 0.7rem;
    font-weight: 600;
    letter-spacing: 0.02em;
}
.avatar-user {
    background: var(--user-bg);
    color: #e0e7ff;
    border: 1px solid rgba(129, 140, 248, 0.3);
}
.avatar-ai {
    background: var(--surface-2);
    border: 1px solid var(--border);
    color: var(--accent);
}

.bubble {
    min-width: 0;
    max-width: min(100%, 640px);
}
.turn-user .bubble { max-width: min(560px, 92%); }

.bubble-user {
    padding: 0.75rem 1rem;
    border-radius: var(--radius-lg);
    background: var(--user-bg);
    border: 1px solid var(--border-strong);
    color: var(--text);
    font-size: 0.95rem;
    white-space: pre-wrap;
    word-break: break-word;
}

.bubble-ai {
    flex: 1;
    max-width: 100%;
}

.reasoning-panel {
    margin-bottom: 0.65rem;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    background: rgba(0, 0, 0, 0.2);
    overflow: hidden;
}
.reasoning-panel summary {
    list-style: none;
    cursor: pointer;
    padding: 0.45rem 0.65rem;
    font-size: 0.78rem;
    font-weight: 500;
    color: var(--text-3);
    user-select: none;
    display: flex;
    align-items: center;
    gap: 0.4rem;
}
.reasoning-panel summary::-webkit-details-marker { display: none; }
.reasoning-panel summary::before {
    content: "▸";
    font-size: 0.65rem;
    transition: transform 0.15s var(--ease);
}
.reasoning-panel[open] summary::before { transform: rotate(90deg); }
.reasoning-panel.is-live summary { color: var(--text-2); }
.reasoning-panel.is-live summary::after {
    content: " · идёт";
    font-weight: 400;
    color: var(--text-3);
}

.reasoning-body {
    padding: 0.55rem 0.75rem 0.65rem;
    border-top: 1px solid var(--border);
    font-size: 0.84rem;
    line-height: 1.55;
    color: var(--text-3);
    white-space: pre-wrap;
    word-break: break-word;
    max-height: 220px;
    overflow-y: auto;
}
.reasoning-body.reasoning-stream {
    max-height: 280px;
}

.answer-body {
    font-size: 0.95rem;
    color: var(--text);
}
.answer-body .md { color: inherit; }

.md {
    font-size: 0.95rem;
    color: var(--text);
}
.md > p:first-child { margin-top: 0; }
.md > p:last-child { margin-bottom: 0; }
.md p { margin: 0.65rem 0; }
.md h1, .md h2, .md h3 {
    margin: 1.25rem 0 0.5rem;
    font-weight: 600;
    letter-spacing: -0.02em;
    line-height: 1.3;
}
.md h2 { font-size: 1.1rem; }
.md h3 { font-size: 1rem; }
.md ul, .md ol {
    margin: 0.5rem 0;
    padding-left: 1.25rem;
}
.md li { margin: 0.25rem 0; }
.md a { color: #a5b4fc; }

.code-block {
    margin: 0.65rem 0;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border-strong);
    background: #0a0a0a;
    overflow: hidden;
}
.code-block-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.35rem 0.65rem;
    background: #141414;
    border-bottom: 1px solid var(--border);
    font-size: 0.72rem;
}
.code-lang {
    font-family: var(--mono);
    color: var(--text-3);
    text-transform: lowercase;
}
.code-copy {
    border: none;
    background: transparent;
    color: var(--text-2);
    font: inherit;
    font-size: 0.72rem;
    cursor: pointer;
    padding: 0.15rem 0.35rem;
    border-radius: 4px;
}
.code-copy:hover { background: var(--surface-3); color: var(--text); }
.code-block pre {
    margin: 0;
    padding: 0.85rem 1rem;
    overflow-x: auto;
    font-family: var(--mono);
    font-size: 0.8125rem;
    line-height: 1.55;
}
.code-block pre code { background: none; padding: 0; }
.md :not(pre) > code,
.inline-code {
    padding: 0.12em 0.35em;
    border-radius: 4px;
    background: #1a1a1a;
    border: 1px solid var(--border);
    font-family: var(--mono);
    font-size: 0.88em;
}

.copy-code-btn {
    position: absolute;
    top: 0.5rem;
    right: 0.5rem;
    padding: 0.2rem 0.45rem;
    font-size: 0.68rem;
    font-family: var(--font);
    border-radius: 4px;
    border: 1px solid var(--border);
    background: var(--surface-2);
    color: var(--text-2);
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.15s;
}
.md pre:hover .copy-code-btn { opacity: 1; }

.msg-actions {
    display: flex;
    gap: 0.35rem;
    margin-top: 0.5rem;
}
.msg-action {
    padding: 0.25rem 0.5rem;
    font-size: 0.72rem;
    font-weight: 500;
    color: var(--text-3);
    background: none;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-family: var(--font);
}
.msg-action:hover { color: var(--text-2); background: var(--surface-2); }

/* Tools timeline */
.tools-panel {
    margin-top: 0.75rem;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    background: rgba(0, 0, 0, 0.25);
    overflow: hidden;
}
.tools-panel summary {
    list-style: none;
    cursor: pointer;
    padding: 0.55rem 0.75rem;
    font-size: 0.8rem;
    font-weight: 500;
    color: var(--text-2);
    display: flex;
    align-items: center;
    gap: 0.5rem;
    user-select: none;
}
.tools-panel summary::-webkit-details-marker { display: none; }
.tools-panel summary::before {
    content: "";
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--success);
    box-shadow: 0 0 8px rgba(52, 211, 153, 0.5);
}
.tools-panel[open] summary { border-bottom: 1px solid var(--border); }

.tool-card {
    padding: 0.65rem 0.75rem;
    border-top: 1px solid var(--border);
    font-size: 0.78rem;
}
.tool-card:first-of-type { border-top: none; }
.tool-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    margin-bottom: 0.35rem;
}
.tool-name {
    font-family: var(--mono);
    font-weight: 500;
    color: #a5b4fc;
    font-size: 0.8rem;
}
.tool-ms {
    color: var(--text-3);
    font-size: 0.72rem;
}
.tool-block {
    font-family: var(--mono);
    font-size: 0.72rem;
    color: var(--text-3);
    background: var(--surface);
    border-radius: 6px;
    padding: 0.4rem 0.5rem;
    margin-top: 0.25rem;
    max-height: 100px;
    overflow: auto;
    white-space: pre-wrap;
    word-break: break-word;
}

.typing {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 0.25rem 0;
}
.typing span {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--text-3);
    animation: typing 1.2s infinite;
}
.typing span:nth-child(2) { animation-delay: 0.15s; }
.typing span:nth-child(3) { animation-delay: 0.3s; }
@keyframes typing {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
    30% { transform: translateY(-4px); opacity: 1; }
}

.stream-cursor::after {
    content: "";
    display: inline-block;
    width: 2px;
    height: 1em;
    background: var(--accent);
    margin-left: 2px;
    vertical-align: text-bottom;
    animation: blink 0.9s step-end infinite;
}
@keyframes blink { 50% { opacity: 0; } }

/* Composer */
.composer-wrap {
    flex-shrink: 0;
    width: 100%;
    max-width: var(--thread-w);
    margin: 0 auto;
    padding: 0 var(--thread-pad) calc(1.25rem + env(safe-area-inset-bottom, 0px));
    box-sizing: border-box;
    background: linear-gradient(to top, var(--bg) 70%, transparent);
}
.composer-card {
    width: 100%;
    margin: 0 auto;
    border-radius: var(--radius-xl);
    border: 1px solid var(--border-strong);
    background: rgba(18, 18, 22, 0.95);
    backdrop-filter: blur(12px);
    box-shadow: var(--shadow);
    overflow: hidden;
}
.composer-hint {
    padding: 0.5rem 1rem 0;
    font-size: 0.75rem;
    color: var(--text-3);
}
.composer-hint code {
    font-size: 0.85em;
    color: var(--text-2);
}
.composer-row {
    display: flex;
    align-items: flex-end;
    gap: 0.5rem;
    padding: 0.65rem 0.65rem 0.5rem 1rem;
}
.composer-row textarea {
    flex: 1;
    resize: none;
    min-height: 24px;
    max-height: 200px;
    border: none;
    background: transparent;
    color: var(--text);
    font: inherit;
    font-size: 0.95rem;
    line-height: 1.5;
    outline: none;
}
.composer-buttons {
    display: flex;
    gap: 0.35rem;
    flex-shrink: 0;
}
.send-btn, .stop-btn {
    width: 40px;
    height: 40px;
    border: none;
    border-radius: 12px;
    cursor: pointer;
    display: grid;
    place-items: center;
    transition: transform 0.12s, background 0.15s;
}
.send-btn {
    background: var(--accent-2);
    color: #fff;
}
.send-btn:hover:not(:disabled) { background: var(--accent); transform: scale(1.03); }
.send-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.stop-btn {
    background: rgba(248, 113, 113, 0.15);
    color: var(--danger);
    border: 1px solid rgba(248, 113, 113, 0.35);
}
.composer-foot {
    padding: 0 1rem 0.55rem;
    font-size: 0.68rem;
    color: var(--text-3);
    text-align: center;
}

/* Drawer (notes) */
.drawer {
    display: flex;
    flex-direction: column;
    background: rgba(12, 12, 15, 0.9);
    backdrop-filter: blur(12px);
    border-left: 1px solid var(--border);
    min-height: 0;
    transition: opacity 0.25s var(--ease), transform 0.25s var(--ease);
}
.drawer-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1rem 1rem 0.5rem;
}
.drawer-head h2 {
    margin: 0;
    font-size: 0.95rem;
    font-weight: 600;
}
.drawer-desc {
    margin: 0;
    padding: 0 1rem 0.75rem;
    font-size: 0.78rem;
    color: var(--text-3);
    line-height: 1.45;
}
.notes-list {
    list-style: none;
    margin: 0;
    padding: 0 0.75rem 1rem;
    overflow-y: auto;
    flex: 1;
}
.notes-list li {
    padding: 0.75rem;
    margin-bottom: 0.5rem;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    background: var(--surface);
    font-size: 0.85rem;
    line-height: 1.45;
}
.notes-list .note-meta {
    font-size: 0.68rem;
    color: var(--text-3);
    margin-top: 0.4rem;
}
.notes-empty {
    padding: 1rem;
    text-align: center;
    color: var(--text-3);
    font-size: 0.85rem;
}

/* Login & settings pages */
.login-page {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 1.5rem;
    background: var(--bg);
}
.login-card {
    width: min(420px, 100%);
    padding: 2.25rem;
    border-radius: var(--radius-xl);
    border: 1px solid var(--border);
    background: var(--surface);
    box-shadow: var(--shadow);
}
.login-card .logo {
    margin-bottom: 1.5rem;
}
.login-card h1 {
    margin: 0 0 0.35rem;
    font-size: 1.5rem;
    font-weight: 600;
}
.login-card label {
    display: block;
    margin: 1.25rem 0 0.4rem;
    font-size: 0.85rem;
    font-weight: 500;
    color: var(--text-2);
}
.login-card input {
    width: 100%;
    padding: 0.7rem 0.85rem;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    background: var(--bg-2);
    color: var(--text);
    font: inherit;
    outline: none;
}
.login-card input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.15);
}
.login-card .btn-primary {
    width: 100%;
    margin-top: 1.5rem;
    padding: 0.75rem;
}
.alert-error {
    margin-top: 1rem;
    padding: 0.75rem;
    border-radius: var(--radius-sm);
    background: rgba(248, 113, 113, 0.1);
    border: 1px solid rgba(248, 113, 113, 0.3);
    color: #fecaca;
    font-size: 0.88rem;
}

.page-shell {
    min-height: 100vh;
    background: var(--bg);
}
.page-topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.85rem 1.5rem;
    border-bottom: 1px solid var(--border);
    background: rgba(12, 12, 15, 0.8);
    backdrop-filter: blur(8px);
}
.page-topbar nav {
    display: flex;
    gap: 1.25rem;
}
.page-topbar nav a {
    color: var(--text-2);
    font-size: 0.9rem;
    font-weight: 500;
}
.page-topbar nav a.active { color: var(--text); }

.settings-main {
    max-width: 560px;
    margin: 0 auto;
    padding: 2rem 1.25rem 4rem;
}
.settings-wide { max-width: 720px; }
.settings-section {
    margin-bottom: 2rem;
    padding-bottom: 1.5rem;
    border-bottom: 1px solid var(--border);
}
.settings-section h2 {
    margin: 0 0 0.35rem;
    font-size: 1.1rem;
}
.settings-form textarea {
    width: 100%;
    min-height: 120px;
    padding: 0.75rem 0.85rem;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    font: inherit;
    font-family: var(--mono);
    font-size: 0.82rem;
    line-height: 1.45;
    resize: vertical;
    outline: none;
}
.settings-form textarea:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-glow);
}
.settings-form select {
    width: 100%;
    padding: 0.65rem 0.75rem;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    font: inherit;
}
.settings-main h1 {
    margin: 0 0 0.35rem;
    font-size: 1.5rem;
    font-weight: 600;
}
.settings-form label {
    display: block;
    margin: 1.35rem 0 0.4rem;
    font-size: 0.85rem;
    font-weight: 500;
    color: var(--text-2);
}
.settings-form input[type="text"],
.settings-form input[type="url"],
.settings-form input[type="password"],
.settings-form input[type="number"] {
    width: 100%;
    padding: 0.7rem 0.85rem;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    font: inherit;
    outline: none;
}
.settings-form input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.15);
}
.settings-form .checkbox {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-top: 1.25rem;
    color: var(--text);
}
.form-actions { margin-top: 1.75rem; }
#settings-status { margin-top: 0.75rem; font-size: 0.85rem; }

/* Tablet & mobile */
@media (max-width: 1024px) {
    :root {
        --thread-w: 100%;
        --thread-pad: 1rem;
    }
    .app {
        grid-template-columns: 1fr;
    }
    .app.drawer-closed {
        grid-template-columns: 1fr;
    }
    .stage {
        width: 100%;
        min-width: 0;
    }
    .drawer {
        position: fixed;
        top: 0;
        right: 0;
        bottom: 0;
        width: min(var(--drawer-w), 100vw);
        z-index: 25;
        box-shadow: var(--shadow);
        padding-bottom: env(safe-area-inset-bottom, 0);
    }
    .rail {
        position: fixed;
        top: 0;
        left: 0;
        bottom: 0;
        width: min(var(--rail-w), 92vw);
        transform: translateX(-100%);
        transition: transform 0.28s var(--ease);
        box-shadow: var(--shadow);
        padding-top: env(safe-area-inset-top, 0);
        padding-bottom: env(safe-area-inset-bottom, 0);
    }
    .app.rail-open .rail {
        transform: translateX(0);
    }
    .app.rail-open .rail-scrim {
        display: block;
    }
    .app.rail-open {
        overflow: hidden;
    }
    .rail-close, #rail-open { display: inline-flex; }
    .top {
        padding-top: max(0px, env(safe-area-inset-top, 0));
    }
    .turn-user .bubble {
        max-width: min(100%, 520px);
    }
}

@media (max-width: 640px) {
    :root {
        --thread-pad: 0.65rem;
        --top-h: 52px;
    }
    .top {
        padding-left: 0.5rem;
        padding-right: 0.5rem;
        gap: 0.35rem;
    }
    .top-title {
        font-size: 0.92rem;
        max-width: 42vw;
    }
    .top-meta { display: none; }
    .status-pill { display: none; }
    .header-actions .icon-btn {
        width: 40px;
        height: 40px;
    }
    .thread {
        padding-top: 0.75rem;
        padding-bottom: 1rem;
    }
    .composer-wrap {
        padding-left: var(--thread-pad);
        padding-right: var(--thread-pad);
    }
    .composer-hint,
    .composer-foot {
        display: none;
    }
    .composer-row textarea {
        font-size: 16px;
        min-height: 28px;
    }
    .send-btn, .stop-btn {
        width: 44px;
        height: 44px;
        min-width: 44px;
        min-height: 44px;
    }
    .composer-card {
        border-radius: var(--radius-lg);
    }
    .agent-step-label {
        font-size: 0.68rem;
    }
    .code-block pre {
        font-size: 0.75rem;
        -webkit-overflow-scrolling: touch;
    }
    .chat-item-delete {
        opacity: 0.85;
        width: 2.75rem;
        min-width: 44px;
    }
    .new-chat-btn {
        margin-left: 0.5rem;
        margin-right: 0.5rem;
    }
    .empty-state {
        padding: 2rem 0.75rem;
    }
    .empty-chips {
        flex-direction: column;
        align-items: stretch;
    }
    .chip {
        text-align: left;
    }
}

@media (min-width: 1400px) {
    :root {
        --thread-w: 1120px;
    }
}

body.rail-open-body {
    overflow: hidden;
    touch-action: none;
}
