/* Self-contained design system (no framework). Fintech dark theme.
   All brand/layout lives here; templates stay markup-only (req 5). */

/* ---------- tokens ---------- */
:root {
  --bg: #0f1420;
  --surface: #161c2c;
  --surface-2: #1b2336;
  --border: #232c40;
  --text: #e6ebf5;
  --muted: #94a2bd;
  --brand: #2d7ff9;
  --brand-2: #7b5cff;
  --green: #4ade80;
  --amber: #fbbf24;
  --red: #f87171;
  --radius: 14px;
  --radius-sm: 10px;
  --shadow: 0 8px 30px -16px rgba(0, 0, 0, 0.8);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "Cascadia Code", "SF Mono", Menlo, monospace;
}

/* ---------- reset ---------- */
*,
*::before,
*::after {
  box-sizing: border-box;
}
* {
  margin: 0;
}
html {
  -webkit-text-size-adjust: 100%;
}
body {
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.5;
  color: var(--text);
  background:
    radial-gradient(1200px 600px at 80% -10%, rgba(123, 92, 255, 0.18), transparent 60%),
    radial-gradient(1000px 500px at -10% 0%, rgba(45, 127, 249, 0.18), transparent 55%),
    var(--bg);
  background-attachment: fixed;
  min-height: 100vh;
}
a {
  color: var(--brand);
  text-decoration: none;
}
a:hover {
  text-decoration: underline;
}
img {
  max-width: 100%;
  height: auto;
  display: block;
}
button,
input,
select {
  font: inherit;
  color: inherit;
}

/* ---------- layout ---------- */
.container {
  width: 100%;
  max-width: 1040px;
  margin: 0 auto;
  padding: 1.5rem 1.25rem 2rem;
}

/* ---------- footer ---------- */
.site-footer {
  width: 100%;
  max-width: 1040px;
  margin: 0 auto;
  padding: 1.5rem 1.25rem 2.5rem;
  border-top: 1px solid var(--border);
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}
.footer-disclaimer {
  color: var(--muted);
  font-size: 0.78rem;
  line-height: 1.5;
  max-width: 600px;
  margin: 0 auto;
}
.footer-copy {
  color: var(--muted);
  font-size: 0.72rem;
  opacity: 0.6;
}

/* ---------- hero ---------- */
.hero {
  margin: 0.5rem 0 1.5rem;
  padding: 2rem 1.75rem;
  border-radius: 18px;
  background: linear-gradient(135deg, rgba(45, 127, 249, 0.22), rgba(123, 92, 255, 0.12));
  border: 1px solid rgba(123, 92, 255, 0.25);
  box-shadow: 0 10px 40px -12px rgba(45, 127, 249, 0.35);
}
.hero h1 {
  font-size: clamp(1.8rem, 4vw, 2.6rem);
  font-weight: 800;
  letter-spacing: -0.02em;
  line-height: 1.1;
  margin-bottom: 0.35rem;
  background: linear-gradient(90deg, var(--brand), var(--brand-2));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.hero p {
  color: var(--muted);
}

/* ---------- search toolbar ---------- */
.toolbar {
  margin-bottom: 1rem;
}
.toolbar input[type="search"] {
  width: 100%;
  padding: 0.7rem 1rem;
  border-radius: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  outline: none;
  transition: border-color 0.12s ease, box-shadow 0.12s ease;
}
.toolbar input[type="search"]::placeholder {
  color: var(--muted);
}
.toolbar input[type="search"]:focus {
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(45, 127, 249, 0.2);
}
/* chips row (Sort + Sector buttons): mobile only - desktop uses table headers */
.chips-row {
  display: none;
  align-items: center;
  gap: 0.5rem;
  margin-top: 0.6rem;
}
.sort-pop {
  position: relative;
}
.sort-pop > summary {
  list-style: none;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
}
.sort-pop > summary::-webkit-details-marker {
  display: none;
}
/* direction arrow on the Sort button (border triangle, no glyphs) */
.sort-pop > summary::after {
  content: "";
  width: 0;
  height: 0;
  border-left: 4px solid transparent;
  border-right: 4px solid transparent;
}
.sort-pop > summary[data-dir="asc"]::after {
  border-bottom: 5px solid currentColor;
}
.sort-pop > summary[data-dir="desc"]::after {
  border-top: 5px solid currentColor;
}

/* backdrop behind mobile bottom-sheets (shown via JS removing [hidden]) */
.sheet-backdrop {
  display: none;
}
@keyframes sheet-up {
  from {
    transform: translateY(100%);
  }
  to {
    transform: translateY(0);
  }
}
.chip {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  white-space: nowrap;
  padding: 0.45rem 0.9rem;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
}
.chip.active {
  color: #fff;
  background: linear-gradient(135deg, var(--brand), var(--brand-2));
  border-color: transparent;
}
/* direction arrow on the active chip (border triangle, no glyphs) */
.chip[aria-sort="ascending"]::after,
.chip[aria-sort="descending"]::after {
  content: "";
  width: 0;
  height: 0;
  border-left: 4px solid transparent;
  border-right: 4px solid transparent;
}
.chip[aria-sort="ascending"]::after {
  border-bottom: 5px solid currentColor;
}
.chip[aria-sort="descending"]::after {
  border-top: 5px solid currentColor;
}

/* ---------- table ---------- */
table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
th,
td {
  text-align: left;
  padding: 0.7rem 1rem;
}
tbody tr + tr {
  border-top: 1px solid var(--border);
}

/* column titles: clickable to sort */
.cols th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--surface-2);
  text-transform: uppercase;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  color: var(--muted);
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
}
.cols th:hover {
  color: var(--text);
}
.th-label {
  vertical-align: middle;
}
/* sort arrow drawn with borders (no glyphs), only on the active column */
.cols th .th-label::after {
  content: "";
  display: inline-block;
  width: 0;
  height: 0;
  margin-left: 0.4rem;
  vertical-align: middle;
  border-left: 4px solid transparent;
  border-right: 4px solid transparent;
  opacity: 0;
}
.cols th[aria-sort="ascending"] .th-label::after {
  border-bottom: 5px solid currentColor;
  opacity: 0.9;
}
.cols th[aria-sort="descending"] .th-label::after {
  border-top: 5px solid currentColor;
  opacity: 0.9;
}

