/* ==========================================================
   KIS Monitor
   차트지(모눈) 바탕 + 국내 시장 관례 색(매수 적 / 매도 청)
   숫자는 전부 등폭 tabular. 손익은 자릿수가 흔들리면 안 읽힌다.
   ========================================================== */

:root {
  --paper:  #E9EDF2;
  --grid:   rgba(20, 28, 40, 0.055);
  --card:   #FFFFFF;
  --ink:    #141C28;
  --ink-2:  #64728A;
  --ink-3:  #93A0B4;
  --rule:   #D5DCE6;
  --up:     #D62B22;   /* 매수 / 상승 */
  --down:   #1550C4;   /* 매도 / 하락 */
  --flat:   #6B7A90;
  --warn:   #A85B00;
  --ok:     #0B7355;
  --shadow: 0 1px 2px rgba(20, 28, 40, .06), 0 6px 18px rgba(20, 28, 40, .05);

  --mono: ui-monospace, "Cascadia Mono", Consolas, "D2Coding", "Menlo", monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo",
          "Malgun Gothic", "Segoe UI", Roboto, sans-serif;

  --foot-h: 68px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper:  #10151C;
    --grid:   rgba(150, 180, 220, 0.05);
    --card:   #182029;
    --ink:    #E4EAF2;
    --ink-2:  #93A3B8;
    --ink-3:  #6B7A8F;
    --rule:   #2A3542;
    --up:     #FF6257;
    --down:   #6098FF;
    --flat:   #8A99AD;
    --warn:   #E0A458;
    --ok:     #4FC79B;
    --shadow: 0 1px 2px rgba(0,0,0,.4);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: var(--sans);
  color: var(--ink);
  background-color: var(--paper);
  background-image:
    repeating-linear-gradient(0deg, var(--grid) 0 1px, transparent 1px 28px),
    repeating-linear-gradient(90deg, var(--grid) 0 1px, transparent 1px 28px);
  padding-bottom: calc(var(--foot-h) + env(safe-area-inset-bottom, 0px));
  overscroll-behavior-y: contain;
}

.num { font-family: var(--mono); font-variant-numeric: tabular-nums; letter-spacing: -0.02em; }
.up   { color: var(--up); }
.down { color: var(--down); }
.flat { color: var(--flat); }

/* ---------- 상단 상태바 (HTS 상태줄에서 가져옴) ---------- */
.topbar { position: sticky; top: 0; z-index: 30; }

.statusbar {
  background: var(--card);
  border-bottom: 1px solid var(--rule);
  padding: 10px 14px calc(10px + env(safe-area-inset-top, 0px));
}

.statusbar .line1 {
  display: flex; align-items: center; gap: 6px; flex-wrap: wrap;
}

.pill {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: 12px; font-weight: 700; letter-spacing: .04em;
  padding: 4px 9px; border-radius: 999px;
  border: 1px solid currentColor;
}
.pill::before {
  content: ""; width: 6px; height: 6px; border-radius: 50%;
  background: currentColor;
}
.pill.running  { color: var(--ok); }
.pill.stopped  { color: var(--ink-2); }
.pill.halted   { color: var(--up); }
.pill.stale    { color: var(--warn); }
.pill.no_data  { color: var(--ink-3); }
.pill.running::before { animation: blip 2s ease-in-out infinite; }

@keyframes blip { 0%,100% { opacity: 1; } 50% { opacity: .25; } }

.chip {
  font-size: 11px; font-weight: 600; letter-spacing: .03em;
  padding: 3px 7px; border-radius: 4px;
  background: rgba(107,122,144,.12); color: var(--ink-2);
}
.chip.alert { background: rgba(214,43,34,.12); color: var(--up); }

.statusbar .line2 {
  display: flex; align-items: baseline; justify-content: space-between;
  margin-top: 8px;
}
.equity { font-size: 26px; font-weight: 600; }
.equity small { font-size: 13px; color: var(--ink-2); margin-left: 3px; font-weight: 400; }
.delta { font-size: 15px; font-weight: 600; text-align: right; }
.delta span { display: block; font-size: 11px; color: var(--ink-3); font-weight: 400; }

/* ---------- 탭 ---------- */
.tabs {
  display: grid; grid-template-columns: repeat(3, 1fr);
  background: var(--card); border-bottom: 1px solid var(--rule);
}
.tabs button {
  appearance: none; border: 0; background: none; cursor: pointer;
  font-family: inherit; font-size: 14px; color: var(--ink-2);
  padding: 13px 0; position: relative;
}
.tabs button[aria-selected="true"] { color: var(--ink); font-weight: 700; }
.tabs button[aria-selected="true"]::after {
  content: ""; position: absolute; left: 22%; right: 22%; bottom: 0;
  height: 2px; background: var(--ink);
}

main { padding: 14px; max-width: 640px; margin: 0 auto; }
section[hidden] { display: none; }

h2 {
  font-size: 11px; text-transform: uppercase; letter-spacing: .12em;
  color: var(--ink-3); margin: 22px 0 8px; font-weight: 700;
}
h2:first-child { margin-top: 4px; }

