:root {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #1f232c;
  --border: #2a2f3a;
  --text: #e8eaf0;
  --muted: #8b93a7;
  --accent: #ffcc00;
  --accent-text: #111;
  --danger: #ff5c5c;
  --radius: 10px;
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 100vh;
}

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

.muted { color: var(--muted); }
.small { font-size: 13px; }
.spacer { flex: 1; }

/* Header */
.topbar {
  display: flex;
  align-items: center;
  gap: 24px;
  flex-wrap: wrap;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}

.brand {
  margin: 0;
  font-family: Anton, Impact, sans-serif;
  font-size: 26px;
  letter-spacing: 0.5px;
  text-transform: uppercase;
}
.brand span { color: var(--accent); }

.tabs { display: flex; gap: 4px; }

.tab {
  background: none;
  border: 1px solid transparent;
  border-radius: 999px;
  padding: 6px 14px;
  cursor: pointer;
  color: var(--muted);
}
.tab:hover { color: var(--text); }
.tab.is-active {
  color: var(--accent-text);
  background: var(--accent);
  font-weight: 600;
}

/* Layout */
.layout {
  display: grid;
  grid-template-columns: 300px minmax(0, 1fr) 300px;
  gap: 16px;
  padding: 16px;
  align-items: start;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  min-width: 0;
}

.panel h2 {
  margin: 0 0 10px;
  font-size: 16px;
}

@media (max-width: 1100px) {
  .layout { grid-template-columns: 280px minmax(0, 1fr); }
  .props { grid-column: 1 / -1; }
}

@media (max-width: 760px) {
  .layout { grid-template-columns: minmax(0, 1fr); padding: 12px; gap: 12px; }
  /* Before a picture is chosen, the template list comes first... */
  .source { order: 1; }
  .stage-panel { order: 2; }
  .props { order: 3; }
  /* ...after that, the meme and its text settings come first. */
  .has-image .stage-panel { order: 1; }
  .has-image .props { order: 2; }
  .has-image .source { order: 3; }

  .topbar { gap: 8px 12px; padding: 10px 12px; }
  .brand { font-size: 22px; }
  .tabs { order: 3; width: 100%; }
  .tab { flex: 1; }
  .layout .stage { min-height: 200px; }
  .stage canvas { max-height: 60vh; }
}

/* Source panel */
.tab-panel { display: none; }
.tab-panel.is-active { display: block; }

.search-row {
  display: flex;
  gap: 6px;
}

.search-row input,
.search-row select,
.field textarea,
.field select {
  width: 100%;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
}
/* After the shared width: 100% rule so the dropdown keeps its natural width. */
.search-row > input { flex: 1; min-width: 0; }
.search-row > select { flex: 0 0 auto; width: auto; max-width: 45%; }

.search-row input:focus,
.search-row select:focus,
.field textarea:focus,
.field select:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}

.template-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(84px, 1fr));
  gap: 8px;
  max-height: calc(100vh - 210px);
  overflow-y: auto;
  padding-right: 2px;
}

@media (max-width: 760px) {
  .template-grid { max-height: 360px; }
}

.template {
  position: relative;
  aspect-ratio: 1;
  padding: 0;
  border: 2px solid transparent;
  border-radius: 8px;
  overflow: hidden;
  background: var(--panel-2);
  cursor: pointer;
}
.template:hover,
.template:focus-visible { border-color: var(--accent); outline: none; }
.template img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.template span {
  position: absolute;
  inset: auto 0 0 0;
  padding: 14px 4px 3px;
  font-size: 11px;
  line-height: 1.2;
  text-align: left;
  color: #fff;
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.85));
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.drop-zone {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  text-align: center;
  min-height: 200px;
  padding: 20px;
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
.drop-zone:hover,
.drop-zone.is-over {
  border-color: var(--accent);
  background: rgba(255, 204, 0, 0.06);
}

/* Caption ideas */
.ideas {
  margin-top: 8px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
}
.ideas-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.ideas-head h2 { margin: 0; font-size: 16px; }
.ideas-head .btn { padding: 6px 12px; font-size: 13px; }
#ideasStatus { margin: 6px 0 10px; }

.idea-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 8px;
  max-height: 260px;
  overflow-y: auto;
}