/* data rows */
.stock-row {
  cursor: pointer;
  transition: background 0.12s ease;
}
.stock-row:hover {
  background: rgba(45, 127, 249, 0.08);
}
.c-name {
  font-weight: 600;
}
.c-ticker {
  font-family: var(--mono);
  color: var(--muted);
}

/* non-sortable column header */
.cols th.th-static {
  cursor: default;
}
.cols th.th-static:hover {
  color: var(--muted);
}

/* ---------- per-column bar charts (one series each) ---------- */
.chart {
  display: inline-flex;
  align-items: flex-end;
  gap: 2px;
  height: 34px;
  min-width: 70px;
}
.bar {
  flex: 1;
  min-height: 1px;
  border-radius: 1px 1px 0 0;
}
.bar-0 {
  background: var(--brand);
}
.bar-1 {
  background: var(--brand-2);
}
.bar-2 {
  background: var(--green);
}
.bar-missing {
  opacity: 0.2;
}
.bar-negative {
  background: var(--red);
}

/* ---------- stock-page histograms ---------- */
.charts {
  margin-top: 1.75rem;
}
.charts h2 {
  font-size: 1rem;
  font-weight: 700;
  margin: 1.5rem 0 0.6rem;
}

/* chart color tokens (overridden per metric via JS) */
:root {
  --ch-rev:   #2d7ff9;
  --ch-earn:  #7b5cff;
  --ch-div:   #4ade80;
  --ch-trend: #fbbf24;
  --ch-neg:   #f87171;
}

/* outer container */
.chart-lg {
  position: relative;
  height: 176px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
}

/* inner chart wrapper */
.ch {
  position: relative;
  height: 100%;
}

/* bars flex row */
.ch-bars {
  display: flex;
  align-items: stretch;
  gap: 2px;
  height: 100%;
  padding: 0 8px;
}

/* one column per year: 18px yoy / 1fr plot / 14px year */
.ch-slot {
  display: grid;
  grid-template-rows: 18px 1fr 14px;
  flex: 1 1 0;
  min-width: 0;
  height: 100%;
  cursor: default;
}

