/* Trade visualisation platform — design tokens & layout.
 *
 * Colour roles come from the validated palette; the sector hues themselves are
 * served by the API (light + dark column) so Python and the browser can never
 * disagree about what colour a sector is.
 *
 * Dark mode is a selected set, not an inverted one: the media query covers the
 * OS setting and the [data-theme] scope covers the in-page toggle, which must
 * win in both directions.
 */

:root {
  color-scheme: light;

  --surface-1: #fcfcfb;          /* chart surface */
  --surface-page: #f9f9f7;       /* page plane */
  --surface-raised: #ffffff;

  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;

  --grid: #e1e0d9;               /* hairline gridline */
  --axis: #c3c2b7;               /* baseline / axis */
  --border: rgba(11, 11, 11, 0.10);
  --ghost: rgba(11, 11, 11, 0.04);

  --delta-good: #006300;
  --delta-bad: #d03b3b;

  --radius: 6px;
  --radius-lg: 10px;
  --gap: 16px;

  --font: system-ui, -apple-system, "Segoe UI", "Noto Sans SC", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --surface-1: #1a1a19;
    --surface-page: #0d0d0d;
    --surface-raised: #212120;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --ghost: rgba(255, 255, 255, 0.06);
    --delta-good: #0ca30c;
    --delta-bad: #e66767;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --surface-1: #1a1a19;
  --surface-page: #0d0d0d;
  --surface-raised: #212120;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted: #898781;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.10);
  --ghost: rgba(255, 255, 255, 0.06);
  --delta-good: #0ca30c;
  --delta-bad: #e66767;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--font);
  background: var(--surface-page);
  color: var(--text-primary);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* ---------- app shell ---------- */

.app {
  display: grid;
  grid-template-columns: 216px 1fr;
  min-height: 100vh;
}

.sidebar {
  border-right: 1px solid var(--border);
  background: var(--surface-1);
  padding: 20px 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}

.brand {
  padding: 0 20px 18px;
  font-weight: 600;
  font-size: 15px;
  letter-spacing: -0.01em;
}
.brand small {
  display: block;
  font-weight: 400;
  font-size: 12px;
  color: var(--text-muted);
  letter-spacing: 0;
}

.nav-group-label {
  padding: 14px 20px 6px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 8px 20px;
  color: var(--text-secondary);
  text-decoration: none;
  border: none;
  background: none;
  font: inherit;
  cursor: pointer;
  width: 100%;
  text-align: left;
  border-left: 2px solid transparent;
}
.nav-item:hover { background: var(--ghost); color: var(--text-primary); }
.nav-item[aria-current="page"] {
  color: var(--text-primary);
  font-weight: 500;
  border-left-color: #2a78d6;
  background: var(--ghost);
}
.nav-item:disabled { opacity: 0.45; cursor: default; }
.nav-item:disabled:hover { background: none; color: var(--text-secondary); }

/* Width: generous but not edge-to-edge. A treemap does use extra width well
 * (more cells clear the direct-label threshold), so the column grows past the
 * usual ~1200px reading measure — but it stops at 2100px and adds roomier side
 * padding on large screens, so the page keeps a margin instead of running into
 * the bezel. */
.main { padding: 24px 28px 56px; width: 100%; max-width: 2100px; }

@media (min-width: 1700px) {
  .main { padding: 32px 48px 72px; }
}

/* Long-form text stays readable regardless of how wide the column gets. */
.page-head p, .note { max-width: 90ch; }

.page-head { margin-bottom: 20px; }
.page-head h1 {
  margin: 0 0 4px;
  font-size: 22px;
  font-weight: 600;
  letter-spacing: -0.015em;
}
.page-head p { margin: 0; color: var(--text-secondary); font-size: 13px; }

/* ---------- filter row: one row, above the charts ---------- */

.filters {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 12px;
  padding: 14px 16px;
  margin-bottom: var(--gap);
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
}

.field { display: flex; flex-direction: column; gap: 4px; }
.field > label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}

select, input[type="search"], button.btn {
  font: inherit;
  font-size: 13px;
  color: var(--text-primary);
  background: var(--surface-raised);
  border: 1px solid var(--axis);
  border-radius: var(--radius);
  padding: 6px 9px;
  min-height: 32px;
}
select:focus-visible, input:focus-visible, button:focus-visible {
  outline: 2px solid #2a78d6;
  outline-offset: 1px;
}

