:root {
  --bg: #faf7f0;
  --paper: #fdfbf6;
  --ink: #1f1c16;
  --ink-2: #6e675c;
  --line: #e5dfd2;
  --accent: #955f22;
  --serif: "Iowan Old Style", Palatino, "Palatino Linotype", "Book Antiqua", Georgia, serif;
  --col: 44rem;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14120e;
    --paper: #191712;
    --ink: #ece7dc;
    --ink-2: #9b948a;
    --line: #2b2820;
    --accent: #c89858;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 17px/1.55 var(--serif);
  -webkit-tap-highlight-color: transparent;
}
/* the first screen: bar + game. The colophon sits below it, so it never takes height from the plate */
.fold { min-height: 100dvh; display: flex; flex-direction: column; }
[hidden] { display: none !important; }
a { color: inherit; text-decoration-color: var(--line); text-underline-offset: 3px; }
@media (hover: hover) { a:hover { color: var(--accent); } }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }

/* ---- top bar ---- */
.bar {
  display: flex; justify-content: space-between; align-items: baseline;
  width: 100%; max-width: var(--col); margin: 0 auto;
  padding: max(20px, env(safe-area-inset-top)) 20px 6px;
}
.brand {
  font-size: 13px; font-weight: 600; letter-spacing: 0.42em; text-transform: uppercase;
  color: var(--accent); text-decoration: none;
}
.bar-nav { display: flex; gap: 30px; } /* 18 px between the visible words, see .link */
button { font: inherit; color: inherit; }
/* a 45 px hit area; the negative margins give it back, so the line it sits in keeps its height */
.link {
  background: none; border: 0; padding: 12px 6px; margin: -12px -6px; cursor: pointer;
  font-size: 13.5px; color: var(--ink-2);
}
@media (hover: hover) { .link:hover { color: var(--accent); } }
/* the sound toggle: struck through when off */
#btn-sound[aria-pressed="false"] { text-decoration: line-through; text-decoration-thickness: 1px; text-decoration-color: var(--ink-2); }

