/* ==========================================================================
   DarkWeb Monitor — the infrastructure graph (/infrastructure)
   --------------------------------------------------------------------------
   Two jobs, the same two charts.css has:

     1. Publish the --graph-* palette. graph.js reads it off <html> with
        getComputedStyle and re-reads it on every 'dwm:themechange', which is
        what stops the graph carrying its own colours.
     2. Lay out the page.

   Nothing here declares a raw colour. Every --graph-* value is an alias of a
   token from tokens.css, or of a --chart-* alias charts.css already publishes,
   which keeps a cluster the same hue here as a series is on /stats.

   The aliases are plain var() references and never color-mix(), for the reason
   charts.css documents: an unregistered custom property computes to its
   substituted token stream, so var(--cyan) reads back as a literal colour
   ECharts can parse, while color-mix(...) comes back unresolved and silently
   breaks every series. Tints, alpha and gradients are mixed in graph.js from a
   value that did resolve.

   This sheet is not in layout.php's stylesheet list; pages/infrastructure.php
   links it itself. Moving 'graph.css' into that array is the tidier home and
   makes the <link> in the template redundant.
   ========================================================================== */

:root {
    /* Cluster ramp. graph.js extends it with a pale and a deep pass when there
       are more clusters than hues, so the ninth and the eighteenth cluster are
       still distinguishable. */
    --graph-1: var(--cyan);
    --graph-2: var(--ok);
    --graph-3: var(--warn);
    --graph-4: var(--violet);
    --graph-5: var(--state-broken);
    --graph-6: var(--info);
    --graph-7: var(--bad);
    --graph-8: var(--blue);
    --graph-9: var(--muted);

    /* The two ends graph.js mixes towards. */
    --graph-hi: var(--light);
    --graph-lo: var(--ink);

    --graph-edge:        var(--border-strong);
    --graph-edge-strong: var(--accent);
    --graph-node-line:   var(--surface);
    --graph-focus:       var(--accent);
    --graph-dimmed:      var(--text-faint);

    --graph-text:    var(--text);
    --graph-dim:     var(--text-dim);
    --graph-faint:   var(--text-faint);
    --graph-surface: var(--surface-2);
    --graph-line:    var(--border);
}

/* ------------------------------------------------------------------ page --- */

.infra {
    display: flex;
    flex-direction: column;
    gap: 1.4rem;
}

.infra__head h1 {
    margin: .2rem 0 .5rem;
}

.infra__lede {
    max-width: 78ch;
    color: var(--text-dim);
    line-height: 1.65;
    margin: 0;
}

.infra__counters {
    margin: 0;
}

/* --------------------------------------------------------------- toolbar --- */

.infra__toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: .5rem;
}

.infra__toolbar-spacer {
    flex: 1 1 auto;
}

.infra__filters {
    display: flex;
    flex-wrap: wrap;
    gap: .4rem;
}

/* A filter chip is a real <button>, so it needs the chip's look without the
   user-agent button chrome underneath it. */
.infra__filters .chip {
    font: inherit;
    font-family: var(--font-mono);
    font-size: .78rem;
    background: var(--surface-2);
    border: 1px solid var(--border);
    color: var(--text-dim);
    cursor: pointer;
}

.infra__filters .chip:hover {
    background: var(--surface-hover);
    color: var(--text);
}

.infra__filters .chip.is-active {
    background: var(--surface-3);
    border-color: var(--border-strong);
    color: var(--text);
}

.chip__count {
    margin-left: .35rem;
    padding: .05rem .35rem;
    border-radius: var(--radius-pill);
    background: var(--surface-3);
    color: var(--text-faint);
    font-size: .72rem;
}

.chip.is-active .chip__count {
    background: var(--accent-2);
    color: var(--light);
}

/* ----------------------------------------------------------------- stage --- */

.infra__stage {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 22rem;
    gap: 1rem;
    align-items: start;
}

.infra__panel,
.infra__rail {
    margin: 0;
}

.graph {
    width: 100%;
    height: clamp(28rem, 62vh, 46rem);
    /* A canvas with no size renders nothing and reports no error, which is a
       miserable thing to debug. */
    min-height: 24rem;
}

/* No JavaScript, no canvas. The cluster table below is the page. */
html:not(.has-js) .graph,
html:not(.has-js) .infra__rail,
html:not(.has-js) .infra__filters,
html:not(.has-js) [data-graph-reset] {
    display: none;
}

html:not(.has-js) .infra__stage {
    grid-template-columns: minmax(0, 1fr);
}

