/* ── Reset & base ──────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --bg:         #0f0f1a;
    --bg-surface: #1a1a2e;
    --bg-card:    #222240;
    --bg-input:   #2a2a4a;
    --text:       #e0e0f0;
    --text-dim:   #8888aa;
    --accent:     #6366f1;
    --accent-dim: #4f46e5;
    --green:      #22c55e;
    --red:        #ef4444;
    --orange:     #f59e0b;
    --blue:       #3b82f6;
    --user-bg:    #4f46e5;
    --radius:     12px;
    --radius-sm:  8px;
}

html, body {
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
    font-size: 15px;
    line-height: 1.5;
    color: var(--text);
    background: var(--bg);
    overflow: hidden;
}

/* ── App layout ───────────────────────────────────── */
#app {
    display: flex;
    height: 100%;
    height: 100dvh;
}

/* ── Sidebar ──────────────────────────────────────── */
#sidebar {
    width: 260px;
    min-width: 260px;
    background: var(--bg-surface);
    display: flex;
    flex-direction: column;
    border-right: 1px solid #2a2a4a;
}

.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px;
    border-bottom: 1px solid #2a2a4a;
}

.sidebar-header h2 {
    font-size: 18px;
    font-weight: 700;
    color: var(--accent);
}

.btn-new {
    width: 32px;
    height: 32px;
    border-radius: var(--radius-sm);
    border: 1px solid #3a3a5a;
    background: transparent;
    color: var(--text);
    font-size: 20px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s;
}
.btn-new:hover { background: var(--bg-card); }

#conversation-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px;
}

.conv-item {
    display: block;
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    color: var(--text-dim);
    text-decoration: none;
    font-size: 13px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    transition: background 0.15s;
}
.conv-item:hover { background: var(--bg-card); color: var(--text); }
.conv-item.active { background: var(--bg-card); color: var(--text); }
.conv-empty { padding: 16px; color: var(--text-dim); font-size: 13px; text-align: center; }

.sidebar-footer {
    padding: 12px 16px;
    border-top: 1px solid #2a2a4a;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 13px;
}
.user-label { color: var(--text-dim); }
.btn-logout { color: var(--text-dim); text-decoration: none; }
.btn-logout:hover { color: var(--red); }

.sidebar-toggle {
    display: none;
    position: fixed;
    top: 12px;
    left: 12px;
    z-index: 200;
    width: 40px;
    height: 40px;
    border-radius: var(--radius-sm);
    border: 1px solid #3a3a5a;
    background: var(--bg-surface);
    color: var(--text);
    font-size: 20px;
    cursor: pointer;
}

/* ── Chat area ────────────────────────────────────── */
#chat-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
}

#messages {
    flex: 1;
    overflow-y: auto;
    padding: 24px 16px;
    display: flex;
    flex-direction: column;
    gap: 16px;
    scroll-behavior: smooth;
}

.welcome {
    text-align: center;
    margin: auto;
    color: var(--text-dim);
}
.welcome h2 { font-size: 24px; color: var(--text); margin-bottom: 8px; }

/* ── Messages ─────────────────────────────────────── */
.message {
    max-width: 800px;
    width: 100%;
    margin: 0 auto;
}

.message.user .msg-content {
    background: var(--user-bg);
    color: #fff;
    padding: 10px 16px;
    border-radius: var(--radius) var(--radius) 4px var(--radius);
    display: inline-block;
    max-width: 85%;
    float: right;
    clear: both;
    white-space: pre-wrap;
    word-break: break-word;
}