.card {
  background: var(--card); border: 1px solid var(--rule);
  border-radius: 10px; box-shadow: var(--shadow); overflow: hidden;
}
.card + .card { margin-top: 10px; }

.empty {
  padding: 26px 16px; text-align: center; color: var(--ink-2); font-size: 14px;
}
.empty b { display: block; color: var(--ink); margin-bottom: 4px; font-size: 15px; }

/* ---------- 보유 종목 ---------- */
.pos {
  display: grid; grid-template-columns: 1fr auto;
  gap: 2px 10px; padding: 12px 14px;
}
.pos + .pos { border-top: 1px solid var(--rule); }
.pos .name { font-weight: 600; font-size: 15px; }
.pos .code { font-size: 11px; color: var(--ink-3); margin-left: 5px; }
.pos .sub  { font-size: 12px; color: var(--ink-2); }
.pos .rate { font-size: 17px; font-weight: 700; text-align: right; }
.pos .amt  { font-size: 12px; text-align: right; }

/* ---------- 손익 그래프 ---------- */
.chartbox { padding: 12px 6px 6px; }
.chartbox svg { display: block; width: 100%; height: auto; aspect-ratio: 320 / 140; }
.chart-line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart-base { stroke: var(--ink-3); stroke-width: 1; stroke-dasharray: 3 3; }
.chart-axis { fill: var(--ink-3); font-size: 10px; font-family: var(--mono); }
.chart-tabs { display: flex; gap: 6px; padding: 0 12px 12px; }
.chart-tabs button {
  appearance: none; font-family: inherit; font-size: 12px; cursor: pointer;
  padding: 5px 11px; border-radius: 6px;
  border: 1px solid var(--rule); background: none; color: var(--ink-2);
}
.chart-tabs button[aria-pressed="true"] {
  background: var(--ink); color: var(--card); border-color: var(--ink);
}

/* ---------- 오늘 요약 ---------- */
.counts { display: grid; grid-template-columns: repeat(4, 1fr); }
.counts div { padding: 12px 6px; text-align: center; }
.counts div + div { border-left: 1px solid var(--rule); }
.counts b { display: block; font-size: 20px; font-weight: 600; }
.counts span { font-size: 11px; color: var(--ink-2); }

/* ---------- 기록(테이프) : 이 화면의 중심 ---------- */
.tape { padding: 4px 0; }
.tape-row {
  display: grid; grid-template-columns: 52px 14px 1fr auto;
  align-items: start; gap: 0 8px;
  padding: 9px 14px 9px 10px;
  position: relative;
}
.tape-row .t {
  font-family: var(--mono); font-size: 11px; color: var(--ink-3);
  padding-top: 2px; text-align: right;
}
.tape-row .rail {
  position: relative; align-self: stretch; justify-self: center;
  width: 1px; background: var(--rule);
}
.tape-row:first-child .rail { top: 8px; }
.tape-row:last-child  .rail { height: 10px; }
.tape-row .rail::before {
  content: ""; position: absolute; top: 5px; left: 50%;
  width: 7px; height: 7px; margin-left: -3.5px; border-radius: 50%;
  background: var(--card); border: 2px solid var(--flat);
}
.tape-row.buy  .rail::before { border-color: var(--up); }
.tape-row.sell .rail::before { border-color: var(--down); }
.tape-row.order .rail::before { background: currentColor; }
.tape-row.buy.order  .rail::before { background: var(--up); }
.tape-row.sell.order .rail::before { background: var(--down); }
.tape-row.failed .rail::before { border-color: var(--warn); background: var(--warn); }

