:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --text: #111827;
  --muted: #6b7280;
  --line: #e5e7eb;
  --accent: #2563eb;
  --up: #dc2626;   /* 台股慣例：漲紅 */
  --down: #16a34a; /* 跌綠 */
  --flat: #6b7280;
  --hover: #f1f5f9;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b1120; --panel: #111827; --text: #e5e7eb; --muted: #9ca3af;
    --line: #1f2937; --accent: #60a5fa; --up: #f87171; --down: #4ade80; --hover: #1e293b;
  }
}
:root[data-theme="dark"] {
  --bg: #0b1120; --panel: #111827; --text: #e5e7eb; --muted: #9ca3af;
  --line: #1f2937; --accent: #60a5fa; --up: #f87171; --down: #4ade80; --hover: #1e293b;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
  font-variant-numeric: tabular-nums;
  padding-bottom: env(safe-area-inset-bottom);
}
button { font: inherit; color: inherit; cursor: pointer; }

.topbar {
  display: flex; align-items: baseline; gap: 12px;
  padding: calc(12px + env(safe-area-inset-top)) 16px 12px;
  background: var(--panel); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 5;
}
.topbar h1 { font-size: 18px; margin: 0; }
.status { font-size: 12px; color: var(--muted); margin-left: auto; }

.layout { display: grid; gap: 16px; padding: 16px; max-width: 1200px; margin: 0 auto; }
@media (min-width: 900px) {
  .layout { grid-template-columns: 360px 1fr; align-items: start; }
  .detail { position: sticky; top: 72px; }
}

.watch, .detail { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; }
.watch { overflow: visible; }

.add { display: flex; gap: 8px; padding: 12px; position: relative; border-bottom: 1px solid var(--line); }
.add input {
  flex: 1; min-width: 0; padding: 10px 12px; font: inherit; color: var(--text);
  background: var(--bg); border: 1px solid var(--line); border-radius: 8px;
}
.add input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.add button[type=submit] {
  padding: 0 16px; border: 0; border-radius: 8px; background: var(--accent); color: #fff; font-weight: 600;
}
.suggest {
  position: absolute; left: 12px; right: 12px; top: calc(100% - 6px); z-index: 10;
  list-style: none; margin: 0; padding: 4px; max-height: 280px; overflow: auto;
  background: var(--panel); border: 1px solid var(--line); border-radius: 8px;
  box-shadow: 0 8px 24px rgb(0 0 0 / .15);
}
.suggest li { padding: 8px 10px; border-radius: 6px; cursor: pointer; display: flex; gap: 10px; }
.suggest li b { min-width: 56px; }
.suggest li small { margin-left: auto; color: var(--muted); }
.suggest li[aria-selected="true"], .suggest li:hover { background: var(--hover); }

.list { list-style: none; margin: 0; padding: 0; }
.item {
  display: grid; grid-template-columns: 24px 1fr auto 28px; align-items: center; gap: 8px;
  padding: 10px 12px 10px 4px; border-bottom: 1px solid var(--line); cursor: pointer; user-select: none;
}
.item:last-child { border-bottom: 0; }
.item:hover { background: var(--hover); }
.item.active { background: var(--hover); box-shadow: inset 3px 0 0 var(--accent); }
.item.dragging { opacity: .6; background: var(--hover); }
.handle {
  color: var(--muted); text-align: center; cursor: grab; touch-action: none; font-size: 18px; line-height: 1;
  padding: 8px 0;
}
.item .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .code { font-size: 12px; color: var(--muted); }
.item .px { text-align: right; }
.item .px .p { font-weight: 600; font-size: 16px; }
.item .px .c { font-size: 12px; }
.del {
  border: 0; background: none; color: var(--muted); font-size: 18px; width: 28px; height: 28px; border-radius: 6px;
}
.del:hover { background: var(--line); color: var(--up); }
.up { color: var(--up); } .down { color: var(--down); } .flat { color: var(--flat); }
.empty { padding: 24px 16px; color: var(--muted); text-align: center; margin: 0; }

