:root {
  --neutral-0: #f8faf7;
  --neutral-50: #eef0eb;
  --neutral-100: #d9ddd4;
  --neutral-200: #b8beb2;
  --neutral-300: #8f988b;
  --neutral-500: #555e56;
  --neutral-700: #2d312f;
  --neutral-800: #191b1a;
  --neutral-900: #0d0e0d;
  --neutral-950: #070807;
  --coral-400: #ff6b5f;
  --lime-300: #d8ff63;
  --lime-500: #9bd72f;
  --cyan-300: #7ee7d7;
  --amber-300: #ffd166;
  --danger: #ff8a7a;
  --bg: var(--neutral-950);
  --surface: #111211;
  --surface-2: #181918;
  --surface-3: #222420;
  --fg: var(--neutral-0);
  --fg-muted: #aeb4ac;
  --border: #343832;
  --border-strong: #565d54;
  --accent: var(--lime-300);
  --accent-2: var(--coral-400);
  --accent-fg: #10130b;
  --focus-ring: var(--cyan-300);
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;
  --space-7: 48px;
  --radius-sm: 4px;
  --radius-md: 6px;
  --radius-lg: 8px;
  --shadow-md: 0 18px 60px rgba(0, 0, 0, .35);
  --font-ui: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --font-meme: Impact, Haettenschweiler, "Arial Black", sans-serif;
  --text-xs: .75rem;
  --text-sm: .875rem;
  --text-base: 1rem;
  --text-md: 1.125rem;
  --text-lg: 1.35rem;
  --text-xl: 1.68rem;
  --text-2xl: 2.1rem;
  --leading-tight: 1.1;
  --leading-body: 1.5;
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  color-scheme: dark;
  background: var(--bg);
}

body {
  min-height: 100%;
  margin: 0;
  background:
    linear-gradient(180deg, rgba(216, 255, 99, .06), transparent 260px),
    var(--bg);
  color: var(--fg);
  font-family: var(--font-ui);
  line-height: var(--leading-body);
}

button,
textarea,
input {
  font: inherit;
}

button,
.file-btn {
  min-height: 44px;
}

button {
  cursor: pointer;
}

button:focus-visible,
textarea:focus-visible,
input:focus-visible,
.file-btn:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  max-width: 760px;
  font-size: clamp(1.72rem, 1.45rem + 1vw, 2.45rem);
  line-height: 1.02;
  letter-spacing: 0;
}

h2 {
  font-size: var(--text-lg);
  line-height: var(--leading-tight);
}

.app-shell {
  width: min(1680px, calc(100vw - 32px));
  margin: 0 auto;
  padding: var(--space-5) 0 var(--space-7);
}

.topbar,
.prompt-strip,
.template-section,
.stage-panel,
.control-panel,
.batch-drawer {
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  box-shadow: var(--shadow-md);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-5);
  padding: var(--space-4);
}

.eyebrow {
  margin-bottom: var(--space-2);
  color: var(--accent);
  font-size: var(--text-xs);
  font-weight: 800;
  letter-spacing: .12em;
  text-transform: uppercase;
}

.topbar-actions,
.prompt-actions,
.stage-actions,
.mode-group {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.btn,
.file-btn,
.mode-pill,
.text-btn,
.template-tile,
.variant-card {
  border-radius: var(--radius-md);
  transition: background .12s ease, border-color .12s ease, transform .04s ease, color .12s ease;
}

.btn,
.file-btn,
.mode-pill,
.text-btn {
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--fg);
  font-weight: 750;
}

.btn,
.file-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 var(--space-4);
  white-space: nowrap;
}

.btn:hover,
.file-btn:hover,
.mode-pill:hover,
.text-btn:hover {
  border-color: var(--border-strong);
  background: var(--surface-3);
}

.btn:active,
.file-btn:active,
.mode-pill:active,
.text-btn:active,
.template-tile:active,
.variant-card:active {
  transform: translateY(1px);
}

.btn-primary {
  border-color: color-mix(in srgb, var(--accent) 70%, transparent);
  background: var(--accent);
  color: var(--accent-fg);
}

.btn-primary:hover {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 84%, white);
}

