:root {
  --bg: #f4f1ea;
  --surface: #ffffff;
  --text: #1d1b18;
  --muted: #6b665e;
  --border: #d9d3c7;
  --accent: #1f4e8c;
  --accent-text: #ffffff;
  --card-red: #c8102e;
  --card-black: #161616;
  --card-paper: #fdfcf8;
  --back-red: #b3151d;
  --good: #1f7a3f;
  --bad: #b3261e;
  --warn-bg: #fff4d6;
  --warn-border: #e8c766;
  --card-w: 110px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17181b;
    --surface: #222429;
    --text: #ece9e3;
    --muted: #a39e94;
    --border: #3a3d44;
    --accent: #5b8fd6;
    --good: #5cc283;
    --bad: #f07a70;
    --warn-bg: #3a3020;
    --warn-border: #7d6630;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
}

a { color: var(--accent); }
h1 { font-size: 1.6rem; margin: 0 0 1rem; }
h2 { font-size: 1.2rem; margin: 0; }
code { background: var(--surface); border: 1px solid var(--border); border-radius: 4px; padding: 0 4px; }
.muted { color: var(--muted); }

.site-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 16px; border-bottom: 1px solid var(--border); background: var(--surface); }
.brand { font-weight: 700; text-decoration: none; color: var(--text); }
.page { max-width: 1100px; margin: 0 auto; padding: 24px 16px 64px; }

.page-head { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; justify-content: space-between; margin-bottom: 1rem; }
.page-head h1 { margin: 0; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; }

.button {
  display: inline-block;
  border: 1px solid var(--accent);
  background: var(--accent);
  color: var(--accent-text);
  border-radius: 8px;
  padding: 8px 16px;
  font: inherit;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
}
.button.secondary { background: transparent; color: var(--accent); }
.button:focus-visible, input:focus-visible, textarea:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

.flash { background: var(--surface); border-left: 4px solid var(--good); padding: 8px 12px; }
.warning { background: var(--warn-bg); border: 1px solid var(--warn-border); border-radius: 8px; padding: 10px 14px; }
.errors { color: var(--bad); }

.deck-list { list-style: none; padding: 0; margin: 0; }
.deck-item {
  display: flex; flex-wrap: wrap; gap: 12px; align-items: center; justify-content: space-between;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 16px;
}
.deck-item p { margin: 4px 0 0; }

/* Full set grid */
.card-grid {
  list-style: none; padding: 0; margin: 1.5rem 0 0;
  display: grid; gap: 16px 12px;
  grid-template-columns: repeat(auto-fill, minmax(var(--card-w), 1fr));
}
.card-cell { display: flex; flex-direction: column; align-items: center; gap: 6px; }
.card-number { font-weight: 700; font-variant-numeric: tabular-nums; color: var(--muted); }

/* Playing cards: faces are public-domain SVG artwork, backs are app artwork */
.playing-card {
  position: relative;
  width: var(--card-w);
  aspect-ratio: 167.087 / 242.667;
  container-type: inline-size;
  filter: drop-shadow(0 1px 1px rgb(0 0 0 / 0.2)) drop-shadow(0 4px 8px rgb(0 0 0 / 0.12));
  user-select: none;
  -webkit-user-select: none;
}
.playing-card img { display: block; width: 100%; height: 100%; }

.playing-card.back {
  --back-color: #b5121b;
  background: url("/assets/cards/back-red-84f925af.svg") center / 100% 100% no-repeat;
  display: flex; align-items: center; justify-content: center;
}
[data-back-color=blue] .playing-card.back,
.back-option.back-blue .playing-card.back {
  --back-color: #1d4f9c;
  background-image: url("/assets/cards/back-blue-662da31b.svg");
}
.back-option.back-red .playing-card.back {
  --back-color: #b5121b;
  background-image: url("/assets/cards/back-red-84f925af.svg");
}
.back-number {
  min-width: 34cqw;
  padding: 2cqw 4cqw;
  border-radius: 999px;
  background: #fff;
  color: var(--back-color);
  box-shadow: 0 0 0 1.2cqw var(--back-color), 0 0 0 2cqw #fff;
  font: 700 22cqw/1 Georgia, "Times New Roman", serif;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

/* Back color picker */
.back-picker { margin-top: 32px; }
.back-picker h2 { margin-bottom: 12px; }
.back-options { display: flex; gap: 16px; }
.back-options form { margin: 0; }
.back-option {
  --card-w: 72px;
  display: flex; flex-direction: column; align-items: center; gap: 8px;
  padding: 12px 16px;
  border: 2px solid var(--border); border-radius: 12px;
  background: var(--surface); color: var(--text);
  font: inherit; font-weight: 600;
  cursor: pointer;
}
.back-option[aria-pressed=true] { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); }

