/*
  Onelinify — visual identity
  ---------------------------
  The product's whole trick is compression: a function that sprawls across
  a screen goes in, a single unbroken strip of lambdas comes out. The input
  pane reads like ruled notebook paper (numbered lines, a margin rule) and
  the output pane reads like a strip of punched tape (sprocket holes; the
  one logical line wraps and you scroll down to follow it). That contrast
  is the whole design; everything else stays quiet.
*/

:root {
  --ink: #14151a;
  --ink-raised: #1b1d24;
  --paper: #ece7da;
  --paper-dim: #a9a6a0;
  --line: #33353f;
  --amber: #f2a900;
  --amber-ink: #2a1d00;
  --teal: #4fd1c5;
  --teal-dim: #2d7f78;
  --teal-fg: #4fd1c5;
  --scroll-thumb: #2d7f78;
  --crimson: #ff6b6e;
  --crimson-bg: #3a1618;

  /* code-pane theme surface — Notebook maps onto the house palette;
     [data-code-theme=...] blocks (Task 3) override these */
  --code-bg: var(--ink);
  --code-bg-raised: var(--ink-raised);
  --code-fg: var(--paper);
  --code-line: var(--line);
  --code-gutter-fg: var(--paper-dim);
  --tok-comment: var(--paper-dim);
  --tok-string: var(--teal-fg);
  --tok-number: var(--crimson);
  --tok-keyword: var(--amber);
  --tok-builtin: var(--teal-fg);
  --tok-def: var(--amber);
  --tok-call: var(--code-fg);
  --tok-op: var(--paper-dim);

  --mono: ui-monospace, "SFMono-Regular", "JetBrains Mono", Menlo, Consolas,
    "Liberation Mono", monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;

  color-scheme: dark;
}

@media (prefers-color-scheme: light) {
  :root {
    --ink: #f1ede1;
    --ink-raised: #ffffff;
    --paper: #1c1a14;
    --paper-dim: #6b6558;
    --line: #d9d2c1;
    --amber-ink: #2a1d00;
    --teal-dim: #bfece7;
    --teal-fg: #0f766e;
    --scroll-thumb: #2a7069;
    --crimson: #b3261e;
    --crimson-bg: #ffe3e2;
    color-scheme: light;
  }
}

/* ---------- code-pane themes (fixed palettes, independent of OS mode) ---------- */

:root[data-code-theme="monokai"] {
  --code-bg: #272822;
  --code-bg-raised: #272822;
  --code-fg: #f8f8f2;
  --code-line: rgba(255, 255, 255, 0.07);
  --code-gutter-fg: #75715e;
  --tok-comment: #75715e;
  --tok-string: #e6db74;
  --tok-number: #ae81ff;
  --tok-keyword: #f92672;
  --tok-builtin: #66d9ef;
  --tok-def: #a6e22e;
  --tok-call: #a6e22e;
  --tok-op: #f8f8f2;
}

:root[data-code-theme="dracula"] {
  --code-bg: #282a36;
  --code-bg-raised: #282a36;
  --code-fg: #f8f8f2;
  --code-line: rgba(255, 255, 255, 0.07);
  --code-gutter-fg: #6272a4;
  --tok-comment: #6272a4;
  --tok-string: #f1fa8c;
  --tok-number: #bd93f9;
  --tok-keyword: #ff79c6;
  --tok-builtin: #8be9fd;
  --tok-def: #50fa7b;
  --tok-call: #50fa7b;
  --tok-op: #f8f8f2;
}

:root[data-code-theme="solarized-light"] {
  --code-bg: #fdf6e3;
  --code-bg-raised: #fdf6e3;
  --code-fg: #657b83;
  --code-line: rgba(0, 0, 0, 0.09);
  --code-gutter-fg: #93a1a1;
  --tok-comment: #93a1a1;
  --tok-string: #2aa198;
  --tok-number: #d33682;
  --tok-keyword: #859900;
  --tok-builtin: #268bd2;
  --tok-def: #b58900;
  --tok-call: #268bd2;
  --tok-op: #657b83;
}

