/* =========================================================
   CSS Custom Properties — Dark Theme
   ========================================================= */
:root {
  --bg:        #0f1923;
  --surface:   #1a2535;
  --surface2:  #243348;
  --surface3:  #2a3a52;
  --border:    #2e4265;
  --text:      #e8eef6;
  --text-soft: #c9d6e7;
  --muted:     #a8b8cc;
  --accent:    #4d9fff;
  --up:        #40c057;
  --down:      #fa5252;
  --warn:      #fab005;
  --radius:    8px;
  --shadow:    0 2px 12px rgba(0,0,0,.5);
  --focus:     0 0 0 3px rgba(77,159,255,.25);
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", "Microsoft JhengHei", "Noto Sans TC", system-ui, sans-serif;
  font-size: 15px;
  line-height: 1.65;
  letter-spacing: 0;
  text-rendering: optimizeLegibility;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* =========================================================
   Navbar
   ========================================================= */
.navbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 20px;
  height: 54px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 100;
}

.nav-brand {
  font-weight: 700;
  font-size: 16px;
  color: var(--text);
  white-space: nowrap;
  text-decoration: none;
}
.brand-icon { color: var(--accent); margin-right: 4px; }

.nav-indices {
  display: flex;
  gap: 4px;
  flex: 1;
  overflow-x: auto;
  scrollbar-width: none;
}
.nav-index-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 11px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface2);
  text-decoration: none;
  white-space: nowrap;
}
.nav-idx-label { font-weight: 600; color: var(--muted); font-size: 12px; }
.nav-idx-price { color: var(--text); font-size: 13px; }
.nav-idx-chg   { font-size: 13px; }
.nav-index-item:hover { background: var(--border); }
.nav-index-item.active-nav { border-color: var(--accent); }
.nav-right { margin-left: auto; }

.clock {
  font-size: 13px;
  color: var(--muted);
  white-space: nowrap;
}

/* =========================================================
   Layout helpers
   ========================================================= */
.main-wrap { padding: 22px 24px; max-width: 1520px; margin: 0 auto; }
.section    { margin-bottom: 18px; }

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

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
  flex-wrap: wrap;
  gap: 8px;
}
.card-title { font-weight: 600; font-size: 15px; }

.two-col {
  display: grid;
  grid-template-columns: 1fr 360px;
  gap: 16px;
}
.col-wide  {}
.col-narrow {}

.divider { border: none; border-top: 1px solid var(--border); margin: 12px 0; }
.chart-wrap { position: relative; }

/* =========================================================
   Index cards
   ========================================================= */
.index-cards {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
}

.index-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
}
.index-card .idx-label { font-size: 12px; color: var(--muted); text-transform: uppercase; }
.index-card .idx-price { font-size: 23px; font-weight: 700; margin: 4px 0; }
.index-card .idx-chg   { font-size: 14px; }
.index-card .idx-sub   { font-size: 12px; color: var(--muted); margin-top: 4px; }
.index-card.up   { border-left: 3px solid var(--up); }
.index-card.down { border-left: 3px solid var(--down); }
.index-card:hover { opacity: .85; transform: translateY(-1px); transition: opacity .15s, transform .15s; }
.index-card.active-idx { outline: 2px solid var(--accent); outline-offset: 2px; }

/* =========================================================
   Macro strip (FRED)
   ========================================================= */
.macro-strip {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}
.macro-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 9px 14px;
  display: flex;
  flex-direction: column;
  min-width: 130px;
  position: relative;
}
.macro-label-row { display: flex; align-items: center; gap: 4px; }
.macro-label  { font-size: 12px; color: var(--muted); }
.macro-value  { font-size: 17px; font-weight: 600; margin-top: 2px; }

/* Shared "ⓘ" click-to-toggle info popover — also used on ML/DL analysis pages */
.kpi-info-icon { cursor: pointer; color: var(--muted); font-size: .82em; margin-left: 3px; }
.kpi-info-icon:hover { color: var(--accent); }
.kpi-info-box {
  position: absolute; top: 100%; left: 0; margin-top: 6px;
  width: 260px; max-width: 80vw;
  background: var(--surface); border: 1px solid var(--border); border-radius: 6px;
  padding: 10px 12px; font-size: .78rem; line-height: 1.6; color: var(--text);
  white-space: pre-line; z-index: 60; box-shadow: 0 8px 24px rgba(0,0,0,.5);
  text-transform: none; font-weight: 400; letter-spacing: normal;
}