.ch-yoy {
  align-self: center;
  overflow: hidden;
  color: var(--muted);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  line-height: 16px;
  text-align: center;
  white-space: nowrap;
}
.ch-yp { color: #4ade80; }
.ch-yn { color: #f87171; }

/* plot area: simple positioned container; JS sets top/height on all children in px */
.ch-plot {
  position: relative;
  min-height: 0;
}

/* zero line: top = --zy (px from top of plot, set by JS) */
.ch-zl {
  position: absolute;
  left: 0;
  right: 0;
  top: var(--zy, 100%);
  height: 1px;
  background: #52627c;
  opacity: 0.6;
}

/* annual bar: top/height/border-radius set inline by JS in px */
.ch-ab {
  position: absolute;
  left: 0;
  right: 0;
  background: linear-gradient(180deg, var(--ch-slot-color, var(--ch-rev)), color-mix(in srgb, var(--ch-slot-color, var(--ch-rev)) 70%, #000));
  box-shadow: 0 0 10px color-mix(in srgb, var(--ch-slot-color, var(--ch-rev)) 30%, transparent);
  transition: filter 100ms ease;
}

/* negative bar: red gradient, grows downward from zero line */
.ch-ab.ch-neg {
  background: linear-gradient(180deg, var(--ch-neg), #b83b55);
  box-shadow: 0 0 10px color-mix(in srgb, var(--ch-neg) 30%, transparent);
}

/* annual-quarter-bar: transparent frame around quarterly sub-bars */
.ch-aqb {
  border: 1px solid color-mix(in srgb, var(--ch-slot-color, var(--ch-rev)) 60%, #fff);
  background: color-mix(in srgb, var(--ch-slot-color, var(--ch-rev)) 18%, transparent);
  box-shadow: none;
}

/* negative frame uses ch-neg color */
.ch-aqb.ch-neg {
  border-color: color-mix(in srgb, var(--ch-neg) 60%, #fff);
  background: color-mix(in srgb, var(--ch-neg) 18%, transparent);
}

/* quarter grid: positioned absolutely with top+height set inline by JS */
.ch-qg {
  position: absolute;
  left: 1px;
  right: 1px;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 1px;
  overflow: hidden;
  z-index: 1;
}

/* positive quarter grid: segments grow from the bottom upward */
.ch-qg-pos {
  align-items: end;
}

/* negative quarter grid: segments grow from the top downward */
.ch-qg-neg {
  align-items: start;
}

/* quarter segment: height set inline by JS in px */
.ch-qs {
  display: block;
  min-height: 2px;
}

.ch-qg-pos .ch-qs { align-self: end; }
.ch-qg-neg .ch-qs { align-self: start; }

.ch-qs:nth-child(1) { background: color-mix(in srgb, var(--ch-slot-color, var(--ch-rev)) 90%, #fff); }
.ch-qs:nth-child(2) { background: color-mix(in srgb, var(--ch-slot-color, var(--ch-rev)) 75%, #fff); }
.ch-qs:nth-child(3) { background: color-mix(in srgb, var(--ch-slot-color, var(--ch-rev)) 85%, #000); }
.ch-qs:nth-child(4) { background: color-mix(in srgb, var(--ch-slot-color, var(--ch-rev)) 60%, #fff); }

.ch-qs.ch-qneg {
  background: repeating-linear-gradient(135deg, #f87171 0 3px, #b83b55 3px 6px);
}

.ch-qph { visibility: hidden; }

/* missing-data marker: top set inline by JS to land at zero line */
.ch-mm {
  position: absolute;
  left: 20%;
  right: 20%;
  height: 2px;
  background: #46536a;
}

/* year label */
.ch-yr {
  align-self: end;
  color: var(--muted);
  font-size: 12px;
  line-height: 16px;
  text-align: center;
}

/* slot hover */
.ch-slot:hover .ch-ab { filter: brightness(1.3); }

/* exponential trend SVG overlay: sits over the plot area (18px from top, 14px from bottom) */
.ch-trend {
  position: absolute;
  z-index: 2;
  inset: 18px 0 14px;
  width: 100%;
  height: calc(100% - 32px);
  overflow: visible;
  pointer-events: none;
}

.ch-tl {
  fill: none;
  stroke: var(--ch-trend);
  stroke-width: 2.5;
  stroke-linecap: round;
  stroke-linejoin: round;
  vector-effect: non-scaling-stroke;
  filter: drop-shadow(0 0 3px color-mix(in srgb, var(--ch-trend) 50%, transparent));
}

/* CAGR badge */
.ch-badge {
  position: absolute;
  z-index: 3;
  top: 22px;
  right: 10px;
  color: var(--ch-trend);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  font-weight: 800;
  line-height: 1;
  background: rgba(0, 0, 0, 0.5);
  border-radius: 4px;
  padding: 2px 5px;
  pointer-events: none;
}

/* ---------- score badges ---------- */
.badge {
  display: inline-block;
  min-width: 2.5rem;
  text-align: center;
  padding: 0.15rem 0.55rem;
  border-radius: 999px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.badge-high {
  background: rgba(34, 197, 94, 0.18);
  color: var(--green);
  border: 1px solid rgba(34, 197, 94, 0.35);
}
.badge-mid {
  background: rgba(245, 158, 11, 0.16);
  color: var(--amber);
  border: 1px solid rgba(245, 158, 11, 0.35);
}
.badge-low {
  background: rgba(239, 68, 68, 0.16);
  color: var(--red);
  border: 1px solid rgba(239, 68, 68, 0.35);
}

/* ---------- sector filter: popup shared by a header funnel + a mobile chip ---------- */
.sector-filter {
  position: relative;
}
.sector-filter > summary {
  list-style: none;
  cursor: pointer;
}
.sector-filter > summary::-webkit-details-marker {
  display: none;
}

/* header trigger: compact funnel icon */
.sector-filter.icon {
  display: inline-block;
  vertical-align: middle;
  margin-right: 0.4rem;
}
.sector-filter.icon > summary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.6rem;
  height: 1.6rem;
  border-radius: 8px;
  color: var(--muted);
  border: 1px solid transparent;
}
.sector-filter.icon > summary:hover {
  color: var(--text);
  background: rgba(45, 127, 249, 0.12);
}
.sector-filter.icon.active > summary {
  color: #fff;
  background: linear-gradient(135deg, var(--brand), var(--brand-2));
}

/* mobile trigger: a chip (summary uses the .chip styling) */
.chip-sector.active > summary {
  color: #fff;
  background: linear-gradient(135deg, var(--brand), var(--brand-2));
  border-color: transparent;
}
.sector-menu,
.pop-menu {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  z-index: 20;
  min-width: 12rem;
  max-height: 16rem;
  overflow-y: auto;
  padding: 0.35rem;
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 16px 40px -12px rgba(0, 0, 0, 0.85);
}
.sector-option,
.pop-option {
  width: 100%;
  text-align: left;
  white-space: nowrap;
  padding: 0.4rem 0.6rem;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  font-size: 0.85rem;
  text-transform: none;
  letter-spacing: normal;
  font-weight: 500;
  cursor: pointer;
}
.sector-option:hover,
.pop-option:hover {
  background: rgba(45, 127, 249, 0.12);
}
.sector-option[aria-selected="true"],
.pop-option[aria-selected="true"] {
  background: rgba(45, 127, 249, 0.2);
  font-weight: 700;
  color: #fff;
}

/* ---------- pager ---------- */
.pager {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  align-items: center;
  margin-top: 1.25rem;
}
.pager button {
  min-width: 2.4rem;
  padding: 0.4rem 0.7rem;
  border-radius: var(--radius-sm);
  background: var(--surface);
  border: 1px solid var(--border);
  cursor: pointer;
  transition: border-color 0.12s ease, background 0.12s ease;
}
.pager button:hover:not(:disabled) {
  border-color: var(--brand);
}
.pager button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}
.pager button[aria-current="page"] {
  font-weight: 800;
  color: #fff;
  background: linear-gradient(135deg, var(--brand), var(--brand-2));
  border-color: transparent;
}
.pager-gap {
  padding: 0 0.25rem;
  color: var(--muted);
}
.pager-info {
  margin-left: auto;
  color: var(--muted);
  font-size: 0.9rem;
}

/* ---------- stock page ---------- */
article {
  margin-top: 1rem;
  padding: 1.5rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
article > nav {
  margin-bottom: 1rem;
}
article header {
  margin-bottom: 1.25rem;
}
article h1 {
  font-size: 1.8rem;
  font-weight: 800;
  letter-spacing: -0.01em;
}
article h1 small {
  font-family: var(--mono);
  font-size: 0.55em;
  font-weight: 600;
  color: var(--muted);
}
article header p {
  color: var(--muted);
}
article img {
  border-radius: var(--radius-sm);
  margin: 0 0 1.25rem;
}

article nav a {
  color: var(--muted);
}

/* ---------- mobile: cards + reachable sector filter ---------- */
@media (max-width: 640px) {
  .container {
    width: auto;
    max-width: none;
    padding-right: 1rem;
    padding-left: 1rem;
  }
  .hero {
    padding: 1.5rem;
  }
  .chips-row {
    display: flex;
  }

  /* popups become bottom-sheets: full-width, slide up, large tap targets */
  .sheet-backdrop:not([hidden]) {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 30;
    background: rgba(0, 0, 0, 0.55);
  }
  body.sheet-open {
    overflow: hidden;
  }
  .js-popup[open] > .pop-menu,
  .js-popup[open] > .sector-menu {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    top: auto;
    z-index: 40;
    min-width: 0;
    max-height: 75vh;
    border-radius: 18px 18px 0 0;
    padding: 0.4rem 0.75rem calc(1.25rem + env(safe-area-inset-bottom));
    box-shadow: 0 -12px 40px -10px rgba(0, 0, 0, 0.8);
    animation: sheet-up 0.22s ease;
  }
  /* grab handle + title */
  .js-popup[open] > .pop-menu::before,
  .js-popup[open] > .sector-menu::before {
    display: block;
    text-align: center;
    font-weight: 700;
    color: var(--muted);
    padding: 1rem 0 0.75rem;
    margin-bottom: 0.4rem;
    border-bottom: 1px solid var(--border);
    border-top: 4px solid var(--border); /* doubles as a grab-handle cue */
    border-radius: 3px 3px 0 0;
  }
  .sort-pop[open] > .pop-menu::before {
    content: "Sort by";
  }
  .chip-sector[open] > .sector-menu::before {
    content: "Filter by sector";
  }
  .js-popup[open] .pop-option,
  .js-popup[open] .sector-option {
    padding: 0.95rem 1rem;
    font-size: 1rem;
    border-radius: 12px;
  }
  table {
    display: block;
    background: transparent;
    border: 0;
    box-shadow: none;
    overflow: visible;
  }
  thead {
    display: block;
  }
  /* column titles hidden on mobile; sorting + sector filter live in the chips row */
  .cols {
    display: none;
  }
  tbody {
    display: block;
  }
  tbody tr + tr {
    border-top: 0;
  }

  /* card shell */
  .stock-row {
    display: grid;
    grid-template-columns: auto 1fr auto;
    grid-template-rows: auto auto auto auto auto;
    gap: 0.1rem 0.4rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    margin-bottom: 0.75rem;
    padding: 0.55rem 0.75rem;
    width: auto;
  }

  /* all cells: reset the default block/grid from tbody td rule */
  .stock-row td {
    display: block;
    width: auto;
    padding: 0;
    min-width: 0;
  }
  .stock-row td::before { display: none; }

  /* Name: spans col 1+2, row 1 */
  .stock-row td:nth-child(1) {
    grid-column: 1 / 3;
    grid-row: 1;
    align-self: center;
    overflow-wrap: anywhere;
  }

  /* Ticker: col 1, row 2 */
  .stock-row td:nth-child(2) {
    grid-column: 1;
    grid-row: 2;
    font-size: 0.8rem;
    color: var(--muted);
    font-family: var(--mono);
  }

  /* Sector: col 2, row 2 — prefixed with separator */
  .stock-row td:nth-child(3) {
    grid-column: 2;
    grid-row: 2;
    font-size: 0.8rem;
    color: var(--muted);
    align-self: center;
  }
  .stock-row td:nth-child(3)::before {
    content: "· ";
    display: inline;
  }

  /* Score badge: col 3, rows 1+2 — centered on the right */
  .stock-row td:nth-child(4) {
    grid-column: 3;
    grid-row: 1 / 3;
    align-self: center;
    justify-self: end;
  }

  /* Charts: full width with label | bar layout */
  .stock-row td:nth-child(5) { grid-column: 1 / -1; grid-row: 3; }
  .stock-row td:nth-child(6) { grid-column: 1 / -1; grid-row: 4; }
  .stock-row td:nth-child(7) { grid-column: 1 / -1; grid-row: 5; }
  .stock-row td:nth-child(5),
  .stock-row td:nth-child(6),
  .stock-row td:nth-child(7) {
    display: grid;
    grid-template-columns: minmax(5rem, 2fr) minmax(0, 3fr);
    gap: 0.5rem;
    align-items: center;
    padding: 0.15rem 0;
  }
  .stock-row td:nth-child(5)::before,
  .stock-row td:nth-child(6)::before,
  .stock-row td:nth-child(7)::before {
    display: block;
    content: attr(data-label);
    font-weight: 700;
    color: var(--muted);
    font-size: 0.8rem;
  }

  .chart {
    width: 100%;
    min-width: 0;
    height: 51px;
  }
  .chart-lg {
    height: 264px;
  }
  .badge {
    max-width: 100%;
  }
}
