/* RasterTrace - dark precise-tool UI, matching the gridwall/typewall family.
   Neutral dark ramp, one hazard-amber accent, one radius scale. */
:root {
  --bg: #141517;
  --surface-1: #1b1d20;
  --surface-2: #232529;
  --surface-3: #2b2e33;
  --border: #2e3136;
  --border-strong: #3b3f45;
  --border-input: #666c77; /* >= 3:1 on surface-1 for WCAG 1.4.11 */
  --text: #eceef0;
  --text-dim: #9b9fa6;
  --text-faint: #6b6f76;
  --accent: #f4a92c;
  --accent-hover: #f8c46b;
  --accent-press: #d98e12;
  --accent-soft: rgba(244, 169, 44, 0.14);
  --accent-ink: #1a1204;
  --error: #e5675b;
  --success: #57b98a;
  --r: 8px;
  --r-sm: 6px;
  --check-a: #36393f;
  --check-b: #2a2d32;
  --ring: 0 0 0 2px var(--bg), 0 0 0 4px var(--accent);
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }

/* hidden must beat any class display value */
[hidden] { display: none !important; }

html, body { margin: 0; min-height: 100%; }

body {
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
}

/* Hidden from view but kept in layout, so iOS Safari allows a programmatic
   click() on the file input (it blocks click on display:none inputs). */
.visually-hidden { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }

:focus-visible { outline: none; box-shadow: var(--ring); border-radius: var(--r-sm); }

/* ---------- Topbar ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 56px;
  padding: 8px 16px;
  background: var(--surface-1);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.brand { display: flex; align-items: baseline; gap: 10px; min-width: 0; }
.brand .logo { width: 20px; height: 20px; flex-shrink: 0; align-self: center; color: var(--accent); }
.brand h1 { margin: 0; font-size: 16px; font-weight: 650; letter-spacing: -0.01em; }
.brand .tagline { font-size: 12px; color: var(--text-dim); }

/* ---------- Main ---------- */
main {
  flex: 1;
  width: 100%;
  max-width: 1240px;
  margin: 0 auto;
  padding: 20px 16px;
}

/* ---------- Empty state ---------- */
.stage-empty { display: grid; min-height: 55vh; place-items: center; }

.dropzone {
  display: grid;
  justify-items: center;
  gap: 12px;
  width: min(560px, 100%);
  padding: 40px 24px;
  border: 2px dashed var(--border-strong);
  border-radius: var(--r);
  background: var(--surface-1);
  text-align: center;
}

.dropzone.dragover { border-color: var(--accent); background: var(--accent-soft); }

.dropzone-mark { width: 52px; height: 52px; color: var(--accent); }
.dropzone-title { margin: 0; font-size: 18px; font-weight: 650; }
.dropzone-hint { margin: 0; color: var(--text-dim); max-width: 40ch; }

/* ---------- Buttons ---------- */
.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 40px;
  padding: 0 16px;
  border-radius: var(--r-sm);
  border: 1px solid transparent;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
}

.button-primary { background: var(--accent); color: var(--accent-ink); }
.button-primary:hover { background: var(--accent-hover); }
.button-primary:active { background: var(--accent-press); }

.button-secondary { background: var(--surface-2); color: var(--text); border-color: var(--border-input); }
.button-secondary:hover { border-color: var(--text-faint); }

.button[disabled],
.button[aria-disabled="true"] { opacity: 0.45; cursor: not-allowed; pointer-events: none; }

/* ---------- Workspace ---------- */
.workspace {
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr);
  gap: 20px;
  align-items: start;
}

.controls {
  display: grid;
  gap: 16px;
  padding: 16px;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--r);
}

.field { display: grid; gap: 6px; margin: 0; padding: 0; border: 0; min-width: 0; }

.field label,
.field legend { display: flex; justify-content: space-between; gap: 8px; font-size: 13px; font-weight: 600; padding: 0; }

.field output { font-variant-numeric: tabular-nums; color: var(--accent); }

.choice {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 400 !important;
  justify-content: flex-start !important;
  min-height: 24px;
}

.button-row { display: flex; gap: 8px; align-items: center; }
.button-row-label { font-size: 13px; font-weight: 600; margin-right: auto; }
.icon-button { min-height: 32px; min-width: 44px; padding: 0 10px; }