/* =========================================================
   Tab groups
   ========================================================= */
.tab-group {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
}
.tab-btn {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--muted);
  cursor: pointer;
  font-size: 14px;
  padding: 5px 12px;
  transition: background .15s, color .15s;
}
.tab-btn:hover  { background: var(--border); color: var(--text); }
.tab-btn.active { background: var(--accent); border-color: var(--accent); color: #fff; }

/* View toggle (daily / longterm) */
.view-toggle { display: flex; gap: 8px; }
.view-btn {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--muted);
  cursor: pointer;
  font-size: 14px;
  padding: 6px 18px;
  font-weight: 600;
  transition: background .15s, color .15s;
}
.view-btn:hover  { background: var(--border); color: var(--text); }
.view-btn.active { background: var(--accent); border-color: var(--accent); color: #fff; }

/* Period toggle (annual / quarterly) */
.period-toggle { display: flex; gap: 4px; margin-bottom: 12px; }
.period-btn {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--muted);
  cursor: pointer;
  font-size: 13px;
  padding: 4px 11px;
}
.period-btn.active { background: var(--warn); border-color: var(--warn); color: #000; }

/* Range buttons (1D/1M/…/5Y) */
.range-btns { display: flex; gap: 4px; margin-bottom: 10px; }
.range-btn {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--muted);
  cursor: pointer;
  font-size: 13px;
  padding: 4px 11px;
}
.range-btn.active { background: var(--accent); border-color: var(--accent); color: #fff; }

/* =========================================================
   Sector Volatility Matrix
   ========================================================= */
.vol-matrix-scroll {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.vol-matrix {
  width: 100%;
  border-collapse: separate;
  border-spacing: 4px;
  min-width: 380px;
}
.vol-matrix thead th {
  font-size: 12px;
  color: var(--muted);
  font-weight: 600;
  padding: 2px 4px 6px;
  text-align: center;
}
.vol-matrix thead th:first-child { width: 90px; }
.vol-sector-name {
  font-size: 12px;
  color: var(--muted);
  font-weight: 600;
  padding: 3px 10px 3px 0;
  text-align: right;
  white-space: nowrap;
}
.vol-cell {
  border-radius: 5px;
  padding: 7px 4px;
  text-align: center;
  font-size: 12px;
  font-weight: 700;
  cursor: default;
  min-width: 56px;
  transition: opacity .15s;
}
.vol-cell:hover { opacity: .78; }
.vol-legend {
  margin-top: 10px;
  font-size: 11px;
  color: var(--muted);
  text-align: right;
  font-style: italic;
}
.vol-empty {
  padding: 20px 0;
  text-align: center;
  color: var(--muted);
  font-size: 13px;
}

/* =========================================================
   Sector heatmap
   ========================================================= */
.sector-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 6px;
}
.sector-cell {
  border-radius: 6px;
  padding: 9px 10px;
  text-align: center;
  cursor: pointer;
  transition: opacity .15s;
}
.sector-cell:hover { opacity: .8; }
.sector-cell .sc-name { font-size: 12px; font-weight: 600; }
.sector-cell .sc-pct  { font-size: 15px; margin-top: 3px; }

/* =========================================================
   Stock table
   ========================================================= */
.table-wrap { overflow-x: auto; }
.stock-table, .data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
.stock-table th, .data-table th {
  text-align: left;
  padding: 11px 13px;
  border-bottom: 2px solid var(--border);
  color: var(--muted);
  font-weight: 600;
  white-space: nowrap;
  font-size: 13px;
  letter-spacing: 0;
}
.stock-table td, .data-table td {
  padding: 11px 13px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  vertical-align: middle;
  line-height: 1.45;
}
.stock-table tr:hover td { background: var(--surface2); }
.stock-table td:first-child a { font-weight: 600; color: var(--accent); }

/* =========================================================
   Back to home breadcrumb
   ========================================================= */
.back-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
  text-decoration: none;
  margin-bottom: 10px;
  transition: color .15s;
}
.back-link:hover { color: var(--accent); text-decoration: none; }
.back-link::before { content: "←"; font-size: 14px; }

