/* ============================================================
   portfolio-dash — design tokens & layout
   Dark primary. One token set in :root; a light variant can be
   produced later by overriding [data-theme="light"].
   Color semantics: Taiwan convention — red = gain, green = loss.
   ============================================================ */

:root {
  /* surfaces */
  --bg:        #0c1015;
  --panel:     #131922;
  --panel-2:   #18202b;
  --panel-3:   #1e2835;
  --border:    #232e3c;
  --border-soft: #1b2430;

  /* text */
  --text:      #e6ebf2;
  --text-2:    #9aa6b5;
  --text-3:    #5e6b7c;

  /* P&L semantics (紅漲綠跌) */
  --up:        #f0544f;
  --up-soft:   rgba(240, 84, 79, 0.12);
  --down:      #2fb47c;
  --down-soft: rgba(47, 180, 124, 0.12);

  /* status */
  --amber:       #d9a13f;
  --amber-soft:  rgba(217, 161, 63, 0.14);
  --ok:          #4fae6c;
  --ok-soft:     rgba(79, 174, 108, 0.14);

  /* data accents (non-P&L series) */
  --accent:      #58a6dd;   /* market value series, TWD */
  --accent-soft: rgba(88, 166, 221, 0.10);
  --series-usd:  #9b86d8;
  --series-myr:  #d9a13f;
  --series-gray: #8a96a6;   /* net invested line */

  /* typography */
  --font-ui:   "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
  --font-num:  "IBM Plex Mono", "SFMono-Regular", Consolas, monospace;

  /* scale */
  --radius:    6px;
  --radius-sm: 4px;
  --gap:       14px;
  --pad:       16px;
  --maxw:      1840px;
}

/* ---------- light theme ---------- */
:root[data-theme="light"] {
  --bg:        #eef1f5;
  --panel:     #ffffff;
  --panel-2:   #f3f6fa;
  --panel-3:   #e7ecf3;
  --border:    #d5dde7;
  --border-soft: #e4eaf2;

  --text:      #1a2330;
  --text-2:    #4e5d6e;
  --text-3:    #8694a6;

  --up:        #d8433d;
  --up-soft:   rgba(216, 67, 61, 0.10);
  --down:      #1f9866;
  --down-soft: rgba(31, 152, 102, 0.10);

  --amber:       #a87b1f;
  --amber-soft:  rgba(168, 123, 31, 0.12);
  --ok:          #2e8f57;
  --ok-soft:     rgba(46, 143, 87, 0.12);

  --accent:      #2b76b9;
  --accent-soft: rgba(43, 118, 185, 0.10);
  --series-usd:  #6f58be;
  --series-myr:  #a87b1f;
  --series-gray: #7d8a99;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html { scrollbar-gutter: stable; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  font-size: 13px;
  line-height: 1.5;
}

.num { font-family: var(--font-num); font-variant-numeric: tabular-nums; }

.sign-up   { color: var(--up); }
.sign-down { color: var(--down); }
.sign-flat { color: var(--text-2); }
.sign-nil  { color: var(--text-3); }

a { color: inherit; }

/* ---------- page scaffold ---------- */

.page {
  max-width: var(--maxw);
  margin: 0 auto;
  padding: 0 20px 48px;
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  width: 100%;
  min-width: 0;
  box-sizing: border-box;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
}

.panel-head {
  display: flex;
  align-items: baseline;
  gap: 12px;
  padding: 12px var(--pad) 0;
}

.panel-title {
  margin: 0;
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: var(--text);
}

.panel-sub {
  font-size: 11px;
  color: var(--text-3);
}

.panel-head .spacer { flex: 1; }

/* ---------- app shell: sidebar + topbar ---------- */

.shell { display: flex; min-height: 100vh; }

.sidebar {
  width: 196px;
  flex: none;
  position: sticky;
  top: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-right: 1px solid var(--border-soft);
  padding: 14px 10px 12px;
  gap: 2px;
  transition: width 0.15s ease;
  overflow: hidden;
}
.sidebar .brand {
  font-family: var(--font-num);
  font-size: 15px;
  font-weight: 600;
  padding: 0 8px 14px;
  white-space: nowrap;
}
.sidebar .brand .tld { color: var(--accent); }
.sb-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  font-size: 12.5px;
  color: var(--text-2);
  text-decoration: none;
  white-space: nowrap;
}
.sb-item:hover { background: var(--panel-2); color: var(--text); }
.sb-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 700; }
.sb-item .ico { width: 18px; text-align: center; font-size: 13px; flex: none; }
.sb-item .sb-badge {
  margin-left: auto;
  font-size: 9px;
  color: var(--text-3);
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 0 4px;
  line-height: 14px;
}
.sb-group { margin-top: 2px; }
.sb-children { display: flex; flex-direction: column; gap: 1px; margin: 1px 0 2px; }
.sb-children .sb-item { padding-left: 38px; font-size: 12px; }
.sb-spacer { flex: 1; }
.sb-collapse {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-3);
  border-radius: var(--radius-sm);
  font-size: 11px;
  padding: 5px;
  cursor: pointer;
  font-family: var(--font-num);
}
.sb-collapse:hover { color: var(--text); }

