:root {
  color-scheme: light;
  --bg:        #f4f7fb;
  --bg2:       #ffffff;
  --bg3:       #f1f5f9;
  --border:    #dce3ec;
  --accent:    #4f46e5;
  --accent2:   #7c3aed;
  --green:     #047857;
  --red:       #dc2626;
  --yellow:    #b45309;
  --text:      #172033;
  --muted:     #5f6f85;
  --glass:     #ffffff;
  --glass2:    #f7f9fc;
  --radius:    12px;
  --shadow:    0 6px 20px rgba(15,23,42,.07);
}

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

body {
  font-family: 'Inter', 'Microsoft JhengHei', 'Noto Sans TC', sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  overflow-x: hidden;
}

/* ─ Root Container ───────────────────────────────── */
.root {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  width: 100%;
}

/* ─ Topbar & Presets Tabs ────────────────────────── */
.topbar {
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  padding: 10px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  z-index: 10;
  box-shadow: 0 2px 10px rgba(15,23,42,.04);
  position: sticky;
  top: 0;
  flex-wrap: wrap;
  gap: 12px;
}

.topbar .logo {
  font-size: 1.15rem;
  font-weight: 800;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.topbar .subtitle {
  color: var(--muted);
  font-size: 0.75rem;
  margin-top: 1px;
}

.preset-tabs {
  display: flex;
  gap: 6px;
  background: #edf2f7;
  padding: 3px;
  border-radius: 9px;
  border: 1px solid var(--border);
  flex-wrap: wrap;
}

.preset-tab-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 12px;
  background: transparent;
  border: none;
  border-radius: 6px;
  color: #475569;
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
  font-family: inherit;
}

.preset-tab-btn:hover {
  background: #ffffff;
  color: var(--text);
}

.preset-tab-btn.active {
  background: #ffffff;
  color: var(--accent);
  box-shadow: 0 2px 8px rgba(79, 70, 229, 0.15);
  font-weight: 700;
}

.tab-badge {
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.68rem;
  padding: 1px 4px;
  border-radius: 3px;
  background: #e2e8f0;
  color: #334155;
  font-weight: 700;
}

.preset-tab-btn.active .tab-badge {
  background: #eef2ff;
  color: var(--accent);
}

/* ─ Stats Bar ────────────────────────────────────── */
.stats-bar {
  background: #f8fafc;
  border-bottom: 1px solid var(--border);
  padding: 10px 16px;
  display: grid;
  grid-template-columns: repeat(10, minmax(0, 1fr));
  gap: 8px;
}

.stat-card {
  background: var(--glass);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 10px;
  text-align: center;
  transition: all 0.2s;
  box-shadow: 0 2px 6px rgba(15,23,42,.03);
}

.stat-card:hover {
  background: var(--glass2);
  border-color: #c6d0df;
  transform: translateY(-1px);
}

.stat-label {
  font-size: 0.65rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 3px;
  font-weight: 500;
}

.stat-value {
  font-size: 1.02rem;
  font-weight: 700;
  font-family: 'JetBrains Mono', monospace;
}

.stat-value.pos { color: var(--green); }
.stat-value.neg { color: var(--red); }
.stat-value.neutral { color: var(--text); }

/* ─ Main Content (滿版擴展) ──────────────────────── */
.main-content {
  padding: 12px 16px 36px 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: 100%;
  max-width: 100%;
}

.strategy-badge {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-left: 4px solid var(--accent);
  border-radius: 8px;
  padding: 8px 14px;
  font-size: 0.82rem;
  color: #334155;
  box-shadow: 0 2px 4px rgba(0,0,0,0.02);
}

/* ─ Charts Stack ─────────────────────────────────── */
.chart-wrap {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  width: 100%;
}

