:root {
  --bg: #f5f2ec;
  --ink: #2f241b;
  --muted: #75675a;
  --line: #d9cdbf;
  --accent: #8a4f2b;
  --accent-dark: #60361f;
  --cut: #1f2937;
  --stitch: #b45309;
  --margin-line: #7c3aed;
  --grid: rgba(47, 36, 27, 0.08);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  height: 100vh;
  overflow: hidden;
  background: radial-gradient(circle at top left, rgba(138, 79, 43, 0.15), transparent 32rem), var(--bg);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

.app {
  display: grid;
  grid-template-columns: 360px minmax(0, 1.05fr) minmax(300px, 0.85fr) 320px;
  gap: 0;
  height: 100vh;
  min-height: 0;
  padding: 0.5rem;
}


.app.hide-3d {
  grid-template-columns: 360px minmax(0, 1fr);
}

.app.hide-3d .three-view-panel,
.app.hide-3d .three-settings-panel {
  display: none;
}

.app.hide-3d .preview-panel {
  border-radius: 0 24px 24px 0;
  border-right: 1px solid var(--line);
}

aside, .preview-panel, .three-view-panel, .three-settings-panel {
  background: rgba(255, 250, 242, 0.92);
  border: 1px solid var(--line);
  box-shadow: 0 24px 60px rgba(47, 36, 27, 0.12);
}

aside {
  padding: 0.85rem;
  overflow-y: auto;
  min-height: 0;
  border-radius: 24px 0 0 24px;
}

.preview-panel {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  border-left: 0;
  border-radius: 0 24px 24px 0;
}

h1 {
  margin: 0 0 0.15rem;
  font-size: 1.35rem;
  line-height: 1.05;
  letter-spacing: -0.03em;
}

.section {
  padding: 0.62rem 0;
  border-top: 1px solid var(--line);
}

.page-section {
  border-top: 0;
  padding-top: 0.15rem;
}

.section-title {
  margin: 0 0 0.42rem;
  color: var(--accent-dark);
  font-size: 0.78rem;
  font-weight: 750;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.55rem;
}

.cut-margin-row {
  align-items: start;
}

.inline-check {
  min-height: 2.35rem;
  margin-top: 1.45rem;
}

.compact-control {
  margin-top: 0;
}

.hidden { display: none; }

label {
  display: block;
  margin: 0.42rem 0 0.22rem;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 650;
}

input, select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #fffdf9;
  color: var(--ink);
  font: inherit;
  outline: none;
}

input[type="number"] { padding: 0.48rem 3rem 0.48rem 0.65rem; }

input:focus, select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(138, 79, 43, 0.16);
}

.unit-field { position: relative; }

.unit-field span {
  position: absolute;
  right: 0.7rem;
  top: 50%;
  transform: translateY(-50%);
  color: var(--muted);
  font-size: 0.74rem;
  font-weight: 750;
  pointer-events: none;
}

.control { margin-top: 0.55rem; }
.control label { margin-top: 0; }

input[type="range"] {
  appearance: none;
  height: 0.42rem;
  margin-top: 0.42rem;
  padding: 0;
  border: 0;
  border-radius: 999px;
  background: #e6d3bf;
  cursor: pointer;
}

input[type="range"]::-webkit-slider-thumb {
  appearance: none;
  width: 1rem;
  height: 1rem;
  border: 2px solid #fffaf2;
  border-radius: 999px;
  background: var(--accent);
  box-shadow: 0 3px 8px rgba(47, 36, 27, 0.25);
}

input[type="range"]::-moz-range-thumb {
  width: 1rem;
  height: 1rem;
  border: 2px solid #fffaf2;
  border-radius: 999px;
  background: var(--accent);
  box-shadow: 0 3px 8px rgba(47, 36, 27, 0.25);
}

.segmented {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.4rem;
}

.segmented.four { grid-template-columns: repeat(4, 1fr); }

.seg-button {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0.48rem 0.42rem;
  min-height: 2.35rem;
  background: #fffdf9;
  color: var(--muted);
  box-shadow: none;
  font-size: 0.78rem;
}

.icon-button {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  min-height: 2.35rem;
}

.icon-button svg {
  width: 16px;
  height: 16px;
  background: transparent;
}

.icon-button svg * {
  fill: none;
  stroke: currentColor;
  stroke-width: 2.1;
}

.seg-button.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fffaf2;
}

.seg-button:hover {
  background: #efe3d5;
  color: var(--accent-dark);
}

.seg-button.active:hover {
  background: var(--accent-dark);
  color: #fffaf2;
}

.shape-toggle {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.45rem;
  margin-top: 0.25rem;
}

