:root {
  color-scheme: light;
  --bg: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f1f0ec;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --border: rgba(11, 11, 11, 0.10);
  --grid: #e1e0d9;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --good: #0ca30c;
  --good-bg: rgba(12, 163, 12, 0.12);
  --warning: #fab219;
  --warning-bg: rgba(250, 178, 25, 0.16);
  --critical: #d03b3b;
  --critical-bg: rgba(208, 59, 59, 0.12);
}

/* Dark palette applies when the OS prefers dark (unless the user picked
   "light" via the on-page toggle), OR when the user explicitly picked
   "dark" via the toggle regardless of OS setting. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #232322;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #898781;
    --border: rgba(255, 255, 255, 0.10);
    --grid: #2c2c2a;
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --good: #0ca30c;
    --good-bg: rgba(12, 163, 12, 0.18);
    --warning: #fab219;
    --warning-bg: rgba(250, 178, 25, 0.14);
    --critical: #e66767;
    --critical-bg: rgba(230, 103, 103, 0.16);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #232322;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted: #898781;
  --border: rgba(255, 255, 255, 0.10);
  --grid: #2c2c2a;
  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --good: #0ca30c;
  --good-bg: rgba(12, 163, 12, 0.18);
  --warning: #fab219;
  --warning-bg: rgba(250, 178, 25, 0.14);
  --critical: #e66767;
  --critical-bg: rgba(230, 103, 103, 0.16);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Pretendard", system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text-primary);
  min-height: 100vh;
}

a { color: inherit; }

.theme-toggle-btn {
  position: fixed;
  bottom: 16px;
  right: 16px;
  z-index: 50;
  padding: 8px 14px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-secondary);
  font: inherit;
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.16);
}
.theme-toggle-btn:hover { border-color: var(--series-1); color: var(--text-primary); }

.topbar {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 14px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
  flex-wrap: wrap;
}

.topbar .brand {
  font-weight: 700;
  font-size: 1.05rem;
  text-decoration: none;
  white-space: nowrap;
  display: flex;
  align-items: center;
  gap: 8px;
}

.topbar .brand-logo {
  height: 28px;
  width: 28px;
  display: block;
}

.topbar .market-badge {
  font-size: 0.78rem;
  font-weight: 600;
  padding: 3px 10px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text-secondary);
  white-space: nowrap;
}

.market-clock-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 24px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
  font-size: 0.86rem;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
  flex-wrap: wrap;
}
.market-clock-bar strong {
  color: var(--text-primary);
  font-weight: 700;
}
.market-clock-bar .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-muted);
  flex-shrink: 0;
}
.market-clock-bar .dot.open { background: var(--good); }
.market-clock-bar .dot.closed { background: var(--critical); }

.market-select {
  font-size: 0.78rem;
  font-weight: 600;
  padding: 5px 10px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text-secondary);
  border: 1px solid var(--border);
  white-space: nowrap;
  max-width: 190px;
}

.tabs {
  display: flex;
  gap: 4px;
  margin-left: auto;
  flex-wrap: nowrap;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }

.tabs a {
  padding: 7px 14px;
  border-radius: 8px;
  text-decoration: none;
  font-size: 0.9rem;
  color: var(--text-secondary);
  white-space: nowrap;
  flex-shrink: 0;
}

.tabs a:hover { background: var(--surface-2); }
.tabs a.active { background: var(--series-1); color: #fff; }

.container {
  max-width: 1080px;
  margin: 0 auto;
  padding: 28px 20px 60px;
}

h1 { font-size: 1.5rem; margin: 0 0 4px; }
h2 { font-size: 1.15rem; margin: 0 0 12px; }

.collapsible-header {
  cursor: pointer;
  user-select: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.collapsible-header .chevron {
  color: var(--text-muted);
  transition: transform 0.15s ease;
  font-size: 0.85rem;
}
.collapsible-header.open .chevron {
  transform: rotate(90deg);
}
.subtitle { color: var(--text-secondary); margin: 0 0 24px; font-size: 0.92rem; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 20px;
  margin-bottom: 20px;
}

.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

@media (max-width: 720px) {
  .grid-2 { grid-template-columns: 1fr; }
  .topbar { padding: 12px 16px; gap: 12px; }
  .container { padding: 20px 14px 48px; }
  .market-picker { grid-template-columns: 1fr; }
  h1 { font-size: 1.25rem; }
  th, td { padding: 8px 6px; font-size: 0.83rem; }
  .card { padding: 16px; }
  .field-row { gap: 10px; }
  .theme-toggle-btn { bottom: 12px; right: 12px; padding: 7px 12px; font-size: 0.74rem; }
}

.market-picker {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 16px;
}

.market-card {
  display: block;
  text-decoration: none;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 14px;
  padding: 24px;
  transition: border-color 0.15s ease, transform 0.15s ease;
}

.market-card:hover { border-color: var(--series-1); transform: translateY(-1px); }
.market-card .flag { font-size: 0.78rem; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.04em; }
.market-card .name { font-size: 1.3rem; font-weight: 700; margin: 6px 0 4px; }
.market-card .desc { color: var(--text-secondary); font-size: 0.88rem; }

.stat-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 14px;
  margin-bottom: 4px;
}

.stat-tile .label { font-size: 0.78rem; color: var(--text-muted); margin-bottom: 4px; }
.stat-tile .value { font-size: 1.3rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat-tile .value.good { color: var(--good); }
.stat-tile .value.bad { color: var(--critical); }
.stat-tile .value.gain { color: var(--critical); }
.stat-tile .value.loss { color: var(--series-1); }

/* holdings table profit/loss: 상승(gain) = 빨강, 하락(loss) = 파랑
   (한국 주식 시장 관례 — 이 앱의 다른 곳에서 쓰는 초록/빨강 good/bad 배색과는 다름) */