.tape-row .body { min-width: 0; }
.tape-row .head { font-size: 14px; font-weight: 600; }
.tape-row .head .side { font-weight: 700; }
.tape-row.buy  .head .side { color: var(--up); }
.tape-row.sell .head .side { color: var(--down); }
.tape-row .note {
  font-size: 11px; color: var(--ink-2); margin-top: 2px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.tape-row .right { text-align: right; font-size: 12px; color: var(--ink-2); padding-top: 2px; }
.tape-row .tag {
  display: inline-block; font-size: 10px; font-weight: 700; letter-spacing: .04em;
  padding: 1px 5px; border-radius: 3px; margin-left: 5px;
  background: rgba(107,122,144,.14); color: var(--ink-2);
}
.tape-row .tag.dry  { background: rgba(107,122,144,.14); color: var(--ink-2); }
.tape-row .tag.fail { background: rgba(168,91,0,.15); color: var(--warn); }
.day-sep {
  padding: 10px 14px 6px; font-size: 11px; font-weight: 700;
  letter-spacing: .08em; color: var(--ink-3);
  background: linear-gradient(var(--card), var(--card));
  border-top: 1px solid var(--rule);
}
.tape .day-sep:first-child { border-top: 0; }

/* ---------- 설정 ---------- */
.field {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 13px 14px;
}
.field + .field { border-top: 1px solid var(--rule); }
.field label { font-size: 14px; }
.field .hint { display: block; font-size: 11px; color: var(--ink-3); margin-top: 2px; }
.field .ctl { flex: 0 0 auto; display: flex; align-items: center; gap: 6px; }
.field input[type="number"] {
  width: 108px; padding: 8px 10px; text-align: right;
  font-family: var(--mono); font-size: 15px;
  color: var(--ink); background: var(--paper);
  border: 1px solid var(--rule); border-radius: 7px;
}
.field select {
  padding: 8px 10px; font-family: inherit; font-size: 14px;
  color: var(--ink); background: var(--paper);
  border: 1px solid var(--rule); border-radius: 7px;
}
.field .unit { font-size: 12px; color: var(--ink-2); min-width: 20px; }
.field input:focus-visible, .field select:focus-visible,
button:focus-visible, a:focus-visible {
  outline: 2px solid var(--down); outline-offset: 2px;
}

/* 토글 */
.switch { position: relative; width: 50px; height: 30px; flex: none; }
.switch input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.switch span {
  position: absolute; inset: 0; border-radius: 999px;
  background: var(--rule); transition: background .18s ease;
}
.switch span::after {
  content: ""; position: absolute; top: 3px; left: 3px;
  width: 24px; height: 24px; border-radius: 50%;
  background: var(--card); box-shadow: 0 1px 3px rgba(0,0,0,.25);
  transition: transform .18s ease;
}
.switch input:checked + span { background: var(--ok); }
.switch input:checked + span::after { transform: translateX(20px); }
.switch input:focus-visible + span { outline: 2px solid var(--down); outline-offset: 2px; }

/* 종목 편집 */
.sym {
  display: grid; grid-template-columns: 88px 1fr 38px;
  gap: 8px; align-items: center; padding: 9px 14px;
}
.sym + .sym { border-top: 1px solid var(--rule); }
.sym input {
  padding: 8px 10px; font-size: 14px; color: var(--ink);
  background: var(--paper); border: 1px solid var(--rule); border-radius: 7px;
  width: 100%;
}
.sym input.code { font-family: var(--mono); }
.sym button {
  appearance: none; border: 1px solid var(--rule); background: none;
  color: var(--ink-2); border-radius: 7px; height: 34px; cursor: pointer;
  font-size: 17px; line-height: 1;
}
.rowbtn {
  display: block; width: 100%; padding: 12px; cursor: pointer;
  appearance: none; border: 0; border-top: 1px solid var(--rule);
  background: none; color: var(--down); font-family: inherit;
  font-size: 14px; font-weight: 600;
}

.readonly { padding: 12px 14px; font-size: 13px; color: var(--ink-2); }
.readonly div { display: flex; justify-content: space-between; padding: 3px 0; }
.readonly b { color: var(--ink); font-weight: 600; }

.savebar { margin: 16px 0 6px; }
.btn {
  display: block; width: 100%; padding: 15px; cursor: pointer;
  appearance: none; border: 0; border-radius: 10px;
  font-family: inherit; font-size: 15px; font-weight: 700;
  background: var(--ink); color: var(--paper);
}
.btn[disabled] { opacity: .45; cursor: default; }
.btn.ghost { background: none; border: 1px solid var(--rule); color: var(--ink-2); }

/* ---------- 하단 고정 정지 버튼 ---------- */
footer {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 40;
  background: var(--card); border-top: 1px solid var(--rule);
  padding: 10px 14px calc(10px + env(safe-area-inset-bottom, 0px));
}
footer .inner { max-width: 640px; margin: 0 auto; }
.stopbtn {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  width: 100%; padding: 15px; cursor: pointer;
  appearance: none; border-radius: 10px;
  font-family: inherit; font-size: 15px; font-weight: 700;
  border: 1.5px solid var(--up); background: none; color: var(--up);
}
.stopbtn.resume { border-color: var(--ok); color: var(--ok); }

/* ---------- 토스트 ---------- */
#toast {
  position: fixed; left: 14px; right: 14px;
  bottom: calc(var(--foot-h) + 16px + env(safe-area-inset-bottom, 0px));
  z-index: 60; max-width: 612px; margin: 0 auto;
  padding: 13px 15px; border-radius: 9px;
  background: var(--ink); color: var(--paper); font-size: 14px;
  box-shadow: var(--shadow);
  transform: translateY(12px); opacity: 0; pointer-events: none;
  transition: opacity .18s ease, transform .18s ease;
}
#toast.show { transform: none; opacity: 1; }
#toast.bad { background: var(--up); }

/* 로그인 */
.login {
  max-width: 340px; margin: 18vh auto; padding: 0 20px; text-align: center;
}
.login h1 { font-size: 17px; letter-spacing: .02em; margin-bottom: 4px; }
.login p { font-size: 13px; color: var(--ink-2); margin: 0 0 22px; }
.login input {
  width: 100%; padding: 14px; font-size: 16px; text-align: center;
  font-family: var(--mono); color: var(--ink);
  background: var(--card); border: 1px solid var(--rule); border-radius: 10px;
}
.login .err { color: var(--up); font-size: 13px; margin-top: 12px; }
.login .btn { margin-top: 10px; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