.shape-button {
  display: grid;
  gap: 0.24rem;
  place-items: center;
  min-height: 70px;
  padding: 0.45rem 0.25rem;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: #fffdf9;
  color: var(--muted);
  box-shadow: none;
  font-size: 0.68rem;
}

.shape-button svg {
  width: 30px;
  height: 30px;
  background: transparent;
}

.shape-button svg * {
  fill: none;
  stroke: currentColor;
  stroke-width: 4;
  vector-effect: non-scaling-stroke;
}

.shape-button.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fffaf2;
}

.shape-button:hover {
  background: #efe3d5;
  color: var(--accent-dark);
}

.shape-button.active:hover {
  background: var(--accent-dark);
  color: #fffaf2;
}

.check {
  display: flex;
  align-items: center;
  gap: 0.48rem;
  margin-top: 0.55rem;
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 650;
}

.check input {
  width: 1rem;
  height: 1rem;
  padding: 0;
  appearance: none;
  border: 1.6px solid var(--accent);
  border-radius: 4px;
  background: #fffdf9;
}

.check input:checked {
  background: var(--accent);
  box-shadow: inset 0 0 0 3px #fffdf9;
}

button {
  border: 0;
  border-radius: 14px;
  padding: 0.65rem 0.72rem;
  background: var(--accent);
  color: #fffaf2;
  font: inherit;
  font-weight: 750;
  cursor: pointer;
  box-shadow: 0 12px 24px rgba(96, 54, 31, 0.2);
}

button:hover { background: var(--accent-dark); }

button.secondary {
  background: #efe3d5;
  color: var(--accent-dark);
  box-shadow: none;
}

button.secondary:hover { background: #e6d3bf; }

.preview-wrap {
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding: 0.5rem;
  display: flex;
  align-items: center;
  background-image: linear-gradient(var(--grid) 1px, transparent 1px), linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: 24px 24px;
}

.pages {
  display: flex;
  align-items: center;
  gap: 1rem;
  min-width: max-content;
  height: 100%;
}

.paper {
  flex: 0 0 auto;
  height: 100%;
  padding: 0.35rem;
  background: white;
  border: 1px solid #ddd4c9;
  box-shadow: 0 18px 50px rgba(47, 36, 27, 0.16);
  display: flex;
  align-items: center;
}

svg {
  display: block;
  height: 100%;
  width: auto;
  background: white;
}

.page-svg { max-height: 100%; }

@media (max-width: 1240px) {
  body {
    height: auto;
    overflow: auto;
  }

  .app {
    grid-template-columns: 1fr;
    height: auto;
    gap: 1rem;
  }

  aside, .preview-panel, .three-view-panel, .three-settings-panel {
    border-radius: 24px;
    border-left: 1px solid var(--line);
  }

  .preview-panel { min-height: 80vh; }
  .preview-wrap { min-height: 70vh; }
}

.preview-panel {
  display: flex;
  flex-direction: column;
  border-left: 0;
  border-right: 0;
  border-radius: 0;
}

.three-view-panel, .three-settings-panel {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  border-left: 0;
  border-radius: 0 24px 24px 0;
}

.panel-top {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.8rem 0.95rem 0.6rem;
  border-bottom: 1px solid var(--line);
}

.panel-top h2 {
  margin: 0;
  font-size: 1rem;
  line-height: 1.1;
  letter-spacing: -0.02em;
}

.panel-top p {
  margin: 0;
  color: var(--muted);
  font-size: 0.75rem;
  white-space: nowrap;
}

.preview-wrap {
  padding: 0.45rem;
}

.three-controls {
  padding: 0 0.95rem;
  overflow-y: auto;
  max-height: 48%;
}

.three-controls .section:first-child {
  border-top: 0;
}

.swatch-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.42rem;
  margin-top: 0.18rem;
}

.compact-swatch-grid {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.swatch-button {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  width: 100%;
  min-height: 2.45rem;
  padding: 0.4rem 0.5rem;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #fffdf9;
  color: var(--muted);
  font: inherit;
  font-size: 0.72rem;
  font-weight: 650;
  box-shadow: none;
}

.swatch-button .swatch-face {
  width: 1.1rem;
  height: 1.1rem;
  flex: 0 0 auto;
  border-radius: 999px;
  border: 1px solid rgba(47, 36, 27, 0.18);
  background: var(--swatch);
  box-shadow: inset 0 0 0 1px rgba(255,255,255,0.22);
}

.swatch-button.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fffaf2;
}

.swatch-button:hover {
  background: #efe3d5;
  color: var(--accent-dark);
}

.swatch-button.active:hover {
  background: var(--accent-dark);
  color: #fffaf2;
}

.three-viewport-wrap {
  flex: 1;
  min-height: 240px;
  padding: 0.7rem;
  border-top: 1px solid var(--line);
  background: linear-gradient(180deg, rgba(255,250,242,0.82), rgba(245,242,236,0.96));
}

