:root {
    --bg: #0f1115;
    --panel: #171a21;
    --panel-2: #1e222b;
    --border: #2a2f3a;
    --text: #e6e8eb;
    --text-dim: #9aa1ac;
    --accent: #6c5ce7;
    --accent-hover: #7d70eb;
    --danger: #e35d6a;
    --success: #45c98a;
    --radius: 10px;
}

* { box-sizing: border-box; }

html, body {
    height: 100%;
    margin: 0;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    overflow: hidden; /* the app manages its own scroll regions */
}

/* Auth pages */
.auth-page {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
}
.auth-card {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 32px;
    width: 360px;
}
.auth-card h1 { margin: 0 0 4px; font-size: 22px; }
.subtitle { color: var(--text-dim); margin: 0 0 20px; font-size: 14px; }
.auth-card label { display: block; margin-bottom: 14px; font-size: 13px; color: var(--text-dim); }
.auth-card input {
    width: 100%; margin-top: 6px; padding: 10px 12px;
    background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px;
    color: var(--text); font-size: 14px;
}
.auth-card button[type="submit"] {
    width: 100%; padding: 11px; border: none; border-radius: 8px;
    background: var(--accent); color: white; font-size: 14px; font-weight: 600; cursor: pointer;
}
.auth-card button[type="submit"]:hover { background: var(--accent-hover); }
.inline-form { margin-top: 10px; text-align: center; }
.link-button { background: none; border: none; color: var(--text-dim); font-size: 13px; cursor: pointer; text-decoration: underline; }
.alert { padding: 10px 12px; border-radius: 8px; font-size: 13px; margin-bottom: 16px; }
.alert-error { background: rgba(227,93,106,0.15); color: var(--danger); border: 1px solid rgba(227,93,106,0.3); }
.alert-success { background: rgba(69,201,138,0.15); color: var(--success); border: 1px solid rgba(69,201,138,0.3); }

