:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-alt: #f8fafc;
  --border: #e2e8f0;
  --text: #0f172a;
  --text-muted: #64748b;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-soft: rgba(37, 99, 235, 0.12);
  --fixed: #059669;
  --fixed-soft: rgba(5, 150, 105, 0.14);
  --excluded: #dc2626;
  --excluded-soft: rgba(220, 38, 38, 0.12);
  --gold: #f59e0b;
  --shadow-sm: 0 1px 3px rgba(15, 23, 42, 0.06);
  --shadow-md: 0 10px 30px rgba(15, 23, 42, 0.08);
  --radius-lg: 20px;
  --radius-md: 14px;
  --radius-sm: 10px;
}

[data-theme="dark"] {
  --bg: #070b14;
  --surface: #0f172a;
  --surface-alt: #162033;
  --border: #24324a;
  --text: #f1f5f9;
  --text-muted: #94a3b8;
  --primary: #3b82f6;
  --primary-hover: #60a5fa;
  --primary-soft: rgba(59, 130, 246, 0.2);
  --fixed: #10b981;
  --fixed-soft: rgba(16, 185, 129, 0.2);
  --excluded: #f87171;
  --excluded-soft: rgba(248, 113, 113, 0.2);
  --shadow-sm: 0 2px 6px rgba(0, 0, 0, 0.45);
  --shadow-md: 0 18px 42px rgba(0, 0, 0, 0.58);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Apple SD Gothic Neo", "Malgun Gothic", "Noto Sans KR", system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
  transition: background-color 0.25s ease, color 0.25s ease;
}

[data-theme="dark"] body {
  background:
    radial-gradient(circle at 18% 0%, rgba(37, 99, 235, 0.16), transparent 42%),
    radial-gradient(circle at 86% 10%, rgba(124, 58, 237, 0.14), transparent 38%),
    var(--bg);
}

.app-shell {
  max-width: 1200px;
  margin: 0 auto;
  padding: 24px 20px 44px;
}

/* Header */
.top-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 24px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 14px;
}

.brand-badge {
  display: grid;
  place-items: center;
  width: 50px;
  height: 50px;
  border-radius: 16px;
  background: linear-gradient(135deg, #2563eb, #7c3aed);
  color: #fff;
  font-weight: 800;
  font-size: 1.05rem;
  letter-spacing: -0.02em;
  box-shadow: 0 8px 18px rgba(37, 99, 235, 0.28);
}

.brand h1 {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 800;
  letter-spacing: -0.02em;
}

.brand-sub {
  margin: 2px 0 0;
  font-size: 0.875rem;
  color: var(--text-muted);
}

/* Layout Grid */
.main-grid {
  display: grid;
  grid-template-columns: 1.45fr 1fr;
  gap: 20px;
  align-items: start;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 22px;
  box-shadow: var(--shadow-md);
}

[data-theme="dark"] .panel {
  background: rgba(15, 23, 42, 0.88);
  backdrop-filter: blur(10px);
}

.history-panel {
  grid-column: 1 / -1;
}

.panel-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 18px;
}

.panel-header.compact {
  flex-wrap: nowrap;
}

.panel-header h2 {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 700;
}

.panel-desc {
  margin: 3px 0 0;
  font-size: 0.85rem;
  color: var(--text-muted);
}

/* Controls */
.stage-controls,
.history-controls {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
}

.segmented {
  display: inline-flex;
  padding: 3px;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 12px;
  gap: 2px;
}

.seg-btn {
  border: 0;
  background: transparent;
  color: var(--text-muted);
  font: inherit;
  font-size: 0.82rem;
  font-weight: 600;
  padding: 6px 10px;
  border-radius: 9px;
  cursor: pointer;
  transition: all 0.18s ease;
}

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

.seg-btn.active {
  background: var(--surface);
  color: var(--primary);
  box-shadow: var(--shadow-sm);
}

/* Toggle Switch */
.toggle-switch {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  font-size: 0.84rem;
  font-weight: 600;
  color: var(--text-muted);
  user-select: none;
}

.toggle-switch input {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.switch-track {
  position: relative;
  width: 38px;
  height: 22px;
  border-radius: 999px;
  background: var(--border);
  transition: background 0.2s ease;
}

.switch-track::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25);
  transition: transform 0.2s ease;
}

