/* ==========================================================================
   추첨 도구 모음 — 디자인 시스템
   ========================================================================== */

:root {
  --bg: #f3f4f8;
  --surface: #ffffff;
  --surface-2: #eef0f5;
  --text: #1d2333;
  --text-muted: #64708a;
  --border: #e2e5ec;
  --rail: #c9cedb;

  --primary: #f43f5e;
  --primary-hover: #e11d48;
  --primary-contrast: #ffffff;
  --primary-soft: #ffe4e9;

  --danger: #dc2626;
  --success: #16a34a;

  --wheel-1: #f43f5e;
  --wheel-2: #f59e0b;
  --wheel-3: #10b981;
  --wheel-4: #3b82f6;
  --wheel-5: #8b5cf6;
  --wheel-6: #ec4899;
  --wheel-7: #14b8a6;
  --wheel-8: #f97316;

  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 20px;

  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;

  --shadow-card: 0 1px 2px rgba(16, 24, 40, 0.05), 0 4px 16px rgba(16, 24, 40, 0.06);
  --shadow-pop: 0 8px 30px rgba(16, 24, 40, 0.18);
}

[data-theme="dark"] {
  --bg: #0f1117;
  --surface: #171a21;
  --surface-2: #21252f;
  --text: #e6e8ee;
  --text-muted: #98a0b3;
  --border: #2a2f3a;
  --rail: #3d4452;

  --primary: #f43f5e;
  --primary-hover: #fb7185;
  --primary-contrast: #ffffff;
  --primary-soft: rgba(244, 63, 94, 0.16);

  --danger: #f87171;
  --success: #34d399;

  --wheel-1: #e11d48;
  --wheel-2: #d97706;
  --wheel-3: #059669;
  --wheel-4: #2563eb;
  --wheel-5: #7c3aed;
  --wheel-6: #db2777;
  --wheel-7: #0d9488;
  --wheel-8: #ea580c;

  --shadow-card: 0 1px 2px rgba(0, 0, 0, 0.4), 0 4px 16px rgba(0, 0, 0, 0.35);
  --shadow-pop: 0 8px 30px rgba(0, 0, 0, 0.55);
}

/* --------------------------------------------------------------------------
   기본
   -------------------------------------------------------------------------- */

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

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

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Apple SD Gothic Neo", "Pretendard",
    "Noto Sans KR", "Malgun Gothic", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  word-break: keep-all;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

h1, h2, h3, p {
  margin: 0;
}

button {
  font-family: inherit;
  color: inherit;
}

/* --------------------------------------------------------------------------
   헤더 / 내비게이션
   -------------------------------------------------------------------------- */

.site-header {
  position: sticky;
  top: 0;
  z-index: 50;
  background: var(--bg);
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}

.site-header .header-inner {
  max-width: 1080px;
  margin: 0 auto;
  padding: var(--space-3) var(--space-4);
  display: flex;
  align-items: center;
  gap: var(--space-3);
}

.tabs {
  flex: 1;
  display: flex;
  gap: var(--space-1);
  background: var(--surface-2);
  border-radius: var(--radius-md);
  padding: 4px;
  max-width: 460px;
}

.tab {
  flex: 1;
  text-align: center;
  padding: 8px 6px;
  border-radius: 9px;
  color: var(--text-muted);
  font-weight: 600;
  font-size: 0.95rem;
  text-decoration: none;
  white-space: nowrap;
  transition: color 0.15s, background 0.15s;
}

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

.tab.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 3px rgba(16, 24, 40, 0.12);
}

.theme-toggle {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
}

.theme-toggle:hover {
  background: var(--surface-2);
}

.theme-toggle svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: var(--text-muted);
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

[data-theme="dark"] .icon-moon { display: none; }
[data-theme="light"] .icon-sun { display: none; }

/* --------------------------------------------------------------------------
   페이지 레이아웃
   -------------------------------------------------------------------------- */

.page {
  flex: 1;
  width: 100%;
  max-width: 1080px;
  margin: 0 auto;
  padding: var(--space-5) var(--space-4) var(--space-6);
}

.page-title {
  font-size: 1.75rem;
  font-weight: 800;
  letter-spacing: -0.02em;
}

.page-subtitle {
  color: var(--text-muted);
  margin-top: var(--space-1);
  margin-bottom: var(--space-5);
  font-size: 0.95rem;
}

.layout-split {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: var(--space-5);
  align-items: start;
}

.side-col {
  display: grid;
  gap: var(--space-4);
  min-width: 0;
}

.layout-stack {
  display: grid;
  gap: var(--space-4);
}

.side-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: var(--space-4);
  align-items: start;
}

@media (min-width: 900px) {
  .layout-split {
    grid-template-columns: minmax(0, 1.35fr) minmax(0, 1fr);
  }
  .side-grid {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  }
}

