/* 台股每日觀察 · Taiwan Stock Watchlist — dashboard styles.
   Maps onto the shared brand tokens in /assets/tokens.css.
   Default list served from data/report.json (GitHub Action); user-added tickers
   fetched live from /api/twwatch. User list persists in localStorage. */

:root {
  --bg:        var(--brand-bg);
  --surface:   var(--brand-surface);
  --border:    var(--brand-border);
  --text:      var(--brand-text);
  --muted:     var(--brand-text-muted);
  --gold:      var(--brand-gold);
  --accent:    var(--brand-accent);
  /* Taiwan convention: 紅漲綠跌 (red = up, green = down). */
  --up:        #ef4444;
  --down:      #22c55e;
  --radius:    16px;
  --row-bg:    #0c0c12;   /* opaque backing for the sticky first column */
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background:
    radial-gradient(1100px 620px at 78% -8%, rgba(99,102,241,0.16), transparent 60%),
    radial-gradient(900px 520px at 8% 108%, rgba(217,164,65,0.10), transparent 60%),
    var(--bg);
  color: var(--text);
  font-family: var(--brand-font-body);
  font-weight: 300;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

.container {
  max-width: 1120px;
  margin: 0 auto;
  padding: clamp(18px, 4vw, 30px) clamp(12px, 3vw, 22px) 72px;
}

.glass {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
}

/* ── Header ─────────────────────────────────────────────── */
.hero {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 20px;
  padding: clamp(16px, 3vw, 24px);
  margin-bottom: 16px;
}
.hero-main { min-width: 0; }
.back-link {
  display: inline-block;
  font-family: var(--brand-font-mono);
  font-size: 0.72rem;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--muted);
  text-decoration: none;
  margin-bottom: 10px;
  transition: color 0.2s var(--brand-ease);
}
.back-link:hover { color: var(--gold); }
.hero h1 {
  font-family: var(--brand-font-display);
  font-style: italic;
  font-weight: 400;
  font-size: clamp(2rem, 6vw, 3rem);
  margin: 0;
  line-height: 1.02;
}
.hero-sub { margin: 6px 0 0; color: var(--muted); font-size: clamp(0.8rem, 2.4vw, 0.9rem); }
.day-box { flex-shrink: 0; text-align: right; }
.day-label {
  font-family: var(--brand-font-mono);
  font-size: 0.62rem;
  letter-spacing: var(--brand-credit-tracking);
  text-transform: uppercase;
  color: var(--gold);
}
.day-val {
  font-family: var(--brand-font-head);
  font-weight: 600;
  font-size: clamp(1.2rem, 4vw, 1.5rem);
  margin-top: 4px;
  font-variant-numeric: tabular-nums;
}
.day-meta { font-size: 0.74rem; color: var(--muted); margin-top: 2px; }

/* ── Add bar ────────────────────────────────────────────── */
.add-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  margin-bottom: 14px;
  flex-wrap: wrap;
}
.add-bar input {
  background: rgba(255,255,255,0.03);
  border: 1px solid var(--border);
  border-radius: 9999px;
  color: var(--text);
  font-family: var(--brand-font-body);
  font-size: 0.92rem;
  padding: 10px 16px;
  outline: none;
}
.add-bar input:focus { border-color: var(--brand-accent-glow); }
.add-bar input::placeholder { color: var(--muted); }
#add-code { width: 170px; font-family: var(--brand-font-head); letter-spacing: 0.04em; }
#add-name { width: 150px; }
.add-msg { font-size: 0.82rem; color: var(--muted); }
.add-msg.err { color: var(--up); }
.add-msg.ok  { color: var(--down); }

/* ── Controls ───────────────────────────────────────────── */
.controls {
  display: flex;
  gap: 12px;
  margin-bottom: 14px;
  flex-wrap: wrap;
  align-items: center;
}
.search-wrap { flex: 1 1 220px; min-width: 0; padding: 2px 6px; border-radius: 9999px; }
.search-wrap input {
  width: 100%;
  background: transparent;
  border: none;
  outline: none;
  color: var(--text);
  font-family: var(--brand-font-body);
  font-size: 0.92rem;
  padding: 10px 14px;
}
.search-wrap input::placeholder { color: var(--muted); }
.control-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.pill {
  border-radius: 9999px;
  padding: 10px 16px;
  color: var(--text);
  font-family: var(--brand-font-mono);
  font-size: 0.7rem;
  letter-spacing: 0.06em;
  cursor: pointer;
  background: transparent;
  white-space: nowrap;
}
.pill:hover { color: var(--gold); }
button.pill { border: none; }

