:root {
  --bg: #0f1115;
  --panel: #181b22;
  --panel-2: #1f232c;
  --border: #2a2f3a;
  --text: #e6e9ef;
  --muted: #8a90a0;
  --accent: #4f9eff;
  --accent-hover: #6db0ff;
  --positive: #4ade80;
  --negative: #f87171;
  --warning: #fbbf24;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Apple SD Gothic Neo", "Malgun Gothic", system-ui, sans-serif;
  font-size: 14px;
  line-height: 1.5;
}

.topbar {
  border-bottom: 1px solid var(--border);
  padding: 16px 24px 0;
  background: var(--panel);
  position: sticky;
  top: 0;
  z-index: 10;
}

.topbar h1 {
  margin: 0 0 12px;
  font-size: 18px;
  font-weight: 600;
}

.tabs {
  display: flex;
  gap: 4px;
}

.tab {
  background: transparent;
  border: none;
  color: var(--muted);
  padding: 10px 16px;
  cursor: pointer;
  font-size: 14px;
  border-bottom: 2px solid transparent;
  transition: color 0.15s, border-color 0.15s;
}

.tab:hover {
  color: var(--text);
}

.tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

main {
  padding: 24px;
  max-width: 1400px;
  margin: 0 auto;
}

.panel {
  display: none;
}

.panel.active {
  display: block;
}

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.panel-header h2 {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
}

.btn-group {
  display: flex;
  gap: 8px;
}

button {
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 16px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  font-family: inherit;
  transition: background 0.15s, border-color 0.15s;
}

button:hover {
  background: #2a2f3a;
  border-color: #3a4150;
}

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 500;
}

button.primary:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

button.danger {
  border-color: var(--negative);
  color: var(--negative);
}

button.danger:hover {
  background: rgba(248, 113, 113, 0.1);
}

input, select {
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 12px;
  border-radius: 6px;
  font-size: 14px;
  font-family: inherit;
}

input:focus, select:focus {
  outline: none;
  border-color: var(--accent);
}

input[type="number"] {
  width: 120px;
}

.summary-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 12px;
  margin-bottom: 24px;
}

.summary-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px;
}

.summary-card .label {
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 6px;
}

.summary-card .value {
  font-size: 20px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.summary-card .value.positive {
  color: var(--positive);
}

.summary-card .value.negative {
  color: var(--negative);
}

.table-wrap {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: auto;
}

.chart-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 24px;
  position: relative;
}

.chart-card .chart-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 8px;
}

.chart-card .chart-title {
  font-size: 13px;
  color: var(--muted);
}

.chart-card .chart-final {
  font-size: 16px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.chart-card svg {
  display: block;
  width: 100%;
  height: 420px;
}

.chart-card svg .grid {
  stroke: var(--border);
  stroke-width: 1;
  opacity: 0.5;
}

.chart-card svg .zero {
  stroke: var(--muted);
  stroke-width: 1;
  stroke-dasharray: 3 3;
  opacity: 0.7;
}

.chart-card svg .big-win {
  fill: var(--warning);
  stroke: var(--panel);
  stroke-width: 2;
}

.chart-card svg .line {
  fill: none;
  stroke: var(--accent);
  stroke-width: 2;
}

.chart-card svg .area-pos {
  fill: rgba(74, 222, 128, 0.15);
}

.chart-card svg .area-neg {
  fill: rgba(248, 113, 113, 0.15);
}

.chart-card svg .label {
  fill: var(--muted);
  font-size: 11px;
  font-family: inherit;
}

.chart-card svg .dot {
  fill: var(--accent);
}

.chart-card svg .crosshair {
  stroke: var(--accent);
  stroke-width: 1;
  stroke-dasharray: 3 3;
  opacity: 0.6;
  pointer-events: none;
}

.chart-card svg .cursor-dot {
  fill: var(--accent);
  stroke: var(--panel);
  stroke-width: 2;
  pointer-events: none;
}

.chart-card svg .cursor-dot.positive {
  fill: var(--positive);
}

.chart-card svg .cursor-dot.negative {
  fill: var(--negative);
}

.chart-tooltip {
  position: absolute;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 12px;
  line-height: 1.5;
  pointer-events: none;
  z-index: 5;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.5);
  white-space: nowrap;
  max-width: 280px;
}