.btn-secondary {
  color: var(--fg);
}

.prompt-strip {
  margin-top: var(--space-3);
  padding: var(--space-3);
}

.prompt-label {
  display: block;
  margin-bottom: var(--space-2);
  color: var(--fg-muted);
  font-size: var(--text-sm);
  font-weight: 750;
}

.prompt-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: var(--space-3);
  align-items: stretch;
}

textarea {
  width: 100%;
  min-height: 84px;
  resize: vertical;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: #0b0c0b;
  color: var(--fg);
  padding: var(--space-3);
}

.template-section {
  margin-top: var(--space-3);
  padding: var(--space-4);
}

.gallery-note {
  margin-top: var(--space-3);
  padding: 0 var(--space-1);
  color: var(--fg-muted);
  font-size: var(--text-sm);
  font-weight: 650;
}

.editor-view {
  margin-top: var(--space-4);
}

.editor-bar {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: var(--space-4);
  margin-bottom: var(--space-3);
  padding: 0 var(--space-1);
}

.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
  margin-bottom: var(--space-3);
}

.section-head.compact {
  margin-bottom: var(--space-2);
}

.mode-group {
  flex-wrap: wrap;
}

.mode-pill {
  min-width: 64px;
  padding: 0 var(--space-3);
}

.mode-pill.is-active {
  border-color: var(--accent);
  background: rgba(216, 255, 99, .16);
  color: var(--accent);
}

.template-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(92px, 1fr));
  gap: var(--space-3);
}

.template-tile {
  position: relative;
  display: grid;
  min-height: 112px;
  padding: var(--space-2);
  border: 1px solid var(--border);
  background: #101110;
  color: var(--fg);
  place-items: center;
}

.template-tile:hover {
  border-color: var(--border-strong);
  background: #171916;
}

.template-tile.is-active {
  border-color: var(--accent);
  box-shadow: inset 0 0 0 1px var(--accent);
}

.template-tile img,
.template-tile canvas {
  width: 100%;
  max-width: 90px;
  aspect-ratio: 1;
  border-radius: var(--radius-sm);
  object-fit: cover;
}

.template-tile span {
  position: absolute;
  right: var(--space-2);
  bottom: var(--space-2);
  left: var(--space-2);
  overflow: hidden;
  color: var(--fg);
  font-size: .68rem;
  font-weight: 850;
  line-height: 1.05;
  text-overflow: ellipsis;
  text-shadow: 0 1px 8px #000;
  white-space: nowrap;
}

.template-tile em {
  position: absolute;
  top: var(--space-2);
  right: var(--space-2);
  border: 1px solid rgba(216, 255, 99, .55);
  border-radius: var(--radius-sm);
  background: rgba(7, 8, 7, .78);
  color: var(--accent);
  padding: 2px 5px;
  font-size: .58rem;
  font-style: normal;
  font-weight: 900;
  letter-spacing: .08em;
  text-transform: uppercase;
}

.template-tile.random {
  font-size: var(--text-sm);
  font-weight: 850;
}

.random-mark {
  display: grid;
  width: 52px;
  height: 52px;
  margin-bottom: var(--space-2);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  color: var(--accent);
  font-size: 1.6rem;
  place-items: center;
}

.workbench {
  display: grid;
  grid-template-columns: minmax(0, 1.35fr) minmax(360px, .65fr);
  gap: var(--space-4);
  margin-top: var(--space-3);
}

.stage-panel,
.control-panel {
  min-width: 0;
  padding: var(--space-4);
}

.stage-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
  margin-bottom: var(--space-3);
}

.score {
  display: grid;
  width: 76px;
  height: 64px;
  border: 1px solid color-mix(in srgb, var(--accent-2) 55%, var(--border));
  border-radius: var(--radius-md);
  background: rgba(255, 107, 95, .12);
  color: var(--accent-2);
  line-height: 1;
  place-items: center;
}

.score span {
  font-size: var(--text-xl);
  font-weight: 900;
}

.score small {
  color: var(--fg-muted);
  font-size: var(--text-xs);
  font-weight: 800;
  text-transform: uppercase;
}