/* Live indicator */
.live-dot {
  display: inline-block;
  width: 7px;
  height: 7px;
  background: var(--up);
  border-radius: 50%;
  margin-left: 6px;
  animation: pulse 1.5s ease-in-out infinite;
}
@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50%       { opacity: .4; transform: scale(.7); }
}

/* =========================================================
   Stock header
   ========================================================= */
.stock-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 16px;
}
.stock-hero { display: flex; flex-direction: column; gap: 4px; }
.stock-ticker-big { font-size: 30px; font-weight: 800; color: var(--accent); }
.stock-name-big   { font-size: 17px; color: var(--muted); }
.stock-meta-tags  { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 4px; }
.tag {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 2px 9px;
  font-size: 12px;
  color: var(--text);
}
.tag-dim { color: var(--muted); }

.stock-kpis { display: flex; gap: 16px; flex-wrap: wrap; align-items: center; }
.kpi-box {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
}
.kpi-box .kpi-label { font-size: 11px; color: var(--muted); text-transform: uppercase; }
.kpi-box .kpi-value { font-size: 19px; font-weight: 700; }
.kpi-box .kpi-sub   { font-size: 12px; font-weight: 500; margin-top: 2px; }

/* =========================================================
   KPI row (inside long-term cards)
   ========================================================= */
.kpi-row {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 14px;
}
.kpi-card {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px 16px;
  min-width: 115px;
}
.kpi-card .kc-label { font-size: 11px; color: var(--muted); text-transform: uppercase; }
.kpi-card .kc-value { font-size: 18px; font-weight: 700; margin-top: 3px; }

/* KPI rows that stretch cards to fill full width */
#div-kpis, #cf-kpis, #fin-kpis, #sector-kpis, #ind-kpis { flex-wrap: nowrap; }
#div-kpis .kpi-card, #cf-kpis .kpi-card, #fin-kpis .kpi-card, #sector-kpis .kpi-card, #ind-kpis .kpi-card { flex: 1; }

/* MA summary */
.ma-summary { display: flex; gap: 10px; margin-bottom: 8px; flex-wrap: wrap; }
.ma-box {
  background: var(--surface2);
  border-radius: 6px;
  padding: 4px 11px;
  font-size: 13px;
  border-left: 3px solid;
}

/* =========================================================
   Daily CF summary
   ========================================================= */
#daily-cf-summary {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  padding: 8px 0;
}
.dcf-item {
  background: var(--surface2);
  border-radius: 6px;
  padding: 10px 16px;
  min-width: 140px;
}
.dcf-item .dcf-label { font-size: 11px; color: var(--muted); text-transform: uppercase; }
.dcf-item .dcf-value { font-size: 19px; font-weight: 700; margin-top: 2px; }

/* =========================================================
   News list
   ========================================================= */
.news-list { list-style: none; }
.news-item {
  padding: 11px 0;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.news-item:last-child { border-bottom: none; }
.news-title { color: var(--text); font-size: 14px; font-weight: 500; }
.news-title:hover { color: var(--accent); }
.news-meta  { font-size: 12px; color: var(--muted); }

/* =========================================================
   Quarterly report (auto-generated summary)
   ========================================================= */
.qr-subtitle { font-weight: 700; color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .03em; margin: 20px 0 8px; }
.qr-narrative { margin-top: 18px; display: flex; flex-direction: column; gap: 14px; }
.qr-block-title { font-weight: 700; color: var(--accent); font-size: 13px; margin-bottom: 6px; text-transform: uppercase; letter-spacing: .02em; }
.qr-text { color: var(--text); font-size: 14px; line-height: 1.75; margin: 0; white-space: pre-line; }
.qr-disclaimer { font-size: 11px; color: var(--muted); margin-top: 4px; }

/* =========================================================
   Export buttons
   ========================================================= */
.export-btns { display: flex; gap: 4px; margin-left: auto; align-items: center; }
.export-btn {
  padding: 3px 10px;
  font-size: 12px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--muted);
  border-radius: 4px;
  cursor: pointer;
  transition: color .15s, border-color .15s;
  white-space: nowrap;
}
.export-btn:hover { color: var(--accent); border-color: var(--accent); }