.toggle-switch input:checked + .switch-track {
  background: var(--primary);
}

.toggle-switch input:checked + .switch-track::after {
  transform: translateX(16px);
}

/* Buttons */
.primary-btn,
.secondary-btn,
.ghost-btn,
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font: inherit;
  font-weight: 700;
  border-radius: 12px;
  cursor: pointer;
  transition: transform 0.12s ease, background-color 0.18s ease, border-color 0.18s ease, opacity 0.18s ease;
}

.primary-btn:active,
.secondary-btn:active,
.ghost-btn:active,
.icon-btn:active {
  transform: scale(0.98);
}

.primary-btn:disabled,
.secondary-btn:disabled,
.ghost-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
}

.primary-btn {
  flex: 1;
  min-width: 180px;
  padding: 13px 22px;
  border: 0;
  background: linear-gradient(135deg, #2563eb, #4f46e5);
  color: #fff;
  font-size: 1rem;
  box-shadow: 0 10px 20px rgba(37, 99, 235, 0.26);
}

.primary-btn:hover:not(:disabled) {
  filter: brightness(1.06);
}

.secondary-btn {
  padding: 12px 16px;
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--text);
  font-size: 0.9rem;
}

.secondary-btn:hover:not(:disabled) {
  border-color: var(--primary);
  color: var(--primary);
}

.ghost-btn {
  padding: 12px 14px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-muted);
  font-size: 0.88rem;
}

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

.ghost-btn.sm {
  padding: 6px 10px;
  font-size: 0.82rem;
  border-radius: 9px;
}

.ghost-btn.danger:hover:not(:disabled) {
  color: var(--excluded);
  background: var(--excluded-soft);
}

.icon-btn {
  padding: 9px 14px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 0.86rem;
  box-shadow: var(--shadow-sm);
}

.icon-btn:hover {
  border-color: var(--primary);
}

/* Draw Results Stage */
.draw-results {
  min-height: 180px;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 18px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 12px;
}

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

.empty-stage p {
  margin: 14px 0 0;
  font-size: 0.92rem;
}

.empty-balls {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 10px;
}

.ball-placeholder {
  width: 46px;
  height: 46px;
  border-radius: 50%;
  border: 2px dashed var(--border);
  display: grid;
  place-items: center;
  font-weight: 700;
  color: var(--text-muted);
}

.ball-placeholder.bonus {
  border-color: var(--gold);
  color: var(--gold);
}

.game-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 11px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  transition: border-color 0.18s ease, box-shadow 0.18s ease, transform 0.15s ease;
}

.game-row:hover {
  border-color: var(--primary);
}

.game-row.selected {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}

.game-select-btn {
  flex: 1;
  min-width: 0;
  border: 0;
  background: transparent;
  padding: 0;
  margin: 0;
  font: inherit;
  color: inherit;
  text-align: left;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.game-select-btn:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 4px;
  border-radius: 8px;
}

.game-label {
  display: inline-grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border-radius: 8px;
  background: var(--primary-soft);
  color: var(--primary);
  font-weight: 800;
  font-size: 0.85rem;
  flex-shrink: 0;
}

.balls-wrap {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
}

.plus-sign {
  font-weight: 800;
  color: var(--text-muted);
  margin: 0 2px;
}

/* Lotto Ball Styles */
.lotto-ball {
  position: relative;
  display: inline-grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  color: #fff;
  font-weight: 800;
  font-size: 1rem;
  letter-spacing: -0.02em;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.38);
  box-shadow:
    inset -3px -5px 10px rgba(0, 0, 0, 0.22),
    inset 3px 4px 8px rgba(255, 255, 255, 0.42),
    0 5px 12px rgba(15, 23, 42, 0.18);
  user-select: none;
}

.lotto-ball::after {
  content: "";
  position: absolute;
  top: 6px;
  left: 9px;
  width: 12px;
  height: 7px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.45);
  transform: rotate(-28deg);
  pointer-events: none;
}

.lotto-ball.is-fixed {
  outline: 2px solid var(--fixed);
  outline-offset: 2px;
}