.subfield { display: grid; gap: 4px; }
.color-row { display: flex; gap: 8px; align-items: stretch; }
.color-row input[type="color"] { width: 56px; flex-shrink: 0; }
.color-row .button { min-height: 38px; padding: 0 10px; font-size: 13px; flex: 1; }
.color-row .button[aria-pressed="true"] { border-color: var(--accent); color: var(--accent); }
.preview.picking { cursor: crosshair; }
.preview.picking img { cursor: crosshair; }
.subfield label { font-weight: 500; font-size: 12px; color: var(--text-dim); }

select, input[type="color"] {
  min-height: 38px;
  padding: 4px 8px;
  border: 1px solid var(--border-input);
  border-radius: var(--r-sm);
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  width: 100%;
}

input[type="range"] { width: 100%; accent-color: var(--accent); min-height: 24px; }
input[type="radio"], input[type="checkbox"] { accent-color: var(--accent); width: 16px; height: 16px; }

/* ---------- Preview ---------- */
.preview-pane { display: grid; gap: 12px; min-width: 0; }

.preview-toolbar { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; }

.view-toggle { display: inline-flex; border: 1px solid var(--border-strong); border-radius: var(--r-sm); overflow: hidden; }

.toggle {
  min-height: 36px;
  min-width: 44px;
  padding: 0 16px;
  border: 0;
  background: var(--surface-2);
  color: var(--text-dim);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

.toggle[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); }

.status { margin: 0; color: var(--text-dim); font-size: 13px; }

.preview {
  position: relative;
  min-height: 320px;
  height: 70vh;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: var(--r);
  display: grid;
  place-items: center;
  padding: 16px;
  cursor: grab;
  touch-action: none;
}

.preview.panning { cursor: grabbing; }

.pan-stage {
  transform-origin: 0 0;
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
}

.checkerboard {
  background-image: conic-gradient(var(--check-b) 90deg, var(--check-a) 90deg 180deg, var(--check-b) 180deg 270deg, var(--check-a) 270deg);
  background-size: 20px 20px;
}

.green-screen { background: #00b140; }

.preview img { max-width: 100%; max-height: 100%; object-fit: contain; display: block; user-select: none; -webkit-user-drag: none; }

.zoom-controls { display: inline-flex; border: 1px solid var(--border-strong); border-radius: var(--r-sm); overflow: hidden; }
.zoom-controls .toggle { min-width: 40px; padding: 0 10px; }
.zoom-level { font-size: 12px; min-width: 56px; }

/* result img has no src until the first trace lands; hide it rather than
   show the broken-image icon behind the tracing veil */
#result-view:not([src]) { display: none; }

.tracing-veil {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(20, 21, 23, 0.72);
  font-weight: 600;
}

.tracing-veil p { animation: pulse 1.2s ease-in-out infinite; }
.veil-elapsed { color: var(--text-dim); font-variant-numeric: tabular-nums; font-weight: 400; }

@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.4; } }

@media (prefers-reduced-motion: reduce) { .tracing-veil p { animation: none; } }

.error { margin: 0; color: var(--error); font-weight: 600; }

/* ---------- Actions ---------- */
.actions { display: flex; justify-content: space-between; align-items: center; gap: 16px; flex-wrap: wrap; }

.stats { display: flex; gap: 24px; margin: 0; }
.stats div { display: grid; }
.stats dt { font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--text-faint); }
.stats dd { margin: 0; font-weight: 700; font-variant-numeric: tabular-nums; }

.action-buttons { display: flex; gap: 12px; }

/* ---------- Statusbar footer ---------- */
.statusbar {
  display: flex;
  align-items: center;
  gap: 14px;
  min-height: 30px;
  padding: 4px 16px;
  background: var(--surface-1);
  border-top: 1px solid var(--border);
  color: var(--text-dim);
  font-size: 12px;
  flex-wrap: wrap;
}

.statusbar p { margin: 0; }
.statusbar .spacer { flex: 1; }
.statusbar .repo-link { display: inline-flex; align-items: center; gap: 6px; color: var(--text-dim); text-decoration: none; }
.statusbar .repo-link:hover { color: var(--accent); }

/* ---------- Small screens ---------- */
@media (max-width: 820px) {
  main { padding: 16px 12px; }

  .workspace { grid-template-columns: 1fr; }

  /* Task order on mobile: result first, tweak below */
  .preview-pane { order: 1; }
  .controls { order: 2; }

  .preview { min-height: 220px; max-height: 55vh; }

  .actions {
    position: sticky;
    bottom: 0;
    background: var(--bg);
    padding: 10px 0;
    border-top: 1px solid var(--border);
    z-index: 1;
  }
}
