/*
 * spell.css — a Spw expression shown as a spell (spw-spell.mjs).
 *
 * Every sigil carries its role's ink, leaned toward the page's own ink so it reads on any cover
 * (a light cover darkens the hue, a dark one lightens it). An atom lit by hover or focus glows in
 * its role; a bracket lit lights its pair. On first sight the spell is written in light, glyph by
 * glyph (--i), and its sigils flare once. Reduced motion: written at once, still lit on hover.
 */

.spell {
  /* The twelve roles, each a hue; type leans it toward the ink. The hues are named on the spell itself
   * so its spectrum (the roles in the order they are cast, spw-spell.mjs --spectrum) can use them. */
  --hue-probe: #2f6688;
  --hue-potential: #2c8b86;
  --hue-perspective: #7a4f8e;
  --hue-confluence: #b0602a;
  --hue-value: #c4922e;
  --hue-integration: #8a4fb0;
  --hue-action: #c0392b;
  --hue-annotation: #9a7a20;
  --hue-ground: #5d6a73;
  --hue-config: #2b5aa6;
  --hue-measure: #3d7a3a;
  --hue-substrate: #7a5a44;

  --spell-ink: var(--hub-ink, var(--color-text-main, #1d1d22));
  --spell-muted: var(--hub-muted, var(--color-text-alt, #5b6472));
  --spell-paper: var(--hub-panel, var(--color-background-panel, #fbf7ef));
  --spell-gold: var(--hub-gold, var(--color-gold, #c4922e));
  display: block;
  max-width: 100%;
  margin: 0;
  padding: 0.55rem 0.7rem;
  border: 1px solid color-mix(in srgb, var(--spell-gold) 35%, transparent);
  border-radius: 0.5rem;
  position: relative;
  background:
    radial-gradient(120% 140% at 0% 0%, color-mix(in srgb, var(--spell-gold) 9%, transparent), transparent 60%),
    color-mix(in srgb, var(--spell-paper) 88%, var(--spell-ink) 3%);
  box-shadow: inset 0 0 0 1px color-mix(in srgb, #fff 18%, transparent);
}

.spell-line {
  display: block;
  font-family: "JetBrainsMono", var(--font-family-mono, monospace);
  font-size: 0.76rem;
  line-height: 1.7;
  color: var(--spell-ink);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  background: none;
}

/* Each role's hue, as the --role its atoms, pips and gloss read. */
.spell .op-probe { --role: var(--hue-probe); }
.spell .op-potential { --role: var(--hue-potential); }
.spell .op-perspective { --role: var(--hue-perspective); }
.spell .op-confluence { --role: var(--hue-confluence); }
.spell .op-value { --role: var(--hue-value); }
.spell .op-integration { --role: var(--hue-integration); }
.spell .op-action { --role: var(--hue-action); }
.spell .op-annotation { --role: var(--hue-annotation); }
.spell .op-ground { --role: var(--hue-ground); }
.spell .op-config { --role: var(--hue-config); }
.spell .op-measure { --role: var(--hue-measure); }
.spell .op-substrate { --role: var(--hue-substrate); }

.spell-atom {
  border-radius: 0.3rem;
  transition: background-color 160ms ease, box-shadow 160ms ease;
  outline: none;
}

.spell-sigil {
  display: inline-block;
  min-width: 1.05em;
  padding: 0 0.12em;
  border-radius: 0.24em;
  background: color-mix(in srgb, var(--role, var(--spell-gold)) 12%, transparent);
  box-shadow: inset 0 -1px 0 color-mix(in srgb, var(--role, var(--spell-gold)) 30%, transparent);
  text-align: center;
  line-height: 1.35;
  font-weight: 700;
  color: color-mix(in srgb, var(--role, var(--spell-gold)) 72%, var(--spell-ink));
  text-shadow: 0 0 0.45em color-mix(in srgb, var(--role, var(--spell-gold)) 40%, transparent);
}

.spell-label {
  color: color-mix(in srgb, var(--role, var(--spell-ink)) 38%, var(--spell-ink));
}

.spell-atom.is-lit,
.spell-atom:focus-visible {
  background: color-mix(in srgb, var(--role, var(--spell-gold)) 14%, transparent);
  box-shadow: 0 0 0 1px color-mix(in srgb, var(--role, var(--spell-gold)) 45%, transparent), 0 0 0.9em color-mix(in srgb, var(--role, var(--spell-gold)) 22%, transparent);
}

.spell-atom.is-lit .spell-sigil,
.spell-atom:focus-visible .spell-sigil {
  text-shadow: 0 0 0.6em color-mix(in srgb, var(--role, var(--spell-gold)) 70%, transparent), 0 0 0.15em color-mix(in srgb, var(--role, var(--spell-gold)) 60%, transparent);
}

.spell-bracket {
  color: color-mix(in srgb, var(--spell-ink) 55%, transparent);
  transition: color 120ms ease, text-shadow 120ms ease;
}

.spell-bracket.is-paired {
  color: color-mix(in srgb, var(--spell-gold) 70%, var(--spell-ink));
  text-shadow: 0 0 0.5em color-mix(in srgb, var(--spell-gold) 60%, transparent);
}

.spell-string {
  font-family: var(--font-family-body, Georgia, serif);
  font-style: italic;
  font-size: 1.05em;
  color: color-mix(in srgb, var(--spell-ink) 82%, var(--spell-muted));
}

.spell-number {
  color: color-mix(in srgb, var(--spell-gold) 55%, var(--spell-ink));
  font-weight: 600;
}

.spell-path {
  color: color-mix(in srgb, var(--hub-accent, var(--color-accent-main, #246270)) 70%, var(--spell-ink));
  text-decoration: underline dotted;
  text-underline-offset: 3px;
}

.spell-word {
  color: color-mix(in srgb, var(--spell-ink) 85%, transparent);
}

.spell-gloss {
  max-width: none;
  min-height: 1.3em;
  margin: 0.25rem 0 0;
  font-family: var(--font-family-body, Georgia, serif);
  font-size: 0.82rem;
  font-style: italic;
  color: var(--spell-muted);
}

.spell-gloss:empty::before {
  content: "Touch a sigil to hear what it does.";
  opacity: 0.85;
}

/* Written in light: each glyph inks in after the one before, and the sigils flare once. */
.spell.is-uncast :is(.spell-sigil, .spell-label, .spell-bracket, .spell-string, .spell-path, .spell-number, .spell-word, .spell-other) {
  opacity: 0;
}

.spell.is-casting :is(.spell-sigil, .spell-label, .spell-bracket, .spell-string, .spell-path, .spell-number, .spell-word, .spell-other) {
  animation: spell-ink 420ms ease both;
  animation-delay: calc(min(var(--i, 0), 120) * 14ms);
}

.spell.is-casting .spell-sigil {
  animation: spell-ink 420ms ease both, spell-flare 900ms ease-out both;
  animation-delay: calc(min(var(--i, 0), 120) * 14ms), calc(min(var(--i, 0), 120) * 14ms);
}

@keyframes spell-ink {
  from { opacity: 0; filter: blur(3px); transform: translateY(0.15em); }
  to { opacity: 1; filter: none; transform: none; }
}

@keyframes spell-flare {
  0% { text-shadow: 0 0 0 transparent; }
  35% { text-shadow: 0 0 0.9em color-mix(in srgb, var(--role, var(--spell-gold)) 85%, transparent), 0 0 0.2em var(--role, var(--spell-gold)); }
  100% { text-shadow: 0 0 0.45em color-mix(in srgb, var(--role, var(--spell-gold)) 40%, transparent); }
}

@media (prefers-reduced-motion: reduce) {
  .spell.is-uncast :is(.spell-sigil, .spell-label, .spell-bracket, .spell-string, .spell-path, .spell-number, .spell-word, .spell-other) {
    opacity: 1;
  }

  .spell.is-casting :is(.spell-sigil, .spell-label, .spell-bracket, .spell-string, .spell-path, .spell-number, .spell-word, .spell-other) {
    animation: none;
  }
}

/* A compact spell: inline in a card or a list, no box of its own. */
.spell.is-compact {
  padding: 0.3rem 0.45rem;
  border-style: dashed;
  background: color-mix(in srgb, var(--spell-paper) 70%, transparent);
}

.spell.is-compact .spell-line {
  font-size: 0.7rem;
}

.spell.is-compact .spell-gloss {
  font-size: 0.76rem;
}

/* In a chapter's projection: each line a small spell, inline with what it does; where it came
 * from (a link's visit, a carry) marks its edge. */
main.chapter .projection-spell-lines .spell {
  display: inline-block;
  vertical-align: middle;
}

main.chapter .projection-spell-lines .spell-gloss {
  display: none;
}

main.chapter .projection-spell-lines li.is-visit .spell {
  border-color: color-mix(in srgb, var(--color-accent-main, #246270) 55%, transparent);
}

main.chapter .projection-spell-lines li.is-carries .spell {
  border-color: color-mix(in srgb, var(--color-berry, #5b3a6e) 55%, transparent);
}

/* ▶: the spell played (spw-sound.mjs); each atom lights as it sounds. */
.spell-play {
  float: right;
  margin: -0.15rem -0.25rem 0.1rem 0.4rem;
  width: 1.7rem;
  height: 1.7rem;
  border: 1px solid color-mix(in srgb, var(--spell-gold) 55%, transparent);
  border-radius: 999px;
  background: color-mix(in srgb, var(--spell-gold) 10%, var(--spell-paper));
  color: color-mix(in srgb, var(--spell-gold) 55%, var(--spell-ink));
  font-size: 0.7rem;
  line-height: 1;
  cursor: pointer;
}

.spell-play:hover,
.spell-play:focus-visible {
  border-color: var(--spell-gold);
  outline: none;
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--spell-gold) 30%, transparent);
}

.spell-play.is-playing {
  background: color-mix(in srgb, var(--spell-gold) 26%, var(--spell-paper));
}

.spell-atom.is-sounding {
  background: color-mix(in srgb, var(--role, var(--spell-gold)) 26%, transparent);
  box-shadow: 0 0 0 1px color-mix(in srgb, var(--role, var(--spell-gold)) 60%, transparent), 0 0 1.2em color-mix(in srgb, var(--role, var(--spell-gold)) 40%, transparent);
}

.spell-atom.is-sounding .spell-sigil {
  text-shadow: 0 0 0.8em var(--role, var(--spell-gold)), 0 0 0.2em var(--role, var(--spell-gold));
}

/* Made of something: a spell that names a material wears its ground and sheen (materials.css). */
.spell.has-material {
  background:
    var(--mat-sheen, none),
    color-mix(in srgb, var(--mat-ground, var(--spell-paper)) 85%, transparent);
  border-color: color-mix(in srgb, var(--mat-edge, var(--spell-gold)) 55%, transparent);
}

/* ⧉: copy the spell, clean. */
.spell-copy {
  float: right;
  margin: -0.15rem -0.1rem 0.1rem 0.3rem;
  width: 1.7rem;
  height: 1.7rem;
  border: 1px solid color-mix(in srgb, var(--spell-ink) 22%, transparent);
  border-radius: 999px;
  background: color-mix(in srgb, var(--spell-paper) 90%, transparent);
  color: color-mix(in srgb, var(--spell-ink) 75%, transparent);
  font-size: 0.75rem;
  line-height: 1;
  cursor: pointer;
}

.spell-copy:hover,
.spell-copy:focus-visible {
  border-color: var(--spell-gold);
  outline: none;
}

/* A number that is a dial: dotted underline, a ruler cursor; it jumps a little as it turns. */
.spell-number.is-scrub {
  padding: 0 0.12em;
  border-radius: 0.2rem;
  border-bottom: 1px dotted color-mix(in srgb, var(--spell-gold) 75%, var(--spell-ink));
  cursor: ns-resize;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  outline: none;
}

.spell-number.is-scrub:hover,
.spell-number.is-scrub:focus-visible,
.spell-number.is-scrub.is-dragging {
  background: color-mix(in srgb, var(--spell-gold) 22%, transparent);
  box-shadow: 0 0 0 1px color-mix(in srgb, var(--spell-gold) 55%, transparent);
}

.spell-number.is-nudged {
  animation: spell-nudge 220ms ease-out;
}

@keyframes spell-nudge {
  0% { text-shadow: 0 0 0.6em var(--spell-gold); }
  100% { text-shadow: none; }
}

@media (prefers-reduced-motion: reduce) {
  .spell-number.is-nudged { animation: none; }
}

/* Prime, then cast (the canonical tap): a primed atom glows by its charge, which bleeds every 2.8 s;
 * a cast flares once and leaves residue, a faint dashed halo, the way an answered question stays found. */
.spell-atom.is-primed {
  background: color-mix(in srgb, var(--role, var(--spell-gold)) calc(var(--charge, 0.8) * 30%), transparent);
  box-shadow:
    0 0 0 1px color-mix(in srgb, var(--role, var(--spell-gold)) calc(var(--charge, 0.8) * 80%), transparent),
    0 0 calc(var(--charge, 0.8) * 1.4em) color-mix(in srgb, var(--role, var(--spell-gold)) 45%, transparent);
  transition: background-color 600ms ease, box-shadow 600ms ease;
}

.spell-atom.is-spent {
  outline: 1px dashed color-mix(in srgb, var(--role, var(--spell-gold)) 45%, transparent);
  outline-offset: 1px;
}

.spell-atom.is-cast {
  animation: spell-cast 700ms ease-out;
}

@keyframes spell-cast {
  0% { box-shadow: 0 0 0 2px var(--role, var(--spell-gold)), 0 0 1.6em var(--role, var(--spell-gold)); }
  100% { box-shadow: 0 0 0 0 transparent; }
}

@media (prefers-reduced-motion: reduce) {
  .spell-atom.is-cast { animation: none; }
  .spell-atom.is-primed { transition: none; }
}

/* Its spectrum: a hairline along the top in the spell's roles, in the order they are cast, so two
 * spells of the same shape look kin and a spell of a different shape does not. */
.spell[style*='--spectrum']::before {
  content: '';
  position: absolute;
  inset: -1px 0.6rem auto;
  height: 2px;
  border-radius: 0 0 2px 2px;
  background: var(--spectrum);
  opacity: 0.85;
  pointer-events: none;
}

/* Its head: the shape at a glance, then what can be done with it. */
.spell-head {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  min-height: 1.2rem;
  margin: -0.1rem 0 0.3rem;
}

.spell-shape {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 3px;
  margin-right: auto;
}

.spell-pip {
  display: block;
  width: 0.5rem;
  height: 0.5rem;
  border-radius: 50%;
  background: color-mix(in srgb, var(--role, var(--spell-gold)) 78%, var(--spell-ink));
  box-shadow: 0 0 0 1.5px color-mix(in srgb, var(--role, var(--spell-gold)) 22%, transparent);
  transition: transform 140ms ease, box-shadow 140ms ease;
  cursor: default;
}

.spell-pip.is-lit {
  transform: scale(1.35);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--role, var(--spell-gold)) 45%, transparent), 0 0 0.6em color-mix(in srgb, var(--role, var(--spell-gold)) 60%, transparent);
}

.spell-head .spell-play,
.spell-head .spell-copy {
  float: none;
  flex: none;
  margin: 0;
  width: 1.55rem;
  height: 1.55rem;
}

.spell-head .spell-shape + .spell-play,
.spell-head .spell-shape + .spell-copy {
  margin-left: 0.3rem;
}

.spell-head:not(:has(.spell-shape)) {
  justify-content: flex-end;
}

/* Verse: a long spell a line an atom, so its shape reads before its words. */
.spell.is-verse .spell-line {
  line-height: 1.8;
}

/* The gloss speaks in the role of the atom it is about: its sigil as a keycap, its ink as a rule. */
.spell-gloss[data-sigil]:not(:empty) {
  padding-left: 0.5rem;
  border-left: 2px solid color-mix(in srgb, var(--role, var(--spell-gold)) 70%, transparent);
  font-style: normal;
  color: color-mix(in srgb, var(--role, var(--spell-ink)) 30%, var(--spell-ink));
}

.spell-gloss[data-sigil]:not(:empty)::before {
  content: attr(data-sigil);
  display: inline-block;
  min-width: 1.15em;
  margin-right: 0.4em;
  padding: 0 0.15em;
  border-radius: 0.24em;
  background: color-mix(in srgb, var(--role, var(--spell-gold)) 16%, transparent);
  color: color-mix(in srgb, var(--role, var(--spell-gold)) 72%, var(--spell-ink));
  font-family: "JetBrainsMono", var(--font-family-mono, monospace);
  font-weight: 700;
  text-align: center;
}

.spell.is-compact .spell-sigil {
  background: color-mix(in srgb, var(--role, var(--spell-gold)) 8%, transparent);
  box-shadow: none;
}

.spell.is-compact .spell-head {
  margin-bottom: 0.15rem;
}

.spell.is-compact .spell-pip {
  width: 0.4rem;
  height: 0.4rem;
}

@media (prefers-reduced-motion: reduce) {
  .spell-pip { transition: none; }
}

/* Inline in a chapter's projection: no head, the line is the whole of it. */
main.chapter .projection-spell-lines .spell-head {
  display: none;
}

/* Night bindings and lamplight: the spell's ground (and a material's) is the room's own night, so a
 * spell's ink, which turns light, never sits on a light panel. Pages without room tokens keep theirs. */
html:is([data-nook-light="lamp"], [data-binding-tone="dark"]) .spell {
  --spell-paper: var(--room-chip, var(--hub-panel, var(--color-background-panel, #fbf7ef)));
  --mat-paper: var(--room-chip, var(--hub-panel, var(--color-background-panel, #fbf7ef)));
}
