/* ── primitives.css ──────────────────────────
   Buttons, forms, toasts, popups, toggles, badges
   ──────────────────────────────────────────── */

/* ── Buttons ─────────────────────────────── */

button { font-family: var(--font); cursor: pointer; }
.btn-primary {
    background: var(--accent); color: var(--text-on-color); border: none;
    padding: 8px 16px; border-radius: var(--radius);
    font-weight: 500; font-size: 13px;
    transition: background var(--transition);
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-full { width: 100%; }
.btn-small {
    background: var(--bg-raised); border: 1px solid var(--border);
    color: var(--text-2); padding: 6px 12px; border-radius: var(--radius);
    font-size: 12px; transition: all var(--transition);
}
.btn-small:hover { background: var(--bg-hover); color: var(--text); }
.btn-small.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--text-on-color); }
.btn-danger { background: var(--danger); color: var(--text-on-color); border: none; border-radius: var(--radius); }


/* ── Forms ────────────────────────────────── */

/* Base form element styling — ensures selects/inputs match the dark theme
   even when not wrapped in .form-group (e.g. role config, inline toolbars) */
select, input[type="text"], input[type="email"], input[type="password"],
input[type="number"], input[type="search"], textarea {
    background: var(--bg-raised); border: 1px solid var(--border);
    color: var(--text); padding: 8px 12px; border-radius: var(--radius);
    font-family: var(--font); font-size: 13px;
    transition: border-color var(--transition);
}
select:focus, input[type="text"]:focus, input[type="email"]:focus,
input[type="password"]:focus, textarea:focus {
    outline: none; border-color: var(--accent);
}
select option { background: var(--bg-surface); color: var(--text); }

.form-row { display: flex; gap: 0.75rem; }
.form-row .form-group { flex: 1; }
.form-hint { font-weight: 400; color: var(--text-3); margin-left: 4px; }
.range-input {
    width: 100%; height: 4px; -webkit-appearance: none; appearance: none;
    background: var(--border); border-radius: 2px; outline: none;
    margin: 8px 0 4px; cursor: pointer;
}
.range-input::-webkit-slider-thumb {
    -webkit-appearance: none; width: 16px; height: 16px; border-radius: 50%;
    background: var(--accent); border: 2px solid var(--bg-surface); cursor: pointer;
}
.range-input::-moz-range-thumb {
    width: 16px; height: 16px; border-radius: 50%;
    background: var(--accent); border: 2px solid var(--bg-surface); cursor: pointer;
}
.range-labels { display: flex; justify-content: space-between; font-size: 11px; color: var(--text-3); }
.checkbox-label { display: flex; align-items: center; gap: 8px; font-size: 14px; cursor: pointer; margin: 0.5rem 0; color: var(--text-2); }


/* ── Toast ────────────────────────────────── */

.toast-container { position: fixed; bottom: 1rem; left: 50%; transform: translateX(-50%); z-index: 10000; display: flex; flex-direction: column; gap: 0.5rem; }
.toast {
    background: var(--bg-surface); border: 1px solid var(--border);
    border-radius: var(--radius); padding: 8px 16px; font-size: 13px;
    display: flex; align-items: center; gap: 8px;
    animation: toast-in 0.25s ease; box-shadow: 0 4px 12px rgba(0,0,0,0.3);
}
.toast.error { border-color: var(--danger); }
.toast.warning { border-color: var(--warning); }
.toast.success { border-color: var(--success); }
.toast button { background: none; border: none; color: var(--text-3); cursor: pointer; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }


/* ── Mode Selector (v0.21.3) ───────────── */

/* Surface region wrappers must participate in parent flex layouts */
[data-surface-region="sidebar-content"] {
    flex: 1; min-height: 0;
    display: flex; flex-direction: column;
}

.mode-selector {
    display: flex; gap: 2px; padding: 6px 8px;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}
