/* =============================================================================
   Prism — Design System
   Tokens, layout, and component styles. Dark-first with a light theme toggle.
   ========================================================================== */

:root {
  /* Brand */
  --brand: #7c5cff;
  --brand-2: #4f8cff;
  --brand-grad: linear-gradient(135deg, #7c5cff 0%, #4f8cff 100%);

  /* Dark theme (default) */
  --bg: #0b0d17;
  --bg-2: #12152400;
  --surface: #141829;
  --surface-2: #1b2036;
  --surface-3: #232a45;
  --border: #262c48;
  --text: #eef1ff;
  --text-dim: #9aa3c7;
  --text-mute: #6b7398;

  --good: #2fd28a;
  --warn: #ffb020;
  --bad: #ff5d6c;
  --info: #4f8cff;

  /* Spacing / radius / shadow / type */
  --sp: 8px;
  --radius: 14px;
  --radius-sm: 9px;
  --shadow: 0 6px 24px rgba(0,0,0,.35);
  --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --sidebar-w: 246px;
}

[data-theme='light'] {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-2: #f7f8fc;
  --surface-3: #eef1f8;
  --border: #e3e7f0;
  --text: #131628;
  --text-dim: #4c5570;
  --text-mute: #8891ad;
  --shadow: 0 6px 20px rgba(20,30,60,.08);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}
a { color: inherit; text-decoration: none; }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: var(--surface-3); border-radius: 8px; }

/* ---- App shell ----------------------------------------------------------- */
.app { display: grid; grid-template-columns: var(--sidebar-w) 1fr; min-height: 100vh; }

.sidebar {
  background: var(--surface); border-right: 1px solid var(--border);
  padding: 18px 12px; position: sticky; top: 0; height: 100vh; overflow-y: auto;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 18px; }
.brand .logo {
  width: 30px; height: 30px; border-radius: 9px; background: var(--brand-grad);
  display: grid; place-items: center; font-weight: 800; color: #fff; box-shadow: 0 4px 14px rgba(124,92,255,.5);
}
.brand h1 { font-size: 16px; margin: 0; letter-spacing: .2px; }
.brand small { color: var(--text-mute); font-size: 11px; display: block; }

.nav-group { margin: 14px 0 4px; padding: 0 10px; color: var(--text-mute);
  font-size: 10.5px; font-weight: 700; letter-spacing: .12em; text-transform: uppercase; }
.nav-item {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 9px;
  color: var(--text-dim); cursor: pointer; font-weight: 500; font-size: 13px;
  transition: background .12s, color .12s;
}
.nav-item:hover { background: var(--surface-2); color: var(--text); }
.nav-item.active { background: var(--surface-3); color: var(--text); }
.nav-item.active .ico { filter: none; }
.nav-item .ico { width: 18px; text-align: center; opacity: .9; }

/* ---- Main / topbar ------------------------------------------------------- */
.main { min-width: 0; display: flex; flex-direction: column; }
.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 14px;
  padding: 12px 22px; background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(10px); border-bottom: 1px solid var(--border);
}
.topbar .title { font-size: 18px; font-weight: 700; }
.topbar .sub { color: var(--text-mute); font-size: 12px; }
.spacer { flex: 1; }
.search {
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 8px 12px; color: var(--text); width: 260px; font-size: 13px;
}
.pill {
  display: inline-flex; align-items: center; gap: 6px; background: var(--surface);
  border: 1px solid var(--border); border-radius: 999px; padding: 6px 12px;
  font-size: 12px; color: var(--text-dim); cursor: pointer;
}
.pill .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--good); box-shadow: 0 0 8px var(--good); }
.icon-btn {
  width: 36px; height: 36px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); cursor: pointer; font-size: 15px;
}
.content { padding: 22px; max-width: 1500px; width: 100%; }

/* ---- Grid + cards -------------------------------------------------------- */
.grid { display: grid; gap: 16px; }
/* A grid item may shrink below its content's width, so a wide table scrolls
   inside its own box instead of widening the whole page on a phone. */
.grid > * { min-width: 0; }
.cols-2 { grid-template-columns: repeat(2, 1fr); }
.cols-3 { grid-template-columns: repeat(3, 1fr); }
.cols-4 { grid-template-columns: repeat(4, 1fr); }
.cols-5 { grid-template-columns: repeat(5, 1fr); }
.cols-6 { grid-template-columns: repeat(6, 1fr); }
.span-2 { grid-column: span 2; }
.span-3 { grid-column: span 3; }
.span-4 { grid-column: span 4; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow);
}
.card h3 { margin: 0 0 2px; font-size: 14px; }
.card .card-sub { color: var(--text-mute); font-size: 11.5px; margin-bottom: 12px; }
.card-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.card-head h3 { margin: 0; }