/* Stack forms */
.stack-form .field { margin-bottom: 16px; }
.stack-form label { display: block; font-weight: 600; margin-bottom: 4px; }
.stack-form .field p { margin: 0 0 6px; }
.stack-form input[type=text], .stack-form input[type=password], .stack-form textarea {
  width: 100%;
  font: inherit;
  padding: 8px 12px;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--text);
}
.stack-form textarea { font: 1rem/1.6 ui-monospace, "SF Mono", Menlo, monospace; padding: 12px; }
.stack-form .actions { margin-top: 12px; justify-content: flex-end; }
.deck-list { display: flex; flex-direction: column; gap: 12px; }

/* Quiz */
.quiz { max-width: 600px; margin: 0 auto; }
.quiz-play { display: flex; flex-direction: column; align-items: center; gap: 12px; text-align: center; }
.quiz-status { display: flex; justify-content: space-between; width: 100%; color: var(--muted); font-size: 0.95rem; }

.quiz-bar { display: flex; align-items: center; gap: 10px; width: 100%; }
.timer-track { flex: 1; min-width: 60px; height: 10px; border-radius: 999px; background: var(--border); overflow: hidden; }
.timer-fill { height: 100%; background: var(--good); transform-origin: left; transition: transform 50ms linear; }
.timer-track.low .timer-fill { background: var(--bad); }
.timer-text { min-width: 3.2em; text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }
.timer-setting { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 0.9rem; }
.timer-setting input {
  width: 3.6em; font: inherit; font-size: 16px; padding: 4px 6px;
  border: 1px solid var(--border); border-radius: 6px;
  background: var(--surface); color: var(--text);
}
.button.small { padding: 5px 12px; font-size: 0.9rem; }

.quiz-stage-wrap { position: relative; width: 100%; }
.quiz-stage { --card-w: 220px; min-height: 310px; display: flex; align-items: center; justify-content: center; gap: 16px; }
.quiz-stage.pair { --card-w: 180px; }
.paused-overlay {
  display: none;
  position: absolute; inset: 0 -12px -12px; z-index: 1;
  align-items: center; justify-content: center;
  font-size: 1.5rem; font-weight: 700;
  background: var(--bg); border-radius: 12px;
}
.quiz-play.paused .paused-overlay { display: flex; }
.quiz-play.paused .quiz-form { opacity: 0.5; }
.quiz-play.paused .quiz-prompt { visibility: hidden; }

.quiz-bottom { display: flex; flex-direction: column; align-items: center; width: 100%; }
.quiz-prompt { font-size: 1.25rem; font-weight: 600; margin: 0; }
.quiz-feedback { min-height: 1.5em; margin: 6px 0; font-weight: 600; }
.quiz-feedback.correct { color: var(--good); }
.quiz-feedback.wrong { color: var(--bad); }
.quiz-feedback.hint { color: var(--muted); }
.quiz-form { display: flex; gap: 8px; width: 100%; max-width: 380px; }
.quiz-form input {
  flex: 1; min-width: 0;
  font: inherit; font-size: 1.2rem;
  padding: 10px 12px;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--text);
  text-transform: uppercase;
}
.quiz-form input::placeholder { text-transform: none; }
.quiz-answer { width: 100%; max-width: 380px; }
.quiz-answer .button { width: 100%; padding: 12px; }

