/* Spool Maker — warm workshop palette, light + dark. */

:root {
  --bg: #f3ede2;
  --panel: #fbf7f0;
  --panel-edge: #e2d7c5;
  --ink: #2a221b;
  --ink-soft: #6b5d4f;
  --ink-faint: #9a8b7a;
  --field: #ffffff;
  --field-edge: #d6c8b3;
  --accent: #f2f2f2;          /* overridden live by the filament colour */
  --accent-ink: #1f1408;
  --accent-text: color-mix(in srgb, var(--accent) 35%, var(--ink));  /* accent for text/marks */
  --track: #e6dccb;
  --stage: #e9e1d3;
  --grid: #cbbda7;
  --danger: #b3261e;
  --danger-bg: #fbe4e1;
  --warn: #8a5a00;
  --warn-bg: #fbefd2;
  --focus: #1d6fd8;
  --shadow: 0 1px 0 rgba(255, 255, 255, .7) inset, 0 1px 2px rgba(60, 40, 20, .08);
  --radius: 10px;
  --font-display: "Bricolage Grotesque", "Avenir Next", system-ui, sans-serif;
  --font-body: "IBM Plex Sans", system-ui, -apple-system, "Segoe UI", sans-serif;
  --font-mono: "IBM Plex Mono", ui-monospace, "SF Mono", Menlo, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17130f;
    --panel: #1f1a15;
    --panel-edge: #342b22;
    --ink: #f1e8dc;
    --ink-soft: #bfae99;
    --ink-faint: #8a7b69;
    --field: #2a231c;
    --field-edge: #47392c;
    --track: #3a3026;
    --stage: #120f0c;
    --grid: #3b3128;
    --danger: #ff8a80;
    --danger-bg: #3a1714;
    --warn: #f3c46a;
    --warn-bg: #3a2c12;
    --focus: #7cb4ff;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
    color-scheme: dark;
  }
}

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

[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.45 var(--font-body);
  -webkit-font-smoothing: antialiased;
}

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

.app {
  display: grid;
  grid-template-columns: minmax(320px, 380px) 1fr;
  height: 100vh;
  height: 100dvh;
}

/* ---------- Panel ---------- */

.panel {
  background: var(--panel);
  border-right: 1px solid var(--panel-edge);
  overflow-y: auto;
  padding: 20px 20px 40px;
}

.brand {
  display: flex;
  gap: 12px;
  align-items: center;
  padding-bottom: 16px;
  border-bottom: 2px solid var(--ink);
  margin-bottom: 8px;
}

