:root {
  color-scheme: light dark;

  --bg: #fafafa;
  --bg-secondary: #f4f4f5;
  --surface: #ffffff;
  --border: #d4d4d8;
  --text: #18181b;
  --text-secondary: #52525b;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --checker: #e4e4e7;
  --shadow: rgba(0, 0, 0, 0.12);
  --overlay: rgba(37, 99, 235, 0.12);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #09090b;
    --bg-secondary: #18181b;
    --surface: #18181b;
    --border: #3f3f46;
    --text: #fafafa;
    --text-secondary: #a1a1aa;
    --accent: #60a5fa;
    --accent-text: #09090b;
    --checker: #27272a;
    --shadow: rgba(0, 0, 0, 0.5);
    --overlay: rgba(96, 165, 250, 0.15);
  }
}

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

[hidden] {
  display: none !important;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  padding: 1rem;
  line-height: 1.5;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.container {
  width: 100%;
  max-width: 72rem;
  margin-inline: auto;
}

header {
  margin-bottom: 1rem;

  & h1 {
    font-family: Georgia, "Times New Roman", Times, serif;
    font-size: 1.75rem;
    line-height: 1.2;
    letter-spacing: -0.01em;
  }

  & .subtitle {
    color: var(--text-secondary);
    font-size: 0.875rem;
  }
}

.layout {
  display: grid;
  gap: 1.25rem;
}

/* ---------- preview ---------- */

.preview-pane {
  display: grid;
  gap: 0.75rem;
  align-content: start;
}

.preview-frame {
  --ratio: 1;
  aspect-ratio: var(--ratio);
  width: min(100%, 70dvh * var(--ratio));
  margin-inline: auto;
  border-radius: 0.5rem;
  overflow: hidden;
  box-shadow: 0 8px 30px var(--shadow);
  background: repeating-conic-gradient(var(--checker) 0 25%, transparent 0 50%) 0 0 / 16px 16px;

  & canvas {
    display: block;
    width: 100%;
    height: 100%;
  }
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  justify-content: center;
}

.status {
  min-height: 1.5em;
  text-align: center;
  font-size: 0.875rem;
  color: var(--text-secondary);
}

.btn {
  font: inherit;
  font-size: 0.9375rem;
  font-weight: 500;
  padding: 0.5rem 1rem;
  border-radius: 0.375rem;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;

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

  &.primary {
    background: var(--accent);
    border-color: var(--accent);
    color: var(--accent-text);
  }

  &.small {
    font-size: 0.8125rem;
    padding: 0.25rem 0.625rem;
  }
}

/* ---------- controls ---------- */

.controls {
  display: grid;
  gap: 1rem;
  align-content: start;
}

.row {
  display: grid;
  gap: 1rem;
}

.field {
  display: grid;
  gap: 0.375rem;
  border: 0;
  min-width: 0;

  & > span, & > legend {
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 0.375rem;
  }

  & > span { margin-bottom: 0; }

  & input[type="text"], & textarea {
    font: inherit;
    width: 100%;
    padding: 0.5rem 0.625rem;
    border: 1px solid var(--border);
    border-radius: 0.375rem;
    background: var(--surface);
    color: var(--text);

    &:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
  }

  & textarea {
    resize: vertical;
    min-height: 6rem;
  }

  & .hint {
    font-size: 0.75rem;
    color: var(--text-secondary);
    line-height: 1.8;

    & code {
      font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
      font-size: 0.6875rem;
      padding: 0.0625rem 0.3125rem;
      border-radius: 0.25rem;
      background: var(--bg-secondary);
      border: 1px solid var(--border);
      white-space: nowrap;
    }
  }
}

.accent-field input[type="color"] {
  width: 3rem;
  height: 2.25rem;
  padding: 0.125rem;
  border: 1px solid var(--border);
  border-radius: 0.375rem;
  background: var(--surface);
  cursor: pointer;
}

/* avatar */

.avatar-row {
  display: flex;
  gap: 0.875rem;
  align-items: center;
}

.avatar-drop {
  position: relative;
  flex: none;
  width: 4rem;
  height: 4rem;
  border-radius: 50%;
  border: 2px dashed var(--border);
  display: grid;
  place-items: center;
  cursor: pointer;
  overflow: hidden;
  background: var(--bg-secondary);

  &:hover, &:focus-within { border-color: var(--accent); }

  & img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }

  & .avatar-empty {
    font-size: 1.75rem;
    color: var(--text-secondary);
    line-height: 1;
  }

  &:has(img:not([hidden])) { border-style: solid; }
}

.avatar-help {
  display: grid;
  gap: 0.375rem;
  justify-items: start;
  font-size: 0.8125rem;
  color: var(--text-secondary);
}

/* style picker */

.styles {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.5rem;
}

.style-option {
  display: grid;
  gap: 0.25rem;
  text-align: center;
  font-size: 0.8125rem;
  cursor: pointer;
  color: var(--text-secondary);

  & canvas {
    width: 100%;
    aspect-ratio: 1;
    border-radius: 0.375rem;
    border: 2px solid transparent;
    outline: 1px solid var(--border);
    transition: transform 0.15s;
  }

  &:hover canvas { transform: translateY(-2px); }

  &:has(input:checked) {
    color: var(--text);
    font-weight: 600;

    & canvas { border-color: var(--accent); outline-color: var(--accent); }
  }

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

/* size segmented control */

.segmented {
  display: flex;
  flex-wrap: wrap;
  border: 1px solid var(--border);
  border-radius: 0.375rem;
  overflow: hidden;
  width: fit-content;
  max-width: 100%;

  & label {
    cursor: pointer;
    font-size: 0.875rem;

    & span {
      display: block;
      padding: 0.375rem 0.75rem;
    }

    & + label { border-left: 1px solid var(--border); }

    &:has(input:checked) span {
      background: var(--accent);
      color: var(--accent-text);
    }

    &:has(input:focus-visible) span { outline: 2px solid var(--accent); outline-offset: -4px; }
  }
}

/* drag and drop */

.drop-overlay {
  position: fixed;
  inset: 0.5rem;
  display: grid;
  place-items: center;
  border: 3px dashed var(--accent);
  border-radius: 1rem;
  background: var(--overlay);
  backdrop-filter: blur(2px);
  font-weight: 600;
  font-size: 1.125rem;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s;

  & span {
    background: var(--surface);
    padding: 0.75rem 1.25rem;
    border-radius: 0.5rem;
  }
}

.dragover .drop-overlay { opacity: 1; }

@media (min-width: 480px) {
  .row { grid-template-columns: 1fr 1fr; }
  .row:has(.accent-field) { grid-template-columns: 1fr auto; }
}

@media (min-width: 640px) {
  body { padding: 2rem; }
  .styles { grid-template-columns: repeat(6, 1fr); }
}

@media (min-width: 960px) {
  .layout {
    grid-template-columns: minmax(0, 1.25fr) minmax(0, 1fr);
    align-items: start;
  }

  .preview-pane {
    position: sticky;
    top: 1rem;
  }

  .styles { grid-template-columns: repeat(3, 1fr); }
}
