:root {
  --felt: #1f6f43;
  --felt-dark: #18582f;
  --ink: #1a1a1a;
  --red: #c0392b;
  --black: #1a1a1a;
  --card-bg: #fdfdf7;
  --card-w: 60px;
  --card-h: 84px;
  --gap: 8px;
  --gold: #f2c14e;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: #eee;
  background: radial-gradient(circle at 50% 30%, var(--felt) 0%, var(--felt-dark) 80%);
}

.screen { min-height: 100%; }
.hidden { display: none !important; }

.potato-home-link {
  position: fixed;
  top: 1rem;
  left: 1rem;
  z-index: 20;
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  color: #fff7df;
  text-decoration: none;
  font-weight: 700;
  letter-spacing: 0.02em;
  background: rgba(0, 0, 0, 0.28);
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 999px;
  padding: 0.55rem 0.8rem;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.22);
}
.potato-home-link:hover,
.potato-home-link:focus-visible {
  background: rgba(0, 0, 0, 0.42);
  outline: 2px solid rgba(242, 193, 78, 0.75);
  outline-offset: 3px;
}

/* --- Start screen --- */
#start-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
}
.start-card {
  background: rgba(0, 0, 0, 0.35);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 16px;
  padding: 2.5rem 3rem;
  text-align: center;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.4);
}
.start-card h1 {
  margin: 0;
  font-size: 3rem;
  letter-spacing: 0.05em;
  color: var(--gold);
}
.tagline { margin: 0.25rem 0 2rem; opacity: 0.8; }
.field { display: block; margin-bottom: 2rem; }
.field > span { display: block; margin-bottom: 0.5rem; font-size: 0.9rem; opacity: 0.85; }
.stepper {
  display: inline-flex;
  align-items: center;
  gap: 1rem;
}
.stepper button {
  width: 40px; height: 40px;
  font-size: 1.4rem;
  border-radius: 50%;
  border: none;
  background: rgba(255, 255, 255, 0.15);
  color: #fff;
  cursor: pointer;
}
.stepper button:hover { background: rgba(255, 255, 255, 0.28); }
.stepper output { font-size: 1.8rem; font-weight: 700; min-width: 1.5ch; }

.segmented {
  display: inline-flex;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.2);
}
.segmented button {
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
  border: none;
  padding: 0.5rem 1rem;
  font-size: 0.95rem;
  cursor: pointer;
}
.segmented button + button { border-left: 1px solid rgba(255, 255, 255, 0.2); }
.segmented button:hover { background: rgba(255, 255, 255, 0.16); }
.segmented button.active { background: var(--gold); color: #2a2200; font-weight: 700; }

button.primary {
  background: var(--gold);
  color: #2a2200;
  border: none;
  padding: 0.8rem 2rem;
  font-size: 1.1rem;
  font-weight: 700;
  border-radius: 10px;
  cursor: pointer;
}
button.primary:hover { filter: brightness(1.08); }
button.ghost {
  background: transparent;
  border: 1px solid rgba(255, 255, 255, 0.4);
  color: #fff;
  padding: 0.4rem 0.9rem;
  border-radius: 8px;
  cursor: pointer;
}
button.ghost:hover { background: rgba(255, 255, 255, 0.12); }
button.ghost[aria-pressed="true"] { background: var(--gold); color: #2a2200; border-color: var(--gold); font-weight: 700; }
#topbar-controls { display: flex; gap: 0.5rem; flex-wrap: wrap; }

.link-btn {
  display: block;
  margin: 0.75rem auto 0;
  background: none;
  border: none;
  color: rgba(255, 255, 255, 0.75);
  text-decoration: underline;
  cursor: pointer;
  font-size: 0.9rem;
}
.link-btn:hover { color: #fff; }

.close-btn {
  position: absolute;
  top: 0.75rem;
  right: 0.9rem;
  background: none;
  border: none;
  color: #fff;
  font-size: 1.2rem;
  cursor: pointer;
  opacity: 0.7;
}
.close-btn:hover { opacity: 1; }

.overlay-card.help-card { position: relative; max-width: 540px; text-align: left; }
.help-card h2 { text-align: center; }

.confirm-actions { display: flex; gap: 1rem; justify-content: center; margin-top: 1.25rem; }
.help-body { max-height: 65vh; overflow-y: auto; }
.help-body h3 { color: var(--gold); margin: 1rem 0 0.3rem; font-size: 0.95rem; }
.help-body ul { margin: 0.2rem 0; padding-left: 1.2rem; line-height: 1.6; }
.help-body p { line-height: 1.5; }

/* --- Game screen layout --- */
#game-screen {
  display: grid;
  grid-template-areas:
    "topbar    topbar"
    "banner    banner"
    "opponents scoreboard"
    "table     scoreboard"
    "player    scoreboard";
  grid-template-columns: 1fr 220px;
  gap: 1rem;
  padding: 1rem;
}
#topbar {
  grid-area: topbar;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}
#round-info { font-weight: 700; }
#turn-info { opacity: 0.85; }

#opponents {
  grid-area: opponents;
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  justify-content: center;
}
#table {
  grid-area: table;
  display: flex;
  justify-content: center;
}
#player-area { grid-area: player; display: flex; flex-direction: column; align-items: center; gap: 0.5rem; }
#scoreboard { grid-area: scoreboard; }