.brand-mark {
  flex: none;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background:
    radial-gradient(circle, var(--panel) 0 22%, transparent 23%),
    repeating-radial-gradient(circle, var(--accent) 0 2px, color-mix(in srgb, var(--accent) 70%, #000) 2px 3px);
  box-shadow: 0 0 0 3px var(--ink);
}

h1 {
  font: 800 28px/1 var(--font-display);
  letter-spacing: -0.02em;
  margin: 0;
}

.tagline {
  margin: 4px 0 0;
  color: var(--ink-soft);
  font-size: 13px;
}

.loading-note { color: var(--ink-faint); }

fieldset.group {
  min-width: 0;          /* fieldsets default to min-content and overflow narrow screens */
  border: 0;
  margin: 0;
  padding: 16px 0 4px;
  border-bottom: 1px dashed var(--panel-edge);
}

fieldset.group:last-child { border-bottom: 0; }

legend.group-title {
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 0;
  margin-bottom: 10px;
  font: 700 13px/1 var(--font-display);
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

legend.group-title .num {
  font-family: var(--font-mono);
  font-weight: 500;
  color: var(--accent-text);
  filter: saturate(1.1);
  letter-spacing: 0;
}

.field { margin-bottom: 12px; min-width: 0; }

.field-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
}

.field label, .field .label {
  font-weight: 600;
  font-size: 14px;
}

.unit {
  font: 12px var(--font-mono);
  color: var(--ink-faint);
}

.help {
  margin: 3px 0 0;
  font-size: 12px;
  color: var(--ink-soft);
}

.badge {
  display: inline-block;
  margin-top: 6px;
  padding: 3px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 500;
  background: var(--warn-bg);
  color: var(--warn);
}

.range-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 84px;
  gap: 10px;
  align-items: center;
  margin-top: 4px;
}

input[type="number"], input[type="text"], select {
  width: 100%;
  min-width: 0;
  font: 14px var(--font-body);
  color: var(--ink);
  background: var(--field);
  border: 1px solid var(--field-edge);
  border-radius: 7px;
  padding: 6px 8px;
  box-shadow: var(--shadow);
}

input[type="number"] { font-family: var(--font-mono); text-align: right; }

select { margin-top: 4px; }
input[type="text"] { margin-top: 4px; }

input:disabled, select:disabled { opacity: .55; cursor: not-allowed; }

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

input[type="checkbox"] {
  width: 18px;
  height: 18px;
  accent-color: var(--accent-text);
  margin: 0;
}

.check-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.color-row {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-top: 4px;
}

input[type="color"] {
  width: 44px;
  height: 32px;
  padding: 2px;
  border: 1px solid var(--field-edge);
  border-radius: 7px;
  background: var(--field);
  cursor: pointer;
}

.swatches { display: flex; gap: 6px; flex-wrap: wrap; }

.swatch {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 2px solid var(--panel);
  box-shadow: 0 0 0 1px var(--field-edge);
  cursor: pointer;
  padding: 0;
}

.swatch[aria-pressed="true"] { box-shadow: 0 0 0 2px var(--ink); }

.field.is-disabled .label, .field.is-disabled label { color: var(--ink-faint); }

/* ---------- Stage ---------- */

.stage {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

.viewport {
  position: relative;
  flex: 1;
  min-height: 0;
  background:
    radial-gradient(ellipse at 50% 40%, color-mix(in srgb, var(--stage) 70%, #fff 30%), var(--stage) 70%);
}

@media (prefers-color-scheme: dark) {
  .viewport {
    background: radial-gradient(ellipse at 50% 40%, #221c16, var(--stage) 70%);
  }
}

#canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}

.viewer-tools {
  position: absolute;
  top: 14px;
  left: 14px;
  display: flex;
  gap: 8px;
}

.pill-toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--panel) 88%, transparent);
  border: 1px solid var(--panel-edge);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  backdrop-filter: blur(6px);
}

.busy {
  position: absolute;
  top: 14px;
  right: 14px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--ink);
  color: var(--panel);
  font: 500 12px var(--font-mono);
}

.busy-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent-text);
  animation: pulse 1s ease-in-out infinite;
}

@keyframes pulse { 50% { opacity: .25; } }

@media (prefers-reduced-motion: reduce) {
  .busy-dot { animation: none; }
}

.viewer-msg {
  position: absolute;
  inset: auto 14px 14px 14px;
  text-align: center;
  font-size: 13px;
  color: var(--ink-soft);
}

.statsbar {
  border-top: 2px solid var(--ink);
  background: var(--panel);
  padding: 14px 20px 16px;
}

.errors {
  background: var(--danger-bg);
  color: var(--danger);
  border-left: 4px solid var(--danger);
  border-radius: 6px;
  padding: 10px 12px;
  margin-bottom: 12px;
  font-weight: 500;
}

.errors strong { display: block; margin-bottom: 4px; }
.errors ul { margin: 0; padding-left: 18px; }

.stats {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 28px;
  margin: 0;
}

.stats dt {
  font-size: 11px;
  letter-spacing: .1em;
  text-transform: uppercase;
  color: var(--ink-soft);
}

.stats dd {
  margin: 0;
  font: 500 22px/1.2 var(--font-display);
  font-variant-numeric: tabular-nums;
}