.idea {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  gap: 2px;
  text-align: left;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
}
.idea span + span { color: var(--muted); font-weight: 500; }
.idea:hover, .idea:focus-visible { border-color: var(--muted); outline: none; }
.idea.is-active { border-color: var(--accent); }

/* Typing on the image */
.text-editor {
  position: absolute;
  z-index: 2;
  margin: 0;
  padding: 0;
  border: 2px solid var(--accent);
  border-radius: 2px;
  background: rgba(0, 0, 0, 0.15);
  overflow: hidden;
  resize: none;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  transform-origin: center;
  caret-color: var(--accent);
  cursor: text;
  outline: none;
  animation: editor-glow 1.2s ease-in-out infinite alternate;
}
@keyframes editor-glow {
  from { box-shadow: 0 0 0 0 rgba(255, 204, 0, 0.6); }
  to { box-shadow: 0 0 0 4px rgba(255, 204, 0, 0.15); }
}
@media (prefers-reduced-motion: reduce) {
  .text-editor { animation: none; }
}
.text-editor[hidden] { display: none; }

/* Stage */
.stage canvas[hidden] { display: none; }
.stage {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 360px;
  background:
    repeating-conic-gradient(#1b1e25 0% 25%, #15181e 0% 50%) 50% / 24px 24px;
  border-radius: 8px;
  overflow: hidden;
}

.stage canvas {
  display: block;
  max-width: 100%;
  max-height: calc(100vh - 190px);
  max-height: calc(100svh - 190px);
  touch-action: none;
  cursor: default;
}

.empty-state {
  padding: 40px 20px;
  text-align: center;
  color: var(--muted);
}

.stage-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 12px;
}

.toast {
  min-height: 1.4em;
  margin: 8px 0 0;
  font-size: 13px;
  color: var(--accent);
}

/* Buttons */
.btn {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 14px;
  cursor: pointer;
}
.btn:hover:not(:disabled) { border-color: var(--muted); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
}
.btn-primary:hover:not(:disabled) { filter: brightness(1.08); border-color: var(--accent); }
.btn-danger { color: var(--danger); }
.btn-danger:hover:not(:disabled) { border-color: var(--danger); }