.graph-note {
    display: flex;
    gap: .5rem;
    align-items: flex-start;
    margin: .8rem 0 0;
    color: var(--text-faint);
    font-size: .85rem;
    line-height: 1.6;
}

.graph-note .icon {
    flex: 0 0 auto;
    margin-top: .15rem;
}

/* ------------------------------------------------------------------ rail --- */

.infra__rail-body {
    display: flex;
    flex-direction: column;
    gap: .7rem;
    max-height: clamp(24rem, 58vh, 42rem);
    overflow-y: auto;
}

.infra__hint {
    display: flex;
    gap: .5rem;
    align-items: flex-start;
    margin: 0;
    color: var(--text-faint);
    font-size: .86rem;
    line-height: 1.6;
}

.infra__hint .icon {
    flex: 0 0 auto;
    margin-top: .15rem;
}

/* Built by graph.js through the CSSOM, never innerHTML - style-src 'self'
   would drop a style attribute in an HTML string and the swatches would all
   come out colourless. */
.rail-title {
    margin: 0;
    font-family: var(--font-display);
    font-size: 1rem;
    color: var(--text);
    letter-spacing: .01em;
}

.rail-sub {
    margin: 0;
    color: var(--text-dim);
    font-size: .82rem;
    line-height: 1.55;
}

.rail-sub code {
    font-family: var(--font-mono);
    font-size: .78rem;
    color: var(--text-faint);
    word-break: break-all;
}

.rail-list {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    gap: .45rem;
}

.rail-signal {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr);
    gap: .1rem .55rem;
    padding: .5rem .6rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface-2);
}

.rail-signal__kind {
    grid-column: 1 / -1;
    font-family: var(--font-mono);
    font-size: .72rem;
    text-transform: uppercase;
    letter-spacing: .06em;
    color: var(--text-faint);
}

.rail-signal__value {
    grid-column: 1 / -1;
    font-family: var(--font-mono);
    font-size: .78rem;
    color: var(--text);
    word-break: break-word;
}

.rail-signal__meta {
    grid-column: 1 / -1;
    font-size: .74rem;
    color: var(--text-dim);
}

/* The weight bar. Width is the only thing graph.js writes to it. */
.rail-bar {
    grid-column: 1 / -1;
    height: 3px;
    margin-top: .3rem;
    border-radius: var(--radius-pill);
    background: var(--surface-3);
    overflow: hidden;
}

.rail-bar__fill {
    display: block;
    height: 100%;
    border-radius: var(--radius-pill);
    background: var(--accent);
}

.rail-actions {
    display: flex;
    flex-wrap: wrap;
    gap: .4rem;
    margin-top: .2rem;
}

/* ---------------------------------------------------------------- legend --- */

.legend-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(17rem, 1fr));
    gap: .9rem 1.4rem;
    margin-bottom: 1.2rem;
}

.legend-item {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr);
    gap: .1rem .7rem;
    align-items: start;
}

.legend-item p {
    margin: 0;
    color: var(--text-dim);
    font-size: .86rem;
    line-height: 1.6;
}

.legend-item strong {
    color: var(--text);
}

.legend-item code {
    font-family: var(--font-mono);
    font-size: .78rem;
    color: var(--text-faint);
}

.legend-icon {
    margin-top: .2rem;
    color: var(--warn);
}

.legend-swatch {
    margin-top: .45rem;
    border-radius: var(--radius-pill);
}

.legend-swatch--edge {
    width: 2.4rem;
}

.legend-swatch--strong {
    height: 5px;
    background: var(--accent);
}

.legend-swatch--weak {
    height: 1px;
    background: var(--border-strong);
}

.legend-swatch--node {
    width: .9rem;
    height: .9rem;
    background: var(--cyan);
    border: 2px solid var(--surface);
    box-shadow: 0 0 0 1px var(--border-strong);
}

.legend-heading {
    margin: 0 0 .6rem;
    font-family: var(--font-display);
    font-size: .95rem;
    color: var(--text);
}

.legend-signals {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(19rem, 1fr));
    gap: .55rem 1.4rem;
    margin: 0;
}

.legend-signal dt {
    display: flex;
    align-items: center;
    gap: .4rem;
    font-family: var(--font-mono);
    font-size: .8rem;
    color: var(--text);
}

.legend-signal dd {
    margin: .1rem 0 0 1.4rem;
    color: var(--text-faint);
    font-size: .8rem;
    line-height: 1.55;
}

/* ----------------------------------------------------------- the tables --- */

.graph-figures td,
.graph-figures th {
    vertical-align: top;
}

