/* ==========================================================================
   ThruDark live dashboard
   --------------------------------------------------------------------------
   Dark by design, not by inversion: the surfaces, ink and the one data colour
   were each chosen against the near-black plane and checked for contrast and
   colour-vision separation. The single accent (#3E93C4) sits inside the
   dark-mode lightness band and clears 3:1 on the card surface; discount and
   return figures never rely on colour alone.

   Type scales with the viewport, so the same page reads correctly on a laptop
   and on a mounted 55" display without a separate "TV mode".
   ========================================================================== */

:root {
  /* Surfaces */
  --plane:      #0A0B0B;
  --surface-1:  #121414;
  --surface-2:  #171A1A;
  --surface-3:  #1D2121;

  /* Ink */
  --text-primary:   #FFFFFF;
  --text-secondary: #A9AFAF;
  --text-muted:     #79807F;

  /* Chart chrome */
  --grid:     #1E2222;
  --baseline: #2C3131;
  --border:   rgba(255, 255, 255, 0.09);
  --border-strong: rgba(255, 255, 255, 0.16);

  /* Data */
  --series-1:   #3E93C4;  /* current period */
  --series-cmp: #6E7474;  /* comparison period — deliberately achromatic */

  /* Status (reserved: never used as a series colour) */
  --good:     #0CA30C;
  --critical: #D03B3B;
  --warning:  #FAB219;

  --radius: 10px;
  --gap: clamp(10px, 0.9vw, 18px);
  --pad: clamp(14px, 1.15vw, 24px);

  --font: "Inter", "Helvetica Neue", Helvetica, Arial,
          system-ui, -apple-system, "Segoe UI", sans-serif;

  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background: var(--plane);
  color: var(--text-primary);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

.sr-only {
  position: absolute; width: 1px; height: 1px;
  padding: 0; margin: -1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* --------------------------------------------------------------------------
   Login / blocked gate
   -------------------------------------------------------------------------- */

.gate {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.gate-card {
  width: min(420px, 100%);
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 32px;
}

.gate-card-wide { width: min(580px, 100%); }

.gate-brand {
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.34em;
  color: var(--text-primary);
  margin-bottom: 28px;
}

.gate-card h1 {
  font-size: 19px;
  font-weight: 600;
  margin: 0 0 8px;
  letter-spacing: 0.01em;
}

.gate-card p {
  margin: 0 0 22px;
  font-size: 14px;
  line-height: 1.5;
  color: var(--text-secondary);
}

.gate-card code {
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 13px;
  color: var(--text-primary);
}

.gate-error {
  margin: 0 0 20px !important;
  padding: 11px 13px;
  font-size: 13px;
  line-height: 1.45;
  color: #F0A4A4;
  background: rgba(208, 59, 59, 0.1);
  border: 1px solid rgba(208, 59, 59, 0.4);
  border-radius: 7px;
}

.gate-warn {
  margin: 4px 0 0 !important;
  padding: 11px 13px;
  font-size: 13px;
  line-height: 1.5;
  color: #F3D08A;
  background: rgba(250, 178, 25, 0.09);
  border: 1px solid rgba(250, 178, 25, 0.32);
  border-radius: 7px;
}

.gate-foot {
  margin: 20px 0 0 !important;
  font-size: 12px;
  line-height: 1.5;
  color: var(--text-muted);
}

/* Google's sign-in button: their mark on a neutral surface, per their branding
   guidance, sized as the primary action on the card. */
.google-button {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  width: 100%;
  padding: 13px 16px;
  font: inherit;
  font-size: 14.5px;
  font-weight: 600;
  text-decoration: none;
  color: #1F1F1F;
  background: #FFFFFF;
  border-radius: 6px;
  transition: background 0.12s linear;
}

.google-button:hover { background: #E9EBEB; }
.google-button:active { background: #DCDEDE; }

.google-mark {
  display: inline-flex;
  align-items: center;
  flex: none;
}

.setup-steps {
  margin: 0 0 4px;
  padding-left: 20px;
  font-size: 13.5px;
  line-height: 1.65;
  color: var(--text-secondary);
}

.setup-steps li + li { margin-top: 10px; }
.setup-steps strong { color: var(--text-primary); font-weight: 600; }
.setup-steps code { color: var(--text-primary); }

.setup-uri {
  display: block;
  margin-top: 7px;
  padding: 9px 11px;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 12.5px;
  line-height: 1.4;
  word-break: break-all;
  color: var(--text-primary) !important;
  background: var(--plane);
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  user-select: all;
}

/* --------------------------------------------------------------------------
   Shell
   -------------------------------------------------------------------------- */

.app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  padding: var(--gap);
  gap: var(--gap);
}

.topbar {
  display: flex;
  align-items: center;
  gap: var(--gap);
  flex-wrap: wrap;
  padding: 4px 2px 0;
}

.brand {
  display: flex;
  align-items: baseline;
  gap: 14px;
  min-width: 0;
}

.brand-logo {
  height: clamp(20px, 1.9vw, 34px);
  width: auto;
  display: block;
  align-self: center;
}

.brand-wordmark {
  font-size: clamp(15px, 1.3vw, 22px);
  font-weight: 700;
  letter-spacing: 0.3em;
}

.brand-sub {
  font-size: clamp(10px, 0.72vw, 13px);
  text-transform: uppercase;
  letter-spacing: 0.2em;
  color: var(--text-muted);
  white-space: nowrap;
}

/* Channel and period switchers — both filters, one row above the charts.
   The channel group comes first and is pushed right, so the pair reads as a
   single control cluster with the period group ending at the status area. */
.filters {
  display: flex;
  gap: 2px;
  padding: 3px;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 8px;
}

#channels { margin-left: auto; }

.filter {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--text-secondary);
  font: inherit;
  font-size: clamp(11px, 0.76vw, 13px);
  font-weight: 500;
  padding: 7px clamp(9px, 0.8vw, 15px);
  border-radius: 6px;
  cursor: pointer;
  white-space: nowrap;
}

.filter:hover { background: var(--surface-3); color: var(--text-primary); }

.filter[aria-pressed="true"] {
  background: var(--text-primary);
  color: var(--plane);
  font-weight: 600;
}

.status {
  display: flex;
  align-items: center;
  gap: clamp(8px, 0.7vw, 14px);
  font-size: clamp(10px, 0.72vw, 12.5px);
  color: var(--text-muted);
  white-space: nowrap;
}

.live { display: inline-flex; align-items: center; gap: 7px; }

.live-dot {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--text-muted);
  flex: none;
}

.live[data-state="live"]    .live-dot { background: var(--good); animation: pulse 2.4s ease-in-out infinite; }
.live[data-state="stale"]   .live-dot { background: var(--warning); }
.live[data-state="error"]   .live-dot { background: var(--critical); }
.live[data-state="loading"] .live-dot { background: var(--series-1); animation: pulse 1s ease-in-out infinite; }

.live[data-state="live"]  .live-text { color: var(--text-secondary); }
.live[data-state="stale"] .live-text { color: var(--warning); }
.live[data-state="error"] .live-text { color: var(--critical); }

@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.35; } }

