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

:root {
  --ink: #1a1a1a;
  --ink-soft: #4a4a4a;
  --ink-faint: #888;
  --line: #e0e0e0;
  --surface: #ffffff;
  --sunken: #fafafa;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--sunken);
  color: var(--ink);
  min-height: 100vh;
  padding: 20px;
  font-size: 14px;
  line-height: 1.5;
}

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

/* Layout ------------------------------------------------------------------ */

.page-header {
  max-width: 980px;
  margin: 0 auto 24px;
  display: flex;
  align-items: center;
  gap: 20px;
}

.page-header h1 {
  font-size: 22px;
  font-weight: 600;
}

.back-button {
  background: var(--ink);
  color: white;
  padding: 8px 16px;
  border-radius: 6px;
  text-decoration: none;
  white-space: nowrap;
  transition: background 0.2s ease;
}

.back-button:hover {
  background: #333;
}

/* Options on the left, the result on the right where it stays visible while
   the options change. */
.layout {
  max-width: 980px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(300px, 380px);
  gap: 20px;
  align-items: start;
}

.column {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 0;
}

.card,
.result {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 16px;
}

.result {
  position: sticky;
  top: 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

h2 {
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--ink-faint);
  margin-bottom: 14px;
}

.hint {
  font-size: 12px;
  color: var(--ink-faint);
}

@media (max-width: 860px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  .result { position: static; }
  /* The result matters more than the knobs once they are stacked. */
  #resultColumn { order: -1; }
}

/* Collapsible sections ----------------------------------------------------- */

details.card,
#filesDetails {
  padding: 0;
}

summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  cursor: pointer;
  font-weight: 500;
  list-style: none;
  border-radius: 10px;
}

summary::-webkit-details-marker { display: none; }

summary::after {
  content: '›';
  color: var(--ink-faint);
  transform: rotate(90deg);
  transition: transform 0.2s ease;
  font-size: 16px;
  line-height: 1;
}

details[open] > summary::after { transform: rotate(-90deg); }

summary:hover { background: var(--sunken); }

.summary-value {
  color: var(--ink-faint);
  font-size: 12px;
  font-weight: 400;
  margin-left: auto;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

details > *:not(summary) {
  margin: 0 16px 16px;
}

#filesDetails {
  border: 1px solid var(--line);
  border-radius: 10px;
}

/* Upload ------------------------------------------------------------------ */

.upload-area {
  border: 2px dashed #ccc;
  border-radius: 10px;
  padding: 28px 20px;
  text-align: center;
  cursor: pointer;
  background: var(--surface);
  transition: all 0.2s ease;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.upload-area:hover,
.upload-area.dragover {
  border-color: var(--ink);
  background: #f2f2f2;
}

.upload-icon { font-size: 32px; }

input[type="file"] { display: none; }

/* Status ------------------------------------------------------------------ */

.error-message,
.notice {
  display: none;
  padding: 10px 12px;
  border-radius: 6px;
  font-size: 13px;
}

.error-message {
  background: #fee;
  color: #c33;
  border: 1px solid #fcc;
}

.notice {
  background: #fffbe6;
  color: #7a5b00;
  border: 1px solid #f0e0a0;
}

/* Sits on top of the preview instead of taking part in the layout. */
.loading {
  display: none;
  position: absolute;
  inset: 0;
  align-items: center;
  justify-content: center;
  gap: 8px;
  background: rgba(250, 250, 250, 0.72);
  border-radius: 8px;
  color: var(--ink-soft);
  font-size: 13px;
}

.preview-stage.busy .loading { display: flex; }

/* Fade what is on screen so it reads as stale while the new one renders. */
.preview-stage.busy #previewZoom { opacity: 0.35; }

.spinner {
  width: 14px;
  height: 14px;
  border: 2px solid var(--line);
  border-top-color: var(--ink);
  border-radius: 50%;
  animation: spin 1s linear infinite;
  flex-shrink: 0;
}

@keyframes spin { to { transform: rotate(360deg); } }

@media (prefers-reduced-motion: reduce) {
  .spinner { animation: none; }
  * { transition: none !important; }
}

/* Controls ----------------------------------------------------------------- */

.control-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px 20px;
}

.control {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
}

.control-label {
  font-size: 12px;
  color: var(--ink-faint);
}

.control-label b {
  color: var(--ink);
  font-weight: 500;
}

.control-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.inline-check {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  white-space: nowrap;
}

@media (max-width: 480px) {
  .control-grid { grid-template-columns: minmax(0, 1fr); }
}