.mode-btn {
    display: flex; align-items: center; gap: 6px;
    padding: 4px 10px; height: 28px; border: none;
    border-radius: var(--radius); background: none;
    color: var(--text-3); cursor: pointer; font-size: 12px;
    transition: background var(--transition), color var(--transition);
    white-space: nowrap;
}
.mode-btn svg { flex-shrink: 0; }
.mode-btn-label { font-weight: 500; }
.mode-btn:hover { background: var(--bg-hover); color: var(--text); }
.mode-btn.active { background: var(--bg-active); color: var(--accent); }
.sidebar.collapsed .mode-selector { justify-content: center; }
.sidebar.collapsed .mode-btn-label { display: none; }
.sidebar.collapsed .mode-btn { padding: 4px; width: 28px; justify-content: center; }


/* ── REPL Console (v0.21.3) ────────────── */

.repl-toolbar {
    display: flex; gap: 0.5rem; padding: 6px 12px;
    border-bottom: 1px solid var(--border); font-size: 11px;
}
.repl-output {
    flex: 1; overflow-y: auto; padding: 6px;
    font-family: var(--mono); font-size: 12px; line-height: 1.6;
}
.repl-input-wrap {
    display: flex; align-items: flex-start; gap: 6px;
    padding: 8px 12px; border-top: 1px solid var(--border);
    background: var(--bg-raised);
}
.repl-prompt {
    font-family: var(--mono); font-size: 12px; line-height: 1.6;
    color: var(--accent); font-weight: bold;
    flex-shrink: 0; padding-top: 2px;
}
.repl-input {
    flex: 1; background: none; border: none; outline: none;
    font-family: var(--mono); font-size: 12px; line-height: 1.6;
    color: var(--text); resize: none; overflow: hidden;
    min-height: 20px;
}
.repl-entry { padding: 2px 6px; }
.repl-entry-input pre { color: var(--text-2); margin: 0; white-space: pre-wrap; word-break: break-all; }
.repl-entry-result { color: var(--text); }
.repl-entry-error { color: var(--danger, #e55); }
.repl-entry-info { color: var(--text-3); font-size: 11px; }

.repl-error-msg { margin: 0; white-space: pre-wrap; font-weight: 500; }
.repl-error-stack { margin: 2px 0 0 0; color: var(--text-3); font-size: 11px; white-space: pre-wrap; }
.repl-stack-toggle {
    background: none; border: none; color: var(--text-3);
    cursor: pointer; font-size: 10px; font-family: var(--mono);
    padding: 0; margin-top: 2px;
}
.repl-stack-toggle:hover { color: var(--text-2); }

.repl-elapsed { color: var(--text-3); font-size: 10px; margin-left: 8px; }

/* REPL value types */
.repl-null, .repl-undefined { color: var(--text-3); margin: 0; }
.repl-string { color: #98c379; margin: 0; white-space: pre-wrap; word-break: break-all; }
.repl-number { color: #d19a66; margin: 0; }
.repl-boolean { color: #56b6c2; margin: 0; }
.repl-function { color: #c678dd; margin: 0; font-style: italic; }
.repl-symbol { color: #e5c07b; margin: 0; }
.repl-dom { color: #61afef; margin: 0; }
.repl-object { color: var(--text); margin: 0; }
.repl-json { margin: 0; white-space: pre-wrap; word-break: break-all; color: var(--text); font-size: 11px; }

.repl-collapsible { display: inline; }
.repl-json-toggle {
    background: none; border: none; color: var(--text-2);
    cursor: pointer; font-family: var(--mono); font-size: 12px;
    padding: 0;
}
.repl-json-toggle:hover { color: var(--accent); }


/* ── Popup Menu (shared base) ───────────── */

.popup-menu {
    display: none; position: absolute; z-index: 200;
    background: var(--bg-raised); border: 1px solid var(--border-light);
    border-radius: var(--radius); padding: 4px;
    box-shadow: 0 4px 16px rgba(0,0,0,0.4);
    min-width: 160px;
}
.popup-menu.open { display: block; }
.popup-menu-item {
    display: flex; align-items: center; gap: 10px;
    padding: 8px 12px; border-radius: var(--radius);
    background: none; border: none; color: var(--text-2);
    cursor: pointer; font-size: 13px; width: 100%;
    transition: background var(--transition), color var(--transition);
}
.popup-menu-item:hover { background: var(--bg-hover); color: var(--text); }
.popup-menu-item.disabled { opacity: 0.4; pointer-events: none; }
.popup-menu-danger:hover { color: var(--danger); }
.popup-menu-icon { flex-shrink: 0; display: flex; align-items: center; }
.popup-menu-hint { margin-left: auto; font-size: 11px; color: var(--text-3); }
.popup-menu-divider { height: 1px; background: var(--border); margin: 4px 8px; }

/* ── Tools Toggle ────────────────────────── */

/* ── Popup (shared base for tools/KB popups) ── */
.popup {
    display: none; position: absolute;
    background: var(--bg-raised); border: 1px solid var(--border-light);
    border-radius: var(--radius-lg); padding: 4px;
    box-shadow: var(--shadow-lg); z-index: 300;
}
.popup.open { display: block; }

.tools-toggle-wrap { position: relative; display: flex; align-items: center; }
.tools-toggle-btn.has-disabled { color: var(--text-3); }
.tools-toggle-btn.has-disabled::after {
    content: ''; position: absolute; top: 4px; right: 4px;
    width: 6px; height: 6px; border-radius: 50%;
    background: var(--warning);
}
.tools-popup { bottom: 100%; left: 0; margin-bottom: 8px; min-width: 210px; }
.tools-popup-category, .tools-popup-tool {
    display: flex; align-items: center; gap: 10px;
    padding: 8px 12px; border-radius: var(--radius);
    cursor: pointer; font-size: 13px; color: var(--text-2);
    transition: background var(--transition);
}
.tools-popup-category:hover, .tools-popup-tool:hover { background: var(--bg-hover); }
.tools-popup-tool { padding-left: 36px; font-size: 12px; } /* indented children */
/* Toggle switch */
.tools-popup-switch {
    width: 32px; height: 18px; border-radius: 9px;
    background: var(--border); flex-shrink: 0;
    position: relative; cursor: pointer;
    transition: background 0.2s;
}
.tools-popup-switch-knob {
    position: absolute; top: 2px; left: 2px;
    width: 14px; height: 14px; border-radius: 50%;
    background: var(--text-3);
    transition: transform 0.2s, background 0.2s;
}
.tools-popup-switch.on { background: var(--accent); }
.tools-popup-switch.on .tools-popup-switch-knob {
    transform: translateX(14px); background: var(--text-on-color);
}
.tools-popup-switch.on.partial { background: var(--accent-dim); }
.tools-popup-switch.on.partial .tools-popup-switch-knob {
    transform: translateX(7px); background: var(--accent);
}
.tools-popup-label { flex: 1; }
.tools-popup-expand {
    font-size: 14px; color: var(--text-3); cursor: pointer;
    transition: transform var(--transition); padding: 0 2px;
    user-select: none;
}
.tools-popup-expand.open { transform: rotate(90deg); }

/* ── Knowledge Bases ────────────────────── */

/* Toggle button in input bar */
.kb-toggle-wrap {
    position: relative; display: flex; align-items: center;
}
.kb-toggle-btn.has-active {
    color: var(--accent);
}

/* Channel KB popup */
.kb-popup {
    bottom: 100%; left: 0; margin-bottom: 8px;
    min-width: 240px; max-height: 300px; overflow-y: auto;
}
.kb-popup-header {
    padding: 8px 12px 4px; font-size: 12px; font-weight: 600;
    color: var(--text-2); text-transform: uppercase; letter-spacing: 0.5px;
}
.kb-popup-item {
    display: flex; align-items: center; gap: 8px;
    padding: 6px 12px; cursor: pointer;
    transition: background var(--transition);
}
.kb-popup-item:hover { background: var(--bg-hover); }
.kb-popup-item .kb-popup-check {
    width: 16px; height: 16px; border-radius: 3px;
    border: 1.5px solid var(--border); flex-shrink: 0;
    display: flex; align-items: center; justify-content: center;
    transition: all var(--transition);
}
.kb-popup-item.enabled .kb-popup-check {
    background: var(--accent); border-color: var(--accent);
}
.kb-popup-item.enabled .kb-popup-check::after {
    content: '✓'; font-size: 11px; color: var(--text-on-color);
}
.kb-popup-label { display: flex; flex-direction: column; min-width: 0; }
.kb-popup-name {
    font-size: 13px; color: var(--text);
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.kb-popup-meta { font-size: 11px; color: var(--text-3); }

/* KB management panel (settings tab) */
.kb-manage-header {
    display: flex; align-items: center; gap: 8px;
    margin-bottom: 12px;
}
.kb-manage-header h3 { flex: 1; margin: 0; font-size: 15px; }
.kb-manage-summary { font-size: 11px; color: var(--text-3); margin-bottom: 10px; padding: 0 2px; }
.kb-manage-list { display: flex; flex-direction: column; gap: 2px; }
.kb-manage-item {
    display: flex; align-items: center; gap: 8px;
    padding: 8px 10px; border-radius: var(--radius);
    background: var(--bg-raised); transition: background var(--transition);
}
.kb-manage-item:hover { background: var(--bg-hover); }
.kb-manage-info { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.kb-manage-name { font-size: 13px; font-weight: 500; color: var(--text); }
.kb-manage-desc { font-size: 12px; color: var(--text-3); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.kb-manage-stats { font-size: 11px; color: var(--text-3); }
.kb-manage-desc-block { font-size: 12px; color: var(--text-3); margin-bottom: 12px; }
.kb-manage-actions { display: flex; gap: 4px; flex-shrink: 0; }
.kb-doc-item[data-doc-status="pending"] .kb-manage-name,
.kb-doc-item[data-doc-status="extracting"] .kb-manage-name,
.kb-doc-item[data-doc-status="chunking"] .kb-manage-name,
.kb-doc-item[data-doc-status="embedding"] .kb-manage-name {
    opacity: 0.7;
}


/* ── Shared Form Controls ────────────────── */

.form-group { margin-bottom: 14px; }
.form-group label {
    display: block; font-size: 12px; font-weight: 500;
    color: var(--text-2); margin-bottom: 5px;
}
.form-group input, .form-group select, .form-group textarea {
    width: 100%; outline: none;
}
.form-group input:focus, .form-group select:focus, .form-group textarea:focus {
    border-color: var(--accent);
}
.form-group input:disabled { opacity: 0.6; }
.form-group input[type="range"] { border: none; background: none; }
.form-group input[type="range"]:focus { box-shadow: none; }
.form-group input[type="checkbox"] { width: auto; accent-color: var(--accent); }

.toggle-group {
    display: flex; gap: 1px; padding: 3px;
    background: var(--bg); border-radius: 10px;
    border: 1px solid var(--border); width: fit-content;
}
.toggle-btn {
    padding: 6px 14px; border: none; border-radius: 7px;
    background: transparent; color: var(--text-3);
    cursor: pointer; font-size: 12px; font-weight: 500;
    font-family: inherit; transition: all 0.15s;
}
.toggle-btn.active { background: var(--bg-raised); color: var(--text); }
.toggle-btn:hover { color: var(--text-2); }

.btn-ghost { background: transparent; color: var(--text-2); border: 1px solid var(--border); }
.btn-ghost:hover { background: var(--bg-hover); }
.btn-subtle { background: var(--accent-dim); color: var(--accent); border: none; }
.btn-sm { padding: 5px 11px; font-size: 12px; border-radius: 6px; cursor: pointer; font-weight: 500; font-family: inherit; }
.btn-md { padding: 7px 15px; font-size: 13px; border-radius: var(--radius); cursor: pointer; font-weight: 500; font-family: inherit; }

/* ── Badge ───────────────────────────────── */
.badge {
    font-size: 10px; padding: 2px 8px; border-radius: 4px;
    font-weight: 600; letter-spacing: 0.3px; white-space: nowrap;
    display: inline-flex; align-items: center;
}

/* Dim variants (colored text on tinted background) */
.badge-accent, .badge-models, .badge-team   { background: var(--accent-dim); color: var(--accent-light); }
.badge-success, .badge-active, .badge-healthy { background: var(--success-dim); color: var(--success-light); }
.badge-warning, .badge-degraded              { background: var(--warning-dim); color: var(--warning-light); }
.badge-danger, .badge-disabled, .badge-unhealthy { background: var(--danger-dim); color: var(--danger-light); }
.badge-muted, .badge-unknown, .badge-provider-type, .badge-global, .badge-user { background: var(--bg-raised); color: var(--text-3); }
.badge-purple, .badge-service               { background: var(--purple-dim); color: var(--purple); }

/* Solid variants (white text on colored background) */
.badge-admin    { background: var(--accent); color: var(--text-on-color); }
.badge-inactive { background: var(--danger); color: var(--text-on-color); }
.badge-pending  { background: var(--warning); color: var(--text-on-warning); }
.badge-private  { background: var(--purple); color: var(--text-on-color); }

/* ── Status Dot ──────────────────────────── */
.status-dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.status-dot.healthy { background: var(--success); }
.status-dot.degraded { background: var(--warning); }
.status-dot.down { background: var(--danger); }
.status-dot.disabled { background: var(--text-3); }

/* ── Icon Button ─────────────────────────── */
.icon-btn {
    background: none; border: none; color: var(--text-3);
    cursor: pointer; padding: 5px; border-radius: 7px;
    display: flex; align-items: center; justify-content: center;
    transition: all 0.15s;
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text-2); }
.icon-btn.active { color: var(--accent); }
.icon-btn-danger:hover { color: var(--danger); }

/* ── Utility States ─────────────────────── */
.loading { color: var(--text-3); font-size: 13px; padding: 0.5rem; }
.error-hint { color: var(--danger); font-size: 13px; padding: 0.5rem; }
.empty-hint { color: var(--text-3); font-size: 13px; text-align: center; padding: 1rem; }

/* ── SDK Primitives (sw.menu, sw.dropdown, sw.toolbar, sw.tabs) ── */

/* Menu flyout — reusable dropdown attached to any anchor */
.sw-menu-wrap { position: relative; display: inline-flex; }
.sw-menu-flyout {
    display: none; position: absolute; right: 0; left: auto; z-index: 200;
    background: var(--bg-elevated, #42424e); border: 1px solid var(--border-elevated, #555);
    border-radius: var(--radius-lg, 8px); padding: 4px; min-width: 170px;
    box-shadow: 0 8px 40px rgba(0,0,0,0.8), 0 0 0 1px rgba(255,255,255,0.08);
}
.sw-menu-flyout[data-position="down"] { top: 100%; margin-top: 4px; }
.sw-menu-flyout[data-position="up"]   { bottom: 100%; margin-bottom: 4px; top: auto; }
.sw-menu-flyout.open { display: block; }
/* Fixed positioning — escapes overflow:hidden ancestors (set by JS) */
.sw-menu-flyout[data-fixed] { position: fixed; }
/* Flyout items — single source of truth for all flyout menus (sw.menu + UserMenu) */
.sw-menu-flyout .flyout-item {
    display: flex; align-items: center; gap: 10px;
    padding: 8px 12px; border-radius: var(--radius, 6px);
    background: none; border: none; color: var(--text, #e8e8ed);
    cursor: pointer; font-size: 13px; font-family: inherit; width: 100%;
    transition: background 0.15s, color 0.15s;
}
.sw-menu-flyout .flyout-item:hover {
    background: var(--bg-hover, rgba(255,255,255,0.06)); color: var(--text, #eee);
}
.sw-menu-flyout .flyout-item svg { flex-shrink: 0; opacity: 0.8; }
.sw-menu-flyout .flyout-danger { color: var(--danger, #f44336); }
.sw-menu-flyout .flyout-danger:hover {
    background: var(--danger-dim, rgba(244,67,54,0.1)); color: var(--danger, #f44336);
}
.sw-menu-flyout .flyout-divider {
    border: none; height: 1px; background: var(--border, #2a2a2e); margin: 4px 0;
}

/* Dropdown — styled native select */
.sw-dropdown {
    background: var(--bg-raised, #222); color: var(--text, #eee);
    border: 1px solid var(--border, #2e2e35); border-radius: var(--radius, 6px);
    padding: 6px 10px; font-size: 13px; font-family: inherit;
    cursor: pointer; min-width: 100px;
}
.sw-dropdown:hover { border-color: var(--border-light, #444); }
.sw-dropdown:focus { outline: none; border-color: var(--accent, #b38a4e); }

/* Toolbar — horizontal button row */
.sw-toolbar {
    display: flex; align-items: center; gap: 4px;
    padding: 4px 0;
}

/* Tabs — tabbed content container */
.sw-tabs { display: flex; flex-direction: column; height: 100%; }
.sw-tabs-bar {
    display: flex; gap: 0; border-bottom: 1px solid var(--border, #2e2e35);
    flex-shrink: 0;
}
.sw-tab-btn {
    padding: 8px 16px; background: none; border: none; border-bottom: 2px solid transparent;
    color: var(--text-2, #999); cursor: pointer; font-size: 12px; font-weight: 600;
    font-family: inherit; text-transform: uppercase; letter-spacing: 0.5px;
    transition: color 0.15s, border-color 0.15s;
}
.sw-tab-btn:hover { color: var(--text, #eee); }
.sw-tab-btn--active { color: var(--accent, #b38a4e); border-bottom-color: var(--accent, #b38a4e); }
.sw-tabs-content { flex: 1; min-height: 0; overflow: hidden; }
.sw-tab-panel { height: 100%; overflow-y: auto; }

/* ── Responsive: Mobile ───────────────────── */

@media (max-width: 768px) {
    /* Flyout items: larger touch targets */
    .sw-menu-flyout .flyout-item {
        padding: 10px 12px;
        min-height: 44px;
    }

    /* Flyout: constrain to viewport */
    .sw-menu-flyout {
        max-width: calc(100vw - 16px);
    }

    /* Dialog/modal: full width on mobile, max with margins */
    .sw-dialog__content {
        width: calc(100vw - 32px);
        max-width: 100%;
        max-height: calc(100vh - 64px);
    }

    /* Toast: full width, centered */
    .sw-toast {
        max-width: calc(100vw - 32px);
    }
}

/* ── User Picker (autocomplete) ──────────── */

.sw-user-picker {
    position: relative;
}

.sw-user-picker__input {
    width: 100%;
    padding: 8px 10px;
    border-radius: var(--radius, 6px);
    border: 1px solid var(--border, #2a2a2e);
    background: var(--bg, #0e0e10);
    color: var(--text, #eee);
    font-size: 0.85em;
}

.sw-user-picker__input:focus {
    outline: none;
    border-color: var(--accent, #b38a4e);
}

.sw-user-picker__spinner {
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
    width: 14px;
    height: 14px;
    border: 2px solid var(--border, #2a2a2e);
    border-top-color: var(--accent, #b38a4e);
    border-radius: 50%;
    animation: sw-spin 0.6s linear infinite;
}

@keyframes sw-spin { to { transform: translateY(-50%) rotate(360deg); } }

.sw-user-picker__dropdown {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    margin-top: 4px;
    background: var(--bg-elevated, #2a2a2e);
    border: 1px solid var(--border, #2a2a2e);
    border-radius: var(--radius, 6px);
    box-shadow: 0 8px 24px rgba(0,0,0,0.5);
    z-index: 300;
    max-height: 240px;
    overflow-y: auto;
}

.sw-user-picker__option {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 10px;
    cursor: pointer;
    transition: background 0.1s;
}

.sw-user-picker__option:hover,
.sw-user-picker__option--active {
    background: var(--bg-hover, rgba(255,255,255,0.06));
}

.sw-user-picker__avatar {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background: var(--accent, #b38a4e);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.7em;
    font-weight: 600;
    flex-shrink: 0;
}

.sw-user-picker__label {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.sw-user-picker__name {
    font-size: 0.85em;
    color: var(--text, #eee);
}

.sw-user-picker__handle {
    font-size: 0.75em;
    color: var(--text-muted, #888);
}

