:root {
  --bg: #161d19;
  --bg-panel: #1e2620;
  --bg-panel-raised: #232c25;
  --line: #384338;
  --line-soft: #2a332a;

  --birch: #ece3d0;
  --birch-dim: #a9a290;
  --tan: #c9a876;
  --tan-deep: #9c7c4c;
  --blueprint: #6fa3b8;
  --blueprint-dim: #3c5c66;
  --rust: #c1543a;
  --rust-soft: #7a3d2e;

  --font-display: "Space Grotesk", system-ui, sans-serif;
  --font-mono: "JetBrains Mono", "SFMono-Regular", Consolas, monospace;

  --radius: 3px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--birch);
  font-family: var(--font-mono);
  font-size: 15px;
  line-height: 1.5;
}

body {
  position: relative;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.grid-backdrop {
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  background-image:
    linear-gradient(var(--line-soft) 1px, transparent 1px),
    linear-gradient(90deg, var(--line-soft) 1px, transparent 1px);
  background-size: 32px 32px;
  opacity: 0.35;
  mask-image: radial-gradient(ellipse 80% 60% at 50% 0%, black 40%, transparent 100%);
}

.site-header, main, .site-footer {
  position: relative;
  z-index: 1;
}

/* ---------- Header ---------- */

.site-header {
  border-bottom: 1px solid var(--line);
  padding: 28px clamp(16px, 4vw, 48px);
}

.header-inner {
  max-width: 1100px;
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 14px;
}

.brand-mark {
  font-family: var(--font-mono);
  color: var(--tan);
  font-size: 22px;
  letter-spacing: -2px;
  transform: rotate(90deg);
  display: inline-block;
}

.brand-text h1 {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: clamp(22px, 3vw, 28px);
  margin: 0;
  letter-spacing: -0.01em;
}

.brand-sub {
  margin: 2px 0 0;
  color: var(--birch-dim);
  font-size: 13px;
}

.scale-note {
  color: var(--blueprint);
  font-size: 12px;
  letter-spacing: 0.04em;
  border: 1px solid var(--blueprint-dim);
  padding: 5px 10px;
  border-radius: var(--radius);
}

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

main {
  flex: 1;
  max-width: 1100px;
  width: 100%;
  margin: 0 auto;
  padding: 32px clamp(16px, 4vw, 48px) 64px;
}

.panel-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
}

.panel-wide { grid-column: 1 / -1; }

.panel {
  background: var(--bg-panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 22px 22px 24px;
}

.panel-head {
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px dashed var(--line);
  position: relative;
}

.panel-index {
  position: absolute;
  top: -30px;
  right: 0;
  font-family: var(--font-display);
  font-size: 12px;
  color: var(--tan-deep);
  border: 1px solid var(--tan-deep);
  border-radius: 50%;
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.panel-head h2 {
  font-family: var(--font-display);
  font-size: 17px;
  margin: 0 0 4px;
  font-weight: 600;
}

.panel-head p {
  margin: 0;
  color: var(--birch-dim);
  font-size: 13px;
}

/* ---------- Rows / fields ---------- */

.rows {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 14px;
}

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

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex: 1;
  position: relative;
}

.field span:first-child {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--birch-dim);
}

.field input {
  background: var(--bg-panel-raised);
  border: 1px solid var(--line);
  color: var(--birch);
  font-family: var(--font-mono);
  font-size: 15px;
  padding: 9px 34px 9px 10px;
  border-radius: var(--radius);
  width: 100%;
}

.field input:focus {
  outline: none;
  border-color: var(--blueprint);
  box-shadow: 0 0 0 1px var(--blueprint);
}

.field .unit {
  position: absolute;
  right: 10px;
  bottom: 9px;
  color: var(--tan);
  font-size: 12px;
  pointer-events: none;
}

.field-kerf {
  max-width: 260px;
}

.row-piece {
  align-items: flex-end;
}

.row-piece .field:first-child { flex: 0 0 90px; }

.btn-remove {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--birch-dim);
  border-radius: var(--radius);
  width: 34px;
  height: 34px;
  cursor: pointer;
  font-family: var(--font-mono);
  flex-shrink: 0;
}

.btn-remove:hover {
  border-color: var(--rust);
  color: var(--rust);
}

.btn-add {
  background: transparent;
  border: 1px dashed var(--tan-deep);
  color: var(--tan);
  padding: 9px 14px;
  border-radius: var(--radius);
  font-family: var(--font-mono);
  font-size: 13px;
  cursor: pointer;
}

