/* 나의 투자일지 — 화면 스타일
   색은 전부 토큰으로 정의하고, 다크 모드는 같은 역할의 값을 따로 지정한다.
   국내 관례: 상승/이익 = 빨강(--up), 하락/손실 = 파랑(--down). 색만으로 구분하지 않도록 항상 +/− 부호를 같이 쓴다. */
:root {
  color-scheme: light;
  --bg: #f4f4f1;
  --surface: #fcfcfb;
  --surface-2: #f0efeb;
  --surface-3: #e7e6e1;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #7b7a74;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, .10);
  --border-strong: rgba(11, 11, 11, .18);
  --accent: #2a78d6;
  --accent-ink: #1c5cab;
  --accent-soft: #e3eefb;
  --up: #cc2b36;
  --down: #1f63c9;
  --up-soft: #fbe9ea;
  --down-soft: #e6effb;
  --warn: #8a5a00;
  --warn-bg: #fdf3dc;
  --ok: #006300;
  --ok-bg: #e5f3e5;
  --danger: #b42318;
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100;
  --s5: #e87ba4; --s6: #008300; --s7: #4a3aa7; --s8: #e34948;
  --other: #8f8d86;   /* 미분류 */
  --fold: #cbc9c1;    /* 도넛의 '그 외 n개' 묶음 */
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.06);
  --radius: 10px;
  --nav-w: 216px;
  font-family: system-ui, -apple-system, "Apple SD Gothic Neo", "Malgun Gothic", "Segoe UI", sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0d0d0d; --surface: #1a1a19; --surface-2: #222220; --surface-3: #2c2c2a;
    --text: #f5f5f2; --text-2: #c3c2b7; --muted: #8f8d86; --grid: #2c2c2a; --axis: #383835;
    --border: rgba(255,255,255,.10); --border-strong: rgba(255,255,255,.18);
    --accent: #3987e5; --accent-ink: #86b6ef; --accent-soft: #17263a;
    --up: #ff6b6b; --down: #6aa6ff; --up-soft: #3a1d1f; --down-soft: #172538;
    --warn: #f5c35a; --warn-bg: #3a2e12; --ok: #0ca30c; --ok-bg: #12301a; --danger: #ff7b72;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
    --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
    --other: #8f8d86;
    --fold: #4a4945;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 4px 16px rgba(0,0,0,.3);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0d0d0d; --surface: #1a1a19; --surface-2: #222220; --surface-3: #2c2c2a;
  --text: #f5f5f2; --text-2: #c3c2b7; --muted: #8f8d86; --grid: #2c2c2a; --axis: #383835;
  --border: rgba(255,255,255,.10); --border-strong: rgba(255,255,255,.18);
  --accent: #3987e5; --accent-ink: #86b6ef; --accent-soft: #17263a;
  --up: #ff6b6b; --down: #6aa6ff; --up-soft: #3a1d1f; --down-soft: #172538;
  --warn: #f5c35a; --warn-bg: #3a2e12; --ok: #0ca30c; --ok-bg: #12301a; --danger: #ff7b72;
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
  --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
  --other: #8f8d86;
  --fold: #4a4945;
  --shadow: 0 1px 2px rgba(0,0,0,.4), 0 4px 16px rgba(0,0,0,.3);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }  /* display 규칙이 hidden 속성을 덮어쓰지 않게 */
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font-size: 14px; line-height: 1.5; -webkit-font-smoothing: antialiased; }
a { color: var(--accent-ink); }
button, input, select, textarea { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0; line-height: 1.3; }
h1 { font-size: 20px; font-weight: 700; }
h2 { font-size: 16px; font-weight: 650; }
h3 { font-size: 14px; font-weight: 650; }

/* ---------- 레이아웃 */
#app { display: flex; min-height: 100vh; }
#nav { width: var(--nav-w); flex: none; background: var(--surface); border-right: 1px solid var(--border);
  position: sticky; top: 0; height: 100vh; overflow-y: auto; display: flex; flex-direction: column; z-index: 40; }
.brand { padding: 18px 18px 10px; }
.brand b { display: block; font-size: 15px; }
.brand span { color: var(--muted); font-size: 12px; }
#nav nav { display: flex; flex-direction: column; padding: 6px 8px; gap: 2px; }
#nav nav a { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px; color: var(--text-2);
  text-decoration: none; font-weight: 500; }
