/* ============================================================================
   The sheet — a page of Notamine paper: ruled lines, a margin line, flags, inks, glowing caret.
   Used live (hero editor), scroll-driven (beacon scene) and static (everywhere else).
   ========================================================================== */

/* ---------------------------------------------------------------- flow tiers (colour appears only when earned) */
[data-tier='0'] { --tier-color: var(--t0); --aura-s: 0px; --flow: 0; }
[data-tier='1'] { --tier-color: var(--t1); --aura-s: 22px; --flow: 0.25; }
[data-tier='2'] { --tier-color: var(--t2); --aura-s: 40px; --flow: 0.5; }
[data-tier='3'] { --tier-color: var(--t3); --aura-s: 60px; --flow: 0.8; }
[data-tier='4'] { --tier-color: var(--t4); --aura-s: 84px; --flow: 1; }

/* ---------------------------------------------------------------- the sheet */
.sheet {
  --lh: 34px;
  --pad-l: 78px;
  position: relative;
  background: var(--paper-bg);
  color: var(--paper-text);
  border-radius: 10px;
  box-shadow: 0 0 0 1px var(--paper-edge), var(--shadow);
  transition: background-color 0.5s var(--ease), box-shadow 0.8s var(--ease), color 0.4s;
}
[data-paper='kraft'] .sheet { background-image: repeating-linear-gradient(97deg, rgba(0, 0, 0, 0.025) 0 2px, transparent 2px 5px); }
[data-paper='chalk'] .sheet { background-image: radial-gradient(ellipse at 30% 10%, rgba(255, 255, 255, 0.07), transparent 55%); }

.sheet-hero { box-shadow: 0 0 0 1px var(--paper-edge), var(--shadow), 0 0 var(--aura-s) color-mix(in srgb, var(--tier-color) 50%, transparent); }
.sheet-hero[data-tier='4'] { animation: aura-pulse 1.4s ease-in-out infinite alternate; }
@keyframes aura-pulse { to { box-shadow: 0 0 0 1px var(--paper-edge), var(--shadow), 0 0 calc(var(--aura-s) * 1.35) color-mix(in srgb, var(--tier-color) 70%, transparent); } }

