/* Antimatter Dimensions script library
 *
 * The code is the subject, so the page is a reading surface with the quietest
 * chrome it can get away with. Two things carry information rather than
 * decorate: the character-budget meter (the game rejects a script over ~10,000
 * characters, so that is the only number worth being loud about) and the
 * prestige-layer hues, which are the tags, the index markers and the syntax
 * colours all at once.
 */

/* Cascadia Mono, subset to the characters an Automator script can contain
 * (14 KB + 16 KB rather than 412 KB of TTF). Cascadia ships with Windows
 * Terminal, so local() spares most Windows machines the download, and the
 * unicode-range sends Hangul straight to the fallback instead of making the
 * browser try this face first and come back empty.
 */
@font-face {
  font-family: "Cascadia Mono Web";
  src: local("Cascadia Mono"),
    url("/assets/fonts/cascadia-mono-regular.woff2") format("woff2");
  font-weight: 400;
  font-style: normal;
  font-display: swap;
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA,
    U+02DC, U+2000-206F, U+20AC, U+2122, U+2190-2193, U+2212, U+2215, U+FEFF,
    U+FFFD;
}

/* Comments are set in italic, so the real italic is worth its 16 KB. */
@font-face {
  font-family: "Cascadia Mono Web";
  src: local("Cascadia Mono Italic"),
    url("/assets/fonts/cascadia-mono-italic.woff2") format("woff2");
  font-weight: 400;
  font-style: italic;
  font-display: swap;
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA,
    U+02DC, U+2000-206F, U+20AC, U+2122, U+2190-2193, U+2212, U+2215, U+FEFF,
    U+FFFD;
}

:root {
  --void: #0c0a18;
  --panel: #131027;
  --raised: #1c1733;
  --edge: #2a2347;
  --edge-soft: #1f1a38;

  --ink: #e6e2f5;
  --dim: #9a93bc;
  --faint: #5d5685;

  /* Prestige layers, in the order you reach them in the game. */
  --ec: #7fa8ff;
  --infinity: #f5a94c;
  --eternity: #b07cff;
  --dilation: #4fd6c9;
  --reality: #ff7bb8;

  --ok: #7fe0a8;
  --bad: #ff6b8a;
  --accent: var(--eternity);
  --on-accent: #180f2c;

  --mono: "Cascadia Mono Web", "Cascadia Mono", Consolas, ui-monospace,
    "SFMono-Regular", Menlo, "Malgun Gothic", "Apple SD Gothic Neo", monospace;
  --sans: Archivo, "Segoe UI", system-ui, -apple-system, "Malgun Gothic",
    "Apple SD Gothic Neo", sans-serif;

  --rail: 20.5rem;
  --gutter: 1.65rem;
  --radius: 5px;

  /* Both code surfaces — the viewer and the editor — read from these, so
     they stay identical. Tighter than prose: a script is scanned down the
     indentation, not read line by line. */
  --code-size: 0.8125rem;
  --code-leading: 0.9;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--void);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 0.875rem;
  line-height: 1.55;
  -webkit-text-size-adjust: 100%;
}

a { color: var(--accent); text-decoration-thickness: 1px; text-underline-offset: 2px; }

button, input, textarea, select { font: inherit; color: inherit; }

button {
  background: var(--raised);
  border: 1px solid var(--edge);
  border-radius: var(--radius);
  padding: 0.45rem 0.85rem;
  min-height: 2.25rem;
  cursor: pointer;
  white-space: nowrap;
}

button:hover { border-color: var(--faint); background: #221c3d; }
button:disabled { opacity: 0.4; cursor: default; background: var(--raised); border-color: var(--edge); }

.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--on-accent);
  font-weight: 600;
}

.primary:hover { background: #bd90ff; border-color: #bd90ff; }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 3px;
}