.site-footer {
  border-top: 1px solid var(--border);
  color: var(--text-muted);
  font-size: 0.85rem;
  text-align: center;
  padding: var(--space-4);
}

/* --------------------------------------------------------------------------
   공통 컴포넌트
   -------------------------------------------------------------------------- */

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

.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
  margin-bottom: var(--space-3);
}

.card-head h2 {
  font-size: 1.05rem;
  font-weight: 700;
}

.count-badge {
  display: inline-block;
  min-width: 24px;
  text-align: center;
  padding: 1px 8px;
  margin-left: 2px;
  border-radius: 999px;
  background: var(--primary-soft);
  color: var(--primary);
  font-size: 0.85rem;
  font-weight: 700;
}

.head-actions {
  display: flex;
  gap: var(--space-1);
  flex-shrink: 0;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  padding: 0 var(--space-4);
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  font-size: 0.95rem;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
  -webkit-tap-highlight-color: transparent;
}

.btn:active {
  transform: scale(0.97);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
}

.btn-primary {
  background: var(--primary);
  color: var(--primary-contrast);
}

.btn-primary:hover:not(:disabled) {
  background: var(--primary-hover);
}

.btn-ghost {
  background: transparent;
  border-color: var(--border);
  color: var(--text);
}

.btn-ghost:hover:not(:disabled) {
  background: var(--surface-2);
}

.btn-ghost.danger {
  color: var(--danger);
}

.btn-sm {
  min-height: 34px;
  padding: 0 var(--space-3);
  font-size: 0.85rem;
  font-weight: 600;
  border-radius: var(--radius-sm);
}

.input {
  width: 100%;
  min-height: 44px;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface);
  color: var(--text);
  font-family: inherit;
  font-size: 1rem;
}

.input:focus {
  outline: 2px solid var(--primary);
  outline-offset: -1px;
  border-color: transparent;
}

textarea.input {
  resize: vertical;
  line-height: 1.6;
}

.hint {
  color: var(--text-muted);
  font-size: 0.85rem;
  margin-top: var(--space-2);
}

/* 스테퍼 */
.stepper {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  padding: 2px;
}

.stepper button {
  width: 30px;
  height: 30px;
  border: none;
  border-radius: 6px;
  background: transparent;
  font-size: 1rem;
  font-weight: 700;
  color: var(--text-muted);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.stepper button:hover:not(:disabled) {
  background: var(--surface);
  color: var(--text);
}

.stepper button:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.stepper .stepper-value {
  min-width: 52px;
  text-align: center;
  font-size: 0.9rem;
  font-weight: 700;
  padding: 0 4px;
}

/* 접이식 카드 (details) */
details.card {
  padding: 0;
}

details.card > summary {
  list-style: none;
  cursor: pointer;
  padding: var(--space-4);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
  font-weight: 700;
  font-size: 1.05rem;
  -webkit-tap-highlight-color: transparent;
}

details.card > summary::-webkit-details-marker {
  display: none;
}

details.card > summary::after {
  content: "";
  width: 9px;
  height: 9px;
  border-right: 2px solid var(--text-muted);
  border-bottom: 2px solid var(--text-muted);
  transform: rotate(45deg);
  transition: transform 0.2s;
  flex-shrink: 0;
  margin-right: 4px;
}

details.card[open] > summary::after {
  transform: rotate(-135deg);
  margin-top: 6px;
}

details.card > .details-body {
  padding: 0 var(--space-4) var(--space-4);
}

/* --------------------------------------------------------------------------
   참가자 패널
   -------------------------------------------------------------------------- */

.add-row {
  display: flex;
  gap: var(--space-2);
  margin-bottom: var(--space-3);
}

.add-row .input {
  flex: 1;
  min-width: 0;
}

.entry-list {
  list-style: none;
  margin: 0 0 var(--space-3);
  padding: 0;
  display: grid;
  gap: 6px;
  max-height: 420px;
  overflow-y: auto;
}

.entry-row {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  background: var(--surface-2);
  border-radius: var(--radius-md);
  padding: 5px 6px 5px 12px;
}

.entry-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--dot, var(--text-muted));
  flex-shrink: 0;
}

.entry-name {
  flex: 1;
  min-width: 0;
  border: none;
  background: transparent;
  color: var(--text);
  font-family: inherit;
  font-size: 0.95rem;
  font-weight: 600;
  min-height: 38px;
  padding: 0 4px;
  border-radius: 6px;
}

.entry-name:focus {
  outline: 2px solid var(--primary);
  background: var(--surface);
}

.entry-del {
  width: 34px;
  height: 34px;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-muted);
  font-size: 1.15rem;
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s, color 0.15s;
}