.sidebar.collapsed { width: 54px; padding: 14px 8px 12px; }
.sidebar.collapsed .brand { font-size: 13px; padding-left: 4px; }
.sidebar.collapsed .brand .full { display: none; }
.sidebar.collapsed .sb-item { padding: 8px 0; justify-content: center; }
.sidebar.collapsed .sb-item .label, .sidebar.collapsed .sb-item .sb-badge { display: none; }
.sidebar.collapsed .sb-children .sb-item { padding-left: 0; }

.shell-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 11px 20px;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(6px);
  border-bottom: 1px solid var(--border);
}
.topbar .page-title { margin: 0; font-size: 15px; font-weight: 700; }
.topbar .avatar {
  width: 26px; height: 26px;
  border-radius: 50%;
  background: var(--panel-3);
  border: 1px solid var(--border);
  color: var(--text-3);
  display: flex; align-items: center; justify-content: center;
  font-size: 11px;
  font-family: var(--font-num);
}

/* ---------- toasts ---------- */

.toast-host {
  position: fixed;
  right: 18px; bottom: 18px;
  z-index: 80;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: 360px;
}
.toast {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 10px 14px;
  border-radius: var(--radius);
  font-size: 12px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
  animation: toast-in 0.18s ease;
}
@keyframes toast-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
.toast-ok { border-color: rgba(79, 174, 108, 0.5); color: var(--ok); }
.toast-fail { border-color: rgba(240, 84, 79, 0.5); color: var(--up); }
.toast .msg { color: var(--text); }
.toast .sub { color: var(--text-2); font-size: 11px; font-family: var(--font-num); }
.toast .x {
  margin-left: auto; background: none; border: none;
  color: var(--text-3); cursor: pointer; font-size: 13px; padding: 0;
}

/* neutral direction chips (買賣是方向不是損益 — 不用紅綠) */
.dir-chip {
  font-size: 10px;
  padding: 0 8px;
  line-height: 17px;
  border-radius: 3px;
  font-family: var(--font-ui);
}
.dir-buy { color: var(--bg); background: var(--text-2); font-weight: 700; }
.dir-sell { color: var(--text-2); border: 1px solid var(--text-3); }

/* ---------- app nav (legacy top nav, kept for compat) ---------- */

.app-nav { display: flex; gap: 2px; margin-left: 14px; }
.app-nav a {
  padding: 4px 13px;
  border-radius: var(--radius-sm);
  font-size: 12px;
  color: var(--text-2);
  text-decoration: none;
}
.app-nav a:hover { color: var(--text); background: var(--panel-2); }
.app-nav a.active {
  color: var(--accent);
  background: var(--accent-soft);
  font-weight: 700;
}

/* ---------- modal (shared) ---------- */

.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(4, 7, 11, 0.6);
  backdrop-filter: blur(2px);
  display: flex; align-items: center; justify-content: center;
  z-index: 60;
}
.modal {
  width: 480px; max-width: calc(100vw - 40px);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 18px 60px rgba(0, 0, 0, 0.5);
}
.modal-head {
  display: flex; align-items: center;
  padding: 13px var(--pad);
  border-bottom: 1px solid var(--border-soft);
}
.modal-title { margin: 0; font-size: 13px; font-weight: 700; }
.modal-close {
  margin-left: auto; background: none; border: none;
  color: var(--text-3); font-size: 15px; cursor: pointer;
}
.modal-close:hover { color: var(--text); }
.modal-body { padding: 14px var(--pad); display: flex; flex-direction: column; gap: 12px; }
.modal-foot {
  display: flex; justify-content: flex-end; gap: 8px;
  padding: 12px var(--pad);
  border-top: 1px solid var(--border-soft);
}

/* ---------- segmented control (shared) ---------- */

.segmented {
  display: inline-flex;
  gap: 2px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 2px;
}
.segmented button {
  border: none; background: transparent;
  color: var(--text-2);
  font-family: var(--font-ui); font-size: 12px;
  padding: 5px 16px;
  border-radius: 3px;
  cursor: pointer;
  white-space: nowrap;
}
.segmented button:hover { color: var(--text); }
.segmented button.active {
  background: var(--panel-3);
  color: var(--accent);
  font-weight: 700;
}

/* ---------- header bar ---------- */

.app-header {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 0 12px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 2px;
}

.app-name {
  margin: 0;
  font-family: var(--font-num);
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 0.02em;
}
.app-name .tld { color: var(--accent); }

.asof {
  display: flex;
  align-items: baseline;
  gap: 6px;
  font-size: 12px;
  color: var(--text-2);
}
.asof .label { color: var(--text-3); }

