/* StraightUpJigSaw — light palette by default; dark via html[data-theme="dark"]. */
:root,
:root[data-theme='light'] {
  --bg: #f4efe7;
  --bg-elev: #fffdf9;
  --border: #dccfbd;
  --text: #2a1f14;
  --text-dim: #7a6a58;
  --accent: #a0612a;
  --accent-strong: #7f4a1c;
  --accent-soft: rgba(160, 97, 42, 0.12);
  --on-accent: #fff;
  --good: #15803d;
  --bad: #dc2626;
  --warn: #b45309;
  --wood: #c08a52;
  --cut-line: #3b2614;
  --drill: #1d4ed8;
  --mat: #2f6b57;
  --mat-line: rgba(255, 255, 255, 0.14);
  --shadow: rgba(60, 40, 20, 0.14);
  --radius: 10px;
  --font: 'Segoe UI', system-ui, -apple-system, sans-serif;
}

:root[data-theme='dark'] {
  --bg: #17130f;
  --bg-elev: #221c16;
  --border: #3d3228;
  --text: #f1e8dc;
  --text-dim: #a8998a;
  --accent: #e0a060;
  --accent-strong: #f0b77a;
  --accent-soft: rgba(224, 160, 96, 0.14);
  --on-accent: #1a1007;
  --good: #4ade80;
  --bad: #f87171;
  --warn: #fbbf24;
  --wood: #c99460;
  --cut-line: #2a1a0c;
  --drill: #60a5fa;
  --mat: #1f4a3c;
  --mat-line: rgba(255, 255, 255, 0.08);
  --shadow: rgba(0, 0, 0, 0.45);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
}

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

.app {
  max-width: 1500px;
  margin: 0 auto;
  padding: 12px 16px 24px;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.update-banner {
  display: flex;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  border-radius: var(--radius);
  background: var(--accent-soft);
  border: 1px solid var(--accent);
}
.update-banner[hidden] {
  display: none;
}
.update-warn {
  color: var(--text-dim);
}

.header {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
}
.brand {
  display: flex;
  gap: 12px;
  align-items: center;
}
.brand-mark {
  width: 40px;
  height: 40px;
}
.brand h1 {
  margin: 0;
  font-size: 1.35rem;
  letter-spacing: -0.01em;
}
.tagline {
  margin: 0;
  color: var(--text-dim);
  font-size: 0.9rem;
}
.header-actions,
.row {
  display: flex;
  gap: 8px;
  align-items: center;
}
.row.wrap {
  flex-wrap: wrap;
}
.row.small {
  font-size: 0.85rem;
}

.btn {
  border: 1px solid var(--border);
  background: var(--bg-elev);
  border-radius: 8px;
  padding: 7px 12px;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover {
  border-color: var(--accent);
}
.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--on-accent);
  font-weight: 600;
}
.btn.primary:hover {
  background: var(--accent-strong);
}
.btn.wide {
  width: 100%;
}
.btn[aria-pressed='true'] {
  background: var(--accent-soft);
  border-color: var(--accent);
  font-weight: 600;
}
.link {
  border: 0;
  background: none;
  color: var(--accent);
  cursor: pointer;
  padding: 2px 4px;
  text-decoration: underline;
  text-underline-offset: 2px;
}
.muted {
  color: var(--text-dim);
  margin-right: auto;
}

.main {
  display: grid;
  grid-template-columns: 330px minmax(0, 1fr);
  gap: 16px;
  align-items: start;
}

