/*
 * Dashboard styles. Kept in its own file (rather than an inline <style>) so the
 * page can run under a CSP without 'unsafe-inline', and so browsers can cache it.
 *
 * Layout tiers: phone <=560px, tablet 561-900px, desktop >900px. The charts do
 * not depend on these breakpoints -- dashboard-app.js measures the SVG's real
 * width and draws to it -- so CSS only has to hand the chart a sensible box.
 */

:root {
  color-scheme: light;
  --ink: #15243b;
  --muted: #68768a;
  --paper: #f4f7fb;
  --card: #fff;
  --grid: #dbe3ee;
  --twse: #1769e0;
  --tpex: #e77a12;
  --danger: #d83b4d;
  --series-maintenance: #21a9d8;
  --series-index: #ef5a3c;
  --shadow: 0 20px 55px rgba(31, 55, 89, .11);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: linear-gradient(145deg, #edf4ff 0, #f7f8fb 46%, #fff5e9 100%);
  color: var(--ink);
  font-family: Inter, "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  min-height: 100vh;
  min-height: 100dvh;
  overflow-x: hidden;
}

main {
  width: min(1240px, calc(100% - 28px));
  margin: 34px auto 60px;
  padding-bottom: env(safe-area-inset-bottom);
}

header {
  display: flex;
  justify-content: space-between;
  gap: 24px;
  align-items: end;
  margin-bottom: 20px;
}

.eyebrow { color: var(--twse); font-size: .78rem; font-weight: 800; letter-spacing: .16em; }
h1 { margin: .35rem 0 .45rem; font-size: clamp(1.75rem, 4vw, 3.25rem); line-height: 1.08; letter-spacing: -.04em; }
.subtitle { margin: 0; color: var(--muted); line-height: 1.7; }
.data-freshness {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  margin: 8px 0 0;
  color: #287450;
  font-size: .78rem;
  font-weight: 750;
}
.data-freshness::before {
  content: "";
  width: 8px;
  height: 8px;
  flex: 0 0 auto;
  border-radius: 999px;
  background: currentColor;
}
.data-freshness[data-state="loading"] { color: var(--muted); }
.data-freshness[data-state="warning"] { color: #a15b09; }

/* ---------- metric cards ---------- */

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
  margin-bottom: 14px;
}
.metric {
  min-width: 0;
  background: rgba(255, 255, 255, .82);
  border: 1px solid rgba(255, 255, 255, .9);
  border-radius: 18px;
  padding: 15px 17px;
  box-shadow: 0 8px 28px rgba(31, 55, 89, .06);
}
.metric.focused { border-color: rgba(23, 105, 224, .28); box-shadow: 0 10px 30px rgba(23, 105, 224, .12); }
.metric small { display: block; color: var(--muted); margin-bottom: 7px; }
.metric strong {
  display: block;
  font-size: clamp(1.05rem, 2vw, 1.45rem);
  font-variant-numeric: tabular-nums;
  overflow-wrap: anywhere;
}
.metric.twse strong { color: var(--twse); }
.metric.tpex strong { color: var(--tpex); }

/* ---------- range controls ---------- */

.range-bar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  margin: 0 0 14px;
  padding: 12px 15px;
  background: rgba(255, 255, 255, .78);
  border: 1px solid white;
  border-radius: 18px;
  box-shadow: 0 8px 26px rgba(31, 55, 89, .06);
}
.range-bar > strong { margin-right: 2px; font-size: .9rem; }
.range-controls { display: flex; align-items: center; flex-wrap: wrap; gap: 5px; }
.stock-range-bar { display: flex; align-items: center; flex-wrap: wrap; gap: 9px; margin: 4px 6px 0; padding: 7px 0 2px; }
.stock-range-bar > strong { color: var(--muted); font-size: .78rem; }

