:root {
  color-scheme: light;
  --bg: #f8f4ec;
  --paper: #fffaf2;
  --paper-strong: #ffffff;
  --ink: #22211f;
  --muted: #6f685e;
  --line: #dfd3c1;
  --wood: #9a5c32;
  --wood-dark: #6d3c20;
  --sage: #517568;
  --sage-dark: #2f5147;
  --clay: #b75f3a;
  --gold: #dba94d;
  --gap: #8b8f91;
  --shadow: 0 18px 45px rgba(58, 42, 27, 0.12);
  --radius: 8px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-width: 320px;
  background:
    linear-gradient(180deg, rgba(255, 250, 242, 0.7), rgba(248, 244, 236, 0.95)),
    var(--bg);
  color: var(--ink);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
}

button,
input,
select {
  font: inherit;
}

a {
  color: inherit;
  text-decoration: none;
}

.app-shell {
  width: min(1440px, 100%);
  margin: 0 auto;
  padding: 18px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  gap: 18px;
  align-items: center;
  padding: 20px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--paper);
  box-shadow: var(--shadow);
}

.eyebrow {
  margin: 0 0 6px;
  color: var(--clay);
  font-size: 0.76rem;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1,
h2 {
  margin: 0;
  letter-spacing: 0;
}

h1 {
  font-size: clamp(1.75rem, 4vw, 2.85rem);
  line-height: 1.04;
}

h2 {
  font-size: 1.08rem;
}

.subtitle {
  max-width: 52ch;
  margin: 10px 0 0;
  color: var(--muted);
}

.contact-box {
  display: grid;
  gap: 8px;
  min-width: 210px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--paper-strong);
  color: var(--sage-dark);
  font-weight: 800;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1.35fr) minmax(340px, 0.65fr);
  gap: 18px;
  margin-top: 18px;
  align-items: start;
}

.side-stack {
  display: grid;
  gap: 18px;
  position: sticky;
  top: 12px;
}

.panel,
.estimate-panel {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--paper);
  box-shadow: var(--shadow);
}

.panel {
  padding: 18px;
}

.estimate-panel {
  padding: 18px;
  background: #223f38;
  color: #fffaf2;
}

.section-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
  margin-bottom: 14px;
}

.sketch-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  justify-content: flex-end;
}

.secondary-action,
.icon-button {
  min-height: 40px;
  border: 1px solid var(--sage);
  border-radius: var(--radius);
  background: var(--sage);
  color: #fff;
  cursor: pointer;
  font-weight: 800;
}

.secondary-action {
  padding: 0 14px;
}

.icon-button {
  width: 40px;
  font-size: 1.1rem;
}

.secondary-action:hover,
.icon-button:hover {
  background: var(--sage-dark);
}

.light-action {
  background: #fffdf8;
  color: var(--sage-dark);
}

.light-action:hover {
  background: rgba(81, 117, 104, 0.12);
}

.sketch-frame {
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background:
    linear-gradient(rgba(81, 117, 104, 0.07) 1px, transparent 1px),
    linear-gradient(90deg, rgba(81, 117, 104, 0.07) 1px, transparent 1px),
    #fffdf8;
  background-size: 28px 28px;
}

.sketch {
  display: block;
  width: 100%;
  aspect-ratio: 1.65;
  min-height: 310px;
  touch-action: none;
}

.fence-segment {
  stroke: #b56f3e;
  stroke-width: 8;
  stroke-linecap: round;
}

.selected-segment {
  stroke: #8f4f27;
  stroke-width: 10;
}

.fence-rail-shadow {
  stroke: var(--wood-dark);
  stroke-width: 14;
  stroke-linecap: round;
  opacity: 0.2;
}

.selected-segment-shadow {
  opacity: 0.35;
}

.gap-segment {
  stroke: var(--gap);
  stroke-width: 7;
  stroke-linecap: round;
  stroke-dasharray: 14 12;
}

.segment-hit {
  stroke: transparent;
  stroke-width: 30;
  stroke-linecap: round;
  pointer-events: stroke;
  cursor: pointer;
}

