:root {
  --bg: #f5f4fa;
  --surface: #ebe9f3;
  --surface-raised: #ffffff;
  --line: #dcd8e8;
  --text: #17141f;
  --muted: #6d6880;
  --accent: #3b2eff;
  --on-accent: #ffffff;
  --checker-a: #aaa5bb;
  --checker-b: #9d98af;
  --shadow: 0 1px 2px rgba(23, 20, 31, .1), 0 0 0 1px rgba(23, 20, 31, .04);
  --swatch-ring: rgba(23, 20, 31, .14);
  --radius: 14px;
  --ui: "Schibsted Grotesk", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --mono: "Martian Mono", ui-monospace, "SF Mono", Menlo, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121019;
    --surface: #1d1a27;
    --surface-raised: #2a2638;
    --line: #2a2638;
    --text: #f2f0f8;
    --muted: #9c97ae;
    --accent: #6e62ff;
    --on-accent: #ffffff;
    --checker-a: #8f8aa1;
    --checker-b: #837e95;
    --shadow: 0 1px 2px rgba(0, 0, 0, .5);
    --swatch-ring: rgba(255, 255, 255, .18);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 var(--ui);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

button { font: inherit; color: inherit; cursor: pointer; }
svg { display: block; }

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

.app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 560px;
  margin: 0 auto;
  padding-top: env(safe-area-inset-top);
}

/* Preview: a transparency checker, the native backdrop of a PNG */
.preview {
  position: relative;
  flex: 0 0 36dvh;
  min-height: 190px;
  margin: 12px 16px 0;
  border-radius: var(--radius);
  display: grid;
  place-items: center;
  overflow: hidden;
  padding: 24px;
  transition: background-color .2s;
}
.preview[data-bg="checker"] {
  background-color: var(--checker-a);
  background-image: conic-gradient(var(--checker-b) 25%, transparent 0 50%, var(--checker-b) 0 75%, transparent 0);
  background-size: 20px 20px;
}
.preview[data-bg="light"] { background: #ffffff; box-shadow: inset 0 0 0 1px var(--line); }
.preview[data-bg="dark"] { background: #17141f; }

/* Absolutely sized so tall renders scale down instead of overflowing the stage. */
#canvas {
  position: absolute;
  inset: 24px;
  width: calc(100% - 48px);
  height: calc(100% - 48px);
  object-fit: contain;
}

/* Copy: the text peels off the checker like a sticker. drop-shadow follows the glyph alpha. */
#canvas.lift { animation: lift .7s cubic-bezier(.3, .7, .2, 1); }
@keyframes lift {
  0%   { transform: none; filter: none; }
  35%  { transform: translateY(-8px) rotate(-2.5deg) scale(1.05); filter: drop-shadow(0 14px 10px rgba(23, 20, 31, .35)); }
  100% { transform: none; filter: none; }
}

.preview-meta {
  position: absolute;
  left: 12px;
  bottom: 12px;
  font: 500 10px/1 var(--mono);
  letter-spacing: .02em;
  padding: 6px 8px;
  border-radius: 6px;
  background: rgba(255, 255, 255, .85);
  color: #17141f;
  pointer-events: none;
  font-variant-numeric: tabular-nums;
}

.bg-toggle {
  position: absolute;
  right: 10px;
  bottom: 10px;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 0;
  display: grid;
  place-items: center;
  background: rgba(255, 255, 255, .85);
  color: #17141f;
  box-shadow: 0 1px 3px rgba(23, 20, 31, .18);
}

/* Controls */
.controls {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 16px 16px 24px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  -webkit-overflow-scrolling: touch;
}

textarea {
  width: 100%;
  resize: none;
  border: 1px solid var(--line);
  background: var(--surface-raised);
  color: var(--text);
  border-radius: var(--radius);
  padding: 12px 14px;
  font-family: inherit;
  font-size: 17px; /* >=16px prevents iOS zoom-on-focus */
  line-height: 1.35;
  height: calc(5 * 1.35em + 26px); /* fixed at 5 lines; longer text scrolls */
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}
textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent); }
textarea::placeholder { color: var(--muted); }

