/* Syntax highlighting for the element fields the app runs as Lua: the
 * editors (.lua-editor) and the vault's read-only pages (.lua-view).
 *
 * Deliberately not part of the Tailwind build: this file needs no npm step to
 * change, and the editor's own class names would otherwise have to survive
 * Tailwind's content scan of a JavaScript file.
 *
 * The editor is a highlighted <pre> with a transparent <textarea> lying
 * exactly on top of it. The <pre> is the element in normal flow, so it -- not
 * a scrollHeight measurement -- is what gives the box its height, and the two
 * layers must therefore agree on every property that decides where a glyph
 * lands: font, size, line height, padding, border width, wrapping, tab size.
 * Change one below and you must change the other.
 */

.lua-editor,
.lua-view {
  /* One palette, so a theme change is a handful of lines rather than a hunt.
   * Light values are the defaults; the dark themes override them below. */
  --lua-bg: var(--color-base-200, #f7f7f8);
  --lua-bg-focus: var(--color-base-100, #fff);
  --lua-fg: #383a42;
  --lua-border: color-mix(in oklab, var(--color-base-content, #383a42) 20%, transparent);
  --lua-border-focus: var(--color-primary, #4078f2);
  --lua-keyword: #a626a4;
  --lua-string: #4d8f3f;
  --lua-comment: #9195a1;
  --lua-number: #b26a00;
  --lua-api: #3d6fd6;
  --lua-member: #0184bc;
  --lua-operator: #6a6f7d;
  --lua-interp: #c9482f;
  --lua-interp-bg: color-mix(in oklab, #c9482f 12%, transparent);
  --lua-match-bg: color-mix(in oklab, #3d6fd6 22%, transparent);
  --lua-error: var(--color-error, #d33a3a);
  --lua-warning: #b26a00;

  margin-top: 0.25rem;
}

:where([data-theme="dark"], [data-theme="coffe"], [data-theme="coffee"], [data-theme="night"], [data-theme="dracula"], [data-theme="business"]) :is(.lua-editor, .lua-view) {
  --lua-fg: #cdd3de;
  --lua-keyword: #c792ea;
  --lua-string: #98c379;
  --lua-comment: #7c8494;
  --lua-number: #e5a663;
  --lua-api: #79b8ff;
  --lua-member: #56b6c2;
  --lua-operator: #9aa2b1;
  --lua-interp: #ff8f7a;
  --lua-interp-bg: color-mix(in oklab, #ff8f7a 16%, transparent);
  --lua-match-bg: color-mix(in oklab, #79b8ff 26%, transparent);
  --lua-warning: #e5a663;
}

/* The "Lua" marker next to the field label. */
.lua-tag {
  margin-left: 0.5rem;
  padding: 0.05rem 0.4rem;
  border: 1px solid color-mix(in oklab, var(--color-base-content, #383a42) 25%, transparent);
  border-radius: 0.35rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  font-size: 0.65rem;
  line-height: 1.4;
  letter-spacing: 0.03em;
  opacity: 0.65;
  cursor: help;
  user-select: none;
}

/* ------------------------------------------------------------------ layers */

.lua-editor__box {
  position: relative;
  border-radius: 0.5rem;
}

/* Everything in this block has to hold for both layers at once. */
.lua-editor__pre,
.lua-editor__input,
.lua-view__pre,
textarea.lua-input {
  margin: 0;
  display: block;
  box-sizing: border-box;
  width: 100%;
  padding: 0.5rem 0.65rem;
  border: 1px solid transparent;
  border-radius: 0.5rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  font-size: 0.8125rem;
  font-weight: 400;
  font-variant-ligatures: none;
  line-height: 1.6;
  letter-spacing: 0;
  tab-size: 4;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  word-break: break-word;
  text-align: left;
}

.lua-editor__pre {
  min-height: 2.6rem;
  background: var(--lua-bg);
  border-color: var(--lua-border);
  color: var(--lua-fg);
  overflow: hidden;
}

/* The vault's read-only block. Sized like the plain value boxes it sits among,
 * and selectable, so a script can still be read and copied. */
.lua-view__pre {
  min-height: 2.5rem;
  background: var(--lua-bg);
  border-color: var(--lua-border);
  color: var(--lua-fg);
  overflow-x: auto;
}

.lua-editor__input {
  position: absolute;
  inset: 0;
  height: 100%;
  resize: none;
  overflow: hidden;
  background: transparent;
  color: transparent;
  caret-color: var(--color-base-content, #111);
  outline: none;
}

.lua-editor__input::selection {
  background: color-mix(in oklab, var(--lua-api) 30%, transparent);
}

.lua-editor__box:focus-within .lua-editor__pre {
  background: var(--lua-bg-focus);
  border-color: var(--lua-border-focus);
  box-shadow: 0 0 0 2px color-mix(in oklab, var(--lua-border-focus) 25%, transparent);
}

/* Before the JavaScript upgrades it -- and forever, if scripts are off -- the
 * bare textarea is still a usable monospace field rather than a prose box. */
textarea.lua-input {
  background: var(--color-base-200, #f7f7f8);
  border-color: color-mix(in oklab, var(--color-base-content, #383a42) 20%, transparent);
  color: inherit;
  resize: vertical;
}

/* --------------------------------------------------------------- highlight */

.lua-tok-keyword   { color: var(--lua-keyword); }
.lua-tok-string    { color: var(--lua-string); }
.lua-tok-comment   { color: var(--lua-comment); font-style: italic; }
.lua-tok-number    { color: var(--lua-number); }
.lua-tok-std       { color: var(--lua-api); }
.lua-tok-api       { color: var(--lua-api); font-weight: 600; }
.lua-tok-member    { color: var(--lua-member); }
.lua-tok-ident     { color: inherit; }
.lua-tok-op        { color: var(--lua-operator); }

/* {{ … }} is substituted before the chunk is ever handed to Lua, so it reads
 * as its own thing rather than as the surrounding string or code. */
.lua-tok-interp {
  color: var(--lua-interp);
  background: var(--lua-interp-bg);
  border-radius: 0.15rem;
  font-weight: 600;
}

.lua-tok-match {
  background: var(--lua-match-bg);
  border-radius: 0.15rem;
}

.lua-mark-error {
  text-decoration: underline wavy var(--lua-error);
  text-underline-offset: 0.2em;
  text-decoration-skip-ink: none;
}

.lua-mark-warning {
  text-decoration: underline wavy var(--lua-warning);
  text-underline-offset: 0.2em;
  text-decoration-skip-ink: none;
}

/* ------------------------------------------------------------- diagnostics */

.lua-editor__status {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem;
  margin-top: 0.3rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  font-size: 0.7rem;
  line-height: 1.5;
  color: color-mix(in oklab, var(--color-base-content, #383a42) 65%, transparent);
}

.lua-editor__diagnostics {
  list-style: none;
  margin: 0.15rem 0 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  width: 100%;
}

.lua-editor__diagnostic {
  display: flex;
  gap: 0.4rem;
  align-items: baseline;
  padding: 0.1rem 0.3rem;
  border-radius: 0.3rem;
  background: none;
  border: 0;
  font: inherit;
  text-align: left;
  cursor: pointer;
  color: inherit;
  width: 100%;
}

.lua-editor__diagnostic:hover {
  background: color-mix(in oklab, var(--color-base-content, #383a42) 8%, transparent);
}

.lua-editor__diagnostic--error > .lua-editor__where { color: var(--lua-error); }
.lua-editor__diagnostic--warning > .lua-editor__where { color: var(--lua-warning); }

.lua-editor__where {
  flex: none;
  font-weight: 600;
  white-space: nowrap;
}

.lua-editor__ok {
  color: color-mix(in oklab, var(--color-base-content, #383a42) 45%, transparent);
}

@media (prefers-reduced-motion: no-preference) {
  .lua-editor__pre {
    transition: border-color 120ms ease, background-color 120ms ease;
  }
}
