/* Alphinity Monitor — the monitor page's own layout. Every colour here is a
   `var(--…)` from `shell.css`'s `:root` (a Refinitiv-adjacent palette), which
   is what let the whole app be re-themed by editing one block; `web/figure.py`
   holds the same values by hand because Plotly paints its own canvas and
   cannot read a CSS variable, so a sidebar dot and a chart marker are the same
   colour for the same decision. The `:root` palette and the `html, body` reset
   live in `shell.css` (feature #37). */
/* Three columns under the chart: the tick INDEX, one tick's SUMMARY, and the
   one document you asked to read. The split exists because a tick's evidence
   is ~80k characters — a single column could only ever stack it, which is
   what it used to do. Now the summary answers "what and who" in a screenful
   and the reader holds exactly one thing at a time. */
#layout {
  display: grid;
  grid-template-columns:
    var(--sidebar-w, 300px) 6px
    var(--summary-w, 400px) 6px
    minmax(0, 1fr);
  grid-template-rows: var(--chart-h, 58%) 6px minmax(0, 1fr);
  min-height: 0;
  height: 100%;
}

#sidebar, #chart, #detail, #reader {
  overflow-y: auto;
  min-height: 0;
  padding: 12px;
}

#chart {
  grid-row: 1;
  grid-column: 1 / -1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#sidebar {
  grid-row: 3;
  grid-column: 1;
  border-right: 1px solid var(--border);
  background: var(--panel);
}

#detail {
  grid-row: 3;
  grid-column: 3;
  background: var(--panel);
  border-right: 1px solid var(--border);
}

#reader {
  grid-row: 3;
  grid-column: 5;
}

/* The two drag handles. They are the grid tracks between the regions, so
   dragging one only rewrites a CSS variable — no layout is recomputed by JS. */
#row-gutter, #col-gutter, #col-gutter-2 {
  background: var(--border);
  transition: background 120ms ease;
  touch-action: none;
}

#row-gutter {
  grid-row: 2;
  grid-column: 1 / -1;
  cursor: row-resize;
}

#col-gutter {
  grid-row: 3;
  grid-column: 2;
  cursor: col-resize;
}

#col-gutter-2 {
  grid-row: 3;
  grid-column: 4;
  cursor: col-resize;
}

#row-gutter:hover, #col-gutter:hover, #col-gutter-2:hover,
#row-gutter:focus-visible, #col-gutter:focus-visible, #col-gutter-2:focus-visible,
#layout.is-dragging #row-gutter, #layout.is-dragging #col-gutter,
#layout.is-dragging #col-gutter-2 {
  background: var(--selected, #3fa9d6);
  outline: none;
}

/* While dragging, stop the pointer from selecting text or landing on the
   Plotly canvas, which would otherwise start a pan mid-drag. */
#layout.is-dragging {
  user-select: none;
}

#layout.is-dragging #chart-plot {
  pointer-events: none;
}

#sidebar-header h1 {
  font-size: 14px;
  letter-spacing: 0.05em;
  margin: 0 0 4px 0;
}

#sidebar-meta, #cache-badge {
  color: var(--text-dim);
  margin: 2px 0;
}

#refresh-button {
  margin-top: 8px;
  width: 100%;
  background: var(--border);
  color: var(--text);
  border: 1px solid var(--text-dim);
  border-radius: 4px;
  padding: 6px;
  cursor: pointer;
}

#refresh-button:hover {
  background: var(--text-dim);
}

#refresh-error {
  color: var(--sell);
  margin-top: 6px;
}

#sidebar-list {
  list-style: none;
  margin: 8px 0 0 0;
  padding: 0;
}

.sidebar-row {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 4px;
  border-radius: 3px;
  cursor: pointer;
}

.sidebar-row:hover {
  background: var(--border);
}

.sidebar-row[aria-selected="true"] {
  background: var(--border);
  outline: 1px solid var(--selected);
}

