:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-2: #eef1f5;
  --text: #111827;
  --muted: #6b7280;
  --border: #e2e6ec;
  --primary: #2563eb;
  --primary-text: #ffffff;
  --good: #059669;
  --bad: #dc2626;
  --warn: #d97706;
  --buy: #dc2626;   /* 국내 관례: 매수 빨강 */
  --sell: #2563eb;  /* 매도 파랑 */
  --prod: #dc2626;
  --demo: #2563eb;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b1220;
    --surface: #111a2c;
    --surface-2: #18233a;
    --text: #e5e9f0;
    --muted: #94a3b8;
    --border: #23304a;
    --primary: #3b82f6;
    --good: #34d399;
    --bad: #f87171;
    --warn: #fbbf24;
    --buy: #f87171;
    --sell: #60a5fa;
    --prod: #f87171;
    --demo: #60a5fa;
    --shadow: none;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0b1220; --surface: #111a2c; --surface-2: #18233a; --text: #e5e9f0; --muted: #94a3b8;
  --border: #23304a; --primary: #3b82f6; --good: #34d399; --bad: #f87171; --warn: #fbbf24;
  --buy: #f87171; --sell: #60a5fa; --prod: #f87171; --demo: #60a5fa; --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Pretendard", "Noto Sans KR", "Malgun Gothic", sans-serif;
  -webkit-tap-highlight-color: transparent;
}
body.is-prod .topbar { border-bottom: 3px solid var(--prod); }

