:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --ink: #1c2127;
  --muted: #6b7280;
  --faint: #9aa1ab;
  --line: #e7e9ee;
  --line-strong: #d7dae1;
  --accent: #3b5bdb;
  --accent-soft: #eef1fd;
  --up: #d12b2b;     /* 紅：獲利 / 漲 */
  --down: #1f9d57;   /* 綠：虧損 / 跌 */
  --up-soft: #fdecec;
  --down-soft: #e9f7ef;
  --radius: 12px;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "PingFang TC", "Helvetica Neue",
          "Microsoft JhengHei", sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: var(--sans);
  background: var(--bg);
  color: var(--ink);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

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

/* 頂部 */
.topbar {
  display: flex; align-items: center; gap: 28px;
  padding: 14px 28px;
  padding-top: calc(14px + env(safe-area-inset-top));
  padding-left: calc(28px + env(safe-area-inset-left));
  padding-right: calc(28px + env(safe-area-inset-right));
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 10;
}
.topbar h1 { font-size: 17px; font-weight: 600; margin: 0; letter-spacing: .3px; }
.tabs { display: flex; gap: 4px; }
.tab {
  font: inherit; cursor: pointer;
  padding: 7px 18px; border: 1px solid var(--line);
  background: transparent; color: var(--muted);
  border-radius: 999px; transition: .12s;
}
.tab:hover { border-color: var(--line-strong); color: var(--ink); }
.tab.active { background: var(--ink); color: #fff; border-color: var(--ink); }

/* 右上：登入者資訊 */
.userbar { margin-left: auto; display: flex; align-items: center; gap: 12px; }
.userbar-name { font-size: 13px; color: var(--muted); }
.userbar-name b { color: var(--ink); }
.userbar-back {
  font-size: 13px; color: var(--accent); text-decoration: none;
  padding: 5px 10px; border: 1px solid var(--line-strong); border-radius: 7px;
}
.userbar-back:hover { border-color: var(--accent); }
.userbar-logout {
  font: inherit; font-size: 13px; cursor: pointer;
  padding: 6px 12px; border: 1px solid var(--line-strong);
  background: #fff; color: var(--ink); border-radius: 7px;
}
.userbar-logout:hover { border-color: var(--up); color: var(--up); }

/* 使用者下拉（設定報表收件 email） */
.user-menu { position: relative; }
button.userbar-name { font: inherit; cursor: pointer; border: 0; background: none;
  color: var(--muted); padding: 5px 8px; border-radius: 7px; }
button.userbar-name:hover { background: var(--bg); }
.user-dropdown { position: absolute; right: 0; top: calc(100% + 6px); z-index: 50;
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px;
  box-shadow: 0 12px 34px rgba(0,0,0,.14); padding: 14px; width: 270px; text-align: left; }
.user-dropdown label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.user-dropdown input { width: 100%; font: inherit; padding: 8px 10px;
  border: 1px solid var(--line-strong); border-radius: 8px; background: var(--surface); color: var(--ink); }
.user-dropdown input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.ud-row { display: flex; align-items: center; gap: 10px; margin-top: 10px; }
.ud-msg { font-size: 12px; color: var(--muted); }
.ud-msg.ok { color: var(--down); }
.ud-msg.err { color: var(--up); }
.ud-hint { font-size: 11px; color: var(--faint); margin: 10px 0 0; line-height: 1.5; }

/* admin 檢視他人資料的提示橫幅 */
.view-banner {
  background: var(--accent-soft); color: var(--accent);
  text-align: center; font-size: 13px; padding: 8px 16px;
  border-bottom: 1px solid var(--line);
}

.wrap { max-width: 1160px; margin: 0 auto; padding: 22px; }

/* 卡片 */
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px 20px;
  margin-bottom: 18px;
}
.card-head {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 14px;
}
.card-head h2 { font-size: 15px; font-weight: 600; margin: 0; }

/* 上方雙欄 */
.grid-top {
  display: grid;
  grid-template-columns: 1.55fr 1fr;
  gap: 18px;
  margin-bottom: 18px;
}
@media (max-width: 880px) { .grid-top { grid-template-columns: 1fr; } }