.gate-bay {
  stroke: var(--sage);
  stroke-width: 12;
  stroke-linecap: round;
  stroke-dasharray: 11 7;
}

.post-dot {
  fill: var(--gold);
  stroke: var(--wood-dark);
  stroke-width: 3;
  cursor: grab;
}

.post-dot.bay-post {
  fill: #efbd61;
  stroke-width: 2.5;
}

.post-dot:active,
.house-anchor:active {
  cursor: grabbing;
}

.house-anchor {
  fill: #fffaf2;
  stroke: var(--sage);
  stroke-width: 3;
  cursor: grab;
}

.node-hit {
  fill: transparent;
  stroke: transparent;
  pointer-events: all;
  cursor: grab;
}

.node-hit:active {
  cursor: grabbing;
}

.selected-node {
  stroke-width: 5;
  filter: drop-shadow(0 0 6px rgba(219, 169, 77, 0.7));
}

.add-node {
  fill: var(--sage);
  stroke: #fffaf2;
  stroke-width: 2;
  cursor: pointer;
}

.add-node-label {
  fill: #fffaf2;
  font-size: 16px;
  font-weight: 900;
  pointer-events: all;
  cursor: pointer;
  user-select: none;
}

.edge-badge {
  fill: #fffaf2;
  stroke: var(--sage);
  stroke-width: 2;
  cursor: pointer;
}

.selected-edge-badge {
  fill: var(--sage);
}

.edge-badge-label {
  fill: var(--sage-dark);
  font-size: 12px;
  font-weight: 900;
  pointer-events: all;
  cursor: pointer;
  user-select: none;
}

.selected-edge-badge + .edge-badge-label {
  fill: #fffaf2;
}

.delete-edge {
  fill: #8f3f2a;
  stroke: #fffaf2;
  stroke-width: 2;
  cursor: pointer;
}

.delete-edge-label {
  fill: #fffaf2;
  font-size: 13px;
  font-weight: 900;
  pointer-events: all;
  cursor: pointer;
  user-select: none;
}

.sketch-label {
  fill: var(--ink);
  font-size: 16px;
  font-weight: 800;
  paint-order: stroke;
  stroke: #fffdf8;
  stroke-width: 5px;
}

.bay-label {
  fill: var(--sage-dark);
  font-size: 13px;
  font-weight: 900;
  paint-order: stroke;
  stroke: #fffdf8;
  stroke-width: 4px;
}

.gate-label {
  fill: var(--sage-dark);
  font-size: 12px;
  font-weight: 900;
  paint-order: stroke;
  stroke: #fffdf8;
  stroke-width: 4px;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin: 12px 0 0;
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 700;
}

.legend span {
  display: inline-flex;
  align-items: center;
  gap: 7px;
}

.legend-line {
  display: inline-block;
  width: 28px;
  height: 5px;
  border-radius: 999px;
}

.legend-line.fence {
  background: var(--wood);
}

.legend-line.gap {
  background: repeating-linear-gradient(
    90deg,
    var(--gap) 0,
    var(--gap) 8px,
    transparent 8px,
    transparent 13px
  );
}

.legend-post {
  width: 11px;
  height: 11px;
  border: 2px solid var(--wood-dark);
  border-radius: 50%;
  background: var(--gold);
}

.legend-post.movable {
  background: var(--gold);
}

.legend-wall {
  width: 12px;
  height: 12px;
  border: 2px solid var(--sage);
  border-radius: 50%;
  background: #fffaf2;
}

.compact-toggle {
  display: inline-flex;
  align-items: center;
  min-height: 40px;
  gap: 8px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--paper-strong);
  color: var(--sage-dark);
  font-size: 0.85rem;
  font-weight: 900;
}

.compact-toggle input {
  width: 18px;
  min-height: 18px;
  padding: 0;
}

.save-panel {
  display: grid;
  grid-template-columns: minmax(160px, 1.2fr) minmax(110px, 0.75fr) auto minmax(170px, 1.1fr) auto;
  gap: 8px;
  align-items: end;
  margin-top: 14px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--paper-strong);
}

