/* ──────────────────────────────────────────────────────────────────────────────
 * AllocLab UI v5 — /portfolio 投組分析（Phase 5e Wave 1）
 * 法律守門：純資料整理、不踩 ToS §2 資產配置規劃紅線
 * 顏色：accent / ink-2 / ink-muted（不借 up/dn token 在「分布」場景）
 * ──────────────────────────────────────────────────────────────────────────── */

/* ---------- Header ---------- */
.pf-hdr {
  position: sticky;
  top: 0;
  z-index: var(--z-sticky);
  background: var(--al-bg);
  border-bottom: 1px solid var(--al-border-soft);
}
.pf-hdr__row {
  display: grid;
  grid-template-columns: 44px 1fr 44px;
  align-items: center;
  height: var(--header-h);
  padding: 0 8px;
  gap: 4px;
}
.pf-hdr__title {
  text-align: center;
  font-family: var(--ff-display);
  font-size: 1.05rem;
  font-weight: 600;
  color: var(--al-ink);
  letter-spacing: -0.005em;
}
.pf-hdr__meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px var(--pad-x) 10px;
  font-size: var(--fs-xs);
  color: var(--al-ink-muted);
  font-variant-numeric: tabular-nums;
  flex-wrap: wrap;
  gap: 8px;
}
.pf-hdr__last strong {
  color: var(--al-accent);
  font-weight: 600;
}

/* ---------- Main ---------- */
.pf-main {
  padding: var(--sp-3) var(--pad-x) var(--sp-7);
  display: flex;
  flex-direction: column;
  gap: var(--sp-3);
}

/* ---------- AI 摘要句 ---------- */
.pf-ai-line {
  background: var(--al-active-bg);
  border: 1px solid var(--al-accent);
  border-left-width: 3px;
}
.pf-ai-line__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 6px;
  flex-wrap: wrap;
  gap: 6px;
}
.pf-ai-line__delta {
  font-size: var(--fs-xs);
  color: var(--al-accent);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.pf-ai-line__txt {
  margin: 0;
  font-size: var(--fs-base);
  line-height: 1.65;
  color: var(--al-ink);
}
.pf-ai-line__txt strong {
  color: var(--al-accent);
  font-weight: 600;
}

/* ---------- 1. 持股總覽 ---------- */
.pf-overview { display: flex; flex-direction: column; gap: var(--sp-3); }
.pf-ov__top {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: var(--sp-3);
  flex-wrap: wrap;
}
.pf-ov__total { display: flex; flex-direction: column; gap: 2px; }
.pf-ov__total .k {
  font-size: 0.7rem;
  color: var(--al-ink-muted);
  letter-spacing: 0.04em;
}
.pf-ov__total .v {
  font-family: var(--ff-display);
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--al-ink);
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
}
.pf-ov__today { text-align: right; display: flex; flex-direction: column; gap: 2px; }
.pf-ov__today .v {
  font-size: 1.05rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.pf-ov__today .d {
  font-size: 0.7rem;
  font-variant-numeric: tabular-nums;
}
.pf-ov__row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  padding-top: 12px;
  border-top: 1px solid var(--al-border-soft);
}
.pf-ov__cell {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.pf-ov__cell .k {
  font-size: 0.68rem;
  color: var(--al-ink-muted);
  letter-spacing: 0.04em;
}
.pf-ov__cell .v {
  font-size: var(--fs-base);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--al-ink);
}