/* Props */
.layer-list {
  list-style: none;
  margin: 0 0 12px;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.layer-list button {
  width: 100%;
  text-align: left;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 10px;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.layer-list button.is-selected { border-color: var(--accent); }

.props-form { display: flex; flex-direction: column; gap: 12px; }

.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; flex: 1; }
.field > span { font-size: 13px; color: var(--muted); }
.field output { color: var(--text); margin-left: 4px; }
.field textarea { resize: vertical; }
.field input[type="range"] { width: 100%; accent-color: var(--accent); }

.field-row { display: flex; gap: 10px; align-items: flex-end; }

.color-field { flex: 0 0 auto; }
.color-field input {
  width: 44px;
  height: 34px;
  padding: 2px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
}

.check {
  flex-direction: row;
  align-items: center;
  gap: 6px;
  padding-bottom: 6px;
  cursor: pointer;
}
.check > span { color: var(--text); }
.check input { accent-color: var(--accent); }

.segmented {
  display: flex;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}
.segmented button {
  flex: 1;
  background: var(--panel-2);
  border: 0;
  padding: 6px 4px;
  cursor: pointer;
  font-size: 13px;
}
.segmented button + button { border-left: 1px solid var(--border); }
.segmented button.is-active { background: var(--accent); color: var(--accent-text); font-weight: 600; }

/* Ads: kept clear of buttons so nobody clicks one by accident */
.ad-slot {
  margin: 16px 0 0;
  padding-top: 8px;
  border-top: 1px solid var(--border);
  min-height: 100px;
  overflow: hidden;
}
.ad-footer { margin: 0 16px 16px; }
.ad-label {
  display: block;
  margin-bottom: 4px;
  font-size: 11px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

/* Footer */
.site-footer {
  margin: 0 16px 24px;
  padding: 20px 16px;
  border-top: 1px solid var(--border);
  color: var(--muted);
}
.site-about { max-width: 820px; }
.site-about h2 { margin: 0 0 8px; font-size: 16px; color: var(--text); }
.site-about p { margin: 0 0 8px; font-size: 14px; }
.footer-links { display: flex; flex-wrap: wrap; gap: 8px 20px; margin: 14px 0 8px; }
.footer-links a, .page a { color: var(--accent); }

/* Content pages (About, Privacy, Terms, Contact) */
.page {
  max-width: 760px;
  margin: 24px auto;
  padding: 0 16px 32px;
  line-height: 1.6;
}
.page h1 { font-size: 28px; margin: 0 0 4px; }
.page h2 { font-size: 19px; margin: 28px 0 8px; }
.page p, .page li { color: var(--text); }
.page ul { padding-left: 20px; }
.page .updated { color: var(--muted); font-size: 14px; margin-bottom: 20px; }
.brand a { color: inherit; text-decoration: none; }
.back-link { margin-left: auto; color: var(--accent); }

/* Header links on content pages and the main page */
.top-links { display: flex; align-items: center; gap: 14px; margin-left: auto; }
.top-links a:not(.btn) { color: var(--text); }
.top-links .btn { text-decoration: none; }
.top-template-link { margin-left: auto; color: var(--muted); }
.top-template-link:hover { color: var(--text); }

/* Template pages */
.breadcrumbs ol {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  list-style: none;
  margin: 0 0 16px;
  padding: 0;
  font-size: 14px;
  color: var(--muted);
}
.breadcrumbs li + li::before { content: "›"; margin-right: 6px; }
.byline { color: var(--muted); font-size: 14px; margin: 4px 0 16px; }

/* Fixed box so the image loading in never shifts the page */
.template-figure { margin: 0 0 12px; }
.template-figure img {
  display: block;
  width: 100%;
  height: auto;
  max-height: 70vh;
  aspect-ratio: 4 / 3;
  object-fit: contain;
  background: var(--panel-2);
  border-radius: var(--radius);
}
.template-figure figcaption { margin-top: 6px; }
.make-btn { display: inline-block; text-decoration: none; }

.caption-ideas li { margin-bottom: 6px; }
.faq dt { font-weight: 700; margin-top: 14px; }
.faq dd { margin: 4px 0 0; color: var(--text); }
.site-footer .faq dd { color: var(--muted); }

.related-links, .template-index, .home-template-links {
  list-style: none;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 6px 16px;
}
.home-template-links a, .footer-links a { color: var(--accent); }
.home-template-links { font-size: 14px; }
.page a.btn-primary { color: var(--accent-text); text-decoration: none; }

/* Adding text boxes */
#addTextBtn:not(:disabled) { border-color: var(--accent); color: var(--accent); font-weight: 600; }
.props-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 10px; }
.props-head h2 { margin: 0; }
.props-head .btn { padding: 6px 12px; font-size: 13px; }

/* Contact form */
.contact-form {
  display: grid;
  gap: 14px;
  margin: 20px 0 28px;
  padding: 18px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.contact-form .field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; align-items: start; }
.contact-form input[type="text"],
.contact-form input[type="email"],
.contact-form select,
.contact-form textarea {
  width: 100%;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
}
.contact-form input:focus, .contact-form select:focus, .contact-form textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}
.contact-form [aria-invalid="true"] { border-color: var(--danger); }
.contact-form button { justify-self: start; }
.contact-form button[disabled] { opacity: 0.6; cursor: progress; }
.form-status { margin: 0; min-height: 1.4em; }
.form-status[data-type="ok"] { color: #4ade80; }
.form-status[data-type="error"] { color: var(--danger); }
.hp { position: absolute; left: -9999px; width: 1px; height: 1px; opacity: 0; }
@media (max-width: 560px) {
  .contact-form .field-row { grid-template-columns: 1fr; }
}