.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 1px 7px;
  border-radius: var(--radius-sm);
  font-size: 11px;
  line-height: 18px;
  white-space: nowrap;
  border: 1px solid var(--border);
  color: var(--text-2);
  background: var(--panel-2);
}
.badge-ccy {
  font-family: var(--font-num);
  color: var(--accent);
  border-color: rgba(88, 166, 221, 0.35);
  background: var(--accent-soft);
}
.badge-fresh-ok {
  color: var(--ok);
  border-color: rgba(79, 174, 108, 0.4);
  background: var(--ok-soft);
}
.badge-fresh-stale {
  color: var(--amber);
  border-color: rgba(217, 161, 63, 0.4);
  background: var(--amber-soft);
  text-decoration: none;
  cursor: pointer;
}
.badge .dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
}
.badge-stale-mini {
  color: var(--amber);
  border-color: rgba(217, 161, 63, 0.4);
  background: var(--amber-soft);
  font-size: 10px;
  line-height: 15px;
  padding: 0 5px;
}
.badge-missing {
  color: var(--up);
  border-color: rgba(240, 84, 79, 0.4);
  background: var(--up-soft);
  font-size: 10px;
  line-height: 15px;
  padding: 0 5px;
}
.badge-ai {
  color: var(--series-usd);
  border-color: rgba(155, 134, 216, 0.4);
  background: rgba(155, 134, 216, 0.12);
  font-family: var(--font-num);
  font-size: 10px;
  letter-spacing: 0.08em;
}

.header-spacer { flex: 1; }

.btn-refresh {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 5px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text-2);
  font-family: var(--font-ui);
  font-size: 12px;
  cursor: pointer;
}
.btn-refresh:hover { border-color: var(--text-3); color: var(--text); }

/* ---------- KPI band ---------- */

.kpi-band {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: var(--gap);
}

.kpi-card {
  background: var(--panel);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  padding: 12px 14px 11px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}
.kpi-card.kpi-up   { box-shadow: inset 3px 0 0 var(--up); }
.kpi-card.kpi-down { box-shadow: inset 3px 0 0 var(--down); }

.kpi-label {
  font-size: 11px;
  color: var(--text-2);
  white-space: nowrap;
  display: flex;
  align-items: center;
  gap: 6px;
}
.kpi-value {
  font-family: var(--font-num);
  font-variant-numeric: tabular-nums;
  font-size: 19px;
  font-weight: 600;
  letter-spacing: -0.01em;
  white-space: nowrap;
}
.kpi-unit {
  font-size: 10px;
  color: var(--text-3);
  font-family: var(--font-num);
}

/* ---------- trend chart ---------- */

.trend-panel .panel-head { padding-bottom: 4px; }

.range-group {
  display: flex;
  gap: 4px;
}
.range-btn {
  padding: 2px 10px;
  font-size: 11px;
  font-family: var(--font-num);
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-2);
  cursor: pointer;
}
.range-btn:hover { color: var(--text); }
.range-btn.active {
  background: var(--accent-soft);
  border-color: rgba(88, 166, 221, 0.4);
  color: var(--accent);
}

.trend-note {
  font-size: 11px;
  color: var(--amber);
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.trend-note .dot { width: 6px; height: 6px; border-radius: 50%; background: var(--amber); }

#trend-chart { width: 100%; height: 360px; }

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 48px 24px;
  color: var(--text-3);
  text-align: center;
}
.empty-state .glyph {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 1px dashed var(--text-3);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-num);
  font-size: 15px;
}
.empty-state .msg { font-size: 12px; }

/* ---------- holdings table ---------- */

.filter-bar {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  padding: 10px var(--pad) 0;
}
.filter-bar .group-label {
  font-size: 11px;
  color: var(--text-3);
  margin-right: 2px;
}
.filter-bar .divider {
  width: 1px;
  height: 16px;
  background: var(--border);
  margin: 0 8px;
}
.chip {
  padding: 2px 10px;
  font-size: 11px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-2);
  cursor: pointer;
  font-family: var(--font-ui);
}
.chip:hover { color: var(--text); }
.chip.active {
  background: var(--accent-soft);
  border-color: rgba(88, 166, 221, 0.45);
  color: var(--accent);
}

.table-wrap {
  overflow-x: auto;
  padding: 8px 0 0;
}

table.data {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
}
table.data th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--panel);
  text-align: right;
  font-weight: 500;
  font-size: 11px;
  color: var(--text-3);
  padding: 6px 10px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  user-select: none;
}
table.data th.sortable { cursor: pointer; }
table.data th.sortable:hover { color: var(--text-2); }
table.data th .arrow { font-size: 9px; margin-left: 2px; color: var(--accent); }
table.data th.col-text, table.data td.col-text { text-align: left; }

table.data td {
  padding: 5px 10px;
  border-bottom: 1px solid var(--border-soft);
  text-align: right;
  white-space: nowrap;
  font-family: var(--font-num);
  font-variant-numeric: tabular-nums;
  height: 32px;
}
table.data td.col-text { font-family: var(--font-ui); }
table.data tbody tr:nth-child(even) { background: rgba(255, 255, 255, 0.015); }
table.data tbody tr:hover { background: var(--panel-2); }

table.data tfoot td {
  border-top: 1px solid var(--border);
  border-bottom: none;
  background: var(--panel-2);
  font-weight: 600;
  height: 36px;
}

