:root {
  color-scheme: light;
  --bg: #ffffff;
  --sheet: #ffffff;
  --text: #111111;
  --text-2: #4b4a46;
  --muted: #7d7b75;
  --line: #ebeae6;
  --grid: #f0efeb;
  --axis: #c9c8c0;
  --hover: rgba(17, 17, 17, 0.045);
  --accent: #3a5ccc;
  --up: #e0393e;
  --down: #2f6fdb;
  --up-wash: rgba(224, 57, 62, 0.1);
  --down-wash: rgba(47, 111, 219, 0.1);
  --good: #0ca30c;
  --warn: #c98500;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;
  --series-5: #e87ba4;
  --font: 'Pretendard Variable', Pretendard, system-ui, -apple-system, 'Segoe UI', sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    color-scheme: dark;
    --bg: #111111;
    --sheet: #111111;
    --text: #f2f1ee;
    --text-2: #c3c2b7;
    --muted: #8f8d86;
    --line: #262624;
    --grid: #1f1f1d;
    --axis: #3a3a37;
    --hover: rgba(255, 255, 255, 0.06);
    --accent: #7b96f0;
    --up: #ff5c61;
    --down: #5b9bff;
    --up-wash: rgba(255, 92, 97, 0.14);
    --down-wash: rgba(91, 155, 255, 0.14);
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --series-5: #d55181;
  }
}

:root[data-theme='dark'] {
  color-scheme: dark;
  --bg: #111111;
  --sheet: #111111;
  --text: #f2f1ee;
  --text-2: #c3c2b7;
  --muted: #8f8d86;
  --line: #262624;
  --grid: #1f1f1d;
  --axis: #3a3a37;
  --hover: rgba(255, 255, 255, 0.06);
  --accent: #7b96f0;
  --up: #ff5c61;
  --down: #5b9bff;
  --up-wash: rgba(255, 92, 97, 0.14);
  --down-wash: rgba(91, 155, 255, 0.14);
  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --series-4: #c98500;
  --series-5: #d55181;
}

* { box-sizing: border-box; }

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

.top,
main,
footer {
  max-width: 1080px;
  margin: 0 auto;
  padding-left: 16px;
  padding-right: 16px;
}

/* ---------- header ---------- */

.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding-top: max(20px, env(safe-area-inset-top));
}

.brand { display: flex; align-items: center; gap: 10px; }
.mark { width: 12px; height: 12px; border-radius: 3px; background: var(--accent); transform: rotate(45deg); }
h1 { margin: 0; font-size: 18px; font-weight: 700; letter-spacing: -0.02em; }

.live { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
.live .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); }
.live[data-state='live'] .dot { background: var(--good); animation: pulse 2s infinite; }
.live[data-state='stale'] .dot { background: var(--warn); }

@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(12, 163, 12, 0.45); }
  70% { box-shadow: 0 0 0 6px rgba(12, 163, 12, 0); }
  100% { box-shadow: 0 0 0 0 rgba(12, 163, 12, 0); }
}

