:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --line: #e3e6eb;
  --ink: #161a21;
  --ink-2: #4a5260;
  --muted: #7b8494;
  --accent: #2f6fed;
  --up: #d9363e;   /* 상승(한국식 빨강) */
  --down: #1f6fe5; /* 하락(한국식 파랑) */
  --up-bg: rgba(217, 54, 62, 0.1);
  --down-bg: rgba(31, 111, 229, 0.1);
  --danger: #c62828;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b0e13;
    --surface: #12151c;
    --surface-2: #1a1f28;
    --line: #262c37;
    --ink: #e9edf3;
    --ink-2: #b4bccb;
    --muted: #7d8697;
    --accent: #5b8cff;
    --up: #e5484d;
    --down: #2f7bf5;
    --up-bg: rgba(229, 72, 77, 0.14);
    --down-bg: rgba(47, 123, 245, 0.16);
    --danger: #ff6b6b;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: 'Pretendard Variable', Pretendard, -apple-system, 'Segoe UI', 'Malgun Gothic', sans-serif;
  font-size: 14px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  font-variant-numeric: tabular-nums;
}
[hidden] { display: none !important; }
button, input, textarea, select { font: inherit; color: inherit; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.up { color: var(--up); }
.down { color: var(--down); }
.flat { color: var(--muted); }
.grow { flex: 1; }

/* ---------- header ---------- */
.top {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 20px; padding-top: max(10px, env(safe-area-inset-top));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 800; font-size: 17px; letter-spacing: -0.02em; }
.top-right { display: flex; align-items: center; gap: 6px; }
.live { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); margin-right: 6px; }
.live i { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.live.on i { background: #22a06b; box-shadow: 0 0 0 3px rgba(34, 160, 107, 0.2); }
.live.err i { background: var(--danger); }
.icon-btn {
  width: 34px; height: 34px; border-radius: 8px; border: 1px solid var(--line);
  background: var(--surface); cursor: pointer; font-size: 16px; line-height: 1;
}
.icon-btn:hover { background: var(--surface-2); }

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

/* ---------- panels / tiles ---------- */
.panel {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px 16px; box-shadow: var(--shadow);
}
.panel h2, .stocks-head h2 {
  margin: 0 0 10px; font-size: 14px; font-weight: 700; display: flex; align-items: baseline; gap: 8px;
}
.panel h2 small { font-weight: 500; color: var(--muted); font-size: 12px; }
.panel h2 .unit { margin-left: auto; }
.market { display: grid; grid-template-columns: 1.35fr 1fr 1fr 1.35fr; gap: 12px; }
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 2px 14px; }
.tile { padding: 7px 0; border-bottom: 1px dashed var(--line); min-width: 0; }
.tile .n { font-size: 12px; color: var(--ink-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: flex; gap: 6px; }
.tile .n em { font-style: normal; color: var(--muted); font-size: 11px; }
.tile .p { font-size: 17px; font-weight: 700; letter-spacing: -0.01em; }
.tile .c { font-size: 12px; font-weight: 600; }
.tiles.manual:not(:empty) { margin-top: 6px; padding-top: 6px; border-top: 1px solid var(--line); }
.tile.skeleton .p { color: transparent; background: var(--surface-2); border-radius: 4px; width: 70%; }
.link-btn { margin-left: auto; border: 0; background: none; color: var(--accent); cursor: pointer; font-size: 12px; font-weight: 600; padding: 0; }

/* ---------- investor flows ---------- */
.flows { margin-top: 12px; }
.flow-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
.flow h3 { margin: 0 0 8px; font-size: 13px; display: flex; gap: 8px; align-items: baseline; }
.flow h3 small { color: var(--muted); font-weight: 500; }
.bars { display: grid; grid-template-columns: 64px 1fr 76px; gap: 6px 10px; align-items: center; }
.bars .lab { font-size: 12px; color: var(--ink-2); }
.bars .val { font-size: 13px; font-weight: 700; text-align: right; }
.bar-track { position: relative; height: 14px; }
.bar-track::before { content: ''; position: absolute; left: 50%; top: -3px; bottom: -3px; width: 1px; background: var(--line); }
.bar { position: absolute; top: 0; height: 14px; border-radius: 0 4px 4px 0; }
.bar.neg { border-radius: 4px 0 0 4px; }
.bar.pos { background: var(--up); left: 50%; }
.bar.neg { background: var(--down); right: 50%; }
table.mini { width: 100%; border-collapse: collapse; margin-top: 12px; font-size: 12px; }
table.mini th, table.mini td { padding: 5px 6px; text-align: right; border-bottom: 1px solid var(--line); white-space: nowrap; }
table.mini th { color: var(--muted); font-weight: 500; }
table.mini th:first-child, table.mini td:first-child { text-align: left; }

/* ---------- stock cards ---------- */
.stocks { margin-top: 20px; }
.stocks-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.stocks-head h2 { margin: 0; font-size: 16px; }
.empty { text-align: center; padding: 40px 10px; color: var(--muted); background: var(--surface); border: 1px dashed var(--line); border-radius: var(--radius); }

.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); margin-bottom: 12px; overflow: hidden;
  display: grid; grid-template-columns: 330px 1fr;
}
.card-side { padding: 16px; border-right: 1px solid var(--line); display: flex; flex-direction: column; gap: 12px; min-width: 0; }
.card-title { display: flex; align-items: flex-start; gap: 8px; }
.card-title h3 { margin: 0; font-size: 18px; letter-spacing: -0.02em; }
.card-title .code { font-size: 12px; color: var(--muted); }
.card-tools { margin-left: auto; display: flex; gap: 2px; }
.card-tools button { border: 0; background: none; color: var(--muted); cursor: pointer; width: 28px; height: 28px; border-radius: 6px; }
.card-tools button:hover { background: var(--surface-2); color: var(--ink); }
.price-big { font-size: 26px; font-weight: 800; letter-spacing: -0.02em; line-height: 1.1; }
.price-chg { font-size: 13px; font-weight: 700; margin-top: 2px; }
.price-sub { font-size: 11px; color: var(--muted); margin-top: 2px; }
.chips { display: flex; flex-wrap: wrap; gap: 4px; }
.chip { font-size: 11px; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--ink-2); border: 1px solid var(--line); }
.chip.on { background: var(--accent); border-color: var(--accent); color: #fff; }
button.chip { cursor: pointer; }
.pos-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 12px; background: var(--surface-2); border-radius: 8px; padding: 10px 12px; }
.pos-grid div { display: flex; flex-direction: column; }
.pos-grid span { font-size: 11px; color: var(--muted); }
.pos-grid b { font-size: 14px; }
.spark { width: 100%; height: 56px; display: block; }
.spark-wrap { position: relative; }
.spark-cap { font-size: 11px; color: var(--muted); display: flex; justify-content: space-between; }
.memo-line { font-size: 12px; color: var(--ink-2); white-space: pre-wrap; border-left: 3px solid var(--line); padding-left: 8px; }