.entry-del:hover {
  background: color-mix(in srgb, var(--danger) 12%, transparent);
  color: var(--danger);
}

.entry-empty {
  color: var(--text-muted);
  font-size: 0.9rem;
  text-align: center;
  padding: var(--space-4) 0;
}

.bulk-panel {
  display: grid;
  gap: var(--space-2);
  margin-top: var(--space-2);
}

.bulk-actions {
  display: flex;
  gap: var(--space-2);
  justify-content: flex-end;
}

/* --------------------------------------------------------------------------
   기록 패널
   -------------------------------------------------------------------------- */

.history-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: var(--space-2);
  max-height: 320px;
  overflow-y: auto;
}

.history-row {
  display: flex;
  align-items: baseline;
  gap: var(--space-2);
  font-size: 0.9rem;
}

.chip {
  flex-shrink: 0;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text-muted);
  font-size: 0.75rem;
  font-weight: 700;
}

.history-text {
  flex: 1;
  min-width: 0;
  overflow-wrap: anywhere;
}

.history-time {
  flex-shrink: 0;
  color: var(--text-muted);
  font-size: 0.78rem;
}

.history-empty {
  color: var(--text-muted);
  font-size: 0.9rem;
  text-align: center;
  padding: var(--space-3) 0;
}

.history-clear-row {
  margin-top: var(--space-3);
  text-align: right;
}

/* --------------------------------------------------------------------------
   돌림판
   -------------------------------------------------------------------------- */

.wheel-card {
  padding: var(--space-5);
}

.wheel-wrap {
  position: relative;
  width: min(100%, 440px);
  aspect-ratio: 1;
  margin: 0 auto;
}

#wheelCanvas {
  display: block;
  width: 100%;
  height: 100%;
  cursor: pointer;
}

.wheel-pointer {
  position: absolute;
  top: -6px;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 15px solid transparent;
  border-right: 15px solid transparent;
  border-top: 26px solid var(--primary);
  z-index: 5;
  filter: drop-shadow(0 2px 3px rgba(0, 0, 0, 0.3));
  pointer-events: none;
}

.wheel-center-btn {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 86px;
  height: 86px;
  border-radius: 50%;
  border: 5px solid var(--surface);
  background: var(--primary);
  color: var(--primary-contrast);
  font-size: 1.05rem;
  font-weight: 800;
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25);
  z-index: 4;
  transition: background 0.15s, transform 0.15s;
}

.wheel-center-btn:hover:not(:disabled) {
  background: var(--primary-hover);
  transform: translate(-50%, -50%) scale(1.05);
}

.wheel-center-btn:disabled {
  background: var(--text-muted);
  cursor: not-allowed;
}

.wheel-hint {
  text-align: center;
  margin-top: var(--space-3);
}

/* 당첨 다이얼로그 */
dialog {
  border: none;
  border-radius: var(--radius-lg);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-pop);
  padding: var(--space-6) var(--space-5) var(--space-5);
  width: min(92vw, 400px);
}

dialog::backdrop {
  background: rgba(0, 0, 0, 0.45);
}

.dialog-label {
  text-align: center;
  color: var(--text-muted);
  font-weight: 700;
  font-size: 0.95rem;
}

.winner-name {
  text-align: center;
  font-size: 2rem;
  font-weight: 800;
  letter-spacing: -0.02em;
  margin: var(--space-3) 0 var(--space-5);
  overflow-wrap: anywhere;
}

.dialog-actions {
  display: grid;
  gap: var(--space-2);
}

.dialog-body {
  margin: var(--space-3) 0 var(--space-5);
  display: grid;
  gap: var(--space-2);
}

/* --------------------------------------------------------------------------
   사다리타기
   -------------------------------------------------------------------------- */

.tool-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2);
  margin-bottom: var(--space-4);
}

.tool-controls .spacer {
  flex: 1;
}

.ladder-scroll {
  overflow-x: auto;
  padding-bottom: var(--space-2);
}

.ladder-inner {
  margin: 0 auto;
}

.ladder-row {
  display: flex;
}

.ladder-cell {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2px;
}

.ladder-name {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  min-height: 44px;
  padding: 4px 4px;
  font-size: 0.82rem;
  font-weight: 700;
  cursor: pointer;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  transition: background 0.15s, border-color 0.15s;
  -webkit-tap-highlight-color: transparent;
}

.ladder-name:hover:not(:disabled) {
  border-color: var(--accent, var(--primary));
}

.ladder-name.done {
  background: var(--surface);
  border-color: var(--accent, var(--primary));
  color: var(--accent, var(--primary));
  cursor: default;
}

.ladder-name:disabled {
  cursor: default;
}