.range-button {
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 8px 11px;
  min-height: 40px;
  border-radius: 10px;
  cursor: pointer;
  font: inherit;
  font-size: .86rem;
  font-weight: 750;
  touch-action: manipulation;
}
.range-button:hover { background: #edf3fb; color: var(--ink); }
.range-button:focus-visible { outline: 2px solid var(--twse); outline-offset: 2px; }
.range-button.active { color: white; background: var(--ink); }
.range-note { margin-left: auto; color: var(--muted); font-size: .78rem; }

.etf-mode-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin: 0 0 10px;
  padding: 13px 15px;
  background: rgba(255, 255, 255, .82);
  border: 1px solid white;
  border-radius: 18px;
  box-shadow: 0 8px 26px rgba(31, 55, 89, .06);
}
.etf-mode-bar strong { display: block; margin-bottom: 3px; font-size: .9rem; }
.etf-mode-bar span { color: var(--muted); font-size: .78rem; line-height: 1.5; }
.segmented-control {
  display: inline-flex;
  flex: 0 0 auto;
  gap: 4px;
  padding: 4px;
  background: #edf3fb;
  border-radius: 13px;
}
.segment-button {
  min-height: 40px;
  padding: 8px 13px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font: inherit;
  font-size: .84rem;
  font-weight: 800;
  touch-action: manipulation;
}
.segment-button.active {
  color: white;
  background: var(--ink);
  box-shadow: 0 4px 12px rgba(20, 36, 61, .18);
}
.segment-button:focus-visible { outline: 2px solid var(--twse); outline-offset: 2px; }
.etf-test-notice {
  margin: 0 0 14px;
  padding: 10px 14px;
  color: #7a4c08;
  background: #fff8e7;
  border: 1px solid #f1d99a;
  border-radius: 14px;
  font-size: .78rem;
  line-height: 1.6;
}

.mobile-only { display: none; }

/* ---------- charts ---------- */

.market-grid { display: grid; gap: 16px; }

.chart-card {
  position: relative;
  background: rgba(255, 255, 255, .93);
  border: 1px solid white;
  border-radius: 24px;
  padding: 18px;
  box-shadow: var(--shadow);
  overflow: hidden;
}
.chart-toolbar { display: flex; justify-content: space-between; gap: 16px; align-items: center; margin: 2px 6px 8px; }
.chart-toolbar strong { font-size: .96rem; }
.hint { color: var(--muted); font-size: .82rem; }

/*
 * No aspect-ratio here on purpose: the SVG's viewBox is written in real CSS
 * pixels by dashboard-app.js, so height:auto gives it exactly the height the
 * script chose for the current width. touch-action:pan-y keeps vertical page
 * scrolling native while horizontal drags pan the time window.
 */
.market-chart {
  display: block;
  width: 100%;
  height: auto;
  outline: none;
  touch-action: pan-y;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}
