:root {
    --bg: #f6f8fa;
    --surface: #ffffff;
    --surface-2: #eef1f4;
    --border: #d0d7de;
    --text: #1f2328;
    --text-muted: #656d76;
    --accent: #0969da;
    --accent-dim: #0969da;
    --on-accent: #ffffff;
    --success: #1a7f37;
    --danger: #cf222e;
    --warning: #9a6700;
    --human: #0969da;
    --session: #8250df;
    --radius: 6px;
    --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif;
    --mono: 'SF Mono', SFMono-Regular, Consolas, 'Liberation Mono', Menlo, monospace;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
    font-family: var(--font);
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
    height: 100vh;
}

#app { height: 100%; }

.view[hidden] { display: none !important; }

.view {
    height: 100%;
    display: flex;
    flex-direction: column;
}

/* Buttons + inputs */
button {
    padding: 0.45rem 0.95rem;
    background: var(--accent-dim);
    color: var(--on-accent);
    border: none;
    border-radius: var(--radius);
    cursor: pointer;
    font-size: 0.875rem;
    font-weight: 500;
}
button:hover { opacity: 0.9; }
button.secondary { background: transparent; border: 1px solid var(--border); color: var(--text); }
button.small { padding: 0.25rem 0.7rem; font-size: 0.78rem; }
button.ghost { background: transparent; color: var(--text-muted); }
button.primary { background: var(--accent-dim); }
button.danger { background: var(--danger); }

input[type="text"], input[type="password"], input[type="url"], textarea, select {
    padding: 0.45rem 0.65rem;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text);
    font-size: 0.875rem;
    font-family: var(--font);
    width: 100%;
}
input:focus, textarea:focus, select:focus { outline: none; border-color: var(--accent); }
textarea { resize: vertical; font-family: var(--font); }

/* Login */
.login-card {
    max-width: 360px;
    margin: auto;
    padding: 2rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}
.login-card h1 { font-size: 1.5rem; }
.subtitle { color: var(--text-muted); font-size: 0.875rem; margin-bottom: 1.5rem; }
form { display: flex; flex-direction: column; gap: 0.75rem; }
.error { color: var(--danger); font-size: 0.8rem; margin-top: 0.5rem; }

/* Header */
header {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.6rem 0.9rem;
    border-bottom: 1px solid var(--border);
    background: var(--surface);
}
header h1, header h2 { font-size: 1.05rem; flex: 1; }
.header-right {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    color: var(--text-muted);
    font-size: 0.78rem;
}
#layout-select { width: auto; font-size: 0.78rem; padding: 0.25rem 0.5rem; }

/* Workspaces list */
#ws-list {
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    overflow-y: auto;
    flex: 1;
}
.ws-card {
    padding: 0.75rem 1rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.ws-card:hover { border-color: var(--accent); }
.ws-card .ws-meta { text-align: right; font-size: 0.75rem; color: var(--text-muted); }
.ws-card h3 { font-size: 0.95rem; }
.ws-card .desc { font-size: 0.8rem; color: var(--text-muted); }
#no-ws { text-align: center; color: var(--text-muted); padding: 3rem; }

/* Workspace body */
#workspace-body {
    flex: 1;
    display: grid;
    grid-template-columns: 220px 1fr;
    overflow: hidden;
}
#workspace-body.with-info {
    grid-template-columns: 220px 1fr 280px;
}
#participants, #info-panel {
    background: var(--surface);
    border-right: 1px solid var(--border);
    padding: 0.75rem 1rem;
    overflow-y: auto;
}
#info-panel {
    border-right: none;
    border-left: 1px solid var(--border);
}
#participants h3, #info-panel h3 {
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-muted);
    margin-bottom: 0.4rem;
}
#participants .section { margin-bottom: 1rem; }
.section-title {
    font-size: 0.65rem;
    color: var(--text-muted);
    text-transform: uppercase;
    margin-bottom: 0.25rem;
}
#humans-list, #sessions-list, #members-list {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}
#humans-list li, #sessions-list li, #members-list li {
    font-size: 0.82rem;
    padding: 0.2rem 0.4rem;
    border-radius: 4px;
    cursor: pointer;
}
#humans-list li:hover, #sessions-list li:hover { background: var(--bg); }
#humans-list li::before { content: '● '; color: var(--human); }
#sessions-list li::before { content: '◆ '; color: var(--session); }
#members-list li::before { content: '• '; color: var(--text-muted); }
.policy-tag {
    display: inline-block;
    font-size: 0.65rem;
    padding: 0.05rem 0.4rem;
    border-radius: 8px;
    margin-left: 0.4rem;
    border: 1px solid var(--border);
    color: var(--text-muted);
}
.policy-tag.open { color: var(--success); border-color: var(--success); }
.policy-tag.info-only { color: var(--text-muted); }
.policy-tag.approval { color: var(--warning); border-color: var(--warning); }

