/* ── Variables ────────────────────────────────────────────────────────────── */
:root {
    --bg:             #0a0a0a;
    --surface:        #111111;
    --surface2:       #181818;
    --surface3:       #222222;
    --border:         #2a2a2a;
    --accent:         #0066CC;
    --accent-hover:   #0055aa;
    --text:           #e8e8e8;
    --text-muted:     #888888;
    --text-dim:       #444444;
    --success:        #22c55e;
    --warning:        #f59e0b;
    --error:          #ef4444;
    --user-bg:        #0066CC;
    --asst-bg:        #181818;
    --font:           -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif;
    --mono:           'SF Mono', 'Fira Code', 'Cascadia Code', Consolas, monospace;
    --radius:         14px;
    --radius-sm:      8px;
    --sidebar-w:      260px;
    --header-h:       54px;
}

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

html, body {
    height: 100%;
    overflow: hidden;
    font-family: var(--font);
    background: var(--bg);
    color: var(--text);
    font-size: 15px;
    line-height: 1.55;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* ── App Shell ────────────────────────────────────────────────────────────── */
#app {
    display: flex;
    height: 100vh;
    height: 100dvh;
}

/* ── Sidebar ──────────────────────────────────────────────────────────────── */
.sidebar {
    width: var(--sidebar-w);
    flex-shrink: 0;
    background: var(--surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.55);
    z-index: 99;
    backdrop-filter: blur(2px);
}
.sidebar-overlay.visible { display: block; }

.sidebar-header {
    height: var(--header-h);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 14px;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.logo {
    font-size: 15px;
    font-weight: 800;
    color: var(--accent);
    letter-spacing: 3px;
    user-select: none;
}

.conversation-list {
    flex: 1;
    overflow-y: auto;
    padding: 6px;
}

.conv-item {
    padding: 9px 12px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 13px;
    color: var(--text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-bottom: 1px;
    transition: background 0.1s, color 0.1s;
}
.conv-item:hover  { background: var(--surface2); color: var(--text); }
.conv-item.active { background: var(--surface3); color: var(--text); }

.sidebar-footer {
    padding: 12px;
    border-top: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    gap: 5px;
}

.user-label {
    font-size: 11px;
    color: var(--text-dim);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.user-select {
    width: 100%;
    background: var(--surface2);
    border: 1px solid var(--border);
    color: var(--text);
    padding: 8px 10px;
    border-radius: var(--radius-sm);
    font-size: 13px;
    font-family: var(--font);
    cursor: pointer;
    outline: none;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%23888' stroke-width='2'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 10px center;
    padding-right: 28px;
}
.user-select:focus { border-color: var(--accent); }

/* ── Main ─────────────────────────────────────────────────────────────────── */
#main {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    overflow: hidden;
}

/* ── Header ───────────────────────────────────────────────────────────────── */
#header {
    height: var(--header-h);
    padding: 0 10px;
    padding-top: env(safe-area-inset-top);
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

.header-title {
    flex: 1;
    display: flex;
    align-items: center;
    gap: 7px;
    font-weight: 600;
    font-size: 16px;
    user-select: none;
}

/* ── Status dot ───────────────────────────────────────────────────────────── */
.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--text-dim);
    flex-shrink: 0;
    transition: background 0.3s;
}
.status-dot.connected    { background: var(--success); }
.status-dot.disconnected { background: var(--error); }

/* ── Sensor bar ───────────────────────────────────────────────────────────── */
.sensor-bar {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 8px 12px;
    overflow-x: auto;
    flex-shrink: 0;
    scrollbar-width: none;
}
.sensor-bar::-webkit-scrollbar { display: none; }

.sensor-grid {
    display: flex;
    gap: 8px;
    min-width: max-content;
}

.sensor-card {
    background: var(--surface2);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 7px 12px;
    min-width: 82px;
    text-align: center;
    flex-shrink: 0;
}
.sensor-card.stale { opacity: 0.5; }