.status {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 16px;
  margin-top: 10px;
  font-size: 13px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.status b { color: var(--text); font-weight: 600; }

/* ---------- 뉴스 티커 ---------- */

.news {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 14px;
  padding: 8px 0 8px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
  font-size: 13px;
}

.news-label {
  flex: none;
  padding: 2px 7px;
  border-radius: 5px;
  background: var(--accent);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
}

.news-viewport {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  -webkit-mask-image: linear-gradient(90deg, transparent, #000 24px, #000 calc(100% - 24px), transparent);
  mask-image: linear-gradient(90deg, transparent, #000 24px, #000 calc(100% - 24px), transparent);
}

/* 같은 목록을 두 번 이어 붙이고 절반만큼 흘려서 끊김 없이 반복 */
.news-track { display: flex; width: max-content; animation: news-scroll var(--news-duration, 90s) linear infinite; }
.news:hover .news-track,
.news:focus-within .news-track { animation-play-state: paused; }

@keyframes news-scroll {
  from { transform: translateX(0); }
  to { transform: translateX(-50%); }
}

.news-item {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding-right: 28px;
  color: var(--text);
  text-decoration: none;
  white-space: nowrap;
}

.news-item:hover .news-title { text-decoration: underline; }
.news-tag { font-size: 11px; font-weight: 600; color: var(--accent); }
.news-time { font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; }

@media (prefers-reduced-motion: reduce) {
  .news-track { animation: none; }
  .news-viewport { overflow-x: auto; }
}

/* ---------- layout ---------- */

main { padding-bottom: 16px; }
section { margin-top: 28px; }

.sec-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px 12px;
  margin-bottom: 10px;
}

h2 { margin: 0; font-size: 15px; font-weight: 700; letter-spacing: -0.01em; }
.sec-note { font-size: 12px; color: var(--muted); }

/* 시트: 칸 사이 1px 간격에 선 색을 비춰 구분선을 만듦 */
.sheet {
  background: var(--sheet);
  border: 1px solid var(--line);
  border-radius: 14px;
  overflow: hidden;
}

.sheet.pad { padding: 14px 16px 10px; }

.strip,
.hero {
  display: grid;
  gap: 1px;
  background: var(--line);
}

.strip { grid-template-columns: repeat(6, minmax(0, 1fr)); }
.hero { grid-template-columns: repeat(2, minmax(0, 1fr)); }

.cell { background: var(--sheet); min-width: 0; }

.badge {
  flex: none;
  padding: 1px 6px;
  border-radius: 5px;
  font-size: 10.5px;
  font-weight: 600;
  background: var(--hover);
  color: var(--muted);
}

.badge:empty { display: none; }

.price { font-variant-numeric: tabular-nums; }
.price .v { border-radius: 4px; transition: background-color 0.6s ease; }
.price.flash-up .v { background: var(--up-wash); transition: none; }
.price.flash-down .v { background: var(--down-wash); transition: none; }

.d { font-weight: 600; font-variant-numeric: tabular-nums; }
.up { color: var(--up); }
.down { color: var(--down); }
.flat { color: var(--text-2); }

/* ---------- 미니 그래프 ---------- */

.spark { display: block; width: 100%; height: 26px; overflow: visible; color: var(--muted); }
.spark.up { color: var(--up); }
.spark.down { color: var(--down); }
.spark .line { fill: none; stroke: currentColor; stroke-width: 1.5; stroke-linejoin: round; vector-effect: non-scaling-stroke; }
.spark .area { fill: currentColor; opacity: 0.07; }
.spark .base { stroke: var(--axis); stroke-width: 1; stroke-dasharray: 2 2; vector-effect: non-scaling-stroke; }

/* ---------- 지수 ---------- */

.tick { padding: 12px 14px; }
.tick-head { display: flex; align-items: center; gap: 6px; min-width: 0; }
.tick-head .name { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tick .price { margin-top: 4px; font-size: 20px; font-weight: 700; letter-spacing: -0.01em; }
.tick .delta { font-size: 12px; }
.tile-note { margin-top: 2px; font-size: 11px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile-note:empty { display: none; }
.tick .spark { margin-top: 8px; }

/* ---------- 다가오는 일정 ---------- */

.events { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1px; background: var(--line); }

.ev {
  display: grid;
  grid-template-columns: 48px minmax(0, 1fr) auto;
  align-items: center;
  gap: 12px;
  padding: 11px 16px;
  background: var(--sheet);
}

.ev[hidden] { display: none; }
.ev.wide { grid-column: 1 / -1; }

.ev-dday {
  justify-self: start;
  padding: 2px 7px;
  border-radius: 6px;
  background: var(--hover);
  color: var(--text-2);
  font-size: 12px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.ev-dday.soon { background: var(--accent); color: #fff; }
.ev-main { min-width: 0; }
.ev-title { display: flex; align-items: center; gap: 6px; font-size: 14px; font-weight: 600; }
.ev-title span:last-child { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ev-sub { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ev-when { text-align: right; font-size: 13px; font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.ev-when small { display: block; font-size: 11px; font-weight: 400; color: var(--muted); }

.ev-kind { flex: none; font-size: 10.5px; font-weight: 600; padding: 1px 5px; border-radius: 4px; border: 1px solid var(--line); color: var(--muted); }
.ev-kind.earnings { color: var(--accent); border-color: currentColor; }
.ev-kind.rate { color: var(--text-2); border-color: currentColor; }

.more {
  display: block;
  margin: 8px auto 0;
  padding: 6px 12px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--text-2);
  font: inherit;
  font-size: 12px;
  cursor: pointer;
}

.more:hover { background: var(--hover); }

/* ---------- 한국 메모리 ---------- */

.stock { padding: 18px 20px; }
.stock .tick-head .name { font-size: 14px; font-weight: 600; color: var(--text); }
.stock-top { display: grid; grid-template-columns: minmax(0, 1fr) minmax(110px, 40%); align-items: center; gap: 16px; }
.stock-top .spark { height: 56px; }
.stock .price { margin-top: 6px; display: flex; align-items: baseline; gap: 2px; }
.stock .price .v { font-size: 36px; font-weight: 800; letter-spacing: -0.03em; line-height: 1.1; }
.stock .price .unit { font-size: 16px; font-weight: 600; color: var(--text-2); }
.stock .delta { margin-top: 2px; display: flex; align-items: baseline; flex-wrap: wrap; gap: 6px; font-size: 15px; }
.delta .muted { font-size: 12px; color: var(--muted); }

.meta {
  margin: 16px 0 0;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px 16px;
}

.meta dt { font-size: 12px; color: var(--muted); }
.meta dd { margin: 0; font-size: 14px; font-weight: 600; font-variant-numeric: tabular-nums; }
.meta small,
.c-ext small,
.since small { font-size: 12px; font-weight: 500; }

/* ---------- 미국 메모리 리스트 ---------- */

.list-head,
.row {
  display: grid;
  grid-template-columns: minmax(150px, 1.5fr) 96px 1fr 1.2fr 1fr 1.3fr 1fr;
  align-items: center;
  gap: 8px;
  padding: 0 16px;
}

.list-head { padding-top: 10px; padding-bottom: 10px; font-size: 12px; color: var(--muted); border-bottom: 1px solid var(--line); }
.list-head span:not(:first-child),
.row > div:not(.c-name) { text-align: right; }

.row { padding-top: 12px; padding-bottom: 12px; font-size: 14px; font-variant-numeric: tabular-nums; }
.row + .row { border-top: 1px solid var(--line); }

.c-name { display: flex; align-items: center; gap: 6px; min-width: 0; }
.c-name .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.c-name .ticker { font-size: 12px; color: var(--muted); }
.c-price { font-weight: 700; font-size: 15px; }
.c-price .unit { color: var(--text-2); font-weight: 500; margin-right: 1px; }
.c-price .v + .unit { margin: 0 0 0 1px; font-size: 13px; }
.row .lbl { display: none; }
.c-spark .spark { height: 26px; }
.list-head span:nth-child(2) { text-align: center; }

/* ---------- 차트 ---------- */

.seg { display: inline-flex; padding: 3px; gap: 2px; border-radius: 9px; background: var(--hover); }

.seg button {
  border: 0;
  padding: 5px 10px;
  border-radius: 7px;
  background: transparent;
  color: var(--text-2);
  font: inherit;
  font-size: 12px;
  cursor: pointer;
}

.seg button[aria-pressed='true'] { background: var(--sheet); color: var(--text); font-weight: 600; box-shadow: 0 0 0 1px var(--line); }

.legend { display: flex; flex-wrap: wrap; gap: 2px 4px; margin-bottom: 6px; }

.legend button {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 8px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: var(--text-2);
  font: inherit;
  font-size: 12px;
  cursor: pointer;
}

.legend button:hover { background: var(--hover); }
.legend button[aria-pressed='false'] { opacity: 0.35; }
.legend .val { color: var(--text); font-weight: 600; font-variant-numeric: tabular-nums; }

.key { display: inline-block; width: 14px; border-top: 2px solid currentColor; }
.key.dashed { border-top-style: dashed; }

.chart-wrap { position: relative; }
.chart { height: 320px; transition: opacity 0.2s; }
.chart.loading { opacity: 0.5; }

.tooltip {
  position: absolute;
  top: 8px;
  z-index: 3;
  min-width: 150px;
  padding: 8px 10px;
  border-radius: 9px;
  background: var(--sheet);
  border: 1px solid var(--line);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.1);
  font-size: 12px;
  pointer-events: none;
}

.tooltip .t { color: var(--muted); margin-bottom: 4px; }
.tooltip .row { display: flex; align-items: center; gap: 8px; padding: 0; line-height: 1.7; border: 0; font-size: 12px; }
.tooltip .row b { margin-left: auto; padding-left: 12px; font-variant-numeric: tabular-nums; }
.tooltip .row span:not(.key) { color: var(--text-2); }

.fine { margin: 8px 2px 0; font-size: 12px; color: var(--muted); }

/* ---------- 연동 표 ---------- */

.table-wrap { overflow-x: auto; }

.link-table { width: 100%; border-collapse: collapse; font-size: 13px; font-variant-numeric: tabular-nums; }

.link-table th,
.link-table td { padding: 10px 16px; text-align: right; white-space: nowrap; }

.link-table th:first-child,
.link-table td:first-child { text-align: left; }

.link-table thead th { font-size: 12px; font-weight: 400; color: var(--muted); border-bottom: 1px solid var(--line); }
.link-table tbody tr + tr td { border-top: 1px solid var(--line); }
.link-table .stat { display: block; font-size: 11px; color: var(--muted); }
.link-table .est { font-weight: 600; }
.link-table .sym { margin-left: 6px; font-size: 11px; color: var(--muted); }

/* ---------- footer ---------- */

footer { padding-top: 12px; padding-bottom: max(40px, env(safe-area-inset-bottom)); font-size: 12px; color: var(--muted); }
footer p { margin: 4px 0; }

/* ---------- 태블릿 ---------- */

@media (max-width: 900px) {
  .strip { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

/* ---------- 모바일 ---------- */

@media (max-width: 640px) {
  body { font-size: 14px; }
  section { margin-top: 24px; }
  .top { padding-top: max(16px, env(safe-area-inset-top)); }

  .status { gap: 2px 12px; font-size: 12px; }
  .status span { white-space: nowrap; }

  .strip { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .strip > :last-child:nth-child(odd) { grid-column: 1 / -1; }
  .tick { padding: 10px 12px; }
  .tick .price { font-size: 18px; }

  .hero { grid-template-columns: minmax(0, 1fr); }
  .events { grid-template-columns: minmax(0, 1fr); }
  .ev { grid-template-columns: 44px minmax(0, 1fr) auto; gap: 10px; padding: 10px 14px; }
  .stock { padding: 16px; }
  .stock .price .v { font-size: 32px; }
  .stock-top { grid-template-columns: minmax(0, 1fr); gap: 10px; }
  .stock-top .spark { height: 44px; }

  .list-head { display: none; }
  .row {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    grid-template-areas:
      'name price price'
      'spark delta delta'
      'close ext since';
    row-gap: 2px;
    padding: 12px 14px;
  }
  .c-name { grid-area: name; flex-wrap: wrap; align-content: center; }
  .c-spark { grid-area: spark; align-self: center; }
  .c-spark .spark { height: 22px; max-width: 96px; }
  .c-price { grid-area: price; }
  .c-delta { grid-area: delta; font-size: 12px; }
  .c-close,
  .c-ext,
  .c-since { margin-top: 8px; font-size: 12px; text-align: left !important; }
  .c-close { grid-area: close; }
  .c-ext { grid-area: ext; }
  .c-since { grid-area: since; text-align: right !important; }
  .row .lbl { display: block; font-size: 11px; color: var(--muted); }
  .c-ext small { display: block; }

  .sheet.pad { padding: 12px 10px 8px; }
  .chart { height: 250px; }
  .seg button { padding: 7px 10px; }

  .link-table { font-size: 12px; }
  .link-table th,
  .link-table td { padding: 9px 10px; }
  .link-table .sym { display: block; margin-left: 0; }
  /* 가로 스크롤해도 종목명은 보이게 */
  .link-table th:first-child,
  .link-table td:first-child { position: sticky; left: 0; z-index: 1; background: var(--sheet); }
}