@media (prefers-reduced-motion: reduce) {
  .live-dot { animation: none !important; }
}

.stamp { font-variant-numeric: tabular-nums; }

.refresh-now {
  position: relative;
  width: 26px; height: 26px;
  padding: 0;
  border: 0;
  background: transparent;
  cursor: pointer;
  flex: none;
}

.refresh-ring {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 2px solid var(--border-strong);
  /* The filled arc is driven from JS as a conic gradient mask. */
  background:
    conic-gradient(var(--series-1) calc(var(--progress, 0) * 1turn), transparent 0);
  mask: radial-gradient(farthest-side, transparent calc(100% - 3px), #000 calc(100% - 2px));
  -webkit-mask: radial-gradient(farthest-side, transparent calc(100% - 3px), #000 calc(100% - 2px));
}

/* The ring fills over the refresh interval; the glyph says what it is at a
   glance, so a paused countdown doesn't read as an empty circle. */
.refresh-glyph {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-size: 12px;
  line-height: 1;
  color: var(--text-muted);
}

.refresh-now:hover .refresh-ring { border-color: var(--text-secondary); }
.refresh-now:hover .refresh-glyph { color: var(--text-primary); }

.banner {
  margin: 0;
  padding: 11px 15px;
  border-radius: 8px;
  font-size: 13px;
  line-height: 1.45;
  background: rgba(250, 178, 25, 0.09);
  border: 1px solid rgba(250, 178, 25, 0.32);
  color: #F3D08A;
}

.banner[data-kind="error"] {
  background: rgba(208, 59, 59, 0.1);
  border-color: rgba(208, 59, 59, 0.4);
  color: #F0A4A4;
}

/* --------------------------------------------------------------------------
   Layout
   -------------------------------------------------------------------------- */

.grid {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(12, minmax(0, 1fr));
  gap: var(--gap);
  align-content: start;
}

/* On anything desk-sized or larger the second row takes the slack, so a big
   wall display fills top to bottom instead of leaving a dead band under the
   charts. The KPI row stays sized to its content.

   The explicit `height` matters: a `1fr` track only caps its content when the
   grid container has a definite height. With `min-height` alone the track
   behaves like `auto`, the best-seller list pushes the card past the fold, and
   the whole page picks up a few dozen pixels of scroll. With a definite height
   the row is bounded and the list scrolls inside its own card instead. */
@media (min-width: 1181px) {
  .app {
    height: 100vh;
    overflow: auto;
  }

  .grid {
    align-content: stretch;
    grid-template-rows: auto minmax(280px, 1fr);
    min-height: 0;
  }

  .card-chart,
  .card-top {
    min-height: 0;
  }
}

.card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: var(--pad);
  min-width: 0;
}

.card-hero  { grid-column: span 4; display: flex; flex-direction: column; gap: var(--pad); }
.tiles      { grid-column: span 8; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: var(--gap); }

/* The chart and the best-seller list share a row. The chart flexes so the plot
   fills whatever height the row settles at; the list scrolls internally rather
   than pushing the page below the fold on a wall display. */
.card-chart {
  grid-column: span 7;
  display: flex;
  flex-direction: column;
  min-height: 320px;
}

.card-top {
  grid-column: span 5;
  display: flex;
  flex-direction: column;
  min-height: 320px;
}

/* Six tiles, so the column count is always a factor of six — otherwise the last
   row ends in a conspicuous gap. */
@media (max-width: 1180px) {
  .card-hero, .tiles, .card-chart, .card-top { grid-column: span 12; }
  .tiles { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  #channels { margin-left: auto; order: 2; }
  .periods { margin-left: 0; order: 3; width: 100%; overflow-x: auto; }
  .status { order: 2; margin-left: 0; }
}

@media (max-width: 820px) {
  .tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .brand-sub { display: none; }
}

/* --------------------------------------------------------------------------
   Hero figure — exactly one per view
   -------------------------------------------------------------------------- */

.hero { display: flex; flex-direction: column; gap: 6px; }

.hero-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
}

.hero-value {
  font-size: clamp(40px, 4.4vw, 92px);
  font-weight: 600;
  line-height: 1.02;
  letter-spacing: -0.022em;
  /* Proportional figures: tabular digits look loose at display sizes. */
}

.hero-delta {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: clamp(12px, 0.86vw, 15px);
  color: var(--text-secondary);
}

/* The breakdown grows into whatever height the row gives the hero card, so a
   tall row reads as four generous panels rather than a void with a strip at
   the bottom. */
.hero-breakdown {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 1px;
  flex: 1;
  min-height: 0;
  margin-top: auto;
  background: var(--border);
  border-radius: 8px;
  overflow: hidden;
}

.breakdown-item {
  background: var(--surface-2);
  padding: clamp(9px, 0.8vw, 14px) clamp(11px, 0.9vw, 16px);
  display: flex;
  flex-direction: column;
  justify-content: center;
  min-height: clamp(52px, 4vw, 82px);
}

.breakdown-label {
  font-size: clamp(9.5px, 0.66vw, 11.5px);
  text-transform: uppercase;
  letter-spacing: 0.11em;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.breakdown-value {
  font-size: clamp(15px, 1.2vw, 22px);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

/* The comparison period is stated once on the hero delta, so each supporting
   figure only needs the direction and the size of its change. */
.breakdown-delta {
  margin-top: 3px;
  font-size: clamp(10px, 0.68vw, 12px);
  font-variant-numeric: tabular-nums;
}

/* --------------------------------------------------------------------------
   Stat tiles
   -------------------------------------------------------------------------- */

.tile {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: var(--pad);
  display: flex;
  flex-direction: column;
  gap: 5px;
  min-width: 0;
}

.tile-label {
  font-size: clamp(10px, 0.7vw, 12.5px);
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.13em;
  color: var(--text-muted);
  margin: 0;
}

.tile-note {
  font-size: clamp(9.5px, 0.65vw, 11.5px);
  color: var(--text-muted);
  line-height: 1.4;
}

.tile-value {
  font-size: clamp(22px, 2vw, 40px);
  font-weight: 600;
  line-height: 1.08;
  letter-spacing: -0.014em;
}

.tile-delta {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: clamp(10.5px, 0.72vw, 13px);
  color: var(--text-secondary);
  margin-top: auto;
  padding-top: 6px;
}

/* Direction is carried by the arrow glyph as well as the colour, so the
   delta still reads under any colour-vision profile or in greyscale print. */
.delta-arrow { font-size: 0.95em; line-height: 1; }
.delta-up   { color: var(--good); }
.delta-down { color: var(--critical); }
.delta-flat { color: var(--text-muted); }
.delta-none { color: var(--text-muted); }
.delta-period { color: var(--text-muted); }

/* --------------------------------------------------------------------------
   Cards / charts
   -------------------------------------------------------------------------- */

.card-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--gap);
  margin-bottom: clamp(10px, 1vw, 18px);
}

.card-title {
  margin: 0;
  font-size: clamp(13px, 0.95vw, 17px);
  font-weight: 600;
  letter-spacing: 0.005em;
}

.card-sub {
  margin: 4px 0 0;
  font-size: clamp(10.5px, 0.7vw, 12.5px);
  color: var(--text-muted);
}

.legend {
  display: flex;
  gap: clamp(10px, 0.9vw, 18px);
  font-size: clamp(10.5px, 0.7vw, 12.5px);
  color: var(--text-secondary);
  flex-wrap: wrap;
}

.legend-item { display: inline-flex; align-items: center; gap: 7px; }

.legend-key {
  width: 14px; height: 3px;
  border-radius: 2px;
  flex: none;
}

.chart-wrap {
  position: relative;
  width: 100%;
  flex: 1;
  min-height: 220px;
}

.chart-wrap svg { display: block; width: 100%; height: 100%; overflow: visible; }

.tick-text {
  font-size: 11px;
  fill: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.gridline { stroke: var(--grid); stroke-width: 1; }
.axisline { stroke: var(--baseline); stroke-width: 1; }

.crosshair { stroke: var(--border-strong); stroke-width: 1; }

.tooltip {
  position: absolute;
  pointer-events: none;
  min-width: 132px;
  padding: 9px 11px;
  background: var(--surface-3);
  border: 1px solid var(--border-strong);
  border-radius: 7px;
  font-size: 12px;
  line-height: 1.5;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.55);
  opacity: 0;
  transition: opacity 0.1s linear;
  z-index: 5;
}

.tooltip[data-visible="true"] { opacity: 1; }
.tooltip-title { color: var(--text-muted); margin-bottom: 5px; font-size: 11px; }
.tooltip-row {
  display: flex; align-items: center; gap: 8px;
  justify-content: space-between;
  font-variant-numeric: tabular-nums;
}
.tooltip-row .k { display: inline-flex; align-items: center; gap: 6px; color: var(--text-secondary); }
.tooltip-row .v { font-weight: 600; }

/* --------------------------------------------------------------------------
   Best sellers
   -------------------------------------------------------------------------- */

.top-wrap {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--surface-3) transparent;
}

