/* ═══ WikiOracle — Main application styles ═══
 *
 * Key classes:
 *   .tree-container    — Tree panel wrapper (overflow:hidden, position:relative)
 *   .chat-container    — Chat scroll area (flex:1, overflow-y:auto)
 *   .message           — Each message div (flex column, role-based alignment)
 *   .message.user      — Right-aligned, accent background
 *   .message.assistant — Left-aligned, neutral background
 *   .msg-meta          — Username + timestamp (small muted text)
 *   .msg-bubble        — Content area (padded, rounded corners)
 *   .tree-context-menu — Context menus (tree + messages), position:absolute, shadow
 *   .ctx-item          — Menu items (hover highlight with --accent)
 *   .ctx-danger        — Delete items (red text)
 *   .chat-placeholder  — Empty-state prompt (centred, muted)
 *
 * CSS custom properties (--accent, --border, --fg, etc.) are read by tree.js
 * at render time via cssVar() from util.js.
 */

/* Light theme (default) */
:root, [data-theme="light"] {
  --bg: #ffffff;
  --fg: #111827;
  --fg-muted: #6b7280;
  --border: #e5e7eb;
  --accent: #2563eb;
  --accent-light: #dbeafe;
  --bubble-user: #f3f4f6;
  --bubble-assistant: #ffffff;
  --input-bg: #ffffff;
  --header-bg: #f9fafb;
  --overlay-bg: rgba(0,0,0,0.3);
  --shadow-color: rgba(0,0,0,0.15);
  color-scheme: light;
}

/* Dark theme */
[data-theme="dark"] {
  --bg: #1a1b1e;
  --fg: #e4e5e7;
  --fg-muted: #9ca3af;
  --border: #2d2f34;
  --accent: #60a5fa;
  --accent-light: #1e3a5f;
  --bubble-user: #2d2f34;
  --bubble-assistant: #1a1b1e;
  --input-bg: #232428;
  --header-bg: #141517;
  --overlay-bg: rgba(0,0,0,0.55);
  --shadow-color: rgba(0,0,0,0.4);
  color-scheme: dark;
}

/* System preference (when data-theme="system" or absent) */
@media (prefers-color-scheme: dark) {
  [data-theme="system"] {
    --bg: #1a1b1e;
    --fg: #e4e5e7;
    --fg-muted: #9ca3af;
    --border: #2d2f34;
    --accent: #60a5fa;
    --accent-light: #1e3a5f;
    --bubble-user: #2d2f34;
    --bubble-assistant: #1a1b1e;
    --input-bg: #232428;
    --header-bg: #141517;
    --overlay-bg: rgba(0,0,0,0.55);
    --shadow-color: rgba(0,0,0,0.4);
    color-scheme: dark;
  }
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; font-family: ui-sans-serif, -apple-system, system-ui, "Segoe UI", Helvetica, Arial, sans-serif; background: var(--bg); color: var(--fg); margin: 0; overflow: hidden; overscroll-behavior: none; }
body { display: flex; flex-direction: column; touch-action: pan-x pan-y; /* disable native pinch-zoom and double-tap zoom; d3.zoom handles zoom in tree + chat */ }

/* Header */
.header { background: var(--header-bg); border-bottom: 1px solid var(--border); padding: 0.75rem 1rem; display: flex; align-items: center; justify-content: space-between; flex-shrink: 0; }
.header-btn { border: 1px solid var(--border); border-radius: 6px; background: var(--bg); color: var(--fg-muted); cursor: pointer; padding: 0.4rem 0.7rem; font-size: 0.8rem; transition: all 0.15s; }
.header-btn:hover { background: var(--accent-light); color: var(--accent); border-color: var(--accent); }

/* Loading modal — blocks interaction until state is loaded */
.loading-overlay { position: fixed; inset: 0; z-index: 10000; background: var(--overlay-bg, rgba(0,0,0,0.5)); display: flex; align-items: center; justify-content: center; }
.loading-box { background: var(--bg); border: 1px solid var(--border); border-radius: 12px; padding: 1.5rem 2rem; min-width: 280px; max-width: 360px; text-align: center; box-shadow: 0 8px 32px var(--shadow-color, rgba(0,0,0,0.2)); }
.loading-label { font-size: 0.9rem; color: var(--fg); margin-bottom: 0.75rem; }
.loading-track { width: 100%; height: 6px; background: var(--border); border-radius: 3px; overflow: hidden; }
.loading-fill { height: 100%; background: var(--accent); transition: width 0.15s ease; width: 0; border-radius: 3px; }
.loading-track.indeterminate .loading-fill { width: 30%; animation: progress-slide 1.2s ease-in-out infinite; }
@keyframes progress-slide {
  0%   { transform: translateX(-100%); }
  50%  { transform: translateX(200%); }
  100% { transform: translateX(400%); }
}

