/*
  Sketchkeys: page styling.
  The paper (warm #f6f2e9 + grain + vignette) is pure CSS so the canvas can stay transparent.
  The grain tile itself is generated at startup by SketchRenderer.makePaperGrain() and set as --grain.
*/
:root {
  --paper: #f6f2e9;
  --ink: #1e1e1e;
  --pencil: rgba(30, 30, 30, 0.42);
  --accent-red: #d93829;
  --water: #5b8fb9;
  --amber: #e9a23b;
  --leaf: #7d9a5a;
  --board: #26302d;
  --chalk: #eeeadf;
  --grain: none;
  --hand: 'Caveat', 'Segoe Print', 'Bradley Hand', cursive;
  --mono: 'Courier Prime', 'Courier New', ui-monospace, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

body {
  background-color: var(--paper);
  background-image: var(--grain);
  background-size: 256px 256px;
  color: var(--ink);
  font-family: var(--hand);
  transition: background-color 0.9s ease, color 0.9s ease;
}

/* Soft vignette over the paper, under the canvas. */
body::before {
  content: '';
  position: fixed;
  inset: 0;
  pointer-events: none;
  background:
    radial-gradient(ellipse at 50% 45%, rgba(255, 252, 244, 0.35) 0%, rgba(255, 252, 244, 0) 55%),
    radial-gradient(ellipse at center, rgba(0, 0, 0, 0) 58%, rgba(92, 70, 38, 0.16) 100%);
  transition: opacity 0.9s ease;
}

/* Chalkboard ("invert") theme: the canvas re-inks itself in chalk; the page turns to slate. */
body.chalk { background-color: var(--board); color: var(--chalk); }
body.chalk::before {
  background:
    radial-gradient(ellipse at 50% 40%, rgba(255, 255, 255, 0.05) 0%, rgba(255, 255, 255, 0) 60%),
    radial-gradient(ellipse at center, rgba(0, 0, 0, 0) 50%, rgba(0, 0, 0, 0.45) 100%);
}

/* Lightning: the whole page flips colours for two frames. */
html.flash { filter: invert(1); }

#stage {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  display: block;
  cursor: crosshair;
}
#stage.can-pan { cursor: grab; }
#stage.panning { cursor: grabbing; }
body.night #stage { cursor: none; }
/* Lantern mode: the corner controls glow faintly so they stay findable in the dark. */
body.night #corner { color: #e9dcc0; }
body.night .sketch-btn .ink.paper { fill: #2a2622; }
body.night .toast { color: #e9dcc0; }

#soft-keys {
  position: fixed;
  left: -1000px;
  top: 0;
  width: 10px;
  height: 10px;
  opacity: 0;
  font-size: 16px; /* stops iOS from zooming on focus */
}

/* ---------- Corner controls ---------- */
#corner {
  position: fixed;
  top: max(14px, env(safe-area-inset-top));
  right: max(16px, env(safe-area-inset-right));
  display: flex;
  align-items: flex-start;
  gap: 10px;
  z-index: 10;
  transition: opacity 1.2s ease, transform 1.2s ease;
}
#corner.hidden { opacity: 0; transform: translateY(-6px); pointer-events: none; }