.btn-add:hover {
  border-style: solid;
  background: rgba(201, 168, 118, 0.08);
}

/* ---------- Submit ---------- */

.submit-row {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  gap: 16px;
  margin-top: 4px;
}

.btn-primary {
  background: var(--tan);
  color: #1b1712;
  border: none;
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 15px;
  padding: 13px 28px;
  border-radius: var(--radius);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 10px;
  transition: background 0.15s ease, transform 0.1s ease;
}

.btn-primary:hover { background: #dcbb8a; }
.btn-primary:active { transform: translateY(1px); }
.btn-primary:disabled { opacity: 0.6; cursor: default; }

.btn-spinner {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 2px solid rgba(27,23,18,0.35);
  border-top-color: #1b1712;
  animation: spin 0.7s linear infinite;
}

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

.error-msg {
  color: var(--rust);
  font-size: 13px;
  margin: 0;
}

/* ---------- Results ---------- */

.results {
  margin-top: 48px;
  padding-top: 32px;
  border-top: 1px solid var(--line);
}

.results-head h2 {
  font-family: var(--font-display);
  font-size: 20px;
  margin: 0 0 4px;
}

.meta-line {
  color: var(--birch-dim);
  font-size: 12px;
  margin: 0 0 20px;
}

.totals-strip {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 1px;
  background: var(--line);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  margin-bottom: 28px;
}

.total-item {
  background: var(--bg-panel);
  padding: 14px 16px;
}

.total-item .label {
  display: block;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--birch-dim);
  margin-bottom: 6px;
}

.total-item .value {
  font-family: var(--font-display);
  font-size: 19px;
  font-weight: 600;
}

.total-item.waste .value { color: var(--rust); }
.total-item.bought .value { color: var(--tan); }

.results-columns {
  display: grid;
  grid-template-columns: 260px 1fr;
  gap: 32px;
}

.results-columns h3 {
  font-family: var(--font-display);
  font-size: 14px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--birch-dim);
  margin: 0 0 12px;
}

.purchase-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

.purchase-table th {
  text-align: left;
  font-size: 11px;
  text-transform: uppercase;
  color: var(--birch-dim);
  border-bottom: 1px solid var(--line);
  padding: 6px 4px;
}

.purchase-table td {
  padding: 8px 4px;
  border-bottom: 1px solid var(--line-soft);
}

.purchase-table td:first-child {
  color: var(--tan);
  font-weight: 500;
}

/* ---------- Cutting diagram ---------- */

.cut-diagram {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.board-row {
  display: grid;
  grid-template-columns: 72px 1fr 64px;
  align-items: center;
  gap: 12px;
}

.board-label {
  font-size: 12px;
  color: var(--birch-dim);
}

.board-label strong {
  display: block;
  color: var(--birch);
  font-size: 13px;
}

.board-bar {
  position: relative;
  height: 34px;
  background: var(--bg-panel-raised);
  border: 1px solid var(--line);
  border-radius: 2px;
  overflow: hidden;
  display: flex;
}

.piece-seg {
  position: relative;
  height: 100%;
  background: var(--tan);
  border-right: 2px solid var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
}

.piece-seg:last-of-type { border-right: none; }

.piece-seg span {
  font-size: 10px;
  color: #241d13;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding: 0 4px;
}

.piece-seg.whole { background: var(--blueprint); }
.piece-seg.whole span { color: #0f1f24; }

.waste-seg {
  height: 100%;
  background: repeating-linear-gradient(
    45deg,
    var(--rust-soft),
    var(--rust-soft) 4px,
    transparent 4px,
    transparent 8px
  );
  background-color: var(--bg-panel);
}

.board-waste {
  text-align: right;
  font-size: 12px;
  color: var(--birch-dim);
}

.board-waste strong {
  display: block;
  color: var(--rust);
  font-size: 13px;
}

/* ---------- Footer ---------- */

.site-footer {
  border-top: 1px solid var(--line);
  padding: 18px clamp(16px, 4vw, 48px);
  text-align: center;
  color: var(--birch-dim);
  font-size: 12px;
}

/* ---------- Responsive ---------- */

@media (max-width: 720px) {
  .panel-grid { grid-template-columns: 1fr; }
  .results-columns { grid-template-columns: 1fr; }
  .board-row { grid-template-columns: 60px 1fr; }
  .board-waste { grid-column: 2 / 3; text-align: left; }
  .totals-strip { grid-template-columns: repeat(2, 1fr); }
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
  .btn-spinner { animation: none; }
}
