:root {
  --bg: #f5f6f8;
  --card: #ffffff;
  --ink: #1a1d23;
  --muted: #6b7280;
  --line: #e5e7eb;
  --up: #d92b2b;
  --down: #0a8f4c;
  --accent: #2563eb;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.45 "Segoe UI", "Microsoft YaHei", system-ui, sans-serif;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 20px;
  background: #fff;
  border-bottom: 1px solid var(--line);
}

.brand { display: flex; align-items: baseline; gap: 10px; }
.brand h1 { margin: 0; font-size: 18px; letter-spacing: 0.5px; }
.brand .sub { color: var(--muted); font-size: 12px; }

.market-switch {
  display: flex;
  align-items: center;
  gap: 3px;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f1f3f7;
}
.market-btn {
  border: 0;
  border-radius: 6px;
  padding: 5px 10px;
  background: transparent;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
}
.market-btn:hover { color: var(--accent); }
.market-btn.active {
  background: #fff;
  color: var(--accent);
  box-shadow: 0 1px 4px rgba(15, 23, 42, .12);
}

.stock-search {
  position: relative;
  flex: 1;
  min-width: 210px;
  max-width: 440px;
}
.stock-search input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 11px;
  background: #f8f9fb;
}
.stock-search input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, .12);
  background: #fff;
}
.search-results {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  right: 0;
  z-index: 60;
  max-height: 360px;
  overflow: auto;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 9px;
  box-shadow: 0 14px 34px rgba(15, 23, 42, .16);
}
.search-result {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 8px;
  border: 0;
  border-bottom: 1px solid #f1f2f4;
  background: #fff;
  padding: 9px 11px;
  text-align: left;
  cursor: pointer;
}
.search-result:last-child { border-bottom: 0; }
.search-result:hover, .search-result.active { background: #f3f7ff; }
.ss-code {
  width: 54px;
  flex: none;
  color: var(--muted);
  font-family: Consolas, monospace;
  font-size: 12px;
}
.ss-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 600;
}
.ss-ind {
  flex: none;
  max-width: 74px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--muted);
  font-size: 11px;
}
.ss-pct {
  width: 58px;
  flex: none;
  text-align: right;
  font-variant-numeric: tabular-nums;
  font-size: 12px;
}
.ss-empty { padding: 14px 12px; color: var(--muted); font-size: 12px; text-align: center; }

.topbar-right { display: flex; align-items: center; gap: 12px; }
.status { color: var(--muted); font-size: 12px; }
.auto { color: var(--muted); font-size: 12px; display: flex; align-items: center; gap: 6px; }

.btn {
  border: 1px solid var(--accent);
  background: var(--accent);
  color: #fff;
  border-radius: 6px;
  padding: 6px 14px;
  font-size: 13px;
  cursor: pointer;
}
.btn:hover { filter: brightness(1.05); }
.btn:disabled { opacity: .55; cursor: default; }
.btn.ghost { background: #fff; color: var(--accent); }

select, input { font: inherit; color: inherit; }
select {
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 4px 8px;
  background: #fff;
}

main { padding: 16px 20px 40px; max-width: 1600px; margin: 0 auto; }

.market {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 14px;
  margin-bottom: 12px;
}

.mkt-stats {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 18px;
  font-size: 13px;
  color: var(--muted);
}
.mkt-stats .mkt b { font-size: 15px; font-variant-numeric: tabular-nums; }
.mkt-stats .mkt b.up { color: var(--up); }
.mkt-stats .mkt b.down { color: var(--down); }
.mkt-stats .mkt b.flat { color: var(--ink); }

.heat-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px dashed var(--line);
  min-width: 0;
}
.heat-label { color: var(--muted); font-size: 12px; flex: none; }
.heat-chips {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 2px;
  scrollbar-width: thin;
}
.heat-chip {
  flex: none;
  display: flex;
  align-items: baseline;
  gap: 6px;
  border: 1px solid var(--line);
  background: #fafafa;
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 12px;
  cursor: pointer;
  white-space: nowrap;
}
.heat-chip:hover { border-color: var(--accent); }
.heat-chip .hn { color: var(--ink); }
.heat-chip .hp { font-variant-numeric: tabular-nums; }
.heat-chip .hc { color: var(--muted); font-size: 11px; }
.heat-chip.cold .hn { color: var(--muted); }

.panel {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 14px 16px 16px;
}

.presets { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; }
.presets-label { color: var(--muted); font-size: 12px; margin-right: 2px; }
.preset-note { color: var(--muted); font-size: 12px; margin-top: 8px; min-height: 16px; }

.saved-row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}
.saved-chips { display: flex; flex-wrap: wrap; gap: 6px; }
.saved-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--line);
  background: #f3f7ff;
  color: var(--accent);
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 12px;
  cursor: pointer;
}
.saved-chip:hover { border-color: var(--accent); }
.saved-chip .x {
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 12px;
  padding: 0 2px;
  line-height: 1;
}
.saved-chip .x:hover { color: var(--up); }

