/* Candle (dark), for dim tables: no pure white, no blue.
   The page is a patch of table; questions are printed on cards of dark stock lying on it. */
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #17120e;
  --fg: #f2e6d3;
  --muted: #b3a28a;
  --line: #3b3027;
  --surface: #211a14;
  --btn: #2c231b;
  --btn-line: #5a4734;
  --primary-bg: #3a2d20;
  --primary-fg: #f6ead8;
  --focus: #e3b36b;

  --table-lit: #221a13;
  --table-edge: #100c09;
  --stock: #2b2119;
  --rim: rgb(255 236 210 / 0.08);
  --lip: #1a140f;
  --edge: #3d3026;
  --shadow: 0 0 0;
  --shadow-a: 0.55;
  --back-ss: #2c2017;
  --back-gd: #311b1a;
  --grain-card: 0.9;
  --btn-press: #241d16;
  --primary-press: #30251a;
}

/* Linen (light) is the default. */
:root,
:root[data-theme="light"] {
  color-scheme: light;
  --bg: #e7ddcc;
  --fg: #1e1913;
  --muted: #65584a;
  --line: #cbbca6;
  --surface: #ede5d7;
  --btn: #ebe2d3;
  --btn-line: #cbbca6;
  --primary-bg: #1e1913;
  --primary-fg: #f5efe4;
  --focus: #8a5a1c;

  --table-lit: #efe6d7;
  --table-edge: #d8ccb8;
  --stock: #fbf7ef;
  --rim: rgb(255 255 255 / 0.7);
  --lip: #d9cdb9;
  --edge: #e9e0d0;
  --shadow: 60 40 20;
  --shadow-a: 0.28;
  --back-ss: #6b5440;
  --back-gd: #5a2a27;
  --grain-card: 0.55;
  --btn-press: #e0d6c5;
  --primary-press: #3a3026;
}

/* Each pile has its own printed back; one rule serves the card screen, the lid and ghosts. */
[data-tier="ss"] { --back: var(--back-ss); --back-art: url(paper/back-ss.svg); --pile-tilt: -1deg; }
[data-tier="gd"] { --back: var(--back-gd); --back-art: url(paper/back-gd.svg); --pile-tilt: 0.8deg; }

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

html {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 100%;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  background: var(--bg);
  /* Only the root element controls the viewport: no pull-to-refresh while passing the phone. */
  overscroll-behavior: none;
}

/* iOS only (touch-callout is iOS-only): follow the Larger Text setting, even in the installed app. */
@supports (font: -apple-system-body) and (-webkit-touch-callout: none) {
  html { font: -apple-system-body; }
}

/* A still pool of light on the table; the grain hides any banding in the gradient. */
body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background:
    url(paper/grain-table.png) 0 0 / 64px 64px,
    radial-gradient(120% 70% at 50% 42%, var(--table-lit), var(--bg) 55%, var(--table-edge)),
    var(--bg);
  color: var(--fg);
  line-height: 1.45;
  -webkit-tap-highlight-color: transparent;
}

/* Light theme in the installed iPhone app: the status bar text is white, so give it a dark band. */
:root[data-theme="light"] body::before {
  content: "";
  position: fixed;
  inset: 0 0 auto 0;
  height: env(safe-area-inset-top, 0px);
  background: #1e1913;
  z-index: 10;
}

button, input { font: inherit; color: inherit; }
button { touch-action: manipulation; cursor: pointer; }
a { color: inherit; text-underline-offset: 0.15em; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 3px; border-radius: 6px; }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* ---------------------------------------------------------------- screens */

.screen {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding:
    max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right))
    max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  max-width: 44rem;
  margin: 0 auto;
}
.screen.center { justify-content: center; align-items: center; text-align: center; gap: 1rem; }

.wordmark {
  font-family: ui-serif, "New York", Georgia, "Times New Roman", serif;
  font-style: italic;
  font-weight: 500;
  font-size: 2.6rem;
  letter-spacing: 0.01em;
  margin: 0;
}

