:root {
  --bg: #f4f2ed;
  --panel: #ffffff;
  --ink: #1f1f1f;
  --muted: #74706a;
  --line: #ddd8cf;
  --accent: #2f6b4f;
  --accent-dark: #214d39;
  --stage: #f9f7f2;
  --shadow: 0 18px 40px rgba(34, 28, 20, 0.12);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--ink);
  font-family:
    -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei",
    sans-serif;
}

button,
select,
textarea {
  font: inherit;
}

button {
  border: 0;
  cursor: pointer;
}

.viewer-app {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 360px;
  min-height: 100vh;
  min-height: 100dvh;
}

.stage {
  position: relative;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  min-width: 0;
  overflow: hidden;
  background: var(--stage);
}

.viewer-topbar,
.estimate-bar {
  position: relative;
  z-index: 3;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 20px;
}

.brand-mark span,
.brand-mark strong {
  display: block;
}

.brand-mark span {
  color: var(--accent);
  font-size: 13px;
  font-weight: 800;
  letter-spacing: 0.12em;
}

.brand-mark strong {
  margin-top: 3px;
  font-size: 24px;
}

.toolbar-btn,
.estimate-bar button {
  min-height: 42px;
  border-radius: 8px;
  background: var(--ink);
  color: #fff;
  padding: 0 16px;
  font-weight: 700;
}

.viewer-canvas {
  position: relative;
  display: grid;
  min-height: 0;
  overflow: hidden;
  place-items: center;
  padding: 56px 76px 80px;
  touch-action: none;
}

.three-stage {
  position: absolute;
  inset: 0;
  z-index: 1;
}

.three-stage canvas {
  display: block;
  width: 100%;
  height: 100%;
  cursor: default;
}

.three-stage canvas.hovering-model {
  cursor: grab;
}

.three-stage canvas.dragging-model {
  cursor: grabbing;
}

.room-grid {
  position: absolute;
  inset: 0;
  background: radial-gradient(circle at 50% 68%, #fff 0%, #f3efe8 62%, #e9e4dc 100%);
}

.system-preview {
  display: none;
}

.system-preview::after {
  content: "";
  position: absolute;
  right: 20px;
  bottom: 28px;
  left: 20px;
  height: 18px;
  border-radius: 50%;
  background: rgba(34, 28, 20, 0.13);
  filter: blur(12px);
}

.system-preview.has-wheels .preview-unit:not(.accessory)::before,
.system-preview.has-wheels .preview-unit:not(.accessory)::after {
  content: "";
  position: absolute;
  bottom: -16px;
  width: 16px;
  height: 16px;
  border: 3px solid #222;
  border-radius: 50%;
  background: #fff;
}

.system-preview.has-wheels .preview-unit::before {
  left: 16px;
}

.system-preview.has-wheels .preview-unit::after {
  right: 16px;
}

.preview-empty {
  position: relative;
  z-index: 1;
  color: var(--muted);
  font-weight: 700;
}

.preview-unit {
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-rows: repeat(var(--rows, 3), 1fr);
  flex: 0 0 var(--unit-width);
  width: var(--unit-width);
  height: var(--unit-height);
  min-width: 84px;
  border: 5px solid var(--wood);
  border-radius: 6px;
  background: color-mix(in srgb, var(--wood) 16%, white);
  box-shadow: 0 22px 28px rgba(32, 27, 21, 0.18);
  transform: rotateX(1deg) rotateY(-8deg);
  transform-origin: bottom center;
  display: none;
}

.preview-unit span {
  border-bottom: 4px solid var(--wood);
}

.preview-unit span:last-child {
  border-bottom: 0;
}

.preview-unit.with-image {
  display: block;
  border: 0;
  background: transparent;
}

.preview-unit.with-image img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.preview-unit.accessory {
  align-self: center;
  height: 120px;
  opacity: 0.95;
}

.preview-unit.dragging {
  opacity: 0.55;
}

.preview-unit button {
  position: absolute;
  top: -13px;
  right: -13px;
  width: 28px;
  height: 28px;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: #fff;
}

.preview-unit strong {
  position: absolute;
  right: 8px;
  bottom: 8px;
  left: 8px;
  overflow: hidden;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.9);
  color: var(--muted);
  font-size: 12px;
  padding: 4px 8px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.canvas-hint {
  position: absolute;
  bottom: 20px;
  left: 50%;
  z-index: 2;
  margin: 0;
  transform: translateX(-50%);
  color: var(--muted);
  font-size: 13px;
}

.selected-list {
  display: grid;
  gap: 8px;
}

.selected-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  padding: 10px;
}

