:root {
  --bg: #f4f0e7;
  --ink: #171714;
  --muted: #77736a;
  --line: rgba(23, 23, 20, 0.14);
  --paper: #fffdf8;
  --card-paper: #fffdf8;
  --card-ink: #171714;
  --back: #f4f3ef url("assets/card-back.png") center / cover no-repeat;
  --edge: #e8e4da;
  --accent: #171714;
  --red: #bc3b31;
  --red-text: #bc3b31;
  --shadow: 0 10px 24px rgba(25, 22, 16, 0.10);
  --ring: 1.5px solid rgba(23, 23, 20, 0.28);
  color-scheme: light;
  --ease: cubic-bezier(0.2, 0.8, 0.2, 1);
  /* Cards scale with the viewport height so the whole table fits without scrolling. */
  --card-h: clamp(72px, calc((100vh - 290px) / 5.4), 124px);
  --card-w: calc(var(--card-h) * 0.71);
  --radius: calc(var(--card-h) * 0.08);
  --gap: calc(var(--card-h) * 0.1);
}

@supports (height: 100dvh) {
  :root { --card-h: clamp(72px, calc((100dvh - 290px) / 5.4), 124px); }
}

/* Dark theme: follows the system unless the toggle picked a side. Cards stay paper-colored. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #161614;
    --ink: #ebe7de;
    --muted: #8f8a80;
    --line: rgba(235, 231, 222, 0.14);
    --paper: #201f1c;
    --card-paper: #ebe7de;
    --back: linear-gradient(rgba(22, 22, 20, 0.12), rgba(22, 22, 20, 0.12)), #e4e1da url("assets/card-back.png") center / cover no-repeat;
    --edge: #cdc8bd;
    --accent: #ebe7de;
    --red-text: #e36b5f;
    --shadow: 0 10px 24px rgba(0, 0, 0, 0.35);
    --ring: 1.5px solid rgba(235, 231, 222, 0.35);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #161614;
  --ink: #ebe7de;
  --muted: #8f8a80;
  --line: rgba(235, 231, 222, 0.14);
  --paper: #201f1c;
  --card-paper: #ebe7de;
  --back: linear-gradient(rgba(22, 22, 20, 0.12), rgba(22, 22, 20, 0.12)), #e4e1da url("assets/card-back.png") center / cover no-repeat;
  --edge: #cdc8bd;
  --accent: #ebe7de;
  --red-text: #e36b5f;
  --shadow: 0 10px 24px rgba(0, 0, 0, 0.35);
  --ring: 1.5px solid rgba(235, 231, 222, 0.35);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body { min-height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: ui-sans-serif, -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}

button, a { font: inherit; }
button { color: inherit; touch-action: manipulation; }

.app {
  width: min(1080px, 100%);
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0 auto;
  padding: 22px 28px 0;
  display: flex;
  flex-direction: column;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 42px;
}

.brand {
  color: var(--ink);
  text-decoration: none;
  font-weight: 700;
  letter-spacing: -0.04em;
  font-size: 18px;
}

.top-actions { display: flex; gap: 18px; }
.text-button,
.icon-button {
  border: 0;
  background: transparent;
  padding: 6px 0;
  cursor: pointer;
  color: var(--muted);
}
.text-button:hover, .icon-button:hover { color: var(--ink); }

/* Play area: history on the left, the table centered, an empty column balancing it. */
.play {
  flex: 1;
  display: grid;
  grid-template-columns: 200px minmax(0, 1fr) 200px;
  gap: 24px;
}
.play::after { content: ""; }