#threeViewport {
  width: 100%;
  height: 100%;
  min-height: 0;
  border: 1px solid #ddd4c9;
  border-radius: 18px;
  background: radial-gradient(circle at 30% 25%, rgba(255,255,255,0.9), rgba(233,226,216,0.92));
  overflow: hidden;
  cursor: grab;
}

#threeViewport.dragging {
  cursor: grabbing;
}

@media (max-width: 1240px) {
  .preview-panel,
  .three-view-panel, .three-settings-panel {
    border-radius: 24px;
    border-left: 1px solid var(--line);
  }
}

.preview-panel {
  border-left: 0;
  border-right: 0;
  border-radius: 0;
}

.three-view-panel {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  border-left: 0;
  border-right: 0;
  border-radius: 0;
}

.three-settings-panel {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  border-left: 0;
  border-radius: 0 24px 24px 0;
}

.three-settings-panel .panel-top {
  justify-content: flex-start;
}

.three-settings-panel .three-controls {
  flex: 1;
  max-height: none;
  padding: 0 0.95rem 0.8rem;
}

.three-view-panel .three-viewport-wrap {
  flex: 1;
  min-height: 0;
  border-top: 0;
}

.three-view-panel #threeViewport {
  min-height: 0;
}

@media (max-width: 1240px) {
  .preview-panel,
  .three-view-panel,
  .three-settings-panel {
    border-radius: 24px;
    border-left: 1px solid var(--line);
    border-right: 1px solid var(--line);
  }

  .three-view-panel,
  .three-settings-panel {
    min-height: 0;
  }
}

#threeViewport,
#threeViewport canvas {
  touch-action: none;
  user-select: none;
}

#threeViewport canvas {
  display: block;
  width: 100% !important;
  height: 100% !important;
}

.view-preset-buttons .seg-button {
  padding-left: 0.25rem;
  padding-right: 0.25rem;
  font-size: 0.72rem;
}

@media print {
  @page {
    margin: 0;
  }

  html,
  body {
    width: auto !important;
    height: auto !important;
    margin: 0 !important;
    padding: 0 !important;
    overflow: visible !important;
    background: white !important;
  }

  body * {
    visibility: hidden !important;
  }

  .app,
  .preview-panel,
  .preview-wrap,
  .pages,
  .paper,
  .paper * {
    visibility: visible !important;
  }

  aside,
  .three-view-panel,
  .three-settings-panel,
  .panel-top,
  .preview-actions,
  .three-estimate-overlay {
    display: none !important;
    visibility: hidden !important;
  }

  .app,
  .preview-panel,
  .preview-wrap,
  .pages {
    display: block !important;
    width: auto !important;
    height: auto !important;
    min-height: 0 !important;
    margin: 0 !important;
    padding: 0 !important;
    border: 0 !important;
    border-radius: 0 !important;
    box-shadow: none !important;
    overflow: visible !important;
    background: white !important;
  }

  .paper {
    display: block !important;
    position: static !important;
    width: auto !important;
    height: auto !important;
    margin: 0 !important;
    padding: 0 !important;
    border: 0 !important;
    box-shadow: none !important;
    background: white !important;
    page-break-after: always;
  }

  .page-svg {
    display: block !important;
    width: auto !important;
    height: auto !important;
    max-height: none !important;
    background: white !important;
  }
}

.estimate-card {
  display: grid;
  gap: 0.34rem;
  margin-top: 0.25rem;
  padding: 0.65rem;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: #fffdf9;
  color: var(--muted);
  font-size: 0.76rem;
  line-height: 1.25;
}

.estimate-card b {
  color: var(--ink);
  font-weight: 750;
}

.estimate-row {
  display: flex;
  justify-content: space-between;
  gap: 0.75rem;
}

.estimate-row span:last-child {
  color: var(--ink);
  font-weight: 700;
  text-align: right;
}

.preview-titlebar {
  align-items: center;
}

.preview-actions {
  display: flex;
  align-items: center;
  gap: 0.42rem;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.preview-actions button {
  padding: 0.44rem 0.54rem;
  border-radius: 11px;
  font-size: 0.7rem;
  line-height: 1;
  white-space: nowrap;
  box-shadow: none;
}

#edgeTreatmentButtons {
  grid-template-columns: repeat(3, 1fr);
}

.three-viewport-wrap {
  position: relative;
  overflow: hidden;
}

.three-estimate-overlay {
  position: absolute;
  top: 0.95rem;
  right: 0.95rem;
  z-index: 4;
  width: min(250px, calc(100% - 1.9rem));
  pointer-events: none;
}

.three-estimate-overlay .estimate-card {
  margin: 0;
  background: rgba(255, 253, 249, 0.9);
  backdrop-filter: blur(5px);
  box-shadow: 0 14px 28px rgba(47, 36, 27, 0.12);
}