.selected-actions {
  display: flex;
  gap: 5px;
}

.selected-item strong,
.selected-item span {
  display: block;
  min-width: 0;
}

.selected-item strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.selected-item span {
  color: var(--muted);
  font-size: 12px;
}

.selected-item button {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: #f0ede6;
}

.floating-tools {
  position: absolute;
  top: 86px;
  left: 20px;
  z-index: 4;
  display: grid;
  gap: 8px;
}

.tool-icon {
  display: grid;
  width: 44px;
  height: 44px;
  place-items: center;
  border-radius: 8px;
  background: #262626;
  color: #fff;
  font-size: 18px;
}

.estimate-bar {
  border-top: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.86);
  backdrop-filter: blur(12px);
}

.estimate-bar div {
  min-width: 150px;
}

.estimate-bar span {
  display: block;
  color: var(--muted);
  font-size: 13px;
}

.estimate-bar strong {
  display: block;
  margin-top: 3px;
  font-size: 22px;
}

.estimate-bar small {
  display: block;
  color: var(--muted);
  font-size: 13px;
  font-weight: 500;
  text-decoration: line-through;
}

.config-panel {
  display: grid;
  align-content: start;
  gap: 16px;
  max-height: 100vh;
  overflow: auto;
  border-left: 1px solid var(--line);
  background: var(--panel);
  padding: 18px;
}

.panel-section {
  display: grid;
  gap: 10px;
}

.panel-section label,
.label,
.mini-label {
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

.row-between {
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
}

.row-between p {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 12px;
}

select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
}

select {
  height: 44px;
  padding: 0 12px;
}

.switch {
  position: relative;
  width: 52px;
  height: 30px;
  border-radius: 999px;
  background: #d5d1c8;
  padding: 3px;
}

.switch span {
  display: block;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #fff;
  transition: transform 160ms ease;
}

.switch.active {
  background: var(--accent);
}

.switch.active span {
  transform: translateX(22px);
}

.color-grid,
.material-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.color-grid button,
.material-grid button {
  display: grid;
  gap: 5px;
  justify-items: center;
  width: 64px;
  background: transparent;
  color: var(--muted);
  font-size: 11px;
}

.color-grid button span,
.material-grid button span {
  width: 42px;
  height: 42px;
  border: 2px solid #fff;
  border-radius: 50%;
  box-shadow: 0 0 0 1px var(--line);
}

.color-grid button.active span,
.material-grid button.active span {
  box-shadow: 0 0 0 3px var(--accent);
}

.color-grid strong,
.material-grid strong {
  overflow: hidden;
  max-width: 62px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tabbar {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  border-radius: 8px;
  background: #efede8;
  padding: 4px;
}

.tabbar button {
  min-height: 40px;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  font-weight: 800;
}

.tabbar button.active {
  background: #fff;
  color: var(--ink);
  box-shadow: 0 4px 12px rgba(34, 28, 20, 0.08);
}

.bay-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.bay-tabs:empty {
  display: none;
}

.bay-tabs button {
  min-height: 36px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--muted);
  padding: 0 10px;
  font-size: 12px;
  font-weight: 800;
}

.bay-tabs button.active {
  border-color: var(--accent);
  background: #e7f0eb;
  color: var(--accent-dark);
}

.product-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.product-card {
  position: relative;
  display: grid;
  gap: 7px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  padding: 8px;
  text-align: left;
  touch-action: manipulation;
}

.product-card:hover {
  border-color: var(--accent);
}

.product-thumb {
  display: grid;
  aspect-ratio: 1;
  overflow: hidden;
  place-items: center;
  border-radius: 6px;
  background: #f2efe8;
}

.product-thumb.model-thumb {
  background: #f9f7f2;
  min-height: 118px;
}

.product-count {
  position: absolute;
  top: 12px;
  right: 12px;
  display: grid;
  min-width: 24px;
  height: 24px;
  place-items: center;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-size: 12px;
  font-weight: 800;
  padding: 0 7px;
  box-shadow: 0 4px 10px rgba(33, 77, 57, 0.24);
}

