:root {
  --bg: #f6f7f9;
  --card: #ffffff;
  --text: #1a1c20;
  --muted: #6b7280;
  --line: #e5e7eb;
  --accent: #2563eb;
  --safe-bg: #dcfce7;
  --safe-fg: #166534;
  --warn-bg: #fef3c7;
  --warn-fg: #92400e;
  --danger-bg: #fee2e2;
  --danger-fg: #991b1b;
  --up: #d33;
  --down: #2563eb;
  --chip-bg: #ffffff;
  --chip-on-bg: #1a1c20;
  --chip-on-fg: #ffffff;
  /* 자산군 시리즈 색 (검증된 카테고리 팔레트, 고정 슬롯: 채권/주식/현금/파생/기타) */
  --s-bond: #2a78d6;
  --s-equity: #eb6834;
  --s-cash: #1baf7a;
  --s-derivatives: #eda100;
  --s-others: #e87ba4;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101216;
    --card: #1b1e24;
    --text: #e8eaed;
    --muted: #9aa1ac;
    --line: #2c313a;
    --accent: #6ea8ff;
    --safe-bg: #123822;
    --safe-fg: #6fdb96;
    --warn-bg: #3a2e10;
    --warn-fg: #f0c05a;
    --danger-bg: #3d1a1a;
    --danger-fg: #f28b8b;
    --up: #f87171;
    --down: #6ea8ff;
    --chip-bg: #1b1e24;
    --chip-on-bg: #e8eaed;
    --chip-on-fg: #101216;
    --s-bond: #3987e5;
    --s-equity: #d95926;
    --s-cash: #199e70;
    --s-derivatives: #c98500;
    --s-others: #d55181;
  }
}

:root[data-theme="light"] {
  --bg: #f6f7f9; --card: #fff; --text: #1a1c20; --muted: #6b7280; --line: #e5e7eb;
  --accent: #2563eb; --safe-bg: #dcfce7; --safe-fg: #166534; --warn-bg: #fef3c7; --warn-fg: #92400e;
  --danger-bg: #fee2e2; --danger-fg: #991b1b; --up: #d33; --down: #2563eb;
  --chip-bg: #fff; --chip-on-bg: #1a1c20; --chip-on-fg: #fff;
  --s-bond: #2a78d6; --s-equity: #eb6834; --s-cash: #1baf7a; --s-derivatives: #eda100; --s-others: #e87ba4;
}

:root[data-theme="dark"] {
  --bg: #101216; --card: #1b1e24; --text: #e8eaed; --muted: #9aa1ac; --line: #2c313a;
  --accent: #6ea8ff; --safe-bg: #123822; --safe-fg: #6fdb96; --warn-bg: #3a2e10; --warn-fg: #f0c05a;
  --danger-bg: #3d1a1a; --danger-fg: #f28b8b; --up: #f87171; --down: #6ea8ff;
  --chip-bg: #1b1e24; --chip-on-bg: #e8eaed; --chip-on-fg: #101216;
  --s-bond: #3987e5; --s-equity: #d95926; --s-cash: #199e70; --s-derivatives: #c98500; --s-others: #d55181;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Pretendard", "Noto Sans KR", "Segoe UI", sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}

#root { max-width: 640px; margin: 0 auto; padding: 0 12px calc(24px + env(safe-area-inset-bottom)); }

.hdr { padding: 20px 4px 12px; }
.hdr h1 { margin: 0; font-size: 1.35rem; letter-spacing: -0.02em; }
.hdr-sub { margin: 4px 0 0; color: var(--muted); font-size: 0.85rem; }
.hdr-sub strong { color: var(--safe-fg); }

.controls {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--bg);
  padding: 8px 0 10px;
  border-bottom: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

#search {
  width: 100%;
  min-height: 44px;
  padding: 10px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--card);
  color: var(--text);
  font-size: 1rem;
}
#search:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.row { display: flex; gap: 8px; align-items: stretch; }

.toggle {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--card);
  font-weight: 600;
  cursor: pointer;
  user-select: none;
}
.toggle input { width: 18px; height: 18px; accent-color: var(--accent); }

