@font-face {
  font-family: 'Fraunces';
  src: url(../fonts/fraunces.woff2) format('woff2');
  font-weight: 400 800;
  font-display: swap;
}
@font-face {
  font-family: 'Figtree';
  src: url(../fonts/figtree.woff2) format('woff2');
  font-weight: 400 700;
  font-display: swap;
}

:root {
  --felt: #2a6b4e;
  --felt-edge: #1f5a42;
  --on-felt: #f4f0e3;
  --on-felt-muted: rgba(244, 240, 227, 0.72);
  --slot: rgba(255, 255, 255, 0.2);
  --bar: rgba(10, 30, 22, 0.28);
  --card: #fcfaf4;
  --card-edge: rgba(40, 32, 20, 0.24);
  --red: #c3302b;
  --black: #1d2125;
  --club: var(--black);
  --diamond: var(--red);
  --back: #9a2f2b;
  --back-line: rgba(255, 238, 214, 0.2);
  --back-rim: #f6eddc;
  --glow: #ffd45e;
  --panel: #fcfaf4;
  --ink: #1d2125;
  --muted: #5d5b53;
  --line: rgba(29, 33, 37, 0.14);
  --accent: #1f5a42;
  --accent-ink: #fff;
  --font: 'Figtree', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --serif: 'Fraunces', Georgia, 'Times New Roman', serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --felt: #18402f;
    --felt-edge: #0f2a20;
    --on-felt: #e9e4d6;
    --on-felt-muted: rgba(233, 228, 214, 0.66);
    --slot: rgba(255, 255, 255, 0.14);
    --bar: rgba(0, 0, 0, 0.26);
    --card: #e9e4d7;
    --card-edge: rgba(0, 0, 0, 0.4);
    --red: #bb3a33;
    --black: #1a1d20;
    --back: #7f2925;
    --back-line: rgba(255, 230, 200, 0.16);
    --back-rim: #d9cfbd;
    --glow: #f2c14e;
    --panel: #1d2421;
    --ink: #ebe6d9;
    --muted: #a7a395;
    --line: rgba(235, 230, 217, 0.14);
    --accent: #6fbf95;
    --accent-ink: #0f2a20;
    color-scheme: dark;
  }
}