/* 時間刻度切換 */
.seg { display: inline-flex; background: var(--bg); border-radius: 8px; padding: 3px; }
.seg button {
  font: inherit; cursor: pointer; border: 0; background: transparent;
  color: var(--muted); padding: 4px 13px; border-radius: 6px; transition: .12s;
}
.seg button.active { background: var(--surface); color: var(--ink); box-shadow: 0 1px 2px rgba(0,0,0,.08); }

.chart-card { display: flex; flex-direction: column; }
.chart-box { position: relative; flex: 1; min-height: 210px; }
#trendChart { width: 100%; height: 230px; display: block; }
.empty-hint {
  position: absolute; inset: 0; margin: 0;
  display: flex; align-items: center; justify-content: center; text-align: center;
  color: var(--faint); font-size: 13px; padding: 0 30px;
}
.chart-foot { margin-top: 8px; font-size: 12px; color: var(--muted); min-height: 16px; }

/* 日曆 */
.cal-card .card-head { margin-bottom: 8px; }
.cal-nav { display: flex; align-items: center; gap: 10px; font-size: 13px; }
.cal-nav button {
  font: inherit; cursor: pointer; border: 1px solid var(--line);
  background: var(--surface); color: var(--muted);
  width: 26px; height: 26px; border-radius: 7px; line-height: 1;
}
.cal-nav button:hover { color: var(--ink); border-color: var(--line-strong); }
.unit-hint { font-size: 11.5px; color: var(--faint); margin: 0 0 10px; }
.calendar { width: 100%; border-collapse: collapse; table-layout: fixed; }
.calendar th {
  font-size: 11px; font-weight: 500; color: var(--muted);
  padding: 4px 2px; text-align: center;
}
.calendar th.sum, .calendar td.sum { border-left: 1px solid var(--line); }
.calendar td {
  height: 46px; text-align: center; vertical-align: middle;
  border-top: 1px solid var(--line); padding: 2px;
}
.cal-day { font-size: 10px; color: var(--faint); display: block; line-height: 1.2; }
.cal-pl { font-family: var(--mono); font-size: 13px; font-variant-numeric: tabular-nums; }
.cal-pl.zero { color: var(--faint); }
td.sum .cal-pl { font-weight: 600; }
.calendar tr:last-child td.sum { font-weight: 600; }

/* 動作列 */
.actionbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px; flex-wrap: wrap; margin-bottom: 6px;
}
.totals { display: flex; gap: 26px; }
.tot { display: flex; flex-direction: column; }
.tot-label { font-size: 12px; color: var(--muted); }
.tot-value { font-family: var(--mono); font-size: 19px; font-weight: 600; font-variant-numeric: tabular-nums; }
.btns { display: flex; gap: 10px; }
.btn {
  font: inherit; cursor: pointer; padding: 9px 16px;
  border: 1px solid var(--line-strong); background: var(--surface);
  color: var(--ink); border-radius: 9px; transition: .12s;
}
.btn:hover { background: var(--bg); }
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: #324bc0; }
.status { font-size: 12.5px; color: var(--muted); min-height: 18px; margin: 4px 2px 14px; }

/* 資料表 */
.table-scroll { overflow-x: auto; }
table.data { width: 100%; border-collapse: collapse; font-size: 13px; }
table.data th {
  font-weight: 500; color: var(--muted); text-align: right;
  padding: 8px 10px; border-bottom: 1px solid var(--line-strong);
  white-space: nowrap;
}
table.data th.l, table.data td.l { text-align: left; }
table.data td {
  padding: 9px 10px; text-align: right; border-bottom: 1px solid var(--line);
  white-space: nowrap; font-variant-numeric: tabular-nums;
}
table.data tbody tr:hover { background: var(--bg); }
.sym { font-weight: 600; }
.sym small { display: block; font-weight: 400; color: var(--muted); font-size: 11.5px; }
.px-date { display: block; font-weight: 400; color: var(--faint); font-size: 11px; }
.pill { font-size: 12px; padding: 2px 9px; border-radius: 6px; }
.pill.buy { background: var(--up-soft); color: var(--up); }
.pill.sell { background: var(--down-soft); color: var(--down); }
.del {
  cursor: pointer; border: 0; background: transparent; color: var(--faint);
  font-size: 15px; padding: 2px 6px; border-radius: 6px;
}
.del:hover { color: var(--up); background: var(--up-soft); }
.empty-row td { text-align: center; color: var(--faint); padding: 22px; }