.message.assistant {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.message.assistant .msg-content {
    background: var(--bg-surface);
    padding: 12px 16px;
    border-radius: 4px var(--radius) var(--radius) var(--radius);
    white-space: pre-wrap;
    word-break: break-word;
    line-height: 1.6;
}

.message.error .msg-content {
    background: #2a1520;
    border: 1px solid var(--red);
    color: #fca5a5;
    padding: 10px 16px;
    border-radius: var(--radius);
}

/* Compact one-line record of an intermediate tool step, shown in history so a
   reloaded conversation reflects what the agent did, not just its final reply. */
.message.tool .msg-content {
    font-family: 'Cascadia Code', 'Fira Code', 'Consolas', monospace;
    font-size: 12px;
    color: var(--text-dim);
    padding: 3px 12px;
    white-space: pre-wrap;
    word-break: break-word;
    line-height: 1.5;
}
.message.tool.blocked .msg-content { color: var(--red); }

/* ── Code blocks ──────────────────────────────────── */
.code-block {
    background: #0d0d1a;
    border: 1px solid #2a2a4a;
    border-radius: var(--radius-sm);
    padding: 12px;
    overflow-x: auto;
    font-family: 'Cascadia Code', 'Fira Code', 'Consolas', monospace;
    font-size: 13px;
    line-height: 1.5;
    margin: 8px 0;
    white-space: pre;
}

code.inline-code {
    background: #2a2a4a;
    padding: 2px 6px;
    border-radius: 4px;
    font-family: 'Cascadia Code', 'Fira Code', monospace;
    font-size: 13px;
}

/* ── Tool cards ───────────────────────────────────── */
.tool-card {
    background: var(--bg-card);
    border: 1px solid #3a3a5a;
    border-radius: var(--radius);
    overflow: hidden;
    font-size: 13px;
}

.tool-card.approval {
    border-color: var(--orange);
    box-shadow: 0 0 0 1px var(--orange), 0 4px 16px rgba(245, 158, 11, 0.1);
}

.tool-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 14px;
    background: rgba(0,0,0,0.2);
    flex-wrap: wrap;
}

.tool-icon { font-size: 16px; }
.tool-name { font-weight: 600; font-family: monospace; }