/* Two-step editing: pick a group, then adjust it. Each tab previews its current value on a mini checker. */
.tabs { display: contents; }
.tabs [role="tab"], .bar-action {
  border: 0;
  background: none;
  padding: 0;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 7px;
  flex: 0 0 auto;
  min-width: 64px;
  scroll-snap-align: end;
}
.tab-tile {
  flex: 0 0 auto;
  height: 44px;
  border-radius: 11px;
  display: grid;
  place-items: center;
  overflow: hidden;
  background-color: var(--checker-a);
  background-image: conic-gradient(var(--checker-b) 25%, transparent 0 50%, var(--checker-b) 0 75%, transparent 0);
  background-size: 10px 10px;
  color: var(--tab-fg, #fff);
  transition: box-shadow .15s;
}
.tab-label {
  font: 500 10.5px/1 var(--mono);
  letter-spacing: .04em;
  text-transform: uppercase;
  color: var(--muted);
  text-align: center;
  transition: color .15s;
}
.tabs [aria-selected="true"] .tab-tile { box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--accent); }
.tabs [aria-selected="true"] .tab-label { color: var(--text); }
.tabs [role="tab"]:focus-visible { outline: none; }
.tabs [role="tab"]:focus-visible .tab-tile { outline: 2px solid var(--accent); outline-offset: 4px; }

.tab-glyph { font-size: 22px; line-height: 1; white-space: nowrap; }
#tab-style-glyph { font: 700 22px/1 var(--ui); padding: 3px 6px; border-radius: 5px; }
#tab-style-glyph[data-style="highlight"] { background: var(--tab-accent); }
#tab-style-glyph[data-style="outline"] { -webkit-text-stroke: 4px var(--tab-accent); paint-order: stroke fill; }
#tab-style-glyph[data-style="shadow"] { text-shadow: 1.5px 2px 2px var(--tab-accent); }
#tab-style-glyph[data-style="neon"] { text-shadow: 0 0 2px var(--tab-accent), 0 0 8px var(--tab-accent); }
.tab-dot { width: 22px; height: 22px; border-radius: 50%; background: var(--tab-fg); box-shadow: 0 0 0 1px rgba(23, 20, 31, .2); }

.panel { display: flex; flex-direction: column; gap: 20px; }
.panel .chips, .panel .swatches { flex-wrap: wrap; overflow: visible; margin: 0; padding: 4px 0; }
.panel .chip { font-size: 17px; }
/* Color picker: saturation/brightness square, hue strip, hex field */
.picker { display: flex; flex-direction: column; gap: 14px; --hue: 0; }
.sv {
  position: relative;
  height: 168px;
  border-radius: var(--radius);
  background:
    linear-gradient(to top, #000, transparent),
    linear-gradient(to right, #fff, hsl(var(--hue) 100% 50%));
  touch-action: none;
  cursor: crosshair;
}
.sv-thumb {
  position: absolute;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: 3px solid #fff;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, .25), 0 2px 6px rgba(0, 0, 0, .3);
  transform: translate(-50%, -50%);
  pointer-events: none;
}
.picker input.hue {
  -webkit-appearance: none;
  appearance: none;
  height: 28px;
  border-radius: 999px;
  background: linear-gradient(to right, #f00, #ff0, #0f0, #0ff, #00f, #f0f, #f00);
}
.picker input.hue::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: hsl(var(--hue) 100% 50%);
  border: 3px solid #fff;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, .25), 0 2px 6px rgba(0, 0, 0, .3);
}
.picker input.hue::-moz-range-thumb {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: hsl(var(--hue) 100% 50%);
  border: 3px solid #fff;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, .25), 0 2px 6px rgba(0, 0, 0, .3);
}
.hex {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 14px;
  height: 44px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-raised);
  font: 500 15px/1 var(--mono);
}
.hex:focus-within { border-color: var(--accent); }
.hex-dot { width: 20px; height: 20px; border-radius: 50%; box-shadow: inset 0 0 0 1px var(--swatch-ring); flex: none; }
.hex-hash { color: var(--muted); }
.hex input {
  flex: 1;
  min-width: 0;
  border: 0;
  outline: none;
  background: none;
  color: var(--text);
  font: inherit;
  font-size: 16px; /* >=16px prevents iOS zoom-on-focus */
  text-transform: uppercase;
  letter-spacing: .04em;
}
.sv:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.preset-groups { display: flex; flex-direction: column; gap: 16px; }
/* Presets panel */
.hint { margin: 0; font-size: 14px; line-height: 1.45; color: var(--muted); }
.field {
  width: 100%;
  height: 48px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-raised);
  color: var(--text);
  font: inherit;
  font-size: 17px; /* >=16px prevents iOS zoom-on-focus */
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}
.field:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent); }
.field::placeholder { color: var(--muted); }
.button {
  height: 48px;
  border: 0;
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font-weight: 600;
  font-size: 16px;
  transition: transform .1s, opacity .15s;
}
.button.primary { background: var(--accent); color: var(--on-accent); }
.button:active { transform: scale(.98); opacity: .85; }

