/* Wordshader shell. The screen is the canvas plus one centred line of words;
   everything else is hidden until asked for. */

:root {
  --mono: 'JetBrains Mono', ui-monospace, 'SF Mono', Menlo, Consolas, monospace;

  /* Editor type */
  --fs: clamp(18px, 1.1vw + 10px, 32px);
  --lh: 1.62;

  /* Token palette — one colour, one meaning. Red is reserved for "not a word / problem". */
  --ink: #160c21;             /* glyph outline */
  --chip: rgba(34, 10, 58, 0.3);
  --t-shape: #cbb4ff;
  --t-form: #fff0c6;
  --t-material: #dcc2ff;
  --t-motion: #bea6ff;
  --t-fx: #f2b8ff;
  --t-size: #e6dbff;
  --t-bg: #a8e2ff;
  --t-dim: #9ff0e4;
  --t-audio: #9df2bf;
  --t-keyword: #ffe597;
  --t-number: #b6f4d4;
  --t-punct: rgba(255, 248, 235, 0.5);
  --t-pending: rgba(255, 250, 240, 0.9);
  --t-plain: #fff6e6;
  --t-egg: #ffd46b;
  --bad: #ff6b6b;

  /* UI chrome */
  --panel: rgba(15, 10, 22, 0.95);
  --panel-line: rgba(255, 255, 255, 0.08);
  --ui-text: #efe8f7;
  --ui-dim: rgba(239, 232, 247, 0.55);
  --ui-faint: rgba(239, 232, 247, 0.32);
  --accent: #ffe597;
  --ease-out: cubic-bezier(0.22, 1, 0.36, 1);
  --spring: cubic-bezier(0.2, 1.2, 0.3, 1);

  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: #b9b38a;   /* close to the renderer's default gradient, so the first frame doesn't flash */
  color: var(--ui-text);
  font-family: var(--mono);
  -webkit-font-smoothing: antialiased;
}
button, input { font: inherit; color: inherit; }
kbd { font: inherit; }

canvas#view {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  display: block;
  touch-action: none;
  cursor: grab;
}
canvas#view:active { cursor: grabbing; }

/* ---------- Editor ---------- */

.stage {
  position: fixed;
  inset: 0;
  display: flex;
  overflow-y: auto;
  overflow-x: hidden;
  pointer-events: none;
  padding: 72px 0;
  scrollbar-width: none;
  transition: padding 0.42s var(--ease-out);
}
/* An open drawer takes its side of the screen; the spell re-centres in what's left. */
@media (min-width: 720px) {
  body.drawer-dict-open .stage { padding-left: 380px; }
  body.drawer-book-open .stage { padding-right: 380px; }
}
.stage::-webkit-scrollbar { display: none; }

.editor {
  position: relative;
  margin: auto;          /* safe centring: never clips the top when the spell is taller than the screen */
  /* Shrink-wrapped to the text (the textarea is inset:0 over the mirror, so it follows), which
     leaves the canvas around a short spell free for orbit-dragging. */
  width: fit-content;
  min-width: 96px;
  max-width: min(880px, calc(100% - 48px));
  pointer-events: auto;
  transition: opacity 0.25s var(--ease-out);
}

/* The mirror and the textarea MUST share every property that affects layout. */
.hl, .ta {
  margin: 0;
  border: 0;
  padding: 10px 18px;
  font-family: var(--mono);
  font-size: var(--fs);
  font-weight: 800;
  font-style: normal;
  font-variant-ligatures: none;
  font-feature-settings: 'liga' 0, 'calt' 0;
  font-kerning: none;
  line-height: var(--lh);
  letter-spacing: 0;
  word-spacing: 0;
  text-align: center;
  text-indent: 0;
  text-transform: none;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  word-break: normal;
  tab-size: 4;
  -moz-tab-size: 4;
}

.hl {
  position: relative;
  min-height: calc(var(--fs) * var(--lh) + 20px);
  color: var(--t-plain);
  pointer-events: none;
  user-select: none;
  /* A tight dark outline around each glyph (text-shadow never affects layout). */
  text-shadow:
    -2px -2px 0 var(--ink), 0 -2px 0 var(--ink), 2px -2px 0 var(--ink),
    -2px 0 0 var(--ink), 2px 0 0 var(--ink),
    -2px 2px 0 var(--ink), 0 2px 0 var(--ink), 2px 2px 0 var(--ink),
    0 3px 6px rgba(10, 4, 18, 0.45);
}