/* ---- game column ---- */
#game {
  flex: 1; width: 100%; max-width: var(--col); margin: 0 auto;
  padding: 10px 20px 0;
  display: flex; flex-direction: column;
}
.masthead { text-align: center; margin: 6px 0 10px; }
h1 {
  margin: 0; font-weight: 400; font-size: clamp(30px, 7.2vw, 44px); line-height: 1.05;
  letter-spacing: -0.01em;
}
.plate-line {
  margin: 4px 0 0; font-size: 12.5px; letter-spacing: 0.18em; text-transform: uppercase; color: var(--ink-2);
  font-variant-numeric: tabular-nums;
}
.challenge, .notice {
  margin: 0 0 10px; padding: 6px 10px; text-align: center; font-size: 14.5px; font-style: italic;
  border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); color: var(--ink);
  text-wrap: pretty;
}
.challenge b, .notice b { font-style: normal; font-weight: 600; }
.notice { font-style: normal; color: var(--ink-2); }
.link.strong { font-size: inherit; color: var(--accent); text-decoration: underline; text-underline-offset: 3px; padding-inline: 4px; margin-inline: -4px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---- the plate ---- */
.stage { flex: 1; display: flex; flex-direction: column; align-items: center; min-height: 0; }
.plate { margin: 0; width: 100%; flex: 1; display: flex; flex-direction: column; min-height: 0; }
/* grows into whatever the first screen leaves (see .fold); the floor is for short viewports, the cap for tall */
.plate-frame {
  position: relative; flex: 1; min-height: 250px; max-height: 560px;
  background: var(--paper);
  border: 1px solid var(--ink);
  outline: 1px solid var(--line); outline-offset: 4px;
  margin: 5px;
}
.plate-frame canvas {
  position: absolute; inset: 0; width: 100%; height: 100%; display: block;
  touch-action: none; cursor: grab; -webkit-user-select: none; user-select: none;
}
.plate-frame canvas:active { cursor: grabbing; }
.plate-frame canvas:focus-visible { outline-offset: -4px; }
.fig-no {
  position: absolute; top: 8px; left: 11px; z-index: 1; pointer-events: none;
  font-size: 13px; font-style: italic; color: var(--ink-2);
}
/* a bottom corner, on the side the piece goes over: below the table top there is only air. When the fallen
   pieces lie in that corner, the top corner of the same side, under the Fig. n row (main.js stampCorner) */
.stamp {
  position: absolute; right: 14px; bottom: 12px; z-index: 1; pointer-events: none;
  padding: 3px 10px 2px; border: 2px solid var(--accent); color: var(--accent);
  font-size: 13px; font-weight: 600; letter-spacing: 0.22em; text-transform: uppercase;
  transform: rotate(-5deg); opacity: 0.9;
  animation: stamp 160ms cubic-bezier(.2, .9, .3, 1.2);
}
.stamp.bottom-left, .stamp.top-left { right: auto; left: 14px; transform: rotate(4deg); }
.stamp.top-right, .stamp.top-left { bottom: auto; top: 30px; }
@keyframes stamp { from { transform: rotate(-5deg) scale(1.7); opacity: 0; } }
figcaption {
  margin: 12px 4px 0; min-height: 3.1em; text-align: center;
  font-size: 15.5px; font-style: italic; color: var(--ink); text-wrap: balance; white-space: pre-line;
}
figcaption b { font-style: normal; font-weight: 600; font-variant-numeric: tabular-nums; }

/* ---- the five bars ---- */
.bars {
  list-style: none; margin: 4px 0 12px; padding: 0;
  display: flex; gap: 10px; align-items: flex-end; height: 34px;
}
.bars li {
  position: relative; width: 22px; height: 100%;
  border-bottom: 1px solid var(--ink-2);
}
.bars li::before {
  content: ""; position: absolute; left: 3px; right: 3px; bottom: 0;
  height: calc(var(--v, 0) * 1%); min-height: 0; background: var(--ink);
  transition: height 420ms cubic-bezier(.2, .8, .2, 1);
}
.bars li.stood::before { min-height: 2px; }
.bars li.fell::after {
  content: "×"; position: absolute; inset: auto 0 1px 0; text-align: center;
  font-size: 20px; line-height: 1; color: var(--accent);
}
.bars li.now { border-bottom: 2px solid var(--accent); }

/* ---- buttons ---- */
.primary, .secondary {
  width: 100%; max-width: 21rem; min-height: 52px; padding: 0 20px; cursor: pointer;
  font-size: 14px; font-weight: 600; letter-spacing: 0.24em; text-transform: uppercase;
  border-radius: 2px; touch-action: manipulation;
}
.primary { background: var(--ink); color: var(--bg); border: 1px solid var(--ink); }
@media (hover: hover) { .primary:hover:not(:disabled) { background: var(--accent); border-color: var(--accent); } }
.primary:active:not(:disabled), .secondary:active { transform: translateY(1px); }
.primary:disabled { background: none; color: var(--ink-2); border-color: var(--line); cursor: default; }
/* pressed and waiting (the hold, the fall, the reveal): stays ink, goes quiet, does not flip to hollow */
.primary:disabled.busy { background: var(--ink); color: var(--bg); border-color: var(--ink); opacity: 0.4; }
.secondary { background: none; color: var(--ink); border: 1px solid var(--ink); }
@media (hover: hover) { .secondary:hover { color: var(--accent); border-color: var(--accent); } }
.stage .primary { margin-bottom: max(16px, env(safe-area-inset-bottom)); }

/* ---- result sheet ---- */
.sheet { text-align: center; padding: 8px 0 24px; }
.sheet .no { margin: 0; font-size: 12.5px; letter-spacing: 0.18em; text-transform: uppercase; color: var(--ink-2); }
.sheet .score {
  margin: 2px 0 0; font-size: clamp(84px, 27vw, 148px); line-height: 1; letter-spacing: -0.03em;
  font-variant-numeric: tabular-nums lining-nums;
}
.sheet .score small { font-size: 0.42em; letter-spacing: 0; color: var(--ink-2); }
.sheet .bars { margin: 14px auto 10px; justify-content: center; height: 44px; }
.sheet .verdict { margin: 0 0 22px; font-size: 20px; font-style: italic; text-wrap: balance; }
.sheet .actions { display: flex; flex-direction: column; gap: 10px; align-items: center; margin-bottom: 26px; }
.sheet .duel { margin: 0 0 18px; font-size: 18px; }
.sheet .duel small { display: block; margin-top: 2px; font-size: 14.5px; color: var(--ink-2); }
.sheet .facts { margin: 0 auto 22px; max-width: 30rem; font-size: 15.5px; color: var(--ink-2); text-wrap: pretty; }
.sheet .facts p { margin: 0 0 6px; }
.sheet .next { margin: 0 0 18px; font-size: 13px; letter-spacing: 0.14em; text-transform: uppercase; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.sheet .more { display: flex; gap: 34px; justify-content: center; }
.name-row { display: flex; gap: 8px; justify-content: center; align-items: center; width: 100%; max-width: 21rem; }
.name-row input {
  flex: 1; min-width: 0; height: 44px; padding: 0 12px; font: inherit; font-size: 16px;
  color: var(--ink); background: var(--paper); border: 1px solid var(--ink-2); border-radius: 2px;
}
.name-row .slim { width: auto; min-height: 44px; padding: 0 16px; }
/* the share text itself, shown when neither the share sheet nor the clipboard would take it */
.share-text {
  margin: 2px 0 0; padding: 10px 14px; width: 100%; max-width: 21rem; text-align: left;
  font: inherit; font-size: 14.5px; line-height: 1.5; white-space: pre-wrap; overflow-wrap: anywhere;
  color: var(--ink); background: var(--paper); border: 1px solid var(--line); border-radius: 2px;
  -webkit-user-select: all; user-select: all;
}
.sheet:focus { outline: none; }
.plates-list .done { font-weight: 600; }
.plates-list .todo { color: var(--accent); }

/* ---- dialogs ---- */
dialog.paper {
  width: min(34rem, calc(100vw - 32px)); max-height: min(86dvh, 44rem); padding: 26px 24px 0;
  background: var(--bg); color: var(--ink); border: 1px solid var(--ink); border-radius: 2px;
  outline: 1px solid var(--line); outline-offset: 4px;
}
dialog.paper::backdrop { background: color-mix(in srgb, var(--ink) 38%, transparent); }
dialog.paper { overflow: auto; overscroll-behavior: contain; }
/* the bottom 30 px: a fade over whatever is still to scroll, plain paper once the end is reached */
dialog.paper::after {
  content: ""; display: block; position: sticky; bottom: 0; height: 30px; pointer-events: none;
  background: linear-gradient(to bottom, transparent, var(--bg) 70%);
}
.dlg-head { display: flex; justify-content: space-between; align-items: baseline; margin: 0 0 12px; }
dialog.paper h2 { margin: 0; font-size: 13px; font-weight: 600; letter-spacing: 0.3em; text-transform: uppercase; color: var(--accent); }
dialog.paper h2:focus { outline: none; }
dialog.paper p { margin: 0 0 12px; font-size: 15.5px; line-height: 1.5; }
dialog.paper p:last-child { margin-bottom: 0; }
dialog.paper .close { padding: 12px 8px; margin: -12px -8px; }
.plates-list { list-style: none; margin: 0; padding: 0; }
.plates-list li { border-top: 1px solid var(--line); }
.plates-list li:last-child { border-bottom: 1px solid var(--line); }
.plates-list button, .plates-list .row {
  display: flex; width: 100%; justify-content: space-between; align-items: baseline; gap: 12px;
  padding: 12px 2px; background: none; border: 0; cursor: pointer; text-align: left;
  font-variant-numeric: tabular-nums;
}
@media (hover: hover) { .plates-list button:hover { color: var(--accent); } }
.plates-list .when { color: var(--ink-2); font-size: 14.5px; }

/* ---- colophon ---- */
.colophon {
  width: 100%; max-width: var(--col); margin: 28px auto 0; padding: 18px 20px max(22px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--line); color: var(--ink-2); font-size: 13.5px; text-align: center;
}
.colophon p { margin: 0 0 6px; text-wrap: pretty; }
.colophon-links { display: flex; gap: 18px; justify-content: center; flex-wrap: wrap; }

/* ---- toast ---- */
.toast {
  position: fixed; left: 50%; bottom: max(24px, env(safe-area-inset-bottom)); transform: translate(-50%, 12px);
  padding: 9px 16px; background: var(--ink); color: var(--bg); font-size: 14px; border-radius: 2px;
  opacity: 0; pointer-events: none; transition: opacity 160ms, transform 160ms;
  white-space: nowrap; max-width: calc(100vw - 32px);
}
.toast.on { opacity: 1; transform: translate(-50%, 0); }

.noscript { max-width: 30rem; margin: 40px auto; padding: 0 20px; text-align: center; }

@media (min-width: 720px) {
  .bar { padding-top: 28px; }
  .masthead { margin: 14px 0 16px; }
  /* a desktop window shows the sheet and the footer at once; the toast drops in from the top, over the bar */
  .toast { bottom: auto; top: 18px; transform: translate(-50%, -12px); }
  .toast.on { transform: translate(-50%, 0); }
}
@media (max-height: 640px) {
  .masthead { margin: 0 0 6px; }
  h1 { font-size: 26px; }
  figcaption { margin-top: 8px; min-height: 2.6em; font-size: 14.5px; }
  .primary, .secondary { min-height: 46px; }
}
/* a phone on its side: the plate takes the left, the bars and the button the right, so nothing is below the fold */
@media (orientation: landscape) and (max-height: 480px) {
  .bar { padding-top: 8px; }
  .masthead { margin: 0 0 2px; }
  .stage { display: grid; grid-template-columns: minmax(0, 1fr) 13rem; grid-template-rows: minmax(0, 1fr) auto; column-gap: 20px; align-items: end; }
  .plate { grid-row: 1 / 3; align-self: stretch; }
  .plate-frame { min-height: 150px; }
  figcaption { margin-top: 6px; min-height: 2.6em; }
  .bars { grid-column: 2; justify-content: center; margin: 0 0 12px; }
  .stage .primary { grid-column: 2; margin-bottom: 10px; }
  .colophon { margin-top: 10px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
