:root {
  --bg: #f5f6f8;
  --card: #ffffff;
  --text: #16181d;
  --sub: #8a8f98;
  --accent: #2f7cf6;
  --accent-soft: rgba(47, 124, 246, .10);
  --line: rgba(0, 0, 0, .06);
  --key: #ffffff;
  --key-fn: #e8eaee;
  --shadow: 0 1px 3px rgba(0,0,0,.05);
  --danger: #e5484d;
  --chart: #6b7280;
  --up: #e5484d;      /* 台股慣例: 紅漲 */
  --down: #16a34a;    /* 綠跌 */
  --grid: rgba(0,0,0,.10);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --card: #191c22;
    --text: #eceef2;
    --sub: #7d838d;
    --accent: #4d90f8;
    --accent-soft: rgba(77, 144, 248, .14);
    --line: rgba(255, 255, 255, .07);
    --key: #22262e;
    --key-fn: #2c313b;
    --shadow: none;
    --chart: #9aa1ac;
    --up: #ff5c60;
    --down: #35c46a;
    --grid: rgba(255,255,255,.10);
  }
}

* { margin: 0; padding: 0; box-sizing: border-box; }
html { height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang TC",
    "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100%;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  padding-bottom: env(safe-area-inset-bottom);
}

/* ---------- 頂列 ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(14px + env(safe-area-inset-top)) 20px 10px;
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 7;   /* 需高於置頂列 (.pinbar), 以免被蓋住 */
}
.topbar h1 { font-size: 30px; font-weight: 800; letter-spacing: .5px; }
.topbar-left { display: flex; flex-direction: column; gap: 2px; }
.topbar-right { display: flex; gap: 6px; }
.status {
  border: 0; background: none; color: var(--sub);
  font-size: 12px; text-align: left; padding: 0; cursor: pointer;
  font-family: inherit;
}
.status.stale { color: #d97706; }
.status.spin::after { content: " ⟳"; display: inline-block; animation: rot 1s linear infinite; }
@keyframes rot { to { transform: rotate(360deg); } }

.iconbtn {
  width: 38px; height: 38px; border: 0; border-radius: 12px;
  background: var(--card); color: var(--sub);
  display: grid; place-items: center; cursor: pointer;
  box-shadow: var(--shadow);
}
.iconbtn svg { width: 20px; height: 20px; fill: currentColor; }
.iconbtn.active { background: var(--accent); color: #fff; }
.iconbtn:active { filter: brightness(.9); }
#refreshBtn.spinning svg { animation: rot .8s linear infinite; }
#refreshBtn.spinning { color: var(--accent); }

/* ---------- 貨幣清單 ---------- */
.list {
  padding: 4px 14px 12px;
  max-width: 560px;
  margin: 0 auto;
}
.row {
  position: relative;
  border-radius: 16px;
  margin-bottom: 8px;
  overflow: hidden;
  box-shadow: var(--shadow);
  touch-action: pan-y;
}
.row-card {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--card);
  border-radius: 16px;
  padding: 13px 16px;
  cursor: pointer;
  border: 2px solid transparent;
  transition: border-color .12s;
  position: relative;
  will-change: transform;
}
.row-card.active { border-color: var(--accent); }

/* 捲動時置頂顯示的輸入貨幣 */
.pinbar {
  position: fixed;
  top: var(--topbar-h, 84px);
  left: 0; right: 0;
  z-index: 6;
  padding: 5px 14px 10px;
  max-width: 560px;
  margin: 0 auto;
  background: linear-gradient(var(--bg) 72%, transparent);
  animation: pindrop .18s ease-out;
}
@keyframes pindrop { from { opacity: 0; transform: translateY(-8px); } }
.pinbar .row-card {
  box-shadow: 0 6px 18px rgba(0,0,0,.16);
  cursor: pointer;
}
.row.dragging { opacity: .5; transform: scale(1.02); z-index: 3; }
.row.dragging .row-card { box-shadow: 0 8px 24px rgba(0,0,0,.22); }
body.reordering { cursor: grabbing; }
body.reordering .row:not(.dragging) { transition: transform .18s; }

/* 右滑動作 */
.row-actions {
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 148px;
  display: flex;
}
.act {
  flex: 1;
  border: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 3px;
  color: #fff;
  font-size: 11px;
  font-family: inherit;
  cursor: pointer;
}
.act svg { width: 20px; height: 20px; fill: currentColor; }
.act:active { filter: brightness(.88); }
.act-chart { background: var(--accent); }
.act-del { background: var(--danger); }
.act[disabled] { background: var(--key-fn); color: var(--sub); cursor: default; }
.flag { font-size: 30px; line-height: 1; width: 38px; text-align: center; flex: none; }
.row-info { flex: none; width: 74px; }
.row-code { font-size: 17px; font-weight: 700; letter-spacing: .3px; }
.row-name { font-size: 11px; color: var(--sub); margin-top: 1px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-right { flex: 1; min-width: 0; text-align: right; }
.row-amount {
  font-size: 26px; font-weight: 650; font-variant-numeric: tabular-nums;
  white-space: nowrap; overflow: hidden;
  letter-spacing: -.3px;
}
.row-card.active .row-amount { color: var(--accent); }
.row-amount .cursor {
  display: inline-block; width: 2px; height: 1em; background: var(--accent);
  vertical-align: -2px; margin-left: 1px;
  animation: blink 1.1s step-end infinite;
}
@keyframes blink { 50% { opacity: 0; } }
.row-sub { font-size: 11px; color: var(--sub); margin-top: 1px; font-variant-numeric: tabular-nums; }
.row-amount.small { font-size: 20px; }
.row-amount.tiny { font-size: 16px; }

/* ---------- 鍵盤 ---------- */
.keypad {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  background: var(--bg);
  border-top: 1px solid var(--line);
  padding: 8px 10px calc(10px + env(safe-area-inset-bottom));
  animation: rise .18s ease-out;
}
@keyframes rise { from { transform: translateY(100%); } }
.kp-close {
  display: block;
  width: 100%; max-width: 560px;
  margin: 0 auto 6px;
  border: 0; background: none; color: var(--sub);
  padding: 2px 0 4px;
  cursor: pointer;
}
.kp-close svg { width: 26px; height: 26px; fill: currentColor; }
.kp-close:active { color: var(--text); }

.keys {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  max-width: 560px;
  margin: 0 auto;
}
.key {
  border: 0; border-radius: 14px;
  background: var(--key); color: var(--text);
  font-size: 24px; font-weight: 600;
  padding: 13px 0;
  cursor: pointer;
  box-shadow: var(--shadow);
  font-family: inherit;
  transition: filter .08s;
}
.key:active { filter: brightness(.85); }
.key.fn { background: var(--key-fn); font-size: 20px; }
.key.op { background: var(--key-fn); color: var(--accent); }
.key.eq { grid-row: span 2; background: var(--accent); color: #fff; }
.key.zero { grid-column: span 2; }

body.keypad-open .alerts { padding-bottom: 380px; }

/* ---------- 底部面板 ---------- */
.sheet { position: fixed; inset: 0; z-index: 20; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.4); }
.sheet-body {
  position: absolute; left: 0; right: 0; bottom: 0;
  max-height: 78vh;
  background: var(--bg);
  border-radius: 20px 20px 0 0;
  padding: 16px 16px calc(16px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column;
  animation: rise .2s ease-out;
  max-width: 560px; margin: 0 auto;
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.sheet-head h2 { font-size: 19px; font-weight: 700; }
#pickerSearch {
  border: 0; border-radius: 12px;
  background: var(--card); color: var(--text);
  font-size: 16px; padding: 12px 14px; margin-bottom: 10px;
  outline: none; font-family: inherit;
  box-shadow: var(--shadow);
}
.picker-list { overflow-y: auto; flex: 1; }
.picker-item {
  display: flex; align-items: center; gap: 12px;
  padding: 11px 8px; border-bottom: 1px solid var(--line);
  cursor: pointer; border-radius: 10px;
}
.picker-item:active { background: var(--accent-soft); }
.picker-item .flag { font-size: 24px; width: 32px; }
.picker-item .pi-code { font-weight: 700; font-size: 15px; }
.picker-item .pi-name { color: var(--sub); font-size: 13px; flex: 1; }
.picker-item .pi-tag {
  font-size: 10px; padding: 3px 7px; border-radius: 6px;
  background: var(--accent-soft); color: var(--accent); flex: none;
}
.picker-item .pi-tag.intl { background: rgba(160,160,160,.15); color: var(--sub); }
.picker-item.added { opacity: .35; pointer-events: none; }

/* 明細 */
.detail-body { overflow-y: auto; }
.detail-grid {
  display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 12px;
}
.detail-cell {
  background: var(--card); border-radius: 14px; padding: 14px;
  box-shadow: var(--shadow);
}
.detail-cell .dc-label { font-size: 12px; color: var(--sub); margin-bottom: 4px; }
.detail-cell .dc-value { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }
.detail-note { font-size: 12px; color: var(--sub); line-height: 1.6; padding: 0 4px; }

/* ---------- 新高新低提醒 ---------- */
.alerts {
  max-width: 560px;
  margin: 0 auto;
  padding: 2px 14px 20px;
}
.alerts-head {
  display: flex; align-items: center; gap: 6px;
  font-size: 12px; color: var(--sub);
  padding: 6px 4px 8px;
}
.alerts-head svg { width: 14px; height: 14px; fill: currentColor; }
.alert {
  display: flex; align-items: center; gap: 10px;
  background: var(--card);
  border-radius: 14px;
  padding: 11px 14px;
  margin-bottom: 7px;
  box-shadow: var(--shadow);
  cursor: pointer;
  border-left: 3px solid transparent;
}
.alert:active { filter: brightness(.97); }
.alert.high { border-left-color: var(--up); }
.alert.low { border-left-color: var(--down); }
.alert .a-flag { font-size: 22px; width: 28px; text-align: center; flex: none; }
.alert .a-main { flex: 1; min-width: 0; }
.alert .a-title { font-size: 14px; font-weight: 600; }
.alert .a-title .a-tag {
  font-size: 11px; font-weight: 700; padding: 2px 6px;
  border-radius: 5px; margin-left: 6px; vertical-align: 1px;
}
.alert.high .a-tag { background: rgba(229,72,77,.12); color: var(--up); }
.alert.low .a-tag { background: rgba(22,163,74,.12); color: var(--down); }
.alert .a-sub { font-size: 11px; color: var(--sub); margin-top: 2px; }
.alert .a-right { text-align: right; flex: none; }
.alert .a-val { font-size: 16px; font-weight: 700; font-variant-numeric: tabular-nums; }
.alert .a-chg { font-size: 11px; font-weight: 600; font-variant-numeric: tabular-nums; }
.alert.high .a-chg { color: var(--up); }
.alert.low .a-chg { color: var(--down); }
.alerts-empty {
  font-size: 12px; color: var(--sub); text-align: center;
  padding: 10px; line-height: 1.7;
}

/* ---------- 走勢圖 ---------- */
.tabs {
  display: flex; gap: 6px; margin-bottom: 14px;
  background: var(--key-fn); padding: 4px; border-radius: 12px;
}
.tab {
  flex: 1; border: 0; border-radius: 9px; padding: 9px 0;
  background: none; color: var(--sub);
  font-size: 14px; font-weight: 600; font-family: inherit;
  cursor: pointer;
}
.tab.on { background: var(--card); color: var(--text); box-shadow: var(--shadow); }

.chart-body { overflow-y: auto; }
.chart-head {
  display: flex; align-items: flex-start; justify-content: space-between;
  margin-bottom: 6px; padding: 0 2px;
}
.chart-now { font-size: 28px; font-weight: 700; font-variant-numeric: tabular-nums; }
.chart-now small { font-size: 14px; color: var(--sub); font-weight: 600; }
.chart-chg { font-size: 14px; font-weight: 700; margin-top: 2px; font-variant-numeric: tabular-nums; }
.chart-chg-sub { color: var(--sub); font-weight: 400; font-size: 12px; margin-left: 2px; }
.chart-hilo {
  text-align: right; font-size: 12px; color: var(--sub); line-height: 1.7;
  font-variant-numeric: tabular-nums;
}
.chart-hilo b { color: var(--text); font-weight: 600; }

.chart-svg {
  width: 100%; height: 220px; display: block;
  margin: 4px 0 10px;
}
.chart-svg .ax { font-size: 15px; fill: var(--sub); }
.chart-svg .grid { stroke: var(--grid); stroke-width: 1; stroke-dasharray: 4 4; vector-effect: non-scaling-stroke; }
.chart-msg {
  padding: 70px 20px; text-align: center; color: var(--sub); font-size: 14px;
}

.empty-hint {
  text-align: center; color: var(--sub); font-size: 14px; padding: 60px 20px;
  line-height: 1.8;
}