.infra__key,
.infra__host {
    display: block;
    font-family: var(--font-mono);
    font-size: .72rem;
    color: var(--text-faint);
    word-break: break-all;
}

.infra__members .state-dot {
    grid-row: 1;
}

.infra__members .infra__host {
    grid-column: 2;
}

.infra__degree {
    grid-column: 3;
    grid-row: 1;
    font-family: var(--font-mono);
    font-size: .72rem;
    color: var(--text-faint);
    white-space: nowrap;
}

.tag[data-confidence='strong']  { color: var(--state-alive); border-color: var(--state-alive); }
.tag[data-confidence='good']    { color: var(--info); border-color: var(--info); }
.tag[data-confidence='partial'] { color: var(--state-blocked); border-color: var(--state-blocked); }
.tag[data-confidence='thin']    { color: var(--text-faint); border-color: var(--border-strong); }

.table--coverage td,
.table--coverage th {
    white-space: nowrap;
}

.table--coverage tbody th {
    display: flex;
    align-items: center;
    gap: .45rem;
    font-weight: 500;
}

/* A row highlighted from the graph, so clicking a node in the canvas leads the
   eye to the same cluster in the table. */
.graph-figures tr.is-focused > th,
.graph-figures tr.is-focused > td {
    background: var(--surface-hover);
}

/* --------------------------------------------------------------- narrow --- */

@media (max-width: 1080px) {
    .infra__stage {
        grid-template-columns: minmax(0, 1fr);
    }

    .infra__rail-body {
        max-height: none;
    }

    .graph {
        height: clamp(24rem, 56vh, 34rem);
    }
}

@media (max-width: 640px) {
    .infra__degree {
        grid-column: 2;
        grid-row: 3;
    }
}

/* A force simulation is motion. Honouring the preference means not running it,
   not running it faster - graph.js reads this same preference and starts the
   layout already settled. */
@media (prefers-reduced-motion: reduce) {
    .rail-bar__fill {
        transition: none;
    }
}

/* The topology word. Deliberately quieter than the confidence tag next to it:
   shape describes the evidence, it does not rank it. */
.tag--shape {
    font-family: var(--font-mono);
    font-size: .72rem;
    color: var(--text-dim);
    border-color: var(--border);
}

/* -----------------------------------------------------------------------------
   Clusters and members
   -------------------------------------------------------------------------- */

/* One <tbody> per cluster: a summary row, then a row per member.

   Members were previously a <ul> inside the summary row's last <td>. With seven
   other columns competing for the width that cell got roughly fourteen
   characters, so a 56-character onion address wrapped into a tall thin ribbon
   and each cluster grew to several hundred pixels with an empty middle. Giving
   each member its own row hands the address the full table width. */

.infra__cluster + .infra__cluster { border-top: 2px solid var(--border-strong); }

.infra__cluster-row > * {
    padding-top: .7rem;
    padding-bottom: .5rem;
    background: color-mix(in srgb, var(--surface-3) 55%, transparent);
}
.infra__cluster-row th { font-weight: 600; }

.infra__member-row > * {
    padding-top: .28rem;
    padding-bottom: .28rem;
    border-top: 1px solid color-mix(in srgb, var(--border) 45%, transparent);
    vertical-align: baseline;
}

/* The indent and the rule are what make a member read as belonging to the
   cluster above it rather than as another cluster. */
.infra__member-name {
    padding-left: 1.6rem !important;
    position: relative;
    font-weight: 400;
    white-space: nowrap;
}
.infra__member-name::before {
    content: '';
    position: absolute;
    left: .7rem;
    top: 0;
    bottom: 0;
    border-left: 1px solid var(--border-strong);
}
.infra__member-name .state-dot { margin-right: .4rem; }

.infra__member-host code.infra__host {
    display: inline-block;
    max-width: 100%;
    padding: .05em .35em;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface-2);
    font-family: var(--font-mono);
    font-size: .78rem;
    color: var(--text-dim);
    /* An onion address has no spaces, so it must be told where it may break.
       Without this it either overflows the table or forces a horizontal
       scrollbar on the whole panel. */
    overflow-wrap: anywhere;
    line-height: 1.4;
}

.infra__member-degree {
    white-space: nowrap;
    font-size: .78rem;
    color: var(--text-faint);
}

/* Narrow screens: the colspan still holds, but the name column must be allowed
   to wrap or it pushes the address off the edge. */
@media (max-width: 60rem) {
    .infra__member-name { white-space: normal; }
    .infra__member-host code.infra__host { font-size: .72rem; }
}