.stats.is-stale dd { color: var(--ink-faint); }

.estimate-note {
  margin: 6px 0 0;
  font-size: 12px;
  color: var(--ink);
}

.estimate-note:empty { display: none; }

.notes {
  margin: 6px 0 12px;
  font-size: 12px;
  color: var(--ink-soft);
}

.notes:empty { display: none; }

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.btn {
  font: 600 14px var(--font-body);
  color: var(--ink);
  background: var(--field);
  border: 1.5px solid var(--ink);
  border-radius: 8px;
  padding: 8px 14px;
  cursor: pointer;
  box-shadow: 0 2px 0 var(--ink);
  transition: transform .06s, box-shadow .06s;
}

.btn:hover:not(:disabled) { transform: translateY(-1px); box-shadow: 0 3px 0 var(--ink); }
.btn:active:not(:disabled) { transform: translateY(2px); box-shadow: 0 0 0 var(--ink); }

/* The primary action stands out whatever the filament colour: heavier border and lift,
   a download glyph, and (when the accent would match the plain buttons' fill) an inked
   face with accent-coloured text. */
.btn-primary {
  background: var(--accent);
  color: var(--accent-ink);
  border-width: 2px;
  box-shadow: 0 3px 0 var(--ink);
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  gap: 7px;
}

.btn-primary::before {
  content: "";
  width: 13px;
  height: 13px;
  flex: none;
  background: currentColor;
  -webkit-mask: var(--icon-download) center / contain no-repeat;
  mask: var(--icon-download) center / contain no-repeat;
}

.btn-primary:hover:not(:disabled) { box-shadow: 0 4px 0 var(--ink); }

:root {
  --icon-download: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M7 1h2v7.2l2.6-2.6 1.4 1.4L8 12 3 7l1.4-1.4L7 8.2zM2 13h12v2H2z'/%3E%3C/svg%3E");
}

:root[data-accent-tone="light"] .btn-primary {
  background: var(--ink);
  color: var(--accent);
}

@media (prefers-color-scheme: dark) {
  :root[data-accent-tone="light"] .btn-primary {
    background: var(--accent);
    color: var(--accent-ink);
  }
  :root[data-accent-tone="dark"] .btn-primary {
    background: var(--ink);
    color: var(--accent);
  }
}

.btn-ghost {
  background: transparent;
  box-shadow: none;
  border-style: dashed;
}

.btn-ghost:hover:not(:disabled) { box-shadow: none; }

.btn:disabled {
  cursor: not-allowed;
  opacity: .45;
  box-shadow: none;
  transform: none;
}

.share-status { font-size: 13px; color: var(--ink-soft); }

.actions-hint {
  margin: 8px 0 0;
  font-size: 12px;
  color: var(--ink-soft);
}

/* ---------- Credit ---------- */

.credit {
  margin-top: 24px;
  font-size: 12px;
  color: var(--ink-faint);
  text-align: center;
}

.credit a, .credit a:visited {
  color: var(--ink-soft);
  text-decoration: none;
  transition: color .2s ease;
}

.credit a:hover { color: var(--ink); }

.credit a:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
  border-radius: 2px;
}

.credit .heart { transition: color .2s ease; }
.credit:hover .heart, .credit:focus-within .heart { color: #e0245e; }

@media (prefers-reduced-motion: reduce) {
  .credit .heart, .credit a { transition: none; }
}

/* ---------- Narrow screens ---------- */

@media (max-width: 799px) {
  .app {
    display: block;
    height: auto;
  }
  .panel {
    min-width: 0;
    border-right: 0;
    border-bottom: 1px solid var(--panel-edge);
    overflow: visible;
    padding: 16px;
  }
  .viewport { height: 70vh; min-height: 320px; flex: none; }
  .statsbar { padding: 14px 16px 20px; }
  .actions .btn { flex: 1 1 calc(50% - 8px); min-width: 0; }
  .stage, .statsbar { min-width: 0; }
}
