:root {
    color-scheme: dark;
    /* Neutral graphite palette — kept in sync with tailwind.config.js.
       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: #969EAD;
    --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;
}

/* 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); }

/* Consistent focus ring for interactive elements */
a:focus-visible,
button:focus-visible,
textarea:focus-visible,
input:focus-visible,
select:focus-visible,
summary:focus-visible,
[role="button"]:focus-visible {
    outline: 2px solid #7FB1FF;
    outline-offset: 2px;
    border-radius: 0.375rem;
}

/* Baseline hit-area contract. Feature styles may make controls larger, but no
   native action or form control should fall below the 44px touch minimum. */
button,
[role="button"],
select,
textarea,
input:not([type="hidden"]):not([type="checkbox"]):not([type="radio"]):not([type="range"]) {
    min-height: 2.75rem;
}

/* Shared interaction primitives. A 44px minimum keeps primary, destructive,
   navigation, and icon controls comfortably usable on touch screens. */
.btn {
    display: inline-flex;
    min-height: 2.75rem;
    align-items: center;
    justify-content: center;
    gap: 0.375rem;
    border-radius: 0.625rem;
    padding: 0.625rem 1rem;
    font-size: 0.875rem;
    font-weight: 600;
    line-height: 1.25rem;
    transition: background-color 0.15s ease, border-color 0.15s ease,
        color 0.15s ease, filter 0.15s ease;
}
.btn-icon {
    display: inline-flex;
    width: 2.75rem;
    height: 2.75rem;
    flex: 0 0 2.75rem;
    align-items: center;
    justify-content: center;
    border-radius: 0.625rem;
}
.btn-secondary {
    color: #D5D9E1;
    background: #252B36;
    border: 1px solid #3A4250;
}
.btn-secondary:hover { background: #303744; color: #fff; }
.btn-danger {
    color: #fff;
    background: #B93F38;
    border: 1px solid #D95A52;
}
.btn-danger:hover { background: #C94A42; }
.btn-danger-subtle {
    color: #F2AAA5;
    background: rgba(127, 29, 29, 0.34);
    border: 1px solid rgba(185, 63, 56, 0.65);
}
.btn-danger-subtle:hover { color: #FFD3D0; background: rgba(153, 27, 27, 0.48); }

html.app-dialog-open { overflow: hidden; }
[inert] { pointer-events: none !important; user-select: none !important; }

/* Global breadcrumb lives in a stable header location. On compact screens it
   becomes its own row so hierarchy and parent navigation never disappear. */
.app-breadcrumb { min-width: 0; flex: 1 1 auto; }
.app-breadcrumb ol {
    display: flex;
    min-width: 0;
    align-items: center;
    gap: 0.45rem;
    color: var(--text-secondary);
    font-size: 0.8125rem;
    white-space: nowrap;
}
.app-breadcrumb li {
    display: inline-flex;
    min-width: 0;
    align-items: center;
}
.app-breadcrumb li + li::before {
    content: "/";
    margin-right: 0.45rem;
    color: #4F5868;
}
.app-breadcrumb a {
    display: inline-flex;
    min-width: 2.75rem;
    max-width: min(18rem, 30vw);
    min-height: 2.75rem;
    align-items: center;
    padding-inline: 0.5rem;
    color: #B5BBC7;
    overflow: hidden;
    text-decoration: none;
    text-overflow: ellipsis;
}
.app-breadcrumb a:hover { color: #fff; }
.app-breadcrumb [aria-current="page"] {
    min-width: 0;
    overflow: hidden;
    color: #D8DCE4;
    text-overflow: ellipsis;
}
.app-brand-short { display: none; }
@media (max-width: 479px) {
    .app-brand-full { display: none; }
    .app-brand-short { display: inline; }
}
@media (max-width: 639px) {
    .app-breadcrumb {
        order: 3;
        flex-basis: calc(100% + 1.5rem);
        margin: 0 -0.75rem -0.5rem;
        padding: 0 0.75rem;
        border-top: 1px solid rgba(51, 65, 85, 0.52);
        background: rgba(11, 13, 18, 0.5);
    }
    .app-breadcrumb ol { min-height: 2.75rem; }
    .app-breadcrumb a { max-width: 7.5rem; }
}

/* ---------------------------------------------------------------------------
 * 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);
}

/* Account-menu scrim. This remains a first-party CSS primitive so its compact
   tablet behavior is independent of generated utility classes. The
   header/menu sits at z-30/50 above this layer. */
.menu-backdrop {
    position: fixed;
    inset: 0;
    z-index: 20;
    background: rgba(0, 0, 0, 0.65);
    -webkit-backdrop-filter: blur(1px);
    backdrop-filter: blur(1px);
}
.menu-backdrop.hidden { display: none; }
@media (min-width: 1024px) {
    .menu-backdrop { display: none !important; }
}

/* Dense inventory tables become labelled, scan-friendly rows on phones and
   compact tablets. Wide screens keep the data table; narrower users no longer
   have to discover a hidden horizontal scroll to reach state/actions. */
@media (max-width: 899px) {
    .responsive-stack-table {
        display: block;
        min-width: 0 !important;
        width: 100%;
    }
    .responsive-stack-table thead {
        position: absolute;
        width: 1px;
        height: 1px;
        padding: 0;
        margin: -1px;
        overflow: hidden;
        clip: rect(0, 0, 0, 0);
        white-space: nowrap;
        border: 0;
    }
    .responsive-stack-table tbody,
    .responsive-stack-table tr { display: block; width: 100%; }
    .responsive-stack-table tbody tr {
        padding: 0.75rem 1rem;
        background: rgba(16, 19, 25, 0.42);
    }
    .responsive-stack-table tbody tr + tr {
        border-top: 1px solid #262B35;
    }
    .responsive-stack-table tbody td {
        display: grid;
        grid-template-columns: minmax(6.5rem, 40%) minmax(0, 1fr);
        align-items: start;
        gap: 1rem;
        width: 100%;
        padding: 0.375rem 0 !important;
        border: 0 !important;
        text-align: right !important;
        overflow-wrap: anywhere;
    }
    .responsive-stack-table tbody td::before {
        content: attr(data-label);
        color: #969EAD;
        font-size: 0.75rem;
        font-weight: 600;
        letter-spacing: 0.04em;
        line-height: 1.25rem;
        text-align: left;
        text-transform: uppercase;
    }
    .responsive-stack-table tbody td[data-primary] {
        display: block;
        margin-bottom: 0.375rem;
        padding: 0.125rem 0 0.75rem !important;
        border-bottom: 1px solid #262B35 !important;
        font-size: 1rem;
        text-align: left !important;
    }
    .responsive-stack-table tbody td[data-primary]::before { display: none; }
    .responsive-stack-table tbody td[data-actions] {
        margin-top: 0.375rem;
        padding-top: 0.75rem !important;
        border-top: 1px solid rgba(38, 43, 53, 0.7) !important;
    }
    .responsive-stack-table tbody td > * { justify-self: end; }
    .responsive-stack-table tbody td[data-primary] > * { justify-self: start; }
    .responsive-stack-table tbody td .responsive-cell-value {
        grid-column: 2;
        justify-self: end;
        min-width: 0;
        text-align: right;
    }
}

/* 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 {
    display: inline-flex;
    min-height: 2.75rem;
    align-items: center;
    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); }
.pid-chip.copy-failed { color: #F2AAA5; border-color: rgba(225, 93, 84, 0.55); }

/* 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;
    }
}
