:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --text: #11151a;
  --muted: #6b7380;
  --line: #e2e5ea;
  --accent: #0a84ff;
  --up: #e5383b;     /* 台股慣例：紅漲 */
  --down: #1a9e55;   /* 綠跌 */
  --danger: #d92d20;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b0f14;
    --surface: #151b23;
    --text: #e8ecf1;
    --muted: #8a94a3;
    --line: #26303b;
    --accent: #3d9bff;
    --danger: #ff6b5e;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "PingFang TC", "Noto Sans TC", "Segoe UI", sans-serif;
  -webkit-text-size-adjust: 100%;
}

body {
  min-height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

[hidden] { display: none !important; }

h1 { font-size: 1.5rem; margin: 0 0 .25rem; }
h2 { font-size: 1.15rem; margin: 1.5rem 0 .5rem; }
p { margin: .5rem 0; }

.muted { color: var(--muted); }

.view.center {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 1rem;
}

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 1.25rem;
}

.narrow { width: 100%; max-width: 360px; text-align: center; }

.logo { width: 72px; height: 72px; border-radius: 16px; margin-bottom: .75rem; }

button, .btn {
  font: inherit;
  border: 0;
  border-radius: 10px;
  padding: .55rem 1rem;
  cursor: pointer;
  text-decoration: none;
  display: inline-block;
  background: var(--line);
  color: var(--text);
}
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--accent); color: #fff; }
button.big { width: 100%; padding: .85rem; font-size: 1.05rem; margin-top: 1rem; }
button.ghost, .btn.ghost { background: transparent; color: var(--accent); }
button.danger { background: transparent; color: var(--danger); }
button.small { padding: .3rem .6rem; font-size: .875rem; }

.field { display: block; text-align: left; margin-top: 1rem; }
.field span { display: block; font-size: .875rem; color: var(--muted); margin-bottom: .25rem; }
input, select {
  font: inherit;
  width: 100%;
  padding: .6rem .75rem;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--text);
}
input[type="checkbox"] { width: auto; }

.msg { min-height: 1.5em; font-size: .9rem; }
.msg.error { color: var(--danger); }

.topbar {
  position: sticky;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .5rem;
  padding: .75rem 1rem;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
  z-index: 20; /* above the chart canvases */
}
.topbar h1 { margin: 0; font-size: 1.25rem; }
.topbar-actions { display: flex; gap: .25rem; }

.content { padding: 1rem; max-width: 760px; margin: 0 auto; }

.hint { margin-top: 1rem; }
.hint p { margin: .25rem 0 0; color: var(--muted); font-size: .9rem; }

