:root {
    color-scheme: dark;
    /* Neutral graphite palette — kept in sync with the tailwind.config override
       in templates/_tailwind_theme.html. Barely-cool darks, one crisp blue. */
    --bg-primary: #0B0D12;   /* app background */
    --bg-surface: #101319;   /* header / panels */
    --bg-card: #171B22;      /* cards / inputs */
    --border-color: #262B35;
    --text-primary: #E9EBEF;
    --text-secondary: #868C9A;
    --accent-blue: #2E7FF0;
    --accent-green: #2FA97A;
    --accent-red: #E15D54;
    --accent-amber: #D69233;
}

html, body {
    background-color: var(--bg-primary);
    color: var(--text-primary);
    min-height: 100vh;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-rendering: optimizeLegibility;
    /* Engage Inter's optical-size axis so small UI text and larger headings are each tuned. */
    font-optical-sizing: auto;
}

/* Align digits in dense data (tables, run IDs, DAG labels) and disambiguate glyphs. */
table, .font-mono, [class*="font-mono"] {
    font-variant-numeric: tabular-nums;
    font-feature-settings: 'tnum' 1, 'calt' 0;
}
.font-mono, [class*="font-mono"] { font-variant-ligatures: none; }

::selection { background: rgba(46, 127, 240, 0.30); }

/* Lab run activity: quiet when read, pulsing only while work is underway. */
.run-activity { display: none; }
.run-activity[data-state] {
    display: block;
    width: 0.5rem;
    height: 0.5rem;
    flex-shrink: 0;
    border-radius: 50%;
    background: var(--accent-green);
}
.run-activity[data-state="working"] { animation: run-working 1.5s ease-in-out infinite; }
@keyframes run-working {
    50% { opacity: 0.35; }
}

/* Consistent focus ring for interactive elements */
a:focus-visible, button:focus-visible, textarea:focus-visible, input:focus-visible, select:focus-visible {
    outline: 2px solid rgba(46, 127, 240, 0.65);
    outline-offset: 1px;
    border-radius: 0.375rem;
}

/* ---------------------------------------------------------------------------
 * Elevation system. Dark UIs read as crisp from a hairline top highlight plus
 * soft ambient shadow — not from brighter borders. Three tiers:
 *   .surface        cards, tables, panels (sits on the page)
 *   .surface-sunken wells, inputs, table bodies (recessed)
 *   .surface-raised dropdowns, modals, popovers (floats above)
 * Templates combine these with Tailwind utilities for radius/padding.
 * ------------------------------------------------------------------------- */
.surface {
    background: linear-gradient(180deg, #181C24 0%, #14181F 100%);
    border: 1px solid #262B35;
    box-shadow:
        inset 0 1px 0 rgba(255, 255, 255, 0.045),
        0 1px 2px rgba(0, 0, 0, 0.40),
        0 8px 24px -14px rgba(0, 0, 0, 0.65);
}
.surface-sunken {
    background: #0E1116;
    border: 1px solid #21262F;
    box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.35);
}
.surface-raised {
    background: #1B202A;
    border: 1px solid #2E3440;
    box-shadow:
        inset 0 1px 0 rgba(255, 255, 255, 0.06),
        0 16px 48px -12px rgba(0, 0, 0, 0.75);
}

