* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

:root {
  --bg-1: #1e7a4c;
  --bg-2: #0f4a2f;
  --bg-3: #07271a;
  --panel-1: #12593a;
  --panel-2: #0a3a25;
  --overlay-bg: rgba(3, 20, 13, 0.72);
  --toast-bg: rgba(8, 40, 26, 0.92);
}

:root[data-theme='wood'] {
  --bg-1: #9a6634;
  --bg-2: #6b4423;
  --bg-3: #3a2412;
  --panel-1: #7a4f28;
  --panel-2: #4e3018;
  --overlay-bg: rgba(28, 16, 6, 0.72);
  --toast-bg: rgba(46, 28, 12, 0.92);
}

:root[data-theme='dark'] {
  --bg-1: #333d4a;
  --bg-2: #1e252e;
  --bg-3: #0d1116;
  --panel-1: #2a333e;
  --panel-2: #1a2129;
  --overlay-bg: rgba(4, 7, 11, 0.75);
  --toast-bg: rgba(16, 22, 30, 0.94);
}

body {
  margin: 0;
  font-family:
    'Segoe UI',
    system-ui,
    -apple-system,
    sans-serif;
  color: #f2f6ff;
  background: radial-gradient(circle at 50% -10%, var(--bg-1) 0%, var(--bg-2) 45%, var(--bg-3) 100%);
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  overflow-x: hidden;
  overflow-y: hidden;
  -webkit-user-select: none;
  user-select: none;
}

button {
  font: inherit;
  color: #0b2a1c;
  background: linear-gradient(#fdfefe, #d7e3ef);
  border: 1px solid rgba(0, 0, 0, 0.25);
  border-radius: 8px;
  padding: 7px 13px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

@media (hover: hover) {
  button:hover:not(:disabled) {
    background: linear-gradient(#ffffff, #e7eff8);
  }
}
button:active:not(:disabled) {
  transform: translateY(1px);
}
button:disabled {
  opacity: 0.45;
  cursor: default;
}

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

.hud {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 14px;
  padding: calc(12px + env(safe-area-inset-top)) calc(20px + env(safe-area-inset-right)) 12px
    calc(20px + env(safe-area-inset-left));
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.hud .title {
  font-size: 21px;
  font-weight: 800;
  letter-spacing: 0.5px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
}

.hud .game-options {
  display: flex;
  gap: 6px;
}

.hud .game-options button {
  color: #eaf6ef;
  font-weight: 700;
  font-size: 14px;
  padding: 5px 12px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.14);
  box-shadow: none;
  cursor: pointer;
}

@media (hover: hover) {
  .hud .game-options button:hover:not(:disabled) {
    background: rgba(255, 255, 255, 0.16);
  }
}

.hud .game-options button:active:not(:disabled) {
  transform: translateY(1px);
}

.hud .stats {
  display: flex;
  gap: 16px;
  font-size: 14px;
  opacity: 0.95;
}

.hud .stats b {
  font-variant-numeric: tabular-nums;
}

.hud .controls {
  display: flex;
  gap: 8px;
  margin-left: auto;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  padding: 0;
  line-height: 0;
}

.icon-btn svg {
  width: 20px;
  height: 20px;
  display: block;
}

.corner-actions {
  position: fixed;
  left: calc(14px + env(safe-area-inset-left));
  bottom: calc(14px + env(safe-area-inset-bottom));
  display: flex;
  gap: 10px;
  z-index: 1200;
}

.corner-actions .icon-btn {
  width: 46px;
  height: 46px;
  border-radius: 50%;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.45);
}

.corner-actions .icon-btn svg {
  width: 22px;
  height: 22px;
}

.game {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  min-width: 0;
  overflow: hidden;
}

.game canvas {
  display: block;
  touch-action: none;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

.overlay {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  background: var(--overlay-bg);
  z-index: 2000;
}

.overlay.show {
  display: flex;
}

.dialog {
  background: linear-gradient(var(--panel-1), var(--panel-2));
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 14px;
  padding: 28px 34px;
  text-align: center;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.6);
}

.dialog h2 {
  margin: 0 0 10px;
  font-size: 30px;
  color: #ffe66d;
}

.dialog p {
  margin: 0 0 18px;
  opacity: 0.95;
}

.fatal-dialog {
  max-width: min(640px, 90vw);
  text-align: left;
}
.fatal-dialog h2 {
  text-align: center;
  color: #ff7b72;
}
.fatal-dialog summary {
  cursor: pointer;
  opacity: 0.8;
  margin-bottom: 8px;
}
.fatal-stack {
  max-height: 240px;
  overflow: auto;
  margin: 0 0 18px;
  padding: 10px 12px;
  background: rgba(0, 0, 0, 0.35);
  border-radius: 8px;
  font:
    12px/1.5 Consolas,
    'Courier New',
    monospace;
  white-space: pre-wrap;
  word-break: break-word;
  color: #ffd9d4;
}

.game-menu {
  padding: 20px;
}

.menu-panel {
  background: linear-gradient(var(--panel-1), var(--panel-2));
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 16px;
  padding: 20px 22px 24px;
  width: min(92vw, 760px);
  max-height: 92vh;
  overflow: auto;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.6);
}

.menu-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}

.menu-head h2 {
  margin: 0;
  font-size: 24px;
  color: #ffe66d;
}

.menu-close {
  padding: 2px 11px;
  font-size: 20px;
  line-height: 1.3;
}

.menu-list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 16px;
}

.menu-card {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 14px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.16);
  color: #eaf6ef;
  text-decoration: none;
  transition:
    transform 0.12s ease,
    background 0.12s ease,
    border-color 0.12s ease;
}

