:root {
  --bg: #0a0e1a;
  --surface: #131827;
  --surface-2: #1a2033;
  --border: rgba(255, 255, 255, 0.06);
  --border-strong: rgba(255, 255, 255, 0.1);
  --primary: #3b82f6;
  --primary-soft: rgba(59, 130, 246, 0.15);
  --profit: #10b981;
  --profit-soft: rgba(16, 185, 129, 0.12);
  --loss: #ef4444;
  --loss-soft: rgba(239, 68, 68, 0.12);
  --text: #e5e7eb;
  --text-dim: #94a3b8;
  --muted: #64748b;
  --radius: 14px;
  --radius-sm: 10px;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 14px;
  line-height: 1.5;
  min-height: 100vh;
}

.mono { font-family: "JetBrains Mono", ui-monospace, monospace; font-variant-numeric: tabular-nums; }

.bg-gradient {
  position: fixed;
  inset: 0;
  background:
    radial-gradient(1200px 600px at 10% -10%, rgba(59, 130, 246, 0.12), transparent 60%),
    radial-gradient(900px 500px at 95% 10%, rgba(16, 185, 129, 0.08), transparent 60%);
  pointer-events: none;
  z-index: 0;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 28px;
  background: rgba(10, 14, 26, 0.75);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}

.brand { display: flex; align-items: center; gap: 12px; color: var(--primary); }
.brand h1 { margin: 0; font-size: 18px; font-weight: 600; color: var(--text); letter-spacing: -0.01em; }

.actions { display: flex; align-items: center; gap: 14px; }
.last-updated { font-size: 12px; color: var(--text-dim); font-family: "JetBrains Mono", monospace; }

.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 9px 16px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--surface-2);
  color: var(--text);
  font-family: inherit;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 160ms ease;
}
.btn:hover { transform: translateY(-1px); border-color: rgba(255,255,255,0.18); }
.btn:active { transform: translateY(0); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }

.btn-primary {
  background: linear-gradient(180deg, #3b82f6, #2563eb);
  border-color: #2563eb;
  box-shadow: 0 4px 14px rgba(59, 130, 246, 0.3);
}
.btn-primary:hover { box-shadow: 0 6px 20px rgba(59, 130, 246, 0.45); }

.btn-ghost { background: transparent; }
.btn-ghost:hover { background: var(--surface-2); }

.btn.loading svg { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.container {
  position: relative;
  z-index: 1;
  max-width: none;
  margin: 0;
  padding: 20px 32px 60px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
  transition: border-color 160ms ease, transform 160ms ease;
}

.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
  flex-wrap: wrap;
  gap: 10px;
}
.card-head h2 { margin: 0; font-size: 14px; font-weight: 600; color: var(--text-dim); letter-spacing: 0.02em; text-transform: uppercase; }

.legend-dots { display: flex; gap: 14px; font-size: 12px; color: var(--text-dim); }
.legend-dots span { display: inline-flex; align-items: center; gap: 6px; }
.legend-dots i { display: inline-block; width: 8px; height: 8px; border-radius: 2px; }

/* Summary */
.summary-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 16px;
}
.stat { display: flex; flex-direction: column; gap: 6px; }
.stat:hover { transform: translateY(-2px); border-color: var(--border-strong); }
.stat-label { font-size: 12px; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.04em; }
.stat-value { font-size: 26px; font-weight: 600; letter-spacing: -0.02em; }
.stat-sub { font-size: 12px; color: var(--muted); }

.profit { color: var(--profit); }
.loss { color: var(--loss); }

/* Charts */
.charts-grid {
  display: grid;
  grid-template-columns: 2fr 1fr 1fr;
  gap: 16px;
}
.chart-card { min-height: 320px; display: flex; flex-direction: column; }
.chart-card canvas { flex: 1; max-height: 280px; }
.chart-card.small { min-height: 320px; }

/* Table */
.table-card { padding: 20px 0; }
.table-card .card-head { padding: 0 20px; margin-bottom: 12px; }