.chart-title {
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.chart-title::before {
  content: '';
  display: inline-block;
  width: 3px;
  height: 12px;
  background: linear-gradient(var(--accent), var(--accent2));
  border-radius: 2px;
}

.chart-stack {
  padding: 0;
  overflow: hidden;
}

.chart-section {
  padding: 10px 14px 12px;
}

.chart-section + .chart-section {
  border-top: 1px solid var(--border);
}

.main-chart-box {
  height: 300px;
  position: relative;
  width: 100%;
}

.mdd-chart-box {
  height: 140px;
  position: relative;
  width: 100%;
}

.empty-state {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: 0.85rem;
  background: rgba(255,255,255,0.8);
  display: none;
}

/* ─ Annual Performance ───────────────────────────── */
.annual-wrap {
  padding: 10px 14px 12px;
}

.annual-wrap .chart-title {
  font-size: 0.86rem;
  margin-bottom: 6px;
}

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

.annual-empty {
  color: var(--muted);
  font-size: 0.8rem;
  padding: 8px 0;
}

.annual-row {
  display: grid;
  grid-template-columns: 76px minmax(0,1fr) 92px;
  align-items: center;
  gap: 12px;
}

.annual-year {
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.84rem;
  font-weight: 700;
  color: #334155;
}

.annual-year small {
  display: block;
  margin-top: 1px;
  color: var(--muted);
  font-family: 'Inter', 'Microsoft JhengHei', sans-serif;
  font-size: 0.6rem;
  font-weight: 500;
  white-space: nowrap;
}

.annual-track {
  height: 16px;
  position: relative;
  background: #eef2f7;
  border: 1px solid #e5eaf1;
  border-radius: 6px;
  overflow: hidden;
}

.annual-zero {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 1px;
  background: #94a3b8;
  opacity: 0.65;
  z-index: 2;
}

.annual-bar {
  position: absolute;
  top: 2px;
  bottom: 2px;
  border-radius: 4px;
  min-width: 2px;
  transition: width 0.5s ease;
}

.annual-bar.pos { background: linear-gradient(90deg, #6366f1, #4f46e5); }
.annual-bar.neg { background: linear-gradient(90deg, #f87171, #dc2626); }

.annual-return {
  text-align: right;
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.84rem;
  font-weight: 700;
}

.annual-return.pos { color: var(--green); }
.annual-return.neg { color: var(--red); }

/* ─ Period Table (零水平滾動條優化) ─────────────── */
.table-wrap {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
  width: 100%;
}

.table-header {
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.table-header h3 {
  font-size: 0.84rem;
  font-weight: 600;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 1;
}

.table-header h3::before {
  content: '';
  display: inline-block;
  width: 3px;
  height: 12px;
  background: linear-gradient(var(--accent), var(--accent2));
  border-radius: 2px;
}

.excel-btn {
  border: 1px solid #a5b4fc;
  background: #eef2ff;
  color: #3730a3;
  border-radius: 6px;
  padding: 5px 12px;
  font-family: inherit;
  font-size: 0.74rem;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}

.excel-btn:hover {
  background: #e0e7ff;
  border-color: #818cf8;
}

.table-container {
  width: 100%;
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.73rem;
  table-layout: auto;
}

thead th {
  background: var(--bg3);
  padding: 7px 5px;
  text-align: center;
  font-size: 0.67rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.02em;
  color: var(--muted);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

tbody tr {
  border-bottom: 1px solid #edf1f6;
  transition: background 0.15s;
}

tbody tr:hover {
  background: var(--glass2);
}

tbody td {
  padding: 7px 5px;
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.72rem;
  white-space: nowrap;
  vertical-align: middle;
  text-align: center;
}

/* 標的代碼與名稱靠左對齊 */
tbody td:nth-child(5) {
  text-align: left;
  padding-left: 8px;
}

/* 買進與賣出日期適度微調 */
tbody td:nth-child(3),
tbody td:nth-child(4) {
  font-size: 0.70rem;
}

.td-cash {
  color: var(--muted);
  font-style: italic;
}

.tag {
  display: inline-flex;
  align-items: center;
  padding: 2px 5px;
  border-radius: 4px;
  font-size: 0.67rem;
  font-weight: 600;
}

.tag-win { background: #ecfdf5; color: var(--green); }
.tag-lose { background: #fef2f2; color: var(--red); }
.tag-cash { background: #f1f5f9; color: var(--muted); }
.tag-open { background: #eff6ff; color: #1d4ed8; }
.tag-pending { background: #fff7ed; color: #c2410c; }

tbody tr.pending-row {
  background: #fffaf5;
  border-bottom: 1px dashed #fdba74;
}

tbody tr.pending-row:hover {
  background: #fff4e6;
}

.pending-date {
  color: #c2410c;
  font-size: 0.70rem;
  font-weight: 700;
  white-space: nowrap;
}

.pending-action {
  display: inline-block;
  font-size: 9px;
  background: #ffedd5;
  color: #c2410c;
  padding: 0 3px;
  border-radius: 3px;
  font-weight: 600;
  vertical-align: middle;
  margin-left: 2px;
}

.action-tag {
  display: inline-block;
  font-size: 9px;
  background: #e0f2fe;
  color: #0369a1;
  padding: 0 3px;
  border-radius: 3px;
  font-weight: 600;
  vertical-align: middle;
  margin-left: 2px;
}

.pending-value {
  color: #9a3412;
  font-weight: 700;
}

.planned-sell {
  color: #475569;
  font-size: 0.65rem;
  font-weight: 600;
  line-height: 1.35;
}

.ret-pos { color: var(--green); }
.ret-neg { color: var(--red); }

/* ─ Responsive ───────────────────────────────────── */
@media (max-width: 1200px) {
  .stats-bar { grid-template-columns: repeat(5, 1fr); }
}

@media (max-width: 760px) {
  .stats-bar { grid-template-columns: repeat(2, minmax(0, 1fr)); padding: 10px; }
  .topbar { flex-direction: column; align-items: flex-start; }
  .preset-tabs { width: 100%; overflow-x: auto; }
}