/* 入金 / 出金 */
.cash-sum { font-size: 13px; color: var(--muted); }
.cf-tag {
  display: inline-block; font-size: 12px; padding: 1px 8px; border-radius: 999px;
  border: 1px solid var(--line);
}
.cf-tag.dep { color: var(--up); background: var(--up-soft); border-color: var(--up); }
.cf-tag.wd  { color: var(--down); background: var(--down-soft); border-color: var(--down); }

/* 庫存表底部總計列 */
tr.total-row td {
  border-top: 2px solid var(--line-strong);
  border-bottom: none;
  font-weight: 600;
  background: var(--bg);
}
tr.total-row td.l { color: var(--muted); font-weight: 500; }

/* 日曆每日盈虧可點 */
.cal-pl.pl-link {
  cursor: pointer;
  text-decoration: underline;
  text-decoration-style: dotted;
  text-underline-offset: 2px;
}
.cal-pl.pl-link:hover { filter: brightness(1.15); text-decoration-style: solid; }

/* 當日損益明細彈窗 */
.detail-modal { width: min(560px, 94vw); max-height: 86vh; overflow: auto; }
.detail-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px; }
.detail-head h2 { margin: 0; font-size: 17px; }
.detail-close {
  border: none; background: none; font-size: 24px; line-height: 1;
  color: var(--muted); cursor: pointer; padding: 0 4px;
}
.detail-close:hover { color: var(--ink); }
.detail-table { border-collapse: collapse; width: 100%; margin-top: 4px; }
.detail-table th {
  text-align: right; font-size: 12px; color: var(--muted);
  font-weight: 600; padding: 8px 10px; border-bottom: 1px solid var(--line);
}
.detail-table th.l { text-align: left; }
.detail-table td { padding: 9px 10px; border-bottom: 1px solid var(--line); font-size: 13px; }
.detail-table td.l { text-align: left; }
.traded-tag {
  display: inline-block; margin-left: 6px; font-size: 10px; color: var(--muted);
  border: 1px solid var(--line); border-radius: 4px; padding: 1px 5px; vertical-align: middle;
}
.detail-note { color: var(--muted); font-size: 11.5px; margin: 10px 2px 0; line-height: 1.5; }
.stale-note { color: #b26a00; }
.stale-close { color: #b26a00; cursor: help; }
.stale-close sup { font-size: 9px; margin-left: 1px; }
.detail-loading { color: var(--muted); text-align: center; padding: 28px 0; }

/* 交易明細：搜尋 + 每頁筆數 + 分頁 */
.tx-controls { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.tx-controls input[type="search"] {
  font: inherit; padding: 6px 11px; border: 1px solid var(--line-strong);
  border-radius: 8px; background: var(--surface); color: var(--ink); width: 190px;
}
.tx-controls input[type="search"]:focus {
  outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft);
}
.tx-controls select {
  font: inherit; padding: 6px 10px; border: 1px solid var(--line-strong);
  border-radius: 8px; background: var(--surface); color: var(--ink); cursor: pointer;
}
.tx-pager {
  display: flex; align-items: center; justify-content: space-between;
  margin-top: 12px; font-size: 12.5px; color: var(--muted);
}
.pager-btns { display: flex; align-items: center; gap: 10px; }
.pager-btns button {
  font: inherit; cursor: pointer; border: 1px solid var(--line);
  background: var(--surface); color: var(--muted);
  width: 28px; height: 28px; border-radius: 8px; line-height: 1;
}
.pager-btns button:hover:not(:disabled) { color: var(--ink); border-color: var(--line-strong); }
.pager-btns button:disabled { opacity: .4; cursor: default; }

/* 彈窗 */
.modal-bg {
  position: fixed; inset: 0; background: rgba(20,24,31,.4);
  display: flex; align-items: center; justify-content: center; z-index: 50; padding: 20px;
}
.modal-bg[hidden] { display: none; }

/* 頁籤：總計分隔 */
.tab-summary { margin-left: 12px; }
.tab-summary::before {
  content: ""; display: inline-block; width: 1px; height: 16px;
  background: var(--line-strong); margin-right: 16px; vertical-align: middle;
}

/* 總計頁面 */
#summaryView { padding-top: 4px; }
.summary-head {
  display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px;
}
.summary-head h2 { font-size: 17px; font-weight: 600; margin: 0; }
.summary-grid {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
  gap: 16px; margin-bottom: 20px;
}
.mk-card {
  background: var(--surface); border: 1px solid var(--line);
  border-top: 3px solid var(--mk-accent, var(--accent));
  border-radius: var(--radius); padding: 18px 20px;
}
.mk-card.tw { --mk-accent: #3b5bdb; }
.mk-card.us { --mk-accent: #1d9e75; }
.mk-card.jp { --mk-accent: #d85a30; }
.mk-card.kr { --mk-accent: #7f77dd; }
.mk-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.mk-title { display: flex; align-items: center; gap: 8px; font-size: 16px; font-weight: 600; }
.mk-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--mk-accent); }
.mk-cur {
  font-size: 11px; color: var(--muted); border: 1px solid var(--line);
  padding: 2px 8px; border-radius: 999px;
}
.mk-stat { padding: 10px 0; }
.mk-stat + .mk-stat { border-top: 1px solid var(--line); }
.mk-stat .mk-label { font-size: 12.5px; color: var(--muted); margin-bottom: 3px; }
.mk-stat .mk-val { font-family: var(--mono); font-size: 22px; font-weight: 600; font-variant-numeric: tabular-nums; }
.mk-stat .mk-twd { font-size: 12px; color: var(--faint); margin-top: 2px; }

/* 底部換算台幣總和 */
.grand {
  display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 16px;
  background: var(--surface); border: 1px solid var(--line-strong);
  border-radius: var(--radius); padding: 22px 26px;
}
@media (max-width: 600px) { .grand { grid-template-columns: 1fr; } }
.g-item { display: flex; flex-direction: column; gap: 6px; }
.g-item + .g-item { border-left: 1px solid var(--line); padding-left: 26px; }
@media (max-width: 600px) { .g-item + .g-item { border-left: 0; padding-left: 0; border-top: 1px solid var(--line); padding-top: 16px; } }
.g-label { font-size: 13px; color: var(--muted); }
.g-sub { font-size: 11.5px; color: var(--faint); font-weight: 400; }
.g-val { font-family: var(--mono); font-size: 30px; font-weight: 600; font-variant-numeric: tabular-nums; }
.modal {
  background: var(--surface); border-radius: 14px; padding: 24px;
  width: 100%; max-width: 420px; box-shadow: 0 12px 40px rgba(0,0,0,.18);
}
.modal h2 { font-size: 16px; margin: 0 0 18px; }
.field { display: flex; flex-direction: column; gap: 5px; margin-bottom: 14px; }
.field label { font-size: 12.5px; color: var(--muted); }
.field input {
  font: inherit; padding: 9px 11px; border: 1px solid var(--line-strong);
  border-radius: 8px; background: var(--surface); color: var(--ink);
}
.field input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.side-field { flex-direction: row; gap: 8px; }
.side-btn {
  flex: 1; font: inherit; cursor: pointer; padding: 9px; border-radius: 8px;
  border: 1px solid var(--line-strong); background: var(--surface); color: var(--muted);
}
.side-btn.buy.active { background: var(--up-soft); color: var(--up); border-color: var(--up); }
.side-btn.sell.active { background: var(--down-soft); color: var(--down); border-color: var(--down); }
.form-err { color: var(--up); font-size: 12.5px; min-height: 16px; margin: 0 0 8px; }
.modal-btns { display: flex; justify-content: flex-end; gap: 10px; }

/* ── 手機 RWD：表格轉卡片、避免左右捲動 ── */
@media (max-width: 700px) {
  html, body { overflow-x: hidden; }
  .wrap { padding: 14px; padding-bottom: calc(14px + env(safe-area-inset-bottom)); }
  .topbar { flex-wrap: wrap; gap: 10px 14px;
            padding: calc(12px + env(safe-area-inset-top)) 14px 12px; }
  .topbar h1 { font-size: 16px; }
  .tabs { order: 3; width: 100%; flex-wrap: wrap; gap: 6px; }
  .tab { padding: 6px 14px; }
  .card { padding: 14px; }
  .totals { gap: 18px; flex-wrap: wrap; }
  .tot-value { font-size: 17px; }

  /* 資料表（庫存表 / 交易明細）→ 每列一張卡片 */
  .table-scroll { overflow-x: visible; }
  table.data, table.data tbody, table.data tr, table.data td { display: block; width: 100%; }
  table.data thead { display: none; }
  table.data tbody tr {
    border: 1px solid var(--line); border-left: 3px solid var(--accent);
    border-radius: var(--radius); margin-bottom: 10px; padding: 4px 2px; background: var(--surface);
  }
  table.data tbody tr:hover { background: var(--surface); }
  table.data td {
    display: flex; justify-content: space-between; align-items: center; gap: 12px;
    text-align: right; white-space: normal; padding: 8px 12px;
    border-top: 0; border-bottom: 1px solid var(--line);
  }
  table.data tr td:last-child { border-bottom: 0; }
  table.data td:empty { display: none; }            /* 隱藏總計列的空白格 */
  table.data td::before {
    content: attr(data-label); flex: 0 0 42%; text-align: left;
    color: var(--muted); font-weight: 500; white-space: nowrap;
  }
  .sym, .sym small, .px-date { display: inline; }
  .sym small, .px-date { margin-left: 5px; }
  .total-row td { font-weight: 600; }
  .del-cell { justify-content: flex-end; }

  .detail-table { font-size: 12px; }
}

/* ── 總計頁：資產佔比甜甜圈 ── */
.donut-card { padding: 16px; }
.donut-empty { color: var(--faint); text-align: center; padding: 30px; }
.donut-grid { display: flex; align-items: center; gap: 24px; }
.donut-svg { flex: 1 1 auto; min-width: 0; }
.donut-svg svg { width: 100%; height: auto; }
.donut-center-label { fill: var(--muted); }
.donut-center-value { fill: var(--ink); font-family: var(--mono); }
.donut-seg-name { fill: var(--ink); }
.donut-seg-amt { fill: var(--muted); font-family: var(--mono); }
@media (max-width: 720px) { .donut-grid { flex-direction: column; gap: 12px; } }

/* 區段 hover 高亮放大（只用加粗＋陰影，避免與 rotate 屬性衝突）*/
.donut-seg { transition: stroke-width .15s ease, opacity .15s ease, filter .15s ease; }
.donut-seg.hl { stroke-width: 52; filter: drop-shadow(0 2px 5px rgba(0,0,0,.28)); }
.donut-seg.dim { opacity: .3; }

/* 右側圖例：總資產 */
.donut-legend { flex: 0 0 260px; }
.donut-legend .legend-title { font-size: 14px; color: var(--muted); }
.donut-legend .legend-total { font-family: var(--mono); font-size: 26px; font-weight: 700;
  color: var(--ink); margin: 2px 0 14px; }
.legend-list { list-style: none; margin: 0; padding: 0; }
.legend-item { display: flex; align-items: center; gap: 8px; padding: 8px 8px;
  border-radius: 8px; cursor: default; transition: background .12s; }
.legend-item.on { background: var(--bg); }
.legend-dot { width: 11px; height: 11px; border-radius: 3px; flex-shrink: 0; }
.legend-name { font-weight: 600; }
.legend-pct { color: var(--muted); font-size: 12px; }
.legend-amt { margin-left: auto; font-family: var(--mono); font-variant-numeric: tabular-nums; }
@media (max-width: 720px) { .donut-legend { flex-basis: auto; width: 100%; } }