/* As tall as the table's cards; older turns fade out at the bottom. */
.history {
  align-self: center;
  height: calc(var(--card-h) * 5.76 + 22px);
  overflow-y: auto;
  scrollbar-width: none;
  -webkit-mask-image: linear-gradient(to bottom, #000 70%, transparent);
  mask-image: linear-gradient(to bottom, #000 70%, transparent);
}
.history::-webkit-scrollbar { display: none; }

.history-title,
.history-who {
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.history-title { margin: 0 0 14px; }
.history-who { font-size: 10px; }

.history-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.modal .history-list { max-height: 60vh; margin-top: 18px; overflow-y: auto; }

.history-group {
  display: flex;
  flex-direction: column;
  gap: 3px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.4;
}
.history-group:first-child { color: var(--ink); }
.history-card { font-weight: 600; color: var(--ink); }
.history-card.red { color: var(--red-text); }
.history-line.fresh { animation: history-in 320ms var(--ease); }
@keyframes history-in { from { opacity: 0; transform: translateY(-4px); } }

.history-toggle { display: none; }

@media (max-width: 860px) {
  .play { grid-template-columns: minmax(0, 1fr); }
  .play::after, .history { display: none; }
  .history-toggle { display: inline-block; }
}

@media (prefers-reduced-motion: reduce) {
  .history-line.fresh { animation: none; }
}

/* Table: only cards live here. */
.table {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: calc(var(--card-h) * 0.28);
  padding-block: 16px;
}

/* Hands are a 2x2 grid; penalty cards add rows away from the table center. */
.hand {
  display: flex;
  flex-wrap: wrap;
  align-content: center;
  justify-content: center;
  gap: var(--gap);
  width: calc(var(--card-w) * 2 + var(--gap));
  min-height: calc(var(--card-h) * 2 + var(--gap));
}

/* The computer sits opposite, so its grid is rotated 180°: its "bottom two" face it. */
.ai-hand {
  flex-direction: row-reverse;
  flex-wrap: wrap-reverse;
}

.table-center {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: calc(var(--card-w) * 0.22);
  padding-bottom: 22px;
}

.slot,
.deck {
  position: relative;
  width: var(--card-w);
  height: var(--card-h);
  border-radius: var(--radius);
}

.pile-label {
  position: absolute;
  left: 0;
  right: 0;
  bottom: -24px;
  text-align: center;
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
  pointer-events: none;
}

.deck {
  padding: 0;
  border: 0;
  background: none;
}
.deck.ready { cursor: pointer; }
.deck-stack { position: absolute; inset: 0; }
.deck-stack:empty { border: 1px dashed var(--line); border-radius: var(--radius); }

/* Each layer is one sliver of the pile, so a fuller deck is a taller stack. */
.deck-layer {
  position: absolute;
  inset: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--edge);
  transform: translateY(calc(var(--i) * -2px));
  transition: transform 160ms var(--ease);
}
.deck-layer:first-child { box-shadow: var(--shadow); }
.deck-layer:last-child { background: var(--back); }
.deck.ready .deck-layer:last-child { outline: var(--ring); outline-offset: 3px; }
/* Hover lifts only where there's a real pointer; on touch they'd stick after a tap. */
@media (hover: hover) {
  .deck.ready:hover .deck-layer:last-child { transform: translateY(calc(var(--i) * -2px - 5px)); }
}

.discard-pile .card { position: absolute; inset: 0; pointer-events: none; }
.discard-pile.empty { border: 1px dashed var(--line); }
.discard-pile.ready { cursor: pointer; }
.discard-pile.ready .card:last-of-type { outline: var(--ring); outline-offset: 3px; }
@media (hover: hover) {
  .discard-pile.ready:hover .card:last-of-type { transform: translateY(-5px); }
}

/* Cards are two faces on a hinge; .face-up turns the hinge. */
.card {
  position: relative;
  width: var(--card-w);
  height: var(--card-h);
  padding: 0;
  border: 0;
  border-radius: var(--radius);
  background: none;
  perspective: 800px;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  transition: transform 160ms var(--ease);
}
.card:disabled { cursor: default; }
.card.selectable { cursor: pointer; outline: var(--ring); outline-offset: 3px; }
@media (hover: hover) {
  .card.selectable:hover { transform: translateY(-5px); }
}
.card.selected { transform: translateY(-8px); outline: 2px solid var(--ink); outline-offset: 3px; }
.card.lifted { transform: translateY(-14px); }

.card-inner {
  position: absolute;
  inset: 0;
  border-radius: inherit;
  transform-style: preserve-3d;
}
.card.face-up .card-inner { transform: rotateY(180deg); }

.card-face {
  position: absolute;
  inset: 0;
  border: 1px solid var(--line);
  border-radius: inherit;
  box-shadow: var(--shadow);
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
}
.card-back { background: var(--back); }
.card-front {
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--card-paper);
  color: var(--card-ink);
  transform: rotateY(180deg);
}

.card-corner {
  position: absolute;
  top: calc(var(--card-h) * 0.07);
  left: calc(var(--card-w) * 0.1);
  display: flex;
  flex-direction: column;
  align-items: center;
  font-size: calc(var(--card-h) * 0.13);
  font-weight: 700;
  line-height: 0.92;
}

.card-corner.bottom {
  top: auto;
  left: auto;
  right: calc(var(--card-w) * 0.1);
  bottom: calc(var(--card-h) * 0.07);
  transform: rotate(180deg);
}

.card-suit {
  margin-top: calc(var(--card-h) * 0.04);
  font-size: calc(var(--card-h) * 0.12);
}

.card-center {
  font-size: calc(var(--card-h) * 0.27);
  line-height: 1;
}

.red { color: var(--red); }

/* Dock: status and the buttons that make sense right now. */
.dock {
  position: sticky;
  bottom: 0;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px 24px;
  min-height: 78px;
  padding-block: 14px;
  border-top: 1px solid var(--line);
  background: var(--bg);
}

.status {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

.turn-label {
  display: flex;
  align-items: center;
  gap: 7px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.turn-label::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
  opacity: 0.5;
}
.turn-label.active { color: var(--ink); }
.turn-label.active::before { opacity: 1; }

.message { font-size: 14px; line-height: 1.4; }
.muted { color: var(--muted); }

.actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

.primary-button,
.secondary-button {
  border-radius: 999px;
  padding: 10px 16px;
  cursor: pointer;
  font-weight: 600;
  font-size: 13px;
}

.primary-button {
  border: 1px solid var(--accent);
  background: var(--accent);
  color: var(--bg);
}
.primary-button:hover { opacity: .88; }

.secondary-button {
  border: 1px solid var(--line);
  background: transparent;
}
.secondary-button:hover { border-color: var(--ink); }

.modal {
  border: 0;
  padding: 0;
  width: min(520px, calc(100vw - 32px));
  border-radius: 18px;
  background: var(--paper);
  color: var(--ink);
  box-shadow: 0 30px 90px rgba(20, 18, 14, .24);
}
.modal::backdrop { background: rgba(27, 25, 21, .24); backdrop-filter: blur(3px); }
.modal-content { padding: 24px; }
.modal-header { display: flex; align-items: center; justify-content: space-between; }
.modal h2 { margin: 0; font-size: 28px; letter-spacing: -.05em; }
.icon-button { font-size: 28px; line-height: 1; }
.rules-grid { display: grid; gap: 8px; margin: 22px 0; }
.rules-grid > div { display: grid; grid-template-columns: 52px 1fr; gap: 12px; padding-bottom: 8px; border-bottom: 1px solid var(--line); }
.rules-grid span { color: var(--muted); }
.small { font-size: 13px; color: var(--muted); line-height: 1.55; }

.result-content { text-align: center; }
.result-content h2 { margin-top: 4px; font-size: 36px; }
.score-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin: 28px 0; }
.score-row > div { border: 1px solid var(--line); border-radius: 13px; padding: 18px; display: flex; flex-direction: column; gap: 4px; }
.score-row span { color: var(--muted); font-size: 12px; }
.score-row strong { font-size: 28px; }
.full { width: 100%; }

@media (max-width: 620px) {
  .app { padding: 16px 16px 0; }
  .top-actions { gap: 14px; }
  .text-button { padding-block: 10px; }
  .dock {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
    min-height: 118px;
    text-align: center;
  }
  .status, .turn-label { align-items: center; justify-content: center; }
  .actions { justify-content: center; }
}

@media (prefers-reduced-motion: reduce) {
  .card, .deck-layer { transition: none; }
}