.sym-cell { display: flex; align-items: center; gap: 8px; }
.sym-code { font-family: var(--font-num); font-weight: 600; color: var(--text); }
.sym-name { color: var(--text-2); font-size: 11px; }
.board-badge {
  font-family: var(--font-num);
  font-size: 9px;
  line-height: 14px;
  padding: 0 4px;
  border-radius: 3px;
  border: 1px solid var(--border);
  color: var(--text-3);
}

.subpct { font-size: 10px; display: block; line-height: 1.1; opacity: 0.85; }

.mini-bar {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  justify-content: flex-end;
}
.mini-bar .track {
  width: 56px;
  height: 5px;
  border-radius: 3px;
  background: var(--panel-3);
  overflow: hidden;
}
.mini-bar .fill {
  display: block;
  height: 100%;
  border-radius: 3px;
  background: var(--accent);
}
.mini-bar .fill.payback { background: var(--series-myr); }

/* ---------- trend + insights row (每日決策層) ---------- */

.row-trend {
  display: grid;
  grid-template-columns: minmax(0, 2.4fr) minmax(300px, 1fr);
  gap: var(--gap);
  align-items: stretch;
}

.insights-panel { display: flex; flex-direction: column; }

.insight-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 12px var(--pad) 14px;
  flex: 1;
}

/* ---------- collapsible low-frequency panels ---------- */

details.collapsible > summary {
  list-style: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 11px var(--pad);
}
details.collapsible > summary::-webkit-details-marker { display: none; }
details.collapsible > summary .caret {
  font-size: 10px;
  color: var(--text-3);
  transition: transform 0.15s ease;
}
details.collapsible[open] > summary .caret { transform: rotate(90deg); }
details.collapsible > summary:hover .panel-title { color: var(--accent); }
details.collapsible > summary .panel-title { font-size: 13px; font-weight: 700; }

.sum-chips {
  display: flex;
  gap: 8px;
  margin-left: auto;
  align-items: center;
  font-size: 11px;
  color: var(--text-2);
  flex-wrap: wrap;
}
.sum-chips .v { font-family: var(--font-num); font-weight: 600; }
.collapsible-note { padding: 0 var(--pad) 4px; }
.row-2.align-start { align-items: start; }

/* ---------- two-up rows ---------- */

.row-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--gap);
}

.chart-box { width: 100%; height: 270px; }

/* currency composition */
.ccy-headline {
  padding: 10px var(--pad) 0;
  display: flex;
  align-items: baseline;
  gap: 8px;
}
.ccy-headline .num { font-size: 24px; font-weight: 600; }
.ccy-headline .cap { font-size: 11px; color: var(--text-3); }

.ccy-stack {
  display: flex;
  height: 14px;
  border-radius: 4px;
  overflow: hidden;
  margin: 12px var(--pad) 4px;
  background: var(--panel-3);
}
.ccy-stack .seg { height: 100%; }

.ccy-rows {
  display: flex;
  flex-direction: column;
  padding: 6px var(--pad) 14px;
}
.ccy-row {
  display: grid;
  grid-template-columns: 64px 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 7px 0;
  border-bottom: 1px solid var(--border-soft);
}
.ccy-row:last-child { border-bottom: none; }
.ccy-key { display: flex; align-items: center; gap: 7px; }
.ccy-swatch { width: 8px; height: 8px; border-radius: 2px; }
.ccy-code { font-family: var(--font-num); font-weight: 600; font-size: 12px; }
.ccy-share { font-size: 11px; color: var(--text-3); font-family: var(--font-num); }
.ccy-amt { font-family: var(--font-num); font-size: 14px; font-weight: 500; }
.ccy-note {
  padding: 0 var(--pad) 12px;
  font-size: 10px;
  color: var(--text-3);
}

/* ---------- FX panel ---------- */

.fx-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--gap);
  padding: 12px var(--pad);
}
.fx-card {
  background: var(--panel-2);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  padding: 12px 14px;
}
.fx-card-head {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}
.fx-account { font-weight: 700; font-size: 13px; }
.fx-pair {
  font-family: var(--font-num);
  font-size: 11px;
  color: var(--text-2);
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 0 6px;
  line-height: 18px;
}
.fx-rates {
  display: flex;
  align-items: baseline;
  gap: 10px;
  margin-bottom: 10px;
}
.fx-rate-block { display: flex; flex-direction: column; }
.fx-rate-label { font-size: 10px; color: var(--text-3); }
.fx-rate-val { font-family: var(--font-num); font-size: 17px; font-weight: 600; }
.fx-arrow { color: var(--text-3); font-size: 13px; }
.fx-delta { font-family: var(--font-num); font-size: 12px; font-weight: 600; }

.fx-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px 16px;
  font-size: 11px;
}
.fx-stat { display: flex; justify-content: space-between; gap: 8px; padding: 3px 0; border-bottom: 1px dashed var(--border-soft); }
.fx-stat:nth-last-child(-n+2) { border-bottom: none; }
.fx-stat .k { color: var(--text-3); }
.fx-stat .v { font-family: var(--font-num); }