@media (hover: hover) {
  .menu-card:hover {
    background: rgba(255, 255, 255, 0.12);
    border-color: rgba(255, 255, 255, 0.34);
    transform: translateY(-2px);
  }
}

.menu-card.active {
  border-color: #ffe66d;
}

.menu-title {
  font-size: 18px;
  font-weight: 800;
  letter-spacing: 0.3px;
}

.menu-badge {
  margin-left: 8px;
  padding: 2px 7px;
  border-radius: 6px;
  font-size: 11px;
  font-weight: 700;
  color: #0b2a1c;
  background: #ffe66d;
  vertical-align: middle;
}

.menu-preview-wrap {
  padding: 8px;
  border-radius: 8px;
  background: radial-gradient(circle at 50% 0%, var(--bg-1) 0%, var(--bg-2) 60%, var(--bg-3) 100%);
  border: 1px solid rgba(0, 0, 0, 0.25);
}

.menu-preview {
  display: block;
  width: 100%;
  height: 134px;
}

.menu-meta {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
}

.menu-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.menu-k {
  opacity: 0.7;
}

.menu-v {
  font-weight: 600;
}

.menu-diff {
  display: inline-flex;
  gap: 4px;
  margin-left: 6px;
  vertical-align: middle;
}

.menu-diff i {
  display: inline-block;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.22);
}

.menu-diff i.on {
  background: #ffe66d;
}

.settings-panel {
  width: min(92vw, 560px);
}

.settings-section {
  margin-top: 20px;
}

.settings-section:first-of-type {
  margin-top: 4px;
}

.settings-title {
  font-size: 15px;
  font-weight: 800;
  letter-spacing: 0.3px;
  color: #eaf6ef;
  margin-bottom: 10px;
}

.settings-opts {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.settings-opt {
  padding: 8px 15px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.16);
  color: #eaf6ef;
  font-weight: 600;
  cursor: pointer;
}

@media (hover: hover) {
  button.settings-opt:hover:not(.active) {
    background: rgba(255, 255, 255, 0.16);
  }
  .settings-opt.active:hover {
    background: linear-gradient(#fdfefe, #d7e3ef);
    color: #0b2a1c;
    border-color: rgba(0, 0, 0, 0.25);
  }
}

.settings-opt.active {
  background: linear-gradient(#fdfefe, #d7e3ef);
  color: #0b2a1c;
  border-color: rgba(0, 0, 0, 0.25);
}

.color-swatches {
  display: inline-flex;
  gap: 3px;
  margin-right: 8px;
  vertical-align: middle;
}

.color-swatches i {
  display: inline-block;
  width: 11px;
  height: 11px;
  border-radius: 50%;
  border: 1px solid rgba(0, 0, 0, 0.35);
}

.back-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(84px, 1fr));
  gap: 12px;
}

.back-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 7px;
  padding: 10px 6px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.16);
  color: #eaf6ef;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition:
    transform 0.12s ease,
    background 0.12s ease,
    border-color 0.12s ease,
    box-shadow 0.12s ease;
}

@media (hover: hover) {
  button.back-item:hover {
    background: rgba(255, 255, 255, 0.12);
    transform: translateY(-2px);
  }
}

.back-item.active {
  border-color: #ffe66d;
  box-shadow: 0 0 0 2px rgba(255, 230, 109, 0.45) inset;
}

.back-thumb {
  display: block;
  width: 64px;
  height: 90px;
  border-radius: 6px;
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.45);
}

.back-name {
  text-align: center;
  line-height: 1.15;
}

#solitaire-toast,
#solitaire-finish {
  position: fixed;
  left: 50%;
  transform: translateX(-50%) translateY(12px);
  padding: 9px 18px;
  border-radius: 10px;
  background: var(--toast-bg);
  border: 1px solid rgba(255, 230, 109, 0.4);
  color: #eaf6ef;
  font-size: 14px;
  font-weight: 600;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
  opacity: 0;
  pointer-events: none;
  transition:
    opacity 0.18s ease,
    transform 0.18s ease;
  z-index: 3000;
}

#solitaire-toast {
  bottom: calc(28px + env(safe-area-inset-bottom));
}
#solitaire-finish {
  bottom: calc(72px + env(safe-area-inset-bottom));
}

@media (pointer: coarse) {
  .hud button {
    min-height: 40px;
  }
  .menu-close {
    min-height: 36px;
  }
  .settings-opt {
    min-height: 42px;
  }
  .corner-actions .icon-btn {
    width: 50px;
    height: 50px;
  }
  .corner-actions .icon-btn svg {
    width: 24px;
    height: 24px;
  }
}

@media (max-width: 820px) {
  .hud {
    gap: 8px 10px;
    padding: calc(8px + env(safe-area-inset-top)) calc(12px + env(safe-area-inset-right)) 8px
      calc(12px + env(safe-area-inset-left));
  }
  .hud .title {
    font-size: 18px;
  }
  .hud .game-options button {
    font-size: 13px;
    padding: 6px 11px;
  }
  .hud .stats {
    order: 3;
    flex: 1 0 100%;
    justify-content: space-between;
    gap: 8px;
    font-size: 14px;
  }
  .hud .controls {
    gap: 6px;
  }
  .hud .controls button {
    padding: 7px 11px;
    font-size: 13px;
  }
}

@media (max-width: 520px) {
  .hud .title {
    font-size: 17px;
  }
  .hud .game-options button {
    font-size: 13px;
    padding: 6px 10px;
  }
  .hud .controls button {
    padding: 7px 10px;
    font-size: 13px;
  }
  .hud .stats {
    font-size: 13px;
  }
}

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