:root {
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-alt: #f1f5f9;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --accent: #2563eb;
  --accent-soft: #eff6ff;
  --profit: #dc2626;
  --loss: #16a34a;
  --warn-bg: #fffbeb;
  --warn-text: #92400e;
  --warn-border: #fde68a;
  --radius: 12px;
}

[data-theme="dark"] {
  --bg: #0b1120;
  --surface: #111827;
  --surface-alt: #1e293b;
  --border: #334155;
  --text: #f8fafc;
  --muted: #94a3b8;
  --accent: #38bdf8;
  --accent-soft: rgba(56, 189, 248, 0.14);
  --profit: #f87171;
  --loss: #4ade80;
  --warn-bg: rgba(245, 158, 11, 0.12);
  --warn-text: #fcd34d;
  --warn-border: rgba(245, 158, 11, 0.35);
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, "PingFang TC", "Microsoft JhengHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 10px 16px;
}
.topbar-inner {
  max-width: 1240px;
  margin: 0 auto;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.brand {
  display: flex;
  align-items: baseline;
  gap: 10px;
}
.brand h1 {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 800;
}
.badge {
  font-size: 0.72rem;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--surface-alt);
  color: var(--muted);
  border: 1px solid var(--border);
}
.nav {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
}
.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  padding: 6px 12px;
  border-radius: 8px;
  font-size: 0.85rem;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-alt); }
.btn.active, .btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #ffffff;
  font-weight: 600;
}
[data-theme="dark"] .btn.active, [data-theme="dark"] .btn.primary {
  color: #0b1120;
}

.container {
  max-width: 1240px;
  margin: 0 auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
}
.panel-title {
  margin: 0 0 10px;
  font-size: 0.95rem;
  font-weight: 700;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}
.input, .select {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  padding: 6px 10px;
  border-radius: 8px;
  font-size: 0.85rem;
}
.ticker-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(145px, 1fr));
  gap: 6px;
  max-height: 210px;
  overflow-y: auto;
  margin-top: 8px;
  padding-right: 2px;
}
.ticker-chip {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  text-align: left;
  padding: 6px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  min-width: 0;
}
.ticker-chip.selected {
  border-color: var(--accent);
  background: var(--accent-soft);
}
.ticker-chip strong { font-size: 0.82rem; font-family: ui-monospace, monospace; }
.ticker-chip span {
  font-size: 0.74rem;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 100%;
}

.cards-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 8px;
  margin-bottom: 12px;
}
.stat-card {
  border: 1px solid var(--border);
  border-top-width: 3px;
  background: var(--surface-alt);
  border-radius: 10px;
  padding: 10px 12px;
  text-align: left;
  cursor: pointer;
  min-width: 0;
  color: var(--text);
  transition: opacity 0.15s ease, transform 0.15s ease;
}
.stat-card.dimmed { opacity: 0.42; }
.stat-card.focused { box-shadow: 0 0 0 2px var(--accent); }
.stat-card .sc-code { font-size: 0.76rem; font-weight: 700; color: var(--muted); }
.stat-card .sc-name { font-size: 0.76rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.stat-card .sc-val { font-size: 1.25rem; font-weight: 800; font-family: ui-monospace, monospace; margin: 2px 0; }
.stat-card .sc-diff { font-size: 0.75rem; font-weight: 600; }
.sc-diff.up { color: var(--profit); }
.sc-diff.down { color: var(--loss); }
.sc-diff.flat { color: var(--muted); }

.chart-wrap {
  position: relative;
  width: 100%;
  overflow: hidden;
}
.tooltip {
  position: absolute;
  top: 12px;
  pointer-events: none;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  min-width: 185px;
  box-shadow: 0 8px 24px rgba(15, 23, 42, 0.16);
  font-size: 0.78rem;
}
.tt-date { font-weight: 700; margin-bottom: 4px; font-family: ui-monospace, monospace; }
.tt-row { display: flex; align-items: center; gap: 6px; justify-content: space-between; margin: 2px 0; }
.tt-dot { width: 8px; height: 8px; border-radius: 50%; flex: 0 0 8px; }
.tt-name { flex: 1; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tt-val { font-family: ui-monospace, monospace; font-weight: 700; }

.notice {
  background: var(--warn-bg);
  color: var(--warn-text);
  border: 1px solid var(--warn-border);
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 0.8rem;
}
.muted { color: var(--muted); font-size: 0.8rem; }

.table-wrap {
  overflow-x: auto;
  width: 100%;
}
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.84rem;
}
th, td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  text-align: right;
  white-space: nowrap;
}
th:first-child, td:first-child, th:nth-child(2), td:nth-child(2) { text-align: left; }
th { color: var(--muted); font-weight: 600; background: var(--surface-alt); }
.num-pos { color: var(--profit); font-weight: 600; font-family: ui-monospace, monospace; }
.num-neg { color: var(--loss); font-weight: 600; font-family: ui-monospace, monospace; }
.num-flat { color: var(--muted); font-family: ui-monospace, monospace; }

.footer {
  max-width: 1240px;
  margin: 0 auto 24px;
  padding: 0 16px;
  font-size: 0.76rem;
  color: var(--muted);
}

.hidden { display: none !important; }