/* Primary action button — one accent, used sparingly. */
.btn-primary {
    background: linear-gradient(180deg, #1668DC 0%, #1257BC 100%);
    box-shadow:
        inset 0 1px 0 rgba(255, 255, 255, 0.18),
        0 1px 2px rgba(0, 0, 0, 0.45);
}
.btn-primary:hover { filter: brightness(1.07); }
.btn-primary:active { filter: brightness(0.96); }

/* Click-to-copy public-id chip (see the .pid-chip click handler in base.html). */
.pid-chip {
    cursor: copy;
    color: #A9AFBC;
    border-radius: 0.375rem;
    padding: 0.125rem 0.375rem;
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid rgba(255, 255, 255, 0.06);
    transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}
.pid-chip:hover { background: rgba(255, 255, 255, 0.07); color: #D0D4DC; }
.pid-chip.copied { color: #5FD3A2; border-color: rgba(47, 169, 122, 0.4); }

/* Metrics: restrained data density, with the existing blue as the only
   decorative accent. Outcome colors remain semantic (ok/warn/danger). */
.metric-kpi {
    position: relative;
    overflow: hidden;
}
.metric-kpi::after {
    content: "";
    position: absolute;
    inset: auto -2.5rem -3.5rem auto;
    width: 8rem;
    height: 8rem;
    border-radius: 999px;
    background: radial-gradient(circle, rgba(46, 127, 240, 0.09), transparent 68%);
    pointer-events: none;
}
.metric-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 2.25rem;
    height: 2.25rem;
    border-radius: 0.75rem;
    color: #7FB1FF;
    background: rgba(46, 127, 240, 0.09);
    border: 1px solid rgba(46, 127, 240, 0.15);
}
.metric-chart {
    position: relative;
    width: 100%;
    height: 18rem;
    min-width: 0;
}
.metric-chart-tall { height: 20rem; }
.metric-chart-loading,
.metric-chart-empty {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 2rem;
    color: #868C9A;
    font-size: 0.75rem;
    text-align: center;
}
.metric-data-details {
    margin-top: 0.25rem;
    color: #868C9A;
    font-size: 0.75rem;
}
.metric-data-details > summary {
    width: fit-content;
    cursor: pointer;
    transition: color 0.15s ease;
}
.metric-data-details > summary:hover { color: #D0D4DC; }
.metric-stat {
    min-width: 0;
    padding: 1rem;
    background: #14181F;
}
.metric-stat p {
    color: #868C9A;
    font-size: 0.6875rem;
    line-height: 1rem;
    letter-spacing: 0.05em;
    text-transform: uppercase;
}
.metric-stat strong {
    display: block;
    margin-top: 0.5rem;
    color: #E9EBEF;
    font-size: 1.25rem;
    line-height: 1.75rem;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}
.metric-stat span {
    display: block;
    margin-top: 0.25rem;
    color: #868C9A;
    font-size: 0.6875rem;
    line-height: 1rem;
}

@media (max-width: 639px) {
    .metric-chart { height: 16rem; }
    .metric-chart-tall { height: 18rem; }
}

/* Disable scroll anchoring so user can scroll up freely */
#chat-messages { overflow-anchor: none; }

/* Custom scrollbar for chat/sidebar/panel */
.custom-scroll { scrollbar-width: thin; scrollbar-color: #262B35 transparent; }
.custom-scroll::-webkit-scrollbar { width: 8px; height: 8px; }
.custom-scroll::-webkit-scrollbar-track { background: transparent; }
.custom-scroll::-webkit-scrollbar-thumb { background: #262B35; border-radius: 999px; border: 2px solid transparent; background-clip: content-box; }
.custom-scroll::-webkit-scrollbar-thumb:hover { background: #3C4250; background-clip: content-box; }

/* Right context panel — subtle left edge + smooth collapse */
#context-panel {
    box-shadow: inset 1px 0 0 rgba(255, 255, 255, 0.02);
}

/* Empty state inside the execution-plan section */
.dag-empty {
    color: #7B8290;
}
.dag-empty svg { color: #3C4250; }

/* Fade-in for newly arriving chat bubbles */
@keyframes chat-msg-in {
    from { opacity: 0; transform: translateY(6px); }
    to   { opacity: 1; transform: translateY(0); }
}
#chat-messages-inner > div { animation: chat-msg-in 0.28s ease-out; }

/* Markdown rendered in chat messages */
/* Chat text is full of unbreakable tokens (public ids, asset reprs, URLs). Without
   this a single one is wider than a phone viewport and the whole chat column scrolls
   sideways. `anywhere` (not `break-word`) so it also shrinks min-content, which is
   what keeps flex/table children from pushing the column wide.
   `pre` opts back out: white-space:pre gives no wrap opportunities anyway and code
   blocks scroll inside their own overflow-x:auto box. */
.chat-markdown { overflow-wrap: anywhere; }
.chat-markdown pre { overflow-wrap: normal; }
/* Same treatment for chat text that isn't markdown (own messages, status pills). */
.chat-wrap-anywhere { overflow-wrap: anywhere; }
.chat-markdown p { margin: 0.25rem 0; }
.chat-markdown p:first-child { margin-top: 0; }
.chat-markdown p:last-child { margin-bottom: 0; }
.chat-markdown strong { font-weight: 600; }
.chat-markdown em { font-style: italic; }
.chat-markdown code { background: rgba(0,0,0,0.3); padding: 0.1em 0.35em; border-radius: 0.25rem; font-size: 0.8em; font-family: 'JetBrains Mono', 'SF Mono', 'Fira Code', ui-monospace, monospace; }
.chat-markdown pre { background: rgba(0,0,0,0.3); padding: 0.5rem 0.75rem; border-radius: 0.375rem; overflow-x: auto; margin: 0.5rem 0; }
.chat-markdown pre code { background: none; padding: 0; font-size: 0.8em; }
.chat-markdown ul, .chat-markdown ol { margin: 0.25rem 0; padding-left: 1.25rem; }
.chat-markdown ul { list-style: disc; }
.chat-markdown ol { list-style: decimal; }
.chat-markdown li { margin: 0.15rem 0; }
.chat-markdown h1, .chat-markdown h2, .chat-markdown h3 { font-weight: 600; margin: 0.5rem 0 0.25rem; }
.chat-markdown h1 { font-size: 1.1em; }
.chat-markdown h2 { font-size: 1.05em; }
.chat-markdown h3 { font-size: 1em; }
.chat-markdown table { width: 100%; border-collapse: collapse; margin: 0.5rem 0; font-size: 0.85em; }
.chat-markdown th, .chat-markdown td { border: 1px solid rgba(148,163,184,0.2); padding: 0.35rem 0.5rem; text-align: left; }
.chat-markdown th { background: rgba(0,0,0,0.2); font-weight: 600; }
.chat-markdown hr { border: none; border-top: 1px solid rgba(148,163,184,0.2); margin: 0.5rem 0; }
.chat-markdown a { color: #7FB1FF; text-decoration: underline; text-underline-offset: 2px; }
.chat-markdown blockquote { border-left: 3px solid rgba(148,163,184,0.3); padding-left: 0.75rem; margin: 0.25rem 0; color: #A9AFBC; }

/* Opt-in syntax highlighted code blocks (e.g. protocol draft proposals) */
.chat-code-block {
    margin: 0;
    tab-size: 4;
    background: rgba(11, 13, 18, 0.7) !important;
}
.chat-code-block > code.chat-syntax-highlight {
    display: block;
    font-family: 'JetBrains Mono', 'SF Mono', 'Fira Code', ui-monospace, monospace;
    font-size: 0.75rem;
    line-height: 1.5;
    white-space: pre;
    background: transparent !important;
    text-shadow: none;
    /* Contextual ligatures are pleasant in code, off everywhere else. */
    font-variant-ligatures: contextual;
}

/* Bouncing dots animation for executing indicator */
@keyframes bounce-dot {
    0%, 80%, 100% { transform: scale(0); }
    40% { transform: scale(1); }
}
.bounce-dot-1 { animation: bounce-dot 1.4s infinite ease-in-out both; animation-delay: -0.32s; }
.bounce-dot-2 { animation: bounce-dot 1.4s infinite ease-in-out both; animation-delay: -0.16s; }
.bounce-dot-3 { animation: bounce-dot 1.4s infinite ease-in-out both; }

/* DAG node running pulse animation */
@keyframes dag-pulse {
    0%, 100% { opacity: 1; filter: drop-shadow(0 0 0 transparent); }
    50% { opacity: 0.85; filter: drop-shadow(0 0 7px rgba(46, 127, 240, 0.40)); }
}
.dag-node-running {
    animation: dag-pulse 2s infinite ease-in-out;
}

/* Live indicator — slow pulse ring for "something is happening" states. */
@keyframes live-ping {
    0%   { box-shadow: 0 0 0 0 rgba(47, 169, 122, 0.45); }
    70%  { box-shadow: 0 0 0 5px rgba(47, 169, 122, 0); }
    100% { box-shadow: 0 0 0 0 rgba(47, 169, 122, 0); }
}
.dot-live { animation: live-ping 2.2s infinite cubic-bezier(0.2, 0.8, 0.2, 1); }

/* Respect the OS reduced-motion setting: collapse animations and transitions
   to imperceptible durations rather than removing classes template-by-template. */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}

/* Multiple camera players share the panel height with the execution plan. */
.context-camera-section { min-height: 0; max-height: 55%; flex-shrink: 1; }
#panel-section-video, #camera-panel-wrapper, .camera-panel {
    display: flex;
    flex-direction: column;
    min-height: 0;
}
#panel-section-video.hidden { display: none; }
.camera-panel { position: relative; gap: 8px; }
.camera-panel [hidden] { display: none !important; }
.camera-toolbar { display: flex; align-items: center; justify-content: flex-end; gap: 6px; flex-shrink: 0; }
.camera-picker { position: relative; min-width: 0; }
.camera-picker-button, .camera-expand-button, .camera-close-button, .camera-play-button {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    min-height: 32px;
    padding: 6px 9px;
    border: 1px solid var(--border-color);
    border-radius: 7px;
    background: var(--bg-card);
    color: var(--text-primary);
    font-size: 12px;
    cursor: pointer;
}
.camera-picker-button { list-style: none; }
.camera-picker-button::-webkit-details-marker { display: none; }
.camera-picker-button:hover, .camera-expand-button:hover, .camera-close-button:hover, .camera-play-button:hover { border-color: var(--text-secondary); }
.camera-picker-button:focus-visible { outline: 2px solid var(--accent-blue); outline-offset: 2px; }
.camera-picker-options {
    position: absolute;
    right: 0;
    top: calc(100% + 6px);
    z-index: 30;
    width: min(280px, calc(100vw - 76px));
    max-height: min(320px, 50dvh);
    overflow-y: auto;
    padding: 6px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: var(--bg-card);
    box-shadow: 0 10px 28px #0006;
}
.camera-picker-option { display: flex; align-items: center; gap: 10px; padding: 9px; border-radius: 5px; color: var(--text-primary); font-size: 13px; cursor: pointer; }
.camera-picker-option:hover { background: var(--bg-surface); }
.camera-picker-option > span { min-width: 0; overflow-wrap: anywhere; }
.camera-choice { width: 16px; height: 16px; flex-shrink: 0; accent-color: var(--accent-blue); }
.camera-grid { display: grid; grid-template-columns: minmax(0, 1fr); align-content: start; gap: 8px; min-height: 0; overflow-y: auto; }
.camera-grid.camera-grid-many { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.camera-tile { min-width: 0; border-radius: 8px; overflow: hidden; background: var(--bg-primary); }
.camera-viewport { position: relative; aspect-ratio: 16 / 9; }
.camera-video { display: block; width: 100%; height: 100%; object-fit: contain; }
.camera-overlay { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; padding: 8px; background: var(--bg-primary); color: var(--text-secondary); text-align: center; font-size: 12px; }
.camera-overlay-spinner { width: 18px; height: 18px; border: 2px solid var(--border-color); border-top-color: var(--text-secondary); border-radius: 50%; animation: camera-spin 1s linear infinite; flex-shrink: 0; }
@keyframes camera-spin { to { transform: rotate(360deg); } }
.camera-tile-caption { padding: 7px 9px; overflow-wrap: anywhere; }
.camera-empty { padding: 40px 16px; text-align: center; color: var(--text-secondary); font-size: 12px; }
.camera-expanded-dialog { width: min(1200px, calc(100vw - 32px)); max-width: none; max-height: calc(100dvh - 32px); margin: auto; padding: 0; border: 1px solid var(--border-color); border-radius: 12px; background: var(--bg-surface); color: var(--text-primary); }
.camera-expanded-dialog[open] { display: flex; flex-direction: column; }
.camera-expanded-dialog::backdrop { background: #000b; }
.camera-expanded-header { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px; border-bottom: 1px solid var(--border-color); flex-shrink: 0; }
.camera-expanded-content { display: flex; flex-direction: column; min-height: 0; padding: 12px; }
.camera-expanded-content .camera-panel { min-height: 0; }
.camera-expanded-content .camera-expand-button { display: none; }
.camera-expanded-content .camera-grid-multiple { grid-template-columns: repeat(2, minmax(0, 1fr)); }
@media (max-width: 479px) {
    .camera-grid.camera-grid-many, .camera-expanded-content .camera-grid-multiple { grid-template-columns: minmax(0, 1fr); }
    .camera-expanded-dialog { width: calc(100vw - 16px); max-height: calc(100dvh - 16px); }
}