/* ── Table (compact — aims to fit a phone without horizontal scroll) ─ */
.tbl-wrap { overflow-x: auto; padding: 2px; -webkit-overflow-scrolling: touch; }
table.report {
  width: 100%;
  border-collapse: collapse;
  font-variant-numeric: tabular-nums;
}
.report th, .report td { padding: 8px 8px; text-align: left; white-space: nowrap; vertical-align: middle; }
.report thead th {
  position: sticky;
  top: 0;
  z-index: 3;
  background: rgba(11,11,17,0.92);
  backdrop-filter: blur(8px);
  font-family: var(--brand-font-mono);
  font-size: 0.62rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
  font-weight: 500;
  border-bottom: 1px solid var(--border);
}
.report th.num, .report td.num { text-align: right; }
.report tbody tr {
  border-bottom: 1px solid rgba(255,255,255,0.05);
  transition: background 0.15s var(--brand-ease);
}
.report tbody tr:hover { background: rgba(255,255,255,0.03); }

/* Sticky first column (股票: name + code) — stays visible on phone scroll. */
.report th.col-stock, .report td.col-stock {
  position: sticky;
  left: 0;
  z-index: 2;
  background: var(--row-bg);
  max-width: 96px;
}
.report thead th.col-stock { z-index: 4; }
.report tbody tr:hover td.col-stock { background: #14141c; }
.col-stock .nm { font-weight: 500; color: var(--text); font-size: 0.86rem; overflow: hidden; text-overflow: ellipsis; }
.col-stock .cd { font-family: var(--brand-font-mono); color: var(--muted); font-size: 0.68rem; margin-top: 1px; }

.report td.num { font-family: var(--brand-font-head); font-size: 0.9rem; }
.up   { color: var(--up); }
.down { color: var(--down); }
.flat { color: var(--muted); }

/* 收盤 cell filled when CLOSED at limit (broker-app style red/green box) */
.close-cell.lim-up   { color: #fff; background: var(--up);   border-radius: 6px; }
.close-cell.lim-down { color: #06210f; background: var(--down); border-radius: 6px; }

/* 漲跌 with 漲跌幅 stacked under it */
.chg-cell { line-height: 1.15; }
.chg-cell .pct { font-size: 0.72rem; opacity: 0.9; }

/* ── Manual marks column (★ ▲ ✓) — squeezed ─────────────── */
.col-mark { width: 1%; white-space: nowrap; }
.report th.col-mark { text-align: center; }
.report td.col-mark { padding: 8px 2px; }
.mk {
  background: transparent;
  border: none;
  cursor: pointer;
  font-size: 0.8rem;
  line-height: 1;
  padding: 2px 1px;
  color: #4b4b55;
  opacity: 0.5;
  transition: color 0.12s, opacity 0.12s, transform 0.08s;
}
.mk:hover { opacity: 1; transform: scale(1.2); }
.mk.on { opacity: 1; }
.mk-star.on { color: var(--gold); }
.mk-tri.on  { color: var(--brand-accent-2); }
.mk-chk.on  { color: var(--down); }

/* ── Merged high/low + time cell (earlier event on top) ──── */
.hl-cell { vertical-align: middle; line-height: 1.2; }
.hl-line { display: flex; align-items: center; justify-content: flex-end; gap: 5px; padding: 1px 0; }
.hl-line + .hl-line { border-top: 1px dashed rgba(255,255,255,0.06); }
.hl-lbl { font-family: var(--brand-font-mono); font-size: 0.56rem; opacity: 0.8; }
.hl-cell .up, .hl-cell .down { font-family: var(--brand-font-head); font-size: 0.82rem; }
.hl-tm { font-family: var(--brand-font-head); color: var(--muted); font-size: 0.72rem; min-width: 34px; text-align: right; }
.lim-dot { display: inline-block; width: 5px; height: 5px; border-radius: 50%; margin-left: 3px; vertical-align: middle; }
.hl-cell .up .lim-dot { background: var(--up); }
.hl-cell .down .lim-dot { background: var(--down); }

.report tr.pending td { color: var(--muted); font-style: italic; }

/* remove (✕) button */
.col-x { width: 1%; }
.rm-btn {
  background: transparent;
  border: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 0.9rem;
  line-height: 1;
  padding: 4px 4px;
  border-radius: 8px;
  opacity: 0.5;
  transition: color 0.15s, opacity 0.15s;
}
.rm-btn:hover { color: var(--up); opacity: 1; }

/* ── badges (still used in the methodology legend) ──────── */
.badge {
  display: inline-block;
  font-family: var(--brand-font-mono);
  font-size: 0.6rem;
  letter-spacing: 0.05em;
  border-radius: 9999px;
  padding: 2px 8px;
  margin-left: 4px;
  vertical-align: middle;
  white-space: nowrap;
}
.badge-up        { background: var(--up); color: #fff; }
.badge-down      { background: var(--down); color: #06210f; }
.badge-up-soft   { color: var(--up); border: 1px solid rgba(239,68,68,0.5); }
.badge-down-soft { color: var(--down); border: 1px solid rgba(34,197,94,0.5); }
.note-chip {
  display: inline-block;
  font-family: var(--brand-font-mono);
  font-size: 0.64rem;
  letter-spacing: 0.06em;
  color: var(--gold);
  border: 1px solid var(--brand-gold-glow);
  border-radius: 9999px;
  padding: 2px 10px;
}
.empty { text-align: center; color: var(--muted); padding: 40px 0; }

/* ── Footnote + methodology ─────────────────────────────── */
.footnote { color: var(--muted); font-size: 0.78rem; margin: 14px 2px 0; }
.methodology { margin-top: 20px; padding: 6px clamp(14px, 3vw, 22px); }
.methodology summary {
  cursor: pointer;
  padding: 14px 0;
  font-family: var(--brand-font-mono);
  font-size: 0.72rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--gold);
}
.method-body { padding: 4px 0 18px; color: var(--muted); font-size: 0.9rem; }
.method-body p { margin: 0 0 10px; }
.method-body strong { color: var(--text); font-weight: 600; }
.method-body .badge { margin-left: 2px; }
.method-body .mk { opacity: 1; padding: 0 2px; }
.method-body .mk-star { color: var(--gold); }
.method-body .mk-tri  { color: var(--brand-accent-2); }
.method-body .mk-chk  { color: var(--down); }
.method-body .muted { font-size: 0.8rem; opacity: 0.8; }

/* ── Admin bar (add-stock + password login side by side) ──── */
.admin-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px 20px;
  padding: 10px 14px;
  margin-bottom: 14px;
  flex-wrap: wrap;
}
.admin-add, .admin-auth {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.admin-add { flex: 1 1 auto; }
.admin-auth { flex: 0 0 auto; }
.admin-bar input {
  background: rgba(255,255,255,0.04);
  border: 1px solid var(--border);
  border-radius: 9999px;
  color: var(--text);
  font-family: var(--brand-font-body);
  font-size: 0.92rem;
  padding: 10px 16px;
  outline: none;
}
.admin-bar input:focus { border-color: var(--brand-accent); }
.admin-bar input:disabled { opacity: 0.45; cursor: not-allowed; }
#add-code { width: 200px; font-family: var(--brand-font-head); letter-spacing: 0.04em; }
#admin-pw { width: 150px; }
.auth-tag {
  font-family: var(--brand-font-mono);
  font-size: 0.66rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--muted);
}
.add-msg { font-size: 0.82rem; color: var(--muted); flex-basis: 100%; }
.add-msg.err { color: var(--up); }
.add-msg.ok  { color: var(--down); }

.mk.ro { cursor: default; }
.mk.ro:hover { transform: none; opacity: 0.55; }
.mk.ro.on:hover { opacity: 1; }
body:not(.can-edit) .col-x { width: 0; padding: 0; }

/* ── Logged-in (admin) theme — light purple, clearly different ─ */
body.is-admin {
  background:
    radial-gradient(1100px 640px at 78% -8%, rgba(168,140,255,0.30), transparent 60%),
    radial-gradient(940px 560px at 6% 108%, rgba(139,92,246,0.20), transparent 62%),
    #0f0b1a;
}
body.is-admin .glass {
  border-color: rgba(180,150,255,0.28);
  background: rgba(150,110,255,0.07);
}
body.is-admin .admin-bar {
  border-color: rgba(180,150,255,0.5);
  background: rgba(150,110,255,0.12);
  box-shadow: 0 0 0 1px rgba(168,140,255,0.15), 0 8px 30px rgba(120,80,220,0.18);
}
body.is-admin .auth-tag { color: #c7b3ff; }
body.is-admin .hero h1 { color: #efe8ff; }
body.is-admin .day-label { color: #c7b3ff; }
body.is-admin .report thead th { background: rgba(24,16,40,0.94); }
/* sticky first column must match the purple theme, not stay black */
body.is-admin .report td.col-stock { background: #170f28; }
body.is-admin .report thead th.col-stock { background: rgba(24,16,40,0.94); }
body.is-admin .report tbody tr:hover td.col-stock { background: #1f1638; }
body.is-admin .col-stock .cd { color: #a892d6; }
body.is-admin .mk:not(.on):not(.ro) { color: #6b5a9a; opacity: 0.7; }

/* ── Historical-view marker: highlight the date pill ────── */
#hist-back { color: var(--gold); border: 1px solid var(--brand-gold-glow); border-radius: 9999px; }

/* ── Single-stock history panel ─────────────────────────── */
.hist-panel { padding: 14px; margin-bottom: 16px; }
.hist-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 8px;
}
.hist-title { font-family: var(--brand-font-head); font-weight: 600; font-size: 1rem; }
.hist-ranges { display: flex; gap: 6px; flex-wrap: wrap; }
.hist-ranges .pill { padding: 7px 12px; font-size: 0.66rem; }
.range-btn.on { color: var(--gold); border: 1px solid var(--brand-gold-glow); border-radius: 9999px; }
.hist-table { min-width: 560px; }
.hist-panel .tbl-wrap { max-height: 420px; overflow-y: auto; }

/* ── Calendar ───────────────────────────────────────────── */
.cal-anchor { position: relative; }
.calendar {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  z-index: 50;
  padding: 12px;
  width: 272px;
  background: rgba(12,12,18,0.97);
}
.cal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-family: var(--brand-font-head);
  font-size: 0.9rem;
  margin-bottom: 8px;
}
.cal-nav {
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 1.1rem;
  cursor: pointer;
  padding: 2px 10px;
}
.cal-nav:hover { color: var(--gold); }
.cal-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 2px;
  text-align: center;
}
.cal-dow {
  font-family: var(--brand-font-mono);
  font-size: 0.6rem;
  color: var(--muted);
  padding: 4px 0;
}
.cal-day {
  border: none;
  background: transparent;
  color: var(--text);
  font-family: var(--brand-font-head);
  font-size: 0.8rem;
  padding: 6px 0;
  border-radius: 8px;
  cursor: pointer;
}
.cal-day:hover { background: rgba(255,255,255,0.08); color: var(--gold); }
.cal-day.cur { background: var(--brand-gold-glow); color: var(--gold); }
.cal-day.off { color: #3a3a42; cursor: default; }

/* ── Responsive ─────────────────────────────────────────── */
@media (max-width: 720px) {
  .hero { flex-direction: column; align-items: flex-start; gap: 12px; }
  .day-box { text-align: left; }
  #add-code, #add-name { flex: 1 1 120px; width: auto; }
  .add-msg { flex-basis: 100%; }
  .search-wrap { flex-basis: 100%; }
  .control-actions { width: 100%; }
  .control-actions .pill { flex: 1 1 auto; text-align: center; }
}
/* Phone: squeeze every column so the whole row fits without horizontal scroll. */
@media (max-width: 480px) {
  .container { padding-left: 8px; padding-right: 8px; }
  .tbl-wrap { padding: 0; }
  .report th, .report td { padding: 7px 5px; }
  .report thead th { font-size: 0.56rem; letter-spacing: 0; }
  .report td.num { font-size: 0.82rem; }
  .col-stock .nm { font-size: 0.8rem; }
  .col-stock .cd { font-size: 0.64rem; }
  .report th.col-stock, .report td.col-stock { max-width: 74px; }
  .hl-cell .up, .hl-cell .down { font-size: 0.76rem; }
  .hl-tm { font-size: 0.66rem; min-width: 30px; }
  .chg-cell .pct { font-size: 0.66rem; }
  .mk { font-size: 0.74rem; }
}
@media (max-width: 360px) {
  .report th, .report td { padding: 6px 3px; }
  .hl-lbl { display: none; }   /* drop 高/低 labels on the very narrowest phones */
}
