:root {
  --paper: #f1e6cc;
  --paper-2: #e6d6b2;
  --ink: #1d1a16;
  --ink-soft: rgba(29, 26, 22, 0.62);
  --accent: #1f6f6b;
  --red: #b8402a;
  --gold: #c8962e;
  --serif: Georgia, "Iowan Old Style", "Times New Roman", serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  background: #12100d;
  color: var(--ink);
  font-family: var(--serif);
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

/* Portrait stage; on wide screens it sits centered like a phone. */
#stage {
  position: relative;
  height: 100dvh;
  width: min(100vw, calc(100dvh * 0.62));
  margin: 0 auto;
  overflow: hidden;
  background: var(--paper);
  touch-action: none;
}

#game { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }

[hidden] { display: none !important; }

/* ---------- HUD ---------- */
#hud {
  position: absolute;
  inset: 0 0 auto 0;
  padding: calc(env(safe-area-inset-top) + 10px) 14px 0;
  display: flex;
  align-items: center;
  gap: 10px;
  pointer-events: none;
  z-index: 2;
}
#timer { flex: 1; height: 8px; border: 2px solid var(--ink); border-radius: 6px; background: rgba(241,230,204,.7); overflow: hidden; }
#timerFill { height: 100%; width: 100%; background: var(--ink); transform-origin: left; }
#hudText { font-weight: 700; font-size: 15px; text-align: right; color: var(--ink); background: rgba(241,230,204,.88); border: 1.5px solid var(--ink); border-radius: 12px; padding: 2px 9px; }
#hudText:empty { display: none; }
.icon {
  pointer-events: auto;
  width: 40px; height: 40px;
  border: 2px solid var(--ink); border-radius: 50%;
  background: var(--paper); color: var(--ink);
  font: 700 14px var(--serif); letter-spacing: -1px;
}

/* ---------- Screens ---------- */
.screen {
  position: absolute;
  inset: 0;
  z-index: 3;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: calc(env(safe-area-inset-top) + 24px) 26px calc(env(safe-area-inset-bottom) + 24px);
  overflow-y: auto;
}
.card-screen {
  background:
    radial-gradient(ellipse at center, rgba(241,230,204,0) 55%, rgba(120,90,50,.28) 100%),
    var(--paper);
}
#pause, #end { background-color: rgba(241,230,204,.96); }

h1 {
  font-size: clamp(54px, 17vw, 82px);
  line-height: .9;
  margin: 6px 0 8px;
  letter-spacing: -1px;
  font-style: italic;
}
h2 { font-size: clamp(28px, 8.5vw, 38px); line-height: 1.05; margin: 4px 0 12px; font-style: italic; }
.eyebrow { font-size: 12px; letter-spacing: .18em; text-transform: uppercase; color: var(--ink-soft); margin: 0 0 4px; }
.sub { margin: 0 0 18px; color: var(--ink-soft); font-size: 16px; }
.log { font-size: clamp(20px, 6vw, 24px); line-height: 1.35; max-width: 22ch; margin: 8px 0 22px; }
.tapon { color: var(--ink-soft); font-size: 14px; animation: blink 1.6s infinite; }
@keyframes blink { 50% { opacity: .35; } }

.cliff, .quip { margin-left: auto !important; margin-right: auto !important; }
.stars .count { font-size: 26px; letter-spacing: 1px; }
.cliff { font-style: italic; font-size: 18px; line-height: 1.35; max-width: 24ch; margin: 6px 0 18px; }
.quip { font-size: 20px; font-weight: 700; margin: 6px 0 8px; max-width: 22ch; line-height: 1.25; }
.stat { color: var(--ink-soft); margin: 0 0 6px; font-size: 15px; }
.stars { font-size: 40px; letter-spacing: 6px; margin: 2px 0 4px; color: var(--ink); }
.stars .off { color: rgba(29,26,22,.2); }

.namecard {
  border: 2px solid var(--ink);
  border-radius: 4px;
  padding: 10px 18px 6px;
  margin: 4px 0 16px;
  background: var(--paper-2);
  box-shadow: 4px 4px 0 var(--ink);
  min-width: 250px;
}
#pirateName { font-size: 26px; font-weight: 700; font-style: italic; margin: 2px 0 4px; }