:root[data-code-theme="solarized-dark"] {
  --code-bg: #002b36;
  --code-bg-raised: #002b36;
  --code-fg: #839496;
  --code-line: rgba(255, 255, 255, 0.06);
  --code-gutter-fg: #586e75;
  --tok-comment: #586e75;
  --tok-string: #2aa198;
  --tok-number: #d33682;
  --tok-keyword: #859900;
  --tok-builtin: #268bd2;
  --tok-def: #b58900;
  --tok-call: #268bd2;
  --tok-op: #839496;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--ink);
  color: var(--paper);
  font-family: var(--sans);
  display: flex;
  flex-direction: column;
}

/* ---------- header ---------- */

header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  padding: 2.25rem clamp(1.25rem, 4vw, 3rem) 1.25rem;
  border-bottom: 1px solid var(--line);
}

header select {
  flex: none;
}

header h1 {
  margin: 0;
  font-family: var(--mono);
  font-size: clamp(1.75rem, 3.2vw, 2.5rem);
  font-weight: 700;
  letter-spacing: -0.03em;
  color: var(--paper);
}

header h1::before {
  content: "$ ";
  color: var(--amber);
  font-weight: 700;
}

header h1::after {
  content: "_";
  color: var(--teal-fg);
  margin-left: 0.15em;
  animation: blink 1.1s steps(1) infinite;
}

header p {
  margin: 0.65rem 0 0;
  max-width: 62ch;
  font-family: var(--mono);
  font-size: 0.9rem;
  line-height: 1.55;
  color: var(--paper-dim);
}

@keyframes blink {
  0%,
  49% {
    opacity: 1;
  }
  50%,
  100% {
    opacity: 0;
  }
}

/* ---------- layout ---------- */

main {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1px;
  background: var(--line);
  min-height: 0;
}

@media (max-width: 860px) {
  main {
    grid-template-columns: 1fr;
  }
}

section {
  background: var(--ink);
  display: flex;
  flex-direction: column;
  min-width: 0;
  padding: clamp(1rem, 2.5vw, 1.75rem);
  gap: 0.85rem;
}

/* ---------- toolbar ---------- */

.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  font-family: var(--mono);
}

.output-pane .toolbar span {
  display: inline-flex;
  align-items: center;
  background: var(--amber);
  color: var(--amber-ink);
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  padding: 0.3rem 0.6rem;
  border-radius: 3px;
  transform: rotate(-1.5deg);
}

select,
button {
  font-family: var(--mono);
  font-size: 0.85rem;
  border-radius: 4px;
  border: 1px solid var(--line);
  background: var(--ink-raised);
  color: var(--paper);
  padding: 0.5rem 0.75rem;
}

select {
  flex: 1;
  min-width: 0;
  cursor: pointer;
}

button {
  cursor: pointer;
  font-weight: 600;
  white-space: nowrap;
  transition: transform 0.12s ease, background 0.12s ease;
}

button:hover:not(:disabled) {
  background: var(--teal-dim);
}

button:active:not(:disabled) {
  transform: translateY(1px);
}

button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

#transform {
  background: var(--amber);
  color: var(--amber-ink);
  border-color: var(--amber);
}

#transform:hover {
  background: var(--teal);
  border-color: var(--teal);
  color: var(--amber-ink);
}

select:focus-visible,
button:focus-visible,
textarea:focus-visible {
  outline: 2px solid var(--teal-fg);
  outline-offset: 2px;
}

/* ---------- input pane: ruled notebook paper ---------- */

/*
  The ruled lines are a repeating gradient with one line per 1.6rem text row.
  background-origin: content-box starts the bands at the first text row (not
  the padding box), and background-attachment: local scrolls them with the
  text — both are load-bearing for the lines to sit under each row. The
  gutter shares the same metrics and is scroll-synced from app.js.
*/

.editor {
  flex: 1;
  display: flex;
  min-height: 260px;
  resize: vertical;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 4px;
}

#gutter {
  margin: 0;
  flex: none;
  min-width: 2.75ch;
  padding: 0.75rem 0.5rem 2.75rem 0.6rem;
  font-family: var(--mono);
  font-size: 0.9rem;
  line-height: 1.6rem;
  tab-size: 4;
  text-align: right;
  color: var(--code-gutter-fg);
  background-color: var(--code-bg);
  background-image: repeating-linear-gradient(
    to bottom,
    transparent 0,
    transparent calc(1.6rem - 1px),
    var(--code-line) 1.6rem
  );
  background-attachment: local;
  background-origin: content-box;
  border-right: 3px solid var(--crimson);
  overflow: hidden;
  user-select: none;
}

