:root {
  color-scheme: light dark;

  --bg: #fafafa;
  --bg-secondary: #f4f4f5;
  --bg-tertiary: #e4e4e7;
  --bg-stage: #e4e4e7;
  --border: #d4d4d8;
  --text: #18181b;
  --text-secondary: #52525b;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --accent-light: #dbeafe;
  --accent-text: #ffffff;
  --danger: #dc2626;
  --shadow: rgba(0, 0, 0, 0.12);

  --radius: 0.5rem;
  --stage-max-h: 45dvh;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #09090b;
    --bg-secondary: #18181b;
    --bg-tertiary: #27272a;
    --bg-stage: #111113;
    --border: #3f3f46;
    --text: #fafafa;
    --text-secondary: #a1a1aa;
    --accent: #60a5fa;
    --accent-hover: #93c5fd;
    --accent-light: #1e3a5f;
    --accent-text: #09090b;
    --danger: #f87171;
    --shadow: rgba(0, 0, 0, 0.5);
  }
}

*, *::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;
  line-height: 1.5;
}

/* Drop overlay */
body.dragover::after {
  content: "Drop your image";
  position: fixed;
  inset: 1rem;
  border: 3px dashed var(--accent);
  background: color-mix(in srgb, var(--accent-light) 85%, transparent);
  color: var(--accent);
  font-size: 1.25rem;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 0.75rem;
  z-index: 1000;
  pointer-events: none;
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  position: sticky;
  top: 0;
  z-index: 10;

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

  & .header-actions {
    display: flex;
    gap: 0.5rem;
  }
}

/* Buttons */
.btn {
  font: inherit;
  font-size: 0.875rem;
  font-weight: 500;
  padding: 0.45rem 0.85rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--bg-secondary);
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;

  &:hover { background: var(--bg-tertiary); }
  &:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
  &:disabled { opacity: 0.5; cursor: not-allowed; }
}

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

  &:hover { background: var(--accent-hover); }
}

.btn-small {
  font-size: 0.8rem;
  padding: 0.3rem 0.65rem;
}

.btn-danger {
  color: var(--danger);
}

/* Layout: mobile first — preview stacked above controls */
.layout {
  display: grid;
  gap: 1rem;
  padding: 1rem;
}

.stage {
  position: sticky;
  top: 3.6rem;
  z-index: 5;
  background: var(--bg-stage);
  border-radius: var(--radius);
  padding: 0.75rem;
  margin-inline: -0.25rem;
}

.canvas-wrap {
  --ratio: 0.8;
  position: relative;
  aspect-ratio: var(--ratio);
  width: min(100%, calc(var(--stage-max-h) * var(--ratio)));
  margin-inline: auto;
  box-shadow: 0 4px 24px var(--shadow);

  & canvas {
    display: block;
    width: 100%;
    height: 100%;
    touch-action: none;
    cursor: grab;

    &:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
    &.dragging { cursor: grabbing; }
    &.over-text { cursor: move; }
  }
}

.selection {
  position: absolute;
  border: 1.5px dashed #fff;
  outline: 1.5px dashed #000;
  outline-offset: 0;
  mix-blend-mode: difference;
  pointer-events: none;
}

.empty-drop {
  /* Sits along the bottom so it doesn't cover the default headline */
  position: absolute;
  inset: auto 6% 5%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.15rem;
  padding: 0.6rem 0.75rem;
  font: inherit;
  text-align: center;
  color: #fff;
  background: rgba(255, 255, 255, 0.06);
  border: 2px dashed rgba(255, 255, 255, 0.4);
  border-radius: 0.75rem;
  cursor: pointer;

  &:hover { background: rgba(255, 255, 255, 0.12); }
  &:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

  & strong { font-size: 1.1rem; }
  & span { font-size: 0.85rem; opacity: 0.8; }

  & kbd {
    font-family: inherit;
    font-size: 0.8em;
    padding: 0 0.3em;
    border: 1px solid rgba(255, 255, 255, 0.5);
    border-radius: 0.25rem;
  }

  & .empty-loading { display: none; }
}

body.loading .empty-drop {
  & > :not(.empty-loading) { display: none; }
  & .empty-loading { display: block; }
}

.has-image .empty-drop { display: none; }

.stage-meta {
  margin-top: 0.5rem;
  text-align: center;
  font-size: 0.75rem;
  color: var(--text-secondary);
}

/* Control panel */
.panel {
  display: grid;
  gap: 0.75rem;
  align-content: start;
  min-width: 0;
}

details {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-secondary);

  & summary {
    list-style: none;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.6rem 0.85rem;
    font-weight: 600;
    font-size: 0.95rem;
    cursor: pointer;
    user-select: none;

    &::-webkit-details-marker { display: none; }
    &:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; border-radius: var(--radius); }
  }

  & .chevron {
    width: 0.5rem;
    height: 0.5rem;
    border-right: 2px solid var(--text-secondary);
    border-bottom: 2px solid var(--text-secondary);
    transform: rotate(45deg);
    transition: transform 0.15s;
  }

  &[open] .chevron { transform: rotate(225deg); }
}