.sensor-icon  { font-size: 16px; line-height: 1; }
.sensor-label { font-size: 10px; color: var(--text-muted); margin-top: 3px; text-transform: uppercase; letter-spacing: 0.4px; }
.sensor-value { font-size: 15px; font-weight: 700; margin-top: 2px; color: var(--text); }

/* ── Messages ─────────────────────────────────────────────────────────────── */
#messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px 16px;
    display: flex;
    flex-direction: column;
    gap: 18px;
    scroll-behavior: smooth;
}

/* ── Welcome state ────────────────────────────────────────────────────────── */
.welcome {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    gap: 10px;
    text-align: center;
    color: var(--text-muted);
    user-select: none;
}
.welcome-logo { font-size: 42px; font-weight: 800; color: var(--accent); letter-spacing: 5px; }
.welcome-tagline { font-size: 15px; }
.welcome-hint { font-size: 13px; color: var(--text-dim); }

/* ── Message bubbles ──────────────────────────────────────────────────────── */
.message {
    display: flex;
    flex-direction: column;
    max-width: 78%;
}
.message.user      { align-self: flex-end;   align-items: flex-end; }
.message.assistant { align-self: flex-start; align-items: flex-start; }

.message-role {
    font-size: 11px;
    color: var(--text-muted);
    margin-bottom: 4px;
    font-weight: 600;
    letter-spacing: 0.3px;
    text-transform: uppercase;
}

.message-bubble {
    padding: 10px 14px;
    border-radius: var(--radius);
    word-wrap: break-word;
    overflow-wrap: break-word;
    min-width: 40px;
}

.message.user .message-bubble {
    background: var(--user-bg);
    color: #fff;
    border-bottom-right-radius: 4px;
}

.message.assistant .message-bubble {
    background: var(--asst-bg);
    border: 1px solid var(--border);
    border-bottom-left-radius: 4px;
}

/* ── Typing indicator ─────────────────────────────────────────────────────── */
.typing-bubble {
    background: var(--asst-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    border-bottom-left-radius: 4px;
    padding: 12px 16px;
    display: flex;
    gap: 5px;
    align-items: center;
}
.typing-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--text-dim);
    animation: bounce 1.3s infinite ease-in-out;
}
.typing-dot:nth-child(2) { animation-delay: 0.18s; }
.typing-dot:nth-child(3) { animation-delay: 0.36s; }

@keyframes bounce {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
    30%            { transform: translateY(-6px); opacity: 1; }
}

/* ── Tool indicator ───────────────────────────────────────────────────────── */
.tool-indicator {
    font-size: 12px;
    color: var(--text-muted);
    font-style: italic;
    margin-bottom: 5px;
    display: flex;
    align-items: center;
    gap: 5px;
}
.tool-spinner {
    width: 10px;
    height: 10px;
    border: 1.5px solid var(--text-dim);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Approval card ────────────────────────────────────────────────────────── */
.approval-card {
    background: var(--surface2);
    border: 1px solid var(--border);
    border-left: 3px solid var(--warning);
    border-radius: var(--radius);
    padding: 14px 16px;
    max-width: 380px;
    align-self: flex-start;
}

.approval-title {
    font-size: 11px;
    font-weight: 700;
    color: var(--warning);
    text-transform: uppercase;
    letter-spacing: 0.6px;
    margin-bottom: 10px;
}

.approval-row {
    font-size: 13px;
    color: var(--text-muted);
    margin-bottom: 4px;
    display: flex;
    gap: 6px;
}
.approval-row strong { color: var(--text); font-weight: 600; min-width: 60px; }

.approval-actions {
    display: flex;
    gap: 8px;
    margin-top: 14px;
}

.btn-reject {
    flex: 1;
    padding: 9px;
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text-muted);
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 13px;
    font-family: var(--font);
    transition: border-color 0.15s, color 0.15s;
}
.btn-reject:hover  { border-color: var(--error); color: var(--error); }