.decision-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
}

.decision-dot.buy { background: var(--buy); }
.decision-dot.sell { background: var(--sell); }
.decision-dot.hold { background: var(--hold); }
.decision-dot.abstain { background: var(--abstain); border: 1px solid var(--abstain); background-color: transparent; }
.decision-dot.incomplete { background: var(--incomplete); }

#granularity-buttons {
  margin-bottom: 8px;
}

#granularity-buttons button {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 4px 10px;
  margin-right: 4px;
  cursor: pointer;
  border-radius: 3px;
}

#granularity-buttons button.granularity-active {
  background: var(--border);
  outline: 1px solid var(--selected);
}

#chart-plot {
  width: 100%;
  flex: 1 1 auto;
  min-height: 0;
}

#detail-empty, #reader-empty {
  color: var(--text-dim);
}

/* ── Column 2: the summary ─────────────────────────────────────────────── */

.sum-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.sum-time {
  font-size: 15px;
  font-weight: 600;
  letter-spacing: 0.02em;
}

.decision-chip {
  padding: 2px 10px;
  border-radius: 3px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.06em;
  color: #12161c;
}

.decision-chip.buy { background: var(--buy); }
.decision-chip.sell { background: var(--sell); }
.decision-chip.hold { background: var(--hold); }
.decision-chip.abstain { background: var(--abstain); }
.decision-chip.incomplete { background: var(--incomplete); }

.sum-sub {
  color: var(--text-dim);
  margin: 2px 0 0 0;
}

.sum-issues {
  color: var(--incomplete);
  margin: 4px 0 0 0;
}

.sum-h {
  font-size: 10px;
  letter-spacing: 0.08em;
  color: var(--text-dim);
  margin: 18px 0 6px 0;
  padding-bottom: 3px;
  border-bottom: 1px solid var(--border);
}

.sum-why {
  margin: 0;
  line-height: 1.5;
}

/* ── The flags: one chip per lens, colour = its final flag ─────────────── */

.flag-grid {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.flag-chip {
  display: grid;
  grid-template-columns: 10px 1fr auto auto;
  align-items: center;
  gap: 7px;
  width: 100%;
  padding: 5px 8px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 3px;
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.flag-chip:hover, .flag-chip:focus-visible {
  border-color: var(--selected);
  outline: none;
}

.flag-chip.doc-active {
  border-color: var(--selected);
  background: var(--border);
}

.flag-chip.is-static {
  display: inline-grid;
  grid-template-columns: 10px auto;
  width: auto;
  cursor: default;
  padding: 1px 7px;
}

.flag-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--hold);
}

.flag-bullish .flag-dot { background: var(--buy); }
.flag-bearish .flag-dot { background: var(--sell); }
.flag-neutral .flag-dot { background: var(--hold); }
.flag-noread .flag-dot { background: transparent; border: 1px solid var(--abstain); }

.flag-value {
  color: var(--text-dim);
  font-size: 11px;
}

.flag-bullish .flag-value { color: var(--buy); }
.flag-bearish .flag-value { color: var(--sell); }

/* A lens that changed its flag between the blind and the sighted round is the
   most interesting event in a tick — Dalio's "was anyone open?" — so it gets
   its own mark rather than being something you find by reading both turns. */
.flag-moved {
  color: var(--incomplete);
  font-size: 12px;
}

/* ── Trade numbers and the drill-in buttons ────────────────────────────── */

.kv-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 2px 12px;
}

.kv {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  border-bottom: 1px solid var(--border);
  padding: 2px 0;
}

.kv .k { color: var(--text-dim); }
.kv .v { font-variant-numeric: tabular-nums; }

.doc-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.doc-button {
  padding: 4px 9px;
  background: var(--border);
  border: 1px solid transparent;
  border-radius: 3px;
  color: var(--text);
  font: inherit;
  cursor: pointer;
}