.player-label { font-weight: 700; }
.active-player { color: var(--gold); text-shadow: 0 0 8px rgba(242, 193, 78, 0.6); }

/* --- Cards --- */
.card {
  width: var(--card-w);
  height: var(--card-h);
  border-radius: 8px;
  background: var(--card-bg);
  color: var(--ink);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 1.1rem;
  position: relative;
  user-select: none;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}
.card .rank { position: absolute; top: 4px; left: 6px; font-size: 0.85rem; }
.card .rank-br { position: absolute; bottom: 4px; right: 6px; font-size: 0.85rem; transform: rotate(180deg); }
.card .suit { font-size: 1.5rem; }
.card.red { color: var(--red); }
.card.black { color: var(--black); }
.card.joker { color: #7b2ff7; font-size: 0.8rem; }

.card-back {
  background: repeating-linear-gradient(45deg, #2b5fae, #2b5fae 6px, #244f90 6px, #244f90 12px);
  border: 2px solid #fff3;
}
.card-empty {
  background: rgba(255, 255, 255, 0.06);
  border: 2px dashed rgba(255, 255, 255, 0.25);
  box-shadow: none;
}
.card.removed {
  background: rgba(255, 255, 255, 0.04);
  border: 2px solid transparent;
  box-shadow: none;
}

/* --- Grids --- */
.grid {
  display: grid;
  grid-template-columns: repeat(3, var(--card-w));
  gap: var(--gap);
}

/* --- Piles --- */
.pile-area { display: flex; gap: 2rem; align-items: flex-start; }
.pile { display: flex; flex-direction: column; align-items: center; gap: 0.35rem; }
.pile-label { font-size: 0.75rem; opacity: 0.75; }

/* --- Opponents --- */
.opponent {
  background: rgba(0, 0, 0, 0.22);
  border-radius: 10px;
  padding: 0.5rem 0.6rem;
  text-align: center;
}
.opponent .opp-name { font-size: 0.8rem; margin-bottom: 0.35rem; }
.opponent .opp-total { font-size: 0.7rem; opacity: 0.75; }
.opponent .grid {
  --card-w: 26px;
  --card-h: 36px;
  --gap: 3px;
}
.opponent .card { font-size: 0.6rem; border-radius: 4px; }
.opponent .card .rank, .opponent .card .rank-br { display: none; }
.opponent .card .suit { font-size: 0.55rem; }
.opponent .card .mini-val { font-size: 0.7rem; }

/* --- Scoreboard --- */
#scoreboard {
  background: rgba(0, 0, 0, 0.25);
  border-radius: 10px;
  padding: 0.75rem;
  font-size: 0.85rem;
}
#scoreboard h3 { margin: 0 0 0.5rem; font-size: 0.9rem; }
#scoreboard table { width: 100%; border-collapse: collapse; }
#scoreboard th, #scoreboard td { padding: 2px 4px; text-align: right; }
#scoreboard th:first-child, #scoreboard td:first-child { text-align: left; }
#scoreboard tr.leader td { color: var(--gold); font-weight: 700; }

/* --- Animations --- */
@keyframes flip-in {
  0%   { transform: rotateY(90deg); opacity: 0.2; }
  100% { transform: rotateY(0deg); opacity: 1; }
}
@keyframes removed-pop {
  0%   { transform: scale(1); opacity: 1; }
  60%  { transform: scale(1.12); opacity: 1; box-shadow: 0 0 16px var(--gold); }
  100% { transform: scale(0.9); opacity: 0.5; }
}
@keyframes overlay-in {
  from { transform: translateY(12px); opacity: 0; }
  to   { transform: translateY(0); opacity: 1; }
}
@keyframes deal-in {
  from { transform: translateY(-18px) scale(0.85); opacity: 0; }
  to   { transform: translateY(0) scale(1); opacity: 1; }
}
@keyframes last-move-pulse {
  0%   { box-shadow: 0 0 0 0 rgba(96, 205, 255, 0.9); }
  100% { box-shadow: 0 0 0 10px rgba(96, 205, 255, 0); }
}
@keyframes hint-glow {
  0%, 100% { outline-color: rgba(74, 222, 128, 0.4); }
  50%      { outline-color: rgba(74, 222, 128, 0.95); }
}
.card.flip-in { animation: flip-in 0.32s ease-out; }
.card.removed-anim { animation: removed-pop 0.45s ease-out; }
@keyframes pile-pop {
  0%   { transform: scale(0.8) rotate(-4deg); }
  60%  { transform: scale(1.08) rotate(2deg); }
  100% { transform: scale(1) rotate(0); }
}
.card.deal-in { animation: deal-in 0.30s ease-out; }
#discard.pile-pop { animation: pile-pop 0.28s ease-out; }
.card.last-move { animation: last-move-pulse 0.6s ease-out; outline: 2px solid rgba(96, 205, 255, 0.9); outline-offset: 2px; }
.card.col-hint { outline: 2px dashed rgba(74, 222, 128, 0.8); outline-offset: 2px; animation: hint-glow 1.4s ease-in-out infinite; }

/* --- Interaction states --- */
.card.clickable {
  cursor: pointer;
  outline: 3px solid var(--gold);
  outline-offset: 2px;
  transition: transform 0.1s ease;
}
.card.clickable:hover { transform: translateY(-4px); }
.card.selected {
  outline: 3px solid #4ade80;
  outline-offset: 2px;
}
#turn-info.prompt { color: var(--gold); font-weight: 700; }

/* --- Final-turn banner --- */
#final-turn-banner {
  grid-area: banner;
  background: linear-gradient(90deg, #b3322b, #d9534f);
  color: #fff;
  font-weight: 700;
  text-align: center;
  padding: 0.5rem 1rem;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.35);
  animation: overlay-in 0.25s ease-out;
}
#final-turn-banner.you { background: linear-gradient(90deg, #c79100, #f2c14e); color: #2a2200; }