.tool-badge {
    font-size: 10px;
    font-weight: 700;
    text-transform: uppercase;
    padding: 2px 8px;
    border-radius: 10px;
    letter-spacing: 0.5px;
}
.badge-read    { background: #1a3a2a; color: var(--green); }
.badge-write   { background: #3a2a1a; color: var(--orange); }
.badge-execute { background: #2a1a3a; color: #a78bfa; }
.badge-admin   { background: #3a1a1a; color: var(--red); }

.tool-status {
    margin-left: auto;
    font-size: 11px;
    font-weight: 500;
}
.status-running { color: var(--blue); }
.status-pending { color: var(--orange); }
.status-completed { color: var(--green); }
.status-failed  { color: var(--red); }
.status-rejected { color: var(--red); }
.status-steered { color: var(--orange); }
.status-approved { color: var(--green); }

.tool-body {
    padding: 10px 14px;
}

.tool-file-path {
    color: var(--text-dim);
    font-family: monospace;
    font-size: 12px;
    margin-bottom: 6px;
}

.tool-code, .tool-json {
    background: #0d0d1a;
    border-radius: 6px;
    padding: 10px;
    font-family: monospace;
    font-size: 12px;
    overflow-x: auto;
    max-height: 300px;
    overflow-y: auto;
    white-space: pre;
}

.tool-command {
    display: block;
    background: #0d0d1a;
    border-radius: 6px;
    padding: 10px;
    font-family: monospace;
    font-size: 13px;
    color: var(--green);
}

.tool-cwd {
    display: block;
    color: var(--text-dim);
    font-size: 11px;
    margin-bottom: 4px;
}

.tool-result {
    margin-top: 8px;
    border-top: 1px solid #3a3a5a;
    padding-top: 8px;
}

.tool-result pre {
    background: #0d0d1a;
    border-radius: 6px;
    padding: 10px;
    font-family: monospace;
    font-size: 12px;
    overflow-x: auto;
    max-height: 200px;
    overflow-y: auto;
    white-space: pre-wrap;
    word-break: break-all;
}

/* ── Diff rendering ───────────────────────────────── */
.diff {
    background: #0d0d1a;
    border-radius: 6px;
    padding: 8px 10px;
    font-family: monospace;
    font-size: 12px;
    overflow-x: auto;
}
.diff-del { color: #fca5a5; background: rgba(239,68,68,0.1); padding: 1px 4px; }
.diff-add { color: #86efac; background: rgba(34,197,94,0.1); padding: 1px 4px; }

/* ── Approval buttons ─────────────────────────────── */
.tool-actions {
    display: flex;
    gap: 8px;
    padding: 10px 14px;
    border-top: 1px solid #3a3a5a;
}

.tool-actions button, .steer-box button {
    padding: 8px 18px;
    border-radius: var(--radius-sm);
    border: none;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: filter 0.15s;
}
.tool-actions button:hover, .steer-box button:hover {
    filter: brightness(1.15);
}

.btn-approve { background: var(--green); color: #fff; }
.btn-reject  { background: var(--red); color: #fff; }
.btn-steer   { background: var(--bg-input); color: var(--text); border: 1px solid #3a3a5a !important; }

.steer-box {
    padding: 10px 14px;
    border-top: 1px solid #3a3a5a;
    display: flex;
    gap: 8px;
}
.steer-box textarea {
    flex: 1;
    background: var(--bg-input);
    border: 1px solid #3a3a5a;
    border-radius: var(--radius-sm);
    color: var(--text);
    padding: 8px 12px;
    font-family: inherit;
    font-size: 14px;
    resize: none;
}
.hidden { display: none !important; }

/* ── System info ─────────────────────────────────── */
.system-info {
    font-size: 11px;
    color: var(--text-dim);
    padding: 4px 0;
    font-family: monospace;
}

/* ── PR cards ─────────────────────────────────────── */
.pr-card {
    background: var(--bg-card);
    border: 1px solid var(--green);
    border-radius: var(--radius);
    overflow: hidden;
    margin: 8px 0;
}

.pr-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 14px;
    background: rgba(34,197,94,0.08);
}

.pr-icon {
    font-size: 18px;
    color: var(--green);
}

.pr-title {
    font-weight: 600;
    font-size: 14px;
}

.pr-badge {
    font-size: 10px;
    font-weight: 700;
    text-transform: uppercase;
    padding: 2px 8px;
    border-radius: 10px;
    background: #1a3a2a;
    color: var(--green);
    margin-left: auto;
}

.pr-actions {
    display: flex;
    gap: 8px;
    padding: 10px 14px;
    border-top: 1px solid #2a3a2a;
}

.btn-pr {
    padding: 8px 16px;
    border-radius: var(--radius-sm);
    border: none;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: filter 0.15s;
}
.btn-pr:hover { filter: brightness(1.15); }

.btn-pr-diff   { background: var(--bg-input); color: var(--text); border: 1px solid #3a3a5a !important; }
.btn-pr-merge  { background: var(--green); color: #fff; }
.btn-pr-close  { background: var(--bg-input); color: var(--text-dim); border: 1px solid #3a3a5a !important; }

/* ── Status bubble ────────────────────────────────── */
.status-bubble {
    display: flex;
    align-items: center;
    gap: 10px;
    color: var(--text-dim);
    font-size: 13px;
    padding: 8px 0;
}

.dot-pulse {
    display: inline-block;
    width: 8px;
    height: 8px;
    background: var(--accent);
    border-radius: 50%;
    animation: pulse 1.2s infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 0.3; transform: scale(0.8); }
    50%      { opacity: 1;   transform: scale(1.2); }
}

/* ── Input area ───────────────────────────────────── */
#input-area {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 12px 16px;
    background: var(--bg-surface);
    border-top: 1px solid #2a2a4a;
    max-width: 800px;
    width: 100%;
    margin: 0 auto;
}

#message-input {
    flex: 1;
    background: var(--bg-input);
    border: 1px solid #3a3a5a;
    border-radius: var(--radius);
    color: var(--text);
    padding: 10px 14px;
    font-family: inherit;
    font-size: 15px;
    line-height: 1.5;
    resize: none;
    max-height: 150px;
    outline: none;
    transition: border-color 0.15s;
}
#message-input:focus {
    border-color: var(--accent);
}

.btn-mic, .btn-send, .btn-stop, .btn-attach {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    border: none;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    flex-shrink: 0;
    transition: background 0.15s;
}

.btn-mic {
    background: var(--bg-card);
    color: var(--text);
}
.btn-mic:hover { background: #333360; }

.btn-attach {
    background: var(--bg-card);
    color: var(--text-dim);
}
.btn-attach:hover { background: #333360; color: var(--text); }

.file-input-hidden { display: none; }

/* ── Attachment bar (selected/uploaded file chip) ──── */
.attachment-bar {
    max-width: 800px;
    width: 100%;
    margin: 0 auto;
    padding: 0 16px;
}
.attachment-bar.hidden { display: none; }

.attachment-chip {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    background: var(--bg-card);
    border: 1px solid #3a3a5a;
    border-radius: var(--radius-sm);
    padding: 6px 10px;
    font-size: 13px;
    color: var(--text);
    max-width: 100%;
}
.chip-icon { flex-shrink: 0; }
.chip-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip-state { color: var(--text-dim); font-size: 12px; }
.chip-remove {
    background: none;
    border: none;
    color: var(--text-dim);
    font-size: 18px;
    line-height: 1;
    cursor: pointer;
    padding: 0 2px;
}
.chip-remove:hover { color: var(--red); }
.btn-mic.recording {
    background: var(--red);
    color: #fff;
    animation: mic-pulse 1s infinite;
}

@keyframes mic-pulse {
    0%, 100% { box-shadow: 0 0 0 0 rgba(239,68,68,0.4); }
    50%      { box-shadow: 0 0 0 10px rgba(239,68,68,0); }
}

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

.btn-stop {
    background: var(--red);
    color: #fff;
}
.btn-stop:hover { background: #dc2626; }

/* ── Connection status ────────────────────────────── */
body.ws-disconnected::after {
    content: 'Reconnecting...';
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    background: var(--red);
    color: #fff;
    text-align: center;
    padding: 4px;
    font-size: 12px;
    font-weight: 600;
    z-index: 999;
}

/* ── Login page ───────────────────────────────────── */
.login-container {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    padding: 24px;
}

.login-card {
    background: var(--bg-surface);
    border-radius: var(--radius);
    padding: 40px 32px;
    width: 100%;
    max-width: 380px;
    text-align: center;
}

.login-card h1 {
    font-size: 28px;
    font-weight: 700;
    color: var(--accent);
    margin-bottom: 4px;
}

.login-subtitle {
    color: var(--text-dim);
    margin-bottom: 28px;
    font-size: 14px;
}

.login-error {
    background: #2a1520;
    border: 1px solid var(--red);
    color: #fca5a5;
    padding: 10px;
    border-radius: var(--radius-sm);
    margin-bottom: 16px;
    font-size: 13px;
}

.form-group {
    margin-bottom: 14px;
}

.form-group input {
    width: 100%;
    padding: 12px 16px;
    background: var(--bg-input);
    border: 1px solid #3a3a5a;
    border-radius: var(--radius-sm);
    color: var(--text);
    font-size: 15px;
    outline: none;
    transition: border-color 0.15s;
}
.form-group input:focus { border-color: var(--accent); }

.btn-login {
    width: 100%;
    padding: 12px;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    margin-top: 4px;
    transition: background 0.15s;
}
.btn-login:hover { background: var(--accent-dim); }

/* ── Scrollbar ────────────────────────────────────── */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #3a3a5a; border-radius: 3px; }

/* ── Lists inside messages ────────────────────────── */
.msg-content ul {
    margin: 4px 0;
    padding-left: 20px;
}
.msg-content li {
    margin: 2px 0;
}

/* ── Mobile ───────────────────────────────────────── */
@media (max-width: 768px) {
    #sidebar {
        position: fixed;
        top: 0;
        left: 0;
        bottom: 0;
        z-index: 100;
        transform: translateX(-100%);
        transition: transform 0.25s ease;
        box-shadow: 4px 0 24px rgba(0,0,0,0.5);
    }

    #sidebar.open {
        transform: translateX(0);
    }

    .sidebar-toggle {
        display: flex;
        align-items: center;
        justify-content: center;
    }

    #chat-area {
        width: 100%;
    }

    #messages {
        padding: 60px 12px 12px;
    }

    #input-area {
        padding: 8px 12px;
        padding-bottom: max(8px, env(safe-area-inset-bottom));
    }

    .message.user .msg-content {
        max-width: 92%;
    }

    .tool-actions {
        flex-wrap: wrap;
    }

    .tool-actions button {
        flex: 1;
        min-width: 80px;
    }
}

/* ── Interactive permission card ───────────────────────── */
.permission-card {
    background: var(--bg-card);
    border: 1px solid var(--orange);
    box-shadow: 0 0 0 1px var(--orange), 0 4px 16px rgba(245, 158, 11, 0.12);
    border-radius: var(--radius);
    overflow: hidden;
    font-size: 13px;
    margin: 8px 0;
}

.perm-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 14px;
    background: rgba(245, 158, 11, 0.08);
}
.perm-icon { font-size: 16px; }
.perm-title { font-weight: 500; }

.perm-body { padding: 10px 14px; }
.perm-body .tool-code,
.perm-body .tool-json {
    max-height: 240px;
    overflow: auto;
}

.perm-actions {
    display: flex;
    gap: 8px;
    padding: 10px 14px;
    border-top: 1px solid #3a3a5a;
}

.btn-perm {
    flex: 1;
    padding: 8px 12px;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
}
.btn-perm:hover { filter: brightness(1.12); }
.btn-perm-allow { background: var(--green); color: #fff; }
.btn-perm-deny  { background: var(--bg-input); color: var(--text); border: 1px solid #3a3a5a; }

.perm-result {
    font-weight: 600;
    color: var(--text-dim);
}
.permission-card.approved { border-color: var(--green); box-shadow: 0 0 0 1px var(--green); }
.permission-card.approved .perm-result { color: var(--green); }
.permission-card.denied   { border-color: var(--red); box-shadow: 0 0 0 1px var(--red); opacity: 0.85; }
.permission-card.denied .perm-result { color: var(--red); }
