/* Commodities page — table + mobile card layout. Style tokens mirror
   /sedar-filings (light grey body, white cards with subtle border + shadow,
   Roboto, gray-900 headings on gray-500 subtitles). */

body {
  background-color: #f7f7f7;
}

/* The `hidden` HTML attribute loses to any explicit `display:` rule on
   page elements (e.g. .cm-loading { display: flex }). Force it across
   both the page body and the detail-modal subtree (sl-dialog is mounted
   as a body-level sibling, not inside .cm-page). */
.cm-page [hidden],
.cm-detail-dialog [hidden] { display: none !important; }

.cm-page {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px 24px 32px;
  font-family: 'Roboto', sans-serif;
  color: #1f2937;
  box-sizing: border-box;
}

.cm-header {
  margin-bottom: 16px;
}

.cm-title {
  margin: 0 0 4px;
  font-size: 24px;
  font-weight: 700;
  color: #111827;
}

.cm-subtitle {
  margin: 0;
  color: #6b7280;
  font-size: 13px;
}

.cm-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 16px;
  padding: 4px 0 12px;
  font-size: 12px;
  color: #6b7280;
}

.cm-currency { font-weight: 500; color: #374151; }
.cm-last-updated {
  text-align: right;
  color: #6b7280;
  font-variant-numeric: tabular-nums;
}
.cm-last-updated:empty { display: none; }

/* Bottom-of-card data-source disclaimer. Lives outside the card so it
   reads as page-footer chrome, not table content. */
.cm-disclaimer {
  margin: 12px 0 0;
  font-size: 11px;
  color: #9ca3af;
  text-align: center;
}

/* Shared card styling — same tokens as .sf-card on /sedar-filings.
   `overflow` left as `visible` so the sticky table header (which lives
   inside this wrapper) can stick to the viewport instead of being clipped
   at the card top — `overflow: hidden` on an ancestor establishes a
   sticky container, which would defeat the freeze-on-scroll behavior. */
.cm-table-card {
  background: #ffffff;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.03);
  position: relative;
  min-height: 260px;
}

/* Loading state */
.cm-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 80px 0;
}
.cm-spinner {
  width: 32px;
  height: 32px;
  border: 3px solid #e5e7eb;
  border-top-color: #42858d;
  border-radius: 50%;
  animation: cm-spin 0.9s linear infinite;
}
@keyframes cm-spin { to { transform: rotate(360deg); } }

/* Brief background flash applied to the price cell when its value changes
   between polls. Cell-scoped so adjacent change/pct columns don't strobe. */
@keyframes cm-flash-up {
  0%   { background-color: rgba(22, 163, 74, 0.20); }
  100% { background-color: transparent; }
}
@keyframes cm-flash-down {
  0%   { background-color: rgba(220, 38, 38, 0.20); }
  100% { background-color: transparent; }
}
.cm-flash-up   { animation: cm-flash-up   1.2s ease-out; }
.cm-flash-down { animation: cm-flash-down 1.2s ease-out; }

.cm-empty {
  padding: 60px 24px;
  text-align: center;
  color: #6b7280;
  font-size: 14px;
}

/* Table */
.cm-table {
  width: 100%;
  /* `separate` so each cell owns its own borders. `collapse` would render
     the thead's bottom border as part of the collapsed table edge, which
     scrolls away with the body when the thead becomes sticky — leaving the
     pinned header with no visible divider beneath it. */
  border-collapse: separate;
  border-spacing: 0;
  font-size: 13px;
}

/* Sticky table header — pins below the fixed nav (65px desktop, 56px mobile,
   per style.css padding-top on body). Background must be opaque so scrolled
   row content doesn't bleed through. The bottom border travels with the
   thead now that the table uses border-collapse: separate. */
.cm-table thead th {
  position: sticky;
  top: 65px;
  background: #ffffff;
  text-align: left;
  font-weight: 600;
  font-size: 11px;
  color: #6b7280;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  padding: 14px 16px;
  z-index: 5;
  border-bottom: 1px solid #e5e7eb;
}
/* Rounded corners on the visible head — restores the wrapper's
   border-radius look without needing overflow:hidden on the wrapper
   (which would clip the sticky header). */
.cm-table thead th:first-child { border-top-left-radius: 8px; }
.cm-table thead th:last-child  { border-top-right-radius: 8px; }
/* Rounded corners on the last row so the wrapper bottom still looks clean. */
.cm-table tbody tr:last-child td:first-child { border-bottom-left-radius: 8px; }
.cm-table tbody tr:last-child td:last-child  { border-bottom-right-radius: 8px; }

