:root {
  --bg: #f4f6f9; --panel: #fff; --border: #dde3ea; --text: #1b2430; --muted: #66727f;
  --brand: #0f3d75; --up: #16803c; --down: #c62828; --up-bg: #e3f6e8; --down-bg: #fdeaea;
  --ema9: #2962ff; --sma20: #ff9800; --sma200: #9c27b0;
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; color: var(--text); background: var(--bg); font-size: 14px; }
button { font: inherit; }
.muted { color: var(--muted); font-size: 13px; }
.up { color: var(--up); }
.down { color: var(--down); }

/* ---------- login ---------- */
.login-body { display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 16px;
  background: linear-gradient(135deg, #0f3d75 0%, #1b5aa6 55%, #e9eef5 55%); }
.login-card { width: 100%; max-width: 380px; background: var(--panel); border-radius: 14px; padding: 28px;
  box-shadow: 0 12px 40px rgba(15, 61, 117, .25); }
.brand { display: flex; gap: 12px; align-items: center; margin-bottom: 20px; }
.brand h1 { font-size: 20px; margin: 0; }
.brand p { margin: 2px 0 0; }
.login-card form { display: flex; flex-direction: column; }
.login-card label { font-weight: 600; font-size: 13px; margin: 12px 0 5px; }
.login-card input { padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; font-size: 15px; }
.login-card input:focus { outline: 2px solid #1b5aa6; outline-offset: -1px; border-color: transparent; }
#captcha_answer { text-transform: uppercase; letter-spacing: 3px; font-family: ui-monospace, Consolas, monospace; margin-top: 8px; }
.captcha-row { display: flex; align-items: center; gap: 8px; }
.captcha-row img { border: 1px solid var(--border); border-radius: 8px; }
.icon-btn { width: 40px; height: 40px; border: 1px solid var(--border); border-radius: 8px; background: #fff; font-size: 20px; cursor: pointer; }
.primary { margin-top: 20px; padding: 11px; border: 0; border-radius: 8px; background: var(--brand); color: #fff; font-weight: 600; font-size: 15px; cursor: pointer; }
.primary:hover { background: #154f96; }
.alert { background: var(--down-bg); color: var(--down); border: 1px solid #f5c2c2; padding: 10px 12px; border-radius: 8px; font-size: 13px; }

/* ---------- app shell ---------- */
.app-body { display: flex; flex-direction: column; height: 100vh; overflow: hidden; }
.topbar { display: flex; align-items: center; gap: 16px; padding: 8px 16px; background: var(--brand); color: #fff; }
.topbar .muted { color: #c7d6ea; }
.brand-sm { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 16px; }
.user { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.user form { margin: 0; }
.ghost { padding: 5px 12px; border: 1px solid var(--border); background: transparent; border-radius: 6px; cursor: pointer; color: inherit; }
.topbar .ghost { border-color: rgba(255,255,255,.4); }
.topbar .ghost:hover { background: rgba(255,255,255,.12); }

#layout { flex: 1; display: flex; min-height: 0; }
#sidebar { width: 440px; min-width: 340px; display: flex; flex-direction: column; background: var(--panel); border-right: 1px solid var(--border); }
.filters { padding: 10px; border-bottom: 1px solid var(--border); display: flex; flex-direction: column; gap: 8px; }
.filters input { padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; font-size: 14px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { padding: 3px 10px; border: 1px solid var(--border); border-radius: 999px; background: #fff; cursor: pointer; font-size: 12px; }
.chip.active { background: var(--brand); color: #fff; border-color: var(--brand); }
.chip .n { opacity: .7; margin-left: 3px; }
.table-wrap { flex: 1; overflow-y: auto; }
#count { padding: 6px 10px; border-top: 1px solid var(--border); }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th { position: sticky; top: 0; z-index: 1; background: #f7f9fb; text-align: left; padding: 7px 8px; border-bottom: 1px solid var(--border); cursor: pointer; user-select: none; white-space: nowrap; }
th.sorted::after { content: " ▾"; }
th.sorted.asc::after { content: " ▴"; }
td { padding: 6px 8px; border-bottom: 1px solid #eef1f4; white-space: nowrap; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
tbody tr { cursor: pointer; }
tbody tr:hover { background: #f1f6fd; }
tbody tr.selected { background: #dce9fb; }
td.code { font-weight: 700; }
td.code small { display: block; font-weight: 400; color: var(--muted); max-width: 180px; overflow: hidden; text-overflow: ellipsis; }
.badge { display: inline-block; padding: 1px 7px; border-radius: 10px; font-size: 11px; font-weight: 600; }
.badge.up { background: var(--up-bg); }
.badge.down { background: var(--down-bg); }
.badge.na { background: #eef1f4; color: var(--muted); }

/* ---------- detail ---------- */
#detail { flex: 1; min-width: 0; display: flex; flex-direction: column; padding: 14px 18px; overflow: auto; }
.placeholder { margin: auto; text-align: center; font-size: 16px; }
#stockView { display: flex; flex-direction: column; flex: 1; min-height: 0; }
#stockView[hidden] { display: none; }
.detail-head { display: flex; align-items: flex-start; gap: 12px; flex-wrap: wrap; }
.title-block { flex: 1; min-width: 200px; }
.title-block h2 { margin: 0; font-size: 24px; }
.price { font-variant-numeric: tabular-nums; margin-left: 6px; }
#chg { font-size: 15px; font-weight: 600; }
.ranges { display: flex; gap: 4px; }
.ranges button { padding: 5px 11px; border: 1px solid var(--border); background: #fff; border-radius: 6px; cursor: pointer; }
.ranges button.active { background: var(--text); color: #fff; border-color: var(--text); }
.cards { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; margin: 12px 0 6px; }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 9px 12px; }
.card .label { font-size: 12px; color: var(--muted); font-weight: 600; }
.card .val { font-size: 18px; font-weight: 700; font-variant-numeric: tabular-nums; margin-top: 2px; }
.card .sub { font-size: 12px; color: var(--muted); }
.dot { display: inline-block; width: 10px; height: 3px; border-radius: 2px; margin-right: 6px; vertical-align: middle; }
.dot.ema9 { background: var(--ema9); } .dot.sma20 { background: var(--sma20); } .dot.sma200 { background: var(--sma200); }
.legend { min-height: 18px; margin-bottom: 4px; font-variant-numeric: tabular-nums; }
#chart { flex: 1; min-height: 360px; background: var(--panel); border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.show-sm { display: none; }

/* ---------- mobile ---------- */
@media (max-width: 860px) {
  .hide-sm { display: none; }
  .show-sm { display: inline-block; }
  #sidebar { width: 100%; min-width: 0; border-right: 0; }
  #detail { display: none; padding: 10px; }
  body.viewing #sidebar { display: none; }
  body.viewing #detail { display: flex; }
  .cards { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  #chart { min-height: 420px; }
}