.product-thumb img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.product-placeholder {
  display: grid;
  width: 72%;
  height: 62%;
  place-items: center;
  border: 5px solid var(--wood);
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.product-card strong,
.product-card span,
.product-card em {
  display: block;
}

.product-card strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.product-card span {
  color: var(--muted);
  font-size: 12px;
}

.product-card em {
  color: var(--accent-dark);
  font-style: normal;
  font-weight: 800;
}

dialog {
  width: min(620px, calc(100vw - 32px));
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

dialog::backdrop {
  background: rgba(24, 22, 19, 0.38);
}

dialog header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}

dialog h2 {
  margin: 0;
}

dialog header button {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: #f0ede6;
}

.preset-list {
  display: grid;
  gap: 10px;
}

.preset-card {
  display: grid;
  gap: 5px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  padding: 14px;
  text-align: left;
}

.preset-card:hover {
  border-color: var(--accent);
}

.preset-card span {
  color: var(--muted);
}

.preset-card em {
  color: var(--accent-dark);
  font-style: normal;
  font-weight: 800;
}

.mini-button {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  padding: 7px 10px;
  font-size: 12px;
  font-weight: 800;
}

textarea {
  min-height: 260px;
  padding: 12px;
  resize: vertical;
}

@media (max-width: 980px) {
  body {
    overflow: hidden;
  }

  .viewer-app {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(390px, 58dvh) minmax(0, 42dvh);
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
  }

  .stage {
    min-height: 0;
  }

  .config-panel {
    max-height: none;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    border-top: 1px solid var(--line);
    border-left: 0;
    padding: 14px;
    padding-bottom: calc(18px + env(safe-area-inset-bottom));
  }

  .viewer-topbar,
  .estimate-bar {
    padding: 12px 14px;
  }

  .brand-mark span {
    font-size: 11px;
  }

  .brand-mark strong {
    font-size: 20px;
  }

  .toolbar-btn,
  .estimate-bar button {
    min-height: 40px;
    padding: 0 12px;
    font-size: 13px;
  }

  .viewer-canvas {
    padding: 24px 16px 46px 54px;
  }

  .floating-tools {
    top: 118px;
    left: 10px;
    gap: 6px;
  }

  .tool-icon {
    width: 38px;
    height: 38px;
    border-radius: 7px;
    font-size: 16px;
  }

  .canvas-hint {
    bottom: 12px;
    width: calc(100% - 32px);
    text-align: center;
    font-size: 12px;
  }

  .estimate-bar {
    display: grid;
    grid-template-columns: 1fr 1fr auto;
    gap: 10px;
    align-items: center;
  }

  .estimate-bar div {
    min-width: 0;
  }

  .estimate-bar strong {
    font-size: 18px;
  }

  .panel-section {
    gap: 8px;
  }

  .color-grid,
  .material-grid {
    gap: 8px;
  }

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

  .product-card {
    gap: 6px;
    padding: 7px;
  }

  .product-thumb.model-thumb {
    min-height: 104px;
  }

  .selected-item {
    padding: 9px;
  }
}

@media (max-width: 620px) {
  .viewer-app {
    grid-template-rows: minmax(360px, 56dvh) minmax(0, 44dvh);
  }

  .viewer-topbar {
    gap: 8px;
  }

  .brand-mark strong {
    font-size: 18px;
  }

  .viewer-canvas {
    padding: 18px 10px 38px 50px;
  }

  .floating-tools {
    top: 104px;
  }

  .canvas-hint {
    display: none;
  }

  .estimate-bar {
    grid-template-columns: 1fr 1fr;
  }

  .estimate-bar button {
    grid-column: 1 / -1;
    width: 100%;
  }

  .config-panel {
    gap: 12px;
    padding: 12px;
    padding-bottom: calc(16px + env(safe-area-inset-bottom));
  }

  .product-thumb.model-thumb {
    min-height: 96px;
  }

  .product-card strong {
    font-size: 13px;
  }

  .product-card span,
  .product-card em {
    font-size: 12px;
  }

  .color-grid button,
  .material-grid button {
    width: 58px;
  }

  .color-grid button span,
  .material-grid button span {
    width: 38px;
    height: 38px;
  }
}

@media (max-width: 390px) {
  .viewer-app {
    grid-template-rows: minmax(330px, 54dvh) minmax(0, 46dvh);
  }

  .viewer-topbar,
  .estimate-bar {
    padding: 10px;
  }

  .viewer-canvas {
    padding-left: 44px;
  }

  .tool-icon {
    width: 36px;
    height: 36px;
  }

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