.muted { color: var(--muted); margin: 0; }
.fine { color: var(--muted); font-size: 0.85rem; margin: 0; }
.eyebrow { color: var(--muted); font-size: 0.85rem; letter-spacing: 0.08em; text-transform: uppercase; margin: 0 0 0.5rem; }

/* ---------------------------------------------------------------- buttons */

.btn {
  min-height: 48px;
  padding: 0.6rem 1.1rem;
  border-radius: 14px;
  border: 1px solid var(--btn-line);
  background: var(--btn);
  font-weight: 600;
}
.btn.primary { background: var(--primary-bg); color: var(--primary-fg); border-color: var(--btn-line); }
.btn.big { min-height: 56px; width: 100%; font-size: 1.15rem; }
.btn.quiet { background: transparent; border-color: var(--line); color: var(--muted); font-weight: 500; }
.btn:disabled { opacity: 0.4; cursor: default; }

.link {
  background: none;
  border: 0;
  padding: 0.6rem 0.4rem;
  min-height: 44px;
  color: var(--muted);
  text-decoration: underline;
  text-underline-offset: 0.2em;
}

.icon-btn {
  width: 48px;
  height: 48px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  font-size: 1.5rem;
  line-height: 1;
  color: var(--muted);
}
.icon-btn:disabled { opacity: 0.3; cursor: default; }