.detail { padding: 16px; min-width: 0; scroll-margin-top: 64px; }
.detail-head { display: flex; flex-wrap: wrap; gap: 12px; justify-content: space-between; align-items: flex-end; }
.detail-title { font-size: 18px; font-weight: 700; }
.detail-title small { color: var(--muted); font-weight: 400; }
.detail-price { font-size: 26px; font-weight: 700; }
.detail-price #dChange { font-size: 15px; font-weight: 600; }
.ranges { display: flex; flex-wrap: wrap; gap: 4px; }
.ranges button {
  border: 1px solid var(--line); background: none; padding: 4px 10px; border-radius: 6px; font-size: 13px;
}
.ranges button.on { background: var(--accent); border-color: var(--accent); color: #fff; }
.ohlc { display: flex; flex-wrap: wrap; gap: 4px 16px; margin: 12px 0 4px; font-size: 13px; }
.ohlc div { display: flex; gap: 4px; } .ohlc dt { color: var(--muted); } .ohlc dd { margin: 0; }
.legend { font-size: 12px; color: var(--muted); display: flex; gap: 6px; align-items: center; margin: 4px 0; }
.legend i { display: inline-block; width: 12px; height: 2px; margin-left: 6px; }
.legend .ma5 { background: #f59e0b; } .legend .ma20 { background: #8b5cf6; } .legend .ma60 { background: #0ea5e9; }
.chart { height: 420px; width: 100%; }
@media (max-width: 600px) { .chart { height: 340px; } }
.note { font-size: 12px; color: var(--muted); margin: 8px 0 0; }

.foreign { margin-top: 20px; padding-top: 16px; border-top: 1px solid var(--line); }
.foreign h3 { margin: 0 0 8px; font-size: 16px; }
.foreign h3 small { font-weight: 400; color: var(--muted); font-size: 12px; }
.fsum { display: flex; flex-wrap: wrap; gap: 6px 18px; font-size: 14px; margin-bottom: 8px; }
.fsum div { display: flex; gap: 6px; } .fsum dt { color: var(--muted); } .fsum dd { margin: 0; font-weight: 600; }
.fchart { height: 160px; width: 100%; }
.ftable { margin-top: 8px; font-size: 13px; }
.ftable summary { cursor: pointer; color: var(--accent); padding: 4px 0; }
.tablewrap { overflow-x: auto; }
.ftable table { width: 100%; border-collapse: collapse; white-space: nowrap; }
.ftable th, .ftable td { padding: 6px 8px; text-align: right; border-bottom: 1px solid var(--line); }
.ftable th:first-child, .ftable td:first-child { text-align: left; }
.ftable th { color: var(--muted); font-weight: 500; }
.fsum .warn { color: var(--muted); font-size: 12px; width: 100%; }

.syncbtn { border: 1px solid var(--line); background: none; border-radius: 8px; padding: 4px 10px; font-size: 13px; }
.syncbtn.on { border-color: var(--accent); color: var(--accent); }
.dlg {
  border: 1px solid var(--line); border-radius: 12px; padding: 20px; width: min(420px, calc(100vw - 32px));
  background: var(--panel); color: var(--text); position: relative;
}
.dlg::backdrop { background: rgb(0 0 0 / .4); }
.dlg h2 { margin: 0 0 8px; font-size: 18px; }
.dlg p { margin: 8px 0; font-size: 14px; }
.dlg button { border: 1px solid var(--line); background: none; border-radius: 8px; padding: 8px 14px; }
.dlg .primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; width: 100%; }
.joinform { display: flex; gap: 8px; margin-top: 12px; }
.joinform input {
  flex: 1; min-width: 0; padding: 8px 10px; font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--line); border-radius: 8px; text-transform: uppercase; letter-spacing: 1px;
}
.dlg .code { display: flex; align-items: center; gap: 10px; font-size: 22px; font-weight: 700; letter-spacing: 2px; margin: 8px 0; }
.dlg .code button { font-size: 13px; letter-spacing: 0; font-weight: 400; }
.dlg .hint { color: var(--muted); font-size: 12px; }
.dlg .msg { color: var(--accent); min-height: 1.2em; }
.dlg .close { position: absolute; top: 10px; right: 10px; border: 0; font-size: 20px; padding: 2px 8px; }
