:root {
  --bg: #14171c;
  --raise: #1c2129;
  --well: #101318;
  --line: #3d362e;
  --ink: #f4efe6;
  --muted: #cbbfae;
  --accent: #ff6b4a;
  --accent-ink: #2a120c;
  --focus: #ffc56d;
  --paper: #f3ecdf;
  --paper-ink: #241c16;
  --paper-muted: #5c4e43;
  --fold: #943c2c;
  --ok: #81b29a;
  --ok-ink: #14241c;
  --sans: "Avenir Next", "Segoe UI", ui-sans-serif, system-ui, sans-serif;
  --serif: Georgia, "Iowan Old Style", "Palatino Linotype", Palatino, serif;
  color-scheme: dark;
  font-family: var(--sans);
  background: var(--bg);
  color: var(--ink);
}

* { box-sizing: border-box; }

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

body {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  font-size: 1rem;
  line-height: 1.45;
  background:
    radial-gradient(1200px 500px at 10% -10%, rgba(255, 107, 74, 0.16), transparent 55%),
    radial-gradient(900px 480px at 100% 0%, rgba(126, 182, 217, 0.1), transparent 50%),
    var(--bg);
}

button, input { font: inherit; color: inherit; }

button, .upload { touch-action: manipulation; }

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

::selection { background: #ffb088; color: #2a120c; }

.sheet ::selection { background: #e07a5f; color: #fff8f3; }

input { caret-color: var(--focus); }

.skip {
  position: absolute;
  left: 12px;
  top: 10px;
  padding: 8px 12px;
  border-radius: 10px;
  background: var(--focus);
  color: var(--accent-ink);
  font-weight: 700;
  text-decoration: none;
  transform: translateY(-150%);
  z-index: 5;
}

.skip:focus { transform: none; }

.vh {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px 28px;
  flex-wrap: wrap;
  padding: 14px 22px 2px;
  flex-shrink: 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.mark {
  width: 44px;
  height: 44px;
  flex: none;
  filter: drop-shadow(0 6px 10px rgba(0, 0, 0, 0.28));
}

.brand-copy { min-width: 0; }

h1 {
  margin: 0;
  font-family: var(--serif);
  font-size: 1.75rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1.05;
  text-wrap: balance;
}

.lede {
  margin: 4px 0 0;
  max-width: 42ch;
  color: var(--muted);
  font-size: 0.9375rem;
  line-height: 1.4;
  text-wrap: pretty;
}

.summary {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0 0 0 auto;
  max-width: min(24rem, 100%);
  padding: 8px 12px 8px 10px;
  border-radius: 999px;
  background: #12161c;
  border: 1px solid var(--line);
  color: var(--ink);
  font-size: 0.875rem;
  line-height: 1.35;
  font-variant-numeric: tabular-nums;
  text-wrap: pretty;
  overflow-wrap: anywhere;
}

.summary::before {
  content: "";
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--ok);
  box-shadow: 0 0 0 3px rgba(129, 178, 154, 0.22);
  flex: none;
}

body.has-error .summary::before {
  background: #e07a5f;
  box-shadow: 0 0 0 3px rgba(224, 122, 95, 0.25);
}

body.is-reading .summary::before {
  background: var(--focus);
  box-shadow: 0 0 0 3px rgba(255, 197, 109, 0.28);
}

.layout {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 12px 16px max(16px, env(safe-area-inset-bottom));
}

.panel {
  background: var(--raise);
  border: 1px solid var(--line);
  border-radius: 16px;
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.28);
  min-width: 0;
}

.controls {
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.group { display: flex; flex-direction: column; gap: 8px; }

.stage, .sheet {
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.stage-bar, .controls h2 {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin: 0;
}

.stage-bar { padding: 14px 16px 10px; }

.stage .stage-bar {
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
}

h2 {
  margin: 0;
  font-size: 0.8125rem;
  font-weight: 700;
  letter-spacing: 0.01em;
  color: var(--muted);
}

.hint {
  margin: 0;
  color: var(--muted);
  font-size: 0.875rem;
  line-height: 1.35;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px 10px;
}

.key {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-weight: 700;
}

.key i {
  display: block;
  width: 18px;
  height: 0;
  border-top: 2px solid currentColor;
}

.key.cut { color: var(--paper-ink); }

.key.fold { color: var(--fold); }

.key.fold i { border-top-style: dashed; }

.demos {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.demos button, .primary, .upload {
  border-radius: 12px;
  border: 1px solid var(--line);
  background: #12161c;
  min-height: 44px;
}

.demos button {
  cursor: pointer;
  padding: 8px 6px;
  color: var(--ink);
  font-weight: 600;
}

.demos button[aria-pressed="true"] {
  background: var(--ink);
  color: #1a140f;
  border-color: transparent;
}

.demos button:hover, .upload:hover { border-color: #8a7d70; }

.demos button:active { transform: translateY(1px); }

.upload {
  position: relative;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  cursor: pointer;
}

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

.upload input:focus-visible { outline: none; }

.upload:focus-within {
  outline: 2px solid var(--focus);
  outline-offset: 3px;
}

.upload strong {
  display: block;
  font-size: 0.975rem;
  font-weight: 600;
  overflow-wrap: anywhere;
}

.upload small {
  display: block;
  color: var(--muted);
  line-height: 1.35;
  margin-top: 2px;
  font-size: 0.8125rem;
  overflow-wrap: anywhere;
}

.upload .ico { color: var(--accent); }

.slider {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  font-size: 0.9375rem;
}

output {
  font-variant-numeric: tabular-nums;
  color: var(--focus);
  font-weight: 700;
  font-size: 1.0625rem;
}

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 28px;
  margin: 0;
  background: transparent;
  cursor: pointer;
  accent-color: var(--accent);
}

input[type="range"]:focus-visible { outline: none; }

input[type="range"]::-webkit-slider-runnable-track {
  height: 4px;
  border-radius: 999px;
  background: #3a342c;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 22px;
  height: 22px;
  margin-top: -9px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid #fff4ea;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.35);
}

input[type="range"]::-moz-range-track {
  height: 4px;
  border: none;
  border-radius: 999px;
  background: #3a342c;
}

input[type="range"]::-moz-range-thumb {
  width: 22px;
  height: 22px;
  border: 2px solid #fff4ea;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.35);
}

input[type="range"]:focus-visible::-webkit-slider-thumb {
  box-shadow: 0 0 0 3px var(--bg), 0 0 0 5px var(--focus);
}

input[type="range"]:focus-visible::-moz-range-thumb {
  box-shadow: 0 0 0 3px var(--bg), 0 0 0 5px var(--focus);
}

.scale {
  display: flex;
  justify-content: space-between;
  margin-top: -4px;
  color: var(--muted);
  font-size: 0.8125rem;
  font-variant-numeric: tabular-nums;
}

.ico { width: 20px; height: 20px; flex: none; }

.primary {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  min-height: 44px;
  border-color: transparent;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 700;
  cursor: pointer;
  transition: background-color 160ms ease, transform 160ms ease, filter 160ms ease;
}

.primary:hover { filter: brightness(1.06); }

.primary:active { transform: translateY(1px); }

.primary:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  filter: none;
  transform: none;
}

.primary.is-saved {
  background: var(--ok);
  color: var(--ok-ink);
}

.error {
  margin: 0;
  padding: 10px 12px;
  border-radius: 12px;
  background: #3a1d22;
  color: #ffd0d0;
  border: 1px solid #7a3944;
  line-height: 1.45;
  font-size: 0.875rem;
}

.error[hidden] { display: none; }

.notes-panel {
  min-height: 0;
  min-width: 0;
  padding: 4px 16px 8px;
  overflow: auto;
  scrollbar-width: thin;
  scrollbar-color: #5c534a transparent;
}

.notes { display: flex; flex-direction: column; }

.notes h3 {
  margin: 10px 0 2px;
  font-size: 0.9375rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  color: var(--ink);
}

.notes h3:first-child { margin-top: 0; }

.notes p {
  margin: 0;
  color: var(--muted);
  font-size: 0.875rem;
  line-height: 1.4;
  text-wrap: pretty;
}

.view-wrap {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  background: var(--well);
}

canvas {
  width: 100%;
  flex: 1;
  min-height: 0;
  display: block;
  cursor: grab;
  touch-action: none;
  transition: opacity 160ms ease;
}

canvas:active { cursor: grabbing; }

canvas:focus-visible {
  outline: none;
  box-shadow: inset 0 0 0 2px var(--focus);
}

body.is-reading canvas { opacity: 0.5; }

.busy {
  position: absolute;
  left: 14px;
  bottom: 12px;
  margin: 0;
  padding: 6px 10px;
  border-radius: 999px;
  background: rgba(18, 22, 28, 0.92);
  border: 1px solid var(--line);
  color: var(--focus);
  font-size: 0.875rem;
}

.busy[hidden] { display: none; }

.sheet {
  background: var(--paper);
  color: var(--paper-ink);
  border-color: #e7d8c4;
  box-shadow: 0 16px 36px rgba(0, 0, 0, 0.34);
}

.sheet:focus { outline: none; }

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

.sheet .stage-bar { border-bottom: 1px solid #e4d5c3; }

.sheet h2 { color: #4a3f36; }

.sheet .hint { color: var(--paper-muted); }

/* Cutting-mat grid: a measurement surface for the net, not page decoration. */
.net {
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding: 8px;
  border-radius: 0 0 16px 16px;
  background:
    linear-gradient(#e6d9c6 1px, transparent 1px) 0 0 / 20px 20px,
    linear-gradient(90deg, #e6d9c6 1px, transparent 1px) 0 0 / 20px 20px,
    var(--paper);
  scrollbar-width: thin;
  scrollbar-color: #c4b49e transparent;
}

.net svg {
  width: 100%;
  height: 100%;
  min-height: 240px;
  display: block;
}

.empty {
  margin: 0;
  min-height: 200px;
  display: grid;
  place-items: center;
  text-align: center;
  color: var(--paper-muted);
  padding: 24px;
}

@keyframes lay {
  from { opacity: 0; transform: translateY(8px); filter: blur(2px); }
  to { opacity: 1; transform: none; filter: none; }
}

.net.lay svg :is(.face, .tab, line, text) {
  animation: lay 280ms cubic-bezier(0.16, 1, 0.3, 1) both;
}

noscript {
  padding: 12px 16px 20px;
  color: var(--muted);
}

@media (min-width: 981px) {
  body { height: 100dvh; overflow: hidden; }

  .layout {
    display: grid;
    grid-template-columns: minmax(260px, 312px) minmax(0, 0.95fr) minmax(300px, 1.25fr);
    grid-template-rows: auto minmax(0, 1fr);
    gap: 16px;
    min-height: 0;
    padding: 12px 16px 16px;
  }

  .controls { grid-column: 1; grid-row: 1; }
  .notes-panel { grid-column: 1; grid-row: 2; padding: 0 16px 2px; }
  .stage { grid-column: 2; grid-row: 1 / span 2; }
  .sheet { grid-column: 3; grid-row: 1 / span 2; }
}

@media (max-width: 980px) {
  .top { align-items: flex-start; padding: 16px 16px 2px; }
  .summary { margin-left: 0; }
  h1 { font-size: 1.375rem; }
  .sheet { min-height: 520px; }
  .stage { min-height: 340px; }
  .view-wrap { min-height: 280px; }
  .stage-bar { flex-direction: column; align-items: flex-start; gap: 4px; }
  .notes-panel { overflow: visible; padding-bottom: 28px; }
}

@media (max-width: 420px) {
  .top, .layout { padding-left: 12px; padding-right: 12px; }
  .lede { font-size: 0.875rem; }
}

@media (forced-colors: active) {
  .primary, .demos button, .upload, .summary { border: 1px solid ButtonText; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation: none !important;
    transition: none !important;
    scroll-behavior: auto !important;
  }
}