/* Quiz stats */
.quiz-summary { text-align: center; }
.quiz-summary h2 { font-size: 1.5rem; margin-bottom: 16px; }
.quiz-summary h3 { margin: 24px 0 0; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; margin: 0; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 12px; }
.stat dt { color: var(--muted); font-size: 0.85rem; }
.stat dd { margin: 4px 0 0; font-size: 1.35rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.quiz-summary .actions { justify-content: center; margin-top: 16px; }
.missed-list { --card-w: 72px; display: flex; flex-wrap: wrap; justify-content: center; gap: 12px; margin: 12px 0; }
.missed-item { display: flex; flex-direction: column; align-items: center; gap: 4px; }

/* Phones: the quiz takes over the screen */
@media (max-width: 600px) {
  :root { --card-w: 84px; }

  .quiz-play {
    position: fixed; inset: 0; z-index: 10;
    height: 100dvh;
    gap: 10px;
    padding: max(12px, env(safe-area-inset-top)) 12px max(12px, env(safe-area-inset-bottom));
    background: var(--bg);
  }
  .quiz-status, .timer-setting-label { display: none; }
  .quiz-bar { gap: 8px; }

  .quiz-stage-wrap { flex: 1; min-height: 0; }
  .quiz-stage {
    height: 100%; min-height: 0;
    container-type: size;
    --card-w: min(96cqw, 100cqh * 0.6886);
  }
  .quiz-stage:not(.pair) { align-items: flex-start; }
  .quiz-stage.pair { gap: 3cqw; --card-w: min(47cqw, 100cqh * 0.6886); }

  .quiz-prompt { font-size: 1.1rem; }
  .quiz-form, .quiz-answer { max-width: none; }

  .stats { grid-template-columns: 1fr 1fr; }
}

/* Shuffle & deal animation on the full set */
.card-grid.dealing .playing-card { visibility: hidden; }
.deal-stage {
  position: absolute; top: 0; left: 0; z-index: 20;
  overflow: hidden;
  pointer-events: none;
}
.deal-card {
  position: absolute; top: 0; left: 0;
  width: var(--card-w);
  aspect-ratio: 167.087 / 242.667;
  transform-style: preserve-3d;
  will-change: transform;
}
.deal-card .playing-card {
  position: absolute; inset: 0;
  width: 100%;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  filter: drop-shadow(0 1px 2px rgb(0 0 0 / 0.3));
}
.deal-card .playing-card.face { transform: rotateY(180deg); }
.deal-hand {
  position: absolute; top: 0; left: 0; z-index: 200;
  filter: drop-shadow(0 6px 10px rgb(0 0 0 / 0.25));
}
.deal-skip {
  position: fixed; z-index: 30;
  left: 50%; bottom: max(16px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--surface);
}

.site-header form { margin: 0; }

/* Sign in: a stage behind velvet curtains */
.login-stage {
  position: fixed; inset: 0; z-index: 40;
  display: grid; place-items: center;
  padding: 16px;
  background: radial-gradient(ellipse 60% 55% at 50% 50%, #4a3a2a, #1a120c 70%, #0b0806);
}
.login {
  width: 100%; max-width: 360px;
  padding: 24px;
  background: var(--surface);
  border-radius: 12px;
  box-shadow: 0 0 0 1px var(--border), 0 0 80px 20px rgb(255 236 190 / 0.18);
}
.login .errors:empty { display: none; }
.login-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 1rem; }
.login-head h1 { margin: 0; }
.sound-toggle {
  border: 1px solid var(--border); border-radius: 999px;
  background: transparent; color: var(--muted);
  padding: 4px 10px;
  font: inherit; font-size: 0.85rem;
  cursor: pointer;
}
.sound-toggle[aria-pressed=false] .sound-on, .sound-toggle[aria-pressed=true] .sound-off { display: none; }

.curtain { position: fixed; inset: 0; z-index: 50; overflow: hidden; pointer-events: none; }
.curtain-panel {
  position: absolute; top: 0; bottom: 0;
  width: 50.5%;
  pointer-events: auto;
  will-change: transform;
  background:
    radial-gradient(ellipse 120% 60% at 50% 35%, rgb(255 120 130 / 0.14), transparent 70%),
    linear-gradient(to bottom, rgb(0 0 0 / 0.35), transparent 22%, transparent 75%, rgb(0 0 0 / 0.5)),
    repeating-linear-gradient(90deg, #3d050c 0, #7a0f1f 2.6%, #b51f33 4.6%, #d8394c 5.4%, #a01a2d 6.6%, #5c0914 8.8%, #3d050c 10%);
}
.curtain-panel.left { left: 0; transform-origin: left center; box-shadow: inset -18px 0 24px -8px rgb(0 0 0 / 0.6); }
.curtain-panel.right { right: 0; transform-origin: right center; box-shadow: inset 18px 0 24px -8px rgb(0 0 0 / 0.6); }
.curtain-velvet { position: absolute; inset: 0; width: 100%; height: 100%; }
.curtain.velvet .curtain-panel { background: none; box-shadow: none; }
.curtain.velvet .curtain-panel::after { display: none; }
.curtain-panel::after {
  content: ""; position: absolute; left: 0; right: 0; bottom: 0; height: 10px;
  background: linear-gradient(#e9c46a, #a87a1f);
}

.curtain-valance {
  --swag: clamp(18px, 4vh, 34px);
  position: absolute; top: 0; left: 0; right: 0;
  height: clamp(48px, 10vh, 96px);
  display: grid; place-items: center;
  pointer-events: auto;
  background:
    linear-gradient(to bottom, rgb(0 0 0 / 0.45), transparent 40%, rgb(0 0 0 / 0.3)),
    repeating-linear-gradient(90deg, #5c0914 0, #9c1a2c 3%, #c22e42 4%, #9c1a2c 5%, #5c0914 8%);
  border-bottom: 6px solid #c99a35;
  box-shadow: 0 4px 10px rgb(0 0 0 / 0.5);
}
.curtain-valance::after {
  content: ""; position: absolute; left: 0; right: 0; top: 100%;
  height: var(--swag); margin-top: 6px;
  background: radial-gradient(ellipse 50% 100% at 50% 0, #8e1426 0 82%, #e9c46a 83% 100%, transparent 100%) 0 0 / calc(var(--swag) * 4) 100%;
  filter: drop-shadow(0 4px 4px rgb(0 0 0 / 0.45));
}
.curtain-valance span {
  font: 700 clamp(1.1rem, 3.4vh, 2rem) / 1 Georgia, "Times New Roman", serif;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: #f2d27a;
  text-shadow: 0 1px 0 #6b4a0e, 0 0 12px rgb(242 210 122 / 0.45);
}
.curtain-valance span::before, .curtain-valance span::after { content: " ♠ "; font-size: 0.7em; vertical-align: 0.15em; }

.curtain-hint {
  position: absolute; left: 0; right: 0; bottom: max(32px, env(safe-area-inset-bottom));
  margin: 0; padding: 0 16px;
  text-align: center;
  font: italic 1.05rem Georgia, "Times New Roman", serif;
  color: #f2d27a;
  text-shadow: 0 1px 3px rgb(0 0 0 / 0.8);
  animation: hint-in 0.8s 1.6s both;
}
.curtain-hint .for-touch { display: none; }
@media (hover: none) {
  .curtain-hint .for-pointer { display: none; }
  .curtain-hint .for-touch { display: inline; }
}
@keyframes hint-in { from { opacity: 0; } }

.magic-hand {
  position: fixed; z-index: 60; bottom: -4px;
  left: calc(50% + min(120px, 14vw));
  width: min(220px, 42vw);
  aspect-ratio: 200 / 260;
  visibility: hidden;
  transform-origin: 50% 80%;
  pointer-events: none;
  filter: drop-shadow(0 8px 14px rgb(0 0 0 / 0.45));
}
.magic-hand img, .magic-hand svg { display: block; width: 100%; height: 100%; overflow: visible; }
.wand-tip { position: absolute; left: 11%; top: 7%; width: 1px; height: 1px; }

/* A fan of cards over the list of stacks */
.card-fan {
  --fan-card-w: min(96px, 13vw);
  position: relative;
  height: calc(var(--fan-card-w) * 2.9);
  margin: 0 auto 24px;
}
.fan-card {
  position: absolute; top: 0; left: 50%;
  width: var(--fan-card-w);
  margin-left: calc(var(--fan-card-w) / -2);
  aspect-ratio: 167.087 / 242.667;
  transform-origin: 50% 250%;
  transform: rotate(calc(-55deg + 110deg * var(--i) / 51));
}
.fan-card img { display: block; width: 100%; height: 100%; filter: drop-shadow(0 1px 2px rgb(0 0 0 / 0.3)); }