/* Rows share any spare height rather than bunching at the top of a tall card,
   and never shrink below their own content, so the list still scrolls when the
   card is short. */
.bar-row {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 5px;
  flex: 1 0 auto;
  /* Tuned so ten rows clear a 1080p wall display without internal scroll. */
  padding: clamp(4px, 0.4vw, 9px) 0;
  cursor: default;
}

.bar-head {
  display: flex;
  align-items: baseline;
  gap: 12px;
}

.bar-name {
  font-size: clamp(11px, 0.78vw, 14px);
  font-weight: 500;
  color: var(--text-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
  min-width: 0;
}

/* Units and orders sit between the name and the value: secondary information,
   but on the same line, so ten rows fit a 1080p screen without scrolling. */
.bar-head .bar-meta { flex: none; margin-left: auto; }

.bar-rank {
  display: inline-block;
  min-width: 1.5em;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  margin-right: 8px;
}

.bar-value {
  font-size: clamp(11px, 0.78vw, 14px);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  flex: none;
  min-width: 4.5em;
  text-align: right;
}

.bar-track { height: 10px; position: relative; }

.bar-fill {
  height: 10px;
  background: var(--series-1);
  /* 4px rounded data-end, square at the baseline */
  border-radius: 0 4px 4px 0;
  min-width: 2px;
  transition: width 0.35s cubic-bezier(0.22, 0.61, 0.36, 1);
}

.bar-meta {
  font-size: clamp(10px, 0.66vw, 11.5px);
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.bar-row + .bar-row { border-top: 1px solid rgba(255, 255, 255, 0.04); }

@media (prefers-reduced-motion: reduce) {
  .bar-fill { transition: none; }
}

/* Table view — the accessible equivalent of every chart on the page */
.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: clamp(11px, 0.76vw, 13.5px);
}

.data-table th,
.data-table td {
  text-align: right;
  padding: clamp(6px, 0.5vw, 10px) 8px;
  border-bottom: 1px solid var(--border);
  font-variant-numeric: tabular-nums;
}

.data-table th {
  font-size: clamp(9.5px, 0.64vw, 11px);
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--text-muted);
}