.tools { display: flex; gap: 8px; align-items: center; }
.tools input,
.tools select {
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  color: var(--text);
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 13px;
  width: 180px;
  outline: none;
  transition: border-color 160ms;
}
.tools select { width: auto; min-width: 120px; cursor: pointer; }
.tools input:focus,
.tools select:focus { border-color: var(--primary); }

.table-wrap { overflow-x: auto; }

table { width: 100%; border-collapse: collapse; font-size: 13px; }
thead th {
  position: sticky;
  top: 0;
  background: var(--surface);
  padding: 12px 20px;
  text-align: left;
  font-weight: 500;
  color: var(--text-dim);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  cursor: pointer;
  user-select: none;
  border-bottom: 1px solid var(--border);
  transition: color 160ms;
}
thead th:hover { color: var(--text); }
thead th.num { text-align: right; }
thead th.sorted::after { content: " ↓"; color: var(--primary); }
thead th.sorted.asc::after { content: " ↑"; }

tbody td {
  padding: 11px 20px;
  border-bottom: 1px solid rgba(255,255,255,0.03);
  font-family: "JetBrains Mono", monospace;
  font-variant-numeric: tabular-nums;
}
tbody td.sym { font-family: "Inter", sans-serif; font-weight: 600; letter-spacing: 0.02em; }
tbody td.num { text-align: right; }
tbody tr:hover { background: rgba(255,255,255,0.02); }
.actions-col { width: 1%; white-space: nowrap; cursor: default; }
.actions-col:hover { color: var(--text-dim); }
.action-cell { white-space: nowrap; }
.btn-small { padding: 6px 10px; font-size: 12px; }
.btn-sell { color: #fca5a5; border-color: rgba(239, 68, 68, 0.35); background: var(--loss-soft); }
.btn-sell:hover { border-color: var(--loss); background: rgba(239, 68, 68, 0.2); }
.btn-danger { background: linear-gradient(180deg, #ef4444, #dc2626); border-color: #dc2626; box-shadow: 0 4px 14px rgba(239, 68, 68, 0.22); }
.empty-cell { padding: 32px 20px; text-align: center; color: var(--muted); }
.section-note { margin-top: 5px; color: var(--muted); font-size: 12px; }
.transaction-summary { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.transaction-pill {
  display: inline-block;
  min-width: 58px;
  padding: 3px 9px;
  border-radius: 7px;
  text-align: center;
  font-family: "Inter", sans-serif;
  font-size: 12px;
  font-weight: 600;
}
.transaction-pill.buy { color: #93c5fd; background: var(--primary-soft); }
.transaction-pill.sell { color: #fca5a5; background: var(--loss-soft); }
.transaction-pill.deposit { color: #6ee7b7; background: var(--profit-soft); }

/* Trade dialogs */
.trade-dialog {
  width: min(460px, calc(100vw - 32px));
  padding: 0;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  box-shadow: 0 20px 64px rgba(0, 0, 0, 0.58);
}
.trade-dialog::backdrop { background: rgba(4, 7, 15, 0.72); backdrop-filter: blur(3px); }
.trade-form { padding: 22px; display: flex; flex-direction: column; gap: 15px; }
.trade-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.trade-head h2 { margin: 0; font-size: 18px; }
.icon-close { border: 0; background: transparent; color: var(--text-dim); font-size: 25px; line-height: 1; cursor: pointer; padding: 0 4px; }
.icon-close:hover { color: var(--text); }
.trade-note { margin: -5px 0 0; color: var(--text-dim); font-size: 13px; }
.trade-form label { display: flex; flex-direction: column; gap: 6px; color: var(--text-dim); font-size: 12px; }
.trade-form input {
  width: 100%; padding: 10px 11px; border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong); background: var(--surface-2); color: var(--text);
  outline: none; font: 500 14px "JetBrains Mono", monospace;
}
.trade-form input:focus { border-color: var(--primary); }
.trade-fields { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.trade-estimate { display: flex; justify-content: space-between; gap: 12px; padding: 11px 12px; border-radius: var(--radius-sm); background: rgba(255,255,255,0.03); color: var(--text-dim); font-size: 13px; }
.trade-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 3px; }

.pill {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 500;
}
.pill.profit { background: var(--profit-soft); color: var(--profit); }
.pill.loss { background: var(--loss-soft); color: var(--loss); }

/* Toast */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  padding: 12px 20px;
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transition: opacity 200ms ease, transform 200ms ease;
  font-size: 13px;
  z-index: 100;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.success { border-color: var(--profit); }
.toast.error { border-color: var(--loss); }

/* Responsive */
@media (max-width: 1100px) {
  .summary-grid { grid-template-columns: repeat(3, 1fr); }
  .charts-grid { grid-template-columns: 1fr 1fr; }
  .chart-card:first-child { grid-column: span 2; }
}
@media (max-width: 768px) {
  .topbar { padding: 14px 18px; }
  .container { padding: 16px 18px 40px; }
  .summary-grid { grid-template-columns: repeat(2, 1fr); }
  .charts-grid { grid-template-columns: 1fr; }
  .chart-card:first-child { grid-column: auto; }
  .stat-value { font-size: 22px; }
  .brand h1 { font-size: 15px; }
  .actions .btn span { display: none; }
  .tools input { width: 120px; }
  .transaction-summary { grid-template-columns: repeat(2, 1fr); }
  .actions { gap: 8px; }
  .actions .btn { padding: 8px 10px; }
  .trade-fields { grid-template-columns: 1fr; }
  .trade-estimate { flex-direction: column; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}

/* Dividend block */
.dividend-block {
  display: flex;
  flex-direction: column;
  gap: 16px;
  margin-top: 8px;
}

.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px;
  padding: 8px 4px;
}

.section-title {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: var(--text);
  display: inline-flex;
  align-items: center;
  gap: 10px;
}
.section-title svg { color: var(--profit); }

.div-controls {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 13px;
  color: var(--text-dim);
}
.div-controls input[type="date"] {
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  color: var(--text);
  padding: 7px 10px;
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 13px;
  outline: none;
  color-scheme: dark;
}
.div-controls input[type="date"]:focus { border-color: var(--primary); }

/* Year tabs */
.year-tabs {
  display: inline-flex;
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  padding: 3px;
  gap: 2px;
}
.year-tab {
  padding: 6px 14px;
  border-radius: 7px;
  font-family: "JetBrains Mono", monospace;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-dim);
  cursor: pointer;
  border: none;
  background: transparent;
  transition: all 150ms ease;
}
.year-tab:hover { color: var(--text); }
.year-tab.active {
  background: var(--primary);
  color: #fff;
  box-shadow: 0 2px 8px rgba(59, 130, 246, 0.35);
}

/* Monthly price block */
.price-block {
  display: flex;
  flex-direction: column;
  gap: 16px;
  margin-top: 20px;
}

.price-table-wrap {
  max-height: 520px;
  overflow: auto;
}

#monthlyPriceTable { min-width: 600px; }
#monthlyPriceTable thead th {
  position: sticky;
  top: 0;
  background: var(--surface);
  z-index: 2;
}
#monthlyPriceTable thead th:first-child {
  left: 0;
  z-index: 3;
  background: var(--surface);
}
#monthlyPriceTable tbody td:first-child {
  position: sticky;
  left: 0;
  background: var(--surface);
  z-index: 1;
  border-right: 1px solid var(--border);
}
#monthlyPriceTable tbody tr:hover td:first-child { background: #181d2e; }

.cell-stack { display: inline-flex; flex-direction: column; align-items: flex-end; gap: 1px; line-height: 1.15; }
.cell-stack small { font-size: 10.5px; font-weight: 500; }
.na { color: var(--muted); }

/* ===== App shell with sidebar ===== */
.app-shell {
  display: grid;
  grid-template-columns: 240px 1fr;
  min-height: 100vh;
  position: relative;
  z-index: 1;
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  background: rgba(13, 17, 28, 0.85);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-right: 1px solid var(--border);
  padding: 20px 14px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 8px 14px;
  border-bottom: 1px solid var(--border);
  color: var(--primary);
}
.sidebar-brand span {
  color: var(--text);
  font-size: 15px;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.side-nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-dim);
  font-family: inherit;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 160ms ease;
  text-align: left;
}
.nav-item:hover { background: rgba(255,255,255,0.03); color: var(--text); }
.nav-item.active {
  background: var(--primary-soft);
  color: var(--primary);
  border-color: rgba(59,130,246,0.3);
}
.nav-item.active svg { color: var(--primary); }

.sidebar-foot {
  padding-top: 12px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.sidebar-stat {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  color: var(--text-dim);
}
.sidebar-stat b { color: var(--text); font-size: 13px; font-weight: 600; }

/* Main area */
.main-area { min-width: 0; display: flex; flex-direction: column; }
.page-title { font-size: 18px; font-weight: 600; color: var(--text); letter-spacing: -0.01em; }

/* View switching */
.view { display: none; flex-direction: column; gap: 20px; }
.view.active { display: flex; }

/* Hide original full-width brand in topbar (not used anymore) */
.topbar .brand { display: none; }

/* Tighter top padding since sidebar */
.container { padding-top: 20px; }

/* Widescreen: let charts breathe; main fills all available width */
@media (min-width: 1600px) {
  .summary-grid { grid-template-columns: repeat(5, 1fr); gap: 20px; }
  .charts-grid { grid-template-columns: 2fr 1fr 1fr; gap: 20px; }
  .stat-value { font-size: 30px; }
  .chart-card { min-height: 360px; }
  .chart-card canvas { max-height: 320px; }
}

/* Mobile */
@media (max-width: 860px) {
  .app-shell { grid-template-columns: 1fr; }
  .sidebar {
    position: static;
    height: auto;
    flex-direction: row;
    overflow-x: auto;
    padding: 10px 14px;
    gap: 10px;
    border-right: none;
    border-bottom: 1px solid var(--border);
  }
  .sidebar-brand { border: none; padding: 0 10px 0 4px; flex-shrink: 0; }
  .sidebar-brand span { display: none; }
  .side-nav { flex-direction: row; flex: 1; gap: 6px; }
  .nav-item { flex-shrink: 0; padding: 8px 12px; }
  .nav-item span { display: inline; }
  .sidebar-foot { display: none; }
}

/* ===== Portfolio simulation ===== */
.sim-setup { display: flex; flex-direction: column; gap: 16px; padding: 20px 0; }
.sim-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 16px;
  padding: 0 20px;
}
.sim-controls label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 12px;
  color: var(--text-dim);
}
.sim-inline { display: flex; gap: 6px; }
.sim-controls input,
.sim-num {
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  color: var(--text);
  padding: 8px 11px;
  border-radius: var(--radius-sm);
  font: 500 13px "JetBrains Mono", monospace;
  outline: none;
  color-scheme: dark;
  transition: border-color 160ms;
}
.sim-controls input:focus,
.sim-num:focus { border-color: var(--primary); }
#simDefaultAmount { width: 130px; }
#simSymbol { width: 190px; font-family: "Inter", sans-serif; text-transform: uppercase; }
.sim-add { margin: 0; }
.sim-actions { display: flex; gap: 8px; margin-left: auto; }
.sim-num { width: 140px; padding: 6px 10px; text-align: right; }
.sim-num.profit { color: var(--profit); }
.sim-num.loss { color: var(--loss); }
.sim-input-table thead th { cursor: default; }
.sim-input-table tbody td { padding-top: 7px; padding-bottom: 7px; }
.sim-input-table tfoot td {
  padding: 12px 20px;
  color: var(--text-dim);
  font-size: 12px;
  border-top: 1px solid var(--border);
}
.sim-input-table tfoot td.num { text-align: right; color: var(--text); font-size: 13px; font-weight: 600; }
.sim-run { display: flex; justify-content: flex-end; padding: 0 20px; }
.sim-run .btn { padding: 10px 28px; }
#simResults { display: flex; flex-direction: column; gap: 20px; }
#simResults[hidden] { display: none; }
.sim-result-table td { white-space: nowrap; }

