/* "Living Manuscript": the look of the game (DESIGN.md section 5).
 *
 * Tokens first. One meaning per colour, everywhere:
 *   vermilion = danger, debt, loss     verdigris = profit, growth
 *   gold      = wealth, records        lapis     = information, trust
 * Profit and loss also carry ▲ ▼ so colour is never the only signal.
 */
:root {
  --vellum: #F3E9D2;
  --vellum-deep: #E8D9B5;
  --vellum-slip: #F8F1DF;
  --ink: #2B2118;
  --ink-soft: rgba(43, 33, 24, .62);
  --ink-faint: rgba(43, 33, 24, .14);
  --vermilion: #B23A2B;
  --gold: #C9A227;
  --gold-deep: #9C7A12;
  --lapis: #2E4A8B;
  --verdigris: #4E8A6E;

  --desk: #2A1D14;
  --desk-deep: #120C08;
  --page-glow: linear-gradient(transparent, transparent);

  --font-head: 'Grenze Gotisch', 'UnifrakturMaguntia', serif;
  --font-body: 'EB Garamond', Garamond, Georgia, serif;

  --line: 30px; /* the ledger's ruled line; text sits on it */
  color-scheme: light;
}

/* Night: a candlelit desk. The page stays light, as if lit by the flame, and
   darkens toward its edges; the desk drops to near black. Ink is unchanged,
   so every colour keeps its meaning. */
:root[data-theme="candle"] {
  --vellum: #EAD3A4;
  --vellum-deep: #C9A770;
  --vellum-slip: #F0DDB4;
  --desk: #150E09;
  --desk-deep: #050302;
  --page-glow: radial-gradient(ellipse 70% 60% at 50% 38%, rgba(255, 214, 140, .28), transparent 70%);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; }

body {
  min-height: 100dvh;
  color: var(--ink);
  font: 400 18px/var(--line) var(--font-body);
  font-variant-numeric: lining-nums;
  background:
    radial-gradient(ellipse 120% 90% at 50% 30%, var(--desk) 0%, var(--desk-deep) 100%);
  overflow-x: hidden;
}

.num { font-variant-numeric: tabular-nums lining-nums; white-space: nowrap; }
.up { color: var(--verdigris); }
.down { color: var(--vermilion); }
.info { color: var(--lapis); }
.gold { color: var(--gold-deep); }

/* ── The book ─────────────────────────────────────────────────────────── */

.book {
  position: relative;
  display: grid;
  grid-template-columns: 1fr 1fr;
  max-width: 1240px;
  margin: 28px auto 72px;
  padding: 0 20px;
}

.page {
  position: relative;
  min-height: 700px;
  padding: 44px 56px 56px;
  background:
    repeating-linear-gradient(180deg, transparent 0 calc(var(--line) - 1px), var(--ink-faint) calc(var(--line) - 1px) var(--line)) 0 6px,
    var(--page-glow),
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='220' height='220'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.85' numOctaves='3' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 .35 0 0 0 0 .25 0 0 0 0 .1 0 0 0 .09 0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E"),
    radial-gradient(ellipse 85% 75% at 50% 45%, var(--vellum) 55%, var(--vellum-deep) 100%);
  box-shadow: 0 30px 60px -20px rgba(0, 0, 0, .7), 0 2px 0 rgba(0, 0, 0, .25);
}
.page.verso { border-radius: 6px 0 0 6px; padding-left: 168px; }
.page.recto { border-radius: 0 6px 6px 0; padding-right: 168px; }

/* The gutter, where the pages curve into the binding. */
.page.verso::after, .page.recto::after {
  content: ""; position: absolute; top: 0; bottom: 0; width: 60px; pointer-events: none;
}
.page.verso::after { right: 0; background: linear-gradient(90deg, transparent, rgba(60, 40, 15, .22)); }
.page.recto::after { left: 0; background: linear-gradient(270deg, transparent, rgba(60, 40, 15, .26)); }

/* The double vermilion rule of an account book. (The ruled lines are the
   first layer of the page background above.) */
.page .margin-rule {
  position: absolute; top: 0; bottom: 0; width: 5px;
  border-left: 1px solid rgba(178, 58, 43, .45);
  border-right: 1px solid rgba(178, 58, 43, .45);
}
.page.verso .margin-rule { left: 140px; }
.page.recto .margin-rule { right: 140px; }

/* The outer margin, home of the marginalia. */
.margin {
  position: absolute; top: 0; bottom: 0; width: 140px;
  display: flex; flex-direction: column; align-items: center; gap: 18px;
  padding: 120px 10px 0;
}
.page.verso .margin { left: 0; }
.page.recto .margin { right: 0; }
.gloss {
  font-style: italic; font-size: 14px; line-height: 18px; text-align: center;
  color: var(--ink-soft); max-width: 120px;
}

/* ── Headings and the illuminated capital ─────────────────────────────── */