.chart-tooltip[hidden] {
  display: none;
}

.chart-tooltip .name {
  font-weight: 500;
  margin-bottom: 4px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chart-tooltip .row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  color: var(--muted);
}

.chart-tooltip .row b {
  color: var(--text);
  font-weight: 500;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th, td {
  text-align: left;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  font-variant-numeric: tabular-nums;
}

th {
  background: var(--panel-2);
  font-weight: 500;
  font-size: 13px;
  color: var(--muted);
  position: sticky;
  top: 0;
  cursor: pointer;
  user-select: none;
}

th[data-sort]::after {
  content: "";
  display: inline-block;
  width: 0;
  height: 0;
  margin-left: 6px;
  vertical-align: middle;
  opacity: 0.4;
}

th.sort-asc::after {
  border-left: 4px solid transparent;
  border-right: 4px solid transparent;
  border-bottom: 5px solid currentColor;
  opacity: 1;
}

th.sort-desc::after {
  border-left: 4px solid transparent;
  border-right: 4px solid transparent;
  border-top: 5px solid currentColor;
  opacity: 1;
}

td.num, th.num {
  text-align: right;
}

tbody tr:hover {
  background: rgba(79, 158, 255, 0.05);
}

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

/* 수익 행 강조 */
tbody tr.highlight-medium {
  background: rgba(74, 222, 128, 0.10);
  box-shadow: inset 3px 0 0 var(--positive);
}

tbody tr.highlight-medium:hover {
  background: rgba(74, 222, 128, 0.16);
}

tbody tr.highlight-big {
  background: rgba(251, 191, 36, 0.14);
  box-shadow: inset 3px 0 0 var(--warning);
  font-weight: 600;
}

tbody tr.highlight-big:hover {
  background: rgba(251, 191, 36, 0.22);
}

tbody tr.row-discontinued td {
  color: var(--muted);
  opacity: 0.65;
}

.inline-check {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  color: var(--muted);
  font-size: 13px;
  cursor: pointer;
  user-select: none;
}

.inline-check input[type="checkbox"] {
  width: auto;
  margin: 0;
}

.positive {
  color: var(--positive);
}

.negative {
  color: var(--negative);
}

.muted {
  color: var(--muted);
}

.tag {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 12px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--muted);
}