/* Settings panel (overlay class shared with context-overlay) */
.settings-panel { background: var(--bg); border-radius: 12px; padding: 1.5rem; max-width: 480px; width: 90%; max-height: 80vh; overflow-y: auto; box-shadow: 0 8px 32px var(--shadow-color); }
.settings-panel h2 { font-size: 1.1rem; margin-bottom: 0; }
.settings-panel label { display: block; font-size: 0.85rem; color: var(--fg-muted); margin-bottom: 0.3rem; margin-top: 0.8rem; }
.settings-panel input, .settings-panel select, .settings-panel textarea { width: 100%; padding: 0.5rem; border: 1px solid var(--border); border-radius: 6px; font-size: 0.9rem; font-family: inherit; background: var(--input-bg); color: var(--fg); }
.settings-panel textarea { min-height: 80px; resize: vertical; }
.settings-row { display: flex; gap: 0.75rem; align-items: center; }
.settings-row input[type=range] { flex: 1; }
.settings-row .range-val { font-size: 0.85rem; color: var(--fg-muted); min-width: 2.5rem; text-align: right; }
.settings-check { display: flex; align-items: center; gap: 0.5rem; font-size: 0.85rem; color: var(--fg-muted); margin-top: 0.8rem; cursor: pointer; }
.settings-check input[type=checkbox] { width: 1rem; height: 1rem; cursor: pointer; }
.settings-actions { margin-top: 1.2rem; display: flex; gap: 0.5rem; justify-content: flex-end; }
.btn { padding: 0.5rem 1rem; border-radius: 6px; border: 1px solid var(--border); background: var(--bg); cursor: pointer; font-size: 0.85rem; transition: all 0.15s; }
.btn-primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn-primary:hover { opacity: 0.9; }
.btn:hover { background: var(--accent-light); }

/* Tree view (top-down hierarchy) */
.tree-container { flex-shrink: 0; background: var(--header-bg); border-bottom: 1px solid var(--border); position: relative; height: 40%; min-height: 0; overflow: hidden; touch-action: none; /* d3.zoom handles gestures */ }
.tree-container.tree-collapsed { border-bottom: none; overflow: hidden; }
.tree-container svg { display: block; min-width: 100%; min-height: 100%; }

/* Chat */
.chat-container { flex: 1; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; touch-action: pan-y pinch-zoom; /* d3.zoom handles pinch; allow vertical scroll */ }
.chat-wrapper { max-width: 90%; margin: 0 auto; padding: 1.5rem 1rem 1rem; display: flex; flex-direction: column; gap: 0.5rem; min-height: 100px; }
.chat-placeholder { color: var(--fg-muted); font-size: 0.9rem; text-align: center; padding: 3rem 1rem; }

/* ── Root summary dashboard ── */
.root-summary { padding: 2rem 1rem; text-align: center; max-width: 420px; margin: 0 auto; }
.root-summary-title { font-size: 1.3rem; font-weight: 700; margin: 0 0 0.2rem; color: var(--fg); }
.root-summary-subtitle { font-size: 0.82rem; color: var(--fg-muted); margin: 0 0 1.4rem; }