.running-head {
  display: flex; justify-content: space-between; align-items: baseline;
  font-size: 14px; letter-spacing: .14em; text-transform: uppercase; color: var(--ink-soft);
  border-bottom: 1px solid var(--ink-faint); margin-bottom: 18px;
}
h1, h2 { font-family: var(--font-head); font-weight: 700; line-height: 1; }
h1 { font-size: 44px; margin: 6px 0 14px; }
h2 { font-size: 30px; margin: 22px 0 10px; }

.initial {
  float: left; width: 64px; height: 64px; margin: 4px 12px 0 0;
  display: grid; place-items: center;
  font: 700 54px/1 var(--font-head); color: var(--vermilion);
  background:
    radial-gradient(circle at 30% 25%, rgba(255, 255, 255, .55), transparent 40%),
    linear-gradient(135deg, #E3C350, var(--gold) 45%, var(--gold-deep));
  border: 2px solid var(--lapis);
  box-shadow: inset 0 0 0 3px var(--vellum), inset 0 0 0 4px var(--vermilion);
}
.lede { font-size: 19px; }
.lede em { color: var(--ink-soft); }

/* ── The purse: coin held in each currency (the float) ───────────────── */

.purse { display: flex; flex-wrap: wrap; gap: 8px 22px; margin: 4px 0 6px; }
.purse .coin { display: flex; flex-direction: column; line-height: 1.1; }
.purse .coin small { font-size: 12px; letter-spacing: .12em; text-transform: uppercase; color: var(--ink-soft); }
.purse .coin b { font-weight: 600; font-size: 24px; }
.purse .coin.short b { color: var(--vermilion); }

/* ── The contract card: a slip of paper laid on the page ─────────────── */

.card {
  position: relative;
  margin: 22px 0 10px;
  padding: 18px 20px 16px;
  background: var(--vellum-slip);
  border: 1px solid rgba(43, 33, 24, .28);
  box-shadow: 0 1px 0 rgba(255, 255, 255, .6) inset, 3px 6px 0 -1px rgba(43, 33, 24, .12), 0 10px 22px -12px rgba(43, 33, 24, .5);
  transform: rotate(-.6deg);
  max-width: 440px;
}
.card-kind { font-size: 13px; letter-spacing: .16em; text-transform: uppercase; color: var(--lapis); }
.card h3 { font: 700 28px/1.1 var(--font-head); margin: 2px 0 2px; }
.card .who { font-style: italic; color: var(--ink-soft); line-height: 22px; margin-bottom: 10px; }

.terms { display: grid; grid-template-columns: auto 1fr; column-gap: 14px; }
.terms dt { font-size: 15px; letter-spacing: .06em; font-variant-caps: all-small-caps; color: var(--ink-soft); }
.terms dd { text-align: right; border-bottom: 1px dotted var(--ink-faint); }
.terms dd.ev { border-bottom: 0; font-weight: 600; font-size: 21px; }

.why {
  display: inline-grid; place-items: center; width: 18px; height: 18px; margin-left: 6px;
  border: 1px solid var(--lapis); border-radius: 50%;
  font: italic 600 12px/1 var(--font-body); color: var(--lapis); vertical-align: 2px;
  cursor: help; background: none;
}

.card-actions { display: flex; justify-content: space-between; align-items: center; margin-top: 14px; }
.walk { font: italic 17px var(--font-body); color: var(--ink-soft); background: none; border: 0; text-decoration: underline; text-decoration-color: var(--ink-faint); cursor: pointer; }

/* The wax seal. Pressing it signs the contract. */
.seal {
  position: relative; width: 74px; height: 74px; border: 0; cursor: pointer; background: none;
  filter: drop-shadow(1px 3px 2px rgba(43, 33, 24, .45));
  -webkit-tap-highlight-color: transparent;
}
.seal svg { width: 100%; height: 100%; display: block; }
.seal span {
  position: absolute; left: 50%; bottom: -22px; transform: translateX(-50%);
  font: 600 13px var(--font-body); letter-spacing: .14em; text-transform: uppercase; color: var(--vermilion);
}

/* ── Settlement ──────────────────────────────────────────────────────── */

.ledger { list-style: none; margin-top: 6px; }
.ledger li {
  display: grid; grid-template-columns: 1fr auto; align-items: baseline; gap: 8px;
  position: relative;
}
.ledger .what { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.ledger .what::after { /* dotted leader to the figure */
  content: " . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ."; color: var(--ink-faint);
}
.ledger .what small { color: var(--ink-soft); font-style: italic; }
.ledger .amt { min-width: 4.6em; text-align: right; font-weight: 600; }
.ledger .chain { color: var(--lapis); }
.ledger .tri { font-size: .7em; vertical-align: .12em; margin-left: 3px; }

/* A default: struck through in red ink, drawn across left to right. */
.ledger li.defaulted::before {
  content: ""; position: absolute; left: -4px; right: -4px; top: 52%; height: 2px;
  background: var(--vermilion); transform-origin: left center; transform: scaleX(var(--strike, 1));
  border-radius: 2px; opacity: .9;
  transition: transform .45s cubic-bezier(.5, 0, .2, 1);
}

.net {
  display: grid; grid-template-columns: 1fr auto; align-items: center;
  margin-top: 12px; padding-top: 10px;
  border-top: 3px double var(--gold-deep);
}
.net .label { font: 700 26px var(--font-head); }
.net .amt { font-size: 32px; font-weight: 600; text-align: right; }

/* Rolling digits: each digit is a reel of 0 to 9 that slides into place. */
.reel { display: inline-block; height: 1.15em; line-height: 1.15em; overflow: hidden; vertical-align: -.24em; }
.reel > span { display: block; transition: transform var(--roll, 900ms) cubic-bezier(.16, .9, .26, 1.04); }
.reel > span > i { display: block; font-style: normal; height: 1.15em; }

.flash-up { animation: flash-up 900ms ease-out; }
@keyframes flash-up { 0% { background: rgba(78, 138, 110, .32); } 100% { background: transparent; } }

/* ── Marginalia ──────────────────────────────────────────────────────── */

.marginalia { width: 132px; overflow: visible; filter: drop-shadow(2px 3px 0 rgba(43, 33, 24, .16)); }
.marginalia g { transform-box: view-box; }

.snail-foot   { transform-origin: 20px 112px;  animation: snail-crawl 2.6s ease-in-out infinite; }
.snail-shell  { transform-origin: 94px 108px;  animation: snail-rock 2.6s ease-in-out infinite; }
.snail-head   { transform-origin: 150px 104px; animation: snail-nod 2.6s ease-in-out infinite; }
.snail-stalks { transform-origin: 176px 74px;  animation: snail-look 3.4s ease-in-out infinite; }
.snail-drop   { animation: snail-sweat 4.2s ease-in infinite; }

@keyframes snail-crawl { 0%, 100% { transform: scaleX(1); } 50% { transform: scaleX(1.035); } }
@keyframes snail-rock  { 0%, 100% { transform: translateX(0) rotate(-2deg); } 50% { transform: translateX(3px) rotate(2.5deg); } }
@keyframes snail-nod   { 0%, 100% { transform: translateX(0); } 50% { transform: translateX(5px) rotate(-2deg); } }
@keyframes snail-look  { 0%, 100% { transform: rotate(-7deg); } 40% { transform: rotate(9deg); } 55% { transform: rotate(6deg); } }
@keyframes snail-sweat { 0%, 55% { transform: translate(0, 0); opacity: 0; } 62% { opacity: 1; } 100% { transform: translate(6px, 34px); opacity: 0; } }

/* ── The news ticker along the foot of the desk ───────────────────────── */

.ticker {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
  height: 40px; overflow: hidden; display: flex; align-items: center;
  background: linear-gradient(180deg, rgba(18, 12, 8, .0), rgba(18, 12, 8, .92) 40%);
  color: var(--vellum-deep); font-style: italic; font-size: 16px;
}
.ticker .run { display: flex; gap: 64px; white-space: nowrap; padding-left: 100%; animation: ticker 38s linear infinite; }
.ticker .run b { font: 700 17px var(--font-head); font-style: normal; color: var(--gold); margin-right: 8px; }
@keyframes ticker { to { transform: translateX(-100%); } }

/* ── Phone: one page at a time, swiped like a book ────────────────────── */

.tabs { display: none; }

@media (max-width: 860px) {
  :root { --line: 28px; }
  body { font-size: 17px; }
  .book {
    display: flex; overflow-x: auto; overflow-y: hidden; scroll-snap-type: x mandatory;
    margin: 0; padding: 0; max-width: none; scrollbar-width: none;
  }
  .book::-webkit-scrollbar { display: none; }
  .page, .page.verso, .page.recto {
    flex: 0 0 100%; scroll-snap-align: start; border-radius: 0;
    min-height: calc(100dvh - 96px);
    padding: 108px 16px 120px;
  }
  .page.verso::after, .page.recto::after { display: none; }
  /* The margin becomes a strip along the top of the page. */
  .margin, .page.verso .margin, .page.recto .margin {
    left: 0; right: 0; top: 0; bottom: auto; width: auto; height: 96px;
    flex-direction: row; justify-content: flex-start; padding: 8px 16px 0;
    border-bottom: 1px solid rgba(178, 58, 43, .45);
  }
  .margin .gloss { text-align: left; max-width: 150px; }
  .page .margin-rule { display: none; }
  .marginalia { width: 104px; }
  h1 { font-size: 38px; }
  .card { max-width: none; transform: rotate(-.4deg); }

  .tabs {
    position: fixed; left: 0; right: 0; bottom: 40px; z-index: 6;
    display: flex; justify-content: center; gap: 4px; padding: 6px 8px;
    background: rgba(18, 12, 8, .92);
  }
  .tabs a {
    flex: 1; max-width: 120px; padding: 6px 0; text-align: center; text-decoration: none;
    font: 700 18px var(--font-head); color: rgba(232, 217, 181, .55); border-radius: 3px;
  }
  .tabs a[aria-current="page"] { color: var(--desk-deep); background: var(--vellum-deep); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