.btn-approve {
    flex: 1;
    padding: 9px;
    background: var(--success);
    border: none;
    color: #fff;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 13px;
    font-weight: 600;
    font-family: var(--font);
    transition: background 0.15s;
}
.btn-approve:hover { background: #16a34a; }

/* ── Proposal card (self-building connector code review) ─────────────────── */
.proposal-card {
    background: var(--surface2);
    border: 1px solid var(--border);
    border-left: 3px solid #7c3aed;
    border-radius: var(--radius);
    padding: 14px 16px;
    max-width: 560px;
    width: 100%;
    align-self: flex-start;
}

.proposal-title {
    font-size: 11px;
    font-weight: 700;
    color: #a78bfa;
    text-transform: uppercase;
    letter-spacing: 0.6px;
    margin-bottom: 10px;
}

.proposal-meta {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 2px 10px;
    margin-bottom: 12px;
}
.proposal-meta dt {
    font-size: 12px;
    color: var(--text-muted);
    font-weight: 600;
}
.proposal-meta dd {
    font-size: 12px;
    color: var(--text);
    font-family: var(--mono);
}

.proposal-code-toggle {
    font-size: 12px;
    color: #a78bfa;
    background: none;
    border: none;
    cursor: pointer;
    padding: 4px 0;
    margin-bottom: 8px;
    font-family: var(--font);
    text-decoration: underline;
    text-underline-offset: 2px;
}
.proposal-code-toggle:hover { color: #c4b5fd; }

.proposal-code {
    background: #0d0d0d;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 12px;
    overflow-x: auto;
    overflow-y: auto;
    max-height: 360px;
    margin-bottom: 12px;
    display: none;
}
.proposal-code.visible { display: block; }

.proposal-code code {
    font-family: var(--mono);
    font-size: 0.82em;
    color: #e2e8f0;
    white-space: pre;
    display: block;
}

.proposal-warning {
    font-size: 12px;
    color: var(--warning);
    margin-bottom: 10px;
    line-height: 1.4;
}

/* ── Markdown content ─────────────────────────────────────────────────────── */
.message-bubble p  { margin: 0 0 6px; }
.message-bubble p:last-child { margin-bottom: 0; }

.message-bubble h1,
.message-bubble h2,
.message-bubble h3 { margin: 10px 0 4px; line-height: 1.3; }
.message-bubble h1 { font-size: 1.15em; }
.message-bubble h2 { font-size: 1.05em; }
.message-bubble h3 { font-size: 0.95em; text-transform: uppercase; letter-spacing: 0.4px; color: var(--text-muted); }

.message-bubble strong { font-weight: 700; }
.message-bubble em     { font-style: italic; }

.message-bubble code {
    font-family: var(--mono);
    font-size: 0.87em;
    background: rgba(0,0,0,0.35);
    padding: 1px 5px;
    border-radius: 4px;
}
.message.user .message-bubble code { background: rgba(255,255,255,0.2); }

.message-bubble pre {
    background: #0d0d0d;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 12px 14px;
    overflow-x: auto;
    margin: 8px 0;
}
.message-bubble pre code {
    background: transparent;
    padding: 0;
    font-size: 0.84em;
    white-space: pre;
    display: block;
}

.message-bubble ul,
.message-bubble ol {
    padding-left: 18px;
    margin: 4px 0;
}
.message-bubble li { margin: 2px 0; }

.message-bubble blockquote {
    border-left: 3px solid var(--border);
    margin: 8px 0;
    padding: 4px 10px;
    color: var(--text-muted);
    font-style: italic;
}

/* ── Input area ───────────────────────────────────────────────────────────── */
.input-area {
    padding: 10px 12px;
    padding-bottom: calc(10px + env(safe-area-inset-bottom));
    border-top: 1px solid var(--border);
    background: var(--surface);
    flex-shrink: 0;
}

#chat-form {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    background: var(--surface2);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 8px 8px 8px 14px;
    transition: border-color 0.15s;
}
#chat-form:focus-within { border-color: #444; }

#message-input {
    flex: 1;
    background: transparent;
    border: none;
    outline: none;
    color: var(--text);
    font-size: 15px;
    font-family: var(--font);
    line-height: 1.5;
    resize: none;
    min-height: 24px;
    max-height: 160px;
    overflow-y: auto;
}
#message-input::placeholder { color: var(--text-dim); }

