:root {
  --ink: #4a4440;
  --paper: rgba(252, 248, 242, 0.82);
  --line: rgba(74, 68, 64, 0.14);
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: #efe6da;
  color: var(--ink);
  font-family: var(--serif);
  -webkit-font-smoothing: antialiased;
  user-select: none;
  -webkit-user-select: none;
}

#stage {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

button { font: inherit; color: inherit; cursor: pointer; }

/* ---------- HUD ---------- */

.hud {
  position: fixed;
  top: 0; left: 0; right: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 18px 22px;
  pointer-events: none;
}
.hud > * { pointer-events: auto; }
.hud-title { font-size: 15px; letter-spacing: 0.02em; opacity: 0.85; }
.brand { font-size: 20px; font-style: italic; margin-right: 6px; }
.hud-actions { display: flex; gap: 8px; }

.icon-btn {
  width: 38px; height: 38px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--paper);
  backdrop-filter: blur(6px);
  font-size: 17px;
  line-height: 1;
  transition: transform 0.2s ease, opacity 0.2s ease;
}
.icon-btn:hover { transform: translateY(-1px); }
.icon-btn.off { opacity: 0.45; text-decoration: line-through; }

.hint {
  position: fixed;
  left: 50%;
  bottom: 22px;
  transform: translateX(-50%);
  margin: 0;
  padding: 8px 16px;
  border-radius: 999px;
  background: var(--paper);
  font-family: var(--sans);
  font-size: 13px;
  white-space: nowrap;
  transition: opacity 0.6s ease;
  pointer-events: none;
}
.hint.fade { opacity: 0; }

kbd {
  font-family: var(--sans);
  font-size: 0.8em;
  padding: 1px 6px;
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 5px;
  background: rgba(255,255,255,0.6);
}

.touch-rotate {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 70px;
  display: flex;
  justify-content: space-between;
  padding: 0 max(16px, env(safe-area-inset-right)) 0 max(16px, env(safe-area-inset-left));
  pointer-events: none; /* only the buttons themselves take touches */
}
.touch-rotate button {
  pointer-events: auto;
  width: 54px; height: 54px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--paper);
  font-size: 24px;
  touch-action: manipulation;
}

/* ---------- Overlays ---------- */

.overlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(239, 230, 218, 0.28);
  backdrop-filter: blur(3px);
  animation: fade-in 0.9s ease both;
}
.overlay[hidden] { display: none; }

.card {
  width: min(440px, 100%);
  padding: 34px 34px 28px;
  border-radius: 22px;
  background: var(--paper);
  backdrop-filter: blur(14px);
  box-shadow: 0 24px 60px -24px rgba(60, 40, 30, 0.35);
  text-align: center;
  animation: rise 0.9s cubic-bezier(.2,.7,.2,1) both;
}

.eyebrow {
  margin: 0 0 10px;
  font-family: var(--sans);
  font-size: 12px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  opacity: 0.6;
}

h1 {
  margin: 0 0 14px;
  font-weight: 400;
  font-size: 32px;
  line-height: 1.2;
}
h1 span { font-style: italic; }

.muted { opacity: 0.72; font-size: 15px; line-height: 1.55; margin: 0 0 18px; }
.small { font-size: 13px; margin: 16px 0 0; }
.done-note { font-style: italic; }

.sentence {
  font-size: 19px;
  font-style: italic;
  line-height: 1.45;
  margin: 0 0 22px;
}

.stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  margin: 0 0 24px;
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
}
.stats div { padding: 12px 4px; }
.stats div + div { border-left: 1px solid var(--line); }
.stats dt {
  font-family: var(--sans);
  font-size: 11px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  opacity: 0.55;
}
.stats dd { margin: 4px 0 0; font-size: 22px; }

.row { display: flex; gap: 10px; justify-content: center; }

