/* 市場總覽｜深色、響應式雙欄卡片 */

:root {
  --bg: #090d14;
  --bg-top: rgba(9, 13, 20, 0.88);
  --card: #0f151e;
  --card-hi: #131a25;
  --line: #263042;
  --text: #f2f5fa;
  --mute: #8d96a8;
  --up: #ff5a5f;      /* 台股慣例：漲紅 */
  --down: #3ecf8e;    /* 跌綠 */
  --flat: #c9ccd6;
  --warn: #f0a93b;
  --bad: #ff7b72;
  --live: #3ecf8e;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html {
  -webkit-text-size-adjust: 100%;
  background: var(--bg);
  color-scheme: dark;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  padding-bottom: env(safe-area-inset-bottom, 0);
}

/* ---------- 頂部 ---------- */
.top {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--bg-top);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--line);
  padding: calc(env(safe-area-inset-top, 0px) + 8px) 12px 6px;
}

.top-inner {
  max-width: 1100px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.title h1 {
  margin: 0;
  font-size: 18px;
  font-weight: 800;
  letter-spacing: 0.2px;
}

.title .sub {
  margin: 2px 0 0;
  font-size: 11px;
  color: var(--mute);
}

.controls {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 0 0 auto;
}

.controls select,
.controls button {
  font: inherit;
  font-size: 13px;
  color: var(--text);
  background: #151c28;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 7px 10px;
  min-height: 34px;
}

.controls button {
  font-weight: 700;
  cursor: pointer;
}

.controls button:active { background: #1c2535; }
.controls button[disabled] { opacity: 0.6; }
.controls button.busy::before {
  content: "";
  display: inline-block;
  width: 8px;
  height: 8px;
  margin-right: 6px;
  border-radius: 50%;
  background: var(--warn);
  animation: pulse 0.9s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 0.3; }
  50% { opacity: 1; }
}

.statusbar {
  max-width: 1100px;
  margin: 6px auto 0;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 2px 12px;
  font-size: 12px;
  color: var(--mute);
}

.summary { font-variant-numeric: tabular-nums; }

/* ---------- 提示橫幅 ---------- */
.banner {
  max-width: 1100px;
  margin: 10px 12px 0;
  padding: 9px 12px;
  border-radius: 10px;
  border: 1px solid #6b4a1c;
  background: #2a2010;
  color: #ffd79a;
  font-size: 13px;
  line-height: 1.45;
}

@media (min-width: 1124px) {
  .banner { margin-left: auto; margin-right: auto; }
}

.banner[hidden] { display: none; }

/* ---------- 內容 ---------- */
.app {
  max-width: 1100px;
  margin: 0 auto;
  padding: 10px 12px 4px;
}

.loading-text {
  color: var(--mute);
  text-align: center;
  padding: 40px 0;
}

.group { margin: 0 0 14px; }

.group-title {
  margin: 0 0 8px;
  padding: 0 0 0 8px;
  font-size: 14px;
  font-weight: 800;
  line-height: 1.2;
  color: var(--accent, #6ea8ff);
  border-left: 3px solid var(--accent, #6ea8ff);
}

/* 雙欄卡片 */
.grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

@media (min-width: 900px) {
  .grid { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; }
}

@media (max-width: 330px) {
  .grid { grid-template-columns: minmax(0, 1fr); }
}

/* ---------- 卡片 ---------- */
.card {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 10px 10px 9px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: linear-gradient(180deg, var(--card-hi), var(--card));
}

.card.st-err {
  border-style: dashed;
  border-color: #6d3a3e;
  background: #140f12;
}

.card.st-stale { border-color: #6b4a1c; }
.card.st-loading { opacity: 0.6; }
.card.offline { opacity: 0.72; }

.row1 {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-width: 0;
}

.id { min-width: 0; flex: 1 1 0; }

.name {
  margin: 0;
  font-size: 14px;
  font-weight: 750;
  line-height: 1.2;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sym {
  display: block;
  margin-top: 2px;
  font-size: 11px;
  color: var(--mute);
  font-variant-numeric: tabular-nums;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* 迷你走勢圖：只畫折線，不填色 */
.chart {
  flex: 0 0 46%;
  height: 36px;
  min-width: 0;
}

.spark {
  display: block;
  width: 100%;
  height: 100%;
  overflow: visible;
  color: var(--flat);
}

.spark .line {
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
  vector-effect: non-scaling-stroke;
}

.spark .base {
  fill: none;
  stroke: rgba(255, 255, 255, 0.28);
  stroke-width: 1;
  stroke-dasharray: 3 3;
  vector-effect: non-scaling-stroke;
}

.spark.empty .base { stroke: rgba(255, 255, 255, 0.16); }

.dir-up .spark { color: var(--up); }
.dir-down .spark { color: var(--down); }
.dir-flat .spark { color: var(--flat); }
.dir-none .spark { color: var(--mute); }

.price {
  margin-top: 2px;
  font-size: clamp(20px, 6.2vw, 26px);
  font-weight: 800;
  line-height: 1.1;
  letter-spacing: 0.2px;
  font-variant-numeric: tabular-nums;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.dir-up .price,
.dir-up .chg { color: var(--up); }
.dir-down .price,
.dir-down .chg { color: var(--down); }
.dir-flat .price,
.dir-flat .chg { color: var(--flat); }
.dir-none .price { color: var(--mute); }
.dir-none .chg { color: var(--bad); }

.chg {
  display: flex;
  flex-wrap: wrap;
  gap: 2px 8px;
  font-size: 13px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  min-height: 17px;
}

.foot {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 3px;
  margin-top: 3px;
  min-width: 0;
}

.meta {
  font-size: 11px;
  line-height: 1.35;
  color: var(--mute);
  word-break: break-word;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* ---------- 狀態標籤 ---------- */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  max-width: 100%;
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid var(--line);
  font-size: 11px;
  font-weight: 700;
  line-height: 1.4;
  color: var(--mute);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.st-live .badge { color: var(--live); border-color: #1f5a41; background: #0f2a1f; }
.st-live .badge::before {
  content: "";
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--live);
  flex: 0 0 auto;
}
.st-delay .badge,
.st-stale .badge { color: var(--warn); border-color: #6b4a1c; background: #271d0d; }
.st-closed .badge { color: #aab2c0; border-color: #343d4d; background: #151b26; }
.st-err .badge { color: var(--bad); border-color: #6d3a3e; background: #2a1316; }
.st-unk .badge { color: var(--mute); }

/* ---------- 頁尾 ---------- */
.foot-wrap { max-width: 1100px; margin: 0 auto; }

footer.foot {
  max-width: 1100px;
  margin: 0 auto;
  padding: 6px 14px calc(env(safe-area-inset-bottom, 0px) + 24px);
  font-size: 11px;
  line-height: 1.55;
  color: var(--mute);
}

footer.foot p { margin: 4px 0; }

.noscript {
  text-align: center;
  color: var(--warn);
  padding: 20px;
}

/* ---------- 自選 ---------- */
.group-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin: 0 0 8px;
}
.group-head .group-title { margin: 0; }

.add-btn {
  font: inherit;
  font-size: 13px;
  font-weight: 700;
  color: var(--text);
  background: #151c28;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 6px 12px;
  min-height: 32px;
  cursor: pointer;
}
.add-btn:active { background: #1c2535; }

.empty-hint {
  margin: 0 0 4px;
  padding: 12px;
  border: 1px dashed var(--line);
  border-radius: 12px;
  color: var(--mute);
  font-size: 12px;
  line-height: 1.5;
}

.brow {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  width: 100%;
  min-width: 0;
}
.del {
  font: inherit;
  font-size: 11px;
  color: var(--mute);
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 2px 9px;
  cursor: pointer;
  flex: 0 0 auto;
}
.del:active { color: var(--bad); border-color: #6d3a3e; }

.sheet {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: rgba(0, 0, 0, 0.62);
}
.sheet[hidden] { display: none; }
.sheet-box {
  width: 100%;
  max-width: 480px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 16px 16px calc(env(safe-area-inset-bottom, 0px) + 16px);
  background: #0f151e;
  border: 1px solid var(--line);
  border-radius: 16px 16px 0 0;
}
.sheet-box h3 { margin: 0 0 2px; font-size: 17px; }
.sheet-box label {
  display: flex;
  flex-direction: column;
  gap: 5px;
  font-size: 12px;
  color: var(--mute);
}
.sheet-box select,
.sheet-box input {
  font: inherit;
  font-size: 16px; /* 16px 以上，iPhone 才不會自動放大畫面 */
  color: var(--text);
  background: #151c28;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  min-height: 42px;
}
.sheet-box .hint { margin: 0; font-size: 12px; color: var(--mute); line-height: 1.5; }
.sheet-box .err { margin: 0; font-size: 12px; color: var(--bad); }
.sheet-box .err[hidden] { display: none; }
.sheet-actions { display: flex; gap: 8px; margin-top: 4px; }
.sheet-actions button {
  flex: 1;
  font: inherit;
  font-size: 15px;
  font-weight: 700;
  color: var(--text);
  background: #151c28;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 11px;
  cursor: pointer;
}
.sheet-actions button.primary { background: #1d4f8f; border-color: #2f6fc0; }

/* ---------- K 線視窗 ---------- */
.card[role="button"] { cursor: pointer; }
.card[role="button"]:focus-visible { outline: 2px solid #4aa8ff; outline-offset: 2px; }
body.ch-open { overflow: hidden; }
.chart-modal {
  position: fixed; inset: 0; z-index: 60; background: var(--bg);
  display: flex; flex-direction: column; overflow-y: auto; -webkit-overflow-scrolling: touch;
}
.chart-modal[hidden] { display: none; }
.ch-box {
  width: 100%; max-width: 900px; margin: 0 auto;
  padding: calc(env(safe-area-inset-top, 0px) + 10px) 12px calc(env(safe-area-inset-bottom, 0px) + 16px);
  display: flex; flex-direction: column; gap: 8px;
}
.ch-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.ch-title { display: flex; align-items: baseline; gap: 8px; min-width: 0; font-size: 18px; }
.ch-title b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ch-sym { color: var(--mute); font-size: 12px; }
.ch-close {
  font: inherit; font-size: 16px; color: var(--text); background: #151c28;
  border: 1px solid var(--line); border-radius: 10px; width: 40px; height: 40px; cursor: pointer; flex: 0 0 auto;
}
.ch-bar { display: flex; flex-wrap: wrap; gap: 6px; }
.ch-btn {
  font: inherit; font-size: 13px; color: var(--mute); background: #11171f;
  border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; min-height: 32px; cursor: pointer;
}
.ch-btn.on { color: var(--text); background: #1d4f8f; border-color: #2f6fc0; }
.ch-info { font-size: 12px; line-height: 1.55; color: var(--flat); min-height: 38px; font-variant-numeric: tabular-nums; }
.ch-canvas { position: relative; width: 100%; height: min(62vh, 520px); min-height: 340px; border: 1px solid var(--line); border-radius: 12px; background: var(--card); overflow: hidden; }
.ch-canvas canvas { display: block; }
.ch-status { font-size: 12px; color: var(--mute); }
.ch-status.warn { color: var(--warn); }
.ch-status.bad { color: var(--bad); }
.ch-sum { display: flex; flex-direction: column; gap: 6px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 12px; background: var(--card); }
.ch-row { display: flex; gap: 10px; font-size: 13px; line-height: 1.45; }
.ch-k { flex: 0 0 82px; color: var(--mute); }
.ch-v { flex: 1; min-width: 0; }
.ch-note { margin: 4px 0 0; font-size: 11px; color: var(--mute); }