/* segmented control — flow switch */
.segmented { display: inline-flex; border: 1px solid var(--axis); border-radius: var(--radius); overflow: hidden; }
.segmented button {
  font: inherit; font-size: 13px; padding: 6px 13px; min-height: 32px;
  border: none; background: var(--surface-raised); color: var(--text-secondary);
  cursor: pointer;
}
.segmented button + button { border-left: 1px solid var(--axis); }
.segmented button[aria-pressed="true"] {
  background: #2a78d6; color: #fff; font-weight: 500;
}

.spacer { flex: 1; }

/* ---------- cards & stat tiles ---------- */

.card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 18px;
}

/* On a large screen the chart card gets more internal breathing room too, so the
 * treemap doesn't sit tight against the card edge. */
@media (min-width: 1700px) {
  .card { padding: 24px 26px; }
}
.card + .card { margin-top: var(--gap); }

.card-head {
  display: flex; align-items: baseline; justify-content: space-between;
  gap: 12px; margin-bottom: 14px;
}
.card-head h2 { margin: 0; font-size: 15px; font-weight: 600; }
.card-head .sub { color: var(--text-muted); font-size: 12px; }

/* auto-fit with a max keeps four tiles compact on a wide monitor instead of
 * stretching each one to 600px of mostly empty card. */
.stat-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 340px));
  gap: var(--gap);
  margin-bottom: var(--gap);
}
.stat {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 14px 16px;
}
.stat .label { font-size: 12px; color: var(--text-secondary); }
.stat .value {
  font-size: 26px; font-weight: 600; letter-spacing: -0.02em;
  margin-top: 2px; line-height: 1.15;
}
.stat .hint { font-size: 12px; color: var(--text-muted); margin-top: 2px; }
.stat .delta { font-size: 12px; font-weight: 500; margin-top: 2px; }
.delta.up { color: var(--delta-good); }
.delta.down { color: var(--delta-bad); }

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

.legend {
  display: flex; flex-wrap: wrap; gap: 6px 14px;
  margin-top: 14px; padding-top: 12px;
  border-top: 1px solid var(--grid);
}
.legend-item {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px; color: var(--text-secondary);
  background: none; border: none; font-family: inherit; cursor: pointer;
  padding: 2px 4px; border-radius: 4px;
}
.legend-item:hover { background: var(--ghost); color: var(--text-primary); }
.legend-item[aria-pressed="false"] { opacity: 0.4; }
.legend-swatch {
  width: 11px; height: 11px; border-radius: 2px; flex: none;
}
.legend-item .pct { color: var(--text-muted); font-variant-numeric: tabular-nums; }

/* ---------- tooltip ---------- */

.tooltip {
  position: fixed;
  z-index: 50;
  pointer-events: none;
  background: var(--surface-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.12), 0 1px 3px rgba(0, 0, 0, 0.08);
  padding: 9px 11px;
  font-size: 12px;
  max-width: 280px;
  opacity: 0;
  transition: opacity 90ms ease;
}
.tooltip[data-show="true"] { opacity: 1; }
.tooltip .tt-value {
  font-size: 15px; font-weight: 600; color: var(--text-primary);
  letter-spacing: -0.01em;
}
.tooltip .tt-name { color: var(--text-secondary); margin-bottom: 1px; }
.tooltip .tt-meta { color: var(--text-muted); margin-top: 3px; font-size: 11px; }
.tooltip .tt-key {
  display: inline-block; width: 12px; height: 2px; border-radius: 1px;
  vertical-align: middle; margin-right: 5px;
}

/* ---------- table view (the relief channel for sub-3:1 fills) ---------- */

.table-toggle {
  background: none; border: none; font: inherit; font-size: 12px;
  color: var(--text-secondary); cursor: pointer; padding: 4px 8px;
  border-radius: 4px; text-decoration: underline; text-underline-offset: 2px;
}
.table-toggle:hover { background: var(--ghost); color: var(--text-primary); }