.ladder-result {
  width: 100%;
  min-height: 44px;
  border: 1px dashed var(--rail);
  border-radius: var(--radius-sm);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  line-height: 1.25;
  font-size: 0.82rem;
  font-weight: 700;
  color: var(--text-muted);
  padding: 4px;
  overflow: hidden;
  text-align: center;
}

.lr-slot-owner {
  font-size: 0.68rem;
  font-weight: 600;
  color: var(--text-muted);
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.ladder-result.win .lr-slot-owner {
  color: inherit;
  opacity: 0.8;
}

.lr-slot-label {
  font-size: 0.85rem;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.ladder-result.revealed {
  border-style: solid;
  border-color: var(--border);
  background: var(--surface-2);
  color: var(--text);
}

.ladder-result.win {
  border-color: var(--primary);
  background: var(--primary-soft);
  color: var(--primary);
}

#ladderCanvas {
  display: block;
}

/* 공개된 결과 요약 (누가 → 무엇) */
.ladder-results {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 18px;
  margin-top: var(--space-3);
  padding: var(--space-3);
  background: var(--surface-2);
  border-radius: var(--radius-md);
  font-size: 0.9rem;
}

.lr-row {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.lr-row .lr-label {
  color: var(--text-muted);
  font-weight: 700;
}

.lr-row.win .lr-label {
  color: var(--primary);
}

.tool-notice {
  text-align: center;
  color: var(--text-muted);
  padding: var(--space-6) var(--space-4);
  font-size: 0.95rem;
}

/* --------------------------------------------------------------------------
   제비뽑기
   -------------------------------------------------------------------------- */

.turn-banner {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  background: var(--surface-2);
  border-radius: var(--radius-md);
  min-height: 48px;
  padding: var(--space-2) var(--space-3);
  font-weight: 700;
  margin-bottom: var(--space-4);
}

.turn-banner .entry-dot {
  width: 12px;
  height: 12px;
}

.cards-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(84px, 120px));
  justify-content: center;
  gap: 10px;
}

.lot-card {
  position: relative;
  aspect-ratio: 3 / 4;
  border: none;
  background: transparent;
  padding: 0;
  cursor: pointer;
  perspective: 600px;
  -webkit-tap-highlight-color: transparent;
}

.lot-card:disabled {
  cursor: default;
}

.lot-inner {
  position: absolute;
  inset: 0;
  transition: transform 0.5s;
  transform-style: preserve-3d;
}

.lot-card.flipped .lot-inner {
  transform: rotateY(180deg);
}

.lot-card:not(.flipped):not(:disabled):hover .lot-inner {
  transform: translateY(-4px);
}

.lot-face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  border-radius: var(--radius-md);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  font-weight: 800;
}

.lot-front {
  background: var(--surface-2);
  background: linear-gradient(145deg, var(--surface-2), color-mix(in srgb, var(--surface-2) 70%, var(--border)));
  border: 1px solid var(--border);
  color: var(--text-muted);
  font-size: 1.5rem;
}

.lot-back {
  transform: rotateY(180deg);
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text-muted);
  font-size: 1.05rem;
}

.lot-back.win {
  background: var(--primary-soft);
  border-color: var(--primary);
  color: var(--primary);
}

.lot-back .lot-owner {
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--text-muted);
  max-width: 90%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.lot-card.dealing .lot-inner {
  animation: deal-in 0.4s both;
  animation-delay: calc(var(--i) * 35ms);
}

@keyframes deal-in {
  from {
    opacity: 0;
    transform: translateY(14px) scale(0.92);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

/* --------------------------------------------------------------------------
   토스트
   -------------------------------------------------------------------------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: 28px;
  transform: translateX(-50%) translateY(16px);
  background: var(--text);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 0.9rem;
  font-weight: 600;
  box-shadow: var(--shadow-pop);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s, transform 0.25s;
  z-index: 100;
  max-width: 90vw;
  text-align: center;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* --------------------------------------------------------------------------
   좁은 화면
   -------------------------------------------------------------------------- */

@media (max-width: 520px) {
  .site-header .header-inner {
    padding: 10px;
    gap: 8px;
  }
  .tabs {
    max-width: none;
  }
  .tab {
    font-size: 0.85rem;
    padding: 8px 2px;
  }
  .theme-toggle {
    width: 36px;
    height: 36px;
  }
  .page {
    padding: var(--space-4) var(--space-3) var(--space-5);
  }
  .page-title {
    font-size: 1.45rem;
  }
  .wheel-card {
    padding: var(--space-4);
  }
}

/* --------------------------------------------------------------------------
   모션 최소화
   -------------------------------------------------------------------------- */

@media (prefers-reduced-motion: reduce) {
  * {
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
  }
  .lot-inner {
    transition-duration: 0.15s !important;
  }
}
