/* danta — MTS 스타일 다크 트레이딩 대시보드 (설계서 §5)
   한국 관례 색: 상승 빨강 #f04452 / 하락 파랑 #3182f6. 폰 우선 반응형. */

:root {
  --bg: #0a0d12;
  --panel: #12161d;
  --panel-2: #0f141b;
  --panel-3: #171d27;
  --border: #232a35;
  --border-soft: #1b212b;
  --text: #e7ebf2;
  --text-2: #aab3c2;
  --muted: #737f90;
  --up: #f04452;      /* 상승 */
  --down: #3182f6;    /* 하락 */
  --flat: #9aa4b2;
  --up-soft: rgba(240, 68, 82, 0.14);
  --down-soft: rgba(49, 130, 246, 0.14);
  --accent: #2f6bd8;
  --ok: #22c55e;
  --warn: #f5b301;
  --radius: 10px;
  --gap: 12px;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", "Roboto Mono", Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "Apple SD Gothic Neo",
    "Noto Sans KR", "Malgun Gothic", Roboto, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

body {
  font-size: 13px;
  line-height: 1.35;
  padding-bottom: env(safe-area-inset-bottom);
}

.num { font-family: var(--mono); font-variant-numeric: tabular-nums; letter-spacing: -0.2px; }
.pos { color: var(--up); }
.neg { color: var(--down); }
.flat { color: var(--flat); }
.dim { color: var(--muted); }

/* ── 헤더 ─────────────────────────────────────────── */
.hdr {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 16px;
  background: linear-gradient(180deg, #10151c 0%, rgba(16, 21, 28, 0.94) 100%);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(6px);
  flex-wrap: wrap;
}

.hdr-name { display: flex; flex-direction: column; min-width: 0; }
.hdr-name .name { font-size: 15px; font-weight: 700; letter-spacing: -0.2px; }
.hdr-name .code { font-size: 11px; color: var(--muted); font-family: var(--mono); }

.hdr-price { display: flex; align-items: baseline; gap: 10px; margin-left: 2px; }
.hdr-price .price {
  font-family: var(--mono);
  font-variant-numeric: tabular-nums;
  font-size: clamp(26px, 6vw, 38px);
  font-weight: 700;
  line-height: 1;
  letter-spacing: -1px;
}
.hdr-price .chg { display: flex; flex-direction: column; font-size: 12px; font-family: var(--mono); }
.hdr-price .chg .amt { font-weight: 600; }

.hdr-spacer { flex: 1 1 auto; }

.idx-wrap { display: flex; gap: 8px; }
.idx {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  padding: 4px 10px;
  border: 1px solid var(--border-soft);
  border-radius: 8px;
  background: var(--panel-2);
  min-width: 92px;
}
.idx .idx-name { font-size: 10px; color: var(--muted); letter-spacing: 0.3px; }
.idx .idx-val { font-family: var(--mono); font-size: 13px; font-weight: 600; }
.idx .idx-diff { font-family: var(--mono); font-size: 10px; }

.report-link {
  font-size: 12px; font-weight: 600;
  color: var(--text-2);
  text-decoration: none;
  padding: 5px 11px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: var(--panel-2);
  white-space: nowrap;
  transition: border-color 0.12s, color 0.12s;
}
.report-link:hover { color: var(--text); border-color: var(--accent); }

.conn {
  display: flex; align-items: center; gap: 7px;
  font-size: 12px; font-weight: 600; color: var(--text-2);
  cursor: pointer; user-select: none;
  padding: 8px 13px;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: var(--panel-2);
  transition: border-color 0.12s;
  -webkit-tap-highlight-color: rgba(47, 107, 216, 0.25);
}
.conn:active, .conn:hover { border-color: var(--accent); }
.conn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 6px; }
.conn .dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--muted);
  box-shadow: 0 0 0 0 rgba(34, 197, 94, 0.5);
}
.conn.on .dot { background: var(--ok); animation: pulse 2.2s infinite; }
.conn.off .dot { background: var(--up); }
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(34, 197, 94, 0.45); }
  70% { box-shadow: 0 0 0 6px rgba(34, 197, 94, 0); }
  100% { box-shadow: 0 0 0 0 rgba(34, 197, 94, 0); }
}