.canvas-frame {
  display: grid;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: #030303;
  place-items: center;
}

#memeCanvas {
  display: block;
  width: min(100%, 820px);
  height: auto;
  aspect-ratio: 1;
}

.stage-actions {
  flex-wrap: wrap;
  margin-top: var(--space-3);
}

.file-btn {
  cursor: pointer;
}

.control-panel {
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
}

.panel-section {
  min-width: 0;
}

.caption-list,
.editor-fields,
.variant-grid {
  display: grid;
  gap: var(--space-2);
}

.caption-card,
.edit-field {
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: #101110;
}

.caption-card {
  display: grid;
  width: 100%;
  min-height: 64px;
  padding: var(--space-3);
  color: var(--fg);
  text-align: left;
}

.caption-card:hover {
  border-color: var(--accent);
  background: rgba(216, 255, 99, .08);
}

.caption-card strong {
  font-size: var(--text-sm);
}

.caption-card span {
  color: var(--fg-muted);
  font-size: var(--text-xs);
}

.edit-field {
  display: grid;
  gap: var(--space-2);
  padding: var(--space-3);
}

.edit-field label,
.range-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  color: var(--fg-muted);
  font-size: var(--text-sm);
  font-weight: 750;
}

.edit-field textarea {
  min-height: 68px;
}

.text-btn {
  min-height: 44px;
  padding: 0 var(--space-3);
  color: var(--accent);
}

input[type="range"] {
  width: 100%;
  min-height: 44px;
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  accent-color: var(--accent);
}

input[type="range"]:focus-visible {
  border-color: var(--focus-ring);
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}

.file-input {
  display: none;
}

.batch-drawer {
  margin-top: var(--space-4);
  padding: var(--space-4);
}

.variant-grid {
  grid-template-columns: repeat(auto-fill, minmax(158px, 1fr));
}

.variant-card {
  display: grid;
  gap: var(--space-2);
  padding: var(--space-2);
  border: 1px solid var(--border);
  background: #101110;
  color: var(--fg);
  text-align: left;
}

.variant-card:hover {
  border-color: var(--accent);
}

.variant-card canvas {
  width: 100%;
  aspect-ratio: 1;
  border-radius: var(--radius-sm);
}

.variant-card span {
  color: var(--fg-muted);
  font-size: var(--text-xs);
  line-height: 1.25;
}

.toast {
  position: fixed;
  right: var(--space-4);
  bottom: var(--space-4);
  z-index: 20;
  max-width: min(360px, calc(100vw - 32px));
  transform: translateY(20px);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--neutral-0);
  color: var(--neutral-950);
  opacity: 0;
  padding: var(--space-3) var(--space-4);
  font-weight: 850;
  pointer-events: none;
  transition: opacity .15s ease, transform .15s ease;
}

.toast.is-visible {
  transform: translateY(0);
  opacity: 1;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

@media (max-width: 980px) {
  .app-shell {
    width: min(100% - 24px, 760px);
  }

  .topbar,
  .prompt-row,
  .workbench {
    grid-template-columns: 1fr;
  }

  .topbar {
    display: grid;
    align-items: start;
  }

  .topbar-actions,
  .prompt-actions {
    justify-content: stretch;
  }

  .topbar-actions .btn,
  .prompt-actions .btn {
    flex: 1;
  }

  .prompt-actions {
    flex-wrap: wrap;
  }

  .template-grid {
    grid-template-columns: repeat(auto-fill, minmax(82px, 1fr));
  }
}

@media (max-width: 620px) {
  .app-shell {
    width: min(100% - 16px, 520px);
    padding-top: var(--space-3);
  }

  .topbar,
  .prompt-strip,
  .template-section,
  .stage-panel,
  .control-panel,
  .batch-drawer {
    padding: var(--space-3);
  }

  .section-head,
  .stage-toolbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .mode-group,
  .stage-actions {
    width: 100%;
  }

  .mode-pill,
  .stage-actions .btn,
  .stage-actions .file-btn {
    flex: 1;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: .001ms !important;
    animation-iteration-count: 1 !important;
    scroll-behavior: auto !important;
    transition-duration: .001ms !important;
  }
}