.fx-footer {
  display: flex;
  gap: 24px;
  padding: 0 var(--pad) 14px;
  font-size: 12px;
  color: var(--text-2);
}
.fx-footer .v { font-family: var(--font-num); font-weight: 600; }

/* ---------- realized + dividends ---------- */

.realized-footer {
  display: flex;
  gap: 10px;
  align-items: center;
  padding: 10px var(--pad) 14px;
  font-size: 11px;
  color: var(--text-3);
}
.ccy-chip {
  font-family: var(--font-num);
  font-size: 11px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 2px 8px;
  color: var(--text-2);
}
.ccy-chip b { color: var(--text); font-weight: 600; }

.div-chips {
  display: flex;
  gap: 8px;
  padding: 8px var(--pad) 0;
  flex-wrap: wrap;
}

/* ex-dividend calendar */
.exdiv-list {
  display: flex;
  flex-direction: column;
  padding: 10px var(--pad) 14px;
  gap: 8px;
}
.exdiv-item {
  display: grid;
  grid-template-columns: 56px 1fr auto;
  align-items: center;
  gap: 14px;
  background: var(--panel-2);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  padding: 9px 12px;
}
.exdiv-date {
  display: flex;
  flex-direction: column;
  align-items: center;
  font-family: var(--font-num);
  border-right: 1px solid var(--border);
  padding-right: 12px;
}
.exdiv-date .mm { font-size: 10px; color: var(--text-3); letter-spacing: 0.06em; }
.exdiv-date .dd { font-size: 20px; font-weight: 600; line-height: 1.15; }
.exdiv-main { display: flex; flex-direction: column; gap: 1px; min-width: 0; }
.exdiv-sym { display: flex; align-items: baseline; gap: 8px; }
.exdiv-pay { font-size: 10px; color: var(--text-3); }
.exdiv-amt { text-align: right; font-family: var(--font-num); }
.exdiv-amt .a { font-size: 15px; font-weight: 600; }
.exdiv-amt .c { font-size: 10px; color: var(--text-3); margin-left: 4px; }

/* ---------- AI insights ---------- */

.insight-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--gap);
  padding: 12px var(--pad) 14px;
}
.insight-card {
  background: var(--panel-2);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.insight-head { display: flex; align-items: center; gap: 8px; }
.insight-title { margin: 0; font-size: 13px; font-weight: 700; }
.insight-body { margin: 0; font-size: 12px; color: var(--text-2); line-height: 1.65; }
.insight-foot { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.insight-time { font-size: 10px; color: var(--text-3); font-family: var(--font-num); }
.insight-cost { font-size: 10px; color: var(--text-3); }

/* ---------- freshness footer ---------- */

details.freshness { border-radius: var(--radius); }
details.freshness > summary {
  list-style: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px var(--pad);
  font-size: 12px;
  font-weight: 700;
  color: var(--text-2);
}
details.freshness > summary::-webkit-details-marker { display: none; }
details.freshness > summary .caret {
  font-size: 10px;
  color: var(--text-3);
  transition: transform 0.15s ease;
}
details.freshness[open] > summary .caret { transform: rotate(90deg); }
details.freshness > summary .chips { display: flex; gap: 6px; margin-left: auto; }

.fresh-body {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--gap);
  padding: 0 var(--pad) 14px;
}
.fresh-col h4 {
  margin: 0 0 6px;
  font-size: 11px;
  font-weight: 500;
  color: var(--text-3);
}
table.fresh {
  width: 100%;
  border-collapse: collapse;
  font-size: 11px;
}
table.fresh td {
  padding: 4px 8px;
  border-bottom: 1px solid var(--border-soft);
  font-family: var(--font-num);
}
table.fresh td:last-child { text-align: right; }
.fresh-note {
  grid-column: 1 / -1;
  font-size: 11px;
  color: var(--amber);
  background: var(--amber-soft);
  border: 1px solid rgba(217, 161, 63, 0.3);
  border-radius: var(--radius-sm);
  padding: 6px 10px;
}

/* ---------- KPI band v2: 3 hero + 2 combo (5 visual units) ---------- */

.kpi-band.v2 { grid-template-columns: 1.15fr 1.3fr 1.05fr 1.25fr 1.25fr; }

.kpi-card.kpi-hero .kpi-value { font-size: 24px; }
.kpi-card .kpi-subline {
  font-size: 11px;
  color: var(--text-3);
  font-family: var(--font-num);
  display: flex;
  gap: 6px;
  align-items: baseline;
  white-space: nowrap;
}
.kpi-combo { gap: 6px; }
.kpi-combo .combo-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
}
.kpi-combo .combo-row .k { font-size: 11px; color: var(--text-2); white-space: nowrap; }
.kpi-combo .combo-row .v {
  font-family: var(--font-num);
  font-variant-numeric: tabular-nums;
  font-size: 14.5px;
  font-weight: 600;
  white-space: nowrap;
}

/* ---------- 各幣別報酬拆分 ---------- */