@media (max-width: 768px) {
  .sim-controls { padding: 0 16px; }
  .sim-actions { margin-left: 0; }
  #simSymbol { width: 150px; }
  .sim-num { width: 110px; }
}

/* Simulation: dividend tax */
.sim-tax .card-head { margin-bottom: 16px; }
.sim-tax-body { display: grid; grid-template-columns: minmax(240px, 320px) 1fr; gap: 24px; align-items: start; }
.sim-tax-inputs { display: flex; flex-direction: column; gap: 14px; }
.sim-tax-inputs[hidden] { display: none; }
.sim-tax-inputs label { display: flex; flex-direction: column; gap: 6px; font-size: 12px; color: var(--text-dim); }
.sim-tax-inputs select {
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  color: var(--text);
  padding: 8px 11px;
  border-radius: var(--radius-sm);
  font: 500 13px "Inter", sans-serif;
  outline: none;
  cursor: pointer;
}
.sim-tax-inputs select:focus { border-color: var(--primary); }
.sim-tax-inputs[hidden] + .sim-tax-table { grid-column: 1 / -1; }
.sim-tax-table td { padding: 9px 14px; font-size: 13px; white-space: nowrap; }
.sim-tax-table td:first-child { font-family: "Inter", sans-serif; color: var(--text-dim); }
.sim-tax-table tr.profit td:nth-child(2) { color: var(--profit); }
.sim-tax-table tr.loss td:nth-child(2) { color: var(--loss); }
.sim-tax-table tr.sim-tax-total td { border-top: 1px solid var(--border-strong); border-bottom: none; font-size: 15px; font-weight: 600; padding-top: 12px; }
.sim-tax-table tr.sim-tax-total td:first-child { color: var(--text); }
.sim-tax-table tbody tr:hover { background: transparent; }
#simTaxNote:not(:empty) { margin-top: 12px; }