/* ── 레이아웃 ─────────────────────────────────────── */
.grid {
  display: grid;
  gap: var(--gap);
  padding: var(--gap);
  grid-template-columns: minmax(320px, 380px) 1fr;
  grid-template-areas:
    "chart chart"
    "account log"
    "strategy log";
  align-items: start;
}
.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.card > .card-h {
  display: flex; align-items: center; justify-content: space-between;
  padding: 9px 12px;
  border-bottom: 1px solid var(--border-soft);
  font-size: 12px; font-weight: 600; color: var(--text-2);
  letter-spacing: 0.2px;
}
.card-h .sub { font-size: 11px; color: var(--muted); font-weight: 400; }

#chartCard { grid-area: chart; }
#accountCard { grid-area: account; }
#strategyCard { grid-area: strategy; }
#logCard { grid-area: log; }

/* ── 익명(비로그인) 공개 뷰 (§7.3, §7.5): 차트 + 당일 손익만 노출 ──────── */
body.anon .authed-only { display: none !important; }
body.anon #strategyCard,
body.anon #logCard { display: none; }
/* §7.5: 계좌 카드는 노출하되 손익 히어로(.acct-hero)만. 상세 stat/보유는 숨김. */
body.anon #accountCard .stat-grid,
body.anon #accountCard .holding { display: none; }
body.anon .grid {
  grid-template-columns: 1fr;
  grid-template-areas:
    "chart"
    "account";
}
body.anon .chart-wrap { height: min(66vh, 560px); }

/* ── 차트 ─────────────────────────────────────────── */
#chartCard { background: var(--panel-2); }
.chart-legend {
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px;
  padding: 7px 12px;
  font-family: var(--mono);
  font-size: 11px;
  border-bottom: 1px solid var(--border-soft);
  color: var(--text-2);
  min-height: 30px;
}
.chart-legend .ohlc span { margin-right: 8px; }
.chart-legend .sma { display: inline-flex; align-items: center; gap: 4px; }
.chart-legend .sma i { width: 9px; height: 3px; border-radius: 2px; display: inline-block; }
.chart-wrap { position: relative; width: 100%; height: 420px; }
#chart { position: absolute; inset: 0; }

/* ── 계좌 패널 ────────────────────────────────────── */
.acct-hero {
  padding: 14px 14px 12px;
  border-bottom: 1px solid var(--border-soft);
}
.acct-hero .label { font-size: 11px; color: var(--muted); margin-bottom: 3px; }
.acct-hero .pnl {
  font-family: var(--mono); font-variant-numeric: tabular-nums;
  font-size: 30px; font-weight: 700; line-height: 1; letter-spacing: -0.5px;
}
.acct-hero .rate { font-family: var(--mono); font-size: 14px; font-weight: 600; margin-left: 4px; }

.stat-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1px;
  background: var(--border-soft);
}
.stat {
  background: var(--panel);
  padding: 9px 12px;
  display: flex; flex-direction: column; gap: 2px;
}
.stat .k { font-size: 10.5px; color: var(--muted); }
.stat .v { font-family: var(--mono); font-size: 13px; font-weight: 600; }

.holding {
  display: flex; align-items: center; justify-content: space-between;
  gap: 8px; padding: 10px 12px;
  border-top: 1px solid var(--border-soft);
  background: var(--panel-3);
}
.holding .h-item { display: flex; flex-direction: column; }
.holding .h-item .k { font-size: 10px; color: var(--muted); }
.holding .h-item .v { font-family: var(--mono); font-size: 13px; font-weight: 600; }

/* ── 전략 패널 ────────────────────────────────────── */
.strat-top {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  padding: 11px 12px;
  border-bottom: 1px solid var(--border-soft);
}
.badge {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 10px; border-radius: 20px;
  font-size: 12px; font-weight: 700; letter-spacing: 0.2px;
  border: 1px solid transparent;
}
.badge::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.badge.b-idle { color: var(--text-2); background: rgba(255,255,255,0.05); border-color: var(--border); }
.badge.b-buying { color: var(--up); background: var(--up-soft); border-color: rgba(240,68,82,0.35); }
.badge.b-cooldown { color: var(--warn); background: rgba(245,179,1,0.12); border-color: rgba(245,179,1,0.3); }
.badge.b-armed { color: var(--down); background: rgba(49,130,246,0.12); border-color: rgba(49,130,246,0.35); }