.market-chart:focus-visible { outline: 2px solid var(--twse); outline-offset: 3px; }
.market-chart * { user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
.chart-overlay { cursor: crosshair; }
.drag-series { will-change: transform; }

.axis { fill: var(--muted); font-size: 12px; }
.panel-title { fill: var(--ink); font-size: 15px; font-weight: 800; }
.axis-title { fill: var(--muted); font-size: 12px; font-weight: 750; }
.grid { stroke: var(--grid); stroke-width: 1; }
.path { fill: none; stroke-linecap: round; stroke-linejoin: round; vector-effect: non-scaling-stroke; }
.path-density-low { stroke-width: 2.2; }
.path-density-medium { stroke-width: 1.55; }
.path-density-high { stroke-width: 1.1; }
.path-density-xhigh { stroke-width: .85; }
.path-density-ultra { stroke-width: .65; }
.crosshair { stroke: #4d5d73; stroke-width: 1; stroke-dasharray: 5 5; vector-effect: non-scaling-stroke; pointer-events: none; }
.cursor-label {
  fill: #34445b;
  font-size: 12px;
  font-weight: 800;
  paint-order: stroke;
  stroke: white;
  stroke-width: 4px;
  stroke-linejoin: round;
  pointer-events: none;
}
.dot { stroke: white; stroke-width: 3; vector-effect: non-scaling-stroke; }
.reference { stroke: var(--danger); stroke-width: 1.5; stroke-dasharray: 7 5; vector-effect: non-scaling-stroke; }
.reference-label { fill: var(--danger); font-size: 12px; font-weight: 750; }

.legend { display: flex; flex-wrap: wrap; gap: 14px; margin: 10px 8px 2px; color: var(--muted); font-size: .82rem; }
.legend span::before {
  content: "";
  display: inline-block;
  width: 18px;
  height: 3px;
  margin: 0 7px 3px 0;
  border-radius: 4px;
  background: currentcolor;
}
/* Swatch colours live here rather than in style="" so script-src/style-src can
   both stay free of 'unsafe-inline'. */
.legend .swatch-maintenance::before { background: var(--series-maintenance); }
.legend .swatch-index::before { background: var(--series-index); }
.legend .swatch-alert::before { background: var(--danger); }

.chart-tooltip {
  position: absolute;
  z-index: 3;
  pointer-events: none;
  width: 235px;
  max-width: calc(100% - 16px);
  background: rgba(21, 36, 59, .96);
  color: white;
  padding: 13px 14px;
  border-radius: 14px;
  box-shadow: 0 12px 34px rgba(20, 35, 57, .25);
  opacity: 0;
  transform: translateY(4px);
  transition: opacity .12s, transform .12s;
  font-size: .82rem;
}
.chart-tooltip.show { opacity: 1; transform: none; }
.chart-tooltip b { display: block; margin-bottom: 8px; font-size: .92rem; }
.tip-row { display: flex; justify-content: space-between; gap: 12px; line-height: 1.75; }
.tip-row span:first-child { color: #cbd5e5; }
.tip-row span:last-child { font-variant-numeric: tabular-nums; font-weight: 750; }

/* ---------- stock search ---------- */

.stock-section {
  margin-top: 18px;
  padding: 22px;
  border-radius: 24px;
  background: rgba(255, 255, 255, .8);
  border: 1px solid white;
  box-shadow: var(--shadow);
}
.stock-section-head { display: flex; align-items: end; justify-content: space-between; gap: 20px; margin-bottom: 15px; }
.stock-section h2 { margin: 0 0 6px; font-size: clamp(1.25rem, 2.5vw, 1.8rem); }
.stock-section p { margin: 0; color: var(--muted); line-height: 1.65; }
.stock-search-wrap { position: relative; width: min(480px, 100%); }
.stock-search-form { display: flex; gap: 8px; }
.stock-search-form input {
  min-width: 0;
  flex: 1;
  min-height: 46px;
  border: 1px solid #cad5e4;
  background: white;
  color: var(--ink);
  border-radius: 13px;
  padding: 12px 14px;
  font: inherit;
  font-size: 16px; /* keeps iOS Safari from zooming on focus */
  outline: none;
}
.stock-search-form input:focus { border-color: var(--twse); box-shadow: 0 0 0 3px rgba(23, 105, 224, .12); }
.stock-search-form button {
  border: 0;
  color: white;
  background: var(--ink);
  border-radius: 13px;
  padding: 0 18px;
  min-height: 46px;
  font: inherit;
  font-weight: 800;
  cursor: pointer;
  touch-action: manipulation;
}
.stock-search-form input:disabled,
.stock-search-form button:disabled { cursor: not-allowed; opacity: .58; }

.stock-suggestions {
  position: absolute;
  z-index: 8;
  top: calc(100% + 6px);
  left: 0;
  right: 0;
  display: grid;
  background: white;
  border: 1px solid #d7e0ec;
  border-radius: 14px;
  padding: 6px;
  box-shadow: 0 16px 40px rgba(21, 36, 59, .18);
}
.suggestion {
  display: grid;
  grid-template-columns: 72px minmax(0, 1fr) auto;
  gap: 10px;
  align-items: center;
  width: 100%;
  min-height: 44px;
  border: 0;
  border-radius: 9px;
  padding: 9px 10px;
  text-align: left;
  background: transparent;
  color: var(--ink);
  cursor: pointer;
  font: inherit;
}
.suggestion:hover { background: #edf4ff; }
.suggestion span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.suggestion small { color: var(--muted); }

.stock-search-status { min-height: 1.6em; margin: 10px 2px 13px; color: var(--muted); font-size: .82rem; }
.stock-search-status.error { color: var(--danger); }
.methodology { margin: 8px 7px 1px; color: var(--muted); font-size: .78rem; line-height: 1.65; }

[hidden] { display: none !important; }

footer { color: var(--muted); font-size: .78rem; line-height: 1.7; margin: 16px 5px 0; }
footer a { color: inherit; }

/* ---------- tablet ---------- */

@media (max-width: 900px) {
  main { width: calc(100% - 24px); margin: 26px auto 48px; }
  header { align-items: stretch; flex-direction: column; gap: 16px; }
  .chart-card { padding: 14px; border-radius: 20px; }
  .stock-section { padding: 18px; border-radius: 20px; }
  .stock-section-head { align-items: stretch; flex-direction: column; gap: 14px; }
  .stock-search-wrap { width: 100%; }
  .panel-title { font-size: 14px; }
}

/* ---------- phone ---------- */

@media (max-width: 560px) {
  main { width: calc(100% - 16px); margin: 18px auto 36px; }
  h1 { font-size: clamp(1.7rem, 8.2vw, 2.35rem); line-height: 1.12; letter-spacing: -.035em; }
  .subtitle { font-size: .9rem; line-height: 1.65; }
  .desktop-only { display: none; }
  .mobile-only { display: inline; }

  .range-bar { display: block; padding: 12px 14px 10px; border-radius: 17px; }
  .range-bar > strong { display: block; margin: 0 0 8px; }
  .range-controls {
    width: 100%;
    flex-wrap: nowrap;
    gap: 4px;
    overflow-x: auto;
    overscroll-behavior-x: contain;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 2px;
  }
  .range-controls::-webkit-scrollbar { display: none; }
  .range-button { flex: 0 0 auto; min-height: 44px; padding: 10px 13px; border-radius: 11px; }
  .range-note { display: block; width:100%; margin: 7px 0 0; line-height: 1.5; }

  .etf-mode-bar {
    align-items: stretch;
    flex-direction: column;
    gap: 10px;
    padding: 12px 14px;
    border-radius: 17px;
  }
  .segmented-control { display: grid; grid-template-columns: 1fr 1fr; width: 100%; }
  .segment-button { min-height: 44px; }

  .cards { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .metric { padding: 13px; border-radius: 16px; }
  .metric strong { font-size: clamp(1rem, 4.8vw, 1.2rem); }

  .chart-card { padding: 10px; border-radius: 18px; }
  .chart-toolbar { align-items: flex-start; margin: 2px 4px 7px; }
  .chart-toolbar strong { font-size: .9rem; line-height: 1.45; }
  .hint { display: none; }

  .chart-tooltip {
    left: 8px !important;
    top: 44px !important;
    width: calc(100% - 16px);
    padding: 10px 12px;
    border-radius: 12px;
    font-size: .78rem;
    background: #15243b;
    box-shadow: 0 12px 30px rgba(21, 36, 59, .3);
  }
  .chart-tooltip b { margin-bottom: 5px; font-size: .85rem; }
  .tip-row { line-height: 1.65; }

  .legend { display: grid; grid-template-columns: 1fr; gap: 7px; margin: 8px 5px 2px; font-size: .76rem; }
  .legend span::before { width: 16px; margin-right: 6px; }

  .stock-section { margin-top: 14px; padding: 14px; border-radius: 18px; }
  .stock-section h2 { font-size: 1.3rem; line-height: 1.35; }
  .stock-section p { font-size: .88rem; line-height: 1.6; }
  .stock-search-form { display: grid; grid-template-columns: minmax(0, 1fr) auto; }
  .stock-search-form button { min-width: 72px; padding: 0 14px; }
  .suggestion { grid-template-columns: 60px minmax(0, 1fr) auto; gap: 8px; }
  .stock-range-bar { display: block; margin: 2px 4px 0; padding-top: 5px; }
  .stock-range-bar > strong { display: block; margin-bottom: 6px; }
  .stock-search-status { margin-bottom: 10px; }
  .methodology { margin-top: 7px; font-size: .74rem; }
  footer { margin-top: 14px; font-size: .74rem; }
}

@media (max-width: 340px) {
  .cards { grid-template-columns: 1fr; }
  .metric strong { font-size: 1.08rem; }
}

@media (prefers-reduced-motion: reduce) {
  .chart-tooltip { transition: none; }
}

/* ------------------------------------------------------------------ */
/* Watchlist                                                           */
/* ------------------------------------------------------------------ */

.watchlist-section {
  background: var(--card);
  border: 1px solid var(--grid);
  border-radius: 14px;
  padding: 18px 20px;
  margin-bottom: 18px;
}

.watchlist-head {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: 14px;
}

.watchlist-head h2 { margin: 0 0 4px; font-size: 1.05rem; }
.watchlist-head p { margin: 0; color: var(--muted); font-size: .78rem; line-height: 1.5; }
.watchlist-actions { display: flex; gap: 8px; flex-shrink: 0; }

.watchlist-add {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  margin-bottom: 12px;
}

.watchlist-add input {
  flex: 1 1 220px;
  min-width: 0;
  padding: 7px 10px;
  border: 1px solid var(--grid);
  border-radius: 8px;
  font: inherit;
  color: var(--ink);
}

.watchlist-add button {
  padding: 7px 14px;
  border: 1px solid var(--grid);
  border-radius: 8px;
  background: var(--twse);
  color: #fff;
  font: inherit;
  cursor: pointer;
}

.watchlist-status { color: var(--muted); font-size: .78rem; margin: 6px 0 0; }

/* The table is the one element here that cannot always fit a phone. */
.watchlist-table-wrap { overflow-x: auto; }

.watchlist-table { width: 100%; border-collapse: collapse; font-size: .86rem; }
.watchlist-table th,
.watchlist-table td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--grid);
  text-align: left;
  white-space: nowrap;
}
.watchlist-table th {
  color: var(--muted);
  font-weight: 600;
  font-size: .76rem;
}
.watchlist-table .num { text-align: right; font-variant-numeric: tabular-nums; }
.watchlist-table tr:last-child td { border-bottom: 0; }

.watchlist-name {
  border: 0;
  background: none;
  padding: 0;
  font: inherit;
  color: var(--twse);
  cursor: pointer;
  text-align: left;
}
.watchlist-name:hover { text-decoration: underline; }

.watchlist-mm { font-weight: 600; }
.watchlist-mm[data-state="ok"] { color: var(--ink); }
.watchlist-mm[data-state="warning"] { color: #b8730d; }
.watchlist-mm[data-state="danger"] { color: var(--danger); }
.watchlist-error { color: var(--danger); font-weight: 400; font-size: .78rem; }

.watchlist-controls { display: flex; gap: 4px; }
.watchlist-controls button {
  width: 26px;
  height: 26px;
  border: 1px solid var(--grid);
  border-radius: 6px;
  background: var(--paper);
  color: var(--ink);
  font: inherit;
  line-height: 1;
  cursor: pointer;
}
.watchlist-controls button:disabled { opacity: .35; cursor: default; }
.watchlist-controls .watchlist-remove { color: var(--danger); }

/* 紅漲綠跌 -- the Taiwan convention, inverted from most markets. */
.watchlist-change { font-variant-numeric: tabular-nums; }
.watchlist-change[data-dir="up"] { color: #c8323f; }
.watchlist-change[data-dir="dn"] { color: #1a9a5c; }
.watchlist-change[data-dir="flat"] { color: var(--muted); }

/*
 * Below 640px the seven columns stop fitting, and the horizontal scroll they
 * fall back to is unusable once the stock name has scrolled out of view. Each
 * row becomes a card instead: nothing is dropped, and the block fits a portrait
 * screenshot, which is how these get shared.
 */
@media (max-width: 640px) {
  .watchlist-section { padding: 14px; }
  .watchlist-table-wrap { overflow-x: visible; }
  .watchlist-table thead { display: none; }
  .watchlist-table,
  .watchlist-table tbody { display: block; }

  /*
   * Three columns, three rows. The two-column version ran 197px a card, so
   * fifteen of them needed four phone screens to capture -- too long for the
   * screenshot these get shared as. Row two holds the levels, row three the
   * changes.
   */
  .watchlist-table tr {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    grid-template-areas:
      "name  name mm"
      "price bal  use"
      "d1    d5   edit";
    gap: 5px 8px;
    padding: 9px 11px;
    margin-bottom: 7px;
    border: 1px solid var(--grid);
    border-radius: 10px;
    background: var(--paper);
  }
  .watchlist-table tr:last-child { margin-bottom: 0; }

  .watchlist-table td {
    border-bottom: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    gap: 1px;
    min-width: 0;
  }
  /* Header cells are gone on this tier, so each value carries its own label. */
  .watchlist-table td[data-label]::before {
    content: attr(data-label);
    color: var(--muted);
    font-size: .62rem;
    font-weight: 600;
    line-height: 1.25;
    letter-spacing: .02em;
  }
  .watchlist-table td.num { text-align: left; font-size: .88rem; line-height: 1.3; }

  .c-name  { grid-area: name; justify-content: center; }
  .c-mm    { grid-area: mm; align-items: flex-end; }
  .c-price { grid-area: price; }
  .c-bal   { grid-area: bal; }
  .c-d1    { grid-area: d1; }
  .c-d5    { grid-area: d5; }
  .c-use   { grid-area: use; align-items: flex-end; }
  .c-edit  { grid-area: edit; justify-content: flex-end; align-items: flex-end; }

  .watchlist-name { font-size: .92rem; font-weight: 700; }
  .c-mm { font-size: 1.02rem; }
  .c-mm::before,
  .c-use::before { align-self: flex-end; }

  .watchlist-controls { flex-direction: row; }
}