@media (max-width: 1240px) {
  .preview-titlebar {
    align-items: flex-start;
    flex-direction: column;
  }

  .preview-actions {
    justify-content: flex-start;
  }
}

@media print {
  .preview-actions,
  .three-estimate-overlay {
    display: none !important;
    visibility: hidden !important;
  }
}

.panel-top,
.preview-titlebar,
.three-view-panel .panel-top,
.three-settings-panel .panel-top {
  height: 58px;
  min-height: 58px;
  flex: 0 0 58px;
  align-items: center;
  box-sizing: border-box;
  padding-top: 0.65rem;
  padding-bottom: 0.65rem;
}

.preview-actions {
  flex-wrap: nowrap;
}

@media (max-width: 1240px) {
  .panel-top,
  .preview-titlebar,
  .three-view-panel .panel-top,
  .three-settings-panel .panel-top {
    height: auto;
    min-height: 58px;
  }

  .preview-actions {
    flex-wrap: wrap;
  }
}


@media (max-width: 1240px) {
  html,
  body {
    min-height: 100%;
    height: auto;
    overflow: auto;
  }

  .app {
    display: flex;
    flex-direction: column;
    width: 100%;
    min-height: 100vh;
    height: auto;
    gap: 0.85rem;
    padding: 0.55rem;
  }

  aside,
  .preview-panel,
  .three-view-panel,
  .three-settings-panel {
    width: 100%;
    max-width: none;
    min-width: 0;
    height: auto;
    min-height: 0;
    border: 1px solid var(--line);
    border-radius: 22px;
    overflow: visible;
  }

  aside {
    max-height: none;
    overflow: visible;
  }

  .panel-top,
  .preview-titlebar,
  .three-view-panel .panel-top,
  .three-settings-panel .panel-top {
    height: auto;
    min-height: 58px;
    flex: 0 0 auto;
  }

  .preview-panel {
    flex: 0 0 auto;
    min-height: 74vh;
  }

  .preview-wrap {
    flex: 0 0 auto;
    height: 68vh;
    min-height: 430px;
    overflow: auto;
    align-items: flex-start;
    justify-content: flex-start;
  }

  .pages {
    height: 100%;
    min-width: max-content;
    align-items: flex-start;
  }

  .paper {
    height: 100%;
    min-height: 0;
  }

  .page-svg {
    height: 100%;
    max-height: 100%;
  }

  .three-view-panel {
    flex: 0 0 auto;
    min-height: 0;
  }

  .three-viewport-wrap {
    flex: 0 0 auto;
    height: clamp(340px, 58vh, 560px);
    min-height: 0;
    max-height: 560px;
  }

  .three-view-panel #threeViewport {
    min-height: 0;
  }

  .three-settings-panel {
    flex: 0 0 auto;
  }

  .three-settings-panel .three-controls {
    max-height: none;
    overflow: visible;
  }
}

@media (max-width: 700px) {
  .app {
    padding: 0.45rem;
    gap: 0.7rem;
  }

  aside,
  .preview-panel,
  .three-view-panel,
  .three-settings-panel {
    border-radius: 18px;
  }

  .row,
  .shape-toggle {
    grid-template-columns: 1fr 1fr;
  }

  .preview-titlebar {
    align-items: flex-start;
    flex-direction: column;
  }

  .preview-actions {
    width: 100%;
    justify-content: flex-start;
    flex-wrap: wrap;
  }

  .preview-actions button {
    flex: 1 1 8.5rem;
  }

  .preview-wrap {
    height: 62vh;
    min-height: 360px;
  }

  .three-viewport-wrap {
    height: clamp(320px, 54vh, 500px);
    min-height: 0;
    max-height: 500px;
  }

  .three-estimate-overlay {
    top: 0.65rem;
    right: 0.65rem;
    width: min(230px, calc(100% - 1.3rem));
  }

  .three-estimate-overlay .estimate-card {
    border-radius: 12px;
    font-size: 0.7rem;
  }
}


@media (max-width: 1240px) {
  .three-view-panel {
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }

  .three-view-panel .three-viewport-wrap {
    display: block;
    position: relative;
    overflow: hidden;
    flex: 0 0 auto;
  }

  .three-view-panel #threeViewport {
    position: relative;
    display: block;
    height: 100%;
    max-height: 100%;
  }

  .three-settings-panel {
    position: relative;
    z-index: 1;
    overflow: visible;
  }
}

@media (max-width: 1240px) {
  .app.hide-3d {
    display: flex;
    flex-direction: column;
  }

  .app.hide-3d .preview-panel {
    border-radius: 24px;
    border-left: 1px solid var(--line);
  }
}