.auto-tag { font-size: 11px; font-family: var(--mono); color: var(--muted); }
.auto-tag.on { color: var(--ok); }

.param-form {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  padding: 12px;
}
.field { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.field label { font-size: 10px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.field input {
  width: 100%;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-family: var(--mono);
  font-size: 12px;
  padding: 6px 7px;
  min-width: 0;
}
.field input:focus { outline: none; border-color: var(--accent); background: #0c1219; }
.field.wide { grid-column: span 3; }

.controls {
  display: flex; flex-wrap: wrap; gap: 8px; align-items: center;
  padding: 0 12px 12px;
}
.controls .grow { flex: 1 1 auto; }

.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--panel-3);
  color: var(--text);
  font-family: var(--sans);
  font-size: 12px; font-weight: 600;
  padding: 8px 12px;
  border-radius: 7px;
  cursor: pointer;
  transition: filter 0.12s, border-color 0.12s;
  white-space: nowrap;
}
.btn:hover { filter: brightness(1.15); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.sell { color: var(--down); border-color: rgba(49,130,246,0.45); background: var(--down-soft); }
.btn.buy { color: var(--up); border-color: rgba(240,68,82,0.45); background: var(--up-soft); }

.buy-amt {
  width: 118px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 7px;
  color: var(--text);
  font-family: var(--mono);
  font-size: 12px;
  padding: 8px 9px;
}
.buy-amt:focus { outline: none; border-color: var(--up); }

/* 토글 스위치 */
.switch { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; user-select: none; }
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch .track {
  width: 40px; height: 22px; border-radius: 22px;
  background: #2b333f; border: 1px solid var(--border);
  position: relative; transition: background 0.15s;
  flex: none;
}
.switch .track::after {
  content: ""; position: absolute; top: 2px; left: 2px;
  width: 16px; height: 16px; border-radius: 50%;
  background: #cdd4de; transition: transform 0.15s;
}
.switch input:checked + .track { background: var(--ok); border-color: var(--ok); }
.switch input:checked + .track::after { transform: translateX(18px); background: #fff; }
.switch .lab { font-size: 12px; font-weight: 600; }

/* ── 체결 로그 ────────────────────────────────────── */
.log-scroll { max-height: 560px; overflow-y: auto; overflow-x: auto; }
table.log { width: 100%; border-collapse: collapse; font-size: 12px; }
table.log th, table.log td {
  padding: 7px 10px; text-align: right; white-space: nowrap;
  border-bottom: 1px solid var(--border-soft);
}
table.log th {
  position: sticky; top: 0; z-index: 1;
  background: var(--panel-3);
  color: var(--muted); font-weight: 600; font-size: 10.5px;
  text-align: right;
}
table.log th:first-child, table.log td:first-child { text-align: left; }
table.log td.side { text-align: left; font-weight: 700; }
table.log td.num { font-family: var(--mono); font-variant-numeric: tabular-nums; }
table.log td.reason { text-align: left; color: var(--text-2); }
table.log tr:hover td { background: rgba(255,255,255,0.02); }
.log-empty { padding: 24px 12px; text-align: center; color: var(--muted); font-size: 12px; }

/* ── 반응형 ───────────────────────────────────────── */
@media (max-width: 900px) {
  .grid {
    grid-template-columns: 1fr;
    grid-template-areas:
      "chart"
      "account"
      "strategy"
      "log";
  }
  .chart-wrap { height: 320px; }
  .log-scroll { max-height: 420px; }
}

@media (max-width: 560px) {
  .hdr { gap: 10px 12px; padding: 9px 12px; }
  .hdr-spacer { display: none; }
  .idx-wrap { order: 3; width: 100%; }
  .idx { flex: 1 1 0; min-width: 0; }
  .conn { order: 2; margin-left: auto; }
  .param-form { grid-template-columns: repeat(2, 1fr); }
  .field.wide { grid-column: span 2; }
  .acct-hero .pnl { font-size: 26px; }
  body { font-size: 12.5px; }
}