table.data {
  width: 100%; border-collapse: collapse; font-size: 13px; margin-top: 12px;
}
table.data th, table.data td {
  text-align: right; padding: 6px 10px;
  border-bottom: 1px solid var(--grid);
  font-variant-numeric: tabular-nums;
}
table.data th:first-child, table.data td:first-child {
  text-align: left; font-variant-numeric: normal;
}
table.data th {
  font-size: 11px; font-weight: 600; text-transform: uppercase;
  letter-spacing: 0.05em; color: var(--text-muted);
  border-bottom-color: var(--axis);
}
table.data tbody tr:hover { background: var(--ghost); }
table.data .swatch {
  display: inline-block; width: 9px; height: 9px; border-radius: 2px;
  margin-right: 7px; vertical-align: baseline;
}

.table-wrap { overflow-x: auto; }

/* ---------- states ---------- */

.chart-host { position: relative; }
/* Refetch keeps the frame: hold the previous render, dimmed. */
.chart-host[data-loading="true"] { opacity: 0.45; transition: opacity 120ms; }

.empty, .error {
  padding: 40px 20px; text-align: center; color: var(--text-muted);
  font-size: 13px;
}
.error { color: var(--delta-bad); }

.note {
  font-size: 12px; color: var(--text-muted);
  margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--grid);
}

.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

/* treemap cells get their own gap via SVG geometry, not CSS */
svg { display: block; }
svg text { user-select: none; }

@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: static; height: auto; flex-direction: row; flex-wrap: wrap;
    align-items: center; padding: 12px; gap: 2px;
    border-right: none; border-bottom: 1px solid var(--border);
  }
  .brand { padding: 0 12px 0 8px; }
  .nav-group-label { display: none; }
  .nav-item { width: auto; padding: 6px 12px; border-left: none; border-bottom: 2px solid transparent; }
  .nav-item[aria-current="page"] { border-left-color: transparent; border-bottom-color: #2a78d6; }
  .main { padding: 16px; }
}