.lotto-ball.is-bonus {
  box-shadow:
    inset -3px -5px 10px rgba(0, 0, 0, 0.22),
    inset 3px 4px 8px rgba(255, 255, 255, 0.42),
    0 0 0 2px var(--gold),
    0 5px 12px rgba(245, 158, 11, 0.3);
}

.lotto-ball.r1,
.legend-dot.r1,
.range-pill.r1 {
  background: linear-gradient(145deg, #ffd84d, #e0a800);
}

.lotto-ball.r2,
.legend-dot.r2,
.range-pill.r2 {
  background: linear-gradient(145deg, #74d0f7, #359cd6);
}

.lotto-ball.r3,
.legend-dot.r3,
.range-pill.r3 {
  background: linear-gradient(145deg, #ff8585, #e24747);
}

.lotto-ball.r4,
.legend-dot.r4,
.range-pill.r4 {
  background: linear-gradient(145deg, #b8bec9, #7c8594);
}

.lotto-ball.r5,
.legend-dot.r5,
.range-pill.r5 {
  background: linear-gradient(145deg, #bde44e, #7eb222);
}

.lotto-ball.mini {
  width: 30px;
  height: 30px;
  font-size: 0.78rem;
  box-shadow:
    inset -2px -3px 6px rgba(0, 0, 0, 0.2),
    inset 2px 2px 5px rgba(255, 255, 255, 0.35),
    0 2px 5px rgba(15, 23, 42, 0.14);
}

.lotto-ball.mini::after {
  top: 4px;
  left: 6px;
  width: 8px;
  height: 4px;
}

.lotto-ball.reveal {
  opacity: 0;
  transform: translateY(10px) scale(0.55) rotate(-35deg);
  animation: ballPop 0.38s cubic-bezier(0.22, 1.28, 0.36, 1) forwards;
}

@keyframes ballPop {
  0% {
    opacity: 0;
    transform: translateY(10px) scale(0.55) rotate(-35deg);
  }
  70% {
    opacity: 1;
    transform: translateY(-3px) scale(1.08) rotate(8deg);
  }
  100% {
    opacity: 1;
    transform: translateY(0) scale(1) rotate(0deg);
  }
}

.row-copy-btn {
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--text-muted);
  border-radius: 9px;
  padding: 6px 10px;
  font: inherit;
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
  flex-shrink: 0;
  transition: all 0.15s ease;
}

.row-copy-btn:hover {
  border-color: var(--primary);
  color: var(--primary);
}

.stage-actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 16px;
}

/* Stats Card */
.stats-card {
  margin-top: 18px;
  padding: 16px;
  border-radius: var(--radius-md);
  background: var(--surface-alt);
  border: 1px solid var(--border);
}

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

.stats-header h3 {
  margin: 0;
  font-size: 0.95rem;
  display: flex;
  align-items: center;
  gap: 8px;
}

.stats-badge {
  font-size: 0.75rem;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--primary-soft);
  color: var(--primary);
}

.stats-hint {
  font-size: 0.78rem;
  color: var(--text-muted);
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
}

.stat-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.stat-label {
  font-size: 0.76rem;
  color: var(--text-muted);
  font-weight: 600;
}

.stat-value {
  font-size: 1.1rem;
  font-weight: 800;
}

.stat-sub {
  font-size: 0.74rem;
  color: var(--text-muted);
}

.stat-sub.ok {
  color: var(--fixed);
  font-weight: 600;
}

.range-dots {
  display: flex;
  align-items: center;
  gap: 4px;
  min-height: 26px;
}

.range-pill {
  flex: 1;
  height: 22px;
  border-radius: 6px;
  display: grid;
  place-items: center;
  font-size: 0.75rem;
  font-weight: 800;
  color: #fff;
  text-shadow: 0 1px 1px rgba(0, 0, 0, 0.35);
}

/* Filter Panel (1~45 Grid) */
.filter-mode-bar {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 14px;
}

.mode-tab {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 9px 10px;
  border-radius: 11px;
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--text-muted);
  font: inherit;
  font-size: 0.83rem;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.16s ease;
}

.mode-tab.active[data-mode="fixed"] {
  border-color: var(--fixed);
  background: var(--fixed-soft);
  color: var(--fixed);
}

.mode-tab.active[data-mode="excluded"] {
  border-color: var(--excluded);
  background: var(--excluded-soft);
  color: var(--excluded);
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
}

.dot.fixed {
  background: var(--fixed);
}

.dot.excluded {
  background: var(--excluded);
}

.number-grid {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: 7px;
}

.num-btn {
  aspect-ratio: 1;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--text);
  font: inherit;
  font-size: 0.88rem;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.14s ease;
}