.save-actions {
  display: flex;
  gap: 8px;
}

.save-status {
  grid-column: 1 / -1;
  min-height: 18px;
  margin: 0;
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 800;
}

.save-status.error {
  color: #8f3f2a;
}

.save-status.success {
  color: var(--sage-dark);
}

.segment-table {
  display: grid;
  gap: 10px;
  margin-top: 14px;
}

.segment-row {
  display: grid;
  grid-template-columns: 44px minmax(92px, 0.9fr) minmax(90px, 0.9fr) minmax(110px, 1fr) minmax(110px, 1fr) minmax(150px, 1.15fr) 40px;
  gap: 8px;
  align-items: end;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--paper-strong);
}

.selected-segment-row {
  border-color: var(--sage);
  box-shadow: 0 0 0 2px rgba(81, 117, 104, 0.12);
}

.segment-number {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: rgba(81, 117, 104, 0.12);
  color: var(--sage-dark);
  font-weight: 900;
}

.length-editor {
  display: grid;
  gap: 7px;
}

.mini-lock {
  display: inline-flex;
  grid-template-columns: auto 1fr;
  align-items: center;
  gap: 6px;
  color: var(--sage-dark);
  font-size: 0.78rem;
  font-weight: 900;
}

.mini-lock input {
  width: 16px;
  min-height: 16px;
  padding: 0;
}

.bay-controls {
  display: flex;
  flex-wrap: wrap;
  grid-column: 2 / -1;
  gap: 6px;
}

.bay-type-toggle {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  min-height: 30px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fffdf8;
  color: var(--muted);
  padding: 4px 8px;
  font-size: 0.78rem;
  font-weight: 900;
  cursor: pointer;
}

.bay-type-toggle span {
  display: grid;
  place-items: center;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: rgba(81, 117, 104, 0.12);
  color: var(--sage-dark);
}

.bay-type-toggle.active {
  border-color: var(--sage);
  background: rgba(81, 117, 104, 0.13);
  color: var(--sage-dark);
}

label {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 800;
}

input,
select {
  width: 100%;
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fffdf8;
  color: var(--ink);
  padding: 8px 10px;
}

input:focus,
select:focus,
button:focus-visible,
.design-card:focus-within {
  outline: 3px solid rgba(219, 169, 77, 0.45);
  outline-offset: 2px;
}

.bay-summary {
  align-self: center;
  color: var(--sage-dark);
  font-size: 0.78rem;
  font-weight: 800;
}

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

.design-card {
  position: relative;
  min-height: 150px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--paper-strong);
  cursor: pointer;
}

.design-card input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.design-card:has(input:checked) {
  border-color: var(--sage);
  box-shadow: inset 0 0 0 2px var(--sage);
}

.design-card strong,
.design-card small {
  display: block;
}

.design-card strong {
  margin-top: 9px;
  color: var(--ink);
}

.design-card small {
  margin-top: 4px;
  color: var(--muted);
  line-height: 1.25;
}

.fence-preview {
  display: block;
  height: 58px;
  border: 1px solid rgba(109, 60, 32, 0.24);
  border-radius: var(--radius);
  background-color: #e6bb80;
}

.preview-standard {
  background-image: repeating-linear-gradient(
    90deg,
    #88502d 0,
    #88502d 8px,
    #a7683c 8px,
    #a7683c 16px
  );
}

.preview-shadow {
  background-image:
    repeating-linear-gradient(
      90deg,
      rgba(136, 80, 45, 0.95) 0,
      rgba(136, 80, 45, 0.95) 9px,
      transparent 9px,
      transparent 18px
    ),
    repeating-linear-gradient(
      90deg,
      transparent 0,
      transparent 9px,
      rgba(167, 104, 60, 0.92) 9px,
      rgba(167, 104, 60, 0.92) 18px
    );
}

