/* Creases: page styles (owner: opus). No external fonts. */
:root {
  --desk: #3b2f26;
  --desk-2: #2a211b;
  --paper: #efe3c8;
  --paper-2: #e3d2ad;
  --ink: #2b2620;
  --sepia: #7a5a3a;
  --seal: #9e2b25;
  --focus: #f2c14e;
  --hand: "Segoe Print", "Bradley Hand", "Comic Sans MS", cursive;
  --serif: Georgia, "Palatino Linotype", "Book Antiqua", serif;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  background: radial-gradient(ellipse at 50% 40%, var(--desk) 0%, var(--desk-2) 100%);
  color: var(--paper);
  font-family: var(--serif);
  font-size: 17px;
  line-height: 1.45;
}
#app { min-height: 100%; display: flex; flex-direction: column; }
.screen { flex: 1; display: flex; flex-direction: column; }

button, .button {
  font: inherit;
  background: var(--paper);
  color: var(--ink);
  border: 1px solid #b59d72;
  border-radius: 4px;
  padding: 0.35em 0.8em;
  cursor: pointer;
  text-decoration: none;
  display: inline-block;
  box-shadow: 0 1px 0 rgba(0,0,0,0.3);
  /* a tap is a tap: no double-tap zoom delay on phones */
  touch-action: manipulation;
}
summary { touch-action: manipulation; }
button:hover:not(:disabled), .button:hover:not([aria-disabled="true"]) { background: #fff6e0; }
/* Save image waits, faded, until its file is ready */
.button[aria-disabled="true"] { opacity: 0.45; cursor: default; pointer-events: none; }
button:disabled { opacity: 0.45; cursor: default; }
button.big, .button.big { font-size: 1.15em; padding: 0.5em 1.3em; }
button.linkish { background: none; border: none; box-shadow: none; padding: 0; color: inherit; text-align: left; text-decoration: underline dotted; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
/* the board is focused on every page start: a quiet dashed frame, not a bright box */
#board:focus-visible { outline: 2px dashed rgba(242,193,78,0.55); outline-offset: 10px; }

/* title */
#screen-title { align-items: center; justify-content: center; text-align: center; padding: 2em 1em; }
.title-card { max-width: 36em; }
#screen-title h1 { font-family: var(--hand); font-weight: normal; font-size: 4em; margin: 0 0 0.25em; color: var(--paper); letter-spacing: 0.04em; position: relative; }
/* a single soft crease under the title */
#screen-title h1::after {
  content: ""; display: block; height: 2px; width: 60%; margin: 0.12em auto 0;
  background: linear-gradient(to right, rgba(239,227,200,0), rgba(239,227,200,0.45) 30%, rgba(239,227,200,0.45) 70%, rgba(239,227,200,0));
  box-shadow: 0 1px 0 rgba(0,0,0,0.35);
}
.sub { font-size: 1.15em; }
.cw, .keys { opacity: 0.75; font-size: 0.9em; }
.cw { margin-top: 1.6em; }
.keys { line-height: 1.9; }
kbd {
  font-family: var(--serif); font-size: 0.9em; display: inline-block; min-width: 1.6em; padding: 0 0.35em; line-height: 1.5;
  border: 1px solid rgba(239,227,200,0.45); border-bottom-width: 2px; border-radius: 4px; text-align: center;
}
.actions { display: flex; gap: 0.7em; justify-content: center; flex-wrap: wrap; margin: 1.2em 0; }
.warn { color: #ffd9a8; }

/* play */
#topbar { display: flex; align-items: center; gap: 1em; flex-wrap: wrap; padding: 0.6em 1em; background: rgba(0,0,0,0.25); }
#topbar .where { flex: 1; min-width: 12em; }
#topbar h2 { margin: 0; font-weight: normal; font-size: 1.3em; display: inline; }
.chapter { font-family: var(--hand); color: #d9c49a; margin-right: 0.6em; }
#fold-count { font-size: 1.05em; padding: 0.15em 0.8em; border: 1px solid rgba(239,227,200,0.4); border-radius: 4px; white-space: nowrap; }
#fold-count .k { opacity: 0.75; }
#fold-count b { font-weight: normal; font-size: 1.25em; padding: 0 0.1em; }
#fold-count b.aim { font-family: var(--hand); color: var(--focus); }
#fold-count.at { border-color: rgba(242,193,78,0.75); }
#fold-count.over { color: #ffcf9a; border-color: rgba(255,207,154,0.6); }
#fold-count.over b.aim { color: inherit; }
.tools { display: flex; gap: 0.4em; flex-wrap: wrap; }
.tools button { font-size: 0.9em; }
/* the teaching line: a slip of paper tucked above the map */
#teach {
  align-self: center; max-width: min(46em, calc(100% - 2em)); margin: 0.7em auto 0; padding: 0.35em 1.1em;
  text-align: center; font-family: var(--hand); font-size: 1.1em; line-height: 1.4;
  color: var(--ink); background: linear-gradient(175deg, #f7edd6, #ead9b4); border-radius: 2px;
  box-shadow: 0 3px 10px rgba(0,0,0,0.4); transform: rotate(-0.4deg);
}

#play-area { flex: 1; display: flex; gap: 1em; padding: 0.8em 1em 0.4em; min-height: 0; }
#desk {
  flex: 1; min-height: 340px; min-width: 0;
  display: flex; align-items: center; justify-content: center;
  perspective: 1400px;
  position: relative;
}
#board { position: relative; transform-style: preserve-3d; touch-action: manipulation; }
#side {
  width: 17em; flex: none; align-self: flex-start; max-height: 100%;
  background: rgba(0,0,0,0.2); border-radius: 6px; padding: 0.7em 1em 0.8em; font-size: 0.95em; overflow: auto;
}
#side h3 { margin: 0.1em 0 0.5em; font-weight: normal; font-family: var(--hand); font-size: 1.15em; }
#goal-progress { margin: -0.3em 0 0.4em; font-size: 0.88em; color: #d9c49a; }
#goal-list { list-style: none; padding: 0; margin: 0 0 0.9em; }
#goal-list li { margin: 0.25em 0; padding: 0.3em 0.5em; border-left: 3px solid rgba(158,43,37,0.75); background: rgba(255,255,255,0.04); border-radius: 0 3px 3px 0; }
#goal-list li.done { border-left-color: rgba(242,193,78,0.7); }
#goal-list li.done button { color: #e9dcbc; text-decoration: none; }
#goal-list li.done button:hover, #goal-list li.done button:focus-visible { color: #fff6e0; text-decoration: underline dotted; }
#goal-list .again { font-size: 0.82em; opacity: 0.7; }
#goal-list li.ruined { color: #ffb4a0; border-left-color: #6b4a8a; }
#hint-text { font-family: var(--hand); color: #fff3d6; background: rgba(255,255,255,0.06); padding: 0.4em 0.6em; border-radius: 4px; }
#rules { margin-top: 0.4em; border-top: 1px solid rgba(239,227,200,0.15); padding-top: 0.5em; }
#rules summary { cursor: pointer; opacity: 0.85; }
#rules ul { padding-left: 1.1em; margin: 0.4em 0; font-size: 0.92em; }
#rules li { margin: 0.2em 0; }
#status { min-height: 1.5em; margin: 0.2em 1em 0.8em; text-align: center; color: #e9dcbc; }

/* panels */
.cell { position: absolute; transform-style: preserve-3d; }
.cell canvas.face, .card canvas.face { width: 100%; height: 100%; display: block; }
.cell .layers {
  position: absolute; right: 3px; bottom: 2px; font-size: 11px; line-height: 1;
  color: var(--ink); background: rgba(239,227,200,0.85); border-radius: 6px; padding: 1px 4px; pointer-events: none;
}
.cell.bleed { animation: bleed 1.2s ease-out; }
@keyframes bleed { 0% { filter: none; } 25% { filter: brightness(0.7) sepia(0.6) hue-rotate(190deg) saturate(2); } 100% { filter: none; } }
.teach-arc { position: absolute; pointer-events: none; z-index: 30; }

.flap { position: absolute; transform-style: preserve-3d; z-index: 50; pointer-events: none; }
.card { position: absolute; transform-style: preserve-3d; }
.card .face { position: absolute; left: 0; top: 0; backface-visibility: hidden; -webkit-backface-visibility: hidden; }
.flap-shade, .flap-gloss {
  position: absolute; inset: 0; opacity: 0; pointer-events: none;
  backface-visibility: hidden; -webkit-backface-visibility: hidden;
}
.flap-shade { transform: translateZ(1px); }
.fold-shadow, .hinge-bend { position: absolute; opacity: 0; pointer-events: none; transform: translateZ(0.5px); }
.fold-shadow { z-index: 45; }
.hinge-bend { z-index: 55; transform: translateZ(2px); }

/* a legal crease: a warm glow with a dashed fold line down its middle, breathing softly */
.crease-cursor { position: absolute; z-index: 40; pointer-events: none; border-radius: 7px; }
.crease-cursor.ok {
  background: rgba(242,193,78,0.35); box-shadow: 0 0 14px 2px rgba(242,193,78,0.75), inset 0 0 0 1px rgba(255,246,224,0.6);
  animation: crease-breathe 1.6s ease-in-out infinite;
}
.crease-cursor.ok.v { background-image: repeating-linear-gradient(to bottom, rgba(122,90,58,0.95) 0 7px, transparent 7px 12px); background-size: 2px 100%; background-position: center; background-repeat: no-repeat; }
.crease-cursor.ok.h { background-image: repeating-linear-gradient(to right, rgba(122,90,58,0.95) 0 7px, transparent 7px 12px); background-size: 100% 2px; background-position: center; background-repeat: no-repeat; }
@keyframes crease-breathe { 0%, 100% { box-shadow: 0 0 10px 1px rgba(242,193,78,0.6), inset 0 0 0 1px rgba(255,246,224,0.5); } 50% { box-shadow: 0 0 18px 4px rgba(242,193,78,0.9), inset 0 0 0 1px rgba(255,246,224,0.8); } }
.crease-cursor.torn { background: repeating-linear-gradient(45deg, rgba(158,43,37,0.6) 0 6px, transparent 6px 12px); box-shadow: 0 0 8px rgba(158,43,37,0.5); }
.crease-cursor.refuse { animation: refuse 0.45s ease; }
@keyframes refuse { 0%,100% { transform: translate(0,0); } 25% { transform: translate(-3px,-2px); } 75% { transform: translate(3px,2px); } }

/* a hint's first move: the crease and the side that folds over it, pulsing softly; never a fold by itself */
.hint-pulse { position: absolute; pointer-events: none; opacity: 0.7; }
.hint-side { z-index: 39; background: rgba(242,193,78,0.12); outline: 2px dashed rgba(242,193,78,0.55); animation: hint-pulse 2.2s ease-in-out 3; }
.hint-line { z-index: 40; border-radius: 6px; background: rgba(242,193,78,0.3); box-shadow: 0 0 12px 2px rgba(242,193,78,0.6); animation: hint-pulse 2.2s ease-in-out 3; }
@keyframes hint-pulse { 0%, 100% { opacity: 0.45; } 50% { opacity: 1; } }
.side-tint { position: absolute; z-index: 41; pointer-events: none; background: rgba(242,193,78,0.22); outline: 2px dashed rgba(242,193,78,0.9); }
.side-btn {
  position: absolute; z-index: 60; padding: 0; font-size: 24px; line-height: 1;
  border-radius: 50%; background: #fff6e0; border: 2px solid var(--sepia); box-shadow: 0 2px 6px rgba(0,0,0,0.45);
}
/* the choice after the tear: the button is see-through so the torn half stays in view;
   only its label is paper-backed like the preview labels, so it reads over a busy map */
.half-btn {
  position: absolute; z-index: 60; background: transparent; border: 2px dashed rgba(242,193,78,0.6);
  padding: 0.5em; display: flex; align-items: center; justify-content: center; box-shadow: none;
}
/* button.half-btn: outranks the global button:hover:not(:disabled) paper fill */
button.half-btn:hover, button.half-btn:focus-visible { background: rgba(250,243,224,0.12); }
.half-label {
  display: inline-block; max-width: 100%; box-sizing: border-box; padding: 0.25em 0.6em; border-radius: 4px;
  text-align: center; overflow-wrap: anywhere; font-family: var(--hand); font-size: 1em; line-height: 1.3;
  color: var(--ink); text-shadow: none; background: rgba(250,243,224,0.92); box-shadow: 0 2px 8px rgba(0,0,0,0.35);
}

/* the last fold: before the tear, each half says what it would keep */
.tear-half { position: absolute; z-index: 35; pointer-events: none; display: flex; align-items: center; justify-content: center; padding: 6px; }
.tear-half.keeps { background: rgba(242,193,78,0.10); }
.tear-half.empty { background: rgba(40,28,18,0.22); }
.tear-half.armed.keeps { background: rgba(242,193,78,0.18); }
.tear-label {
  max-width: 92%; padding: 0.25em 0.6em; border-radius: 2px; text-align: center;
  font-family: var(--hand); font-size: 0.95em; line-height: 1.3; color: var(--ink);
  background: rgba(250,243,224,0.92); box-shadow: 0 2px 8px rgba(0,0,0,0.35);
}
.tear-half.empty .tear-label { color: #5a4636; font-style: italic; }
.tear-confirm {
  position: absolute; z-index: 60; padding: 0; font-family: var(--hand); font-size: 1em;
  background: #fff6e0; color: var(--seal); border: 2px solid var(--seal); border-radius: 20px; box-shadow: 0 2px 8px rgba(0,0,0,0.45);
}
.tear-confirm:hover, .tear-confirm:focus-visible { background: #fbe3d6; }

/* solved: after the player asks, the map opens out flat */
#board.unfolding { animation: unfold-in 0.7s ease-out; }
@keyframes unfold-in { from { opacity: 0.4; transform: scale(0.96); } to { opacity: 1; transform: none; } }

/* the solved card sits in the side panel, so the unfolded map and its new creases stay in full view */
#solved-card {
  background: linear-gradient(175deg, #f7edd6, #ead9b4); color: var(--ink); padding: 0.7em 0.9em 0.5em; margin: 0 0 1em;
  border-radius: 2px; box-shadow: 0 4px 14px rgba(0,0,0,0.45); text-align: center; font-family: var(--hand);
  transform: rotate(-0.5deg); animation: note-in 0.6s ease-out;
}
#solved-text { margin: 0.1em 0 0.2em; line-height: 1.4; }
#solved-card .actions { margin: 0.5em 0 0.2em; gap: 0.5em; font-family: var(--serif); }
#solved-card button { border-color: #8d7a4e; }

/* overlays */
.overlay { position: fixed; inset: 0; background: rgba(20,14,10,0.55); display: flex; align-items: center; justify-content: center; z-index: 100; padding: 1em; }
.note-paper {
  background: linear-gradient(170deg, #f7edd6 0%, #ecdcb8 100%);
  color: var(--ink); max-width: 30em; width: 100%; padding: 1.6em 2em 1.2em;
  border-radius: 3px; box-shadow: 0 10px 40px rgba(0,0,0,0.6);
  font-family: var(--hand); transform: rotate(-0.6deg);
  animation: note-in 0.7s ease-out;
}
@keyframes note-in { from { opacity: 0; transform: translateY(10px) rotate(-1.5deg); } to { opacity: 1; transform: rotate(-0.6deg); } }
.note-paper .route { font-family: var(--serif); font-size: 0.85em; color: var(--sepia); margin: 0 0 0.8em; }
.note-paper .salutation { margin: 0 0 0.5em; }
.note-paper .body { margin: 0.2em 0; line-height: 1.5; text-wrap: pretty; overflow-wrap: break-word; }
.note-paper .line { opacity: 0; animation: line-in 0.9s ease-out forwards; }
@keyframes line-in { to { opacity: 1; } }
.note-paper .signoff { text-align: right; margin: 0.9em 0 1em; }
.note-paper button { font-family: var(--serif); }
.letters-card, .levels-card {
  background: var(--paper); color: var(--ink); border-radius: 6px; padding: 1.2em 1.6em;
  max-width: 40em; width: 100%; max-height: 85vh; overflow: auto; box-shadow: 0 10px 40px rgba(0,0,0,0.6);
}
#letters-list { list-style: none; padding: 0; columns: 2; }
#letters-list li { margin: 0.3em 0; break-inside: avoid; }
#letters-list ul { list-style: none; padding: 0 0 0 0.6em; margin: 0; }
#letters-list li.thread { margin: 0 0 0.9em; }
#letters-list li.thread h3 { font-family: var(--hand); font-weight: normal; color: var(--sepia); font-size: 1em; margin: 0 0 0.2em; }

/* levels */
#screen-levels { align-items: center; justify-content: center; padding: 1.5em 1em; }
.levels-card h2, .levels-card h3 { font-weight: normal; font-family: var(--hand); }
.levels-card h3 { margin: 0.8em 0 0.3em; color: var(--sepia); }
ul.pages { list-style: none; padding: 0; margin: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(11em, 1fr)); gap: 0.5em; }
button.page { width: 100%; text-align: left; display: flex; flex-direction: column; background: #f7edd6; font-family: var(--serif); }
button.page.solved { background: #e9dfc0; border-color: #8d7a4e; }
button.page .ps { font-size: 0.82em; color: var(--sepia); }
/* the way back stays in view at the foot of the card while the list of pages scrolls under it */
.levels-card .back-row {
  position: sticky; bottom: 0; z-index: 2; margin: 1em -1.6em -1.2em; padding: 0.7em 1.6em 0.9em;
  background: var(--paper); border-top: 1px solid rgba(122,90,58,0.25); box-shadow: 0 -8px 12px -10px rgba(40,25,10,0.5);
}

/* ending */
#screen-ending { align-items: center; justify-content: center; padding: 1.5em 1em; text-align: center; }
#screen-ending h2 { font-family: var(--hand); font-weight: normal; }
/* the canvas takes the final sheet's own shape (set from the level); it scales down whole, never cropped */
#keepsake { width: auto; height: auto; max-width: min(90vw, 760px); max-height: 66vh; box-shadow: 0 10px 40px rgba(0,0,0,0.6); border-radius: 2px; }

@media (max-width: 760px) {
  #play-area { flex-direction: column; }
  #side { width: auto; align-self: stretch; }
  #screen-title h1 { font-size: 3em; }
  /* narrow: the Pages card fills the screen, and its Back row keeps to the foot of it */
  #screen-levels { padding: 0.6em 0.5em; }
  .levels-card { padding: 1em 1em; max-height: calc(100vh - 1.2em); }
  .levels-card .back-row { margin: 1em -1em -1em; padding: 0.6em 1em 0.8em; }
}

/* phones and small tablets: the toolbar's buttons and the rules summary are full finger-sized targets. The
   toolbar stays in the page flow and wraps to a row of its own, so it pushes the map down, never over it */
@media (max-width: 820px) {
  #topbar { gap: 0.5em 0.8em; padding: 0.5em 0.6em; }
  #topbar .tools { flex: 1 1 100%; gap: 0.4em; }
  #topbar .tools button { flex: 1 1 auto; min-height: 44px; min-width: 44px; }
  #rules summary { min-height: 44px; padding: 0.6em 0; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; animation-delay: 0ms !important; transition-duration: 0.01ms !important; }
  .note-paper .line { opacity: 1; }
  .crease-cursor.ok { animation: none; }
  .hint-pulse { animation: none; }
}