.card-main { min-width: 0; display: flex; flex-direction: column; }
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--line); padding: 0 10px; overflow-x: auto; scrollbar-width: none; }
.tabs button {
  border: 0; background: none; padding: 12px 12px 10px; cursor: pointer; font-weight: 600; color: var(--muted);
  border-bottom: 2px solid transparent; white-space: nowrap;
}
.tabs button[aria-selected='true'] { color: var(--ink); border-bottom-color: var(--accent); }
.tab-body { padding: 14px 16px; min-height: 240px; max-height: 460px; overflow: auto; }
.loading { color: var(--muted); padding: 20px 0; }

.news { list-style: none; margin: 0; padding: 0; }
.news li { padding: 8px 0; border-bottom: 1px solid var(--line); }
.news li a { color: var(--ink); font-weight: 500; }
.news li .meta { font-size: 11px; color: var(--muted); margin-top: 2px; }
.news-filter { display: flex; gap: 4px; flex-wrap: wrap; margin-bottom: 8px; }

.form-row { display: flex; flex-wrap: wrap; gap: 6px; align-items: flex-end; margin-bottom: 12px; }
.form-row input, .form-row select, .form-row textarea, .field input, .field textarea, .dlg input {
  border: 1px solid var(--line); background: var(--surface); border-radius: 8px; padding: 7px 10px; min-width: 0;
}
.form-row textarea { flex: 1 1 100%; resize: vertical; }
.form-row .w-date { width: 140px; }
.form-row .w-num { width: 110px; }
.form-row .w-memo { flex: 1; min-width: 140px; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg label { padding: 7px 12px; cursor: pointer; font-weight: 600; }
.seg input { display: none; }
.seg input:checked + span { color: #fff; }
.seg label:has(input[value='buy']:checked) { background: var(--up); color: #fff; }
.seg label:has(input[value='sell']:checked) { background: var(--down); color: #fff; }
.seg label:has(input[type='radio']:checked):not(:has([value='buy'])):not(:has([value='sell'])) { background: var(--accent); color: #fff; }

.btn {
  border: 1px solid var(--line); background: var(--surface); padding: 7px 14px; border-radius: 8px;
  cursor: pointer; font-weight: 600; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); }
.btn.sm { padding: 4px 10px; font-size: 12px; }
.btn:disabled { opacity: 0.55; cursor: default; }
.x-btn { border: 0; background: none; color: var(--muted); cursor: pointer; padding: 2px 6px; border-radius: 4px; }
.x-btn:hover { color: var(--danger); background: var(--surface-2); }

table.list { width: 100%; border-collapse: collapse; font-size: 13px; }
table.list th, table.list td { padding: 7px 6px; border-bottom: 1px solid var(--line); text-align: right; white-space: nowrap; }
table.list th { color: var(--muted); font-weight: 500; font-size: 12px; }
table.list td.l, table.list th.l { text-align: left; }
table.list td.memo { text-align: left; white-space: normal; color: var(--ink-2); min-width: 120px; }
.side-buy { color: var(--up); font-weight: 700; }
.side-sell { color: var(--down); font-weight: 700; }

.note { padding: 10px 0; border-bottom: 1px solid var(--line); }
.note-head { display: flex; gap: 8px; align-items: center; font-size: 12px; color: var(--muted); }
.note-body { white-space: pre-wrap; margin-top: 4px; }
.kind { font-size: 11px; font-weight: 700; padding: 1px 7px; border-radius: 4px; background: var(--surface-2); color: var(--ink-2); }
.kind.k-전망 { background: rgba(47, 111, 237, 0.14); color: var(--accent); }
.kind.k-체크포인트 { background: rgba(214, 140, 0, 0.16); color: #b87700; }

.md h2 { font-size: 14px; margin: 14px 0 6px; }
.md h2:first-child { margin-top: 0; }
.md ul { margin: 4px 0; padding-left: 18px; }
.md p { margin: 4px 0; }
.brief-meta { font-size: 11px; color: var(--muted); margin-bottom: 10px; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

/* ---------- login ---------- */
.login { position: fixed; inset: 0; display: grid; place-items: center; background: var(--bg); z-index: 50; padding: 16px; }
.login-box { width: min(340px, 100%); display: flex; flex-direction: column; gap: 10px; align-items: center; text-align: center;
  background: var(--surface); border: 1px solid var(--line); border-radius: 16px; padding: 28px 24px; }
.login-box h1 { margin: 4px 0 0; font-size: 20px; }
.login-box p { margin: 0 0 8px; }
.login-box input { width: 100%; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); text-align: center; font-size: 16px; }
.login-box .btn { width: 100%; padding: 10px; }
.err { color: var(--danger); min-height: 1.2em; font-size: 13px; }

/* ---------- dialogs ---------- */
.dlg { border: 1px solid var(--line); border-radius: 14px; padding: 0; background: var(--surface); color: var(--ink); width: min(480px, calc(100vw - 32px)); }
.dlg::backdrop { background: rgba(0, 0, 0, 0.45); }
.dlg-body { padding: 18px 20px; display: flex; flex-direction: column; gap: 10px; }
.dlg-body h3 { margin: 0; }
.dlg input[type='search'] { width: 100%; padding: 10px 12px; }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 12px; font-weight: 600; color: var(--ink-2); }
.field .muted { font-weight: 400; }
.field input, .field textarea { font-size: 14px; font-weight: 400; color: var(--ink); width: 100%; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.dlg-actions { display: flex; gap: 8px; align-items: center; margin-top: 4px; }
.results { list-style: none; margin: 0; padding: 0; max-height: 280px; overflow: auto; }
.results li button { width: 100%; text-align: left; border: 0; background: none; padding: 9px 8px; border-radius: 8px; cursor: pointer; display: flex; gap: 8px; align-items: center; }
.results li button:hover { background: var(--surface-2); }
.results .code { color: var(--muted); font-size: 12px; }
.results .mk { margin-left: auto; font-size: 11px; color: var(--muted); }
.history { max-height: 160px; overflow: auto; font-size: 12px; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(20px); opacity: 0;
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-weight: 600;
  transition: all 0.2s; pointer-events: none; z-index: 60; max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.tip {
  position: fixed; z-index: 70; pointer-events: none; background: var(--ink); color: var(--bg);
  font-size: 12px; padding: 6px 8px; border-radius: 6px; white-space: nowrap;
}
.foot { font-size: 11px; margin-top: 24px; text-align: center; }

/* ---------- responsive ---------- */
@media (max-width: 1180px) {
  .market { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 860px) {
  .card { grid-template-columns: 1fr; }
  .card-side { border-right: 0; border-bottom: 1px solid var(--line); }
  .flow-grid { grid-template-columns: 1fr; }
}
@media (max-width: 640px) {
  main { padding: 12px 12px 32px; }
  .top { padding-left: 16px; padding-right: 16px; }
  .brand { white-space: nowrap; }
  #liveText { display: none; } /* 폰에서는 상태 점만 */
  .market { grid-template-columns: 1fr; }
  .tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .tab-body { max-height: none; }
  .form-row .w-date, .form-row .w-num { width: calc(50% - 3px); }
  .row2 { grid-template-columns: 1fr; }
}