.sheet-bar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 48px; padding: 8px 12px 8px 18px;
  border-bottom: 1px solid var(--paper-edge); color: var(--paper-dim); font-size: 0.9rem;
}
.sheet-doc { display: inline-flex; align-items: center; gap: 9px; font-weight: 600; color: var(--paper-text); min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sheet-doc .g { color: var(--paper-faint); }
.sheet-doc em { font-style: normal; font-weight: 500; color: var(--paper-faint); }
.sheet-doc em::before { content: '·'; margin-right: 9px; }
.sheet-tools { display: inline-flex; gap: 6px; }
.sheet-tools .icon-btn { color: var(--paper-dim); border-color: var(--paper-edge); }
.sheet-tools .icon-btn:hover { background: var(--code-bg); color: var(--paper-text); }

.page { position: relative; }
.page::before { /* the margin line */
  content: ''; position: absolute; left: 62px; top: 0; bottom: 0; width: 1px; background: var(--paper-margin); transition: background-color 0.8s;
}
.sheet-hero[data-tier='1'] .page::before, .sheet-hero[data-tier='2'] .page::before, .sheet-hero[data-tier='3'] .page::before, .sheet-hero[data-tier='4'] .page::before { background: color-mix(in srgb, var(--tier-color) 55%, var(--paper-margin)); }

.editor {
  font-family: var(--font-serif);
  font-size: 1.24rem;
  font-weight: 400;
  line-height: var(--lh);
  padding: 8px 26px 14px var(--pad-l);
  min-height: calc(var(--lh) * 9 + 22px);
  outline: none;
  caret-color: transparent;
  color: var(--paper-text);
  background-image: linear-gradient(to bottom, transparent calc(var(--lh) - 1px), var(--paper-rule) calc(var(--lh) - 1px));
  background-size: 100% var(--lh);
  background-position: 0 8px;
  overflow-wrap: anywhere;
  cursor: text;
  -webkit-tap-highlight-color: transparent;
}
.editor::selection, .editor *::selection { background: color-mix(in srgb, var(--accent) 30%, transparent); }
.editor p { margin: 0; min-height: var(--lh); position: relative; }
.page-static .editor { cursor: default; min-height: 0; }

/* ---------------------------------------------------------------- beacons = a page flag in the margin + an ink on the text */
[data-kind] { --bc: var(--b-exam); }
[data-kind='exam'] { --bc: var(--b-exam); }
[data-kind='define'] { --bc: var(--b-define); }
[data-kind='question'] { --bc: var(--b-question); }
[data-kind='example'] { --bc: var(--b-example); }
[data-kind='todo'] { --bc: var(--b-todo); }

.editor p[data-kind]::before, .ln[data-kind]::before {
  content: ''; position: absolute; width: 44px; height: 36px; left: calc(var(--pad-l) * -1 + 10px); top: calc((var(--lh) - 36px) / 2 - 1px);
  background: no-repeat center / contain; pointer-events: none; transform-origin: 100% 50%;
  animation: flag-in 0.6s var(--spring) both; transition: transform 0.3s var(--spring);
}
.editor p[data-kind='exam']::before, .ln[data-kind='exam']::before { background-image: var(--flag-exam); }
.editor p[data-kind='define']::before, .ln[data-kind='define']::before { background-image: var(--flag-define); }
.editor p[data-kind='question']::before, .ln[data-kind='question']::before { background-image: var(--flag-question); }
.editor p[data-kind='example']::before, .ln[data-kind='example']::before { background-image: var(--flag-example); }
.editor p[data-kind='todo']::before, .ln[data-kind='todo']::before { background-image: var(--flag-todo); }
.editor p[data-kind]:hover::before { transform: translateX(4px) rotate(-3deg); }
@keyframes flag-in {
  from { transform: translateX(-30px) rotate(-26deg) scale(0.3); opacity: 0; }
  60% { opacity: 1; }
}
@keyframes flag-flash { 0%, 35% { filter: drop-shadow(0 0 7px var(--bc)); } }

.ink { -webkit-box-decoration-break: clone; box-decoration-break: clone; }
[data-kind='exam'] .ink {
  background: linear-gradient(to bottom, transparent 18%, color-mix(in srgb, var(--bc) var(--marker-a), transparent) 18% 90%, transparent 90%);
  border-radius: 0.45em 0.15em 0.5em 0.2em / 0.5em 0.2em 0.45em 0.2em;
  padding: 0 0.28em; margin: 0 -0.28em; font-weight: 600;
}
[data-kind='define'] .term {
  font-weight: 700; text-decoration: underline; text-decoration-color: var(--bc); text-decoration-thickness: 0.14em; text-underline-offset: 0.2em; text-decoration-skip-ink: none;
}
[data-kind='question'] .ink { text-decoration: underline wavy var(--bc); text-decoration-thickness: 0.07em; text-underline-offset: 0.3em; text-decoration-skip-ink: none; }
[data-kind='example'] .ink { font-style: italic; text-decoration: underline dotted var(--bc); text-decoration-thickness: 0.1em; text-underline-offset: 0.3em; text-decoration-skip-ink: none; }
[data-kind='todo'] .ink { text-decoration: underline var(--bc); text-decoration-thickness: 0.08em; text-underline-offset: 0.28em; font-weight: 560; }
.flash::before { animation: flag-in 0.6s var(--spring) both, flag-flash 1.4s ease-out !important; }

/* ---------------------------------------------------------------- the glowing caret, pops and sparks */
.caret {
  position: absolute; left: 0; top: 0; width: 2px; height: 26px; pointer-events: none; z-index: 3; opacity: 0;
  transform: translate3d(var(--cx, 0px), var(--cy, 0px), 0); transition: transform 0.085s var(--ease-out), opacity 0.2s;
  --cc: var(--accent);
}
.caret.on { opacity: 1; }
[data-tier='1'] .caret, [data-tier='2'] .caret, [data-tier='3'] .caret, [data-tier='4'] .caret { --cc: var(--tier-color); }
.caret-core { position: absolute; inset: 0; border-radius: 2px; background: var(--cc); box-shadow: 0 0 calc(4px + var(--flow) * 14px) var(--cc); animation: caret-blink 1.1s steps(1) infinite; }
.caret-glow { position: absolute; left: -9px; right: -9px; top: -6px; bottom: -6px; border-radius: 50%; background: radial-gradient(closest-side, var(--cc), transparent); opacity: calc(0.18 + var(--flow) * 0.55); filter: blur(8px); }
.caret.typing .caret-core { animation: none; }
@keyframes caret-blink { 50% { opacity: 0; } }

.pops { position: absolute; inset: 0; pointer-events: none; z-index: 4; overflow: visible; }
.pop {
  position: absolute; left: 0; top: 0; font-family: var(--font-serif); font-size: 1.24rem; font-weight: 700; line-height: 1;
  color: var(--tier-color, var(--accent)); text-shadow: 0 0 10px color-mix(in srgb, var(--tier-color, var(--accent)) 60%, transparent);
  animation: pop 0.6s var(--ease-out) forwards;
}
@keyframes pop {
  from { transform: translate(var(--x), var(--y)) scale(1.05); opacity: 0.95; }
  to { transform: translate(calc(var(--x) + var(--dx, 0px)), calc(var(--y) - 30px)) scale(1.9) rotate(var(--r, 0deg)); opacity: 0; }
}
.fx { position: absolute; left: -40px; top: -40px; width: calc(100% + 80px); height: calc(100% + 80px); pointer-events: none; z-index: 5; }

/* ---------------------------------------------------------------- the HUD under the hero sheet: Nib + the Flow meter */
.stage { position: relative; padding-top: 104px; }
.stage .nib-slot { position: absolute; right: clamp(24px, 6%, 56px); top: 0; width: 124px; z-index: 0; transition: transform 0.5s var(--spring); pointer-events: none; }
.stage .sheet { z-index: 1; }
.hud { position: relative; z-index: 1; }

.meter {
  --c: 0%;
  display: grid; grid-template-columns: auto 1fr; grid-template-areas: 'top bar' 'hint hint'; gap: 6px 18px; align-items: center; margin-top: 26px; padding-inline: 4px;
}
.meter-top { grid-area: top; display: flex; align-items: baseline; gap: 10px; min-width: 128px; }
.meter-tier { font-family: var(--font-display); font-weight: 750; font-size: 1.5rem; letter-spacing: -0.03em; color: var(--tier-color); transition: color 0.4s; line-height: 1; }
.meter-mult { font-family: var(--font-mono); font-size: 0.84rem; font-weight: 600; color: var(--text-faint); }
.meter-bar { grid-area: bar; position: relative; height: 12px; border-radius: 99px; background: var(--code-bg); box-shadow: inset 0 0 0 1px var(--border); overflow: visible; }
.meter-fill {
  position: absolute; inset: 0; border-radius: 99px;
  background: linear-gradient(90deg, var(--t1) 0%, var(--t1) 12%, var(--t2) 38%, var(--t3) 68%, var(--t4) 96%);
  clip-path: inset(0 calc(100% - var(--c)) 0 0 round 99px); transition: clip-path 0.28s linear;
}
[data-tier='2'] .meter-fill, [data-tier='3'] .meter-fill, [data-tier='4'] .meter-fill { filter: drop-shadow(0 0 7px color-mix(in srgb, var(--tier-color) 80%, transparent)); }
.meter-bar b { position: absolute; top: -3px; bottom: -3px; width: 2px; margin-left: -1px; border-radius: 2px; background: var(--bg); opacity: 0.9; }
.meter-hint { grid-area: hint; font-size: 0.92rem; color: var(--text-faint); min-height: 1.5em; transition: color 0.3s; }

.hand-note.is-gone { opacity: 0; transform: rotate(-4deg) translateY(-6px); }
.stage .hand-note { position: absolute; z-index: 2; top: 22px; right: clamp(150px, calc(6% + 150px), 230px); }

/* ---------------------------------------------------------------- paper chits (the app's toasts) */
.chits { position: fixed; z-index: 120; left: 20px; bottom: 20px; display: flex; flex-direction: column-reverse; gap: 12px; pointer-events: none; }
.chit {
  position: relative; display: flex; align-items: center; gap: 14px; padding: 12px 22px 12px 30px; min-width: 210px;
  color: #2b2118; background: #fff6df; border-radius: 4px; transform-origin: 0 50%;
  box-shadow: 0 14px 30px -10px rgba(40, 24, 8, 0.5), 0 2px 4px rgba(40, 24, 8, 0.18);
  -webkit-mask: radial-gradient(circle 4.5px at 0 50%, #0000 97%, #000) 0 0 / 14px 14px repeat-y, linear-gradient(#000, #000) 12px 0 / calc(100% - 12px) 100% no-repeat;
  mask: radial-gradient(circle 4.5px at 0 50%, #0000 97%, #000) 0 0 / 14px 14px repeat-y, linear-gradient(#000, #000) 12px 0 / calc(100% - 12px) 100% no-repeat;
  animation: chit-in 0.7s var(--spring) both; --rot: -1.6deg;
}
.chit.out { animation: chit-out 0.5s var(--ease) forwards; }
@keyframes chit-in { from { transform: translateX(-120%) rotate(-8deg); opacity: 0; } to { transform: rotate(var(--rot)); opacity: 1; } }
@keyframes chit-out { to { transform: translateX(-120%) rotate(-6deg); opacity: 0; } }
.chit-medal {
  width: 38px; height: 38px; border-radius: 50%; display: grid; place-items: center; flex: none; color: #3b2605;
  background: radial-gradient(circle at 32% 26%, #fff3b0, #f6b93b 52%, #b97a0e);
  box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.4), inset 0 -2px 3px rgba(120, 70, 0, 0.35), 0 1px 3px rgba(0, 0, 0, 0.3);
}
.chit-medal .g { width: 20px; height: 20px; }
.chit-t { font-family: var(--font-hand); font-weight: 700; font-size: 1.7rem; line-height: 0.95; }
.chit-s { font-family: var(--font-mono); font-size: 0.78rem; color: #7a6a50; }

/* ---------------------------------------------------------------- Nib */
.nib-host { display: block; line-height: 0; }
.nib { overflow: visible; display: block; filter: drop-shadow(0 10px 16px rgba(20, 20, 90, 0.3)); max-width: none; }
.nib-body { transform-origin: 65px 143px; animation: nib-bob 3.6s ease-in-out infinite; }
.nib-eyes { transform-origin: 65px 92px; animation: nib-blink 5.4s infinite; }
.nib-pen { transform-origin: 65px 38px; animation: nib-pen 3.6s ease-in-out infinite; }
.mood-happy .nib-body { animation: nib-happy 1.6s ease-in-out infinite; }
.mood-hyper .nib-body { animation: nib-hyper 0.5s ease-in-out infinite; }
.mood-cheer .nib-body { animation: nib-cheer 0.9s var(--spring) infinite; }
.mood-sleepy .nib-body { animation: nib-sleep 5s ease-in-out infinite; }
.mood-sad .nib-body, .mood-away .nib-body { animation: nib-sad 4s ease-in-out infinite; }
.mood-listening .nib-body { animation: nib-listen 2.8s ease-in-out infinite; }
.nib-zzz { animation: nib-zzz 2.4s ease-in-out infinite; }
.nib-waves { animation: nib-wave 1.4s ease-in-out infinite; }
.nib-halo { animation: nib-glow 0.9s ease-in-out infinite alternate; }
.nib-halo-ring { animation: nib-ring 2.4s ease-in-out infinite; }
.nib-pupils { transition: transform 0.12s ease-out; }
@keyframes nib-bob { 50% { transform: scale(1.02, 0.975); } }
@keyframes nib-pen { 50% { transform: rotate(4deg); } }
@keyframes nib-blink { 0%, 93%, 100% { transform: scaleY(1); } 96% { transform: scaleY(0.08); } }
@keyframes nib-happy { 0%, 100% { transform: translateY(0) scale(1, 1); } 50% { transform: translateY(-6px) scale(0.98, 1.03); } }
@keyframes nib-hyper { 0%, 100% { transform: translateY(0) rotate(-2deg) scale(1.02, 0.98); } 50% { transform: translateY(-9px) rotate(2deg) scale(0.98, 1.03); } }
@keyframes nib-cheer { 0%, 100% { transform: translateY(0) scale(1.04, 0.95); } 50% { transform: translateY(-16px) scale(0.95, 1.06); } }
@keyframes nib-sleep { 50% { transform: scale(1.03, 0.96) rotate(-2deg); } }
@keyframes nib-sad { 50% { transform: scale(1, 0.97) translateY(2px); } }
@keyframes nib-listen { 0%, 100% { transform: rotate(-3deg); } 50% { transform: rotate(3deg); } }
@keyframes nib-zzz { 0% { opacity: 0; transform: translate(-4px, 6px); } 40% { opacity: 1; } 100% { opacity: 0; transform: translate(6px, -10px); } }
@keyframes nib-wave { 0%, 100% { opacity: 0.2; } 50% { opacity: 1; } }
@keyframes nib-glow { to { opacity: 0.9; } }
@keyframes nib-ring { 50% { transform: translateY(-3px); } }
.skin-ghost .nib-drop, .skin-ghost { filter: none; }
.skin-shadow { filter: drop-shadow(0 0 8px rgba(123, 240, 255, 0.35)); }
.rm .nib-body, .rm .nib-eyes, .rm .nib-pen { animation: none !important; }

/* ---------------------------------------------------------------- static / scene lines */
.ln { position: relative; margin: 0; min-height: var(--lh); }
.ln .trig {
  display: inline-block; vertical-align: 0.06em; overflow: hidden; max-width: 6em; white-space: nowrap; margin-right: 0.65em; padding: 0.04em 0.5em 0.1em;
  font-family: var(--font-mono); font-variant-ligatures: none; font-size: 0.74em; font-weight: 600; letter-spacing: 0; line-height: 1.5; border-radius: 6px; color: var(--paper-dim); background: var(--code-bg);
  transition: max-width 0.55s var(--ease), opacity 0.3s, margin 0.55s var(--ease), padding 0.55s var(--ease);
}
.ln.on .trig { max-width: 0; opacity: 0; margin-right: 0; padding-inline: 0; }
.no-js .ln .trig, .rm .ln .trig { display: none; }
.sheet-mini { --pad-l: 70px; }
.sheet-mini .editor { font-size: 1.08rem; --lh: 30px; padding-right: 18px; }
.sheet-mini .page::before { left: 54px; }
.sheet-mini .ln[data-kind]::before { left: calc(var(--pad-l) * -1 + 6px); transform: scale(0.88); }

@media (max-width: 560px) {
  .sheet { --pad-l: 64px; --lh: 31px; }
  .page::before { left: 50px; }
  .editor { font-size: 1.1rem; padding-right: 16px; }
  .editor p[data-kind]::before, .ln[data-kind]::before { left: calc(var(--pad-l) * -1 + 4px); transform: scale(0.88); }
  .sheet-bar { padding-left: 14px; }
}

/* ---------------------------------------------------------------- live editor behaviour */
.js .sheet-hero .editor { white-space: pre-wrap; max-height: calc(var(--lh) * 10 + 22px); overflow-y: auto; scrollbar-width: none; background-attachment: local; }
.sheet-hero .editor::-webkit-scrollbar { display: none; }
.js:not(.rm) .stage:not(.ready) .editor { visibility: hidden; }
.js.safe .stage .editor { visibility: visible !important; }
.js .bench-input, .js .editor { -webkit-user-select: text; user-select: text; }
.stage .nib-slot { pointer-events: auto; cursor: pointer; }
.stage .nib-slot:hover { transform: translateY(-4px); }
.stage .nib-slot:active { transform: translateY(0) scale(0.97); }

/* ---------------------------------------------------------------- the three Pro coats */
.skin-holo .nib-drop { animation: nib-holo 7s linear infinite; }
.skin-lava .nib-cracks { animation: nib-lava 1.8s ease-in-out infinite alternate; filter: drop-shadow(0 0 3px #ff9a3d); }
.skin-stars .nib-stars { animation: nib-twinkle 3s ease-in-out infinite alternate; }
@keyframes nib-holo { to { filter: hue-rotate(360deg); } }
@keyframes nib-lava { from { opacity: 0.55; } to { opacity: 1; } }
@keyframes nib-twinkle { from { opacity: 0.7; } to { opacity: 1; } }
.rm .skin-holo .nib-drop, .rm .skin-lava .nib-cracks, .rm .skin-stars .nib-stars { animation: none !important; }