.chip {
  border: 1px solid var(--line);
  background: #fafafa;
  border-radius: 999px;
  padding: 5px 12px;
  font-size: 13px;
  cursor: pointer;
}
.chip:hover { border-color: var(--accent); color: var(--accent); }
.chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.chip.ghost { color: var(--muted); }

.filters {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 12px 16px;
  margin-top: 14px;
  padding-top: 14px;
  border-top: 1px dashed var(--line);
}

.field { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.field.wide { grid-column: span 2; }
.field > label { color: var(--muted); font-size: 12px; }

.field input[type="number"], .field input[type="search"] {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 6px 9px;
  background: #fff;
  min-width: 0;
}
.field input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, .12);
}

.range { display: flex; align-items: center; gap: 6px; }
.range span { color: var(--muted); flex: none; }

.checks { justify-content: flex-end; gap: 8px; }
.check { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--ink); white-space: nowrap; }

.resultbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 14px 2px 8px;
}
.resultbar .actions { display: flex; gap: 8px; }
.counts { color: var(--muted); font-size: 13px; }
.counts strong { color: var(--ink); font-size: 16px; }
.muted { color: var(--muted); }
.hint { color: var(--muted); font-size: 12px; margin-left: 10px; opacity: .85; }

.table-wrap {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: auto;
  max-height: calc(100vh - 260px);
  min-height: 340px;
}

table { border-collapse: separate; border-spacing: 0; width: 100%; font-size: 13px; }

thead th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: #fbfbfc;
  border-bottom: 1px solid var(--line);
  text-align: right;
  padding: 9px 10px;
  font-weight: 600;
  color: #374151;
  white-space: nowrap;
  cursor: pointer;
  user-select: none;
}
thead th:hover { color: var(--accent); }
thead th.na { color: #b9bcc4; }
thead th.sorted::after { content: " \25B2"; font-size: 10px; }
thead th.sorted.desc::after { content: " \25BC"; }
thead th:nth-child(2), thead th:nth-child(3) { text-align: left; }

tbody td {
  padding: 7px 10px;
  border-bottom: 1px solid #f1f2f4;
  text-align: right;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}
tbody td:nth-child(2), tbody td:nth-child(3) { text-align: left; }
tbody tr:hover { background: #f7f9ff; }

.code { color: var(--muted); font-family: Consolas, monospace; }
.name { font-weight: 600; }
.up { color: var(--up); }
.down { color: var(--down); }
.flat { color: var(--muted); }
.tag { color: var(--muted); font-size: 12px; }

.empty { padding: 48px; text-align: center; color: var(--muted); }

.modal {
  position: fixed;
  inset: 0;
  z-index: 70;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(15, 23, 42, .38);
}
.modal-card {
  width: min(420px, 100%);
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 22px 60px rgba(15, 23, 42, .25);
}
.modal-title { font-size: 17px; font-weight: 700; }
.modal-sub { color: var(--muted); font-size: 12px; line-height: 1.6; margin-top: 5px; }
.modal-card input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 8px 10px;
  margin-top: 13px;
}
.modal-card input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, .12);
}
.modal-error { min-height: 18px; color: var(--up); font-size: 12px; margin-top: 4px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }

.drawer {
  position: fixed;
  top: 0;
  right: 0;
  z-index: 40;
  width: 520px;
  max-width: 100vw;
  height: 100vh;
  overflow: auto;
  background: #fff;
  border-left: 1px solid var(--line);
  box-shadow: -12px 0 32px rgba(15, 23, 42, .12);
  padding: 16px 18px 24px;
}

.drawer-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}
.d-title { font-size: 18px; font-weight: 700; }
.d-sub { color: var(--muted); font-size: 12px; margin-top: 3px; }

.d-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin: 14px 0 10px;
}
.d-stat {
  background: #f8f9fb;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
}
.d-stat .k { color: var(--muted); font-size: 11px; }
.d-stat .v { font-size: 15px; font-weight: 600; margin-top: 2px; font-variant-numeric: tabular-nums; }