button { font-family: var(--serif); cursor: pointer; touch-action: manipulation; }
.primary {
  min-width: 230px;
  padding: 14px 24px;
  font-size: 21px; font-weight: 700;
  color: var(--paper); background: var(--ink);
  border: 2px solid var(--ink); border-radius: 4px;
  box-shadow: 4px 4px 0 var(--red);
  margin: 6px 0 10px;
}
.primary:active { transform: translate(2px, 2px); box-shadow: 2px 2px 0 var(--red); }
.ghost {
  padding: 9px 14px;
  font-size: 16px;
  color: var(--ink); background: transparent;
  border: 2px solid var(--ink); border-radius: 4px;
  margin: 4px;
}
.ghost.small { font-size: 13px; padding: 6px 10px; border-width: 1.5px; opacity: .8; }
.namecard .ghost { font-size: 13px; padding: 5px 10px; border-width: 1.5px; }
.row { display: flex; gap: 6px; justify-content: center; flex-wrap: wrap; }

#chapterList {
  list-style: none;
  padding: 0;
  margin: 10px 0 12px;
  width: 100%;
  max-width: 320px;
  text-align: left;
}
#chapterList li {
  display: flex; align-items: center; gap: 10px;
  padding: 7px 4px;
  border-bottom: 1px dashed rgba(29,26,22,.3);
  font-size: 16px;
}
#chapterList li button { all: unset; flex: 1; cursor: pointer; }
#chapterList li.locked { opacity: .38; }
#chapterList .num { font-weight: 700; width: 22px; }
#chapterList .mini { font-size: 14px; letter-spacing: 1px; white-space: nowrap; }
#chapterList .act { font-size: 11px; letter-spacing: .16em; text-transform: uppercase; color: var(--ink-soft); border: 0; padding: 12px 4px 2px; }

/* ---------- Share card ---------- */
#share { background: var(--ink); }
.bigcard {
  width: min(100%, 360px);
  background: var(--paper);
  border: 3px solid var(--paper);
  outline: 2px solid var(--ink);
  outline-offset: -10px;
  padding: 34px 22px 26px;
  margin-bottom: 20px;
  box-shadow: 0 0 0 6px var(--gold);
}
.bigname { font-size: clamp(34px, 11vw, 46px); font-weight: 700; font-style: italic; line-height: 1; margin: 8px 0; }
.rank { font-size: 20px; letter-spacing: .12em; text-transform: uppercase; color: var(--red); font-weight: 700; margin: 0 0 6px; }
.stamp { font-size: 13px; color: var(--ink-soft); margin: 12px 0 0; letter-spacing: .06em; }
#share .primary { background: var(--paper); color: var(--ink); border-color: var(--paper); }

@media (prefers-reduced-motion: reduce) {
  .tapon { animation: none; }
}

/* ---------- Doubloon ---------- */
#doubloon { background: radial-gradient(ellipse at center, #2b2418 0%, #12100d 70%); color: var(--paper); }
.coin { width: min(72vw, 300px); aspect-ratio: 1; margin-bottom: 22px; animation: coinSpin 4.5s ease-in-out infinite; filter: drop-shadow(0 10px 22px rgba(230,183,68,.35)); }
@keyframes coinSpin { 0%, 100% { transform: rotateY(0deg) translateY(0); } 25% { transform: rotateY(18deg) translateY(-6px); } 50% { transform: rotateY(0deg) translateY(0); } 75% { transform: rotateY(-18deg) translateY(-6px); } }
.treasure { font-size: clamp(26px, 8vw, 34px); font-style: italic; font-weight: 700; line-height: 1.2; max-width: 14ch; margin: 0 auto 22px; color: #f4d47a; }
.ghost.light { color: var(--paper); border-color: var(--paper); }
#doubloon .primary { background: #e6b744; color: var(--ink); border-color: #e6b744; box-shadow: 4px 4px 0 var(--red); }
@media (prefers-reduced-motion: reduce) { .coin { animation: none; } }