@media (max-width: 1100px) {
  .sim-tax-body { grid-template-columns: 1fr; }
  .sim-tax-inputs { flex-direction: row; flex-wrap: wrap; }
}

/* Simulation: SET index comparison */
.sim-index-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 16px; }
.sim-index-cell { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.sim-index-cell .stat-label b { color: var(--text-dim); font-weight: 500; text-transform: none; margin-left: 4px; }
.sim-index-cell .stat-value { font-size: 22px; }
#simIndexBuy {
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  color: var(--text);
  padding: 6px 11px;
  border-radius: var(--radius-sm);
  font: 600 20px "JetBrains Mono", monospace;
  outline: none;
  width: 100%;
  max-width: 200px;
}
#simIndexBuy:focus { border-color: var(--primary); }
#simIndexBuy:placeholder-shown { border-color: rgba(245, 158, 11, 0.6); }
#simIndexBuy::placeholder { font-size: 14px; font-weight: 500; color: var(--muted); }
#simIndexHint { margin: 0; }

@media (max-width: 1100px) {
  .sim-index-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

/* ===== Standalone simulator site ===== */
.site { position: relative; z-index: 1; min-height: 100vh; display: flex; flex-direction: column; }
.site .container { width: 100%; max-width: 1400px; margin: 0 auto; flex: 1; }
.site-brand { border: none; padding: 0; }
.site-brand span { font-size: 17px; }
.site-foot { padding: 20px 32px 32px; text-align: center; color: var(--muted); font-size: 12px; }
@media (max-width: 860px) {
  .site-brand span { display: inline; font-size: 15px; }
  .site-foot { padding: 16px; }
}