.doc-button:hover, .doc-button:focus-visible {
  border-color: var(--selected);
  outline: none;
}

.doc-button.doc-active {
  background: var(--selected);
  color: #12161c;
}

/* ── Column 3: the reader, one document at a time ──────────────────────── */

.reader-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  position: sticky;
  top: -12px;
  background: var(--bg);
  padding: 4px 0 6px 0;
  border-bottom: 1px solid var(--border);
}

.reader-head h2 {
  font-size: 12px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  margin: 0;
}

#reader-close {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 3px;
  color: var(--text-dim);
  font-size: 14px;
  line-height: 1;
  padding: 2px 7px;
  cursor: pointer;
}

#reader-close:hover, #reader-close:focus-visible {
  color: var(--text);
  border-color: var(--selected);
  outline: none;
}

.reader-body {
  padding-top: 8px;
  line-height: 1.55;
}

.reader-body img {
  max-width: 100%;
  border: 1px solid var(--border);
  margin-bottom: 10px;
}

.reader-body pre {
  white-space: pre-wrap;
  word-break: break-word;
}

.reason-block {
  margin-bottom: 14px;
}

.reason-block h4 {
  font-size: 10px;
  letter-spacing: 0.08em;
  color: var(--incomplete);
  margin: 0 0 3px 0;
}

.reason-block p {
  margin: 0;
}

.turn {
  border-top: 1px solid var(--border);
  padding-top: 8px;
  margin-top: 10px;
}

.turn-head {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.turn-round {
  font-size: 10px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-dim);
}

.turn-agent {
  font-weight: 600;
}

.turn-stance {
  color: var(--text-dim);
}

.turn-summary {
  margin: 6px 0 0 0;
  color: var(--text);
}

.turn details summary {
  cursor: pointer;
  color: var(--text-dim);
  margin-top: 6px;
}

/* What the lens actually looked at, above what it concluded. */
/* An INDEX of charts, four to a row — eight charts are two rows of four.
   `repeat(4, minmax(0, 1fr))` rather than `auto-fit`: the count must not
   change with the container's width, or the same tick would lay out
   differently in two column widths and a reader could not say "third one,
   second row". `minmax(0, …)` is what stops a wide PNG from forcing its
   column past its share. */
.lens-charts {
  margin-bottom: 12px;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
}

/* A single chart is not a grid — one thumbnail alone in a quarter-width column
   is unreadable for no gain, so it takes the full column. */
.lens-charts:has(.lens-chart:only-child) {
  grid-template-columns: minmax(0, 1fr);
}

.lens-chart {
  margin: 0;
  min-width: 0;
}

/* EVERY thumbnail is the same box, whatever the PNG's own aspect. That is the
   whole point of the grid: a row can be compared at a glance. `contain`, never
   `cover` — cropping a chart hides data, and the one thing this panel must not
   do is show a lens's evidence with part of it cut off. Letterboxing is the
   honest trade, and the full size is one click away. */
.chart-thumb {
  display: block;
  width: 100%;
  aspect-ratio: 16 / 10;
  padding: 0;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 4px;
  cursor: zoom-in;
  overflow: hidden;
}

.chart-thumb:hover, .chart-thumb:focus-visible {
  border-color: var(--selected);
  outline: none;
}

.chart-thumb img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
}

.lens-chart figcaption {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text-dim);
  font-size: 11px;
  margin-top: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* The full-size overlay — where a chart is actually read. */
#chart-zoom {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 24px;
  background: rgba(0, 0, 0, 0.85);
  cursor: zoom-out;
}

#chart-zoom[hidden] {
  display: none;
}

#chart-zoom img {
  max-width: 100%;
  max-height: calc(100% - 40px);
  object-fit: contain;
}

#chart-zoom p {
  margin: 0;
  color: #dfe3e8;
  font-size: 12px;
}