/* KPI tile */
.kpi { display: flex; flex-direction: column; gap: 6px; }
.kpi .label { color: var(--text-mute); font-size: 12px; display: flex; align-items: center; gap: 6px; }
.kpi .value { font-size: 26px; font-weight: 750; letter-spacing: -.5px; }
.kpi .delta { font-size: 12px; font-weight: 600; display: inline-flex; align-items: center; gap: 4px; }
.delta.up { color: var(--good); } .delta.down { color: var(--bad); } .delta.flat { color: var(--text-mute); }

/* Provenance badge */
.prov { font-size: 9.5px; font-weight: 700; letter-spacing: .04em; padding: 2px 6px; border-radius: 6px;
  border: 1px solid var(--border); color: var(--text-mute); cursor: help; text-transform: uppercase; }
.prov.direct { color: var(--good); border-color: color-mix(in srgb, var(--good) 40%, var(--border)); }
.prov.derived { color: var(--info); border-color: color-mix(in srgb, var(--info) 40%, var(--border)); }
.prov.estimated { color: var(--warn); border-color: color-mix(in srgb, var(--warn) 40%, var(--border)); }
.prov.sample { color: var(--warn); border-style: dashed; border-color: color-mix(in srgb, var(--warn) 55%, var(--border)); }
.kpi .kpi-sub { font-size: 12px; color: var(--text-mute); margin-top: 4px; line-height: 1.35; }
.upnext { font-size: 12.5px; color: var(--text-mute); padding: 8px 2px 0; border-top: 1px dashed var(--border); margin-top: 6px; }
.upnext b { color: var(--text); font-weight: 600; }
.prov.manual { color: var(--brand); border-color: color-mix(in srgb, var(--brand) 40%, var(--border)); }
.prov.near { color: var(--info); }

/* Chart container */
.chart { width: 100%; height: 300px; }
.chart.sm { height: 220px; }
.chart.lg { height: 380px; }