.section-body {
  display: grid;
  gap: 0.85rem;
  padding: 0 0.85rem 0.85rem;
}

.row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

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

  & > span {
    display: flex;
    justify-content: space-between;
    font-size: 0.8rem;
    font-weight: 500;
    color: var(--text-secondary);
  }

  & output { font-variant-numeric: tabular-nums; }

  & select,
  & textarea,
  & input[type="text"] {
    font: inherit;
    font-size: 0.9rem;
    width: 100%;
    padding: 0.45rem 0.6rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--bg);
    color: var(--text);

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

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

  & input[type="range"] {
    width: 100%;
    accent-color: var(--accent);
  }

  & input[type="color"] {
    width: 100%;
    height: 2.2rem;
    padding: 0.15rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--bg);
    cursor: pointer;
  }
}

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

@media (min-width: 360px) {
  .field-grid { grid-template-columns: 1fr 1fr; }
}

.check {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  font-size: 0.875rem;
  cursor: pointer;

  & input { accent-color: var(--accent); width: 1rem; height: 1rem; }
}

.font-status {
  font-size: 0.75rem;
  color: var(--text-secondary);
  min-height: 1em;
  margin-top: -0.5rem;

  &:empty { display: none; }
  &.error { color: var(--danger); }
}

/* Segmented radio control */
.segmented {
  display: flex;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--bg);

  & label {
    flex: 1;
    position: relative;
    cursor: pointer;

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

  & input {
    position: absolute;
    opacity: 0;
    inset: 0;
    cursor: pointer;
  }

  & span {
    display: block;
    text-align: center;
    padding: 0.4rem 0.25rem;
    font-size: 0.8rem;
    font-weight: 500;
    color: var(--text-secondary);
  }

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

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

/* Size presets */
.preset-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(6.5rem, 1fr));
  gap: 0.5rem;
}

.preset {
  display: grid;
  justify-items: center;
  gap: 0.35rem;
  padding: 0.55rem 0.4rem;
  font: inherit;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  cursor: pointer;

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

  &[aria-checked="true"] {
    border-color: var(--accent);
    background: var(--accent-light);
  }

  & .shape {
    height: 2rem;
    display: flex;
    align-items: center;

    & i {
      display: block;
      height: 100%;
      border: 2px solid currentColor;
      border-radius: 3px;
      opacity: 0.7;
    }
  }

  & .name { font-size: 0.8rem; font-weight: 600; }
  & .dims { font-size: 0.7rem; color: var(--text-secondary); font-variant-numeric: tabular-nums; }
}

/* Text layers */
.layers {
  display: grid;
  gap: 0.5rem;
}

.layer-list {
  display: grid;
  gap: 0.35rem;
}

.layer {
  font: inherit;
  font-size: 0.85rem;
  text-align: left;
  padding: 0.4rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  cursor: pointer;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;

  &[aria-checked="true"] {
    border-color: var(--accent);
    background: var(--accent-light);
  }

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

  &.empty-text { color: var(--text-secondary); font-style: italic; }
}

#text-editor {
  display: grid;
  gap: 0.85rem;
  padding-top: 0.75rem;
  border-top: 1px solid var(--border);

  &[aria-disabled="true"] {
    opacity: 0.45;
    pointer-events: none;
  }
}

/* 3×3 placement pad */
.place-grid {
  display: grid;
  grid-template-columns: repeat(3, 2.25rem);
  grid-template-rows: repeat(3, 1.6rem);
  gap: 0.25rem;
  padding: 0.35rem;
  width: max-content;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);

  & button {
    border: 0;
    border-radius: 0.25rem;
    background: var(--bg-tertiary);
    cursor: pointer;
    position: relative;

    &::after {
      content: "";
      position: absolute;
      inset: 35% 30%;
      border-radius: 2px;
      background: var(--text-secondary);
    }

    &:hover { background: var(--accent-light); }
    &:hover::after { background: var(--accent); }
    &:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
  }
}

/* Wider screens: preview beside a fixed-width sidebar */
@media (min-width: 900px) {
  :root { --stage-max-h: calc(100dvh - 9rem); }

  .layout {
    grid-template-columns: minmax(0, 1fr) 22rem;
    align-items: start;
    padding: 1.25rem;
    gap: 1.25rem;
  }

  .stage {
    top: 4.6rem;
    margin: 0;
    padding: 1.25rem;
  }

  .panel {
    max-height: calc(100dvh - 5.85rem);
    overflow-y: auto;
    position: sticky;
    top: 4.6rem;
    padding-bottom: 1rem;
  }
}