/* App shell */
.app-shell {
    display: grid;
    grid-template-columns: 240px 1fr 260px;
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
}
.sidebar {
    background: var(--panel);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    padding: 16px;
    min-height: 0;
    overflow: hidden;
}
.sidebar-header h2 { margin: 0; font-size: 16px; }
.user-tag { color: var(--text-dim); font-size: 12px; }
.full-width { width: 100%; }
.btn-primary {
    background: var(--accent); color: white; border: none; border-radius: 8px;
    padding: 10px; font-size: 13px; font-weight: 600; cursor: pointer; margin: 14px 0;
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-secondary {
    background: var(--panel-2); color: var(--text); border: 1px solid var(--border); border-radius: 8px;
    padding: 10px 14px; font-size: 13px; cursor: pointer;
}
.project-list { flex: 1; min-height: 0; overflow-y: auto; display: flex; flex-direction: column; gap: 4px; }
.project-item {
    display: block; padding: 9px 10px; border-radius: 8px; color: var(--text-dim);
    text-decoration: none; font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.project-item:hover { background: var(--panel-2); }
.project-item.active { background: var(--panel-2); color: var(--text); font-weight: 600; }
.shared-tag { display: block; font-size: 10px; color: var(--text-dim); font-weight: 400; }
.logout-link { color: var(--text-dim); font-size: 12px; text-decoration: none; padding-top: 12px; border-top: 1px solid var(--border); margin-top: 12px; }
.empty-hint { color: var(--text-dim); font-size: 12px; }

/* Chat panel */
.chat-panel { display: flex; flex-direction: column; height: 100%; min-height: 0; min-width: 0; overflow: hidden; }
.chat-header { padding: 16px 20px; border-bottom: 1px solid var(--border); flex: 0 0 auto; display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.chat-header h3 { margin: 0; font-size: 15px; }
.project-desc { margin: 4px 0 0; color: var(--text-dim); font-size: 12px; }
.role-badge { font-size: 11px; color: var(--text-dim); background: var(--panel-2); border: 1px solid var(--border); border-radius: 20px; padding: 5px 12px; white-space: nowrap; }
.readonly-notice { padding: 14px 20px; color: var(--text-dim); font-size: 13px; border-top: 1px solid var(--border); flex: 0 0 auto; }
.chat-log { flex: 1 1 auto; min-height: 0; overflow-y: auto; overflow-x: hidden; padding: 20px; display: flex; flex-direction: column; gap: 16px; }
.chat-msg { max-width: 85%; min-width: 0; }
.msg-user { align-self: flex-end; text-align: right; }
.msg-role { font-size: 11px; color: var(--text-dim); margin-bottom: 4px; display: flex; align-items: center; gap: 6px; }
.speak-btn {
    background: none; border: none; color: var(--text-dim); cursor: pointer; font-size: 12px;
    padding: 0; line-height: 1; opacity: 0.7;
}
.speak-btn:hover, .speak-btn.speaking { opacity: 1; color: var(--accent); }
#micBtn.listening { background: var(--danger); border-color: var(--danger); color: white; animation: pulse 1.2s infinite; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.6; } }
.msg-content { background: var(--panel-2); padding: 10px 14px; border-radius: 12px; font-size: 14px; line-height: 1.5; text-align: left; overflow-wrap: anywhere; word-break: break-word; }
.msg-user .msg-content { background: var(--accent); color: white; }
.code-block { background: #0b0d12; border: 1px solid var(--border); border-radius: 8px; padding: 12px; overflow-x: auto; max-width: 100%; font-size: 12.5px; margin: 8px 0; }
.code-block code { font-family: "SF Mono", Consolas, monospace; white-space: pre; }
.chat-input-row { display: flex; gap: 10px; padding: 16px 20px; border-top: 1px solid var(--border); flex: 0 0 auto; }
.chat-input-row textarea {
    flex: 1; min-width: 0; resize: none; padding: 10px 12px; border-radius: 8px;
    border: 1px solid var(--border); background: var(--panel-2); color: var(--text); font-size: 14px;
}
.chat-input-row button {
    padding: 0 20px; border: none; border-radius: 8px; background: var(--accent); color: white;
    font-weight: 600; cursor: pointer; flex: 0 0 auto;
}
.chat-input-row button:disabled { opacity: 0.5; cursor: default; }
.empty-state { margin: auto; text-align: center; color: var(--text-dim); }

/* Files panel */
.files-panel { background: var(--panel); border-left: 1px solid var(--border); padding: 16px; overflow-y: auto; min-height: 0; }
.files-panel h3 { margin: 0 0 12px; font-size: 13px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--text-dim); }
.file-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.file-item { display: flex; justify-content: space-between; align-items: center; padding: 8px 10px; border-radius: 8px; background: var(--panel-2); font-size: 12.5px; gap: 8px; }
.file-item a { color: var(--text); text-decoration: none; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.file-item a:hover { color: var(--accent); }
.file-actions { display: flex; align-items: center; gap: 6px; flex-shrink: 0; }
.file-size { color: var(--text-dim); font-size: 11px; white-space: nowrap; }
.file-add-btn, .file-del-btn {
    background: none; border: none; color: var(--text-dim); cursor: pointer; font-size: 12px; padding: 2px; line-height: 1;
}
.file-add-btn:hover { color: var(--success); }
.file-del-btn:hover { color: var(--danger); }

/* Modal */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,0.6); display: flex; align-items: center; justify-content: center; }
.modal.hidden { display: none; }
.modal-box { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 24px; width: 340px; }
.modal-box h3 { margin: 0 0 16px; }
.modal-box label { display: block; margin-bottom: 12px; font-size: 13px; color: var(--text-dim); }
.modal-box input, .modal-box textarea, .modal-box select {
    width: 100%; margin-top: 6px; padding: 9px 10px; background: var(--panel-2);
    border: 1px solid var(--border); border-radius: 8px; color: var(--text); font-size: 13px;
}
.member-list { list-style: none; margin: 0 0 16px; padding: 0; display: flex; flex-direction: column; gap: 6px; max-height: 160px; overflow-y: auto; }
.member-item { display: flex; justify-content: space-between; align-items: center; background: var(--panel-2); border-radius: 8px; padding: 8px 10px; font-size: 12.5px; gap: 8px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 6px; }

/* Mobile topbar (hidden on desktop) */
.mobile-topbar {
    display: none;
    align-items: center;
    justify-content: space-between;
    padding: 10px 14px;
    background: var(--panel);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 30;
}
.mobile-title { font-size: 14px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 60%; }
.icon-btn {
    background: var(--panel-2); border: 1px solid var(--border); color: var(--text);
    border-radius: 8px; width: 38px; height: 38px; font-size: 16px; cursor: pointer;
    display: flex; align-items: center; justify-content: center; flex-shrink: 0;
}
.chat-input-row .icon-btn { width: 42px; height: 42px; }

/* Backdrop for mobile drawers */
.backdrop { position: fixed; inset: 0; background: rgba(0,0,0,0.55); z-index: 40; display: none; }
.backdrop.show { display: block; }

/* Attachment chips */
.attachment-chips { display: flex; flex-wrap: wrap; gap: 8px; padding: 0 20px; }
.attachment-chips:empty { display: none; }
.chip {
    display: inline-flex; align-items: center; gap: 6px; background: var(--panel-2);
    border: 1px solid var(--border); border-radius: 20px; padding: 6px 10px; font-size: 12px; color: var(--text-dim);
}
.chip-remove { background: none; border: none; color: var(--text-dim); font-size: 15px; cursor: pointer; line-height: 1; padding: 0; }
.chip-remove:hover { color: var(--danger); }
.hidden { display: none !important; }

@media (max-width: 900px) {
    .mobile-topbar { display: flex; }

    .app-shell {
        grid-template-columns: 1fr;
        height: calc(100vh - 57px); /* minus topbar */
        height: calc(100dvh - 57px);
    }

    .sidebar, .files-panel {
        display: flex;
        position: fixed;
        top: 57px;
        bottom: 0;
        width: 82%;
        max-width: 300px;
        z-index: 50;
        transition: transform 0.25s ease;
    }
    .sidebar { left: 0; transform: translateX(-105%); border-right: 1px solid var(--border); }
    .sidebar.open { transform: translateX(0); }
    .files-panel { right: 0; transform: translateX(105%); border-left: 1px solid var(--border); }
    .files-panel.open { transform: translateX(0); }

    .chat-panel { height: 100%; }
}