td.num.gain { color: var(--critical); }
td.num.loss { color: var(--series-1); }

table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
th, td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--border); font-variant-numeric: tabular-nums; }
th { color: var(--text-muted); font-weight: 600; font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.03em; }
td.num, th.num { text-align: right; }
tbody tr:hover { background: var(--surface-2); }
.table-scroll { overflow-x: auto; }

th.sortable { cursor: pointer; user-select: none; white-space: nowrap; }
th.sortable:hover { color: var(--text); }
th.sortable::after { content: "\2195"; margin-left: 4px; opacity: 0.4; font-size: 0.85em; }
th.sortable.sort-asc::after { content: "\2191"; opacity: 1; }
th.sortable.sort-desc::after { content: "\2193"; opacity: 1; }
tfoot td { font-weight: 700; border-top: 2px solid var(--border); border-bottom: none; }

/* Mobile: tables tagged .card-table stack into cards (one per row) instead
   of scrolling sideways — each <td> needs a data-label attribute matching
   its column header; the first identifying cell should carry class
   "card-title" and any trailing button/input cell "card-actions". */
@media (max-width: 640px) {
  .table-scroll { overflow-x: visible; }

  table.card-table { display: block; }
  table.card-table thead { display: none; }
  table.card-table tbody { display: block; }

  table.card-table tr {
    display: block;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 12px 14px;
    margin-bottom: 10px;
  }
  table.card-table tr:hover { background: var(--surface); }

  table.card-table td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
    padding: 4px 0;
    border: none;
    text-align: right;
  }

  table.card-table td[data-label]::before {
    content: attr(data-label);
    font-size: 0.72rem;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.02em;
    flex-shrink: 0;
    text-align: left;
  }

  table.card-table td.card-title {
    display: block;
    text-align: left;
    font-size: 1rem;
    padding-bottom: 8px;
    margin-bottom: 6px;
    border-bottom: 1px solid var(--border);
  }
  table.card-table td.card-title::before { content: none; }

  table.card-table td.card-actions {
    display: block;
    text-align: left;
    padding-top: 8px;
    margin-top: 4px;
    border-top: 1px solid var(--border);
  }
  table.card-table td.card-actions::before { content: none; }

  table.card-table td.card-hero {
    font-size: 1.05rem;
    font-weight: 700;
  }
  table.card-table td.card-hero::before {
    font-size: 0.72rem;
    font-weight: 600;
  }

  /* rows opted into this pattern show only the title + hero value by
     default; td.mobile-collapsible cells appear once .mobile-expanded is
     toggled on the row (see holdings/hall-of-fame detail buttons). Desktop
     is untouched — this block only exists under the 640px breakpoint. */
  table.card-table tr:not(.mobile-expanded) td.mobile-collapsible {
    display: none;
  }
  .mobile-detail-toggle {
    display: inline-block;
  }

  /* .hof tables (hall-of-fame leaderboards) go further: instead of the
     usual one-value-per-line stack, the title (rank+alias) and the hero
     value (수익률) sit side by side on a single compact line, and the
     collapsible cells — when expanded — drop to a full-width line below. */
  table.card-table.hof tr {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 4px 10px;
  }
  table.card-table.hof td.card-title {
    display: flex;
    align-items: center;
    gap: 6px;
    flex: 1 1 auto;
    min-width: 0;
    border-bottom: none;
    margin-bottom: 0;
    padding-bottom: 4px;
    text-align: left;
  }
  table.card-table.hof td.card-title::before { content: none; }
  table.card-table.hof td.card-hero {
    flex: 0 0 auto;
    padding: 4px 0;
  }
  table.card-table.hof td.card-hero::before { content: none; }
  table.card-table.hof td.mobile-collapsible {
    flex: 1 1 100%;
    border-top: 1px solid var(--border);
    margin-top: 4px;
    padding-top: 6px;
  }
}

.mobile-detail-toggle {
  display: none;
}

.pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 0.76rem;
  font-weight: 700;
}
.pill.buy { background: var(--good-bg); color: var(--good); }
.pill.strong-buy { background: var(--good-bg); color: var(--good); }
.pill.hold { background: var(--warning-bg); color: #9a6a00; }
.pill.avoid { background: var(--critical-bg); color: var(--critical); }

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .pill.hold { color: var(--warning); }
}
:root[data-theme="dark"] .pill.hold { color: var(--warning); }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 16px;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text-primary);
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
}
.btn:hover { border-color: var(--series-1); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.primary { background: var(--series-1); border-color: var(--series-1); color: #fff; }
.btn.primary:hover { opacity: 0.9; }
.btn.danger { color: var(--critical); }
.btn.small { padding: 5px 10px; font-size: 0.8rem; }

.actions-row { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }

.toggle-group {
  display: inline-flex;
  max-width: 100%;
  border: 1px solid var(--border);
  border-radius: 9px;
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.toggle-group::-webkit-scrollbar { display: none; }
.toggle-group button {
  border: none;
  background: var(--surface);
  color: var(--text-secondary);
  padding: 8px 16px;
  font: inherit;
  font-weight: 600;
  font-size: 0.86rem;
  cursor: pointer;
  white-space: nowrap;
  flex-shrink: 0;
}
.toggle-group button:not(:last-child) { border-right: 1px solid var(--border); }
.toggle-group button.active { background: var(--series-1); color: #fff; }
.toggle-group button.sell.active { background: var(--critical); }
.toggle-group button.danger { color: var(--critical); }
.toggle-group button.danger.active { background: var(--critical); color: #fff; }

input, select {
  font: inherit;
  padding: 9px 11px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-primary);
}
input:focus, select:focus { outline: 2px solid var(--series-1); outline-offset: 1px; }

.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field label { font-size: 0.8rem; color: var(--text-secondary); font-weight: 600; }
.field-row { display: flex; gap: 12px; flex-wrap: wrap; align-items: flex-end; }
.field-row .field { flex: 1; min-width: 140px; margin-bottom: 0; }

.search-results { display: flex; flex-direction: column; gap: 6px; margin-top: 10px; }
.search-result {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 9px;
  cursor: pointer;
  background: var(--surface);
}
.search-result:hover { border-color: var(--series-1); background: var(--surface-2); }
.search-result .sym { font-weight: 700; }
.search-result .exch { color: var(--text-muted); font-size: 0.78rem; }

.quote-box {
  display: flex;
  align-items: baseline;
  gap: 14px;
  flex-wrap: wrap;
  margin: 14px 0;
}
.quote-box .price { font-size: 2rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.quote-box .delta { font-size: 1rem; font-weight: 600; }
.quote-box .delta.good { color: var(--good); }
.quote-box .delta.bad { color: var(--critical); }

.msg { padding: 10px 14px; border-radius: 9px; font-size: 0.88rem; margin-bottom: 14px; }
.msg.error { background: var(--critical-bg); color: var(--critical); }
.msg.success { background: var(--good-bg); color: var(--good); }
.msg.info { background: var(--surface-2); color: var(--text-secondary); }
.msg.warning { background: var(--warning-bg); color: #9a6a00; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .msg.warning { color: var(--warning); }
}
:root[data-theme="dark"] .msg.warning { color: var(--warning); }

.empty { color: var(--text-muted); font-size: 0.88rem; padding: 20px 0; text-align: center; }

.legend { display: flex; gap: 16px; flex-wrap: wrap; font-size: 0.8rem; color: var(--text-secondary); margin-bottom: 8px; }
.legend .dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 5px; vertical-align: middle; }

.chart-wrap { position: relative; }
.chart-tooltip {
  position: absolute;
  pointer-events: none;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 0.78rem;
  box-shadow: 0 4px 14px rgba(0,0,0,0.12);
  display: none;
  white-space: nowrap;
  z-index: 5;
}

.reasons { list-style: none; padding: 0; margin: 10px 0 0; display: flex; flex-direction: column; gap: 6px; }
.reasons li { font-size: 0.86rem; color: var(--text-secondary); padding-left: 16px; position: relative; }
.reasons li::before { content: "·"; position: absolute; left: 2px; font-weight: 900; }

.score-badge {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 76px;
  height: 76px;
  border-radius: 50%;
  border: 3px solid var(--series-1);
  font-weight: 700;
}
.score-badge .num { font-size: 1.35rem; line-height: 1; }
.score-badge .max { font-size: 0.65rem; color: var(--text-muted); }

.timing-box { display: flex; flex-direction: column; gap: 10px; margin-top: 6px; }
.timing-row { display: flex; gap: 10px; align-items: flex-start; }
.timing-row .tag { flex-shrink: 0; width: 56px; font-size: 0.76rem; font-weight: 700; padding: 3px 0; text-align: center; border-radius: 6px; }
.timing-row.buy .tag { background: var(--good-bg); color: var(--good); }
.timing-row.sell .tag { background: var(--critical-bg); color: var(--critical); }
.timing-row .text { font-size: 0.88rem; color: var(--text-secondary); }

.spinner {
  width: 18px; height: 18px;
  border: 2px solid var(--border);
  border-top-color: var(--series-1);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  display: inline-block;
}
@keyframes spin { to { transform: rotate(360deg); } }

.hint { font-size: 0.78rem; color: var(--text-muted); margin-top: 4px; }