.code-surface {
  position: relative;
  flex: 1;
  min-width: 0;
  display: flex;
}

#highlight {
  position: absolute;
  inset: 0;
  margin: 0;
  overflow: hidden;
  white-space: pre;
  font-family: var(--mono);
  font-size: 0.9rem;
  line-height: 1.6rem;
  tab-size: 4;
  padding: 0.75rem 2rem 2.75rem 0.9rem;
  color: var(--code-fg);
  background-color: var(--code-bg);
  background-image: repeating-linear-gradient(
    to bottom,
    transparent 0,
    transparent calc(1.6rem - 1px),
    var(--code-line) 1.6rem
  );
  background-attachment: local;
  background-origin: content-box;
}

.editor textarea {
  flex: 1;
  min-width: 0;
  position: relative;
  z-index: 1;
  font-family: var(--mono);
  font-size: 0.9rem;
  line-height: 1.6rem;
  tab-size: 4;
  color: transparent;
  background: transparent;
  border: none;
  padding: 0.75rem 0.9rem;
  caret-color: var(--code-fg);
}

.editor textarea::selection {
  background: rgba(79, 209, 197, 0.3);
  background: color-mix(in srgb, var(--code-fg) 25%, transparent);
  color: transparent;
}

.editor textarea:focus-visible {
  outline-offset: -2px;
}

/* ---------- output pane: punched tape ---------- */

.output-pane {
  position: relative;
}

.tape {
  position: relative;
  flex: 1;
  min-height: 260px;
  background: var(--code-bg-raised);
  border: 1px solid var(--line);
  border-radius: 4px;
}

pre#output {
  position: absolute;
  inset: 0;
  margin: 0;
  overflow-y: auto;
  overflow-x: hidden;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  padding: 1.9rem 1.25rem;
}

.tape::before,
.tape::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  height: 10px;
  background-image: repeating-radial-gradient(
    circle at 12px 5px,
    var(--paper-dim) 0 3px,
    transparent 3.5px
  );
  background-size: 24px 10px;
  opacity: 0.55;
  pointer-events: none;
  z-index: 1;
}

.tape::before {
  top: 6px;
}

.tape::after {
  bottom: 6px;
}

pre#output code {
  font-family: var(--mono);
  font-size: 0.9rem;
  color: var(--code-fg);
}

.tok-comment { color: var(--tok-comment); }
.tok-string  { color: var(--tok-string); }
.tok-number  { color: var(--tok-number); }
.tok-keyword { color: var(--tok-keyword); }
.tok-builtin { color: var(--tok-builtin); }
.tok-def     { color: var(--tok-def); }
.tok-call    { color: var(--tok-call); }
.tok-op      { color: var(--tok-op); }

pre#output::-webkit-scrollbar {
  width: 10px;
}

pre#output::-webkit-scrollbar-track {
  background: transparent;
}

pre#output::-webkit-scrollbar-thumb {
  background: var(--scroll-thumb);
  border-radius: 6px;
}

/* ---------- error state ---------- */

#error {
  display: flex;
  align-items: flex-start;
  gap: 0.6rem;
  font-family: var(--mono);
  font-size: 0.85rem;
  line-height: 1.5;
  color: var(--crimson);
  background: var(--crimson-bg);
  border: 1px solid var(--crimson);
  border-left: 3px solid var(--crimson);
  border-radius: 4px;
  padding: 0.75rem 0.9rem;
}

#error[hidden] {
  display: none;
}

#error::before {
  content: "!";
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: none;
  width: 1.1rem;
  height: 1.1rem;
  border-radius: 50%;
  background: var(--crimson);
  color: var(--crimson-bg);
  font-weight: 800;
  font-size: 0.75rem;
}

/* ---------- reduced motion ---------- */

@media (prefers-reduced-motion: reduce) {
  header h1::after {
    animation: none;
  }
  button {
    transition: none;
  }
}
