/* Shared by admin.html and context.html. Single source of truth: edit here,
   not in either page, or the two drift apart. */
        :root {
            --bg: #f6f5f1;
            --panel: #ffffff;
            --panel-2: #faf9f6;
            --border: #e6e3dc;
            --border-strong: #d9d5cb;
            --text: #14130e;
            --text-muted: #6b6966;
            --text-subtle: #969289;
            --accent: #2b4fff;
            --accent-hover: #1a3be6;
            --accent-soft: rgba(43, 79, 255, 0.08);
            --success: #10b981;
            --danger: #ef4444;
            --warning: #f59e0b;
            --radius: 12px;
            --radius-sm: 8px;
            --radius-lg: 16px;
            --shadow-sm: 0 1px 2px rgba(20,19,14,.04), 0 1px 3px rgba(20,19,14,.05);
            --shadow-md: 0 6px 18px rgba(20,19,14,.08);
            --shadow-lg: 0 16px 48px rgba(20,19,14,.18);
            --ring: 0 0 0 3px rgba(43,79,255,.20);
            --t-fast: 120ms cubic-bezier(.4,0,.2,1);
            --t: 200ms cubic-bezier(.4,0,.2,1);
        }
        * { margin: 0; padding: 0; box-sizing: border-box; }
        html { font-size: 16px; }
        body {
            font-family: 'Segoe UI', system-ui, -apple-system, sans-serif; background: var(--bg); color: var(--text);
            font-size: 0.9375rem; line-height: 1.55; -webkit-font-smoothing: antialiased; text-rendering: optimizeLegibility;
        }
        .container { max-width: 1120px; margin: 0 auto; padding: 32px 24px 96px; }
        h1 { font-size: 1.6rem; font-weight: 700; letter-spacing: -0.01em; margin-bottom: 6px; text-wrap: balance; }
        .subtitle { color: var(--text-muted); font-size: 0.9375rem; margin-bottom: 28px; line-height: 1.5; }

        .tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--border); margin-bottom: 28px; }
        .tab { padding: 11px 20px; cursor: pointer; font-size: 0.9375rem; font-weight: 600; color: var(--text-muted); border: none; background: none; border-bottom: 2px solid transparent; margin-bottom: -1px; border-radius: var(--radius-sm) var(--radius-sm) 0 0; transition: color var(--t), background var(--t), border-color var(--t); }
        .tab:hover { color: var(--text); background: var(--accent-soft); }
        .tab.active { color: var(--accent); border-bottom-color: var(--accent); }
        .tab:focus-visible { outline: none; box-shadow: var(--ring); }
        .tab-content { display: none; }
        .tab-content.active { display: block; animation: fadeUp .28s ease both; }

        .card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 24px; margin-bottom: 20px; box-shadow: var(--shadow-sm); }
        .card-header { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 20px; }
        .card-title { font-size: 1.0625rem; font-weight: 700; letter-spacing: -0.01em; }
        .card-desc { font-size: 0.875rem; color: var(--text-muted); line-height: 1.55; margin: -8px 0 22px; max-width: 72ch; }

        .form-group { margin-bottom: 18px; }
        .form-group label { display: block; font-size: 0.75rem; font-weight: 700; color: var(--text-muted); margin-bottom: 6px; text-transform: uppercase; letter-spacing: 0.06em; }
        input[type="text"], input[type="number"], input[type="date"], textarea, select {
            width: 100%; padding: 10px 13px; border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
            font-size: 0.9375rem; font-family: inherit; background: var(--panel); color: var(--text); transition: border-color var(--t), box-shadow var(--t);
        }
        textarea { min-height: 88px; resize: vertical; line-height: 1.5; }
        input:focus, textarea:focus, select:focus { outline: none; border-color: var(--accent); box-shadow: var(--ring); }
        input::placeholder, textarea::placeholder { color: var(--text-subtle); }

        .btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 9px 16px; border-radius: var(--radius-sm); font-size: 0.875rem; font-weight: 600; cursor: pointer; border: 1px solid transparent; transition: background var(--t), border-color var(--t), transform var(--t-fast), box-shadow var(--t); white-space: nowrap; }
        .btn:active { transform: translateY(1px); }
        .btn:focus-visible { outline: none; box-shadow: var(--ring); }
        .btn-primary { background: var(--accent); color: #fff; }
        .btn-primary:hover { background: var(--accent-hover); box-shadow: var(--shadow-md); }
        .btn-danger { background: var(--danger); color: #fff; }
        .btn-danger:hover { background: #dc2626; }
        .btn-outline { background: var(--panel); border-color: var(--border-strong); color: var(--text); }
        .btn-outline:hover { background: var(--bg); border-color: var(--text-subtle); }
        .btn-success { background: var(--success); color: #fff; }
        .btn-success:hover { background: #0ea371; }
        .btn-sm { padding: 6px 11px; font-size: 0.75rem; }

        table { width: 100%; border-collapse: collapse; }
        th { text-align: left; padding: 10px 12px; font-size: 0.75rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-muted); border-bottom: 1px solid var(--border-strong); }
        td { padding: 12px; border-bottom: 1px solid var(--border); font-size: 0.875rem; vertical-align: middle; }
        tbody tr { transition: background var(--t-fast); }
        tbody tr:hover { background: var(--panel-2); }
        tbody tr:last-child td { border-bottom: none; }

        .badge { display: inline-block; padding: 3px 9px; border-radius: 50px; font-size: 0.75rem; font-weight: 600; letter-spacing: 0.01em; }
        .badge-green { background: #d1fae5; color: #065f46; }
        .badge-gray { background: #f0efe9; color: #4b5563; }
        .badge-blue { background: #dbeafe; color: #1e40af; }
        .badge-red { background: #fee2e2; color: #991b1b; }

        .actions { display: flex; gap: 8px; }
        .form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
        .form-inline { display: flex; gap: 10px; align-items: end; }
        .dynamic-rows { margin-top: 10px; }
        .dynamic-row { display: flex; gap: 8px; align-items: center; margin-bottom: 8px; }
        .dynamic-row input { flex: 1; }
        .dynamic-row .btn { flex-shrink: 0; }
        .checkbox-group { display: flex; align-items: center; gap: 10px; }
        .checkbox-group input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--accent); }

        .toast { position: fixed; top: 20px; right: 20px; padding: 13px 18px; border-radius: var(--radius-sm); color: #fff; font-size: 0.875rem; font-weight: 600; z-index: 4000; box-shadow: var(--shadow-lg); animation: toastIn .3s cubic-bezier(.4,0,.2,1); max-width: min(92vw, 420px); }
        .toast-success { background: var(--success); }
        .toast-error { background: var(--danger); }
        .toast-info { background: var(--text); }

        .thread-item { border-left: 3px solid var(--accent); padding-left: 14px; margin-bottom: 14px; }
        .thread-reply { margin-left: 24px; border-left-color: var(--border-strong); }
        .thread-meta { font-size: 0.75rem; color: var(--text-muted); margin-bottom: 5px; }
        .thread-content { font-size: 0.9375rem; margin-bottom: 6px; line-height: 1.5; }
        .thread-resolved { opacity: 0.6; border-left-color: var(--success); }

        .preview-section { border-left: 3px solid var(--accent); padding-left: 16px; margin-bottom: 28px; }
        .preview-section h3 { font-size: 0.75rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.06em; margin-bottom: 10px; }
        .metric-big { font-size: 2.5rem; font-weight: 700; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; }
        .metric-sub { font-size: 0.875rem; color: var(--text-muted); }
        .doc-card { display: flex; gap: 14px; align-items: center; padding: 14px; border: 1px solid var(--border); border-radius: var(--radius-sm); margin-bottom: 10px; transition: border-color var(--t), box-shadow var(--t); }
        .doc-card:hover { border-color: var(--border-strong); box-shadow: var(--shadow-sm); }
        .doc-thumb { width: 48px; height: 62px; background: var(--bg); border-radius: 6px; overflow: hidden; flex-shrink: 0; }
        .doc-thumb img { width: 100%; height: 100%; object-fit: cover; }
        .empty-state { text-align: center; padding: 48px 24px; color: var(--text-muted); font-size: 0.9375rem; }

        .file-input-wrapper { position: relative; }
        .file-input-wrapper input[type="file"] { position: absolute; left: -9999px; }
        .file-input-label { display: inline-block; padding: 9px 16px; background: var(--bg); border: 1px dashed var(--border-strong); border-radius: var(--radius-sm); cursor: pointer; font-size: 0.875rem; font-weight: 600; transition: border-color var(--t), background var(--t); }
        .file-input-label:hover { border-color: var(--accent); background: var(--accent-soft); }

        .pdf-modal-overlay { display: none; position: fixed; inset: 0; background: rgba(20,19,14,.55); z-index: 2000; align-items: center; justify-content: center; padding: 20px; }
        .pdf-modal-overlay.active { display: flex; animation: fadeIn .2s ease; }
        .pdf-modal { background: var(--panel); border-radius: var(--radius); width: 90vw; height: 90vh; max-width: 920px; display: flex; flex-direction: column; overflow: hidden; box-shadow: var(--shadow-lg); animation: modalIn .28s cubic-bezier(.4,0,.2,1); }
        .pdf-modal-header { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 14px 18px; border-bottom: 1px solid var(--border); }
        .pdf-modal-header span { font-weight: 700; font-size: 0.9375rem; }
        .pdf-modal-close { background: none; border: none; font-size: 1.5rem; line-height: 1; cursor: pointer; color: var(--text-muted); padding: 2px 6px; border-radius: 6px; transition: background var(--t), color var(--t); }
        .pdf-modal-close:hover { color: var(--text); background: var(--bg); }
        .pdf-modal iframe { flex: 1; border: none; background: #fff; }

        /* In-app confirmation dialog (replaces the native browser confirm) */
        .confirm-overlay { display: none; position: fixed; inset: 0; background: rgba(20,19,14,.55); z-index: 5000; align-items: center; justify-content: center; padding: 20px; }
        .confirm-overlay.active { display: flex; animation: fadeIn .15s ease; }
        .confirm-box { background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow-lg); max-width: 420px; width: 100%; padding: 24px; animation: modalIn .2s cubic-bezier(.4,0,.2,1); }
        .confirm-title { font-size: 1.0625rem; font-weight: 700; margin-bottom: 8px; }
        .confirm-msg { font-size: 0.9375rem; line-height: 1.55; color: var(--text-muted); margin-bottom: 22px; }
        .confirm-actions { display: flex; justify-content: flex-end; gap: 10px; }

        .chat-fab { position: fixed; bottom: 24px; right: 24px; width: 58px; height: 58px; border-radius: 50%; background: var(--accent); color: #fff; border: none; cursor: pointer; box-shadow: 0 6px 20px rgba(43,79,255,.4); display: flex; align-items: center; justify-content: center; font-size: 24px; z-index: 3000; transition: transform var(--t), box-shadow var(--t), background var(--t); }
        .chat-fab:hover { transform: translateY(-2px) scale(1.05); box-shadow: 0 10px 28px rgba(43,79,255,.5); }
        .chat-fab:focus-visible { outline: none; box-shadow: var(--ring), 0 6px 20px rgba(43,79,255,.4); }
        .chat-fab.open { background: var(--danger); box-shadow: 0 6px 20px rgba(239,68,68,.4); }

        .chat-widget { position: fixed; bottom: 94px; right: 24px; width: 400px; height: 540px; background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius-lg); box-shadow: var(--shadow-lg); z-index: 3000; display: none; flex-direction: column; overflow: hidden; /* for history panel overlay */ }
        .chat-widget.open { display: flex; animation: popIn .26s cubic-bezier(.34,1.4,.64,1); transform-origin: bottom right; }

        .chat-widget-header { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 14px 16px; border-bottom: 1px solid var(--border); background: var(--panel); }
        .chat-widget-header .card-title { font-size: 0.9375rem; }
        .chat-scope-toggle { display: flex; gap: 3px; background: var(--bg); border-radius: 7px; padding: 3px; }
        .chat-scope-btn { padding: 5px 11px; border: none; border-radius: 5px; font-size: 0.75rem; font-weight: 600; cursor: pointer; background: transparent; color: var(--text-muted); transition: background var(--t), color var(--t); }
        .chat-scope-btn.active { background: var(--accent); color: #fff; }

        .learn-toggle { display: flex; align-items: center; gap: 5px; cursor: pointer; font-size: 0.75rem; color: var(--text-muted); user-select: none; }
        .learn-toggle input { width: 15px; height: 15px; accent-color: var(--accent); margin: 0; cursor: pointer; }
        .learn-label { font-weight: 600; }

        .chat-messages { flex: 1; overflow-y: auto; padding: 16px; background: var(--bg); }
        .chat-msg { margin-bottom: 14px; max-width: 86%; }
        .chat-msg.user { margin-left: auto; }
        .chat-msg.assistant { margin-right: auto; }
        .chat-bubble { padding: 11px 14px; border-radius: 14px; font-size: 0.875rem; line-height: 1.5; box-shadow: var(--shadow-sm); }
        .chat-msg.user .chat-bubble { background: var(--accent); color: #fff; border-bottom-right-radius: 5px; }
        .chat-msg.assistant .chat-bubble { background: var(--panel); border: 1px solid var(--border); border-bottom-left-radius: 5px; }
        .chat-bubble a { color: inherit; }
        .chat-tools-used { font-size: 0.75rem; color: var(--text-subtle); margin-top: 5px; }
        /* Model/token/cost footer, deliberately very faint, present but not attention-grabbing. */
        .chat-usage { font-size: 0.6875rem; color: var(--text-subtle); opacity: 0.55; margin-top: 3px; font-variant-numeric: tabular-nums; letter-spacing: 0.01em; }

        .chat-widget-footer { padding: 14px; border-top: 1px solid var(--border); background: var(--panel); }
        .chat-input-row { display: flex; gap: 8px; }
        .chat-input-row input { flex: 1; font-size: 0.875rem; padding: 10px 13px; }
        .chat-typing { color: var(--text-muted); font-size: 0.75rem; font-style: italic; padding: 0 14px 6px; }

        .insight-item { display: flex; align-items: flex-start; gap: 12px; padding: 12px 14px; border: 1px solid var(--border); border-radius: var(--radius-sm); margin-bottom: 10px; font-size: 0.875rem; transition: border-color var(--t), box-shadow var(--t); }
        .insight-item:hover { border-color: var(--border-strong); box-shadow: var(--shadow-sm); }
        .insight-item.inactive { opacity: 0.5; }
        .insight-text { flex: 1; line-height: 1.5; }
        .insight-text textarea { width: 100%; min-height: 54px; font-size: 0.875rem; padding: 8px 10px; border: 1px solid var(--accent); border-radius: 6px; font-family: inherit; resize: vertical; }
        .insight-meta { display: flex; align-items: center; gap: 8px; margin-top: 6px; }

        .chat-history-panel { display: none; flex-direction: column; position: absolute; inset: 0; background: var(--panel); z-index: 1; }
        .chat-history-panel.open { display: flex; animation: fadeIn .2s ease; }
        .chat-history-header { display: flex; justify-content: space-between; align-items: center; padding: 14px 16px; border-bottom: 1px solid var(--border); }
        .chat-history-header span { font-weight: 700; font-size: 0.9375rem; }
        .chat-history-list { flex: 1; overflow-y: auto; padding: 10px; }
        .chat-history-item { padding: 11px 13px; border: 1px solid var(--border); border-radius: var(--radius-sm); margin-bottom: 8px; cursor: pointer; transition: background var(--t), border-color var(--t); }
        .chat-history-item:hover { background: var(--bg); border-color: var(--border-strong); }
        .chat-history-item .hi-scope { font-size: 0.75rem; text-transform: uppercase; font-weight: 700; letter-spacing: 0.04em; }
        .chat-history-item .hi-date { font-size: 0.75rem; color: var(--text-muted); }
        .chat-history-item .hi-count { font-size: 0.75rem; color: var(--text-muted); }

        /* ---------- Mobile / small screens ---------- */
        @media (max-width: 640px) {
            .container { padding: 20px 14px 88px; }
            h1 { font-size: 1.35rem; }
            .subtitle { margin-bottom: 20px; }

            /* Tabs scroll sideways instead of overflowing or wrapping messily */
            .tabs { overflow-x: auto; flex-wrap: nowrap; scrollbar-width: none; margin-bottom: 22px; }
            .tabs::-webkit-scrollbar { display: none; }
            .tab { white-space: nowrap; flex: 0 0 auto; padding: 12px 16px; }

            .card { padding: 20px 16px; margin-bottom: 18px; overflow-x: auto; }
            .card-header { flex-wrap: wrap; gap: 10px; margin-bottom: 16px; }

            /* Any generic wide table keeps readable columns and scrolls inside its card */
            .card table { min-width: 520px; }

            /* Build Log table -> stacked cards so every row's Edit/Delete stay visible
               without horizontal scrolling (labels come from each cell's data-label) */
            #tab-build-log table { min-width: 0; }
            #tab-build-log thead { display: none; }
            #tab-build-log tbody tr { display: block; border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 6px 14px; margin-bottom: 12px; background: var(--panel); }
            #tab-build-log tbody tr:hover { background: var(--panel); }
            #tab-build-log td { display: flex; justify-content: space-between; align-items: center; gap: 14px; padding: 8px 0; border: none; border-bottom: 1px solid var(--border); }
            #tab-build-log td:last-child { border-bottom: none; }
            #tab-build-log td::before { content: attr(data-label); font-size: 0.75rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-muted); flex-shrink: 0; }
            #tab-build-log td.actions { padding-top: 12px; }
            #tab-build-log td.actions::before { display: none; }
            #tab-build-log td.actions .btn { flex: 1; }
            #tab-build-log td.empty-state { display: block; text-align: center; padding: 28px 12px; }
            #tab-build-log td.empty-state::before { display: none; }

            /* Stack two-up form rows into a single column */
            .form-row { grid-template-columns: 1fr; gap: 16px; }

            /* Chat panel fills the screen width instead of hanging off the right edge */
            .chat-widget { left: 12px; right: 12px; width: auto; bottom: 84px; height: min(74vh, 540px); }
            .chat-fab { bottom: 16px; right: 16px; }

            /* Drop the redundant title so the scope/Learn/History/New controls fit
               on one line (the Admin/Public toggle already signals it's the chat) */
            .chat-widget-header { padding: 13px 14px; }
            .chat-widget-header .card-title { display: none; }

            /* Full-width toasts so they aren't clipped at the screen edge */
            .toast { left: 12px; right: 12px; top: 12px; text-align: center; max-width: none; }
        }

        /* ---------- Animations ---------- */
        @keyframes toastIn { from { transform: translateX(110%); opacity: 0; } to { transform: translateX(0); opacity: 1; } }
        @keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
        @keyframes fadeUp { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
        @keyframes modalIn { from { opacity: 0; transform: translateY(10px) scale(.98); } to { opacity: 1; transform: none; } }
        @keyframes popIn { from { opacity: 0; transform: translateY(12px) scale(.96); } to { opacity: 1; transform: none; } }

        /* Respect users who prefer reduced motion */
        @media (prefers-reduced-motion: reduce) {
            *, *::before, *::after { animation-duration: .001ms !important; animation-iteration-count: 1 !important; transition-duration: .001ms !important; scroll-behavior: auto !important; }
        }

        /* ── Context layer ─────────────────────────────────────────── */
        .ctx-suggest { position:absolute; top:100%; left:0; right:0; z-index:40; background:var(--panel);
            border:1px solid var(--border-strong); border-radius:var(--radius-sm); box-shadow:var(--shadow-md);
            max-height:280px; overflow-y:auto; display:none; }
        .ctx-suggest-item { padding:9px 12px; cursor:pointer; border-bottom:1px solid var(--border); }
        .ctx-suggest-item:last-child { border-bottom:none; }
        .ctx-suggest-item:hover { background:var(--accent-soft); }
        .ctx-suggest-new { color:var(--accent); font-weight:600; font-size:0.8125rem; }
        .ctx-picked { background:var(--accent-soft); border:1px solid rgba(43,79,255,.2);
            border-radius:var(--radius-sm); padding:9px 12px; font-size:0.8125rem; margin-bottom:14px; }
        .ctx-confirm { background:#fffbeb; border:1px solid #fcd34d; border-radius:var(--radius-sm);
            padding:12px 14px; margin-bottom:14px; }
        .ctx-row { display:flex; gap:12px; padding:14px 4px; border-bottom:1px solid var(--border); cursor:pointer; }
        .ctx-row:last-child { border-bottom:none; }
        .ctx-row:hover { background:var(--bg); }
        .ctx-detail-head { border-bottom:1px solid var(--border); padding-bottom:14px; margin-bottom:16px; }
        .ctx-share { display:flex; gap:9px; align-items:flex-start; cursor:pointer; margin-top:6px; }
        .ctx-share input { accent-color:var(--accent); width:16px; height:16px; margin-top:2px; cursor:pointer; }
        .ctx-summary { background:var(--bg); border-radius:var(--radius-sm); padding:14px; margin-bottom:16px; }
        .ctx-docs { border-top:1px solid var(--border); padding-top:14px; margin-top:6px; }
        .ctx-note { border-left:2px solid var(--border-strong); padding:2px 0 12px 14px; margin-bottom:12px; }
        .ctx-note:hover { border-left-color:var(--accent); }

        .ctx-help { background: linear-gradient(180deg, var(--accent-soft), transparent 60%); }
        .ctx-help-grid { display:grid; grid-template-columns:repeat(auto-fit,minmax(215px,1fr)); gap:20px; }
        .ctx-help-grid > div { min-width:0; }
        .ctx-help-grid strong { display:block; font-size:0.875rem; margin-bottom:4px; }
        .ctx-help-grid p { font-size:0.8125rem; color:var(--text-muted); line-height:1.55; margin:0; }
        .ctx-help-step { width:22px; height:22px; border-radius:50%; background:var(--accent); color:#fff;
            font-size:0.6875rem; font-weight:700; display:flex; align-items:center; justify-content:center;
            margin-bottom:8px; }
        /* Collapsed extras on the capture box. Looks like a link, not a button,
           so it reads as "there is more if you need it" rather than a step. */
        .ctx-extras { margin-bottom:14px; }
        .ctx-extra-toggle { background:none; border:none; padding:0; cursor:pointer;
            color:var(--accent); font-size:0.8125rem; font-weight:600; font-family:inherit; }
        .ctx-extra-toggle:hover { text-decoration:underline; }
        .ctx-hint { font-size:0.6875rem; color:var(--text-muted); margin-top:4px; line-height:1.4; }

        /* Step number sitting inline in a field label, so the order of the form
           is visible without reading the help card. */
        .ctx-step { display:inline-flex; align-items:center; justify-content:center;
            width:17px; height:17px; border-radius:50%; background:var(--accent); color:#fff;
            font-size:0.625rem; font-weight:700; margin-right:6px; vertical-align:text-bottom; }
        .ctx-help-foot { margin-top:18px; padding-top:14px; border-top:1px solid var(--border);
            font-size:0.8125rem; color:var(--text-muted); line-height:1.6; }
