/*
 * 색 토큰은 dataviz 가이드의 검증된 레퍼런스 팔레트를 쓴다.
 * 차트가 올라가는 면(--surface-1)은 검증에 쓰인 값(#fcfcfb / #1a1a19)에서 바꾸지 않는다 —
 * 범주색 8개의 대비·CVD 분리 검증이 그 표면을 기준으로 통과한 것이기 때문이다.
 * 대신 페이지 바닥(--page)을 한 단계 더 눌러서 카드가 떠 보이게 하고,
 * 입력란·표 머리 같은 안쪽 면은 --surface-2 로 한 단계 더 들어가게 한다.
 *
 * 레이아웃은 모바일 우선. 좁으면 한 줄로 쌓이고, 넓어지면 카드가 2열이 된다.
 */
:root {
  color-scheme: light;

  --page: #e9e7e0;
  --surface-1: #fcfcfb;
  --surface-2: #f3f1ea;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.1);
  --border-strong: rgba(11, 11, 11, 0.18);

  --shadow-sm: 0 1px 2px rgba(11, 11, 11, 0.05);
  --shadow-md: 0 1px 2px rgba(11, 11, 11, 0.04), 0 6px 18px rgba(11, 11, 11, 0.06);
  --topbar-bg: rgba(252, 252, 251, 0.82);

  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;
  --series-5: #e87ba4;
  --series-6: #008300;
  --series-7: #4a3aa7;
  --series-8: #e34948;

  --good: #0ca30c;
  --warning: #fab219;
  --serious: #ec835a;
  --critical: #d03b3b;

  --radius: 14px;
  --radius-sm: 9px;
  --font: system-ui, -apple-system, "Segoe UI", sans-serif;
  --gutter: 16px;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --page: #090909;
    --surface-1: #1a1a19;
    --surface-2: #242422;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.1);
    --border-strong: rgba(255, 255, 255, 0.2);
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.5);
    --shadow-md: 0 1px 2px rgba(0, 0, 0, 0.5), 0 8px 24px rgba(0, 0, 0, 0.45);
    --topbar-bg: rgba(26, 26, 25, 0.82);
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --series-5: #d55181;
    --series-6: #008300;
    --series-7: #9085e9;
    --series-8: #e66767;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #090909;
  --surface-1: #1a1a19;
  --surface-2: #242422;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted: #898781;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.1);
  --border-strong: rgba(255, 255, 255, 0.2);
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.5);
  --shadow-md: 0 1px 2px rgba(0, 0, 0, 0.5), 0 8px 24px rgba(0, 0, 0, 0.45);
  --topbar-bg: rgba(26, 26, 25, 0.82);
  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --series-4: #c98500;
  --series-5: #d55181;
  --series-6: #008300;
  --series-7: #9085e9;
  --series-8: #e66767;
}

* {
  box-sizing: border-box;
}

/*
 * hidden 속성은 브라우저 기본 스타일(display:none)로 동작하는데, 요소에 display를
 * 따로 선언하면 그 선언이 이겨서 숨겨지지 않는다 — .view-toggle(inline-flex)에서
 * 실제로 겪은 버그다. 전역에서 못을 박아 둔다.
 */
[hidden] {
  display: none !important;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

body {
  margin: 0;
  font-family: var(--font);
  background: var(--page);
  color: var(--text-primary);
  font-size: 15px;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}

/* ── 상단바 ───────────────────────────────────────────── */

.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  background: var(--topbar-bg);
  backdrop-filter: saturate(180%) blur(12px);
  -webkit-backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--border);
  padding-top: env(safe-area-inset-top);
}

.topbar-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 11px var(--gutter) 9px;
}

.topbar h1 {
  margin: 0;
  font-size: 1rem;
  font-weight: 680;
  letter-spacing: -0.015em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: flex;
  align-items: center;
  gap: 9px;
}

/* 제목 앞 작은 악센트 — 로고 없이도 브랜드 자리를 만들어 준다. */
.topbar h1::before {
  content: "";
  width: 4px;
  height: 17px;
  border-radius: 2px;
  background: var(--series-1);
  flex: none;
}

.topbar-actions {
  display: flex;
  gap: 6px;
  align-items: center;
  flex: none;
}

/* ── 탭 ───────────────────────────────────────────────── */

.tabs {
  display: flex;
  gap: 2px;
  overflow-x: auto;
  padding: 0 var(--gutter);
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
}

.tabs::-webkit-scrollbar {
  display: none;
}