h2 { font-size: 16px; margin: 24px 0 10px; display: flex; align-items: center; gap: 8px; }
h3 { font-size: 14px; margin: 18px 0 6px; color: var(--muted); font-weight: 600; }
.muted { color: var(--muted); font-size: 14px; }
.center { text-align: center; }
.count { background: var(--bad); color: #fff; border-radius: 999px; font-size: 12px; padding: 0 8px; min-width: 20px; text-align: center; }
.count:empty { display: none; }

/* ---------- 버튼 / 입력 ---------- */
.btn {
  appearance: none; border: 1px solid var(--border); background: var(--surface); color: var(--text);
  border-radius: 10px; padding: 11px 16px; font: inherit; font-weight: 600; cursor: pointer;
  min-height: 44px; transition: opacity .15s, transform .05s;
}
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .4; cursor: default; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn.danger { background: var(--bad); border-color: var(--bad); color: #fff; }
.btn.danger-outline { color: var(--bad); border-color: var(--bad); background: transparent; }
.btn.ghost { background: transparent; }
.btn.small { padding: 6px 12px; min-height: 36px; font-size: 14px; }
.btn.armed { outline: 3px solid var(--warn); outline-offset: 2px; }

.field { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--muted); }
.field.inline { flex-direction: row; align-items: center; justify-content: space-between; gap: 12px; padding: 4px 0; }
.field.inline input { max-width: 170px; }
input:not([type=checkbox]):not([type=radio]) {
  font: inherit; color: var(--text); background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 10px; padding: 10px 12px; width: 100%; min-height: 44px;
}
input.mini { width: 64px !important; min-height: 32px !important; padding: 4px 6px !important; text-align: center; display: inline-block; }
input:focus { outline: 2px solid var(--primary); outline-offset: 1px; }
.check { display: flex; gap: 10px; align-items: flex-start; padding: 6px 0; font-size: 14px; }
.check > input { margin-top: 4px; width: 18px; height: 18px; flex: none; accent-color: var(--primary); }
.msg { min-height: 20px; font-size: 14px; color: var(--bad); margin: 8px 0 0; }
.msg.ok { color: var(--good); }
.stack { display: flex; flex-direction: column; gap: 14px; }

/* ---------- 로그인 ---------- */
body.auth { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.auth-card { width: 100%; max-width: 360px; background: var(--surface); border: 1px solid var(--border); border-radius: 18px; padding: 28px 22px; box-shadow: var(--shadow); }
.auth-title { font-size: 20px; text-align: center; margin: 0 0 22px; }

.pattern {
  position: relative; width: 100%; max-width: 280px; aspect-ratio: 1; margin: 0 auto;
  display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr);
  place-items: center; touch-action: none; user-select: none; -webkit-user-select: none;
}
.pattern-lines { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.pattern-lines polyline { fill: none; stroke: var(--primary); stroke-width: 6; stroke-linecap: round; stroke-linejoin: round; opacity: .7; }
.dot { width: 22px; height: 22px; border-radius: 50%; background: var(--muted); opacity: .55; transition: transform .1s, background .1s; position: relative; z-index: 1; }
.dot.on { background: var(--primary); opacity: 1; transform: scale(1.35); }
.dot.bad { background: var(--bad); opacity: 1; }
.dot.good { background: var(--good); opacity: 1; }
.shake { animation: shake .35s; }
@keyframes shake { 25% { transform: translateX(-8px); } 75% { transform: translateX(8px); } }

/* ---------- 대시보드 ---------- */
.topbar {
  position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 10px;
  padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; flex: 1; }
.badge { font-size: 13px; font-weight: 700; padding: 3px 10px; border-radius: 999px; border: 1px solid var(--border); }
.badge.prod { color: var(--prod); border-color: var(--prod); }
.badge.demo { color: var(--demo); border-color: var(--demo); }

.wrap { max-width: 760px; margin: 0 auto; padding: 16px 16px 60px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 16px; padding: 16px; box-shadow: var(--shadow); margin-top: 16px; }
.card:first-child { margin-top: 0; }
details.card > summary { font-weight: 700; cursor: pointer; list-style: none; display: flex; justify-content: space-between; }
details.card > summary::after { content: "▾"; color: var(--muted); }
details.card[open] > summary { margin-bottom: 8px; }
details.card[open] > summary::after { content: "▴"; }

.status-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.label { font-size: 13px; color: var(--muted); }
.value { font-size: 18px; font-weight: 700; }
.value.good { color: var(--good); }
.value.bad { color: var(--bad); }
.sub { font-size: 13px; color: var(--muted); word-break: keep-all; }

.usage { margin-top: 14px; }
.usage-head { display: flex; justify-content: space-between; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.bar { height: 8px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.bar > div { height: 100%; width: 0; background: var(--primary); transition: width .3s; }

.controls { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-top: 14px; }
@media (max-width: 480px) { .controls { grid-template-columns: repeat(2, 1fr); } }

.list { display: flex; flex-direction: column; gap: 10px; }
.empty { color: var(--muted); font-size: 14px; text-align: center; padding: 18px; background: var(--surface); border: 1px dashed var(--border); border-radius: 14px; margin: 0; }

.proposal { background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--border); border-radius: 14px; padding: 12px 14px; box-shadow: var(--shadow); }
.proposal.buy { border-left-color: var(--buy); }
.proposal.sell { border-left-color: var(--sell); }
.proposal.st-rejected, .proposal.st-expired { opacity: .65; }
.p-head { display: flex; align-items: center; gap: 8px; }
.p-name { font-weight: 700; flex: 1; }
.p-name small { color: var(--muted); font-weight: 400; }
.side { font-size: 12px; font-weight: 700; color: #fff; padding: 2px 8px; border-radius: 6px; }
.side.buy { background: var(--buy); }
.side.sell { background: var(--sell); }
.timer { font-variant-numeric: tabular-nums; font-weight: 700; color: var(--muted); }
.timer.urgent { color: var(--bad); }
.p-meta { display: flex; flex-wrap: wrap; gap: 4px 12px; font-size: 14px; margin-top: 6px; }
.sig { color: var(--muted); }
.mode-tag { font-size: 12px; font-weight: 700; }
.mode-tag.production { color: var(--prod); }
.mode-tag.demo { color: var(--demo); }
.reasons { list-style: none; margin: 10px 0 0; padding: 8px 10px; background: var(--surface-2); border-radius: 10px; font-size: 13px; }
.reasons li { display: flex; justify-content: space-between; gap: 10px; padding: 2px 0; }
.reasons li.yes span { color: var(--good); }
.reasons li.no span { color: var(--muted); }
.reasons em { font-style: normal; color: var(--muted); text-align: right; }
.p-foot { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.st { font-size: 13px; color: var(--muted); }
.p-actions { display: flex; gap: 8px; }
.result { margin-top: 8px; font-size: 13px; color: var(--muted); }
.st-failed .result { color: var(--bad); }
.st-executed .result { color: var(--good); }

.settings { display: flex; flex-direction: column; }
.settings .btn { margin-top: 16px; }

.events { list-style: none; padding: 0; margin: 0; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; overflow: hidden; }
.ev { display: grid; grid-template-columns: auto auto 1fr; gap: 8px; padding: 8px 12px; font-size: 13px; border-top: 1px solid var(--border); }
.ev:first-child { border-top: 0; }
.ev time { color: var(--muted); white-space: nowrap; font-variant-numeric: tabular-nums; }
.ev .src { color: var(--muted); }
.ev.warn { color: var(--warn); }
.ev.error { color: var(--bad); }
.ev.trade { font-weight: 600; }

#toast { position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; opacity: 0; transition: .2s; pointer-events: none; max-width: calc(100% - 32px); }
#toast.show { opacity: 1; transform: translate(-50%, 0); }