.ccyret-table-wrap { padding: 4px var(--pad) 12px; }
table.ccyret { width: 100%; border-collapse: collapse; font-size: 11.5px; }
table.ccyret th {
  text-align: right; font-weight: 500; font-size: 10.5px; color: var(--text-3);
  padding: 4px 8px; border-bottom: 1px solid var(--border); white-space: nowrap;
}
table.ccyret th:first-child { text-align: left; }
table.ccyret td {
  text-align: right; padding: 5px 8px; border-bottom: 1px solid var(--border-soft);
  font-family: var(--font-num); font-variant-numeric: tabular-nums; white-space: nowrap;
}
table.ccyret td:first-child { text-align: left; font-weight: 600; }
table.ccyret tr:last-child td { border-bottom: none; }
.ccyret-note { padding: 0 var(--pad) 10px; font-size: 10px; color: var(--text-3); }

/* ---------- export button ---------- */

.btn-export {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  margin-left: auto;
  padding: 2px 10px;
  font-size: 11px;
  font-family: var(--font-ui);
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-2);
  cursor: pointer;
  white-space: nowrap;
}
.btn-export:hover { color: var(--text); border-color: var(--text-3); }
.panel-head .btn-export { align-self: center; }

/* ---------- stale row desaturation ---------- */

table.data tbody tr.row-stale td { opacity: 0.55; }
table.data tbody tr.row-stale td .badge { opacity: 1; }
table.data tbody tr.row-stale:hover td { opacity: 0.8; }

/* clickable symbol */
.sym-link { cursor: pointer; }
.sym-link .sym-code { text-decoration: underline dotted transparent; text-underline-offset: 3px; transition: text-decoration-color 0.12s; }
.sym-link:hover .sym-code { color: var(--accent); text-decoration-color: var(--accent); }

/* ---------- topbar: search ---------- */

.search-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 4px 11px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text-3);
  font-family: var(--font-ui);
  font-size: 12px;
  cursor: pointer;
}
.search-btn:hover { color: var(--text); border-color: var(--text-3); }
.search-btn kbd, .search-input-row kbd {
  font-family: var(--font-num);
  font-size: 9px;
  color: var(--text-3);
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 0 4px;
  line-height: 14px;
}

.search-backdrop {
  position: fixed; inset: 0; z-index: 75;
  background: rgba(4, 7, 11, 0.6);
  backdrop-filter: blur(2px);
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding-top: 14vh;
}
.search-box {
  width: 520px; max-width: calc(100vw - 40px);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 18px 60px rgba(0, 0, 0, 0.55);
  overflow: hidden;
}
.search-input-row {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border-soft);
}
.search-input-row .glyph { color: var(--text-3); font-size: 14px; }
.search-input {
  flex: 1; background: none; border: none; outline: none;
  color: var(--text); font-family: var(--font-ui); font-size: 14px;
}
.search-list { max-height: 320px; overflow-y: auto; padding: 6px; display: flex; flex-direction: column; }
.search-item {
  display: flex; align-items: baseline; gap: 10px;
  padding: 8px 10px;
  border: none; background: none; text-align: left;
  border-radius: var(--radius-sm);
  cursor: pointer; color: var(--text);
  font-size: 12px;
}
.search-item.active { background: var(--accent-soft); }
.search-item .search-mkt { margin-left: auto; font-size: 10.5px; color: var(--text-3); }
.search-empty { padding: 18px; text-align: center; font-size: 12px; color: var(--text-3); }

/* ---------- topbar: refresh dropdown ---------- */

.refresh-wrap { position: relative; }
.refresh-menu {
  position: absolute; right: 0; top: calc(100% + 6px);
  width: 280px; z-index: 65;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 14px 44px rgba(0, 0, 0, 0.5);
  padding: 5px;
  display: flex; flex-direction: column; gap: 2px;
}
.refresh-opt {
  display: flex; flex-direction: column; gap: 1px;
  padding: 8px 10px; border: none; background: none;
  border-radius: var(--radius-sm); cursor: pointer; text-align: left;
}
.refresh-opt:hover { background: var(--panel-2); }
.refresh-opt .l { font-size: 12px; color: var(--text); font-weight: 600; }
.refresh-opt .s { font-size: 10.5px; color: var(--text-3); }

/* ---------- topbar: user menu ---------- */
.user-wrap { position: relative; }
button.avatar { cursor: pointer; padding: 0; }
button.avatar:hover { border-color: var(--accent); color: var(--text); }
.user-menu {
  position: absolute; right: 0; top: calc(100% + 6px);
  width: 224px; z-index: 65;
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius-sm); box-shadow: 0 8px 28px rgba(0, 0, 0, 0.35);
  padding: 6px; display: flex; flex-direction: column; gap: 2px;
}
.user-head { padding: 8px 10px 10px; border-bottom: 1px solid var(--border-soft); margin-bottom: 4px; }
.user-name { font-size: 13px; font-weight: 700; color: var(--text); }
.user-id { font-size: 11px; color: var(--text-3); font-family: var(--font-num); margin-top: 1px; }
.user-opt {
  display: flex; flex-direction: column; gap: 1px;
  padding: 7px 10px; border: none; background: none;
  border-radius: var(--radius-sm); cursor: pointer; text-align: left;
}
.user-opt:hover { background: var(--panel-2); }
.user-opt .l { font-size: 12px; color: var(--text); font-weight: 600; }
.user-opt .s { font-size: 10.5px; color: var(--text-3); }
.user-opt.danger .l { color: var(--up); }

