:root {
  --felt: #1e6b3d;
  --felt-dark: #17532f;
  --card-bg: #fdfdfb;
  --card-border: #2a2a2a;
  --red: #b3271e;
  --black: #1a1a1a;
  --slot-border: rgba(255, 255, 255, 0.45);
  --ui-bg: #14401f;
  --ui-text: #f4f4f2;
  --btn-bg: #2c8a4f;
  --btn-bg-hover: #35a05d;
  --card-w: 100px;
  --card-h: 138px;
  --offset: 28px;
}
* { box-sizing: border-box; }
html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background: var(--felt);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
  color: var(--ui-text);
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

#topbar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  flex-wrap: wrap;
  padding: 10px 12px;
  background: var(--ui-bg);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
}
#topbar h1 {
  font-size: 1.3rem;
  margin: 0 12px 0 0;
  font-weight: 600;
  letter-spacing: 0.5px;
  width: 100%;
  text-align: center;
}

.mode-tabs, .controls, .mode-options {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: center;
}

button, .mode-tab, .opt-btn {
  font-size: 1rem;
  padding: 10px 16px;
  border-radius: 8px;
  border: none;
  background: var(--btn-bg);
  color: white;
  cursor: pointer;
  font-weight: 600;
}
button:hover, .mode-tab:hover, .opt-btn:hover { background: var(--btn-bg-hover); }
button:active, .mode-tab:active, .opt-btn:active { transform: translateY(1px); }
button:disabled { opacity: 0.5; cursor: default; }

.mode-tab {
  background: rgba(255, 255, 255, 0.12);
}
.mode-tab.active {
  background: var(--btn-bg);
}

.opt-btn {
  padding: 6px 12px;
  background: rgba(255, 255, 255, 0.12);
}
.opt-btn.active {
  background: var(--btn-bg);
}
.mode-options span {
  opacity: 0.85;
  font-size: 0.9rem;
}

.score-display {
  font-size: 1.05rem;
  font-weight: 700;
  padding: 6px 4px;
}

[hidden] { display: none !important; }

#message {
  min-height: 1.4em;
  text-align: center;
  font-size: 1.1rem;
  padding: 4px;
  font-weight: 600;
}

#board {
  max-width: 1040px;
  margin: 0 auto;
  padding: 14px;
}

.game-board[hidden] { display: none; }

.row {
  display: flex;
  gap: 10px;
  margin-bottom: 16px;
}
.top-row { justify-content: space-between; }
.spacer { flex: 1; }

.pile {
  position: relative;
  width: var(--card-w);
  min-height: var(--card-h);
}

.slot-outline {
  position: absolute;
  top: 0; left: 0; right: 0;
  height: var(--card-h);
  border: 2px dashed var(--slot-border);
  border-radius: 8px;
}

.card {
  position: absolute;
  top: 0; left: 0;
  width: var(--card-w);
  height: var(--card-h);
  border-radius: 8px;
  background: var(--card-bg);
  border: 2px solid var(--card-border);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.35);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 6px;
  font-weight: 700;
  cursor: pointer;
}
.card.face-down {
  background: repeating-linear-gradient(45deg, #2c5aa0, #2c5aa0 6px, #244b86 6px, #244b86 12px);
  border: 2px solid #16305c;
}
.card.red { color: var(--red); }
.card.black { color: var(--black); }
.card .corner { font-size: 1.1rem; line-height: 1.05; }
.card .corner.bottom { align-self: flex-end; transform: rotate(180deg); }
.card .center-suit {
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  font-size: 2.2rem;
  opacity: 0.9;
}
.card.selected {
  outline: 4px solid #ffd54a;
  outline-offset: 2px;
  box-shadow: 0 0 10px 3px rgba(255, 213, 74, 0.8);
}

.tableau-pile { min-height: 500px; }

.stock-waste-area { display: flex; gap: 20px; }

.stock-badge {
  text-align: center;
  font-size: 0.8rem;
  margin-top: 6px;
  opacity: 0.85;
  width: var(--card-w);
}

.completed-display {
  color: var(--ui-text);
  font-weight: 700;
  align-self: center;
}

#win-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
  display: none;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 14px;
  z-index: 50;
  text-align: center;
  padding: 20px;
}
#win-overlay.show { display: flex; }
#win-overlay h2 { font-size: 2rem; margin: 0; }