#nav nav a:hover { background: var(--surface-2); color: var(--text); }
#nav nav a.on { background: var(--accent-soft); color: var(--accent-ink); font-weight: 650; }
#nav nav a svg { width: 18px; height: 18px; flex: none; }
#nav .nav-sep { height: 1px; background: var(--border); margin: 8px 10px; }
#nav .nav-foot { margin-top: auto; padding: 12px 18px; color: var(--muted); font-size: 12px; }
#main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
#topbar { position: sticky; top: 0; z-index: 30; display: flex; align-items: center; gap: 10px; padding: 10px 24px;
  background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(8px); border-bottom: 1px solid var(--border); }
#topbar .title { font-weight: 650; font-size: 15px; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#menu-btn { display: none; }
.fx-chip { display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px; border: 1px solid var(--border); border-radius: 999px;
  background: var(--surface); font-size: 12.5px; white-space: nowrap; font-variant-numeric: tabular-nums; }
.fx-chip .lbl { color: var(--text-2); }
.sync-chip.warn { background: var(--warn-bg); color: var(--warn); border-color: transparent; }
.sync-chip.bad { color: var(--danger); }
.sync-veil { position: fixed; inset: 0; z-index: 250; display: flex; align-items: center; justify-content: center; background: rgba(0,0,0,.18); }
.sync-veil > div { display: flex; gap: 8px; align-items: center; padding: 14px 18px; border-radius: 10px; background: var(--surface); border: 1px solid var(--border); box-shadow: var(--shadow); }
.fx-chip b { font-weight: 650; }
#page { padding: 20px 24px 64px; max-width: 1360px; width: 100%; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.page-head p { margin: 4px 0 0; color: var(--text-2); }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.grow { flex: 1; }
.grid { display: grid; gap: 16px; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.stack { display: flex; flex-direction: column; gap: 16px; }
.mt { margin-top: 16px; }
.mt-s { margin-top: 8px; }

/* ---------- 카드 */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; min-width: 0; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; }
.card-head h2, .card-head h3 { display: flex; align-items: center; gap: 6px; }
.sub { color: var(--text-2); font-size: 13px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; min-width: 0; container-type: inline-size; }
.kpi .k { color: var(--text-2); font-size: 12.5px; display: flex; align-items: center; gap: 4px; }
.kpi .v { font-size: clamp(15px, 12cqi, 22px); font-weight: 700; margin-top: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.kpi .d { font-size: 12.5px; margin-top: 2px; color: var(--text-2); }
.kpi.hero .v { font-size: clamp(17px, 13cqi, 26px); }

.up { color: var(--up); }
.down { color: var(--down); }
.flat { color: var(--text-2); }
.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }

/* ---------- 배지·알림 */
.badge { display: inline-flex; align-items: center; gap: 4px; padding: 1px 8px; border-radius: 999px; font-size: 11.5px;
  font-weight: 600; background: var(--surface-3); color: var(--text-2); white-space: nowrap; }
.badge.accent { background: var(--accent-soft); color: var(--accent-ink); }
.badge.up { background: var(--up-soft); color: var(--up); }
.badge.down { background: var(--down-soft); color: var(--down); }
.badge.warn { background: var(--warn-bg); color: var(--warn); }
.badge.ok { background: var(--ok-bg); color: var(--ok); }
.notice { border-radius: 8px; padding: 10px 12px; background: var(--warn-bg); color: var(--warn); display: flex; gap: 8px; align-items: flex-start; }
.notice.info { background: var(--accent-soft); color: var(--accent-ink); }
.notice ul { margin: 0; padding-left: 18px; }

/* ---------- 버튼·입력 */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; height: 34px; padding: 0 14px; border-radius: 8px;
  border: 1px solid var(--border-strong); background: var(--surface); cursor: pointer; font-weight: 550; white-space: nowrap; }