.d-canvas {
  width: 100%;
  height: 240px;
  display: block;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.d-legend {
  display: flex;
  gap: 14px;
  margin-top: 8px;
  font-size: 11px;
  color: var(--muted);
}
.d-legend .lg::before {
  content: "";
  display: inline-block;
  width: 12px;
  height: 3px;
  margin-right: 5px;
  vertical-align: middle;
  border-radius: 2px;
  background: currentColor;
}
.d-legend .close { color: #1f2937; }
.d-legend .ma5 { color: #f59e0b; }
.d-legend .ma20 { color: #7c3aed; }
.d-legend .vol { color: #9ca3af; }

.d-note { margin-top: 12px; color: var(--muted); font-size: 11px; line-height: 1.6; }

tbody tr { cursor: pointer; }
tbody tr.active { background: #eef4ff; }

.foot { margin-top: 14px; color: var(--muted); font-size: 12px; text-align: center; }

.cmp-panel { margin-top: 12px; scroll-margin-top: 74px; }
.cmp-head {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
}
.cmp-heading { display: flex; align-items: baseline; flex-wrap: wrap; gap: 10px; }
.cmp-title { font-weight: 700; font-size: 15px; }
.cmp-actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  margin-left: auto;
}
.cmp-count { color: var(--muted); font-size: 12px; margin-right: 2px; }

.cmp-picker {
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
  margin-top: 11px;
}
.cmp-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fafafa;
  padding: 5px 10px;
  font-size: 12px;
  cursor: pointer;
  user-select: none;
}
.cmp-chip:hover { border-color: var(--accent); }
.cmp-chip.selected { border-color: rgba(37, 99, 235, .45); background: #eef4ff; }
.cmp-chip.disabled { opacity: .48; cursor: not-allowed; }
.cmp-chip input { margin: 0; accent-color: var(--accent); }
.cmp-code { color: var(--muted); font-family: Consolas, monospace; font-size: 11px; }
.cmp-name { font-weight: 600; }
.cmp-pct { font-size: 11px; font-variant-numeric: tabular-nums; }
.cmp-empty { color: var(--muted); font-size: 12px; }

.cmp-chart-title {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 8px 12px;
  margin: 12px 0 8px;
  font-size: 13px;
}
.cmp-range { color: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.cmp-tip { color: var(--muted); font-size: 11px; margin-left: auto; }
.cmp-canvas { height: 280px; }
.cmp-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 7px 14px;
  margin-top: 8px;
  color: var(--muted);
  font-size: 11px;
}
.cmp-lg { display: inline-flex; align-items: center; gap: 5px; color: var(--ink); }
.cmp-lg i {
  width: 14px;
  height: 3px;
  border-radius: 2px;
  background: var(--c);
}
.cmp-table {
  margin-top: 12px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
}
.cmp-table table { font-size: 12px; }
.cmp-table th { cursor: default; text-align: right; }
.cmp-table th:first-child { text-align: left; }
.cmp-table td { text-align: right; padding: 8px 10px; }
.cmp-table td:first-child { text-align: left; }
.cmp-table tbody tr:hover { background: #f7f9ff; }
.cmp-stock-name { font-weight: 600; white-space: nowrap; }
.cmp-stock-code { color: var(--muted); font-family: Consolas, monospace; font-size: 10px; margin-top: 1px; }
.cmp-best { font-weight: 700; box-shadow: inset 0 -2px 0 rgba(37, 99, 235, .22); }

.bt-panel { margin-top: 12px; }
.bt-head { display: flex; align-items: center; flex-wrap: wrap; gap: 10px; }
.bt-title { font-weight: 700; font-size: 15px; }
.bt-sub { color: var(--muted); font-size: 12px; }
.bt-controls {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  margin-left: auto;
}
.bt-controls label { color: var(--muted); font-size: 12px; display: flex; align-items: center; gap: 6px; }
.bt-status { color: var(--muted); font-size: 12px; margin-top: 10px; }
.bt-status.error { color: var(--up); }
#btResult { margin-top: 12px; }
.bt-table { margin-top: 12px; overflow: auto; }
.bt-table table { font-size: 12px; }
.bt-table th { cursor: default; text-align: right; }
.bt-table th:nth-child(1), .bt-table th:nth-child(2) { text-align: left; }

thead th.star, tbody td.star {
  width: 34px;
  text-align: center;
  padding: 6px 4px;
}
thead th.star { cursor: default; }
.star-btn {
  border: none;
  background: transparent;
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
  color: #c9ccd3;
  padding: 2px 4px;
}
.star-btn.on { color: #f59e0b; }

@media (max-width: 720px) {
  .field.wide { grid-column: span 1; }
  main { padding: 12px 10px 32px; }
  .topbar { flex-wrap: wrap; }
  .topbar-right { order: 1; margin-left: auto; }
  .market-switch { order: 2; flex-basis: 100%; justify-content: center; }
  .stock-search { order: 3; flex-basis: 100%; max-width: none; }
  .drawer { width: 100vw; }
  .d-stats { grid-template-columns: repeat(2, 1fr); }
  .cmp-actions { width: 100%; margin-left: 0; }
  .cmp-tip { flex-basis: 100%; margin-left: 0; }
  .cmp-canvas { height: 240px; }
}

@media (min-width: 721px) and (max-width: 1080px) {
  .topbar { flex-wrap: wrap; }
  .stock-search { order: 3; flex-basis: 100%; max-width: none; }
}