/* KPI mini chart label */
.kpi-mini-label { font-size: 11px; color: var(--muted); text-transform: uppercase; margin-bottom: 4px; }

/* Panel top row — wraps toggle/range + export buttons */
.panel-top-row { display: flex; align-items: center; flex-wrap: wrap; gap: 6px; margin-bottom: 14px; }
.panel-top-row .period-toggle,
.panel-top-row .range-btns   { margin-bottom: 0; }

/* =========================================================
   Colour utilities
   ========================================================= */
.up   { color: var(--up); }
.down { color: var(--down); }
.muted { color: var(--muted); }

/* =========================================================
   Responsive tweaks
   ========================================================= */
@media (max-width: 900px) {
  .index-cards { grid-template-columns: repeat(2, 1fr); }
  .two-col     { grid-template-columns: 1fr; }
}
@media (max-width: 540px) {
  .index-cards { grid-template-columns: 1fr 1fr; }
  .main-wrap   { padding: 10px; }
}

/* =========================================================
   ML / DL analysis readability layer
   ========================================================= */
.panel,
.analysis-panel,
.phase-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 22px 24px;
  margin-bottom: 22px;
}

.panel-header,
.phase-header,
.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 16px;
}

.panel h1,
.panel h2,
.panel h3,
.analysis-panel h1,
.analysis-panel h2,
.analysis-panel h3,
.phase-panel h1,
.phase-panel h2,
.phase-panel h3 {
  line-height: 1.3;
  letter-spacing: 0;
}

.panel p,
.analysis-panel p,
.phase-panel p,
.fig-desc,
.explain-box,
.ml-insight,
.auto-summary,
.summary-text {
  color: var(--text-soft);
  font-size: 15px;
  line-height: 1.85;
  max-width: 1180px;
}

.fig-desc,
.explain-box,
.ml-insight,
.auto-summary {
  background: rgba(77,159,255,.08);
  border-left: 4px solid #6ea1ff;
  border-radius: 0 6px 6px 0;
  padding: 16px 18px;
}

.stat-grid,
.decision-grid,
.summary-card-grid,
.fn-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: 14px;
}

.stat-card,
.decision-card,
.summary-card,
.fn-card,
.kpi-card {
  min-height: 104px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 6px;
}

.stat-card .value,
.decision-card .value,
.summary-card .value {
  font-size: clamp(24px, 2.2vw, 34px);
  line-height: 1.15;
}

.phase-tabs,
.dl-tabs,
.ml-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 16px 0 24px;
}

.phase-tab,
.dl-tab,
.ml-tab,
.p6-dir-btn,
.p8-hz-btn,
select,
input[type="text"],
input[type="search"] {
  min-height: 40px;
  font-size: 15px;
}

button,
.tab-btn,
.range-btn,
.period-btn,
.view-btn,
.export-btn {
  line-height: 1.2;
}

.table-wrap,
.p7-stock-table-wrap,
.scroll-table,
.wide-table-wrap {
  overflow: auto;
  border-radius: 6px;
  scrollbar-color: #8fa3bf #111b27;
}

.compact-status-table,
.candidate-table,
.p7-stock-table {
  width: 100%;
  border-collapse: collapse;
  table-layout: auto;
}

.compact-status-table th,
.candidate-table th,
.p7-stock-table th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--surface2);
}

.formula-box,
.tech-details,
pre,
code {
  font-size: 13px;
  line-height: 1.7;
}

.formula-box,
.tech-details,
pre {
  overflow-x: auto;
}

.badge,
.tag,
.chip,
.summary-chip {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  border-radius: 999px;
  padding: 4px 10px;
  line-height: 1.2;
}

@media (max-width: 900px) {
  .panel,
  .analysis-panel,
  .phase-panel {
    padding: 18px 16px;
  }

  .panel-header,
  .phase-header,
  .section-header {
    align-items: flex-start;
    flex-direction: column;
  }

  .main-wrap {
    padding: 16px 14px;
  }
}