#sort {
  flex: 1;
  min-height: 44px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--card);
  color: var(--text);
  font-size: 0.95rem;
}

.chips { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 2px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }

.chip {
  flex-shrink: 0;
  min-height: 38px;
  padding: 6px 14px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--chip-bg);
  color: var(--text);
  font-size: 0.9rem;
  cursor: pointer;
}
.chip[aria-pressed="true"] {
  background: var(--chip-on-bg);
  color: var(--chip-on-fg);
  border-color: var(--chip-on-bg);
  font-weight: 600;
}

.count { margin: 10px 4px 6px; color: var(--muted); font-size: 0.85rem; }

.list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  overflow: hidden;
}

.card-head {
  display: block;
  width: 100%;
  padding: 12px 14px;
  border: 0;
  background: none;
  color: inherit;
  text-align: left;
  font: inherit;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.card-title { display: flex; align-items: flex-start; gap: 8px; }
.card-title .name { flex: 1; font-weight: 700; font-size: 0.98rem; line-height: 1.35; }

.badge {
  flex-shrink: 0;
  margin-top: 1px;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 700;
  white-space: nowrap;
}
.badge.safe { background: var(--safe-bg); color: var(--safe-fg); }
.badge.uncertain { background: var(--warn-bg); color: var(--warn-fg); }
.badge.unsafe { background: var(--danger-bg); color: var(--danger-fg); }

.card-meta { margin-top: 2px; color: var(--muted); font-size: 0.8rem; }

.stats { display: flex; margin-top: 10px; gap: 8px; }
.stat { flex: 1; min-width: 0; }
.stat .k { display: block; color: var(--muted); font-size: 0.72rem; }
.stat .v { display: block; font-weight: 700; font-variant-numeric: tabular-nums; font-size: 0.95rem; }

.pos { color: var(--up); }
.neg { color: var(--down); }

.card-body { display: none; padding: 0 14px 12px; border-top: 1px dashed var(--line); }
.card.open .card-body { display: block; }

.detail-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px 6px;
  padding-top: 10px;
}
.detail-grid .stat .v { font-size: 0.88rem; }

.sec-k {
  margin: 12px 0 6px;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.04em;
}

.bar {
  display: flex;
  gap: 2px;
  height: 12px;
  border-radius: 6px;
  overflow: hidden;
}
.bar .seg { min-width: 2px; }
.bar .seg:first-child { border-radius: 6px 0 0 6px; }
.bar .seg:last-child { border-radius: 0 6px 6px 0; }
.bar .seg:only-child { border-radius: 6px; }

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 12px;
  margin-top: 6px;
  font-size: 0.78rem;
  color: var(--muted);
}
.legend b { color: var(--text); font-weight: 600; font-variant-numeric: tabular-nums; }
.legend .dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: 4px;
  vertical-align: baseline;
}

.holdings ol { margin: 0; padding: 0; list-style: none; counter-reset: h; }
.holdings li {
  display: flex;
  gap: 8px;
  font-size: 0.82rem;
  padding: 2px 0;
}
.holdings li::before {
  counter-increment: h;
  content: counter(h);
  flex-shrink: 0;
  width: 16px;
  color: var(--muted);
  font-size: 0.75rem;
  font-variant-numeric: tabular-nums;
}
.holdings .h-name { min-width: 0; overflow-wrap: anywhere; flex: 1; }
.holdings .h-w { color: var(--muted); font-variant-numeric: tabular-nums; flex-shrink: 0; }
.h-note { margin: 6px 0 0; color: var(--muted); font-size: 0.72rem; }

.reason {
  margin: 10px 0 0;
  padding: 8px 10px;
  border-radius: 10px;
  background: var(--bg);
  color: var(--muted);
  font-size: 0.8rem;
}
.base-index { margin: 8px 0 0; color: var(--muted); font-size: 0.8rem; word-break: break-all; }

.empty { padding: 40px 0; text-align: center; color: var(--muted); }

.ftr { margin-top: 24px; padding: 14px 4px; border-top: 1px solid var(--line); }
.ftr p { margin: 0; color: var(--muted); font-size: 0.78rem; line-height: 1.6; }