#info-panel dl { font-size: 0.82rem; }
#info-panel dt {
    font-size: 0.7rem;
    color: var(--text-muted);
    text-transform: uppercase;
    margin-top: 0.6rem;
}
#info-panel dd { word-break: break-all; }
#admin-actions { margin-top: 1rem; padding-top: 1rem; border-top: 1px solid var(--border); }
#admin-actions h3 { margin-top: 1rem; }
#admin-actions form { gap: 0.5rem; margin-bottom: 0.4rem; }
#admin-actions label { display: flex; flex-direction: column; font-size: 0.72rem; color: var(--text-muted); gap: 0.2rem; }
#admin-actions label.inline { flex-direction: row; align-items: center; gap: 0.4rem; }
#admin-actions label.inline input { width: auto; }
.hint { font-size: 0.7rem; color: var(--text-muted); }

/* Conversation */
#conversation {
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: var(--bg);
}

/* id rules set display:flex, which beats the [hidden] attribute — force it. */
#messages[hidden], #search-results[hidden] { display: none !important; }

/* Search */
#search-bar {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.4rem 0.9rem;
    border-bottom: 1px solid var(--border);
    background: var(--surface);
}
#search-input { flex: 1; }
#search-count { font-size: 0.72rem; color: var(--text-muted); white-space: nowrap; }
#search-results {
    flex: 1;
    overflow-y: auto;
    padding: 0.5rem 1rem 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.2rem;
}
.search-result {
    width: 100%;
    max-width: 820px;
    padding: 0.3rem 0.5rem;
    border-radius: var(--radius);
    cursor: pointer;
}
.search-result:hover { background: var(--surface-2); }
.search-result .meta { display: flex; gap: 0.4rem; align-items: baseline; margin-bottom: 0.1rem; font-size: 0.72rem; color: var(--text-muted); }
.search-result .marker { font-size: 0.6rem; }
.search-result.human .marker, .search-result.human .sender { color: var(--human); }
.search-result.session .marker, .search-result.session .sender { color: var(--session); }
.search-result .sender { font-weight: 600; }
.search-result .ts { margin-left: auto; }
.search-result .body { font-size: 0.85rem; white-space: pre-wrap; word-wrap: break-word; padding-left: 0.95rem; }
.search-empty { color: var(--text-muted); padding: 1rem; font-size: 0.85rem; }
mark { background: #fff3a3; color: inherit; border-radius: 2px; padding: 0 1px; }

/* Reverse history search bar (Ctrl+R) */
#rsearch-bar {
    font-family: var(--mono);
    font-size: 0.75rem;
    color: var(--text-muted);
    padding: 0.15rem 0.2rem 0.35rem;
    display: flex;
    gap: 0.3rem;
    align-items: baseline;
}
#rsearch-bar .rs-query { color: var(--text); }
#rsearch-bar .rs-none { color: var(--danger); }
#rsearch-bar .rs-count { color: var(--text-muted); }

/* Flash a message when jumped to from search */
.msg.flash { animation: flash 1.3s ease-out; }
@keyframes flash {
    from { box-shadow: 0 0 0 2px var(--accent); }
    to { box-shadow: 0 0 0 2px transparent; }
}
#messages {
    flex: 1;
    overflow-y: auto;
    padding: 0.75rem 1rem 1.25rem;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 0.15rem;
}

/* Single-column feed: every message left-aligned in one readable column.
   Sender identity is carried by a colored marker + name, not by side. */