a.btn { color: var(--text); text-decoration: none; }
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.danger { color: var(--danger); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.sm { height: 28px; padding: 0 10px; font-size: 12.5px; }
.btn.icon { width: 34px; padding: 0; }
.btn svg { width: 16px; height: 16px; }
.seg { display: inline-flex; border: 1px solid var(--border-strong); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; background: var(--surface); padding: 0 12px; height: 32px; cursor: pointer; color: var(--text-2); }
.seg button + button { border-left: 1px solid var(--border); }
.seg button.on { background: var(--accent-soft); color: var(--accent-ink); font-weight: 650; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 16px; overflow-x: auto; }
.tabs button { border: 0; background: none; padding: 10px 12px; cursor: pointer; color: var(--text-2); border-bottom: 2px solid transparent;
  font-weight: 550; white-space: nowrap; }
.tabs button.on { color: var(--accent-ink); border-bottom-color: var(--accent); }
input[type=text], input[type=number], input[type=date], input[type=search], input[type=password], select, textarea {
  height: 34px; padding: 0 10px; border: 1px solid var(--border-strong); border-radius: 8px; background: var(--surface); min-width: 0; width: 100%; }
textarea { height: auto; min-height: 84px; padding: 8px 10px; resize: vertical; }
input:focus, select:focus, textarea:focus, .btn:focus-visible, .q:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
label.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
label.field > span { font-size: 12.5px; color: var(--text-2); display: flex; align-items: center; gap: 4px; }
.field .hint { font-size: 12px; color: var(--muted); }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.form-grid .full { grid-column: 1 / -1; }
.check { display: flex; align-items: center; gap: 8px; }
.inline-input { display: inline-flex; align-items: center; gap: 6px; }
.inline-input input, .inline-input select { width: auto; }

/* ---------- 도움말 물음표 + 말풍선 */
.q { display: inline-flex; align-items: center; justify-content: center; width: 16px; height: 16px; border-radius: 50%;
  border: 1px solid var(--border-strong); background: var(--surface-2); color: var(--text-2); font-size: 10.5px; font-weight: 700;
  line-height: 1; cursor: help; padding: 0; flex: none; vertical-align: middle; }
.q:hover, .q[aria-expanded="true"] { background: var(--accent); border-color: var(--accent); color: #fff; }
#tip { position: fixed; z-index: 1000; max-width: 300px; padding: 10px 12px; border-radius: 8px; background: #1f1f1d; color: #f5f5f2;
  font-size: 12.5px; line-height: 1.55; box-shadow: 0 6px 24px rgba(0,0,0,.25); pointer-events: none; }
:root[data-theme="dark"] #tip { background: #f5f5f2; color: #111; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) #tip { background: #f5f5f2; color: #111; } }
#tip { white-space: pre-line; }
#tip b { display: block; margin-bottom: 2px; font-size: 13px; }
#tip::after { content: ""; position: absolute; left: var(--arrow-x, 50%); width: 10px; height: 10px; background: inherit; transform: translateX(-50%) rotate(45deg); }
#tip.below::after { top: -5px; }
#tip.above::after { bottom: -5px; }

/* ---------- 표 */
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
.card > .table-wrap { border: 0; border-radius: 0; margin: 0 -16px; }
.chart-table { display: grid; gap: 16px; grid-template-columns: minmax(0, 2fr) minmax(0, 3fr); align-items: start; }
.table-chart { display: grid; gap: 16px; grid-template-columns: minmax(0, 3fr) minmax(0, 2fr); align-items: start; }
table.t { width: 100%; border-collapse: collapse; font-size: 13px; }
table.t th { position: sticky; top: 0; background: var(--surface-2); color: var(--text-2); font-weight: 600; font-size: 12px;
  text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
table.t th.num { text-align: right; }
table.t td { padding: 8px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
table.t tr:last-child td { border-bottom: 0; }
table.t tbody tr.click { cursor: pointer; }
table.t tbody tr.click:hover td { background: var(--surface-2); }
table.t tfoot td { font-weight: 650; background: var(--surface-2); border-top: 1px solid var(--border-strong); }
table.t td .main { font-weight: 600; }
table.t td:first-child { min-width: 150px; }
table.t td.nowrap, table.t th.nowrap { white-space: nowrap; min-width: 0; }
.clamp2 { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; max-width: 260px; }
table.t td .sub2 { color: var(--muted); font-size: 11.5px; }
table.t tr.hit td { background: var(--up-soft); }
table.t tr.dim td { color: var(--muted); }
.empty { padding: 28px; text-align: center; color: var(--muted); }

/* ---------- 차트 */
.chart { width: 100%; height: 280px; }
.chart.tall { height: 420px; }
.chart.short { height: 200px; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; font-size: 12.5px; color: var(--text-2); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }
.bar-track { height: 8px; border-radius: 999px; background: var(--surface-3); overflow: hidden; }
.bar-fill { height: 100%; border-radius: 999px; background: var(--accent); }

/* ---------- 모달·토스트 */
.modal-back { position: fixed; inset: 0; background: rgba(0,0,0,.42); z-index: 200; display: flex; align-items: flex-start; justify-content: center;
  padding: 6vh 16px 16px; overflow-y: auto; }
.modal { background: var(--surface); border-radius: 14px; width: 100%; max-width: 560px; box-shadow: var(--shadow); border: 1px solid var(--border); }
.modal.wide { max-width: 860px; }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 18px 8px; }
.modal-body { padding: 8px 18px 16px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 18px 16px; border-top: 1px solid var(--border); }
.modal .err { color: var(--danger); font-size: 13px; margin-top: 10px; white-space: pre-wrap; }
#toast-root { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 300; display: flex; flex-direction: column; gap: 8px; align-items: center; }
.toast { background: #1f1f1d; color: #fff; padding: 10px 16px; border-radius: 10px; box-shadow: var(--shadow); font-size: 13px; max-width: 90vw; }
.toast.err { background: var(--danger); }

/* ---------- 검색 자동완성 */
.ac { position: relative; }
.ac-list { position: absolute; left: 0; right: 0; top: calc(100% + 4px); background: var(--surface); border: 1px solid var(--border-strong); border-radius: 8px;
  box-shadow: var(--shadow); z-index: 220; max-height: 300px; overflow-y: auto; }
.ac-list button { display: flex; width: 100%; gap: 8px; align-items: center; text-align: left; padding: 8px 10px; border: 0; background: none; cursor: pointer; }
.ac-list button:hover, .ac-list button.on { background: var(--surface-2); }
.ac-list .code { color: var(--muted); font-size: 12px; margin-left: auto; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { border: 1px solid var(--border-strong); background: var(--surface); border-radius: 999px; padding: 3px 10px; font-size: 12.5px; cursor: pointer; }
.chip:hover { background: var(--surface-2); }

/* ---------- 기타 */
.progress { display: flex; align-items: center; gap: 10px; }
.progress .bar-track { flex: 1; height: 10px; }
.spin { width: 14px; height: 14px; border: 2px solid var(--border-strong); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }
.loading { padding: 40px; display: flex; gap: 10px; align-items: center; justify-content: center; color: var(--muted); }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; font-size: 13px; }
.kv dt { color: var(--text-2); display: flex; align-items: center; gap: 4px; }
.kv dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
.notes { margin: 0; padding-left: 18px; display: flex; flex-direction: column; gap: 4px; }
.journal-item { border-left: 3px solid var(--accent); padding: 4px 0 4px 12px; }
.journal-item .body { white-space: pre-wrap; color: var(--text-2); margin-top: 4px; }
.welcome { max-width: 720px; margin: 24px auto; }
.welcome .steps { display: grid; gap: 12px; margin-top: 16px; }
.drop { border: 2px dashed var(--border-strong); border-radius: var(--radius); padding: 24px; text-align: center; color: var(--text-2); }
.drop.over { border-color: var(--accent); background: var(--accent-soft); }
.pin-pad { max-width: 320px; margin: 12vh auto; text-align: center; }
.pin-pad input { text-align: center; font-size: 24px; letter-spacing: 8px; height: 52px; margin: 16px 0; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.dot.ok { background: var(--ok); } .dot.bad { background: var(--danger); } .dot.run { background: var(--accent); }

/* ---------- 반응형 */
@media (max-width: 1100px) { .chart-table, .table-chart { grid-template-columns: minmax(0, 1fr); } .g4 { grid-template-columns: repeat(2, minmax(0, 1fr)); } .g3 { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (max-width: 900px) {
  #nav { position: fixed; left: 0; top: 0; transform: translateX(-100%); transition: transform .2s; box-shadow: var(--shadow); }
  #nav.open { transform: none; }
  #menu-btn { display: inline-flex; }
  .scrim { position: fixed; inset: 0; background: rgba(0,0,0,.3); z-index: 35; }
  #topbar { padding: 8px 16px; gap: 6px; }
  .fx-chip { padding: 2px 8px; font-size: 12px; }
  .fx-chip .lbl, .fx-chip .q { display: none; }
  #refresh-btn .txt, .sync-chip.quiet .txt { display: none; }
  #page { padding: 16px 16px 64px; }
  .g2, .g3, .g4 { grid-template-columns: minmax(0, 1fr); }
  .form-grid { grid-template-columns: minmax(0, 1fr); }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .kpi { padding: 12px; }
  .kpi.hero { grid-column: 1 / -1; }
  .page-head h1 { font-size: 18px; }
  .chart.tall { height: 360px; }
  .hide-m { display: none !important; }
}
@media print { #nav, #topbar { display: none; } #page { padding: 0; } .card { break-inside: avoid; } }
.card.dim { opacity: .72; }
