*,
*::before,
*::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --bg: #323437;
  --bg-secondary: #2c2e31;
  --surface: #3a3c3f;
  --border: #4a4c4f;
  --text: #d1d0c5;
  --text-muted: #646669;
  --accent: #e2b714;
  --accent-dim: rgba(226, 183, 20, 0.15);
  --radius: 12px;
  --radius-sm: 8px;
  --font: "Lexend Deca", "Inter", system-ui, -apple-system, sans-serif;
  --font-mono: "Roboto Mono", "SF Mono", "Fira Code", monospace;
}

body.light {
  --bg: #e5e5e0;
  --bg-secondary: #d6d6d1;
  --surface: #cdcdc8;
  --border: #b8b8b3;
  --text: #323437;
  --text-muted: #959896;
  --accent: #e2b714;
  --accent-dim: rgba(226, 183, 20, 0.2);
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.5;
}

header {
  text-align: center;
  padding: 3rem 1rem 1.5rem;
  position: relative;
}

#themeToggle {
  position: absolute;
  top: 1.5rem;
  right: 1.5rem;
  padding: 0.5rem;
  border-radius: 50%;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  cursor: pointer;
  line-height: 0;
}

#themeToggle .icon-sun,
#themeToggle .icon-moon {
  display: none;
}

#themeToggle .icon-sun {
  display: block;
}
#themeToggle .icon-moon {
  display: none;
}

body.light #themeToggle .icon-sun {
  display: none;
}
body.light #themeToggle .icon-moon {
  display: block;
}

header h1 {
  font-size: 2rem;
  font-weight: 300;
  letter-spacing: 0.04em;
  color: var(--text);
}

header .accent {
  color: var(--accent);
  font-weight: 600;
}

header .subtitle {
  color: var(--text-muted);
  font-size: 0.85rem;
  margin-top: 0.25rem;
  letter-spacing: 0.02em;
}

main {
  max-width: 960px;
  margin: 0 auto;
  padding: 0 1.5rem 3rem;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

/* --- Controls --- */

.controls {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.field label {
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--text-muted);
  text-transform: lowercase;
  letter-spacing: 0.06em;
}

.field-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}

input,
select {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.65rem 0.85rem;
  font-size: 0.9rem;
  font-family: var(--font-mono);
  outline: none;
  transition: border-color 0.15s;
}

input:focus,
select:focus {
  border-color: var(--accent);
}

input::placeholder {
  color: var(--text-muted);
}

select {
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%23646669' stroke-width='2'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.75rem center;
  padding-right: 2rem;
}

/* --- Theme grid --- */

/* Stretch both children to the same height so the button matches the input */
.theme-search-wrap {
  margin-bottom: 0.5rem;
  display: flex;
  align-items: stretch;
  gap: 0.5rem;
}

#themeSearch {
  flex: 1;
}

/* Square button that matches the input height exactly.
   The input has padding 0.65rem top+bottom + font-size 0.9rem + 2px border = total height.
   We mirror the same padding/font-size so the browser computes the same height,
   then force square with aspect-ratio. */
#randomTheme {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0.65rem;
  font-size: 0.9rem;
  aspect-ratio: 1;
  flex-shrink: 0;
}

#randomTheme svg {
  width: 16px;
  height: 16px;
  opacity: 0.7;
}

.theme-grid {
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-height: 320px;
  overflow-y: auto;
  padding-right: 4px;
}

.theme-grid::-webkit-scrollbar {
  width: 6px;
}

.theme-grid::-webkit-scrollbar-track {
  background: transparent;
}

.theme-grid::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}

.theme-btn {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 6px 10px;
  border-radius: 6px;
  border: 2px solid transparent;
  background: none;
  cursor: pointer;
  transition:
    background 0.12s,
    border-color 0.12s;
  min-height: 32px;
}

.theme-btn:hover {
  background: var(--surface);
}

.theme-btn.active {
  border-color: var(--accent);
  background: var(--surface);
}

.theme-colors {
  display: flex;
  gap: 0;
  width: 100%;
  height: 10px;
  border-radius: 3px;
  overflow: hidden;
  flex: 1;
}

.theme-colors span {
  flex: 1;
  display: block;
}

.theme-name {
  font-size: 0.7rem;
  font-family: var(--font-mono);
  color: var(--text-muted);
  white-space: nowrap;
  min-width: 110px;
  flex-shrink: 0;
}

.theme-btn.active .theme-name {
  color: var(--accent);
}

/* --- Preview & output --- */

.output-section {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.preview-card {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 200px;
}

.preview-card img {
  max-width: 100%;
  border-radius: var(--radius-sm);
}

.actions {
  display: flex;
  gap: 0.5rem;
}

button {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.6rem 1rem;
  font-size: 0.8rem;
  font-family: var(--font);
  font-weight: 500;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition:
    background 0.15s,
    border-color 0.15s;
}

button:hover {
  background: var(--border);
  border-color: var(--text-muted);
}

button svg {
  flex-shrink: 0;
  opacity: 0.7;
}

.url-output textarea {
  width: 100%;
  min-height: 60px;
  background: var(--surface);
  color: var(--accent);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.75rem;
  font-size: 0.8rem;
  font-family: var(--font-mono);
  resize: vertical;
  outline: none;
}

.url-output textarea:focus {
  border-color: var(--accent);
}

.url-output textarea::placeholder {
  color: var(--text-muted);
}

/* --- Toast --- */

.toast {
  position: fixed;
  bottom: 1.5rem;
  left: 50%;
  transform: translateX(-50%) translateY(120%);
  background: var(--accent);
  color: var(--bg);
  padding: 0.5rem 1.25rem;
  border-radius: 999px;
  font-size: 0.8rem;
  font-weight: 600;
  pointer-events: none;
  opacity: 0;
  transition:
    transform 0.25s ease,
    opacity 0.25s ease;
  z-index: 100;
}

.toast.show {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}

/* --- Loading & error states for preview --- */

.preview-card.loading {
  position: relative;
}

.preview-card.loading::after {
  content: "loading...";
  position: absolute;
  color: var(--text-muted);
  font-size: 0.85rem;
  font-family: var(--font-mono);
  letter-spacing: 0.05em;
}

.preview-card.loading img {
  opacity: 0;
}

.preview-card.error {
  position: relative;
}

.preview-card.error img {
  display: none;
}

.preview-card.error::after {
  content: attr(data-error);
  position: absolute;
  color: var(--text-muted);
  font-size: 0.85rem;
  font-family: var(--font-mono);
}

/* --- Footer --- */

footer {
  text-align: center;
  padding: 1.5rem 1rem 2.5rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.4rem;
}

footer a {
  color: var(--text-muted);
  text-decoration: none;
  font-size: 0.8rem;
  font-family: var(--font-mono);
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  transition: color 0.15s;
}

footer a:hover {
  color: var(--accent);
}

footer .footer-note {
  color: var(--text-muted);
  font-size: 0.7rem;
  opacity: 0.5;
}

body.light select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%23959896' stroke-width='2'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
}

/* --- Responsive --- */

@media (max-width: 600px) {
  header h1 {
    font-size: 1.5rem;
  }

  main {
    padding: 0 1rem 2rem;
  }

  .controls {
    padding: 1rem;
  }

  .theme-grid {
    grid-template-columns: repeat(auto-fill, minmax(90px, 1fr));
    max-height: 200px;
  }

  .field-row {
    grid-template-columns: 1fr 1fr;
  }

  .actions {
    flex-wrap: wrap;
  }
}