.send-btn {
    width: 34px;
    height: 34px;
    background: var(--accent);
    border: none;
    border-radius: 9px;
    color: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: background 0.15s, opacity 0.15s;
}
.send-btn:hover:not(:disabled) { background: var(--accent-hover); }
.send-btn:disabled { background: var(--surface3); cursor: not-allowed; opacity: 0.6; }

/* ── Icon button ──────────────────────────────────────────────────────────── */
.btn-icon {
    width: 36px;
    height: 36px;
    background: transparent;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: background 0.1s, color 0.1s;
}
.btn-icon:hover { background: var(--surface2); color: var(--text); }
.btn-icon.active { color: var(--accent); }

/* ── Scrollbar ────────────────────────────────────────────────────────────── */
::-webkit-scrollbar          { width: 4px; height: 4px; }
::-webkit-scrollbar-track    { background: transparent; }
::-webkit-scrollbar-thumb    { background: var(--surface3); border-radius: 2px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-dim); }

/* ── Utility ──────────────────────────────────────────────────────────────── */
.hidden { display: none !important; }

/* ── Mobile ───────────────────────────────────────────────────────────────── */
@media (max-width: 768px) {
    .sidebar {
        position: fixed;
        inset: 0 auto 0 0;
        z-index: 100;
        width: min(var(--sidebar-w), 85vw);
        transform: translateX(-110%);
        transition: transform 0.25s ease;
    }
    .sidebar.open { transform: translateX(0); }

    .message { max-width: 92%; }
}

@media (min-width: 769px) {
    #menu-btn { display: none; }
}

/* ── Reduced motion ───────────────────────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
    .typing-dot, .tool-spinner { animation: none; }
    * { transition: none !important; }
}

/* ── Insights ─────────────────────────────────────────────────────────────── */
.insights-badge {
    position: absolute;
    top: 4px; right: 4px;
    background: var(--error);
    color: #fff;
    font-size: 10px;
    font-weight: 700;
    min-width: 16px;
    height: 16px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 3px;
    pointer-events: none;
}
#insights-btn { position: relative; }

.insights-drawer {
    position: absolute;
    top: var(--header-h);
    right: 0; left: 0;
    z-index: 50;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    max-height: 55vh;
    display: flex;
    flex-direction: column;
}
.insights-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 16px;
    font-size: 13px;
    font-weight: 600;
    color: var(--text-muted);
    border-bottom: 1px solid var(--border);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}
.insights-list {
    overflow-y: auto;
    padding: 8px 0;
}
.insights-empty {
    padding: 20px 16px;
    text-align: center;
    color: var(--text-dim);
    font-size: 13px;
}
.insight-card {
    padding: 10px 16px;
    border-bottom: 1px solid var(--border);
    cursor: default;
}
.insight-card:last-child { border-bottom: none; }
.insight-meta {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 4px;
}
.insight-priority {
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    padding: 2px 6px;
    border-radius: 4px;
}
.insight-priority.high   { background: rgba(239,68,68,0.15); color: #ef4444; }
.insight-priority.medium { background: rgba(245,158,11,0.15); color: #f59e0b; }
.insight-priority.low    { background: rgba(100,116,139,0.15); color: #64748b; }
.insight-title {
    font-size: 13px;
    font-weight: 600;
    color: var(--text);
}
.insight-detail {
    font-size: 12px;
    color: var(--text-muted);
    line-height: 1.5;
    margin-bottom: 2px;
}
.insight-action {
    font-size: 12px;
    color: var(--accent);
    font-style: italic;
}
.insight-time {
    font-size: 11px;
    color: var(--text-dim);
    margin-left: auto;
}