.tab {
  font: inherit;
  font-size: 0.9rem;
  font-weight: 550;
  padding: 9px 14px;
  border: none;
  border-bottom: 2.5px solid transparent;
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  white-space: nowrap;
  min-height: 42px;
  border-radius: var(--radius-sm) var(--radius-sm) 0 0;
  transition: color 0.12s, background 0.12s;
}

.tab:hover {
  color: var(--text-primary);
  background: var(--surface-2);
}

.tab[aria-selected="true"] {
  color: var(--text-primary);
  font-weight: 650;
  border-bottom-color: var(--series-1);
  background: transparent;
}

/* ── 소스 상태 줄 ─────────────────────────────────────── */

main {
  max-width: 1640px;
  margin: 0 auto;
  padding: 16px var(--gutter) 40px;
}

.status-strip {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 16px;
}

/* 상태는 색만으로 전달하지 않는다 — 기호 + 텍스트를 함께 쓴다. */
.src {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.76rem;
  font-weight: 560;
  padding: 5px 11px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface-1);
  color: var(--text-secondary);
  white-space: nowrap;
  box-shadow: var(--shadow-sm);
}

.src .mark {
  font-size: 0.66rem;
  line-height: 1;
}

.src.ready .mark {
  color: var(--good);
}

.src.locked {
  border-color: var(--border-strong);
}

.src.locked .mark {
  color: var(--critical);
}

.src a {
  color: var(--series-1);
  text-decoration: none;
  border-bottom: 1px solid currentColor;
}

/* ── 카드 ─────────────────────────────────────────────── */

.cards {
  display: grid;
  gap: 16px;
  /* 좁으면 1열, 넓으면 2열. 차트가 좁아지면 읽을 수 없으니 640px 밑으로는 안 쪼갠다. */
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 640px), 1fr));
}

.card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  min-width: 0;
  box-shadow: var(--shadow-md);
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: flex-end;
}

.toolbar-buttons {
  display: flex;
  gap: 6px;
  align-items: center;
  margin-left: auto;
}

.params {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 11px;
  padding-top: 11px;
  border-top: 1px solid var(--border);
}

.params:empty {
  display: none;
}

/* ── 폼 ───────────────────────────────────────────────── */

.field {
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 0;
  flex: 1 1 130px;
}

.field.grow {
  flex: 1 1 150px;
}

.field.grow2 {
  flex: 2 1 220px;
}

.field.narrow {
  flex: 0 1 110px;
}

.field > span {
  font-size: 0.68rem;
  color: var(--text-muted);
  font-weight: 620;
  letter-spacing: 0.03em;
}

.inline-field {
  display: flex;
}

select,
input[type="text"],
input[type="number"],
input[type="date"] {
  font: inherit;
  /* iOS는 16px 미만 입력란에 포커스하면 화면을 확대한다. 그걸 막는 최소값. */
  font-size: 16px;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  /* 입력란은 카드보다 한 단계 안쪽으로 들어가 보이게 한다. */
  background: var(--surface-2);
  color: var(--text-primary);
  min-width: 0;
  width: 100%;
  min-height: 38px;
  transition: border-color 0.12s;
}

select:hover,
input[type="text"]:hover,
input[type="number"]:hover,
input[type="date"]:hover {
  border-color: var(--border-strong);
}

@media (min-width: 700px) {
  select,
  input[type="text"],
  input[type="number"],
  input[type="date"] {
    font-size: 0.86rem;
  }
}

select:focus-visible,
input:focus-visible,
.btn:focus-visible,
.toggle:focus-visible,
.tab:focus-visible {
  outline: 2px solid var(--series-1);
  outline-offset: 1px;
}

.btn {
  font: inherit;
  font-size: 0.84rem;
  font-weight: 580;
  padding: 8px 14px;
  min-height: 38px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface-1);
  color: var(--text-primary);
  cursor: pointer;
  white-space: nowrap;
  box-shadow: var(--shadow-sm);
  transition: border-color 0.12s, background 0.12s;
}

.btn:hover {
  border-color: var(--border-strong);
  background: var(--surface-2);
}

.btn:active {
  transform: translateY(0.5px);
}

.btn.ghost {
  background: transparent;
  color: var(--text-secondary);
  box-shadow: none;
}

.btn.small {
  padding: 7px 12px;
  font-size: 0.8rem;
}

.btn.icon {
  padding: 7px 10px;
}

.btn.add {
  margin-top: 16px;
  width: 100%;
  border-style: dashed;
  background: transparent;
  color: var(--text-secondary);
  box-shadow: none;
}