/* ---------- admin ---------- */
h3 { font-size: .85rem; color: var(--muted); font-weight: 600; margin: 1rem 0 .25rem; }
.stack > * + * { margin-top: .75rem; }
.row { display: flex; gap: .5rem; align-items: flex-end; }
.row.wrap { flex-wrap: wrap; margin-top: 1rem; }
.grow { flex: 1; min-width: 0; }
.check { display: flex; align-items: center; gap: .4rem; padding-bottom: .6rem; white-space: nowrap; }
.linkbox { margin-top: 1rem; border-color: var(--accent); }
.linkbox input { margin: .5rem 0; font-size: .85rem; }
.user { margin-bottom: 1rem; }
.user-head { display: flex; flex-wrap: wrap; align-items: center; gap: .4rem; }
.user-head .item-sub { width: 100%; }
.badge { font-size: .75rem; padding: .05rem .5rem; border-radius: 99px; background: var(--line); }
.badge.warn { background: var(--danger); color: #fff; }
.list { list-style: none; margin: 0; padding: 0; }
.list.card { padding: 0 1rem; }
.item { display: flex; align-items: center; gap: .5rem; padding: .6rem 0; border-top: 1px solid var(--line); }
.item:first-child { border-top: 0; }
.item-title { font-weight: 500; overflow-wrap: anywhere; }
.item-sub { font-size: .8rem; color: var(--muted); overflow-wrap: anywhere; }

/* ---------- watchlist ---------- */
button.icon { font-size: 1.5rem; line-height: 1; padding: .25rem .6rem; }

.empty { text-align: center; padding: 3rem 1rem; color: var(--muted); }

.wl-section { margin-bottom: 1.25rem; }
.wl-head { display: flex; align-items: baseline; justify-content: space-between; margin: .25rem .25rem .4rem; }
.wl-head h2 { margin: 0; font-size: 1rem; }
.mkt-status { font-size: .8rem; color: var(--muted); }
.mkt-status.open::before, .mkt-status.pre::before, .mkt-status.post::before {
  content: ""; display: inline-block; width: .5rem; height: .5rem; border-radius: 50%;
  background: var(--up); margin-right: .35rem; vertical-align: .05rem;
}
.mkt-status.pre::before, .mkt-status.post::before { background: #e8a33d; }

.wl-list { list-style: none; margin: 0; padding: 0 .9rem; background: var(--surface);
  border: 1px solid var(--line); border-radius: 14px; }

.q-row { display: flex; align-items: center; gap: .75rem; padding: .7rem 0; border-top: 1px solid var(--line); }
.q-row:first-child { border-top: 0; }
.q-main { flex: 1; min-width: 0; }
.q-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.q-sub { font-size: .8rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.q-price { font-variant-numeric: tabular-nums; font-weight: 600; text-align: right; min-width: 5.5em;
  border-radius: 6px; padding: .1rem .3rem; transition: background-color .8s; }
.q-price.flash-up { background: color-mix(in srgb, var(--up) 30%, transparent); transition: none; }
.q-price.flash-down { background: color-mix(in srgb, var(--down) 30%, transparent); transition: none; }

.q-chg { font-variant-numeric: tabular-nums; font-weight: 600; font-size: .9rem; color: #fff;
  min-width: 5.6em; text-align: right; padding: .35rem .55rem; border-radius: 8px; }
.q-chg.up { background: var(--up); }
.q-chg.down { background: var(--down); }
.q-chg.flat { background: var(--muted); }
/* 漲停 / 跌停: ring around the pill */
.q-chg.up.limit { outline: 2px solid var(--up); outline-offset: 2px; }
.q-chg.down.limit { outline: 2px solid var(--down); outline-offset: 2px; }

.q-row.editing { gap: .5rem; }
.q-move { display: flex; gap: .25rem; }
.icon-btn { width: 2.2rem; height: 2.2rem; padding: 0; border-radius: 50%; font-size: 1.1rem; font-weight: 700; }
.icon-btn.danger { background: var(--danger); color: #fff; }
.icon-btn:disabled { opacity: .25; }

.account { display: flex; align-items: center; justify-content: center; gap: .5rem;
  margin-top: 2rem; color: var(--muted); font-size: .9rem; }

/* ---------- search sheet ---------- */
.sheet { position: fixed; inset: 0; z-index: 10; background: var(--bg); overflow-y: auto;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left); }
.sheet-bar { position: sticky; top: 0; display: flex; gap: .5rem; align-items: center;
  padding: .75rem 1rem; background: var(--bg); border-bottom: 1px solid var(--line); }
.sheet-bar input { flex: 1; font-size: 16px; /* >=16px stops iOS zooming in */ }
.results { list-style: none; margin: 0 auto; padding: 0 1rem; max-width: 760px; }
.result { display: flex; align-items: center; gap: .75rem; padding: .65rem 0; border-bottom: 1px solid var(--line); }
.mkt-badge { font-size: .75rem; font-weight: 700; width: 1.6rem; height: 1.6rem; border-radius: 6px;
  display: grid; place-items: center; color: #fff; flex: none; }
.mkt-badge.TW { background: #c0392b; }
.mkt-badge.US { background: #2c5aa0; }
.add-btn { font-size: 1.2rem; font-weight: 700; min-width: 2.6rem; color: var(--accent); background: transparent; }
.add-btn.done { font-size: .85rem; font-weight: 400; color: var(--muted); }

/* ---------- privacy shield ---------- */
.privacy { position: fixed; inset: 0; z-index: 100; display: grid; place-items: center;
  background: color-mix(in srgb, var(--bg) 55%, transparent);
  -webkit-backdrop-filter: blur(24px); backdrop-filter: blur(24px); }
.privacy img { width: 88px; height: 88px; border-radius: 20px; opacity: .9; }

/* ---------- stock detail ---------- */
.tappable { cursor: pointer; -webkit-tap-highlight-color: transparent; }
.q-row.tappable:active .q-main { opacity: .6; }

.topbar .back { padding-left: 0; white-space: nowrap; }
.title-stack { flex: 1; min-width: 0; text-align: center; line-height: 1.2; }
.title-stack h1 { font-size: 1.05rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.title-stack span { font-size: .75rem; }

.detail { padding-top: .75rem; }
.d-quote { display: flex; align-items: baseline; flex-wrap: wrap; gap: .25rem .75rem; }
.d-price { font-size: 2rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.d-chg { font-weight: 600; font-variant-numeric: tabular-nums; white-space: pre; }
.d-status { font-size: .8rem; width: 100%; }
.d-price.up, .d-chg.up, .d-legend .up { color: var(--up); }
.d-price.down, .d-chg.down, .d-legend .down { color: var(--down); }
.d-price.flat, .d-chg.flat, .d-legend .flat { color: var(--muted); }

.d-stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(6.5rem, 1fr)); gap: .15rem 1rem;
  margin: .75rem 0; font-size: .85rem; }
.stat { display: flex; justify-content: space-between; gap: .4rem; }
.stat dt { color: var(--muted); white-space: nowrap; }
.stat dd { margin: 0; font-variant-numeric: tabular-nums; }

.seg { display: inline-flex; background: var(--line); border-radius: 9px; padding: 2px; margin: .25rem 0 .5rem; }
.seg button { background: transparent; padding: .3rem .9rem; font-size: .85rem; border-radius: 7px; color: var(--muted); }
.seg button.on { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: 0 1px 2px rgb(0 0 0 / .15); }

.d-legend { display: flex; flex-wrap: wrap; gap: .1rem .6rem; font-size: .72rem; min-height: 2.2em;
  font-variant-numeric: tabular-nums; color: var(--muted); }
.d-legend.small { min-height: 1.2em; margin-top: .25rem; }
.lg-date { color: var(--text); font-weight: 600; }
.ma-0 { color: #f5a623; } .ma-1 { color: #2f80ed; } .ma-2 { color: #d63ad6; } .ma-3 { color: #8a8f98; }
.ind-a { color: #f5a623; } .ind-b { color: #2f80ed; }

.chart-wrap { position: relative; }
.chart { height: min(68vh, 560px); min-height: 380px; }
.chart-msg { position: absolute; inset: 0; display: grid; place-items: center; margin: 0; text-align: center; }

.attribution { font-size: .7rem; color: var(--muted); margin-top: 1rem; }
.attribution a { color: inherit; }

/* ---------- alerts ---------- */
.bell { font-size: .75em; margin-left: .3rem; vertical-align: .1em; }
.topbar-spacer { min-width: 4.5rem; }
.d-alerts { margin-top: 1.5rem; }
.d-alerts h2 { margin-top: 0; }
.al-form { display: flex; gap: .5rem; align-items: center; }
.al-form select { width: auto; flex: none; }
.al-form input { flex: 1; min-width: 0; font-size: 16px; }
.d-alerts .list { margin-top: .5rem; }
.push-card { margin-bottom: .75rem; }
.push-card .row.wrap { margin-top: .5rem; }
.push-card .row.wrap:empty, .push-card .msg:empty { display: none; }
.push-state { margin: 0; font-size: .9rem; }
.push-state.on { color: var(--down); }
.push-state.denied, .push-state.off { color: var(--danger); }
.alert-row.inactive .item-title { color: var(--muted); }
.small-note { font-size: .8rem; margin-top: 1.5rem; }
#ao-active, #ao-done { margin-bottom: .5rem; }

/* ---------- market overview ---------- */
.ov { margin-bottom: 1.25rem; }
.ov-cards { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: .5rem; }
.ov-cards + .ov-cards { margin-top: .5rem; }
.ov-card { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: .55rem .6rem;
  font-variant-numeric: tabular-nums; min-width: 0; position: relative; }
.ov-card:active { opacity: .7; }
.ov-title { font-size: .75rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ov-badge { font-size: .65rem; margin-left: .3rem; padding: 0 .3rem; border-radius: 4px; background: var(--line); color: var(--text); }
.ov-price { font-size: 1.05rem; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ov-chg { font-size: .78rem; font-weight: 600; white-space: nowrap; min-height: 1.2em; }
.ov-sub { font-size: .7rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ov-card.up .ov-price, .ov-card.up .ov-chg { color: var(--up); }
.ov-card.down .ov-price, .ov-card.down .ov-chg { color: var(--down); }
.ov-card.up .ov-value, .ov-card.down .ov-value { color: var(--text); } /* level stays neutral, change is colored */
.ov-more { display: flex; flex-direction: column; justify-content: center; }
.ov-more .ov-title { font-size: .95rem; font-weight: 700; color: var(--text); }
.ov-more-arrow { position: absolute; right: .6rem; top: 50%; transform: translateY(-50%); font-size: 1.4rem; color: var(--muted); }

.ov-list { margin: .5rem 0 0; }
.ov-list.chips { padding: 0 .9rem; margin: 0 0 .5rem; }
.ov-row { display: flex; align-items: center; justify-content: space-between; gap: .75rem; padding: .55rem 0;
  border-top: 1px solid var(--line); font-size: .92rem; }
.ov-list.chips .ov-row:first-child { border-top: 0; }
.ov-row dt { color: var(--muted); flex: 1; }
.ov-row dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }
.ov-row dd.up { color: var(--up); }
.ov-row dd.down { color: var(--down); }
.ov-note { display: block; font-size: .75rem; font-weight: 400; color: var(--muted); }
.chev { color: var(--muted); font-size: 1.2rem; }
#chips-body h2 { font-size: .85rem; color: var(--muted); margin: 1rem .25rem .4rem; }
#chips-body h2:first-child { margin-top: 0; }
.series-chart { height: min(55vh, 440px); min-height: 300px; }