/* ---------- AI assistant chat ---------- */
.chat-log {
  min-height: 320px; max-height: 60vh; overflow-y: auto;
  display: flex; flex-direction: column; gap: 14px; padding: 6px 2px;
}
.chat-welcome { color: var(--text-muted); padding: 40px 20px; text-align: center; }
.chat-row { display: flex; flex-direction: column; }
.chat-user { align-items: flex-end; }
.chat-assistant { align-items: flex-start; }
.chat-bubble {
  max-width: 86%; padding: 10px 14px; border-radius: 12px;
  font-size: 13.5px; line-height: 1.55; overflow-wrap: break-word;
}
.chat-user .chat-bubble {
  background: var(--accent, #3987e5); color: #fff;
  border-bottom-right-radius: 4px; white-space: pre-wrap;
}
.chat-assistant .chat-bubble {
  background: var(--ghost); color: var(--text-primary);
  border-bottom-left-radius: 4px;
}
.chat-bubble p { margin: 0 0 8px; }
.chat-bubble p:last-child { margin-bottom: 0; }
.chat-bubble ul { margin: 4px 0 8px; padding-left: 20px; }
.chat-bubble li { margin: 2px 0; }
.chat-bubble h3, .chat-bubble h4, .chat-bubble h5 { margin: 10px 0 6px; }
.chat-bubble code {
  background: var(--surface-1); padding: 1px 5px; border-radius: 4px;
  font-size: 12.5px;
}
.chat-bubble .table-wrap { margin: 8px 0; }
.chat-tools { font-size: 11.5px; color: var(--text-muted); margin-top: 4px; }
.chat-status { font-size: 12px; color: var(--text-muted); font-style: italic; }
.chat-input-row {
  display: flex; gap: 10px; margin-top: 14px; align-items: flex-end;
}
.chat-input-row textarea {
  flex: 1; resize: vertical; min-height: 44px;
  background: var(--surface-1); color: var(--text-primary);
  border: 1px solid var(--border); border-radius: var(--radius);
  padding: 10px 12px; font: inherit; font-size: 13.5px;
}
.chat-input-row textarea:focus { outline: 2px solid var(--accent, #3987e5); }
.chat-send {
  background: var(--accent, #3987e5); color: #fff; border: 0;
  border-radius: var(--radius); padding: 11px 22px; font: inherit;
  font-size: 13.5px; cursor: pointer;
}
.chat-send:disabled { opacity: 0.5; cursor: default; }
#agent-banner {
  background: color-mix(in srgb, var(--accent, #3987e5) 8%, var(--surface-1));
  border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px 14px; margin-bottom: 12px; font-size: 13px;
  color: var(--text-secondary);
}
#agent-banner code {
  background: var(--surface-1); padding: 1px 5px; border-radius: 4px;
}

/* ---------- AI assistant floating widget ---------- */
#agent-fab {
  position: fixed; right: 26px; bottom: 26px; z-index: 70;
  width: 54px; height: 54px; border-radius: 50%; border: 0;
  background: var(--accent, #3987e5); color: #fff; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.22), 0 2px 5px rgba(0, 0, 0, 0.12);
  transition: transform 120ms ease;
}
#agent-fab:hover { transform: scale(1.07); }
#agent-fab[aria-expanded="true"] { transform: scale(0.94); }
#agent-panel {
  position: fixed; right: 26px; bottom: 92px; z-index: 70;
  width: min(460px, calc(100vw - 40px));
  height: min(640px, calc(100vh - 130px));
  display: flex; flex-direction: column;
  background: var(--surface-1); border: 1px solid var(--border);
  border-radius: 14px; padding: 16px 16px 12px;
  box-shadow: 0 14px 40px rgba(0, 0, 0, 0.24), 0 3px 9px rgba(0, 0, 0, 0.12);
}
#agent-panel[hidden] { display: none; }
.agent-panel-head {
  display: flex; justify-content: space-between; align-items: flex-start;
  margin-bottom: 10px; gap: 8px;
}
.agent-panel-head h2 { margin: 0; font-size: 15px; }
.agent-panel-head .sub { font-size: 11.5px; color: var(--text-muted); margin-top: 2px; }
.agent-panel-actions { display: flex; gap: 6px; flex-shrink: 0; }
#agent-panel .chat-log { flex: 1; min-height: 0; max-height: none; }
#agent-panel .note { margin: 8px 0 0; font-size: 10.5px; }

/* ---------- BoL bar lists & origin stacks ---------- */
.barlist-row { display: flex; align-items: center; gap: 10px; padding: 4px 0; }
.barlist-label {
  flex: 0 0 190px; font-size: 12.5px; color: var(--text-primary);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.barlist-track { flex: 1; height: 14px; background: var(--ghost); border-radius: 4px; }
.barlist-track i {
  display: block; height: 100%; border-radius: 4px;
  background: var(--accent, #3987e5); min-width: 2px;
}
.barlist-value {
  flex: 0 0 70px; text-align: right; font-size: 12px;
  color: var(--text-muted); font-variant-numeric: tabular-nums;
}
.stack-row { display: flex; align-items: center; gap: 10px; padding: 5px 0; }
.stack-label { flex: 0 0 44px; font-size: 12.5px; color: var(--text-secondary);
  font-variant-numeric: tabular-nums; }
.stack-track { flex: 1; height: 22px; display: flex; border-radius: 5px;
  overflow: hidden; background: var(--ghost); }
.stack-track i { display: block; height: 100%; margin-right: 1px; }
.card-grid-3 {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 18px;
}

.stack-label-wide { flex: 0 0 185px; white-space: nowrap; overflow: hidden;
  text-overflow: ellipsis; color: var(--text-primary); }


/* The brand block doubles as the way back to the Vestigia portal: the parent
   name sits above the section name, so a reader deep in the platform can see
   what this is part of. */
.brand { display: block; text-decoration: none; color: inherit; }
.brand .parent {
  display: block;
  font-family: Georgia, "Songti SC", serif;
  font-size: 12px;
  letter-spacing: .04em;
  color: var(--text-muted);
  margin-bottom: 2px;
}
.brand:hover .parent { color: var(--text-primary); }

/* ---- Vestigia bloc chrome (shared with the trade platform's app.css) ---- */
/* The brand block doubles as the way back to the portal. */
.brand { display: block; text-decoration: none; color: inherit; }
.brand .parent {
  display: block;
  font-family: Georgia, "Songti SC", serif;
  font-size: 12px; letter-spacing: .04em;
  color: var(--text-muted); margin-bottom: 2px;
}
.brand:hover .parent { color: var(--text-primary); }
.page-head { position: relative; }
.lang {
  position: absolute; top: 0; right: 0;
  border: 1px solid var(--border); background: var(--surface-raised);
  color: var(--text-secondary); border-radius: var(--radius);
  padding: 4px 11px; font: inherit; font-size: 12px; cursor: pointer;
}
.lang:hover { color: var(--text-primary); }