/* A two-option toggle reads faster than two loose radio buttons. */
.segmented {
  display: inline-flex;
  border: 1px solid var(--line);
  border-radius: 6px;
  overflow: hidden;
  align-self: start;
}

.segmented label { cursor: pointer; }

.segmented input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.segmented span {
  display: block;
  padding: 6px 16px;
  font-size: 13px;
  color: var(--ink-soft);
  transition: all 0.2s ease;
}

.segmented input:checked + span {
  background: var(--ink);
  color: white;
}

.segmented input:focus-visible + span {
  outline: 2px solid var(--ink);
  outline-offset: -2px;
}

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

input[type="color"] {
  width: 34px;
  height: 30px;
  padding: 2px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  cursor: pointer;
  flex-shrink: 0;
}

input[type="color"]:disabled { opacity: 0.4; cursor: not-allowed; }

input[type="text"],
textarea {
  width: 100%;
  min-width: 0;
  padding: 8px 10px;
  font-size: 13px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--sunken);
  color: var(--ink);
  font-family: inherit;
  transition: border-color 0.2s;
}

textarea {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  line-height: 1.6;
  resize: vertical;
}

input[type="text"]:focus,
textarea:focus {
  outline: none;
  border-color: var(--ink);
}

.size-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
  color: var(--ink-soft);
  margin-bottom: 8px !important;
}

.size-option {
  display: flex;
  align-items: center;
  gap: 5px;
  cursor: pointer;
}

/* Result ------------------------------------------------------------------- */

.preview-stage,
.preview-strip {
  /* A chequerboard so transparency is visible. */
  background-color: #f5f5f5;
  background-image:
    linear-gradient(45deg, #e8e8e8 25%, transparent 25%, transparent 75%, #e8e8e8 75%),
    linear-gradient(45deg, #e8e8e8 25%, transparent 25%, transparent 75%, #e8e8e8 75%);
  background-size: 16px 16px;
  background-position: 0 0, 8px 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.preview-stage {
  position: relative;
  min-height: 216px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
}

#previewZoom {
  display: none;
  /* Nearest neighbour: the point of the zoom is to show real pixels. */
  image-rendering: pixelated;
}

/* The strip of actual-size icons doubles as the size selector, so no separate
   tab bar or arrow buttons are needed. */
.preview-strip {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 8px;
  flex-wrap: wrap;
  min-height: 48px;
}

.preview-chip {
  background: none;
  border: 2px solid transparent;
  border-radius: 6px;
  padding: 6px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 0;
}

/* Actual size is the point of the strip, but only up to where pixel-level
   judgement matters. Beyond that a 256px chip would dwarf the controls. */
.preview-chip img {
  max-width: 64px;
  max-height: 64px;
}

.preview-chip:hover { border-color: #ccc; }

.preview-chip[aria-pressed="true"] {
  border-color: var(--ink);
  background: rgba(255, 255, 255, 0.7);
}

.preview-label {
  text-align: center;
  font-size: 12px;
  color: var(--ink-faint);
}

.download-row {
  display: flex;
  gap: 8px;
}

.download-row input { flex: 1; }

/* Buttons ------------------------------------------------------------------ */

button { font-family: inherit; }

.primary-btn {
  background: var(--ink);
  color: white;
  border: none;
  border-radius: 6px;
  padding: 9px 18px;
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.2s ease;
}

.primary-btn.wide { width: 100%; padding: 11px 18px; font-size: 14px; }

.primary-btn:hover:not(:disabled) { background: #333; }

.primary-btn:disabled { background: #ccc; cursor: not-allowed; }

.ghost-btn {
  background: var(--surface);
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 5px 12px;
  font-size: 12px;
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.2s ease;
}

.ghost-btn:hover { border-color: var(--ink); }

.ghost-btn[aria-pressed="true"] {
  background: var(--ink);
  border-color: var(--ink);
  color: white;
}

/* File list ---------------------------------------------------------------- */

.files-grid {
  display: grid;
  gap: 6px;
}

.file-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 8px;
  background: var(--sunken);
  border-radius: 6px;
}

.file-item-preview {
  width: 24px;
  height: 24px;
  object-fit: contain;
  background: white;
  border: 1px solid var(--line);
  border-radius: 4px;
  flex-shrink: 0;
}

.file-item-text {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-width: 0;
}

.file-item-name {
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.file-item-size { font-size: 11px; color: var(--ink-faint); }

/* Snippets ----------------------------------------------------------------- */

.snippet { margin-top: 14px; }

.snippet-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 6px;
}

.snippet-header label { font-size: 12px; color: var(--ink-faint); }