.chart-recorded {
  padding: 0 6px;
  border: 1px solid var(--incomplete);
  border-radius: 3px;
  color: var(--incomplete);
  letter-spacing: 0.04em;
}

.detail-section summary {
  cursor: pointer;
  font-weight: 600;
  padding: 6px 0;
  border-bottom: 1px solid var(--border);
}

.detail-section {
  margin-bottom: 8px;
}

.trade-table td, .levels-table td, .levels-table th {
  padding: 2px 6px;
  border-bottom: 1px solid var(--border);
}

.agent-note, .debate-turn {
  border-bottom: 1px solid var(--border);
  padding: 6px 0;
}

img.evidence-png {
  max-width: 100%;
  margin-top: 6px;
  border: 1px solid var(--border);
}


/* ── The timing graph: where the tick's time went ──────────────────────────
   A gantt, not a table of instants. The question it answers — "what should I
   optimise" — is a question about PROPORTION, and the longest bar answers it
   before any number is read. The instants are printed all the same: an
   absolute UTC time is what makes a run reconcilable against Databricks, the
   trigger table and the Telegram scroll afterwards. */
.timing-head {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 2px 12px;
  margin: 0 0 12px 0;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--border);
  font-variant-numeric: tabular-nums;
}

.timing-head dt {
  color: var(--text-dim);
}

.timing-head dd {
  margin: 0;
}

.timing-open {
  color: var(--incomplete);
}

.timing-steps {
  list-style: none;
  margin: 0;
  padding: 0;
}

.timing-step {
  display: grid;
  grid-template-columns: max-content max-content minmax(60px, 1fr) max-content;
  align-items: center;
  gap: 10px;
  padding: 3px 0;
  font-variant-numeric: tabular-nums;
}

.timing-at {
  color: var(--text-dim);
  font-size: 11px;
}

.timing-phase {
  font-size: 11px;
}

.timing-bar {
  background: var(--bg);
  border-radius: 3px;
  height: 14px;
  overflow: hidden;
}

.timing-fill {
  display: block;
  height: 100%;
  background: var(--selected);
  border-radius: 3px;
  /* A measured gap of zero must still be visible as a bar that exists — an
     empty track reads as "no data", which is a different fact. */
  min-width: 2px;
}

.timing-start {
  display: block;
  padding-left: 4px;
  font-size: 10px;
  line-height: 14px;
  color: var(--text-dim);
  letter-spacing: 0.05em;
}

.timing-delta {
  font-size: 11px;
  min-width: 62px;
  text-align: right;
}

/* The bottleneck names itself. One colour, used once — if everything were
   highlighted the highlight would say nothing. */
.timing-step.is-slowest .timing-fill {
  background: var(--incomplete);
}

.timing-step.is-slowest .timing-delta {
  color: var(--incomplete);
  font-weight: 600;
}

.timing-note {
  margin: 12px 0 0 0;
  padding-top: 10px;
  border-top: 1px solid var(--border);
  color: var(--text-dim);
  font-size: 11px;
}

/* An agent row is a step INSIDE a phase, so it is indented and quieter — the
   phase transitions are the skeleton and the agents hang off them. */
.timing-step.is-agent .timing-at {
  padding-left: 14px;
}

.timing-step.is-agent .timing-phase {
  color: var(--text-dim);
}

.timing-step.is-agent .timing-fill {
  background: var(--hold);
}

.timing-round {
  font-size: 10px;
  letter-spacing: 0.04em;
  color: var(--text-dim);
  text-transform: uppercase;
}

/* The agent whose output landed LAST — the one the phase was waiting on. The
   only per-agent claim concurrency lets this view make honestly, so it is the
   only one marked. */
.timing-step.is-gate .timing-phase::after {
  content: " \25B8 gated the phase";
  color: var(--selected);
  font-size: 10px;
  letter-spacing: 0.03em;
}

.timing-step.is-gate .timing-fill {
  background: var(--selected);
}
