.viz-root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface-1: #fcfcfb;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --series-1: #2a78d6;   /* 올해 */
  --series-2: #eb6834;   /* 작년 */
  --up: #006300;
  --down: #d03b3b;
  --warn: #fab219;
  --row-hover: rgba(11, 11, 11, 0.035);
}
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) .viz-root {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface-1: #1a1a19;
    --text-primary: #f0efec;
    --text-secondary: #c3c2b7;
    --text-muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --series-1: #3987e5;
    --series-2: #d95926;
    --up: #0ca30c;
    --down: #e66767;
    --row-hover: rgba(255, 255, 255, 0.04);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { font-family: system-ui, -apple-system, "Segoe UI", "Malgun Gothic", sans-serif; }

.viz-root {
  min-height: 100vh;
  background: var(--page);
  color: var(--text-primary);
  padding: 24px clamp(12px, 3vw, 40px) 48px;
  max-width: 1400px;
  margin: 0 auto;
  font-size: 14px;
}

.top { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; flex-wrap: wrap; margin-bottom: 20px; }
h1 { font-size: 22px; margin: 0 0 4px; }
h2 { font-size: 16px; margin: 0; }
.small-title { font-size: 13px; margin: 4px 0 8px; color: var(--text-secondary); font-weight: 600; }
.sub { margin: 0; color: var(--text-secondary); }
.muted { color: var(--text-muted); }
.small { font-size: 12px; margin: 0; }
.status { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.badge { background: var(--warn); color: #0b0b0b; font-weight: 600; font-size: 12px; padding: 4px 10px; border-radius: 999px; }
button, input {
  font: inherit; color: var(--text-primary); background: var(--surface-1);
  border: 1px solid var(--border); border-radius: 8px; padding: 6px 12px;
}
button { cursor: pointer; }
button:hover { background: var(--row-hover); }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi, .card { background: var(--surface-1); border: 1px solid var(--border); border-radius: 12px; }
.kpi { padding: 16px 18px; }
.kpi .label { color: var(--text-secondary); font-size: 13px; }
.kpi .value { font-size: 28px; font-weight: 700; margin: 6px 0 4px; letter-spacing: -0.01em; }
.kpi .foot { font-size: 12px; color: var(--text-muted); display: flex; gap: 8px; flex-wrap: wrap; }

.card { padding: 18px; margin-bottom: 16px; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.tools { display: flex; align-items: center; gap: 10px; }

.legend { display: flex; gap: 16px; font-size: 13px; color: var(--text-secondary); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 16px; height: 2px; display: inline-block; border-radius: 1px; }

.chart { position: relative; width: 100%; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart text { font-size: 11px; fill: var(--text-muted); font-variant-numeric: tabular-nums; }
.chart .series-label { font-size: 12px; font-weight: 600; fill: var(--text-secondary); }

.tooltip {
  position: fixed; pointer-events: none; z-index: 10;
  background: var(--surface-1); color: var(--text-primary);
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12); font-size: 12px; min-width: 180px;
}
.tooltip .row { display: flex; justify-content: space-between; gap: 16px; align-items: center; }
.tooltip .row span:first-child { display: inline-flex; align-items: center; gap: 6px; color: var(--text-secondary); }
.tooltip i { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.tooltip b { font-variant-numeric: tabular-nums; }
.tooltip .head { font-weight: 600; margin-bottom: 4px; }

.table-wrap { overflow-x: auto; }
table.grid { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.grid th, .grid td { padding: 8px 10px; border-bottom: 1px solid var(--grid); white-space: nowrap; }
.grid th { font-size: 12px; font-weight: 600; color: var(--text-secondary); text-align: right; position: sticky; top: 0; background: var(--surface-1); }
.grid th.l, .grid td.l { text-align: left; }
.grid td { text-align: right; }
.grid tbody tr:hover { background: var(--row-hover); }
.grid tr.future td { color: var(--text-muted); }
.grid tr.today td:first-child { box-shadow: inset 3px 0 0 var(--series-1); }
.grid tfoot td { font-weight: 700; border-top: 2px solid var(--axis); border-bottom: none; }
.grid th.group { text-align: center; border-bottom: 1px solid var(--grid); }
.grid th.sortable { cursor: pointer; user-select: none; }
.grid th.sortable:hover { color: var(--text-primary); }
.grid .sub-line { display: block; font-size: 11px; color: var(--text-muted); font-weight: 400; }
.grid td.name { white-space: normal; min-width: 240px; }
.strike { text-decoration: line-through; color: var(--text-muted); font-size: 11px; margin-right: 4px; }

.delta { font-weight: 600; }
.delta.up { color: var(--up); }
.delta.down { color: var(--down); }
.pill { font-size: 11px; padding: 1px 6px; border-radius: 999px; margin-left: 6px; }
.pill.live { background: var(--series-1); color: #fff; }
.stock-low { color: var(--text-primary); font-weight: 600; }
.stock-low::before { content: "⚠ "; color: var(--warn); }

.best { list-style: none; margin: 0 0 16px; padding: 0; display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 8px; }
.best li { border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; display: flex; gap: 10px; align-items: flex-start; }
.best .rank { font-weight: 700; color: var(--series-1); min-width: 16px; }
.best .nm { font-size: 13px; line-height: 1.35; }
.best .v { font-size: 12px; color: var(--text-secondary); margin-top: 2px; font-variant-numeric: tabular-nums; }

.actions { display: flex; gap: 8px; }
button.primary, a.primary { background: var(--series-1); color: #fff; border-color: transparent; font-weight: 600; }
button.primary:hover { filter: brightness(1.08); background: var(--series-1); }
button:disabled { opacity: 0.6; cursor: wait; }
.chart rect#hit { touch-action: pan-y; cursor: crosshair; }
.export-stamp { display: none; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 20;
  background: #0b0b0b; color: #fff; padding: 10px 16px; border-radius: 999px; font-size: 13px;
}

.period { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; justify-content: flex-end; margin-bottom: 14px; }
.period select, .period input { font: inherit; color: var(--text-primary); background: var(--surface-1); border: 1px solid var(--border); border-radius: 8px; padding: 6px 10px; }
.period input:disabled { opacity: 0.5; }
.period .dates { display: inline-flex; gap: 6px; align-items: center; }
body.gated .period { display: none; }

.notice {border: 1px solid var(--warn); background: color-mix(in srgb, var(--warn) 12%, var(--surface-1)); border-radius: 10px; padding: 10px 14px; margin-bottom: 14px; font-size: 13px; }
.notice::before { content: "⚠ "; }
.gate { max-width: 420px; margin: 40px auto; padding: 28px; text-align: center; }
.gate h2 { margin-bottom: 8px; }
.gate p { color: var(--text-secondary); margin: 0 0 16px; line-height: 1.5; }
.gate form { display: flex; flex-direction: column; gap: 10px; }
.gate input { padding: 11px 12px; font-size: 16px; }
.gate .primary, .gate a.primary { padding: 11px 12px; display: block; text-decoration: none; border-radius: 8px; }
.gate .err { color: var(--down); font-size: 13px; min-height: 18px; margin: 0; }
body.gated .kpis, body.gated .card:not(.gate), body.gated #save-img, body.gated #refresh { display: none; }

/* ---------- 이미지 저장 중: 화면 크기와 상관없이 PC 보고용 너비로 ---------- */
.viz-root.capturing { width: 1200px; max-width: none; min-height: 0; padding: 28px 32px 32px; }
.viz-root.capturing [data-noexport] { display: none !important; }
.viz-root.capturing .table-wrap { overflow: visible; }
.viz-root.capturing .export-stamp { display: block; margin-top: 8px; font-size: 12px; color: var(--text-muted); text-align: right; }

/* ---------- 모바일 ---------- */
@media (max-width: 720px) {
  .viz-root:not(.capturing) { padding: 14px 12px 32px; font-size: 13px; }
  .viz-root:not(.capturing) .top { flex-direction: column; align-items: stretch; gap: 10px; margin-bottom: 14px; }
  .viz-root:not(.capturing) h1 { font-size: 19px; }
  .viz-root:not(.capturing) .sub { font-size: 12px; line-height: 1.5; }
  .viz-root:not(.capturing) .status { justify-content: space-between; }
  .viz-root:not(.capturing) .actions { width: 100%; }
  .viz-root:not(.capturing) .actions button { flex: 1; padding: 10px 12px; }

  .viz-root:not(.capturing) .period { display: grid; grid-template-columns: 1fr auto; justify-content: stretch; }
  .viz-root:not(.capturing) .period select { grid-column: 1 / -1; padding: 9px 10px; font-size: 16px; }
  .viz-root:not(.capturing) .period .dates { min-width: 0; }
  .viz-root:not(.capturing) .period .dates input { flex: 1; min-width: 0; padding: 8px 6px; font-size: 14px; }
  .viz-root:not(.capturing) .kpis { grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 12px; }
  .viz-root:not(.capturing) .kpi { padding: 12px; }
  .viz-root:not(.capturing) .kpi .label { font-size: 11px; }
  .viz-root:not(.capturing) .kpi .value { font-size: clamp(17px, 5.2vw, 22px); }
  .viz-root:not(.capturing) .kpi .foot { font-size: 11px; gap: 4px; flex-direction: column; }

  .viz-root:not(.capturing) .card { padding: 12px; border-radius: 10px; margin-bottom: 12px; }
  .viz-root:not(.capturing) .card-head { margin-bottom: 8px; }
  .viz-root:not(.capturing) .tools { width: 100%; }
  .viz-root:not(.capturing) .tools input { flex: 1; min-width: 0; padding: 9px 12px; font-size: 16px; }

  .viz-root:not(.capturing) .grid th,
  .viz-root:not(.capturing) .grid td { padding: 7px 8px; font-size: 12px; }
  .viz-root:not(.capturing) .grid td.name { min-width: 170px; }
  /* 첫 열 고정: 옆으로 밀어도 날짜·상품명이 보이게 */
  .viz-root:not(.capturing) .grid thead tr:first-child th:first-child,
  .viz-root:not(.capturing) .grid tbody td:first-child,
  .viz-root:not(.capturing) .grid tfoot td:first-child { position: sticky; left: 0; z-index: 1; background: var(--surface-1); box-shadow: 1px 0 0 var(--grid); }
  .viz-root:not(.capturing) .grid thead tr:first-child th:first-child { z-index: 2; }
  .viz-root:not(.capturing) .table-wrap { margin: 0 -12px; padding: 0 12px; -webkit-overflow-scrolling: touch; }

  .viz-root:not(.capturing) .best { grid-template-columns: 1fr; gap: 6px; }
  .viz-root:not(.capturing) .best li { padding: 8px 10px; }
}