.root-status-line { font-size: 0.78rem; color: var(--fg-muted); margin: 0 0 0.1rem; }
.root-status-line:last-of-type { margin-bottom: 0.8rem; }
.root-nanochat-status { font-size: 0.78rem; color: var(--fg-muted); margin: 0.2rem 0 1rem; }
.status-online { color: #2a9d2a; }
.status-offline { color: #c44; }

.root-summary-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 0.6rem; margin-bottom: 1.4rem; }
.root-stat { background: var(--input-bg); border: 1px solid var(--border); border-radius: 8px; padding: 0.7rem 0.3rem; }
.root-stat-value { display: block; font-size: 1.4rem; font-weight: 700; color: var(--accent); }
.root-stat-label { display: block; font-size: 0.7rem; color: var(--fg-muted); margin-top: 0.15rem; }

.root-summary-meta { font-size: 0.78rem; color: var(--fg-muted); margin-bottom: 1.2rem; display: flex; flex-direction: column; gap: 0.2rem; }

.root-summary-section { text-align: left; margin-bottom: 1rem; padding: 0.6rem 0.8rem; background: var(--input-bg); border: 1px solid var(--border); border-radius: 8px; }
.root-summary-section h3 { font-size: 0.78rem; font-weight: 600; color: var(--fg-muted); text-transform: uppercase; letter-spacing: 0.04em; margin: 0 0 0.3rem; }
.root-summary-section p { font-size: 0.82rem; color: var(--fg); margin: 0; line-height: 1.45; }
.root-summary-context { white-space: pre-line; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; }

.root-summary-default, .context-default { opacity: 0.5; font-style: italic; }
/* Server-origin truth entries (debug mode) — visually distinct from client truth */
.truth-server-origin { opacity: 0.65; border-left: 3px solid var(--accent, #3b82f6); padding-left: 0.5rem; }
.truth-server-badge { font-size: 0.65rem; color: var(--accent, #3b82f6); text-transform: uppercase; letter-spacing: 0.06em; margin-left: 0.3rem; }
.root-summary-hint { font-size: 0.82rem; color: var(--fg-muted); margin-top: 1.4rem; }
.root-init-section { text-align: center; background: none; border: none; margin-top: 0.6rem; padding: 0; }
.root-init-btn { font-size: 0.78rem; color: var(--fg-muted); background: none; border: 1px solid var(--border); border-radius: 6px; padding: 0.35rem 1rem; cursor: pointer; }
.root-init-btn:hover { color: #dc2626; border-color: #dc2626; }

.message { display: flex; flex-direction: column; margin-bottom: 0.25rem; position: relative; }
.msg-meta { font-size: 0.75rem; color: var(--fg-muted); margin-bottom: 0.15rem; padding: 0 0.25rem; }
.msg-bubble { padding: 0.7rem 1rem; border-radius: 12px; line-height: 1.5; font-size: 0.95rem; white-space: pre-wrap; word-break: break-word; }
.message.user .msg-bubble { background: var(--bubble-user); border: 1px solid var(--border); align-self: flex-start; border-bottom-left-radius: 4px; border-left: 3px solid var(--accent); }
.message.assistant .msg-bubble { background: var(--bubble-assistant); border: 1px solid var(--border); align-self: flex-start; border-bottom-left-radius: 4px; border-left: 3px solid #059669; }
/* Restore list indentation stripped by global * { padding:0 } reset —
   without this, list markers (bullets/numbers) overflow the bubble border. */
.msg-bubble ul, .msg-bubble ol { padding-left: 1.5em; }
.msg-bubble pre { overflow-x: auto; }

/* Structured truth tags inside message bubbles */
.msg-bubble fact { display: block; margin: 0.4rem 0; padding: 0.35rem 0.6rem; border-left: 3px solid var(--accent); background: var(--accent-light); border-radius: 0 4px 4px 0; font-size: 0.9rem; }
.msg-bubble feeling { display: block; margin: 0.4rem 0; padding: 0.35rem 0.6rem; border-left: 3px solid #8b5cf6; background: #ede9fe; border-radius: 0 4px 4px 0; font-size: 0.9rem; font-style: italic; }
[data-theme="dark"] .msg-bubble fact { background: rgba(37, 99, 235, 0.15); }
[data-theme="dark"] .msg-bubble feeling { background: rgba(139, 92, 246, 0.15); }

/* Message interaction */
.msg-bubble { cursor: pointer; }
.msg-bubble.msg-selected { outline: 2px solid var(--accent); outline-offset: -1px; }


/* Conversation navigation in chat pane */
.conv-parent-nav { padding: 0.4rem 0; margin-bottom: 0.3rem; border-bottom: 1px solid var(--border); }
.conv-parent-link { background: none; border: none; padding: 0.2rem 0; font-size: 0.8rem; color: var(--accent); cursor: pointer; font-family: inherit; transition: color 0.15s; }
.conv-parent-link:hover { color: var(--fg); }
.conv-children-nav { display: flex; flex-wrap: wrap; gap: 0.4rem; padding: 0.6rem 0; margin-top: 0.5rem; border-top: 1px solid var(--border); }
.conv-child-link { background: var(--bg); border: 1px solid var(--border); border-radius: 6px; padding: 0.35rem 0.7rem; font-size: 0.8rem; color: var(--accent); cursor: pointer; transition: all 0.15s; font-family: inherit; }
.conv-child-link:hover { background: var(--accent-light); border-color: var(--accent); }

/* Input */
.input-bar { flex-shrink: 0; background: var(--bg); border-top: 1px solid var(--border); padding: 0.75rem 1rem; }
.input-wrapper { max-width: 48rem; margin: 0 auto; display: flex; gap: 0.5rem; }
.input-wrapper textarea { flex: 1; padding: 0.6rem 0.75rem; border: 1px solid var(--border); border-radius: 8px; font-size: 0.95rem; font-family: inherit; resize: none; max-height: 120px; line-height: 1.4; background: var(--input-bg); color: var(--fg); }
.send-btn { padding: 0.6rem 1.2rem; border: none; border-radius: 8px; background: var(--accent); color: #fff; font-size: 0.95rem; cursor: pointer; transition: opacity 0.15s; }
.send-btn:hover { opacity: 0.85; }
.send-btn:disabled { opacity: 0.4; cursor: not-allowed; }

/* Context menu (for tree nodes) */
.tree-context-menu { position: absolute; background: var(--bg); border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 4px 16px var(--shadow-color); z-index: 20; min-width: 140px; padding: 4px 0; font-size: 0.82rem; }
.tree-context-menu .ctx-item { padding: 0.45rem 0.9rem; cursor: pointer; display: flex; align-items: center; gap: 0.5rem; color: var(--fg); }
.tree-context-menu .ctx-item:hover { background: var(--accent-light); color: var(--accent); }
.tree-context-menu .ctx-item.ctx-danger { color: #dc2626; }
.tree-context-menu .ctx-item.ctx-danger:hover { background: var(--accent-light); color: #dc2626; }
.tree-context-menu .ctx-sep { height: 1px; background: var(--border); margin: 4px 0; }
.tree-context-menu .ctx-item.ctx-disabled { opacity: 0.4; cursor: default; pointer-events: none; }

/* Resize divider between tree and chat — wider hit area near screen edges */
.resize-divider { flex-shrink: 0; height: 8px; background: var(--border); cursor: row-resize; position: relative; z-index: 5; transition: background 0.15s; touch-action: none; }
.resize-divider:hover, .resize-divider.active { background: var(--accent); }
.resize-divider::before { content: ""; position: absolute; inset: -6px 0; z-index: -1; } /* invisible hit-area expander */
.resize-divider::after { content: ""; position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); width: 32px; height: 2px; background: var(--fg-muted); border-radius: 1px; opacity: 0.4; }
.resize-divider:hover::after, .resize-divider.active::after { opacity: 0.8; background: #fff; }
@media (pointer: coarse) { .resize-divider { height: 14px; } body.layout-vertical .resize-divider { width: 14px; } }

/* Context editor modal (reuses settings overlay pattern) */
.context-overlay { display: none; position: fixed; inset: 0; background: var(--overlay-bg); z-index: 100; }
.context-overlay.active { display: flex; align-items: center; justify-content: center; }
.context-panel { background: var(--bg); border-radius: 12px; padding: 1.5rem; max-width: 480px; width: 90%; max-height: 80vh; overflow-y: auto; box-shadow: 0 8px 32px var(--shadow-color); }
.context-panel h2 { font-size: 1.1rem; margin-bottom: 0; }
.context-panel p { font-size: 0.8rem; color: var(--fg-muted); margin-bottom: 0.8rem; }
.context-panel textarea { width: 100%; padding: 0.5rem; border: 1px solid var(--border); border-radius: 6px; font-size: 0.9rem; font-family: inherit; min-height: 100px; resize: vertical; background: var(--input-bg); color: var(--fg); }
.context-panel .settings-actions { margin-top: 1rem; display: flex; gap: 0.5rem; justify-content: flex-end; }

/* Vertical (side-by-side) layout */
.main-area { display: flex; flex-direction: column; flex: 1; overflow: hidden; }
body.layout-vertical .main-area { flex-direction: row; }
body.layout-vertical .tree-container { height: auto; width: 280px; min-width: 0; min-height: auto; border-bottom: none; border-right: 1px solid var(--border); overflow: hidden; }
body.layout-vertical .tree-container.tree-collapsed { border-right: none; }
body.layout-vertical .resize-divider { height: auto; width: 6px; cursor: col-resize; align-self: stretch; }
body.layout-vertical .resize-divider::after { width: 2px; height: 32px; }
body.layout-vertical .chat-container { flex: 1; }

/* ═══ 404 / Offline page ═══ */
.offline-page {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: #1a1a2e; color: #e0e0e0;
  display: flex; align-items: center; justify-content: center;
  min-height: 100vh; padding: 2rem;
}
.offline-page .card {
  text-align: center; max-width: 480px;
  background: #16213e; border-radius: 16px;
  padding: 3rem 2.5rem; box-shadow: 0 8px 32px rgba(0,0,0,.4);
}
.offline-page .icon { font-size: 4rem; margin-bottom: 1.5rem; }
.offline-page h1 { font-size: 1.5rem; margin-bottom: 0.75rem; color: #fff; }
.offline-page p { line-height: 1.6; color: #a0aec0; margin-bottom: 1rem; }
.offline-page .retry {
  display: inline-block; margin-top: 1rem;
  padding: 0.6rem 1.5rem; border-radius: 8px;
  background: #0f3460; color: #e0e0e0;
  text-decoration: none; font-weight: 500;
  transition: background .2s;
}
.offline-page .retry:hover { background: #1a4a7a; }

/* ═══ Utility classes (replacing inline style= attributes) ═══ */
.settings-sep  { margin-top: 1.2rem; border-top: 1px solid var(--border); padding-top: 0.8rem; }
.btn-full      { width: 100%; }
.hidden        { display: none; }

/* Config editor */
.config-panel    { max-width: 560px; }
.config-textarea { width: 100%; min-height: 300px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.82rem; line-height: 1.5; tab-size: 2; resize: vertical; padding: 0.6rem; border: 1px solid var(--border); border-radius: 6px; background: var(--input-bg); color: var(--fg); }
.config-error    { display: none; color: #dc2626; font-size: 0.8rem; margin-top: 0.4rem; }

/* Trust editor */
.trust-panel          { max-width: 620px; }
.trust-entries-scroll { max-height: 400px; overflow-y: auto; margin-bottom: 0.6rem; }
.trust-label          { font-size: 0.82rem; font-weight: 600; display: block; margin-bottom: 0.5rem; }
.trust-label-hint     { font-weight: normal; color: var(--fg-muted); }
.trust-input          { width: 100%; margin-bottom: 0.5rem; padding: 0.4rem; border: 1px solid var(--border); border-radius: 4px; font-size: 0.85rem; background: var(--input-bg); color: var(--fg); }
.trust-textarea       { width: 100%; min-height: 120px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.82rem; line-height: 1.5; padding: 0.4rem; border: 1px solid var(--border); border-radius: 4px; resize: vertical; background: var(--input-bg); color: var(--fg); }
.trust-empty          { color: var(--fg-muted); font-size: 0.85rem; padding: 1rem 0; text-align: center; }

/* Settings-actions spacing variants */
.settings-actions-xs { margin-top: 0.4rem; }
.settings-actions-sm { margin-top: 0.6rem; }
.settings-actions-md { margin-top: 0.8rem; }

/* Dialog title bar with close button */
.dialog-title-bar { display: flex; align-items: center; margin-bottom: 0.5rem; }
.dialog-title-bar h2 { flex: 1; font-size: 1.1rem; margin: 0; }
.dialog-close { background: none; border: none; font-size: 1.3rem; cursor: pointer; color: var(--fg-muted); padding: 0.2rem 0.4rem; line-height: 1; border-radius: 4px; transition: all 0.15s; }
.dialog-close:hover { background: var(--accent-light); color: var(--fg); }

/* Search panel */
.search-panel { max-width: 560px; }
.search-bar { display: flex; gap: 0.5rem; margin-bottom: 0.6rem; }
.search-bar input { flex: 1; padding: 0.45rem 0.6rem; border: 1px solid var(--border); border-radius: 6px; font-size: 0.85rem; background: var(--input-bg); color: var(--fg); }
.search-bar input:focus { outline: none; border-color: var(--accent); }
.search-info { font-size: 0.75rem; color: var(--fg-muted); margin-bottom: 0.4rem; }
.search-results { max-height: 50vh; overflow-y: auto; }
.search-result { padding: 0.5rem; border-bottom: 1px solid var(--border); cursor: pointer; font-size: 0.82rem; transition: background 0.1s; }
.search-result:hover { background: var(--accent-light); }
.search-result-title { font-weight: 600; font-size: 0.82rem; margin-bottom: 0.2rem; }
.search-result-snippet { color: var(--fg-muted); font-size: 0.78rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.search-result-snippet mark { background: var(--accent-light); color: var(--accent); font-weight: 600; }

/* ── Mobile: pinch-zoomed layout ── */
@media (pointer: coarse) {
}