.ta {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  resize: none;
  overflow: hidden;
  background: transparent;
  color: transparent;
  -webkit-text-fill-color: transparent;
  caret-color: transparent;   /* we draw our own, thicker caret in the mirror */
  outline: none;
  appearance: none;
}
.ta::selection { background: rgba(255, 246, 225, 0.3); color: transparent; }
.ta::placeholder { color: transparent; }
.editor.debug-align .ta { color: rgba(255, 0, 80, 0.75); -webkit-text-fill-color: rgba(255, 0, 80, 0.75); }

/* Tokens */
.tk { border-radius: 4px; }
.chip {
  background: var(--chip);
  box-shadow: 4px 0 0 var(--chip), -4px 0 0 var(--chip);
}
.c-shape    { color: var(--t-shape); }
.c-form     { color: var(--t-form); }
.c-material { color: var(--t-material); }
.c-motion   { color: var(--t-motion); }
.c-fx       { color: var(--t-fx); }
.c-size     { color: var(--t-size); }
.c-bg       { color: var(--t-bg); }
.c-dim      { color: var(--t-dim); }
.c-audio    { color: var(--t-audio); }
.c-color    { color: var(--t-form); background: var(--swatch, var(--chip)); box-shadow: 4px 0 0 var(--swatch, var(--chip)), -4px 0 0 var(--swatch, var(--chip)); }
.c-egg {
  color: var(--t-egg);
  background: linear-gradient(90deg, rgba(255, 90, 200, 0.5), rgba(255, 210, 80, 0.5), rgba(80, 220, 255, 0.5), rgba(255, 90, 200, 0.5));
  background-size: 300% 100%;
  box-shadow: none;
  animation: egg 2.4s linear infinite;
}
@keyframes egg { to { background-position: 300% 0; } }

.k-keyword { color: var(--t-keyword); background: rgba(255, 222, 130, 0.2); box-shadow: 4px 0 0 rgba(255, 222, 130, 0.2), -4px 0 0 rgba(255, 222, 130, 0.2); }
.k-number  { color: var(--t-number); opacity: 0.85; }
.k-punct   { color: var(--t-punct); }
.k-unknown { color: var(--bad); }
.k-pending { color: var(--t-pending); }
.also-audio {
  text-decoration: underline 2px var(--t-audio);
  text-underline-offset: 0.3em;
  text-decoration-skip-ink: none;
}
.err {
  text-decoration: underline wavy 1.5px var(--bad);
  text-underline-offset: 0.28em;
  text-decoration-skip-ink: none;
}