/* ---------- 2. 持股配比 ---------- */
.pf-stack {
  display: flex;
  height: 14px;
  border-radius: 7px;
  overflow: hidden;
  background: var(--al-surface-2);
  margin-bottom: 12px;
}
.pf-stack__seg {
  display: block;
  height: 100%;
}
.pf-hold-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.pf-hold-row {
  display: grid;
  grid-template-columns: 12px 56px 1fr 50px 60px;
  align-items: center;
  gap: 10px;
  padding: 10px 8px;
  border-radius: var(--radius-inner);
  min-height: 44px;
  transition: background var(--t-fast) var(--ease);
}
.pf-hold-row:hover {
  background: var(--al-surface-2);
}
.pf-hold-row__sw {
  width: 10px;
  height: 10px;
  border-radius: 50%;
}
.pf-hold-row__code {
  font-family: var(--ff-mono);
  font-size: var(--fs-sm);
  color: var(--al-ink);
  font-weight: 500;
}
.pf-hold-row__name {
  font-size: var(--fs-sm);
  color: var(--al-ink);
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.pf-hold-row__weight {
  font-size: var(--fs-sm);
  color: var(--al-ink-2);
  text-align: right;
  font-variant-numeric: tabular-nums;
}
.pf-hold-row__pnl {
  font-size: var(--fs-sm);
  font-weight: 600;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

/* ---------- 3. 產業分散 ---------- */
.pf-sec-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.pf-sec-row {
  display: grid;
  grid-template-columns: 80px 1fr 50px;
  grid-template-rows: auto auto;
  align-items: center;
  gap: 4px 10px;
}
.pf-sec-row .k {
  grid-column: 1;
  grid-row: 1;
  font-size: var(--fs-sm);
  color: var(--al-ink);
  font-weight: 500;
}
.pf-sec-bar {
  grid-column: 2;
  grid-row: 1;
  height: 8px;
  border-radius: 4px;
  background: var(--al-surface-2);
  overflow: hidden;
  position: relative;
}
.pf-sec-bar i {
  display: block;
  height: 100%;
  background: var(--al-accent);
  border-radius: 4px;
}
.pf-sec-row .v {
  grid-column: 3;
  grid-row: 1;
  font-size: var(--fs-sm);
  font-weight: 600;
  text-align: right;
  font-variant-numeric: tabular-nums;
  color: var(--al-ink);
}
.pf-sec-row .meta {
  grid-column: 2 / span 2;
  grid-row: 2;
  font-size: 0.7rem;
  color: var(--al-ink-muted);
  font-variant-numeric: tabular-nums;
}
.pf-sec-note {
  margin: 12px 0 0;
  padding-top: 12px;
  border-top: 1px solid var(--al-border-soft);
  font-size: var(--fs-xs);
  color: var(--al-ink-2);
  line-height: 1.6;
}

/* ---------- 4. vs 大盤 ---------- */
.pf-vs-list { display: flex; flex-direction: column; gap: 8px; }
.pf-vs-row {
  display: grid;
  grid-template-columns: 80px 1fr 60px;
  gap: 10px;
  align-items: center;
}
.pf-vs-row .k {
  font-size: var(--fs-sm);
  color: var(--al-ink-2);
}
.pf-vs-row--mine .k {
  color: var(--al-ink);
  font-weight: 600;
}
.pf-vs-bar {
  height: 10px;
  background: var(--al-surface-2);
  border-radius: 5px;
  overflow: hidden;
}
.pf-vs-bar i {
  display: block;
  height: 100%;
  border-radius: 5px;
}
.pf-vs-row .v {
  font-size: var(--fs-sm);
  font-weight: 600;
  text-align: right;
  font-variant-numeric: tabular-nums;
}
.pf-vs-diff {
  display: flex;
  gap: 16px;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--al-border-soft);
  font-size: var(--fs-xs);
  color: var(--al-ink-muted);
  flex-wrap: wrap;
}
.pf-vs-diff strong {
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

/* ---------- 5. 本月變化 ---------- */
.pf-chg-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}
.pf-chg-cell {
  background: var(--al-surface-2);
  border-radius: var(--radius-inner);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.pf-chg-cell .k {
  font-size: 0.68rem;
  color: var(--al-ink-muted);
  letter-spacing: 0.04em;
}
.pf-chg-cell .v {
  font-size: var(--fs-sm);
  color: var(--al-ink);
  font-weight: 500;
  line-height: 1.3;
}
.pf-chg-cell .d {
  font-size: 0.7rem;
  font-variant-numeric: tabular-nums;
  color: var(--al-ink-2);
}

/* ---------- 6. 風險指標 ---------- */
.pf-risk-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}
.pf-risk-cell {
  background: var(--al-surface-2);
  border-radius: var(--radius-inner);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-height: 84px;
}
.pf-risk-cell .k {
  font-size: 0.7rem;
  color: var(--al-ink-muted);
  letter-spacing: 0.04em;
  display: flex;
  align-items: center;
}
.pf-risk-cell .v {
  font-family: var(--ff-display);
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--al-ink);
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
}
.pf-risk-cell .d {
  font-size: 0.65rem;
  color: var(--al-ink-muted);
  line-height: 1.45;
}