:root[data-four] { --club: #18763a; --diamond: #2559bd; }
@media (prefers-color-scheme: dark) {
  :root[data-four] { --club: #1d7a40; --diamond: #2f5fb8; }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--felt-edge);
  color: var(--on-felt);
  font-family: var(--font);
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

button { font: inherit; color: inherit; }

.noscript { padding: 24px; text-align: center; }

/* ---- the layout: a slim header, the table, the buttons ---- */

.game {
  height: 100vh;
  height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr auto;
  background: radial-gradient(120% 90% at 50% 30%, var(--felt) 0%, var(--felt) 45%, var(--felt-edge) 100%);
  user-select: none;
  -webkit-user-select: none;
}

.top {
  display: flex;
  align-items: baseline;
  gap: 12px;
  padding: calc(8px + env(safe-area-inset-top)) calc(14px + env(safe-area-inset-right)) 4px calc(14px + env(safe-area-inset-left));
  min-width: 0;
}
.name {
  margin: 0;
  font: 600 19px/1.2 var(--serif);
  letter-spacing: 0.01em;
  font-variation-settings: 'opsz' 40;
}
.status {
  margin: 0;
  flex: 1;
  min-width: 0;
  font-size: 13px;
  color: var(--on-felt-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.clock {
  margin: 0;
  font-size: 13px;
  color: var(--on-felt-muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.clock[hidden] { display: none; }

.table {
  position: relative;
  min-height: 0;
  overflow: hidden;
  touch-action: none;
  isolation: isolate; /* cards stack among themselves, under the sheets */
}

.bar {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 4px;
  max-width: 560px;
  width: 100%;
  margin: 0 auto;
  padding: 4px calc(10px + env(safe-area-inset-right)) calc(6px + env(safe-area-inset-bottom)) calc(10px + env(safe-area-inset-left));
}
.bar button {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 7px 4px 6px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: var(--on-felt);
  font-size: 12.5px;
  font-weight: 600;
  letter-spacing: 0.02em;
  cursor: pointer;
  touch-action: manipulation;
}
.bar button:hover { background: var(--bar); }
.bar button:active { background: rgba(0, 0, 0, 0.32); }
.bar button:focus-visible { outline: 2px solid var(--glow); outline-offset: -2px; }
.bar button:disabled { opacity: 0.42; cursor: default; background: transparent; }
.bar svg {
  width: 24px;
  height: 24px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.bar button.busy svg { animation: think 0.9s ease-in-out infinite; }
@keyframes think { 50% { opacity: 0.35; } }

/* On a phone held sideways, the buttons go down the side to leave the height for the cards. */
@media (max-width: 520px) {
  .name { display: none; }
}

@media (orientation: landscape) and (max-height: 520px) {
  .game { grid-template-columns: 1fr auto; grid-template-rows: auto 1fr; }
  .top { grid-column: 1; padding-top: calc(4px + env(safe-area-inset-top)); padding-bottom: 0; }
  .name { font-size: 16px; }
  .table { grid-column: 1; grid-row: 2; }
  .bar {
    grid-column: 2;
    grid-row: 1 / 3;
    grid-template-columns: none;
    grid-template-rows: repeat(4, auto);
    align-content: center;
    width: auto;
    margin: 0;
    padding: calc(6px + env(safe-area-inset-top)) calc(8px + env(safe-area-inset-right)) calc(6px + env(safe-area-inset-bottom)) 4px;
  }
  .bar button { padding: 6px 10px; }
}

/* ---- empty places on the table ---- */

.spot {
  position: absolute;
  left: 0;
  top: 0;
  width: var(--cw);
  height: var(--ch);
  border-radius: var(--r);
  box-shadow: inset 0 0 0 1.5px var(--slot);
  display: grid;
  place-items: center;
  color: var(--slot);
  font: 600 calc(var(--cw) * 0.36) / 1 var(--serif);
}
.spot svg { width: 46%; height: 46%; fill: none; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }
.spot[data-kind='stock'], .spot[data-kind='deals'] { cursor: pointer; color: rgba(255, 255, 255, 0.45); }
.spot[data-kind='stock'].gone { color: var(--slot); cursor: default; }
.spot.hint { box-shadow: inset 0 0 0 2px var(--glow), 0 0 14px 2px color-mix(in srgb, var(--glow) 60%, transparent); animation: pulse 1s ease-in-out 3; }

/* ---- cards ---- */

.card {
  position: absolute;
  left: 0;
  top: 0;
  width: var(--cw);
  height: var(--ch);
  transition: transform 0.2s cubic-bezier(0.2, 0.7, 0.3, 1);
  cursor: pointer;
}
.table.still .card, .card.drag { transition: none; }
.card .flip {
  position: absolute;
  inset: 0;
  transform-style: preserve-3d;
  transition: transform 0.26s ease;
}
.table.still .card .flip { transition: none; }
.card.down .flip { transform: rotateY(180deg); }
.card.down { cursor: default; }
.card.deep { visibility: hidden; }
.card.drag .flip { translate: 0 -2px; }
.card.drag .face { box-shadow: 0 0 0 1px var(--card-edge), 0 8px 18px rgba(0, 0, 0, 0.3); }

.face, .back {
  position: absolute;
  inset: 0;
  border-radius: var(--r);
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}
.face {
  background: var(--card);
  box-shadow: 0 0 0 1px var(--card-edge), 0 1px 2px rgba(0, 0, 0, 0.22);
  color: var(--suit);
  overflow: hidden;
}
.back {
  transform: rotateY(180deg);
  background:
    repeating-linear-gradient(45deg, var(--back-line) 0 1.5px, transparent 1.5px 6px),
    repeating-linear-gradient(-45deg, var(--back-line) 0 1.5px, transparent 1.5px 6px),
    var(--back);
  box-shadow: inset 0 0 0 calc(var(--cw) * 0.055) var(--back-rim), inset 0 0 0 calc(var(--cw) * 0.07) rgba(0, 0, 0, 0.12), 0 0 0 1px rgba(0, 0, 0, 0.28), 0 1px 2px rgba(0, 0, 0, 0.22);
}

.card.s, .card.c { --suit: var(--black); }
.card.h { --suit: var(--red); }
.card.c { --suit: var(--club); }
.card.d { --suit: var(--diamond); }

.ix {
  position: absolute;
  left: 7%;
  top: 2.5%;
  font: 640 max(12px, calc(var(--cw) * 0.36)) / 1.05 var(--serif);
  font-variation-settings: 'opsz' 30;
  letter-spacing: -0.05em;
}
.cs {
  position: absolute;
  right: 7%;
  top: calc(var(--cw) * 0.07);
  width: calc(var(--cw) * 0.24);
  height: calc(var(--cw) * 0.24);
}
.pip {
  position: absolute;
  left: 50%;
  top: 60%;
  width: calc(var(--cw) * 0.5);
  height: calc(var(--cw) * 0.5);
  transform: translate(-50%, -50%);
}
.card[data-r='1'] .pip { width: calc(var(--cw) * 0.62); height: calc(var(--cw) * 0.62); }
.court {
  position: absolute;
  left: 13%;
  right: 13%;
  top: 34%;
  bottom: 7%;
  border-radius: calc(var(--r) * 0.6);
  box-shadow: inset 0 0 0 max(1px, calc(var(--cw) * 0.025)) currentColor;
  background: color-mix(in srgb, currentColor 9%, transparent);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font: italic 560 calc(var(--cw) * 0.44) / 0.9 var(--serif);
  font-variation-settings: 'opsz' 72;
}
.court svg { width: calc(var(--cw) * 0.18); height: calc(var(--cw) * 0.18); margin-top: 6%; }
.face svg { fill: currentColor; display: block; }

.card.hint .face { box-shadow: 0 0 0 2px var(--glow), 0 0 14px 3px color-mix(in srgb, var(--glow) 65%, transparent); }
.card.hint .back { box-shadow: inset 0 0 0 calc(var(--cw) * 0.055) var(--back-rim), 0 0 0 2px var(--glow), 0 0 14px 3px color-mix(in srgb, var(--glow) 65%, transparent); }
.card.hint { animation: pulse 1s ease-in-out 3; }
@keyframes pulse { 50% { filter: brightness(1.06); } }

.card.shake .flip { animation: shake 0.32s ease; }
@keyframes shake {
  20% { translate: -5px 0; }
  45% { translate: 4px 0; }
  70% { translate: -2px 0; }
  100% { translate: 0 0; }
}

/* ---- messages ---- */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(78px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  max-width: min(92vw, 440px);
  width: max-content;
  padding: 10px 16px;
  border-radius: 14px;
  background: var(--panel);
  color: var(--ink);
  font-size: 14.5px;
  line-height: 1.35;
  text-align: center;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.3);
  z-index: 40;
  animation: rise 0.2s ease;
}
.toast[hidden] { display: none; }
.toast button {
  margin: 0 0 0 10px;
  padding: 3px 10px;
  border: 0;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 650;
  font-size: 13.5px;
  cursor: pointer;
}
@keyframes rise { from { opacity: 0; translate: 0 8px; } }

/* ---- sheets (new game, menu) ---- */

.sheet { position: fixed; inset: 0; z-index: 50; }
.sheet[hidden] { display: none; }
.scrim { position: absolute; inset: 0; background: rgba(0, 0, 0, 0.42); animation: fade 0.18s ease; }
@keyframes fade { from { opacity: 0; } }
.panel {
  position: absolute;
  left: 50%;
  bottom: 0;
  transform: translateX(-50%);
  width: min(100%, 480px);
  max-height: 88vh;
  max-height: 88dvh;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 18px 20px calc(18px + env(safe-area-inset-bottom));
  border-radius: 20px 20px 0 0;
  background: var(--panel);
  color: var(--ink);
  box-shadow: 0 -8px 40px rgba(0, 0, 0, 0.3);
  animation: up 0.22s cubic-bezier(0.2, 0.7, 0.3, 1);
  user-select: text;
  -webkit-user-select: text;
}
@keyframes up { from { transform: translate(-50%, 30px); opacity: 0; } }
@media (min-width: 600px) and (min-height: 560px) {
  .sheet .panel { bottom: auto; top: 50%; transform: translate(-50%, -50%); border-radius: 20px; animation: pop 0.2s ease; }
  @keyframes pop { from { transform: translate(-50%, -48%); opacity: 0; } }
}
.panel h2 { margin: 0 0 12px; font: 600 24px/1.2 var(--serif); font-variation-settings: 'opsz' 60; }
.panel h3 { margin: 22px 0 10px; font-size: 13px; letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted); }
.panel-head { display: flex; justify-content: space-between; align-items: center; }
.panel-head h2 { margin: 0; }
.close {
  width: 40px; height: 40px;
  margin: -6px -8px 0 0;
  border: 0; border-radius: 50%;
  background: transparent;
  font-size: 28px; line-height: 1;
  color: var(--muted);
  cursor: pointer;
}
.close:hover { background: var(--line); }
.note { margin: -4px 0 12px; color: var(--muted); font-size: 14.5px; }
.note[hidden] { display: none; }

.games { display: grid; gap: 18px; }
.pick h3 { margin: 0 0 3px; font: 600 19px/1.25 var(--serif); letter-spacing: 0; text-transform: none; color: var(--ink); }
.pick p { margin: 0 0 9px; color: var(--muted); font-size: 14px; line-height: 1.4; }
.pick .row { display: flex; flex-wrap: wrap; gap: 8px; }
.choice {
  flex: 1 1 0;
  min-width: max-content;
  padding: 11px 14px;
  border: 1.5px solid var(--line);
  border-radius: 12px;
  background: transparent;
  font-size: 15.5px;
  font-weight: 650;
  cursor: pointer;
}
.choice:hover { border-color: var(--accent); }
.choice.current { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); color: var(--accent); }
.plain {
  display: block;
  width: 100%;
  margin-top: 10px;
  padding: 12px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: var(--accent);
  font-weight: 650;
  cursor: pointer;
}
.plain:hover { background: var(--line); }
.primary {
  display: block;
  width: 100%;
  margin-top: 16px;
  padding: 14px;
  border: 0;
  border-radius: 14px;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
}

.select {
  margin-bottom: 10px;
  padding: 8px 34px 8px 12px;
  border: 1.5px solid var(--line);
  border-radius: 10px;
  background: var(--panel) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 8'%3E%3Cpath d='M1 1.5l5 5 5-5' fill='none' stroke='%23888' stroke-width='1.6'/%3E%3C/svg%3E") no-repeat right 12px center / 12px 8px;
  color: var(--ink);
  font: 600 15px var(--font);
  appearance: none;
  -webkit-appearance: none;
  cursor: pointer;
}
.select:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin: 0;
}
.stats div { padding: 10px 8px; border-radius: 12px; background: var(--line); text-align: center; }
.stats dt { font-size: 12px; color: var(--muted); }
.stats dd { margin: 2px 0 0; font: 600 21px/1.2 var(--serif); font-variant-numeric: tabular-nums; }

.toggle { display: flex; gap: 12px; align-items: flex-start; padding: 9px 0; font-size: 14.5px; line-height: 1.4; color: var(--muted); cursor: pointer; }
.toggle b { display: block; color: var(--ink); font-weight: 600; font-size: 15.5px; }
.toggle input {
  appearance: none;
  -webkit-appearance: none;
  flex: none;
  width: 44px; height: 26px;
  margin: 0;
  border-radius: 999px;
  background: var(--line);
  box-shadow: inset 0 0 0 1px var(--line);
  position: relative;
  cursor: pointer;
  transition: background 0.15s;
}
.toggle input::after {
  content: '';
  position: absolute;
  top: 3px; left: 3px;
  width: 20px; height: 20px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  transition: transform 0.15s;
}
.toggle input:checked { background: var(--accent); }
.toggle input:checked::after { transform: translateX(18px); }
.toggle input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.howto details { border-top: 1px solid var(--line); }
.howto details:last-of-type { border-bottom: 1px solid var(--line); }
.howto summary { padding: 10px 0; font-weight: 650; font-size: 15.5px; cursor: pointer; }
.howto ul { margin: 0 0 12px; padding-left: 20px; font-size: 14.5px; line-height: 1.5; }
.howto li { margin: 4px 0; }
kbd { font: 600 12px var(--font); padding: 1px 6px; border-radius: 5px; border: 1px solid var(--line); background: var(--line); }
.small { font-size: 13.5px; line-height: 1.5; color: var(--muted); }

.jd-foot {
  margin-top: 20px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
  text-align: center;
  font-size: 13px;
  color: var(--muted);
}
.jd-foot a { color: var(--muted); text-decoration: none; }
.jd-foot a:hover, .jd-foot a:focus-visible { text-decoration: underline; }

/* ---- winning ---- */

.won { position: fixed; inset: 0; z-index: 45; }
.won[hidden] { display: none; }
.won canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.won .panel {
  bottom: auto;
  top: 50%;
  transform: translate(-50%, -50%);
  width: min(calc(100% - 32px), 400px);
  border-radius: 20px;
  text-align: center;
  animation: pop2 0.3s ease;
}
.won .panel[hidden] { display: none; }
@keyframes pop2 { from { opacity: 0; transform: translate(-50%, -46%); } }
.won .panel h2 { font-size: 30px; margin-bottom: 6px; }
.won .panel p { margin: 0 0 16px; color: var(--muted); }

@media (prefers-reduced-motion: reduce) {
  .card, .card .flip { transition-duration: 0.01s; }
  .card.hint, .spot.hint { animation: none; }
}