/* Buttons give a little under the thumb. Only transform animates, so theme switches stay instant. */
.btn, .icon-btn { transition: transform 160ms cubic-bezier(0.2, 0.8, 0.2, 1); }
.btn:active:not(:disabled), .icon-btn:active:not(:disabled) { transform: translateY(1px) scale(0.98); transition-duration: 40ms; }
.btn:active:not(:disabled) { background: var(--btn-press); }
.btn.primary:active:not(:disabled) { background: var(--primary-press); }
.btn.quiet:active:not(:disabled), .icon-btn:active:not(:disabled) { background: var(--btn); }
.link:active { opacity: 0.7; }
/* While the tap guard is up, guarded buttons don't press: the hand learns the tap didn't count. */
[data-guard] :is(#next, #shuffle-again):active:not(:disabled) { transform: none; background: var(--primary-bg); }
[data-guard] :is(#skip, #back, #empty-back):active:not(:disabled) { transform: none; background: transparent; }

/* ---------------------------------------------------------------- unlock */

.unlock { display: flex; flex-direction: column; gap: 0.75rem; width: min(100%, 20rem); text-align: left; }
.unlock label { color: var(--muted); }
.unlock input {
  min-height: 52px;
  padding: 0.6rem 0.9rem;
  border-radius: 12px;
  border: 1.5px solid var(--muted); /* at least 3:1 against the page in both themes */
  background: var(--surface);
  font-size: 1.15rem;
}
.msg { min-height: 1.5em; margin: 0; color: var(--muted); text-align: center; }

/* ---------------------------------------------------------------- pile edges (card screen and lid) */

/* Four card-sized rectangles under the top card, bottom to top; only a sliver of each shows.
   Rest jitter uses the individual translate/rotate properties so animations compose on top. */
.stack { position: absolute; inset: 0 0 14px; }
.stack > span {
  position: absolute;
  inset: 0;
  border-radius: var(--r, 16px);
  background: var(--back);
  border-bottom: 1px solid var(--edge);
  box-shadow: 0 2px 3px rgb(var(--shadow) / calc(var(--shadow-a) * 0.6));
  transition: opacity 300ms ease-out 200ms; /* an edge drops away as the next card lifts */
}
.stack > span:nth-child(1) { translate: 0 12px; rotate: 0.5deg; }
.stack > span:nth-child(2) { translate: -1px 9px; rotate: -0.2deg; }
.stack > span:nth-child(3) { translate: 1px 6px; rotate: 0.3deg; }
.stack > span:nth-child(4) { translate: 0 3px; rotate: -0.4deg; background: var(--back-art) center / cover no-repeat, var(--back); }
[data-depth="3"] .stack > span:nth-child(-n+1),
[data-depth="2"] .stack > span:nth-child(-n+2),
[data-depth="1"] .stack > span:nth-child(-n+3),
[data-depth="0"] .stack > span { opacity: 0; }

/* ---------------------------------------------------------------- lid */

.lid-head { text-align: center; padding: 3vh 0 1.5rem; }
.piles { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1rem; }
.pile { display: flex; flex-direction: column; align-items: center; }
.pile-btn {
  width: 100%;
  border: 0;
  border-radius: 16px;
  background: none;
  padding: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
}
.pile-art { position: relative; display: block; width: min(100%, 11rem); padding-bottom: 14px; --r: 12px; }
.pile-art .stack > span { transition: none; }
.pile-top {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  aspect-ratio: 5 / 7;
  padding: 0.75rem;
  border-radius: 12px;
  background: var(--back-art) center / cover no-repeat, var(--back);
  color: #f2e6d3;
  rotate: var(--pile-tilt);
  box-shadow:
    inset 0 1px 0 rgb(255 236 210 / 0.12),
    0 1px 2px rgb(var(--shadow) / var(--shadow-a)),
    0 10px 22px -10px rgb(var(--shadow) / var(--shadow-a));
  transition: translate 160ms cubic-bezier(0.2, 0.8, 0.2, 1);
}
.pile-btn:active .pile-top { translate: 0 2px; transition-duration: 60ms; }
.pile-title {
  position: relative;
  font-family: ui-serif, "New York", Georgia, "Noto Serif", serif;
  font-size: 1.35rem;
  font-weight: 600;
  line-height: 1.15;
  text-shadow: 0 1px 0 rgb(0 0 0 / 0.5);
  overflow-wrap: break-word;
  hyphens: auto;
}
/* All drawn: just the outline of where the pile was. */
.pile-btn[data-depth="0"] .pile-top { background: none; box-shadow: none; rotate: 0deg; border: 1.5px dashed var(--btn-line); color: var(--muted); }
.pile-btn[data-depth="0"] .pile-top::before { content: none; }
.pile-btn[data-depth="0"] .pile-title { text-shadow: none; }
.pile-left { color: var(--muted); }
.nudge { text-align: center; color: var(--muted); font-style: italic; margin: 2rem 0 1rem; }
.lid-foot { margin-top: auto; text-align: center; display: flex; flex-direction: column; align-items: center; gap: 0.25rem; }

/* ---------------------------------------------------------------- intro, empty, about */

.bar { display: flex; align-items: center; gap: 0.25rem; min-height: 48px; }
.count { margin-left: auto; color: var(--muted); font-variant-numeric: tabular-nums; padding-right: 0.5rem; }

.intro { flex: 1; display: flex; flex-direction: column; justify-content: center; padding: 1rem 0.5rem; }
.intro.center { align-items: center; text-align: center; }
.intro-subtitle { font-size: 2rem; line-height: 1.2; font-weight: 600; margin: 0 0 1rem; }
.intro-text { font-size: 1.2rem; margin: 0 0 1.25rem; }
.intro-tip { color: var(--muted); border-left: 3px solid var(--btn-line); padding-left: 0.9rem; margin: 0 0 1.5rem; }

/* The bottom of the box once every card is out. */
.spot { width: min(40vw, 8rem); aspect-ratio: 5 / 7; margin: 0 auto 1.5rem; border: 1.5px dashed var(--btn-line); border-radius: 12px; }

.controls { display: flex; flex-direction: column; gap: 0.75rem; padding-top: 0.75rem; }
.secondary { display: flex; gap: 0.75rem; }
.secondary .btn { flex: 1; }

.about { padding: 0 0.25rem 2rem; }
.about h2 { font-size: 1.6rem; margin: 0.5rem 0 1rem; }
.about h3 { font-size: 1.05rem; margin: 1.75rem 0 0.5rem; }
.about p { margin: 0 0 0.75rem; }
.links { padding-left: 1.2rem; margin: 0; }
.links li { margin-bottom: 0.5rem; }
.theme-pick { border: 0; padding: 0; margin: 0; display: flex; gap: 1.5rem; }
.theme-pick label, .feel { display: flex; align-items: center; gap: 0.5rem; min-height: 44px; }
.theme-pick input, .feel input { width: 1.25rem; height: 1.25rem; accent-color: var(--focus); flex: none; }

/* ---------------------------------------------------------------- the card */

.card-screen {
  display: grid;
  grid-template-rows: auto 1fr auto;
  height: 100vh;
  height: 100dvh;
  min-height: 0;
}
/* Only while a card is moving or being twisted: let it sweep past the card area, never scroll the
   page. A moving card ignores taps; a twisted one is under two fingers. */
.card-screen.moving, .card-screen.twisting { overflow: hidden; overflow: clip; }
.card-screen.moving .card-area, .card-screen.twisting .card-area { overflow: visible; }
.card-screen.moving .card { pointer-events: none; }

/* The deck is centred with auto margins rather than align-items: center, so a question
   taller than the area (very large text sizes) scrolls from its top instead of being cut off. */
.card-area {
  position: relative;
  z-index: 1;
  container-type: size;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 0.5rem clamp(0.25rem, 2vw, 1rem);
}

.deck { position: relative; width: min(100%, 24rem); margin-block: auto; padding-bottom: 14px; }

/* #card is the live region and focus target; Turn spins it flat on the table, clockwise; two fingers can twist it round either way. */
.card {
  position: relative;
  border-radius: 16px;
  transform: rotate(calc(var(--turns, 0) * 180deg));
  transition: transform 720ms cubic-bezier(0.22, 0.68, 0.3, 1);
}
.card:focus { outline: none; }
.card.still { transition: none; }
/* Two fingers are turning it: the inline transform follows them, then a release animation lands it. */
.card.twist { transition: none; will-change: transform; }
/* Sideways strokes on the card are ours (slide to set it aside); vertical ones and pinches stay the browser's,
   except two fingertips turning it, which swipe.js claims by script. */
.card { touch-action: pan-y; touch-action: pan-y pinch-zoom; }
.zoomed .card { touch-action: auto; }
/* While a finger holds the top card, the next one waits face down beneath it (bare table under the last). */
.card.lifted .sheet { transform: rotateY(180deg); }
.card.lifted .face { opacity: 0; }
.card.lifted .back { opacity: 1; }
.deck[data-depth="0"] .card.lifted .sheet, .card.lifted.bare .sheet { opacity: 0; }

/* The physical card. Everything visual lives here so a clone (ghost) looks identical. */
.sheet {
  position: relative;
  -webkit-touch-callout: none; /* no callout under a resting thumb; text can still be selected to hear or translate it */
  min-height: min(22rem, 52dvh);
  min-height: min(calc(100cqh - 14px), calc(min(100cqw, 24rem) * 1.4)); /* a 5:7 card when there's room */
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 1.25rem 1rem;
  border-radius: 16px;
  text-align: center;
  color: var(--fg);
  background: linear-gradient(to bottom, rgb(255 240 220 / 0.035), transparent 45%), var(--stock);
  box-shadow:
    inset 0 1px 0 var(--rim),
    0 1px 0 var(--lip),
    0 1px 2px rgb(var(--shadow) / var(--shadow-a)),
    0 12px 26px -10px rgb(var(--shadow) / var(--shadow-a));
}
.face { position: relative; font-family: ui-serif, "New York", "Iowan Old Style", Charter, Georgia, "Noto Serif", serif; }
.back { position: absolute; inset: 0; border-radius: inherit; opacity: 0; background: var(--back-art) center / cover no-repeat, var(--back); }
.sheet::before, .back::before, .pile-top::before {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  pointer-events: none;
  background: url(paper/grain-card.png) 0 0 / 64px 64px;
  opacity: var(--grain-card);
}
/* The shadow spreading underneath as a card lifts to turn over. */
.lift { position: absolute; inset: 3% 3% 14px; border-radius: 16px; opacity: 0; box-shadow: 0 22px 34px -8px rgb(var(--shadow) / 0.6); }

.card-label { color: var(--muted); font-size: 0.85rem; letter-spacing: 0.08em; text-transform: uppercase; margin: 0 0 1.25rem; }

/* Viewport units (not container units) so a ghost outside the card area sets identically. */
.card-text p {
  font-size: clamp(1.5rem, 8vw, 2.75rem);
  font-size: clamp(1.5rem, min(8vw, 6.2dvh), 2.75rem);
  font-weight: 500;
  line-height: 1.22;
  max-width: 14em;
  margin: 0 auto;
  text-wrap: balance;
  overflow-wrap: break-word;
  hyphens: auto;
}
.card-text p + p { margin-top: 0.8em; }

.card-hint { color: var(--muted); font-style: italic; font-size: 1.15rem; margin: 1.25rem 0 0; }

.blank {
  display: inline-block;
  width: 3.5em;
  border-bottom: 0.07em solid currentColor;
  margin: 0 0.12em;
  position: relative;
}

/* Cards that leave the table fly in this layer, above the card area (z 1) but below the toast. */
.flight { position: absolute; top: 0; left: 0; width: 100%; height: 100vh; height: 100dvh; overflow: hidden; pointer-events: none; }
.sheet.ghost { position: absolute; margin: 0; min-height: 0; z-index: 2; }
.sheet.ghost.held { will-change: transform; }
.flight .band { position: absolute; left: 0; width: 100%; overflow: hidden; z-index: 2; will-change: transform; }

.toast {
  position: fixed;
  z-index: 3;
  left: max(16px, env(safe-area-inset-left));
  right: max(16px, env(safe-area-inset-right));
  bottom: calc(max(16px, env(safe-area-inset-bottom)) + 9rem);
  margin: 0 auto;
  max-width: 26rem;
  padding: 0.8rem 1rem;
  border-radius: 14px;
  background: var(--surface);
  border: 1px solid var(--btn-line);
  color: var(--fg);
  font-size: 0.95rem;
  text-align: center;
}

/* A phone propped up sideways: the card on the left, buttons on the right. */
@media (orientation: landscape) and (max-height: 520px) {
  .card-screen {
    max-width: none;
    grid-template-columns: 1fr minmax(12rem, 30%);
    grid-template-rows: auto 1fr;
    grid-template-areas: "bar bar" "card controls";
    column-gap: 1rem;
  }
  .card-screen .bar { grid-area: bar; }
  .card-screen .card-area { grid-area: card; }
  .card-screen .controls { grid-area: controls; justify-content: center; padding-top: 0; }
  .card-screen .secondary { flex-direction: column; }
  .deck { width: 100%; }
  .sheet { min-height: calc(100cqh - 14px); padding: 0.9rem 1.25rem; }
  .card-label { margin-bottom: 0.6rem; }
  .card-text p { max-width: 20em; }
  .toast { bottom: max(16px, env(safe-area-inset-bottom)); }
}

/* ---------------------------------------------------------------- dialog */

.confirm {
  border: 1px solid var(--btn-line);
  border-radius: 18px;
  background: var(--surface);
  color: var(--fg);
  padding: 1.25rem;
  width: min(22rem, calc(100vw - 32px));
}
.confirm::backdrop { background: rgb(0 0 0 / 0.55); }
.confirm p { margin: 0 0 1rem; font-size: 1.1rem; }
.confirm-actions { display: flex; gap: 0.75rem; justify-content: flex-end; }

/* Selectors repeat the originals so these overrides win on specificity. */
@media (prefers-reduced-motion: reduce) {
  .card, .stack > span, .btn, .icon-btn, .pile-top { transition: none; }
  .btn:active:not(:disabled), .icon-btn:active:not(:disabled) { transform: none; }
  .pile-btn:active .pile-top { translate: none; opacity: 0.85; }
}