/* Our caret: an empty inline (no break opportunity, no width) with an absolute bar. */
.caret { position: relative; }
.caret::after {
  content: '';
  position: absolute;
  left: -1px;
  top: -0.06em;
  bottom: -0.06em;
  width: 3px;
  border-radius: 1px;
  background: #fffaf0;
  box-shadow: 0 0 0 1px rgba(22, 12, 33, 0.6);
  animation: blink 1.06s steps(1, end) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

.hint {
  position: absolute;
  left: 0;
  right: 0;
  top: 100%;
  margin-top: 6px;
  text-align: center;
  font-size: 12.5px;
  font-weight: 500;
  color: rgba(255, 250, 240, 0.86);
  opacity: 0;
  transform: translateY(-3px);
  transition: opacity 0.3s var(--ease-out), transform 0.3s var(--ease-out);
  pointer-events: none;
}
.hint > span {
  display: inline-block;
  padding: 3px 11px 4px;
  border-radius: 999px;
  background: rgba(18, 9, 28, 0.42);
}
.hint.on { opacity: 1; transform: none; }
.hint.bad { color: #ffd0d0; }
.hint kbd { padding: 0 5px; border-radius: 4px; background: rgba(255, 245, 225, 0.14); color: #fff6e6; }

/* ---------- Corner buttons (nearly invisible until hovered) ---------- */

.corner {
  position: fixed;
  z-index: 20;
  padding: 10px 14px;
  border: 0;
  border-radius: 999px;
  background: transparent;
  font-size: 12px;
  font-weight: 500;
  letter-spacing: 0.02em;
  color: rgba(255, 252, 245, 0.95);
  text-shadow: 0 1px 2px rgba(10, 4, 18, 0.55);
  opacity: 0.16;
  cursor: pointer;
  transition: opacity 0.35s var(--ease-out), background-color 0.35s var(--ease-out), transform 0.35s var(--spring);
}
.corner kbd { opacity: 0; margin-left: 6px; color: rgba(255, 252, 245, 0.7); transition: opacity 0.3s var(--ease-out); }
.corner:hover, .corner:focus-visible, .corner.active {
  opacity: 1;
  background: rgba(16, 9, 24, 0.55);
  text-shadow: none;
}
.corner:hover kbd, .corner:focus-visible kbd { opacity: 1; }
.corner:active { transform: scale(0.96); }
.corner:focus-visible { outline: 1.5px solid var(--accent); outline-offset: 2px; }
.corner.active { opacity: 0; pointer-events: none; } /* its drawer covers it */
.corner.tl { top: 14px; left: 14px; }
.corner.tr { top: 14px; right: 14px; }
.corner.bl { bottom: 14px; left: 14px; }
.corner.br { bottom: 14px; right: 14px; }
.corner.muted { opacity: 0.5; }
body.idle .corner:not(:hover):not(.active) { opacity: 0.07; }

/* ---------- Drawers ---------- */

.drawer {
  position: fixed;
  z-index: 30;
  top: 10px;
  bottom: 10px;
  width: min(360px, calc(100vw - 20px));
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 16px 16px 12px;
  border-radius: 16px;
  background: var(--panel);
  border: 1px solid var(--panel-line);
  box-shadow: 0 24px 60px rgba(8, 3, 14, 0.45), 0 2px 8px rgba(8, 3, 14, 0.3);
  opacity: 0;
  transition: transform 0.42s var(--ease-out), opacity 0.3s var(--ease-out);
}
.drawer.left { left: 10px; transform: translateX(calc(-100% - 24px)); }
.drawer.right { right: 10px; transform: translateX(calc(100% + 24px)); }
.drawer.open { transform: none; opacity: 1; }

.drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.drawer-head h2 {
  margin: 0;
  font-size: 13px;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ui-text);
}
.icon-btn {
  border: 0;
  padding: 4px 8px;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.06);
  color: var(--ui-dim);
  font-size: 11px;
  cursor: pointer;
  transition: background-color 0.2s, color 0.2s;
}
.icon-btn:hover { background: rgba(255, 255, 255, 0.12); color: var(--ui-text); }

.field {
  width: 100%;
  padding: 9px 12px;
  border-radius: 10px;
  border: 1px solid var(--panel-line);
  background: rgba(255, 255, 255, 0.05);
  font-size: 13px;
  outline: none;
  transition: border-color 0.2s, background-color 0.2s;
}
.field::placeholder { color: var(--ui-faint); }
.field:focus { border-color: rgba(255, 229, 151, 0.55); background: rgba(255, 255, 255, 0.08); }
.field::-webkit-search-cancel-button { filter: invert(1) opacity(0.5); }

.drawer-note { margin: -2px 2px 0; font-size: 11px; color: var(--ui-faint); line-height: 1.5; }

.drawer-body {
  flex: 1;
  overflow-y: auto;
  margin: 0 -8px;
  padding: 0 8px 12px;
  scrollbar-width: thin;
  scrollbar-color: rgba(255, 255, 255, 0.15) transparent;
}

.btn {
  border: 0;
  padding: 9px 14px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.09);
  font-size: 12.5px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  transition: background-color 0.2s, transform 0.25s var(--spring), opacity 0.2s;
}
.btn:hover { background: rgba(255, 255, 255, 0.15); }
.btn:active { transform: scale(0.97); }
.btn:focus-visible, .icon-btn:focus-visible { outline: 1.5px solid var(--accent); outline-offset: 2px; }
.btn.primary { background: var(--accent); color: #1c1206; font-weight: 800; }
.btn.primary:hover { background: #fff0bf; }
.btn.ghost { background: transparent; color: var(--ui-dim); }
.btn.ghost:hover { background: rgba(255, 255, 255, 0.07); color: var(--ui-text); }
.btn:disabled { opacity: 0.4; pointer-events: none; }

/* Dictionary */
.dict-group { margin-top: 14px; }
.dict-group:first-child { margin-top: 4px; }
.dict-group h3 {
  margin: 0 2px 8px;
  font-size: 10.5px;
  font-weight: 500;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--ui-faint);
}
.dict-words { display: flex; flex-wrap: wrap; gap: 6px 6px; }
.word {
  position: relative;
  border: 0;
  padding: 3px 9px 4px;
  border-radius: 7px;
  background: rgba(255, 255, 255, 0.05);
  font-size: 13px;
  font-weight: 800;
  line-height: 1.4;
  cursor: pointer;
  text-shadow: -1px -1px 0 var(--ink), 1px -1px 0 var(--ink), -1px 1px 0 var(--ink), 1px 1px 0 var(--ink);
  transition: background-color 0.18s, transform 0.25s var(--spring);
}
.word:hover { background: rgba(255, 255, 255, 0.12); }
.word:active { transform: scale(0.94); }
.word:focus-visible { outline: 1.5px solid var(--accent); outline-offset: 1px; }
.word.c-color { background: var(--swatch); box-shadow: none; }
.word.k-keyword { box-shadow: none; }
.dict-desc {
  min-height: 34px;
  margin: 0 2px;
  padding-top: 10px;
  border-top: 1px solid var(--panel-line);
  font-size: 11.5px;
  line-height: 1.45;
  color: var(--ui-dim);
}
.dict-desc b { color: var(--ui-text); font-weight: 800; }
.dict-empty { margin: 24px 4px; font-size: 12px; color: var(--ui-faint); text-align: center; }

/* Spellbook */
.save-row { display: flex; gap: 8px; }
.save-row .field { flex: 1; min-width: 0; }
.spell {
  position: relative;
  margin-bottom: 8px;
  padding: 11px 12px 10px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid transparent;
  cursor: pointer;
  transition: background-color 0.2s, border-color 0.2s;
}
.spell:hover { background: rgba(255, 255, 255, 0.07); }
.spell:focus-visible { outline: none; border-color: rgba(255, 229, 151, 0.55); }
.spell.current { border-color: rgba(255, 229, 151, 0.28); }
.spell-name {
  display: block;
  margin-bottom: 6px;
  font-size: 12.5px;
  font-weight: 800;
  color: var(--ui-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding-right: 2px;
}
.spell-name[contenteditable='true'] {
  outline: none;
  white-space: normal;
  text-overflow: clip;
  border-bottom: 1px solid var(--accent);
  cursor: text;
}
.spell-text {
  margin: 0;
  font-size: 11px;
  font-weight: 800;
  line-height: 1.55;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  max-height: calc(11px * 1.55 * 4);
  overflow: hidden;
  color: var(--t-plain);
  text-shadow: -1px -1px 0 var(--ink), 1px -1px 0 var(--ink), -1px 1px 0 var(--ink), 1px 1px 0 var(--ink);
}
.spell-text .chip, .spell-text .c-color, .spell-text .k-keyword { box-shadow: none; }
.spell-actions {
  position: absolute;
  top: 7px;
  right: 7px;
  display: flex;
  gap: 1px;
  padding: 2px;
  border-radius: 8px;
  background: rgba(24, 17, 33, 0.96);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s var(--ease-out);
}
.spell:hover .spell-actions, .spell:focus-within .spell-actions { opacity: 1; pointer-events: auto; }
.spell-actions button {
  border: 0;
  padding: 3px 6px;
  border-radius: 6px;
  background: transparent;
  font-size: 11px;
  color: var(--ui-dim);
  cursor: pointer;
}
.spell-actions button:hover { background: rgba(255, 255, 255, 0.08); color: var(--ui-text); }
.spell-actions button.danger:hover { color: #ffb0b0; }
.book-foot { margin: 10px 2px 0; font-size: 11px; color: var(--ui-faint); line-height: 1.5; }

/* ---------- Export modal ---------- */

.modal {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(10, 5, 16, 0.28);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s var(--ease-out), background-color 0.4s var(--ease-out);
}
.modal.open { opacity: 1; pointer-events: auto; }
.modal-card {
  width: min(430px, 100%);
  padding: 18px;
  border-radius: 18px;
  background: var(--panel);
  border: 1px solid var(--panel-line);
  box-shadow: 0 30px 80px rgba(8, 3, 14, 0.5), 0 2px 10px rgba(8, 3, 14, 0.3);
  transform: translateY(10px) scale(0.98);
  transition: transform 0.45s var(--spring), width 0.4s var(--ease-out);
}
.modal.open .modal-card { transform: none; }

.ex-run, .ex-done { display: none; }
.modal[data-state='running'] .ex-setup, .modal[data-state='done'] .ex-setup { display: none; }
.modal[data-state='running'] .ex-run { display: block; }
.modal[data-state='done'] .ex-done { display: block; }

/* While exporting the modal steps aside: a small progress card at the bottom, canvas otherwise clean. */
.modal[data-state='running'] { place-items: end center; background: transparent; pointer-events: none; }
.modal[data-state='running'] .modal-card { width: min(380px, 100%); pointer-events: auto; padding: 14px 16px; }

.ex-group { margin: 16px 0 0; padding: 0; border: 0; min-width: 0; }
.ex-group legend {
  display: flex;
  width: 100%;
  justify-content: space-between;
  padding: 0;
  margin-bottom: 9px;
  font-size: 10.5px;
  font-weight: 500;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--ui-faint);
}
.ex-group legend output { letter-spacing: 0.02em; text-transform: none; font-size: 12px; color: var(--ui-text); }

.formats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.format {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  padding: 12px 6px 10px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid transparent;
  cursor: pointer;
  transition: background-color 0.2s, border-color 0.2s;
}
.format:hover { background: rgba(255, 255, 255, 0.08); }
.format input, .seg input { position: absolute; opacity: 0; pointer-events: none; }
.format:has(input:checked) { border-color: rgba(255, 229, 151, 0.6); background: rgba(255, 229, 151, 0.07); }
.format:has(input:focus-visible), .seg label:has(input:focus-visible) { outline: 1.5px solid var(--accent); outline-offset: 2px; }
.format .ar {
  display: block;
  height: 34px;
  aspect-ratio: var(--w) / var(--h);
  max-width: 60px;
  margin-bottom: 6px;
  border-radius: 4px;
  border: 1.5px solid var(--ui-dim);
  background: linear-gradient(160deg, rgba(255, 229, 151, 0.2), rgba(190, 166, 255, 0.2));
}
.format:has(input:checked) .ar { border-color: var(--accent); }
.format b { font-size: 13px; font-weight: 800; }
.format small { font-size: 10.5px; color: var(--ui-faint); }

.slider { width: 100%; accent-color: var(--accent); margin: 2px 0; }

.seg { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; padding: 3px; border-radius: 11px; background: rgba(255, 255, 255, 0.05); }
.seg label { position: relative; }
.seg span {
  display: block;
  padding: 7px;
  border-radius: 8px;
  text-align: center;
  font-size: 12.5px;
  color: var(--ui-dim);
  cursor: pointer;
  transition: background-color 0.2s, color 0.2s;
}
.seg input:checked + span { background: rgba(255, 255, 255, 0.12); color: var(--ui-text); font-weight: 800; }

.ex-meta { margin: 16px 2px 0; font-size: 11px; line-height: 1.5; color: var(--ui-faint); }
.ex-meta.warn { color: #ffd9a8; }
.ex-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }

.ex-run-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.ex-phase { font-size: 12.5px; font-weight: 800; }
.ex-pct { font-size: 12px; color: var(--ui-dim); font-variant-numeric: tabular-nums; }
.ex-path { font-size: 10.5px; color: var(--ui-faint); }
.bar { height: 4px; margin: 10px 0 8px; border-radius: 4px; background: rgba(255, 255, 255, 0.08); overflow: hidden; }
.bar i { display: block; height: 100%; width: 100%; border-radius: inherit; background: var(--accent); transform-origin: left; transform: scaleX(0); transition: transform 0.2s linear; }
.ex-done-msg { margin: 12px 2px 0; font-size: 12.5px; line-height: 1.6; color: var(--ui-dim); overflow-wrap: anywhere; }
.ex-done-msg b { color: var(--ui-text); }
.ex-done-msg.bad { color: #ffc4c4; }

body.modal-open .editor { opacity: 0.12; }

/* Everything but the canvas (and the progress card) disappears while exporting. */
body.exporting .editor,
body.exporting .corners,
body.exporting .drawer,
body.exporting .toast { opacity: 0 !important; pointer-events: none !important; visibility: hidden; }

/* ---------- Toast & fatal ---------- */

.toast {
  position: fixed;
  z-index: 50;
  left: 50%;
  bottom: 22px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 10px 9px 14px;
  border-radius: 999px;
  background: var(--panel);
  border: 1px solid var(--panel-line);
  font-size: 12px;
  opacity: 0;
  transform: translate(-50%, 8px);
  pointer-events: none;
  transition: opacity 0.25s var(--ease-out), transform 0.35s var(--spring);
}
.toast.on { opacity: 1; transform: translate(-50%, 0); pointer-events: auto; }
.toast button { border: 0; padding: 3px 8px; border-radius: 999px; background: rgba(255, 229, 151, 0.16); color: var(--accent); font-size: 11.5px; cursor: pointer; }
.toast:not(:has(button)) { padding-right: 14px; }

.fatal {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: grid;
  place-content: center;
  gap: 4px;
  padding: 24px;
  text-align: center;
  background: linear-gradient(170deg, #cfc58e, #9fb7a8);
  color: #1d1428;
  font-size: 14px;
  line-height: 1.6;
}
.fatal[hidden] { display: none; }
.fatal p { margin: 0; max-width: 460px; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: 0.01ms !important; }
  .caret::after { animation: none; }
  .c-egg { animation: none; }
}