.sketch-btn {
  background: none;
  border: none;
  padding: 2px;
  cursor: pointer;
  color: inherit;
  font: inherit;
  display: flex;
  flex-direction: column;
  align-items: center;
  opacity: 0.78;
  transition: opacity 0.2s ease, transform 0.2s ease;
}
.sketch-btn:hover, .sketch-btn:focus-visible { opacity: 1; transform: rotate(-3deg) scale(1.05); outline: none; }
.sketch-btn svg { filter: url(#wobble); overflow: visible; }
.sketch-btn .ink { fill: none; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }
.sketch-btn .ink.paper { fill: var(--paper); transition: fill 0.9s ease; }
body.chalk .sketch-btn .ink.paper { fill: var(--board); }
#journal-btn svg { width: 34px; height: 40px; }
#sound-btn svg { width: 24px; height: 24px; margin-top: 8px; }
.zoom { display: flex; gap: 0; margin-top: 7px; margin-right: 4px; }
.zoom-btn { padding: 1px; }
.zoom-btn svg { width: 22px; height: 22px; }
#sound-btn .mute { display: none; }
#sound-btn.off .mute { display: inline; }
#sound-btn.off .waves { display: none; }
#journal-count {
  font-family: var(--hand);
  font-size: 19px;
  font-weight: 700;
  line-height: 1;
  margin-top: 1px;
  letter-spacing: 0.5px;
}
#journal-btn.bump { animation: bump 0.7s ease; }
@keyframes bump {
  0% { transform: scale(1); }
  30% { transform: scale(1.25) rotate(-6deg); }
  60% { transform: scale(0.95) rotate(3deg); }
  100% { transform: scale(1); }
}

/* ---------- Toasts ---------- */
#toasts {
  position: fixed;
  left: max(18px, env(safe-area-inset-left));
  bottom: max(16px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 4px;
  pointer-events: none;
  z-index: 9;
}
.toast {
  font-family: var(--hand);
  font-size: 22px;
  font-weight: 600;
  opacity: 0;
  transform: translateY(8px) rotate(-1.5deg);
  animation: toast 4.8s ease forwards;
  white-space: nowrap;
}
.toast b { color: var(--accent-red); font-weight: 700; }
@keyframes toast {
  0% { opacity: 0; transform: translateY(8px) rotate(-1.5deg); }
  10% { opacity: 0.92; transform: translateY(0) rotate(-1.5deg); }
  80% { opacity: 0.92; }
  100% { opacity: 0; transform: translateY(-6px) rotate(-1.5deg); }
}

/* ---------- Journal modal ---------- */
#journal {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(40, 34, 26, 0.28);
  transition: opacity 0.25s ease;
  padding: 16px;
}
#journal.hidden { opacity: 0; pointer-events: none; }
.journal-sheet {
  position: relative;
  width: min(760px, 100%);
  max-height: min(86vh, 900px);
  overflow-y: auto;
  background-color: var(--paper);
  background-image: var(--grain), repeating-linear-gradient(transparent 0 33px, rgba(91, 143, 185, 0.16) 33px 34px);
  background-size: 256px 256px, auto;
  padding: 26px 34px 20px 54px;
  color: var(--ink);
  box-shadow: 0 18px 50px rgba(30, 20, 10, 0.28);
  transform: rotate(-0.4deg);
  -webkit-user-select: text;
  user-select: text;
}
.journal-sheet::before { /* red margin line */
  content: '';
  position: absolute;
  left: 38px;
  top: 0;
  bottom: 0;
  border-left: 1.5px solid rgba(217, 56, 41, 0.45);
  pointer-events: none;
}
.sketch-border { border: 2.5px solid var(--ink); border-radius: 6px 10px 7px 12px; }
body.chalk .journal-sheet { background-color: #2c3633; color: var(--chalk); border-color: var(--chalk); }
#journal header h1 { font-family: var(--hand); font-size: 44px; margin: 0; line-height: 1; }
#journal header .sub { font-family: var(--mono); font-size: 13px; margin: 6px 0 10px; opacity: 0.7; }
.close {
  position: absolute; top: 8px; right: 14px; background: none; border: none; color: inherit;
  font-size: 36px; line-height: 1; cursor: pointer; font-family: var(--hand); opacity: 0.7;
}
.close:hover { opacity: 1; transform: rotate(8deg); }
.j-section { margin: 18px 0 6px; }
.j-section h2 {
  font-family: var(--hand); font-size: 30px; margin: 0 0 6px; font-weight: 700;
  display: flex; align-items: baseline; gap: 10px;
}
.j-section h2 small { font-family: var(--mono); font-size: 12px; font-weight: 400; opacity: 0.6; }
.j-grid { display: flex; flex-wrap: wrap; gap: 8px 10px; }
.j-word {
  font-family: var(--mono);
  font-size: 15px;
  padding: 3px 10px 2px;
  border: 1.6px solid currentColor;
  border-radius: 12px 6px 11px 7px;
  background: none;
  color: inherit;
  cursor: pointer;
  transition: transform 0.15s ease, background 0.15s ease;
}
.j-word:hover { transform: rotate(-2deg) scale(1.05); background: rgba(233, 162, 59, 0.18); }
.j-word.unknown { border-style: dashed; opacity: 0.45; cursor: help; letter-spacing: 3px; }
.j-word.unknown:hover { transform: none; background: none; opacity: 0.7; }
.j-combos { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 10px; }
.j-combo {
  border: 1.6px solid currentColor;
  border-radius: 9px 14px 8px 12px;
  padding: 8px 12px;
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1.35;
}
.j-combo .name { font-family: var(--hand); font-size: 24px; font-weight: 700; display: block; }
.j-combo .recipe { opacity: 0.75; }
.j-combo .unlock { display: block; margin-top: 3px; color: var(--accent-red); }
.j-combo.unknown { border-style: dashed; opacity: 0.62; }
.j-combo.unknown .name { letter-spacing: 4px; opacity: 0.5; }
.j-combo .clue { display: block; margin-top: 4px; color: var(--accent-red); }
.j-tip { font-family: var(--mono); font-size: 13px; opacity: 0.7; margin: 12px 0 0; }
#journal footer { display: flex; flex-wrap: wrap; gap: 8px 18px; margin-top: 22px; align-items: center; }
.text-btn {
  background: none; border: none; color: inherit; cursor: pointer; padding: 2px 0;
  font-family: var(--hand); font-size: 22px; font-weight: 700;
  text-decoration: underline; text-decoration-thickness: 1.5px; text-underline-offset: 4px;
}
.text-btn:hover { color: var(--accent-red); }
.text-btn.quiet { font-size: 17px; font-weight: 400; opacity: 0.55; margin-left: auto; }

.noscript { font-family: var(--hand); font-size: 28px; text-align: center; margin-top: 40vh; }

@media (max-width: 560px) {
  .journal-sheet { padding: 20px 18px 16px 34px; }
  .journal-sheet::before { left: 24px; }
  #journal header h1 { font-size: 36px; }
}
@media (prefers-reduced-motion: reduce) {
  .toast { animation-duration: 6s; }
  #journal-btn.bump { animation: none; }
}

/* ---------- Main menu / sketchbook ---------- */
#menu {
  position: fixed; inset: 0; z-index: 30;
  display: flex; align-items: center; justify-content: center;
  background: rgba(40, 34, 26, 0.32);
  padding: 16px;
  transition: opacity 0.25s ease;
  -webkit-user-select: none; user-select: none;
}
#menu.hidden { opacity: 0; pointer-events: none; }
body.menu-open #corner, body.menu-open #toasts { opacity: 0; pointer-events: none; }
.m-sheet {
  position: relative;
  background-color: var(--paper);
  background-image: var(--grain);
  background-size: 256px 256px;
  color: var(--ink);
  box-shadow: 0 22px 60px rgba(30, 20, 10, 0.32);
  max-height: min(92vh, 900px);
  overflow-y: auto;
}
.m-sheet.hidden { display: none; }
body.chalk .m-sheet { background-color: #2c3633; color: var(--chalk); border-color: var(--chalk); }
.m-cover {
  width: min(460px, 100%);
  padding: 34px 34px 26px 64px;
  text-align: center;
  transform: rotate(-0.6deg);
}
.m-rings { /* the spiral binding */
  position: absolute; left: 16px; top: 18px; bottom: 18px; width: 22px;
  background: radial-gradient(circle at 11px 11px, transparent 5px, currentColor 5.5px, currentColor 7px, transparent 7.5px) 0 0 / 22px 34px repeat-y;
  opacity: 0.55; filter: url(#wobble);
}
.m-title { font-family: var(--hand); font-size: 64px; line-height: 0.92; margin: 4px 0 6px; transform: rotate(-2deg); }
.m-tag { font-family: var(--mono); font-size: 13px; opacity: 0.7; margin: 0 0 22px; }
.m-buttons { display: flex; flex-direction: column; gap: 12px; align-items: center; }
.m-big {
  font-family: var(--hand); font-size: 32px; font-weight: 700; color: inherit; background: none; cursor: pointer;
  min-width: 240px; padding: 4px 20px 2px;
  border: 2.4px solid currentColor; border-radius: 14px 8px 12px 9px; filter: url(#wobble);
  transition: transform 0.15s ease, background 0.15s ease;
}
.m-big:hover, .m-big:focus-visible { transform: rotate(-1.5deg) scale(1.04); background: rgba(233, 162, 59, 0.18); outline: none; }
#m-play { font-size: 40px; min-width: 260px; }
.m-code { font-family: var(--mono); font-size: 13px; margin: 20px 0 4px; line-height: 1.5; }
.m-code b { font-family: var(--hand); font-size: 24px; letter-spacing: 2px; color: var(--accent-red); }
.m-code-hint { opacity: 0.6; }
.m-ad {
  margin: 18px auto 10px; width: 100%; min-height: 90px;
  display: flex; align-items: center; justify-content: center;
  border: 1.8px dashed currentColor; border-radius: 8px; opacity: 0.6;
  font-family: var(--hand); font-size: 22px;
}
.m-coffee {
  font-family: var(--hand); font-size: 22px; font-weight: 700; cursor: pointer; color: inherit;
  background: rgba(233, 162, 59, 0.25); border: 2px solid currentColor; border-radius: 20px 14px 18px 12px; padding: 2px 16px;
}
.m-coffee:hover { transform: rotate(1.5deg); }
.m-coffee-note { display: block; height: 18px; font-family: var(--mono); font-size: 12px; opacity: 0; transition: opacity 0.3s; }
.m-coffee-note.show { opacity: 0.75; }
.m-wide { width: min(980px, 100%); padding: 26px 32px 22px; transform: rotate(-0.25deg); }
.m-head { display: flex; align-items: baseline; justify-content: space-between; flex-wrap: wrap; gap: 8px 18px; }
.m-head h2 { font-family: var(--hand); font-size: 44px; margin: 0; }
.m-head-btns { display: flex; gap: 16px; flex-wrap: wrap; }
.m-note { font-family: var(--mono); font-size: 13px; opacity: 0.7; }
.m-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 16px; margin: 14px 0; min-height: 120px; }
.m-card { margin: 0; border: 1.8px solid currentColor; border-radius: 10px 6px 11px 7px; padding: 8px; background: rgba(255, 255, 255, 0.25); }
.m-card:nth-child(odd) { transform: rotate(-0.8deg); }
.m-card:nth-child(even) { transform: rotate(0.7deg); }
.m-thumb { display: block; width: 100%; aspect-ratio: 16 / 10; padding: 0; border: 1px solid rgba(0, 0, 0, 0.15); background: var(--paper); cursor: pointer; overflow: hidden; }
.m-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.m-thumb span, .m-page-art span { font-family: var(--mono); font-size: 12px; opacity: 0.5; }
.m-card figcaption { display: flex; flex-direction: column; gap: 2px; margin-top: 6px; }
.m-name { font-family: var(--hand); font-size: 22px; font-weight: 700; line-height: 1; }
.m-date { font-family: var(--mono); font-size: 11px; opacity: 0.6; }
.m-acts { display: flex; gap: 10px; margin-top: 4px; }
.m-acts button { background: none; border: none; color: inherit; cursor: pointer; font-family: var(--mono); font-size: 12px; text-decoration: underline; padding: 0; }
.m-acts .danger:hover { color: var(--accent-red); }
.m-codebar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; border-top: 1.5px dashed currentColor; padding-top: 12px; margin-top: 8px; font-family: var(--mono); font-size: 13px; }
#m-code-input {
  font-family: var(--hand); font-size: 24px; width: 120px; letter-spacing: 3px; text-align: center;
  background: transparent; color: inherit; border: none; border-bottom: 2px solid currentColor; outline: none;
  -webkit-user-select: text; user-select: text;
}
.m-flipstage { display: flex; align-items: center; justify-content: center; gap: 12px; margin-top: 10px; perspective: 1600px; }
.m-flipbook { flex: 1; max-width: 820px; position: relative; }
.m-page {
  background: #fbf8f1; color: var(--ink); border: 2px solid var(--ink); border-radius: 4px 10px 10px 4px;
  box-shadow: inset 18px 0 22px -16px rgba(0, 0, 0, 0.25), 0 10px 30px rgba(0, 0, 0, 0.18);
  padding: 14px 16px 12px 26px; transform-origin: left center;
}
.m-page.turn-next { animation: turnNext 0.55s ease; }
.m-page.turn-prev { animation: turnPrev 0.55s ease; }
@keyframes turnNext { 0% { transform: rotateY(-70deg); opacity: 0.2; } 100% { transform: rotateY(0); opacity: 1; } }
@keyframes turnPrev { 0% { transform: rotateY(70deg); opacity: 0.2; } 100% { transform: rotateY(0); opacity: 1; } }
.m-page-art { aspect-ratio: 16 / 10; display: flex; align-items: center; justify-content: center; border: 1px solid rgba(0, 0, 0, 0.15); overflow: hidden; background: var(--paper); }
.m-page-art img { width: 100%; height: 100%; object-fit: cover; }
.m-page-foot { display: flex; align-items: baseline; gap: 14px; flex-wrap: wrap; margin-top: 8px; }
.m-arrow {
  font-family: var(--hand); font-size: 64px; line-height: 1; background: none; border: none; color: inherit; cursor: pointer; padding: 0 6px;
}
.m-arrow:disabled { opacity: 0.2; cursor: default; }
.m-loading { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; background: rgba(246, 242, 233, 0.6); font-family: var(--hand); font-size: 30px; z-index: 31; }
.m-loading.hidden { display: none; }
.sketch-btn.small svg { width: 24px; height: 24px; margin-top: 8px; }
@media (max-width: 560px) {
  .m-cover { padding: 26px 18px 20px 46px; }
  .m-rings { left: 10px; }
  .m-title { font-size: 50px; }
  .m-big { min-width: 200px; font-size: 28px; }
  .m-wide { padding: 20px 16px; }
}

/* ---------- Boot note (shown until the game runs; explains failures instead of a blank page) ---------- */
.boot {
  position: fixed; left: 50%; top: 42%; transform: translate(-50%, -50%) rotate(-1deg); z-index: 40;
  font-family: var(--hand); font-size: 30px; color: var(--ink); opacity: 0.65; text-align: center; pointer-events: none;
}
.boot.failed { opacity: 1; font-size: 24px; max-width: min(640px, 90vw); pointer-events: auto; -webkit-user-select: text; user-select: text; }
.boot code { display: block; margin: 8px 0; font-family: var(--mono); font-size: 13px; color: var(--accent-red); word-break: break-word; }