/* ---- Tables -------------------------------------------------------------- */
.table-tools { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-bottom: 12px; }
.table-tools input, .table-tools select {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 9px;
  color: var(--text); padding: 8px 11px; font-size: 13px;
}
.table-wrap { overflow-x: auto; border-radius: var(--radius-sm); }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 10px 12px; text-align: left; white-space: nowrap; }
thead th {
  color: var(--text-mute); font-weight: 600; font-size: 11.5px; text-transform: uppercase;
  letter-spacing: .04em; border-bottom: 1px solid var(--border); cursor: pointer; user-select: none;
}
thead th.sorted { color: var(--brand); }
tbody tr { border-bottom: 1px solid var(--border); }
tbody tr:hover { background: var(--surface-2); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
.thumb { width: 78px; height: 44px; border-radius: 6px; object-fit: cover; background: var(--surface-3); }
.tag { font-size: 10.5px; padding: 2px 8px; border-radius: 999px; background: var(--surface-3); color: var(--text-dim); }
.tag.long { color: var(--info); } .tag.short { color: var(--brand); } .tag.live { color: var(--bad); }

.btn {
  background: var(--surface-2); border: 1px solid var(--border); color: var(--text);
  border-radius: 9px; padding: 8px 13px; font-size: 13px; cursor: pointer; font-weight: 500;
}
.btn.primary { background: var(--brand-grad); border: none; color: #fff; }
.btn:hover { filter: brightness(1.08); }
.pager { display: flex; gap: 6px; align-items: center; justify-content: flex-end; margin-top: 12px; color: var(--text-dim); }

/* ---- Health / scores ----------------------------------------------------- */
.score-ring { display: grid; place-items: center; }
.pillars { display: flex; flex-direction: column; gap: 10px; }
.pillar { display: grid; grid-template-columns: 120px 1fr 42px; align-items: center; gap: 10px; font-size: 12.5px; }
.bar { height: 8px; background: var(--surface-3); border-radius: 999px; overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--brand-grad); border-radius: 999px; }

/* ---- Action / insight cards --------------------------------------------- */
.action { display: flex; gap: 12px; padding: 12px; border: 1px solid var(--border);
  border-radius: var(--radius-sm); background: var(--surface-2); margin-bottom: 10px; }
.action .rank { width: 26px; height: 26px; border-radius: 8px; background: var(--brand-grad);
  color: #fff; display: grid; place-items: center; font-weight: 700; font-size: 13px; flex: none; }
.action .body { flex: 1; }
.action .body b { font-size: 13.5px; }
.action .body p { margin: 3px 0 0; color: var(--text-dim); font-size: 12.5px; }
.impact { font-size: 10.5px; font-weight: 700; padding: 2px 7px; border-radius: 6px; }
.impact.high { color: var(--good); background: color-mix(in srgb, var(--good) 14%, transparent); }
.impact.med { color: var(--warn); background: color-mix(in srgb, var(--warn) 14%, transparent); }

/* ---- AI assistant -------------------------------------------------------- */
.ai-wrap { display: grid; grid-template-rows: 1fr auto; height: calc(100vh - 150px); }
.ai-log { overflow-y: auto; padding: 4px; display: flex; flex-direction: column; gap: 14px; }
.msg { display: flex; gap: 10px; max-width: 820px; }
.msg .av { width: 30px; height: 30px; border-radius: 9px; flex: none; display: grid; place-items: center; font-size: 14px; }
.msg.ai .av { background: var(--brand-grad); color: #fff; }
.msg.me { margin-left: auto; flex-direction: row-reverse; }
.msg.me .av { background: var(--surface-3); }
.bubble { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px; font-size: 13.5px; line-height: 1.55; }
.msg.me .bubble { background: var(--surface-3); }
.bubble h4 { margin: 0 0 6px; }
.bubble ul { margin: 6px 0; padding-left: 18px; }
.chips { display: flex; gap: 8px; flex-wrap: wrap; margin: 12px 0; }
.chip { background: var(--surface-2); border: 1px solid var(--border); border-radius: 999px;
  padding: 7px 13px; font-size: 12.5px; cursor: pointer; color: var(--text-dim); }
.chip:hover { color: var(--text); border-color: var(--brand); }
.ai-input { display: flex; gap: 10px; margin-top: 14px; }
.ai-input input { flex: 1; background: var(--surface); border: 1px solid var(--border);
  border-radius: 11px; padding: 12px 14px; color: var(--text); font-size: 14px; }

/* ---- States -------------------------------------------------------------- */
.skeleton { background: linear-gradient(90deg, var(--surface-2) 25%, var(--surface-3) 37%, var(--surface-2) 63%);
  background-size: 400% 100%; animation: sk 1.3s ease infinite; border-radius: 8px; }
@keyframes sk { 0% { background-position: 100% 0; } 100% { background-position: -100% 0; } }
.empty { text-align: center; color: var(--text-mute); padding: 40px 20px; }
.empty .big { font-size: 34px; margin-bottom: 8px; }

.note { font-size: 12px; color: var(--text-mute); border-left: 3px solid var(--brand);
  padding: 8px 12px; background: var(--surface-2); border-radius: 0 8px 8px 0; margin-top: 12px; }

.fade-in { animation: fade .35s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

/* ---- Responsive ---------------------------------------------------------- */
@media (max-width: 1100px) {
  .cols-4, .cols-5, .cols-6 { grid-template-columns: repeat(2, 1fr); }
  .cols-3 { grid-template-columns: repeat(2, 1fr); }
  .span-2, .span-3, .span-4 { grid-column: span 2; }
}
@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; z-index: 100; transform: translateX(-100%); transition: transform .2s; width: 260px; }
  .sidebar.open { transform: none; }
  .cols-2, .cols-3, .cols-4, .cols-5, .cols-6 { grid-template-columns: 1fr; }
  .span-2, .span-3, .span-4 { grid-column: span 1; }
  .search { width: 140px; }
  .menu-toggle { display: inline-block !important; }
}
@media (max-width: 560px) {
  .topbar .pill { display: none; }   /* drop the decorative "Live" status pill so nothing clips */
  .search { width: 120px; }
  .topbar .sub { display: none; }     /* keep the header compact on phones */
}
.menu-toggle { display: none; }

/* Accessibility: visible focus + reduced motion */
:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }

/* ============================================================================
   v2 visual upgrade — ambient background, card depth, sparklines, deep-dive
   ========================================================================== */
body {
  background:
    radial-gradient(1100px 560px at 85% -12%, color-mix(in srgb, var(--brand) 13%, transparent), transparent),
    radial-gradient(820px 460px at -8% 6%, color-mix(in srgb, var(--brand-2) 11%, transparent), transparent),
    var(--bg);
  background-attachment: fixed;
}
.card { transition: transform .15s ease, box-shadow .15s ease, border-color .15s ease; }
.card:hover { transform: translateY(-2px); border-color: color-mix(in srgb, var(--brand) 28%, var(--border)); }
.brand .logo { font-size: 15px; }

/* Sidebar active accent bar */
.nav-item.active { position: relative; background: color-mix(in srgb, var(--brand) 15%, transparent); }
.nav-item.active::before {
  content: ''; position: absolute; left: -12px; top: 6px; bottom: 6px; width: 3px;
  border-radius: 0 3px 3px 0; background: var(--brand-grad);
}

/* KPI sparkline row + delta pill */
.kpi-row { display: flex; align-items: flex-end; justify-content: space-between; gap: 10px; }
.kpi-row .spark { width: 120px; height: 34px; flex: none; opacity: .95; }
.kpi .value { line-height: 1.1; }
.delta { padding: 3px 9px; border-radius: 999px; width: fit-content; margin-top: 2px; }
.delta.up { background: color-mix(in srgb, var(--good) 13%, transparent); }
.delta.down { background: color-mix(in srgb, var(--bad) 13%, transparent); }
.delta.flat { background: var(--surface-3); }

/* Clickable rows / action cards */
.action.clickable { cursor: pointer; }
.action.clickable:hover { background: var(--surface-3); border-color: color-mix(in srgb, var(--brand) 40%, var(--border)); }
tbody tr[style*="cursor"] :is(td) { transition: none; }

/* Video deep-dive hero */
.deepdive-hero { display: flex; gap: 20px; align-items: center; }
.hero-thumb { width: 280px; max-width: 40%; border-radius: 14px; aspect-ratio: 16/9; object-fit: cover; box-shadow: var(--shadow); }
.hero-title { margin: 8px 0 5px; font-size: 21px; line-height: 1.25; }
.hero-meta { color: var(--text-mute); font-size: 12.5px; }

/* Competitor candidate rows */
.cand { display: flex; align-items: center; gap: 13px; padding: 11px 0; border-bottom: 1px solid var(--border); }
.cand:last-child { border-bottom: none; }
.cand-match { width: 48px; height: 48px; flex: none; border-radius: 13px; display: grid; place-items: center; font-weight: 800; font-size: 13px; }
.cand-match.high { color: var(--good); background: color-mix(in srgb, var(--good) 15%, transparent); }
.cand-match.med { color: var(--warn); background: color-mix(in srgb, var(--warn) 15%, transparent); }
.cand-match.low { color: var(--text-mute); background: var(--surface-3); }
.cand-body { flex: 1; min-width: 0; }
.cand-body b { font-size: 13.5px; }
.cand-reason { color: var(--text-mute); font-size: 12px; margin-top: 2px; }

@media (max-width: 820px) {
  .deepdive-hero { flex-direction: column; align-items: flex-start; }
  .hero-thumb { max-width: 100%; width: 100%; }
}

/* Thumbnail Analytics */
.chart.chart-tall { height: 340px; }
.trec { display: flex; gap: 14px; align-items: flex-start; padding: 12px 0; border-top: 1px solid var(--border); }
.trec:first-child { border-top: 0; padding-top: 4px; }
.trec-img { width: 128px; aspect-ratio: 16 / 9; height: auto; border-radius: 8px; object-fit: cover; background: var(--surface-3); flex: none; cursor: pointer; }
.trec-body { flex: 1; min-width: 0; font-size: 13px; line-height: 1.6; color: var(--text-dim); }
.trec-head { color: var(--text); font-size: 14px; }
.trec-sub { font-size: 12px; opacity: .85; }
.tgallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 12px; }
.ttile { cursor: pointer; font-size: 12px; color: var(--text-dim); min-width: 0; }
.ttile img { width: 100%; aspect-ratio: 16 / 9; object-fit: cover; border-radius: 8px; background: var(--surface-3); display: block; }
.ttile-meta { margin-top: 6px; color: var(--text); }
.ttile-title { overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; line-height: 1.4; }
.ttile:hover img, .trec-img:hover { outline: 2px solid var(--brand); outline-offset: 1px; }
@media (max-width: 560px) { .trec-img { width: 96px; } }