.preview-full {
  background-image:
    repeating-linear-gradient(
      90deg,
      #7c4828 0,
      #7c4828 12px,
      #b87846 12px,
      #b87846 24px
    ),
    linear-gradient(180deg, transparent 0 42%, rgba(255, 250, 242, 0.22) 42% 58%, transparent 58%);
}

.control-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 14px;
}

.toggle-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 14px;
  color: var(--ink);
  font-size: 0.95rem;
}

.toggle-row input {
  width: 20px;
  min-height: 20px;
}

.note {
  margin: 12px 0 0;
  color: var(--muted);
  font-size: 0.86rem;
  line-height: 1.45;
}

.total-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  padding: 13px 0;
  border-bottom: 1px solid rgba(255, 250, 242, 0.2);
}

.total-row span {
  color: rgba(255, 250, 242, 0.78);
  font-weight: 800;
}

.total-row strong {
  font-size: 1.9rem;
}

.total-row.invoice strong {
  color: #ffe0a1;
}

.estimate-breakdown {
  display: grid;
  gap: 10px;
  margin: 14px 0;
}

.breakdown-line {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  color: rgba(255, 250, 242, 0.86);
  font-size: 0.93rem;
}

.breakdown-line dd,
.breakdown-line dt {
  margin: 0;
}

.breakdown-line dd {
  font-weight: 900;
  text-align: right;
}

.payment-note {
  display: grid;
  gap: 4px;
  margin-top: 12px;
  padding: 12px;
  border-radius: var(--radius);
  background: rgba(255, 250, 242, 0.1);
  color: #fffaf2;
}

.payment-note span {
  color: rgba(255, 250, 242, 0.76);
  font-size: 0.9rem;
  line-height: 1.35;
}

.disclaimer {
  margin: 18px 0 0;
  padding: 14px 18px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--paper);
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.5;
}

.direction-menu {
  position: fixed;
  z-index: 20;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  width: 160px;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--paper-strong);
  box-shadow: var(--shadow);
}

.direction-menu[hidden] {
  display: none;
}

.direction-menu button {
  min-height: 38px;
  border: 1px solid var(--sage);
  border-radius: var(--radius);
  background: #fffdf8;
  color: var(--sage-dark);
  cursor: pointer;
  font-weight: 900;
}

.direction-menu button:hover {
  background: rgba(81, 117, 104, 0.12);
}

@media (max-width: 1040px) {
  .workspace {
    grid-template-columns: 1fr;
  }

  .side-stack {
    position: static;
  }
}

@media (max-width: 760px) {
  .app-shell {
    padding: 10px;
  }

  .topbar,
  .section-head {
    align-items: stretch;
    flex-direction: column;
  }

  .sketch-actions {
    justify-content: stretch;
  }

  .sketch-actions .secondary-action,
  .sketch-actions .compact-toggle {
    justify-content: center;
    width: 100%;
  }

  .contact-box {
    min-width: 0;
  }

  .panel,
  .estimate-panel,
  .topbar {
    padding: 14px;
  }

  .sketch {
    min-height: 250px;
  }

  .segment-row {
    grid-template-columns: 36px 1fr 1fr;
  }

  .save-panel {
    grid-template-columns: 1fr 1fr;
  }

  .save-actions,
  .save-status {
    grid-column: 1 / -1;
  }

  .segment-row .bay-summary,
  .segment-row .icon-button {
    grid-column: span 1;
  }

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

@media (max-width: 480px) {
  .control-grid {
    grid-template-columns: 1fr;
  }

  .segment-row {
    grid-template-columns: 38px 1fr;
  }

  .save-panel {
    grid-template-columns: 1fr;
  }

  .save-actions,
  .save-status {
    grid-column: 1;
  }

  .segment-row label {
    grid-column: 1 / -1;
  }

  .segment-row label:first-of-type {
    grid-column: 2;
  }

  .segment-row .bay-summary {
    grid-column: 1 / -1;
  }

  .segment-row .icon-button {
    grid-column: 2;
    justify-self: end;
  }

  .total-row strong {
    font-size: 1.55rem;
  }
}