/* ---------- 7. 再平衡計算機（active、純算術工具）---------- */
.pf-rebal__intro {
  margin: 0 0 var(--sp-3);
  font-size: var(--fs-xs);
  color: var(--al-ink-2);
  line-height: 1.6;
}
.pf-rebal__empty {
  padding: var(--sp-3);
  background: var(--al-surface);
  border: 1px dashed var(--al-border);
  border-radius: 8px;
  text-align: center;
  color: var(--al-ink-muted);
  font-size: var(--fs-sm);
}
.pf-rebal__empty p { margin: 0; }
.pf-rebal__rows {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: var(--sp-3);
}
.pf-rebal__row {
  display: grid;
  grid-template-columns: 1fr auto auto auto;
  gap: 10px;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid var(--al-border);
  font-size: var(--fs-sm);
}
.pf-rebal__row:last-child { border-bottom: none; }
.pf-rebal__row-sym {
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.pf-rebal__row-sym strong { color: var(--al-ink); }
.pf-rebal__row-name {
  font-size: var(--fs-xs);
  color: var(--al-ink-muted);
}
.pf-rebal__row-cur {
  color: var(--al-ink-muted);
  font-variant-numeric: tabular-nums;
}
.pf-rebal__row-arrow {
  color: var(--al-ink-muted);
  font-size: var(--fs-xs);
}
.pf-rebal__row-tgt-wrap {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  padding: 2px 6px;
  border: 1px solid var(--al-border);
  border-radius: 6px;
  background: var(--al-bg);
}
.pf-rebal__row-tgt-wrap:focus-within {
  border-color: var(--al-accent);
}
.pf-rebal__row-tgt {
  width: 56px;
  border: none;
  outline: none;
  background: transparent;
  text-align: right;
  color: var(--al-ink);
  font-size: var(--fs-sm);
  font-variant-numeric: tabular-nums;
}
.pf-rebal__row-pct { color: var(--al-ink-muted); font-size: var(--fs-xs); }

.pf-rebal__total {
  display: grid;
  grid-template-columns: auto auto 1fr;
  gap: 8px;
  align-items: baseline;
  padding: 8px 0;
  border-top: 1px solid var(--al-border);
  font-size: var(--fs-sm);
}
.pf-rebal__total .k { color: var(--al-ink-muted); }
.pf-rebal__total .v {
  font-weight: 600;
  color: var(--al-ink);
  font-variant-numeric: tabular-nums;
}
.pf-rebal__total .d {
  font-size: var(--fs-xs);
  color: var(--al-ink-muted);
}
/* severity 非方向性、不用漲跌 token（CLAUDE.md §6 例外）：
   加總正確 = all-clear 綠（國際慣例）；加總錯誤 = 警示紅 */
.pf-rebal__total .d.is-ok   { color: #2a7a50; }
.pf-rebal__total .d.is-warn { color: #b83232; }

.pf-rebal__act {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  margin-top: var(--sp-3);
}

.pf-rebal__result {
  margin-top: var(--sp-3);
  padding-top: var(--sp-3);
  border-top: 1px dashed var(--al-border);
}
.pf-rebal__result-rows {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: var(--sp-3);
}
.pf-rebal__res-row {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 10px;
  align-items: center;
  padding: 6px 0;
  font-size: var(--fs-sm);
}
.pf-rebal__res-sym strong { color: var(--al-ink); }
.pf-rebal__res-shift {
  color: var(--al-ink-muted);
  font-variant-numeric: tabular-nums;
  font-size: var(--fs-xs);
}
.pf-rebal__res-act {
  text-align: right;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}
/* 買賣意圖 = 操作建議色、非價格漲跌方向（CLAUDE.md §6 例外）：
   買進 = 安全綠 hardcode、賣出 = 警示紅 hardcode */
.pf-rebal__res-act--buy  { color: #2a7a50; }
.pf-rebal__res-act--sell { color: #b83232; }

.pf-rebal__result-note {
  margin: 0;
  font-size: 0.7rem;
  color: var(--al-ink-muted);
  line-height: 1.5;
}

/* ---------- Header banner (示範資料 / 真實資料 切換)---------- */
.pf-hdr__banner {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px var(--pad-x) 10px;
  background: var(--al-active-bg);
  border-top: 1px solid var(--al-accent);
  border-bottom: 1px solid var(--al-accent);
  flex-wrap: wrap;
}
.pf-hdr__banner-ico {
  flex: 0 0 auto;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--al-accent);
  color: var(--al-bg);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.7rem;
  font-weight: 700;
}
.pf-hdr__banner-txt {
  flex: 1;
  min-width: 0;
  font-size: var(--fs-xs);
  color: var(--al-ink-2);
  line-height: 1.45;
}
.pf-hdr__banner-txt strong {
  color: var(--al-accent);
  font-weight: 600;
}
.pf-hdr__banner-cta {
  padding: 6px 14px;
  background: var(--al-accent);
  color: var(--al-bg);
  border-radius: var(--radius-pill);
  font-size: 0.75rem;
  font-weight: 600;
  min-height: 32px;
  white-space: nowrap;
  transition: background var(--t-fast) var(--ease);
}
.pf-hdr__banner-cta:hover { background: var(--al-accent-2); }

/* ---------- 編輯 sheet ---------- */
.pf-edit {
  position: fixed;
  inset: 0;
  z-index: var(--z-modal, 50);
  display: none;
}
.pf-edit[aria-hidden="false"] { display: block; }
.pf-edit__backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  animation: pf-fade 180ms var(--ease);
}
.pf-edit__panel {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  max-height: 92vh;
  background: var(--al-bg);
  border-top-left-radius: 20px;
  border-top-right-radius: 20px;
  border-top: 1px solid var(--al-border-soft);
  padding: 8px var(--pad-x) calc(env(safe-area-inset-bottom, 0) + 16px);
  overflow-y: auto;
  animation: pf-up 220ms var(--ease);
}
@keyframes pf-fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes pf-up   { from { transform: translateY(100%); } to { transform: translateY(0); } }
.pf-edit__handle {
  width: 36px;
  height: 4px;
  background: var(--al-border-md);
  border-radius: 2px;
  margin: 6px auto 12px;
}
.pf-edit__head {
  display: grid;
  grid-template-columns: 44px 1fr 44px;
  align-items: center;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--al-border-soft);
  margin-bottom: 14px;
}
.pf-edit__head h3 {
  margin: 0;
  text-align: center;
  font-family: var(--ff-display);
  font-size: 1.05rem;
  font-weight: 600;
  color: var(--al-ink);
}

/* ---------- list step：持股 row ---------- */
.pf-edit__list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 12px;
}
.pf-edit__empty {
  padding: var(--sp-5) 0;
  text-align: center;
  color: var(--al-ink-muted);
  font-size: var(--fs-sm);
}
.pf-edit__row {
  display: grid;
  grid-template-columns: 56px 1fr 36px 36px;
  gap: 10px;
  align-items: center;
  padding: 10px 8px;
  background: var(--al-surface);
  border: 1px solid var(--al-border-soft);
  border-radius: var(--radius-inner);
}
.pf-edit__row-code {
  font-family: var(--ff-mono);
  font-size: var(--fs-sm);
  font-weight: 600;
  color: var(--al-ink);
}
.pf-edit__row-body {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}
.pf-edit__row-name {
  font-size: var(--fs-sm);
  color: var(--al-ink);
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.pf-edit__row-meta {
  font-size: 0.7rem;
  color: var(--al-ink-muted);
  font-variant-numeric: tabular-nums;
}
.pf-edit__row-act {
  width: 36px;
  height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  color: var(--al-ink-muted);
  border-radius: var(--radius-inner);
  transition: color var(--t-fast) var(--ease),
              background var(--t-fast) var(--ease);
}
.pf-edit__row-act:hover {
  color: var(--al-accent);
  background: var(--al-active-bg);
}
.pf-edit__row-act--del:hover {
  color: var(--al-error);
  background: rgba(184, 50, 50, 0.12);
}

.pf-edit__add {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  padding: 12px;
  background: var(--al-active-bg);
  border: 1px dashed var(--al-accent);
  border-radius: var(--radius-card);
  color: var(--al-accent);
  font-size: var(--fs-sm);
  font-weight: 600;
  min-height: 48px;
  transition: background var(--t-fast) var(--ease);
}
.pf-edit__add:hover { background: var(--al-accent); color: var(--al-bg); }
.pf-edit__add svg { width: 18px; height: 18px; }

/* ---------- form step：新增/編輯 input ---------- */
.pf-edit__form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.pf-edit__lbl {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: var(--fs-sm);
  color: var(--al-ink-2);
}
.pf-edit__inp {
  width: 100%;
  padding: 10px 12px;
  background: var(--al-surface);
  border: 1px solid var(--al-border-soft);
  border-radius: var(--radius-inner);
  color: var(--al-ink);
  /* iOS Safari mobile：16px 防 focus auto-zoom */
  font-size: 16px;
  font-family: var(--ff-sans);
  outline: none;
}
@media (min-width: 768px) {
  .pf-edit__inp { font-size: var(--fs-base); }
}
.pf-edit__inp.mono { font-family: var(--ff-mono); }
.pf-edit__inp:focus-visible {
  border-color: var(--al-accent);
  box-shadow: 0 0 0 2px var(--al-active-bg);
}
.pf-edit__hint {
  margin: -4px 0 0;
  font-size: 0.7rem;
  color: var(--al-ink-muted);
  line-height: 1.5;
}
.pf-edit__err {
  margin: 0;
  padding: 8px 12px;
  background: rgba(184, 50, 50, 0.12);
  border: 1px solid var(--al-error);
  border-radius: var(--radius-inner);
  font-size: var(--fs-xs);
  color: var(--al-error);
  line-height: 1.5;
}

.pf-edit__act {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  padding-top: 10px;
  border-top: 1px solid var(--al-border-soft);
  margin-top: 4px;
  flex-wrap: wrap;
}
.pf-edit__act .al-btn {
  min-height: 40px;
  padding: 8px 18px;
}

/* Tablet+ ---- center the sheet panel */
@media (min-width: 768px) {
  .pf-edit__panel {
    max-width: 560px;
    margin: 0 auto;
    border-radius: 20px;
    bottom: 24px;
  }
}

/* ---------- Disclaimer ---------- */
.pf-disclaimer {
  margin: var(--sp-4) 0 0;
  padding: var(--sp-3) var(--sp-4);
  background: var(--al-surface);
  border: 1px solid var(--al-border-soft);
  border-radius: var(--radius-card);
  font-size: var(--fs-xs);
  line-height: 1.65;
  color: var(--al-ink-muted);
}

/* ---------- 漲跌色（單值損益場景，遵 §6 token）---------- */
.pf-main .up { color: var(--al-up); }
.pf-main .dn { color: var(--al-dn); }

/* ---------- Tablet ≥768 ---------- */
@media (min-width: 768px) {
  .pf-main {
    max-width: 720px;
    margin: 0 auto;
  }
  .pf-chg-grid,
  .pf-risk-grid {
    grid-template-columns: repeat(4, 1fr);
  }
}

/* ---------- Desktop ≥1024 ---------- */
@media (min-width: 1024px) {
  .pf-main {
    max-width: 880px;
  }
}