/* Exam Calendar: stage name + date pills wrap as whole boxes, never across lines */
.cal-head { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 8px; }
.cal-head b { line-height: 1.4; }
.cal-head .impact { display: inline-block; line-height: 1.5; }
.cal-pill { display: inline-block; white-space: nowrap; font-size: 11.5px; line-height: 1.5; padding: 1px 9px;
  border-radius: 999px; background: var(--surface-2); border: 1px solid var(--border); color: var(--text-dim); }

/* Reports: Print / Save as PDF shows only the report, dark text on white */
@media print {
  body * { visibility: hidden !important; }
  .report-print, .report-print * { visibility: visible !important; color: #111 !important; background: transparent !important;
    border-color: #ccc !important; box-shadow: none !important; }
  .report-print { position: absolute; left: 0; top: 0; width: 100%; padding: 12px; }
  .report-print table { font-size: 11px; }
  .no-print { display: none !important; }
}

/* Overview: charts and big numbers, few words */
.ov > * + * { margin-top: 16px; }
.ov .card-head .ov-open { margin-left: auto; font-size: 12px; color: var(--text-mute); white-space: nowrap; }
.ov .card-head .ov-open:hover, .ov-more:hover { color: var(--brand); }
.ov-more { display: inline-block; margin-top: 8px; font-size: 12.5px; color: var(--text-dim); }
.ov-stat { font-size: 12px; color: var(--text-mute); margin: -6px 0 6px; }
.ov-stat b { color: var(--text); }
.ov-news { display: flex; align-items: center; gap: 12px; padding: 9px 0; border-top: 1px solid var(--border); }
.ov-news:first-child { border-top: 0; }
.ov-news-main { flex: 1; min-width: 0; }
.ov-news-title { margin-top: 4px; color: var(--text); font-size: 13.5px; line-height: 1.45; overflow-wrap: anywhere;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.ov-news .btn { flex: none; font-size: 12px; padding: 6px 10px; white-space: nowrap; text-decoration: none; }
.ov-date { display: flex; align-items: center; gap: 12px; padding: 9px 0; border-top: 1px solid var(--border); }
.ov-date:first-child { border-top: 0; padding-top: 0; }
.ov-date-when { flex: none; width: 76px; text-align: center; padding: 6px 4px; border-radius: 9px; background: var(--surface-2); border: 1px solid var(--border); }
.ov-date-when b { display: block; font-size: 13px; }
.ov-date-when span { font-size: 11px; color: var(--text-mute); }
.ov-date-when.soon { border-color: color-mix(in srgb, var(--warn) 60%, var(--border)); }
.ov-date-what { font-size: 13px; line-height: 1.5; color: var(--text); min-width: 0; }
.ov-act { display: flex; align-items: flex-start; gap: 10px; padding: 10px 0; border-top: 1px solid var(--border); font-size: 13px; line-height: 1.45; }
.ov-act:first-child { border-top: 0; padding-top: 0; }
.ov-act .rank { width: 22px; height: 22px; border-radius: 7px; background: var(--brand-grad); color: #fff; display: grid; place-items: center; font-weight: 700; font-size: 12px; flex: none; }
.ov-act-text { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.ov-tiles { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
.ov-tile { display: block; min-width: 0; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 13px 14px;
  box-shadow: var(--shadow); color: inherit; transition: border-color .15s ease, transform .15s ease; }
.ov-tile:hover { border-color: color-mix(in srgb, var(--brand) 40%, var(--border)); transform: translateY(-2px); }
.ov-tile-label { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-mute); }
.ov-tile-value { font-size: 24px; font-weight: 750; letter-spacing: -.4px; margin: 5px 0 2px; }
.ov-tile-value.up { color: var(--good); }
.ov-tile-value.down { color: var(--bad); }
.ov-tile-sub { font-size: 11.5px; color: var(--text-mute); line-height: 1.35; }
.ov-foot { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; font-size: 12px; color: var(--text-mute); }
@media (max-width: 1100px) { .ov-tiles { grid-template-columns: repeat(2, 1fr); } }
.ov-health { padding: 11px 14px; border-radius: var(--radius-sm); font-size: 13px; line-height: 1.55; color: var(--text);
  background: color-mix(in srgb, var(--warn) 12%, var(--surface)); border: 1px solid color-mix(in srgb, var(--warn) 50%, var(--border)); }
.ov-health > div + div { margin-top: 4px; }
.ov-down { color: var(--warn); }

/* Earnings are owner-only: the Revenue pages leave the menu when hidden (js/app.js applyEarningsRule). */
.rev-hidden .rev-only { display: none !important; }