/* ---------- settings: authorized users ---------- */
.user-add {
  display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end;
  padding: 14px var(--pad) 0;
}
.user-add .field { flex: 1 1 160px; gap: 4px; }
.user-add .btn { flex: 0 0 auto; }
.user-note { font-size: 10.5px; color: var(--text-3); padding: 8px var(--pad) 14px; }
.users-empty {
  font-size: 12px; color: var(--text-2); line-height: 1.5;
  padding: 12px 14px; background: var(--panel-2);
  border: 1px dashed var(--border-soft); border-radius: var(--radius-sm);
  margin: 12px var(--pad) 0;
}

/* ---------- login: hint ---------- */
.login-hint {
  font-size: 11px; color: var(--text-2); line-height: 1.55;
  background: var(--panel-2); border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm); padding: 8px 10px;
}

/* ---------- topbar: alerts bell + quota chip ---------- */

.quota-chip { text-decoration: none; font-family: var(--font-num); }

.bell-wrap { position: relative; }
.bell-btn {
  position: relative;
  display: inline-flex; align-items: center; justify-content: center;
  width: 30px; height: 28px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text-2);
  cursor: pointer;
  font-size: 13px;
}
.bell-btn:hover { color: var(--text); border-color: var(--text-3); }
.bell-count {
  position: absolute; top: -6px; right: -7px;
  min-width: 15px; height: 15px;
  border-radius: 8px;
  background: var(--amber);
  color: #0c1015;
  font-family: var(--font-num);
  font-size: 9.5px; font-weight: 700;
  line-height: 15px; text-align: center;
  padding: 0 3px;
}
.bell-count.risk { background: var(--up); color: #fff; }

.bell-panel {
  position: absolute; right: 0; top: calc(100% + 8px);
  width: 380px; max-height: 70vh; overflow-y: auto;
  z-index: 65;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 16px 50px rgba(0, 0, 0, 0.5);
  padding: 6px;
  display: flex; flex-direction: column; gap: 3px;
}
.bell-head {
  display: flex; align-items: baseline; gap: 8px;
  padding: 7px 10px 5px;
  font-size: 12px; font-weight: 700;
}
.bell-head .bell-sub { font-size: 10px; font-weight: 400; color: var(--text-3); }
.bell-item {
  display: flex; gap: 10px;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  text-decoration: none;
}
.bell-item:hover { background: var(--panel-2); }
.bell-item .sev-dot {
  flex: none; width: 7px; height: 7px; border-radius: 50%;
  margin-top: 5px;
}
.bell-item.sev-risk .sev-dot { background: var(--up); }
.bell-item.sev-warn .sev-dot { background: var(--amber); }
.bell-item.sev-info .sev-dot { background: var(--accent); }
.bell-title { font-size: 12px; font-weight: 600; color: var(--text); }
.bell-detail { font-size: 11px; color: var(--text-2); line-height: 1.5; }
.bell-empty { padding: 18px; text-align: center; font-size: 12px; color: var(--text-3); }

/* ---------- 待確認匯入收件匣 ---------- */

.inbox-list { display: flex; flex-direction: column; gap: 8px; padding: 10px var(--pad) 14px; }
.inbox-item {
  display: flex; align-items: center; gap: 14px;
  background: var(--panel-2);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  padding: 10px 14px;
}
.inbox-item .src-badge {
  flex: none;
  font-family: var(--font-num);
  font-size: 9.5px;
  letter-spacing: 0.05em;
  padding: 1px 6px;
  border-radius: 3px;
  border: 1px solid rgba(88, 166, 221, 0.4);
  color: var(--accent);
  background: var(--accent-soft);
}
.inbox-main { display: flex; flex-direction: column; gap: 1px; min-width: 0; flex: 1; }
.inbox-title { font-size: 12.5px; font-weight: 600; }
.inbox-sub { font-size: 11px; color: var(--text-2); }
.inbox-rule { font-size: 10px; color: var(--text-3); }
.inbox-actions { display: flex; gap: 6px; flex: none; }
.inbox-note { padding: 0 var(--pad) 12px; font-size: 10.5px; color: var(--text-3); }
.inbox-count-badge {
  font-family: var(--font-num);
  font-size: 10px;
  min-width: 16px; height: 16px; line-height: 16px;
  text-align: center; border-radius: 8px; padding: 0 4px;
  background: var(--amber); color: #0c1015; font-weight: 700;
}

/* generic small buttons reused by inbox */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 13px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text-2);
  font-family: var(--font-ui);
  font-size: 12px;
  cursor: pointer;
  text-decoration: none;
}
.btn:hover { border-color: var(--text-3); color: var(--text); }
.btn-primary {
  color: var(--accent);
  border-color: rgba(88, 166, 221, 0.45);
  background: var(--accent-soft);
  font-weight: 700;
}
.btn-primary:hover { border-color: var(--accent); }
.btn-danger {
  color: var(--up);
  border-color: rgba(240, 84, 79, 0.45);
  background: var(--up-soft);
  font-weight: 700;
}
.btn-danger:hover { border-color: var(--up); }