.cm-table th.cm-col-num,
.cm-table th.cm-col-when { text-align: right; }

.cm-section-row td {
  padding: 14px 16px 6px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.08em;
  color: #6b7280;
  background: #f9fafb;
  border-top: 1px solid #e5e7eb;
  border-bottom: 1px solid #e5e7eb;
  text-transform: uppercase;
}

.cm-row {
  cursor: pointer;
  transition: background-color 0.12s;
}
.cm-row:hover { background-color: #f9fafb; }
.cm-row td {
  padding: 16px;
  border-bottom: 1px solid #f3f4f6;
  vertical-align: top;
}
.cm-row:last-child td { border-bottom: none; }

.cm-row td.cm-num,
.cm-row td.cm-when {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.cm-name-cell { font-weight: 600; color: #111827; }
.cm-name-cell .cm-unit {
  display: block;
  font-size: 11px;
  font-weight: 400;
  color: #9ca3af;
  margin-top: 2px;
}

/* Bold the price + every change value per the mockup. */
.cm-price,
.cm-row td.cm-num { font-weight: 700; }

.cm-pos { color: #16a34a; }
.cm-neg { color: #dc2626; }
.cm-zero { color: #6b7280; }

.cm-when {
  color: #6b7280;
  font-size: 12px;
  font-weight: 400;
}
.cm-when small {
  display: block;
  font-size: 11px;
  color: #9ca3af;
  margin-top: 2px;
}

/* Mobile cards — compact layout:
   Row 1: name | price
   Row 2: USD/<units>  |  +day_abs  +day_pct
   ─────────────────────────────────────
   Row 3 (2×2 grid): 1W -X% | 1M -X% / YTD +X% | 1Y +X%
   Only the price is bold; change values rely on color for emphasis. */
.cm-cards { padding: 4px 0; }
.cm-cards-section-header {
  padding: 14px 16px 6px;
  font-size: 11px;
  letter-spacing: 0.08em;
  font-weight: 600;
  color: #6b7280;
  text-transform: uppercase;
  background: #f9fafb;
  border-top: 1px solid #e5e7eb;
  border-bottom: 1px solid #e5e7eb;
}
.cm-cards-section-header:first-child { border-top: none; }

.cm-card {
  display: block;
  padding: 14px 16px;
  text-decoration: none;
  color: inherit;
  background: #ffffff;
  border-bottom: 1px solid #f3f4f6;
  transition: background-color 0.12s, box-shadow 0.12s;
}
.cm-card:last-child { border-bottom: none; }
.cm-card:hover { background-color: #f9fafb; }

.cm-card-head {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: baseline;
  column-gap: 12px;
  row-gap: 2px;
}
.cm-card-name {
  font-weight: 600;
  font-size: 15px;
  color: #111827;
}
.cm-card-unit {
  font-size: 11px;
  color: #9ca3af;
  font-weight: 400;
}
.cm-card-price {
  font-weight: 700;
  font-size: 17px;
  color: #111827;
  text-align: right;
  font-variant-numeric: tabular-nums;
}
.cm-card-day {
  font-size: 12px;
  text-align: right;
  font-variant-numeric: tabular-nums;
}
.cm-card-day > span + span { margin-left: 8px; }

.cm-card-metrics {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px 16px;
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid #f3f4f6;
  font-size: 12px;
}
.cm-card-metric {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
}
.cm-card-metric-label {
  color: #6b7280;
  font-weight: 500;
}
.cm-card-metric-value {
  font-variant-numeric: tabular-nums;
}

/* ─────────────────────────────────────────────────────────────────────────
   Per-commodity chart modal (sl-dialog). Mounted inline in index.html and
   filled / shown by commodities.js. Deep-link via /commodities?commodity=X.
   ───────────────────────────────────────────────────────────────────────── */

/* Hide the dialog's light-DOM slot content until the <sl-dialog> custom
   element is registered. Without this, the modal's body (header + range
   tabs + chart area) renders inline at the bottom of the page during the
   brief window between HTML parse and Shoelace autoloader running — a
   jarring FOUC flash. `:defined` flips on once the element upgrades. */
sl-dialog:not(:defined) {
  display: none;
}

.cm-detail-dialog {
  --width: min(880px, calc(100vw - 32px));
}
.cm-detail-dialog::part(panel) {
  font-family: 'Roboto', sans-serif;
}
.cm-detail-dialog::part(body) {
  padding: 20px 24px 20px;
  position: relative;
}

/* Manual close button — sl-dialog's built-in one is hidden by `no-header`,
   so we provide our own to keep the visible keyboard / pointer affordance. */
.cmd-close {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 32px;
  height: 32px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  background: transparent;
  border: none;
  border-radius: 4px;
  color: #6b7280;
  cursor: pointer;
  transition: background-color 0.12s, color 0.12s;
}
.cmd-close:hover { background-color: #f3f4f6; color: #111827; }
.cmd-close:focus-visible {
  outline: 2px solid #42858d;
  outline-offset: 2px;
}

.cmd-modal-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 24px;
  flex-wrap: wrap;
  /* Right padding reserves space for the absolute close button (32px wide
     at top:12px right:12px → ~44px total reserve so the price value
     doesn't tuck behind it). */
  padding: 0 44px 12px 0;
  border-bottom: 1px solid #e5e7eb;
  margin-bottom: 16px;
}
.cmd-modal-header-main { min-width: 0; }
.cmd-title {
  margin: 0;
  font-size: 24px;
  font-weight: 700;
  color: #111827;
}
.cmd-units {
  font-size: 12px;
  color: #9ca3af;
  margin-top: 4px;
  letter-spacing: 0.02em;
}
.cmd-modal-header-quote { text-align: right; }
.cmd-price {
  font-size: 28px;
  font-weight: 700;
  color: #111827;
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
}
.cmd-change {
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  margin-top: 2px;
}
.cmd-when {
  font-size: 11px;
  color: #9ca3af;
  margin-top: 4px;
}

.cmd-range-row {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}
.cmd-range-btn {
  background: #ffffff;
  border: 1px solid #e5e7eb;
  border-radius: 4px;
  padding: 6px 12px;
  font-size: 12px;
  font-weight: 500;
  color: #374151;
  cursor: pointer;
  transition: all 0.12s;
  font-family: inherit;
}
.cmd-range-btn:hover { border-color: #9ca3af; color: #111827; }
.cmd-range-btn.is-active {
  background: #42858d;
  border-color: #42858d;
  color: #ffffff;
}

.cmd-chart-wrap {
  position: relative;
  width: 100%;
  height: 380px;
}
/* Loader / empty-state / canvas all share the wrap — overlap them so only
   one is visible at a time. Without this, they stack in flow and the wrap
   overflows its fixed height (visible as a stale chart bleeding below the
   loader, and a vertical scrollbar inside the sl-dialog). */
.cmd-chart-wrap > .cm-loading,
.cmd-chart-wrap > .cm-empty,
.cmd-chart-wrap > #cmd-chart {
  position: absolute;
  inset: 0;
}
.cmd-chart-wrap > .cm-loading { padding: 0; }
#cmd-chart {
  width: 100% !important;
  height: 100% !important;
}

@media (max-width: 768px) {
  .cm-detail-dialog::part(body) { padding: 16px; }
  .cmd-modal-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }
  .cmd-modal-header-quote { text-align: left; }
  .cmd-title { font-size: 20px; }
  .cmd-price { font-size: 22px; }
  .cmd-chart-wrap { height: 280px; }
}

@media (max-width: 768px) {
  .cm-page { padding: 16px 12px 48px; }
  .cm-title { font-size: 20px; }
  .cm-toolbar { flex-direction: column; gap: 4px; align-items: flex-start; }
  .cm-last-updated { text-align: left; }

  /* On mobile, drop the outer card chrome so each commodity card can float
     on the gray page background as its own discrete unit. */
  .cm-table-card {
    background: transparent;
    border: none;
    box-shadow: none;
    border-radius: 0;
    overflow: visible;
    min-height: 0;
  }

  .cm-cards { padding: 0; }

  /* Section labels become bare list dividers — no card chrome of their own. */
  .cm-cards-section-header {
    background: transparent;
    border: none;
    padding: 18px 4px 8px;
  }
  .cm-cards-section-header:first-child { padding-top: 4px; }

  /* Each card is its own elevated tile. */
  .cm-card {
    border: 1px solid #e5e7eb;
    border-bottom: 1px solid #e5e7eb;   /* override the desktop list-divider rule */
    border-radius: 8px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.03);
    margin-bottom: 10px;
    padding: 14px 16px;
  }
  .cm-card:last-child { margin-bottom: 0; border-bottom: 1px solid #e5e7eb; }
  .cm-card:hover { box-shadow: 0 2px 6px rgba(0, 0, 0, 0.06); background-color: #ffffff; }

  /* Remove the inner head→metrics divider on mobile — spacing alone keeps the
     two zones distinct without imitating the new card boundary. */
  .cm-card-metrics {
    border-top: none;
    margin-top: 4px;
    padding-top: 8px;
  }
}