.primary, .ghost {
  padding: 11px 26px;
  border-radius: 999px;
  font-family: var(--sans);
  font-size: 15px;
  transition: transform 0.2s ease, background 0.2s ease;
}
.primary { border: none; background: var(--ink); color: #fbf7f1; }
.ghost { border: 1px solid var(--line); background: transparent; }
.primary:hover, .ghost:hover { transform: translateY(-1px); }

.error {
  position: fixed;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  padding: 16px 22px;
  border-radius: 14px;
  background: var(--paper);
}

@keyframes fade-in { from { opacity: 0; } }
@keyframes rise { from { opacity: 0; transform: translateY(14px); } }

@media (max-width: 520px) {
  .card { padding: 26px 22px 22px; }
  h1 { font-size: 26px; }
  .hint { font-size: 12px; }
}

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

/* ---------- Mode switch ---------- */

.hud-title { display: flex; align-items: baseline; gap: 14px; }
.modes {
  display: inline-flex;
  padding: 3px;
  border-radius: 999px;
  background: var(--paper);
  border: 1px solid var(--line);
  font-family: var(--sans);
  font-size: 13px;
}
.modes a {
  padding: 5px 12px;
  border-radius: 999px;
  color: inherit;
  text-decoration: none;
  opacity: 0.7;
}
.modes a.active { background: var(--ink); color: #fbf7f1; opacity: 1; }

a { color: inherit; }
.small a { opacity: 0.8; text-underline-offset: 3px; }

.link {
  margin-top: 10px;
  border: none;
  background: none;
  font-family: var(--sans);
  font-size: 13px;
  opacity: 0.6;
  text-decoration: underline;
  text-underline-offset: 3px;
}

/* ---------- Tall mode readout ---------- */

.tall-hud {
  position: fixed;
  top: 64px;
  left: 50%;
  transform: translateX(-50%);
  text-align: center;
  pointer-events: none;
}
.tall-height { font-size: 34px; font-variant-numeric: tabular-nums; }
.tall-height span { transition: color 0.4s ease; }
.tall-sub { font-family: var(--sans); font-size: 12px; letter-spacing: 0.06em; opacity: 0.6; }

/* ---------- Result cards sit beside the tower ---------- */
/* The finished tower is the reward: keep it in view. */

.overlay.side {
  background: none;
  backdrop-filter: none;
  place-items: center start;
  padding-left: max(24px, 6vw);
  pointer-events: none;
}
.overlay.side .card { pointer-events: auto; width: min(380px, 100%); }
.rank { margin: -8px 0 16px; }

/* ---------- Gallery ---------- */

.gallery { padding: 0; place-items: stretch; background: rgba(239, 230, 218, 0.5); }
.gallery-panel {
  margin: auto;
  width: min(980px, 100%);
  height: min(100%, 860px);
  display: flex;
  flex-direction: column;
  background: var(--paper);
  backdrop-filter: blur(16px);
  border-radius: 22px;
  box-shadow: 0 24px 60px -24px rgba(60, 40, 30, 0.35);
  overflow: hidden;
  animation: rise 0.7s cubic-bezier(.2,.7,.2,1) both;
}
.gallery-panel > header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  padding: 24px 26px 12px;
}
.gallery-panel h2 { margin: 0; font-weight: 400; font-size: 26px; }
.gallery-grid {
  flex: 1;
  overflow-y: auto;
  padding: 8px 26px 26px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 18px;
  align-content: start;
}
.gallery-grid figure { margin: 0; text-align: center; }
.gallery-grid canvas {
  display: block;
  width: 100% !important;
  height: auto !important;
  aspect-ratio: 3 / 4;
  border-radius: 14px;
  box-shadow: 0 8px 24px -14px rgba(60, 40, 30, 0.5);
}
.gallery-grid figcaption { margin-top: 8px; font-size: 15px; }
.gallery-grid figcaption small { display: block; font-family: var(--sans); font-size: 11px; opacity: 0.55; letter-spacing: 0.05em; }
.gallery-grid figure.mine canvas { outline: 2px solid var(--ink); outline-offset: 3px; }
.gallery-empty { grid-column: 1 / -1; text-align: center; opacity: 0.6; font-style: italic; padding: 40px 0; }

@media (max-width: 860px) {
  .overlay.side { place-items: start center; padding: 72px 16px 0; }
  .overlay.side .card { padding: 22px 20px 18px; }
  .overlay.side h1 { font-size: 24px; margin-bottom: 6px; }
  .overlay.side .sentence { font-size: 16px; margin-bottom: 14px; }
  .overlay.side .stats { margin-bottom: 14px; }
  .overlay.side .stats dd { font-size: 18px; }
  .gallery-panel { border-radius: 0; height: 100%; }
  .gallery-grid { grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 14px; padding: 8px 16px 20px; }
  .gallery-panel > header { padding: 18px 16px 10px; }
}

@media (max-width: 520px) {
  .hud { padding: 14px 14px; }
  .brand { display: none; }
  .tall-hud { top: 58px; }
}

.hint.top { top: 124px; bottom: auto; }

[hidden] { display: none !important; }