.style-sliders { display: flex; flex-direction: column; gap: 20px; }
.style-sliders:not(:has(> :not([hidden]))) { display: none; }
/* Weight adjusts the selected font, so it sits apart from the font list. */
.weight-bar { padding-bottom: 20px; border-bottom: 1px solid var(--line); }
input[type="range"]:disabled { opacity: .4; }
.font-groups { display: flex; flex-direction: column; gap: 18px; }

.row { display: flex; flex-direction: column; gap: 9px; }

.label, .slider-head {
  font: 500 10.5px/1 var(--mono);
  letter-spacing: .04em;
  text-transform: uppercase;
  color: var(--muted);
}

.chips, .swatches {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  scrollbar-width: none;
  margin: 0 -16px;
  padding: 4px 16px;
  scroll-snap-type: x proximity;
  scroll-padding: 0 16px;
}
.chips::-webkit-scrollbar, .swatches::-webkit-scrollbar { display: none; }

.chip {
  flex: 0 0 auto;
  border: 1px solid var(--line);
  background: var(--surface-raised);
  border-radius: 999px;
  padding: 7px 15px;
  font-size: 18px;
  white-space: nowrap;
  scroll-snap-align: start;
  transition: background-color .15s, color .15s, border-color .15s;
}
.chip[aria-selected="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--on-accent);
}

.swatch {
  flex: 0 0 auto;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 0;
  box-shadow: inset 0 0 0 1px var(--swatch-ring);
  padding: 0;
  position: relative;
}
.swatch[aria-pressed="true"] { box-shadow: inset 0 0 0 1px var(--swatch-ring), 0 0 0 2px var(--bg), 0 0 0 4px var(--accent); }
.swatch.none {
  background: linear-gradient(135deg, transparent 46%, var(--muted) 46%, var(--muted) 54%, transparent 54%), var(--surface);
}
.swatch.custom {
  background: conic-gradient(#f87171, #facc15, #4ade80, #38bdf8, #a78bfa, #f472b6, #f87171);
  overflow: visible;
}
.swatch.custom:focus-within { outline: 2px solid var(--accent); outline-offset: 2px; }
.swatch.custom input {
  position: absolute;
  inset: 0;
  opacity: 0;
  width: 100%;
  height: 100%;
  border: 0;
  padding: 0;
  cursor: pointer;
}

.segmented {
  display: flex;
  background: var(--surface);
  border-radius: 11px;
  padding: 3px;
  gap: 2px;
}
.segmented button {
  flex: 1 1 0;
  border: 0;
  background: transparent;
  padding: 7px 4px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  color: var(--muted);
  display: grid;
  place-items: center;
  transition: background-color .15s, color .15s;
}
.segmented button[aria-pressed="true"] {
  background: var(--surface-raised);
  color: var(--text);
  box-shadow: var(--shadow);
}
.segmented.compact { align-self: flex-start; }
.segmented.compact button { width: 40px; height: 32px; padding: 0; }

.slider { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.slider-head { display: flex; justify-content: space-between; gap: 8px; }
.slider output { color: var(--text); font-variant-numeric: tabular-nums; text-transform: none; }
input[type="range"] { width: 100%; margin: 0; height: 32px; accent-color: var(--accent); }

/* Bottom bar: the four editing groups, then Save and Copy, in one horizontal scroller */
.bar {
  flex: 0 0 auto;
  display: flex;
  gap: 10px;
  overflow-x: auto;
  scrollbar-width: none;
  scroll-snap-type: x proximity;
  scroll-padding: 0 16px;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: var(--bg);
}
.bar::-webkit-scrollbar { display: none; }
.bar-action { min-width: 96px; }
.bar-action .tab-tile { background: var(--surface); color: var(--text); }
.bar-action.primary .tab-tile { background: var(--accent); color: var(--on-accent); }
.bar-action.primary .tab-label { color: var(--text); }
.bar-action .tab-tile { transition: transform .1s, opacity .15s; }
.bar-action:active .tab-tile { transform: scale(.96); opacity: .85; }
.bar-action:focus-visible { outline: none; }
.bar-action:focus-visible .tab-tile { outline: 2px solid var(--accent); outline-offset: 4px; }

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(100px + env(safe-area-inset-bottom));
  transform: translate(-50%, 12px);
  max-width: calc(100% - 32px);
  width: max-content;
  background: var(--text);
  color: var(--bg);
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 14px;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  box-shadow: 0 8px 24px rgba(23, 20, 31, .2);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: .01ms !important; }
  #canvas.lift { animation: none; }
}