.num-btn:hover {
  border-color: var(--primary);
  transform: translateY(-1px);
}

.num-btn.fixed {
  background: var(--fixed);
  border-color: var(--fixed);
  color: #fff;
  box-shadow: 0 4px 10px rgba(5, 150, 105, 0.28);
}

.num-btn.excluded {
  background: var(--excluded-soft);
  border-color: var(--excluded);
  color: var(--excluded);
  text-decoration: line-through;
}

.filter-summary {
  margin-top: 16px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.summary-row {
  display: flex;
  align-items: flex-start;
  gap: 10px;
}

.summary-title {
  font-size: 0.78rem;
  font-weight: 800;
  padding: 3px 8px;
  border-radius: 6px;
  flex-shrink: 0;
}

.fixed-label {
  background: var(--fixed-soft);
  color: var(--fixed);
}

.excluded-label {
  background: var(--excluded-soft);
  color: var(--excluded);
}

.chip-list {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  align-items: center;
  min-height: 24px;
}

.chip-empty {
  font-size: 0.8rem;
  color: var(--text-muted);
}

.num-chip {
  font-size: 0.76rem;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--text);
  cursor: pointer;
}

.num-chip.fixed {
  border-color: var(--fixed);
  color: var(--fixed);
}

.num-chip.excluded {
  border-color: var(--excluded);
  color: var(--excluded);
}

.pool-status {
  margin: 12px 0 0;
  padding: 8px 11px;
  border-radius: 10px;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  font-size: 0.8rem;
  color: var(--text-muted);
}

.pool-status.warn {
  border-color: var(--gold);
  color: var(--text);
}

/* History Panel */
.history-title-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
}

.count-badge {
  font-size: 0.78rem;
  font-weight: 800;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  color: var(--text-muted);
}

.history-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.history-empty {
  text-align: center;
  padding: 24px 12px;
  color: var(--text-muted);
  margin: 0;
  font-size: 0.9rem;
}

.history-item {
  border: 1px solid var(--border);
  background: var(--surface-alt);
  border-radius: var(--radius-md);
  padding: 13px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.history-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
}

.history-meta-left {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.82rem;
  color: var(--text-muted);
}

.history-tag {
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 6px;
  background: var(--primary-soft);
  color: var(--primary);
  font-size: 0.75rem;
}

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

.mini-action-btn {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-muted);
  border-radius: 8px;
  padding: 4px 9px;
  font: inherit;
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
}

.mini-action-btn:hover {
  color: var(--text);
  border-color: var(--primary);
}

.mini-action-btn.fav.active {
  color: var(--gold);
  border-color: var(--gold);
}

.mini-action-btn.del:hover {
  color: var(--excluded);
  border-color: var(--excluded);
}

.history-games {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.history-game-line {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
}

.history-game-code {
  width: 22px;
  font-size: 0.8rem;
  font-weight: 800;
  color: var(--text-muted);
}

/* Footer */
.app-footer {
  margin-top: 24px;
  padding-top: 18px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  color: var(--text-muted);
  font-size: 0.82rem;
}

.app-footer p {
  margin: 0;
}

.ball-legend {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
}

.ball-legend span {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.legend-dot {
  display: inline-block;
  width: 11px;
  height: 11px;
  border-radius: 50%;
}

/* Toast */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(16px);
  background: #0f172a;
  color: #f8fafc;
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 0.86rem;
  font-weight: 600;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.25);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 1000;
}

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

/* Responsive */
@media (max-width: 960px) {
  .main-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 640px) {
  .app-shell {
    padding: 16px 14px 36px;
  }

  .panel {
    padding: 16px;
  }

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

  .lotto-ball {
    width: 38px;
    height: 38px;
    font-size: 0.9rem;
  }

  .game-row {
    flex-direction: column;
    align-items: flex-start;
  }

  .row-copy-btn {
    align-self: flex-end;
  }
}