.panel {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.group {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.group h2 {
  margin: 0;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
}
.hint {
  margin: 0;
  color: var(--text-dim);
  font-size: 0.85rem;
  line-height: 1.35;
}
.grid2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.field {
  display: flex;
  flex-direction: column;
  gap: 3px;
  font-size: 0.85rem;
  min-width: 0;
}
.field > span:first-child {
  color: var(--text-dim);
  display: flex;
  justify-content: space-between;
}
.field output {
  color: var(--text);
  font-variant-numeric: tabular-nums;
}
.field select,
.unit {
  border: 1px solid var(--border);
  border-radius: 7px;
  background: var(--bg);
  padding: 5px 8px;
  width: 100%;
}
.unit {
  display: flex;
  align-items: center;
  gap: 4px;
}
.unit input {
  border: 0;
  background: none;
  width: 100%;
  min-width: 0;
  outline: none;
  font-variant-numeric: tabular-nums;
}
.unit i {
  font-style: normal;
  color: var(--text-dim);
}
.unit:focus-within,
.field select:focus {
  outline: 2px solid var(--accent);
  outline-offset: 0;
}
input[type='range'] {
  width: 100%;
  accent-color: var(--accent);
}
.check {
  display: flex;
  gap: 8px;
  align-items: flex-start;
  font-size: 0.88rem;
  cursor: pointer;
}
.check input {
  accent-color: var(--accent);
  margin-top: 3px;
}

.stage-col {
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-width: 0;
  position: sticky;
  top: 12px;
}
.toolbar {
  display: flex;
  gap: 12px;
  align-items: center;
  flex-wrap: wrap;
}
.seg {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}
.seg-btn {
  border: 0;
  background: var(--bg-elev);
  padding: 6px 14px;
  cursor: pointer;
}
.seg-btn + .seg-btn {
  border-left: 1px solid var(--border);
}
.seg-btn[aria-pressed='true'] {
  background: var(--accent);
  color: var(--on-accent);
  font-weight: 600;
}
.busy {
  color: var(--text-dim);
  font-size: 0.85rem;
}
.busy[hidden] {
  display: none;
}

/* Cutting-mat look behind the pattern: cut-outs show the mat. */
.stage-wrap {
  border-radius: var(--radius);
  overflow: hidden;
  height: min(72vh, 900px);
  background-color: var(--mat);
  background-image:
    linear-gradient(var(--mat-line) 1px, transparent 1px),
    linear-gradient(90deg, var(--mat-line) 1px, transparent 1px);
  background-size: 20px 20px;
  box-shadow: inset 0 0 0 1px var(--border);
}
.stage-wrap.drop {
  outline: 3px dashed var(--accent);
  outline-offset: -6px;
}
#stage {
  width: 100%;
  height: 100%;
  display: block;
  cursor: grab;
  touch-action: none;
}
#stage:active {
  cursor: grabbing;
}
#stage.drawing {
  cursor: crosshair;
}

.legend {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 6px 16px;
  font-size: 0.85rem;
}
.legend[hidden] {
  display: none;
}
.legend i {
  display: inline-block;
  width: 12px;
  height: 12px;
  border-radius: 3px;
  vertical-align: -1px;
  margin-right: 4px;
}
.status {
  margin: 0;
  min-height: 1.3em;
  color: var(--text-dim);
  font-size: 0.9rem;
}
.stats {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: 8px;
}
.stat {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-left: 4px solid var(--text-dim);
  border-radius: 8px;
  padding: 8px 10px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.stat span {
  color: var(--text-dim);
  font-size: 0.82rem;
}
.stat.good {
  border-left-color: var(--good);
}
.stat.bad {
  border-left-color: var(--bad);
}
.stat.warn {
  border-left-color: var(--warn);
}
.howto {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 14px;
  font-size: 0.9rem;
}
.howto summary {
  cursor: pointer;
  font-weight: 600;
}
.howto ol {
  margin: 8px 0 0;
  padding-left: 20px;
  line-height: 1.45;
}

.footer {
  margin-top: auto;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  color: var(--text-dim);
  font-size: 0.8rem;
}
.version-line[hidden] {
  display: none;
}

.dialog {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-elev);
  color: var(--text);
  max-width: 460px;
}
.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 8px 14px;
  border-radius: 8px;
}
.toast[hidden] {
  display: none;
}

.print-area {
  display: none;
}

@media (max-width: 860px) {
  .main {
    grid-template-columns: 1fr;
  }
  .stage-col {
    position: static;
    order: -1;
  }
  .stage-wrap {
    height: 60vh;
  }
}

@media print {
  .app,
  .dialog,
  .toast {
    display: none !important;
  }
  body {
    background: #fff;
  }
  .print-area {
    display: block;
  }
  .sheet {
    break-after: page;
    color: #000;
    font: 9pt sans-serif;
  }
  .sheet > svg {
    display: block;
  }
  .sheet footer {
    display: flex;
    gap: 6mm;
    align-items: center;
    margin-top: 2mm;
  }
  .sheet footer span:first-child {
    margin-right: auto;
  }
}

/* Fields that don't apply in the current mode (frame outside one-piece panels). */
.unit:has(input:disabled) {
  opacity: 0.45;
}

.field select:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}