.msg {
    width: 100%;
    max-width: 820px;
    padding: 0.3rem 0.5rem;
    border-radius: var(--radius);
}
.msg.self { background: var(--surface-2); }   /* subtle "mine" — no side flip */
.msg.system {
    align-self: center;
    background: none;
    color: var(--text-muted);
    font-size: 0.75rem;
    padding: 0.15rem;
}
.msg.rejected { opacity: 0.55; }
.msg .meta {
    display: flex;
    gap: 0.4rem;
    align-items: baseline;
    margin-bottom: 0.1rem;
    font-size: 0.72rem;
    color: var(--text-muted);
}
.msg .marker { font-size: 0.6rem; }
.msg.human .marker { color: var(--human); }
.msg.session .marker { color: var(--session); }
.msg .sender { font-weight: 600; }
.msg.human .sender { color: var(--human); }
.msg.session .sender { color: var(--session); }
.msg.self .sender::after { content: ' · you'; font-weight: 400; color: var(--text-muted); }
.msg .addr { color: var(--text-muted); }
.msg .type-tag { font-size: 0.66rem; }
.msg .ts { margin-left: auto; }
.msg .body {
    font-size: 0.9rem;
    white-space: pre-wrap;
    word-wrap: break-word;
    padding-left: 0.95rem;   /* align body under the sender name, past the marker */
}

/* ── Conversation layout variants (per-user preference) ──────────────────
   Default (no class) = "feed". Switching is pure CSS — no DOM change. */
/* indent: your messages keep the left rail, everyone else's nudges right */
#messages.layout-indent .msg:not(.self):not(.system) { margin-left: 28px; }
/* wide: drop the readable width cap, use the whole pane */
#messages.layout-wide .msg { max-width: none; }
/* bubbles: classic left (others) / right (you), kept within a centered band
   so nothing flies to the screen edges on a wide monitor */
#messages.layout-bubbles { padding-inline: max(1rem, calc((100% - 900px) / 2)); }
#messages.layout-bubbles .msg {
    max-width: 70%;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.5rem 0.7rem;
}
#messages.layout-bubbles .msg .body { padding-left: 0; }
#messages.layout-bubbles .msg.human,
#messages.layout-bubbles .msg.session { align-self: flex-start; }
#messages.layout-bubbles .msg.self { align-self: flex-end; background: var(--surface-2); }
#messages.layout-bubbles .msg.system {
    align-self: center; max-width: 100%; background: none; border: none;
}

/* Event (inline tool use) */
.event {
    align-self: flex-start;
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 0.72rem;
    font-family: var(--mono);
    padding: 0.1rem 0.5rem 0.1rem 1.45rem;   /* indent into the feed column */
}

/* Compose */
#compose {
    border-top: 1px solid var(--border);
    background: var(--surface);
    padding: 0.55rem 0.9rem 0.75rem;
}
#compose-meta {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 0.4rem;
}
#compose-to.addr-chip {
    background: var(--accent-dim);
    color: var(--text);
    padding: 0.15rem 0.5rem;
    border-radius: 10px;
    font-size: 0.72rem;
    font-family: var(--mono);
}
#msg-input {
    padding: 0.55rem 0.75rem;
    width: 100%;
}
#mention-popup {
    position: absolute;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.25rem;
    box-shadow: 0 4px 14px rgba(0,0,0,0.4);
    max-height: 240px;
    overflow-y: auto;
    z-index: 200;
}
#mention-popup .item {
    padding: 0.3rem 0.6rem;
    font-size: 0.82rem;
    cursor: pointer;
    border-radius: 4px;
}
#mention-popup .item:hover, #mention-popup .item.active { background: var(--accent-dim); }
#mention-popup .item .role { color: var(--text-muted); font-size: 0.7rem; margin-left: 0.4rem; }

/* Dialog */
.dialog[hidden] { display: none !important; }
.dialog {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.6);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
}
.dialog-content {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.5rem;
    max-width: 480px;
    width: 90%;
}
.dialog-content h3 { margin-bottom: 0.75rem; }
.dialog-content label { display: flex; flex-direction: column; font-size: 0.78rem; color: var(--text-muted); gap: 0.2rem; }
.dialog-actions { display: flex; gap: 0.5rem; justify-content: flex-end; margin-top: 0.75rem; }

/* Responsive */
@media (max-width: 800px) {
    #workspace-body { grid-template-columns: 1fr; }
    #workspace-body.with-info { grid-template-columns: 1fr; }
    #participants, #info-panel { display: none; }
}