.data-table th:first-child,
.data-table td:first-child { text-align: left; font-variant-numeric: normal; }
.data-table tbody tr:last-child td { border-bottom: 0; }

.ghost-btn {
  appearance: none;
  background: transparent;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  color: var(--text-secondary);
  font: inherit;
  font-size: clamp(10.5px, 0.7vw, 12.5px);
  padding: 6px 11px;
  cursor: pointer;
  white-space: nowrap;
}

.ghost-btn:hover { color: var(--text-primary); border-color: var(--text-muted); }
.ghost-btn[aria-pressed="true"] { background: var(--surface-3); color: var(--text-primary); }

.empty {
  padding: 30px 4px;
  color: var(--text-muted);
  font-size: 13px;
}

/* --------------------------------------------------------------------------
   Footer
   -------------------------------------------------------------------------- */

.footer {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  padding: 2px 2px 4px;
  font-size: clamp(9.5px, 0.65vw, 11.5px);
  color: var(--text-muted);
}

.footer-sep { opacity: 0.5; }
.footer-spacer { flex: 1; min-width: 12px; }
.footer-user { color: var(--text-secondary); }
.footer-link { color: var(--text-secondary); text-decoration: none; }
.footer-link:hover { color: var(--text-primary); text-decoration: underline; }

:focus-visible { outline: 2px solid var(--series-1); outline-offset: 2px; }

/* Fed by the accessibility/print channel rather than decoration. */
@media print {
  body { background: #fff; color: #000; }
  .topbar, .footer, .refresh-now { display: none; }
}