@media (min-width: 700px) {
  .btn.add {
    width: auto;
    padding-inline: 22px;
  }
}

/* ── 카드 본문 ────────────────────────────────────────── */

.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-top: 16px;
}

.card-head h2 {
  margin: 0;
  font-size: 0.97rem;
  font-weight: 680;
  letter-spacing: -0.01em;
  min-width: 0;
}

.badges {
  display: flex;
  gap: 5px;
  flex-wrap: wrap;
  margin-top: 7px;
}

.badges:empty {
  display: none;
}

.badge {
  font-size: 0.68rem;
  font-weight: 560;
  color: var(--text-secondary);
  background: var(--surface-2);
  border: 1px solid transparent;
  border-radius: 999px;
  padding: 2px 9px;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

.view-toggle {
  display: inline-flex;
  flex: none;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
  background: var(--surface-2);
}

.toggle {
  font: inherit;
  font-size: 0.78rem;
  padding: 6px 13px;
  min-height: 34px;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
}

.toggle.active {
  background: var(--surface-1);
  color: var(--text-primary);
  font-weight: 640;
  box-shadow: var(--shadow-sm);
}

.chart {
  width: 100%;
  height: 300px;
  margin-top: 10px;
}

@media (min-width: 700px) {
  .chart {
    height: 380px;
  }
}

.chart.tall {
  height: 380px;
}

@media (min-width: 700px) {
  .chart.tall {
    height: 480px;
  }
}

/* 표 전용 위젯에서 빈 차트 영역이 자리를 차지하지 않게 한다. */
.chart:empty {
  height: 0;
  margin-top: 0;
}

.note {
  margin: 12px 0 0;
  font-size: 0.78rem;
  color: var(--text-secondary);
  background: var(--surface-2);
  border-left: 3px solid var(--axis);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  padding: 8px 11px;
}

.note:empty {
  display: none;
}

.status {
  margin: 8px 0 0;
  font-size: 0.74rem;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.status:empty {
  display: none;
}

.status.error {
  color: var(--critical);
  font-weight: 560;
}

.status a {
  color: var(--series-1);
}

/* ── 표 ───────────────────────────────────────────────── */

.table-wrap {
  margin-top: 12px;
  max-height: 420px;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  -webkit-overflow-scrolling: touch;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.79rem;
}

th,
td {
  padding: 7px 11px;
  text-align: left;
  border-bottom: 1px solid var(--grid);
  white-space: nowrap;
}

th {
  position: sticky;
  top: 0;
  background: var(--surface-2);
  color: var(--text-secondary);
  font-weight: 640;
  z-index: 1;
}

tbody tr:last-child td {
  border-bottom: none;
}

td.right,
th.right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

tbody tr:hover {
  background: var(--surface-2);
}

td a {
  color: var(--series-1);
}

/* 리스크 등급은 색 외에 기호를 함께 쓴다. */
.level-위험 {
  color: var(--critical);
  font-weight: 680;
}

.level-주의 {
  color: var(--serious);
  font-weight: 620;
}

.level-정보 {
  color: var(--text-secondary);
}

/* ── 자동완성 ─────────────────────────────────────────── */

.autocomplete {
  position: relative;
}

.autocomplete-list {
  position: absolute;
  z-index: 20;
  top: calc(100% + 3px);
  left: 0;
  right: 0;
  max-height: 240px;
  overflow: auto;
  background: var(--surface-1);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-md);
}

.autocomplete-list button {
  display: block;
  width: 100%;
  text-align: left;
  font: inherit;
  font-size: 0.84rem;
  padding: 9px 11px;
  min-height: 40px;
  border: none;
  background: transparent;
  color: var(--text-primary);
  cursor: pointer;
}

.autocomplete-list button:hover,
.autocomplete-list button[aria-selected="true"] {
  background: var(--surface-2);
}

.autocomplete-list .code {
  color: var(--text-muted);
  font-size: 0.74rem;
  margin-left: 6px;
  font-variant-numeric: tabular-nums;
}

/* ── 푸터 ─────────────────────────────────────────────── */

.footer {
  max-width: 1640px;
  margin: 0 auto;
  padding: 18px var(--gutter) calc(34px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  color: var(--text-muted);
  font-size: 0.74rem;
}

.footer p {
  margin: 0 0 6px;
}

@media (prefers-reduced-motion: reduce) {
  * {
    transition: none !important;
  }
}