::selection { background: #3b2d66; }

/* ---------- shell ---------- */

.shell {
  display: grid;
  grid-template-columns: var(--rail) minmax(0, 1fr);
  height: 100dvh;
}

.rail {
  border-right: 1px solid var(--edge);
  background: var(--panel);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.rail-head {
  padding: 1.15rem 1.1rem 0.9rem;
  display: grid;
  gap: 0.7rem;
}

.wordmark {
  margin: 0;
  font-size: 1.0625rem;
  font-weight: 700;
  font-stretch: 112%;
  letter-spacing: -0.015em;
  line-height: 1.2;
}

.wordmark span { color: var(--accent); }

/* Admin only: the link back to the public page, tucked under the wordmark. */
.rail-note {
  margin: -0.45rem 0 0;
  font-size: 0.75rem;
  color: var(--dim);
}

.search {
  width: 100%;
  background: var(--void);
  border: 1px solid var(--edge);
  border-radius: var(--radius);
  padding: 0.5rem 0.65rem;
  min-height: 2.25rem;
}

.search::placeholder { color: var(--faint); }
.search:focus-visible { border-color: var(--accent); outline-offset: 0; }

.tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
  padding: 0 1.1rem 0.9rem;
}

/* The prestige layer a thing belongs to, as a colour. Set by data-layer and
   inherited by children, so an index row tints its own budget track.
   Everything reads it as var(--hue, <default>) rather than declaring --hue
   itself: a component-level declaration would have the same specificity as
   the map below and silently win on source order. */
[data-layer="ec"]        { --hue: var(--ec); }
[data-layer="infinity"]  { --hue: var(--infinity); }
[data-layer="eternity"]  { --hue: var(--eternity); }
[data-layer="dilation"]  { --hue: var(--dilation); }
[data-layer="reality"]   { --hue: var(--reality); }

.tag {
  background: none;
  border: 1px solid var(--edge);
  border-radius: 999px;
  padding: 0.12rem 0.6rem;
  min-height: 0;
  font-family: var(--mono);
  font-size: 0.75rem;
  color: var(--hue, var(--dim));
}

.tag:hover { background: none; border-color: var(--hue, var(--dim)); }

.tag[aria-pressed="true"] {
  background: color-mix(in oklab, var(--hue, var(--dim)) 18%, transparent);
  border-color: var(--hue, var(--dim));
  color: var(--hue, var(--dim));
}

.index {
  list-style: none;
  margin: 0;
  padding: 0 0 3rem;
  overflow-y: auto;
  overscroll-behavior: contain;
  flex: 1;
  min-height: 0;
  border-top: 1px solid var(--edge-soft);
}

.index li + li { border-top: 1px solid var(--edge-soft); }

.entry {
  display: grid;
  gap: 0.3rem;
  width: 100%;
  text-align: left;
  background: none;
  border: 0;
  border-radius: 0;
  box-shadow: inset 2px 0 0 transparent;
  padding: 0.7rem 1.1rem 0.75rem;
}

.entry:hover { background: #181334; box-shadow: inset 2px 0 0 var(--edge); }

.entry[aria-current="true"] {
  background: #191436;
  box-shadow: inset 2px 0 0 var(--hue, var(--faint));
}

.entry-name {
  display: flex;
  justify-content: space-between;
  gap: 0.6rem;
  align-items: baseline;
}

.entry-name b {
  font-weight: 600;
  font-stretch: 105%;
  letter-spacing: -0.005em;
}

.size {
  font-family: var(--mono);
  color: var(--dim);
  font-size: 0.75rem;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

.size.flag { color: var(--bad); }
.size.draft { color: var(--infinity); }

/* The description keeps the line breaks it was written with, so the clamp
   allows the full three lines a description can have. */
.entry-summary {
  font-size: 0.78rem;
  color: var(--dim);
  line-height: 1.4;
  white-space: pre-line;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* ---------- the character budget ---------- */

/* One track, 0 to the in-game limit: the fill is the script as written, the
   tick is where minifying would pull it back to. */
.track {
  position: relative;
  height: 4px;
  background: var(--edge-soft);
  border-radius: 999px;
  overflow: hidden;
}

.track i {
  position: absolute;
  inset: 0 auto 0 0;
  width: var(--fill, 0%);
  background: var(--hue, var(--accent));
  border-radius: 999px;
}

/* The tick always lands inside the fill (minifying never grows a script), so
   a translucent dark notch reads correctly on any backdrop. */
.track u {
  position: absolute;
  inset: 0 auto 0 var(--min, 0%);
  width: 2px;
  background: rgb(10 8 20 / 0.85);
}

.track.over i { background: var(--bad); }

.entry .track { margin-top: 0.15rem; }

.budget { display: grid; gap: 0.4rem; margin-top: 0.9rem; max-width: 34rem; }

.budget .track { height: 7px; }

.budget-read {
  display: flex;
  align-items: baseline;
  gap: 0.9rem;
  font-family: var(--mono);
  font-size: 0.8125rem;
  color: var(--dim);
  font-variant-numeric: tabular-nums;
}

.budget-read b {
  font-size: 1.5rem;
  font-weight: 400;
  color: var(--ink);
  letter-spacing: -0.02em;
}

.budget-read .over { color: var(--bad); }
.budget-read .cap { margin-left: auto; }

/* ---------- script view ---------- */

.view {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

.view-head {
  padding: 1.15rem var(--gutter) 1.05rem;
  border-bottom: 1px solid var(--edge);
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 0.9rem 1.5rem;
  align-items: start;
}

.view-head h2 {
  margin: 0;
  font-size: 1.375rem;
  font-weight: 600;
  font-stretch: 110%;
  letter-spacing: -0.02em;
  line-height: 1.2;
}

.view-head .about {
  margin: 0.4rem 0 0;
  color: var(--dim);
  max-width: 64ch;
  white-space: pre-line;
  line-height: 1.5;
}

.view-foot {
  grid-column: 1 / -1;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem 0.9rem;
  margin-top: 0.2rem;
}

.layer {
  font-family: var(--mono);
  font-size: 0.75rem;
  color: var(--hue, var(--dim));
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
}

.layer::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 999px;
  background: currentColor;
}

.stamp {
  margin-left: auto;
  font-family: var(--mono);
  font-size: 0.75rem;
  color: var(--faint);
}

.actions { display: flex; gap: 0.5rem; }

.code-wrap {
  overflow: auto;
  overscroll-behavior-x: contain;
  flex: 1;
  padding: 1.1rem 0 4rem;
}

pre.code {
  margin: 0;
  padding: 0 var(--gutter) 0 4.2rem;
  counter-reset: line;
  font-family: var(--mono);
  font-size: var(--code-size);
  line-height: var(--code-leading);
  tab-size: 4;
}

pre.code .ln { display: block; white-space: pre; }

pre.code .ln::before {
  counter-increment: line;
  content: counter(line);
  display: inline-block;
  width: 2.6rem;
  margin-left: -2.6rem;
  padding-right: 1.1rem;
  text-align: right;
  color: #3d3663;
  user-select: none;
  -webkit-user-select: none;
}

.t-comment  { color: #655d91; font-style: italic; }
.t-string   { color: var(--ok); }
.t-keyword  { color: #cbb8ff; }
.t-command  { color: var(--eternity); }
.t-prestige { color: var(--infinity); }
.t-currency { color: var(--ec); }
.t-constant { color: var(--ec); }
.t-modifier { color: #b7aed8; }
.t-path     { color: var(--reality); }
.t-number   { color: var(--dilation); }
.t-operator { color: var(--dim); }
.t-punct    { color: var(--faint); }
.t-name     { color: var(--ink); }

.empty, .loading {
  margin: 0;
  padding: 3.5rem var(--gutter);
  color: var(--dim);
  max-width: 46ch;
}

.empty b { display: block; color: var(--ink); font-weight: 600; margin-bottom: 0.3rem; }

.back { display: none; }

/* ---------- toast ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: 1.5rem;
  z-index: 10;
  transform: translate(-50%, 0.5rem);
  background: var(--raised);
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  box-shadow: 0 10px 30px rgb(6 4 14 / 0.55);
  padding: 0.55rem 1rem;
  font-family: var(--mono);
  font-size: 0.8125rem;
  opacity: 0;
  pointer-events: none;
  max-width: calc(100vw - 2rem);
}

.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (prefers-reduced-motion: no-preference) {
  .toast { transition: opacity 0.16s ease, transform 0.16s ease; }
  .budget .track i { transition: width 0.4s cubic-bezier(0.2, 0.7, 0.2, 1); }
}

/* ---------- admin ---------- */

.rail-head .new { width: 100%; }

.editor { display: flex; flex-direction: column; min-height: 0; flex: 1; }

.fields {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(11rem, 1fr));
  gap: 0.65rem 0.8rem;
  padding: 1rem var(--gutter);
  border-bottom: 1px solid var(--edge);
}

.field { display: flex; flex-direction: column; gap: 0.25rem; min-width: 0; }

.field > span {
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
  font-size: 0.75rem;
  color: var(--dim);
}

.field em { font-style: normal; font-family: var(--mono); color: var(--faint); }
.field em.over { color: var(--bad); }

.field input,
.field textarea {
  background: var(--void);
  border: 1px solid var(--edge);
  border-radius: var(--radius);
  padding: 0.45rem 0.6rem;
  min-height: 2.25rem;
  resize: vertical;
  width: 100%;
}

.field input { font-family: var(--mono); }
.field textarea { line-height: 1.5; }
.field input:focus-visible,
.field textarea:focus-visible { border-color: var(--accent); outline-offset: 0; }
.field input:disabled { color: var(--faint); background: var(--panel); }

.field.wide { grid-column: 1 / -1; }

.source {
  flex: 1;
  min-height: 0;
  width: 100%;
  resize: none;
  background: var(--void);
  border: 0;
  padding: 1.1rem var(--gutter);
  font-family: var(--mono);
  font-size: var(--code-size);
  line-height: var(--code-leading);
  tab-size: 4;
  white-space: pre;
  overflow-wrap: normal;
  overflow-x: auto;
}

.source:focus-visible { outline: 0; box-shadow: inset 2px 0 0 var(--accent); }

.issues {
  max-height: 9.5rem;
  overflow-y: auto;
  border-top: 1px solid var(--edge);
  background: #100d22;
  padding: 0.55rem var(--gutter);
  font-size: 0.8125rem;
}

.issues:empty { display: none; }

.issue { display: flex; }

.issue button {
  display: flex;
  gap: 0.7rem;
  width: 100%;
  text-align: left;
  background: none;
  border: 0;
  border-radius: 3px;
  padding: 0.1rem 0.2rem;
  min-height: 0;
  color: var(--dim);
  white-space: normal;
}

.issue button:hover { background: var(--raised); color: var(--ink); }

.issue .where {
  flex: none;
  min-width: 3.4rem;
  font-family: var(--mono);
  font-variant-numeric: tabular-nums;
  color: var(--faint);
}

.issue.error .where { color: var(--bad); }
.issue.warning .where { color: var(--infinity); }
.ok { color: var(--ok); }

.bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem 0.8rem;
  padding: 0.75rem var(--gutter);
  border-top: 1px solid var(--edge);
  background: var(--panel);
}

.bar .counts {
  font-family: var(--mono);
  font-size: 0.8125rem;
  color: var(--dim);
  font-variant-numeric: tabular-nums;
}

.bar .counts b { color: var(--ink); font-weight: 400; }
.bar .counts b.over { color: var(--bad); }
.bar .counts .unsaved { color: var(--infinity); }

.toggle { display: inline-flex; align-items: center; gap: 0.4rem; cursor: pointer; }
.toggle input { accent-color: var(--accent); width: 1rem; height: 1rem; }

.bar .spacer { flex: 1; }
.bar .danger { color: var(--bad); border-color: #4a2039; }
.bar .danger:hover { border-color: var(--bad); background: #2a1428; }

/* ---------- responsive ---------- */

@media (max-width: 860px) {
  :root { --gutter: 1.1rem; }

  .shell { grid-template-columns: 1fr; }
  .rail { border-right: 0; }

  .shell[data-pane="view"] .rail { display: none; }
  .shell[data-pane="rail"] .view { display: none; }

  .back { display: inline-flex; }

  .view-head {
    grid-template-columns: minmax(0, 1fr);
    padding: 0.95rem var(--gutter) 0.9rem;
    gap: 0.7rem;
  }

  /* Going back belongs at the top, where the thumb expects it on a phone;
     only the copy actions move down to the bar. */
  .view-head > .back { order: -1; justify-self: start; }

  .view-head h2 { font-size: 1.1875rem; }
  .budget { margin-top: 0.5rem; max-width: none; }
  .budget-read b { font-size: 1.25rem; }

  /* Copy is the reason you opened this on a phone: keep it under the thumb. */
  .view .actions {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 5;
    padding: 0.6rem var(--gutter);
    padding-bottom: max(0.6rem, env(safe-area-inset-bottom));
    background: color-mix(in oklab, var(--panel) 93%, transparent);
    backdrop-filter: blur(10px);
    border-top: 1px solid var(--edge);
  }

  .view .actions button { flex: 1; min-height: 2.75rem; }
  .code-wrap { padding-bottom: 6rem; }
  .toast { bottom: 5.25rem; }

  pre.code { padding-left: 3.6rem; }
  pre.code .ln::before { width: 2.2rem; margin-left: -2.2rem; padding-right: 0.8rem; }

  .fields { grid-template-columns: minmax(0, 1fr); }
  .bar { position: sticky; bottom: 0; }
  .bar button { flex: 1 0 auto; min-height: 2.6rem; }
  .issues { max-height: 7rem; }
}

@media (max-width: 420px) {
  .budget-read { flex-wrap: wrap; gap: 0.15rem 0.8rem; }
  .budget-read .cap { margin-left: 0; }
}