/* ---------- E2: sparkline ---------- */
.spark-cell { text-align: center; }
.spark-wrap { display: inline-flex; align-items: center; }
.sparkline { display: block; opacity: 0.9; }
tr.row-stale .sparkline { opacity: 0.5; }

/* ---------- E6: keyboard focus row ---------- */
table.data tbody tr.kb-focus td {
  background: var(--accent-soft);
  box-shadow: inset 2px 0 0 var(--accent);
}

/* ---------- E3: ex-div month view ---------- */
.exdiv-months { display: flex; flex-direction: column; gap: 12px; padding: 10px var(--pad) 4px; }
.exdiv-month { display: flex; flex-direction: column; gap: 6px; }
.exm-title { font-size: 11.5px; font-weight: 700; color: var(--text-2); }
.exm-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 2px; }
.exm-dow { font-size: 9.5px; color: var(--text-3); text-align: center; padding: 2px 0; }
.exm-cell {
  min-height: 34px; border-radius: 3px; background: var(--panel-2);
  border: 1px solid var(--border-soft);
  display: flex; flex-direction: column; gap: 1px; padding: 2px 3px;
}
.exm-cell.empty { background: transparent; border-color: transparent; }
.exm-cell.today { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.exm-day { font-size: 9px; color: var(--text-3); font-family: var(--font-num); }
.exm-ev {
  font-size: 9px; font-family: var(--font-num); line-height: 13px;
  border-radius: 2px; padding: 0 3px; white-space: nowrap; overflow: hidden;
  text-overflow: ellipsis; cursor: pointer;
}
.exm-ev.ev-ex { background: var(--amber-soft); color: var(--amber); border: 1px solid rgba(217, 161, 63, 0.35); }
.exm-ev.ev-pay { color: var(--ok); border: 1px dashed rgba(63, 185, 80, 0.45); }
.exm-legend {
  display: flex; gap: 10px; align-items: center;
  padding: 8px var(--pad) 12px; font-size: 10px; color: var(--text-3);
}
.exm-legend .exm-ev { cursor: default; }

/* ---------- E4: rebalance drawer ---------- */
.rb-drawer { width: 720px; }
.rb-bar { display: flex; gap: 8px; }
.rb-input {
  width: 64px; background: var(--panel); border: 1px solid var(--border);
  color: var(--text); border-radius: var(--radius-sm);
  font-family: var(--font-num); font-size: 11.5px; padding: 3px 6px; text-align: right;
}
.rb-input:focus { outline: none; border-color: var(--accent); }
.rb-table td { height: 34px; }
.rb-foot { display: flex; gap: 18px; flex-wrap: wrap; align-items: baseline; }
.rb-kv { display: inline-flex; gap: 6px; align-items: baseline; font-size: 11px; }
.rb-kv .k { color: var(--text-3); }
.rb-kv .v { font-family: var(--font-num); font-weight: 600; }
.rb-warn { font-size: 11px; color: var(--amber); }
.rb-open-btn { margin-left: 8px; font-size: 11px; padding: 2px 10px; }
.panel-head .rb-open-btn + .btn-export { margin-left: 8px; }

/* ex-div 入帳預覽 (A3) */
.exdiv-preview {
  font-size: 10.5px;
  color: var(--series-myr);
  font-family: var(--font-num);
}

/* ---------- 標籤防折行（窄欄中文標籤一律不換行，金額照舊右對齊） ---------- */

.panel-title,
details.collapsible > summary .panel-title,
details.freshness > summary > span,
.fx-stat .k,
.fx-rate-label,
.fx-account,
.kpi-label,
.topbar .badge,
.topbar button,
.btn,
.btn-export,
.sum-chips .ccy-chip,
.exdiv-sym .sym-name,
.trend-note {
  white-space: nowrap;
}
.panel-sub {
  text-wrap: pretty;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}
.topbar .asof,
.topbar .asof .label,
details.freshness > summary > span:not(.sum-chips) {
  white-space: nowrap;
}
.sum-chips { flex-wrap: wrap; row-gap: 4px; }
.topbar { flex-wrap: nowrap; }
.topbar .page-title { white-space: nowrap; }

/* ---------- responsive fallback (low priority, simple stacking) ---------- */

@media (max-width: 1280px) {
  .kpi-band { grid-template-columns: repeat(4, 1fr); }
  .row-trend { grid-template-columns: 1fr; }
}
@media (max-width: 860px) {
  .kpi-band { grid-template-columns: repeat(2, 1fr); }
  .row-2, .fx-grid, .insight-grid, .fresh-body { grid-template-columns: 1fr; }
  .app-header { flex-wrap: wrap; }
  .sum-chips { display: none; }
}