.tag.type-메인 { color: #93c5fd; border-color: #3b82f6; }
.tag.type-하이롤러 { color: #fbbf24; border-color: #d97706; }
.tag.type-시리즈 { color: #c4b5fd; border-color: #8b5cf6; }
.tag.type-마이너 { color: #9ca3af; border-color: #6b7280; }

.row-actions {
  display: flex;
  gap: 4px;
  justify-content: flex-end;
}

.row-actions button {
  padding: 4px 10px;
  font-size: 12px;
}

/* Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal-backdrop[hidden] {
  display: none;
}

.modal {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  width: 90%;
  max-width: 760px;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}

.modal-header h3 {
  margin: 0;
  font-size: 16px;
}

.modal-body {
  padding: 16px 20px;
  overflow-y: auto;
  flex: 1;
}

.modal-footer {
  padding: 12px 20px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.close {
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 22px;
  padding: 0;
  width: 32px;
  height: 32px;
  cursor: pointer;
  line-height: 1;
}

.close:hover {
  color: var(--text);
}

.field {
  margin-bottom: 16px;
}

.field label {
  display: block;
  margin-bottom: 6px;
  font-size: 13px;
  color: var(--muted);
}

.field input,
.field select {
  width: 100%;
}

.field .hint {
  color: var(--muted);
  font-weight: normal;
  font-size: 12px;
}

#today-candidates {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 8px;
}

.candidate {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
  white-space: nowrap;
}

.candidate:hover {
  border-color: var(--accent);
}

.candidate.selected {
  border-color: var(--accent);
  background: rgba(79, 158, 255, 0.1);
}

.candidate > span:first-child {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}

.candidate .meta {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 12px;
  flex-shrink: 0;
}

.picker-details {
  margin-bottom: 8px;
}

.picker-details summary {
  cursor: pointer;
  padding: 8px 0;
  color: var(--muted);
  font-size: 13px;
  user-select: none;
}

.picker-details summary:hover {
  color: var(--text);
}

#all-candidates {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 8px;
  max-height: 240px;
  overflow-y: auto;
}

#catalog-search {
  width: 100%;
  margin-top: 8px;
}

.inline-form {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  padding: 8px 0;
}

.inline-form input,
.inline-form select {
  width: 100%;
}

.inline-form #btn-register-tourn {
  grid-column: 1 / -1;
}

.selected-tournament {
  margin-top: 12px;
  padding: 10px 12px;
  background: rgba(79, 158, 255, 0.1);
  border: 1px solid var(--accent);
  border-radius: 6px;
  font-size: 13px;
}

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--panel);
  border: 1px solid var(--border);
  padding: 12px 20px;
  border-radius: 8px;
  box-shadow: var(--shadow);
  z-index: 200;
  font-size: 13px;
}

.toast.error {
  border-color: var(--negative);
}

.toast.success {
  border-color: var(--positive);
}

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

/* 모바일 전용 카드 컨테이너 — 데스크탑에선 숨김 */
.record-card-list,
.tournament-card-list,
.topwins-card-list {
  display: none;
}

.rank-badge {
  display: inline-block;
  background: var(--panel-2);
  color: var(--text);
  font-weight: 600;
  padding: 2px 10px;
  border-radius: 4px;
  font-size: 12px;
  white-space: nowrap;
  min-width: 24px;
  text-align: center;
}

.rank-badge.medal-1 {
  background: linear-gradient(135deg, #fde047, #f59e0b);
  color: #422006;
  box-shadow: 0 0 0 1px rgba(251, 191, 36, 0.4);
}

.rank-badge.medal-2 {
  background: linear-gradient(135deg, #f1f5f9, #94a3b8);
  color: #1f2937;
  box-shadow: 0 0 0 1px rgba(148, 163, 184, 0.4);
}

.rank-badge.medal-3 {
  background: linear-gradient(135deg, #fdba74, #b45309);
  color: #fff;
  box-shadow: 0 0 0 1px rgba(180, 83, 9, 0.4);
}

/* 기록 카드 */
.record-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 14px;
}

.record-card.highlight-medium {
  border-left: 4px solid var(--positive);
}

.record-card.highlight-big {
  border-left: 4px solid var(--warning);
}

.record-card .card-title {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 8px;
  flex-wrap: wrap;
}

.record-card .card-title .date {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

.record-card .card-title .name {
  font-weight: 500;
  flex: 1 1 auto;
  min-width: 0;
  word-break: keep-all;
}

.record-card .card-row {
  display: flex;
  gap: 16px;
  padding: 3px 0;
  font-size: 13px;
  flex-wrap: wrap;
  font-variant-numeric: tabular-nums;
}

.record-card .card-row > span {
  display: inline-flex;
  align-items: baseline;
  gap: 6px;
}

.record-card .card-row .lbl {
  color: var(--muted);
  font-size: 12px;
}

/* 토너별 카드 */
.tournament-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 14px;
}

.tournament-card .name {
  font-weight: 500;
  margin-bottom: 10px;
  word-break: keep-all;
}

.tournament-card .grid-2col {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px 12px;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}

.tournament-card .grid-2col > span {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
  min-width: 0;
}

.tournament-card .grid-2col .lbl {
  color: var(--muted);
  font-size: 12px;
  flex-shrink: 0;
}

/* ============================================================
   모바일 — 테이블을 카드 리스트로 변환
   각 td에 data-label 속성 있어야 ::before 라벨 표시됨.
   ============================================================ */
@media (max-width: 720px) {
  /* 기록·토너별·큰위닝: 별도 카드 마크업 사용 → 데스크탑 테이블 숨김.
     아래의 .stat-table 카드 변환 룰에 묶이지 않도록 !important. */
  .records-table,
  #tournament-stat-table,
  #topwins-table {
    display: none !important;
  }

  .record-card-list,
  .tournament-card-list,
  .topwins-card-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
  }

  main {
    padding: 16px 12px;
  }

  .topbar {
    padding: 12px 16px 0;
  }

  .topbar h1 {
    font-size: 16px;
  }

  .tab {
    padding: 10px 12px;
    font-size: 13px;
  }

  .panel-header {
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
  }

  .panel-header h2 {
    font-size: 18px;
  }

  .btn-group {
    flex-wrap: wrap;
  }

  /* 테이블 → 카드 리스트 */
  .table-wrap {
    background: transparent;
    border: none;
    overflow: visible;
  }

  .records-table,
  .stat-table {
    display: block;
  }

  .records-table thead,
  .stat-table thead {
    display: none;
  }

  .records-table tbody,
  .stat-table tbody {
    display: block;
  }

  .records-table tr,
  .stat-table tr {
    display: block;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 8px;
    margin-bottom: 8px;
    padding: 12px 14px;
  }

  .records-table tbody tr:hover,
  .stat-table tbody tr:hover {
    background: var(--panel);
  }

  .records-table td,
  .stat-table td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    padding: 5px 0;
    border-bottom: none;
    text-align: right;
  }

  .records-table td[data-label]::before,
  .stat-table td[data-label]::before {
    content: attr(data-label);
    color: var(--muted);
    font-size: 12px;
    flex-shrink: 0;
    text-align: left;
  }

  /* 빈 셀이면 라벨 숨김 */
  .records-table td:empty,
  .stat-table td:empty {
    display: none;
  }

  /* 액션 셀: 라벨 없이 우측 정렬 */
  td.actions-cell {
    justify-content: flex-end !important;
    padding-top: 10px !important;
    margin-top: 4px;
    border-top: 1px solid var(--border);
  }

  td.actions-cell::before {
    content: none !important;
  }

  /* 행 강조 — 카드 좌측 굵은 바 */
  tbody tr.highlight-medium {
    box-shadow: none;
    border-left: 4px solid var(--positive);
  }

  tbody tr.highlight-big {
    box-shadow: none;
    border-left: 4px solid var(--warning);
  }

  /* 단종 행 */
  tbody tr.row-discontinued td {
    opacity: 1;
  }

  tbody tr.row-discontinued {
    opacity: 0.6;
  }

  /* colspan 사용한 empty row */
  .records-table td.empty,
  .stat-table td.empty,
  .records-table td[colspan],
  .stat-table td[colspan] {
    display: block;
    text-align: center;
    padding: 24px;
  }

  .records-table td[colspan]::before,
  .stat-table td[colspan]::before {
    content: none;
  }

  /* 차트 모바일 높이 */
  .chart-card svg {
    height: 280px;
  }

  /* 차트 헤더 좀 더 컴팩트 */
  .chart-card {
    padding: 12px;
  }

  /* 파일 바 */
  .file-bar {
    flex-direction: column;
    align-items: stretch;
  }

  /* 요약 카드 */
  .summary-card {
    padding: 12px;
  }

  .summary-card .value {
    font-size: 16px;
  }

  /* 모달 — 폰에선 거의 풀스크린 */
  .modal {
    width: 96%;
    max-height: 96vh;
  }

  .inline-form {
    grid-template-columns: 1fr;
  }
}


.file-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
  padding: 10px 14px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
}

.file-status {
  font-size: 13px;
  color: var(--text);
  font-variant-numeric: tabular-nums;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.file-status[hidden] {
  display: none;
}

.file-status .muted {
  color: var(--muted);
  margin-left: 8px;
}

.file-bar .btn-group {
  flex-shrink: 0;
}