footer {
  text-align: center;
  font-size: 0.85rem;
  opacity: 0.75;
  padding: 20px 10px 30px;
}
footer a { color: #cfe9d6; }

#help-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 60;
  padding: 20px;
}
#help-overlay.show { display: flex; }

.help-panel {
  position: relative;
  background: var(--ui-bg);
  color: var(--ui-text);
  border-radius: 12px;
  max-width: 640px;
  width: 100%;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
}

#help-close {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 36px;
  height: 36px;
  padding: 0;
  border-radius: 50%;
  font-size: 1.3rem;
  line-height: 1;
  background: rgba(255, 255, 255, 0.15);
}

.help-tabs {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  padding: 18px 50px 10px 18px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.15);
}
.help-tab { padding: 8px 14px; font-size: 0.95rem; background: rgba(255, 255, 255, 0.12); }
.help-tab.active { background: var(--btn-bg); }

.help-content {
  padding: 16px 20px;
  overflow-y: auto;
  flex: 1;
}
.help-content h3 { margin: 18px 0 6px; font-size: 1.05rem; }
.help-content h3:first-child { margin-top: 0; }
.help-content p { margin: 6px 0; line-height: 1.5; }
.about-signature { margin-top: 16px; font-style: italic; opacity: 0.8; }
.help-content ul { margin: 6px 0; padding-left: 22px; line-height: 1.6; }
.help-content li { margin-bottom: 4px; }

.example-block {
  margin: 12px 0 18px;
  background: rgba(255, 255, 255, 0.06);
  border-radius: 8px;
  padding: 12px;
}
.example-title { font-weight: 700; margin-bottom: 8px; }
.example-stage {
  display: flex;
  align-items: center;
  min-height: 90px;
}
.example-row { gap: 6px; flex-wrap: wrap; }
.example-stack { padding-left: 4px; }
.example-arrow {
  font-size: 1.4rem;
  padding: 0 8px;
  opacity: 0.85;
}
.example-caption {
  margin-top: 8px;
  font-size: 0.9rem;
  font-weight: 600;
}
.example-caption.valid { color: #7be08a; }
.example-caption.invalid { color: #ff8b7e; }

.help-card {
  width: 56px !important;
  height: 78px !important;
  flex-shrink: 0;
}
.help-card .corner { font-size: 0.62rem !important; }
.help-card .center-suit { font-size: 1.05rem !important; }

.help-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  padding: 14px 20px;
  border-top: 1px solid rgba(255, 255, 255, 0.15);
}
.help-dontshow {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.85rem;
  opacity: 0.85;
  cursor: pointer;
}
.help-dontshow input { width: 16px; height: 16px; }

@media (max-width: 600px) {
  .help-panel { max-height: 90vh; }
  .help-footer { flex-direction: column; align-items: stretch; }
  .help-footer button { width: 100%; }
}

#freecell-board { --card-w: 92px; --card-h: 127px; --offset: 28px; }
#spider-board { --card-w: 80px; --card-h: 110px; --offset: 22px; }

@media (max-width: 900px) {
  #spider-board .row.tableau-row {
    overflow-x: auto;
    padding-bottom: 10px;
  }
  #spider-board { --card-w: 68px; --card-h: 94px; --offset: 20px; }
}

@media (max-width: 760px) {
  .row { gap: 6px; }
  :root { --card-w: 12.3vw; --card-h: 17vw; --offset: 7vw; }
  .pile, .card, .slot-outline { min-width: 42px; }
  .card, .slot-outline { min-height: 60px; }
  .card .corner { font-size: 0.75rem; }
  .card .center-suit { font-size: 1.3rem; }
  #topbar h1 { font-size: 1.05rem; }
  button, .mode-tab, .opt-btn { padding: 8px 10px; font-size: 0.85rem; }
  #freecell-board { --card-w: 11vw; --card-h: 15.2vw; --offset: 6vw; }
  #spider-board { --card-w: 56px; --card-h: 78px; --offset: 18px; }
  .stock-badge { width: auto; font-size: 0.7rem; }
}