/* --- Difficulty description --- */
#difficulty-desc {
  margin-top: 0.5rem;
  font-size: 0.8rem;
  opacity: 0.8;
  min-height: 1.2em;
}

/* --- Overlay --- */
.overlay {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex; align-items: center; justify-content: center;
}
.overlay-card {
  background: #1d3a28;
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 14px;
  padding: 2rem 2.5rem;
  text-align: center;
  min-width: 320px;
  animation: overlay-in 0.25s ease-out;
}
.overlay-card h2 { color: var(--gold); margin-top: 0; }

/* Summary / standings tables */
#round-summary-table, .standings-table {
  width: 100%;
  border-collapse: collapse;
  margin: 0.5rem 0 1.25rem;
}
#round-summary-table th, #round-summary-table td,
.standings-table th, .standings-table td {
  padding: 0.35rem 0.75rem;
  text-align: right;
}
#round-summary-table th:first-child, #round-summary-table td:first-child,
.standings-table th:first-child, .standings-table td:first-child {
  text-align: left;
}
#round-summary-table thead th, .standings-table thead th {
  font-size: 0.75rem;
  opacity: 0.7;
  border-bottom: 1px solid rgba(255, 255, 255, 0.15);
}
.standings-table tr.you td { color: var(--gold); font-weight: 700; }
.standings-table .medal { width: 1.5rem; text-align: center; }
.round-delta { opacity: 0.7; font-size: 0.85em; }

@media (max-width: 720px) {
  #game-screen {
    grid-template-areas: "topbar" "banner" "opponents" "table" "player" "scoreboard";
    grid-template-columns: 1fr;
  }
  #scoreboard { font-size: 0.78rem; }
}

@media (max-width: 480px) {
  :root { --card-w: 48px; --card-h: 68px; --gap: 6px; }
  .pile-area { gap: 1.2rem; }
  #topbar { flex-wrap: wrap; row-gap: 0.5rem; }
  .start-card { padding: 2rem 1.5rem; }
  .overlay-card { min-width: auto; width: calc(100vw - 2rem); padding: 1.5rem; }
}

/* Respect reduced-motion preferences. */
@media (prefers-reduced-motion: reduce) {
  .card.flip-in, .card.removed-anim, .overlay-card, .card.clickable { animation: none; transition: none; }
}
