:root {
  --bg: #f4f1ea;
  --panel: #faf8f4;
  --ink: #2a2620;
  --muted: #6b645a;
  --silver: #c5c8ce;
  --wood: #b08968;
  --accent: #f0d45a;
  --accent-real: #e8b923;
  --wall: #3d3a36;
  --room: rgba(176, 137, 104, 0.28);
  --border: #ddd6cb;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: "Segoe UI", "PingFang SC", "Noto Sans SC", "PingFang TC", system-ui, sans-serif;
  color: var(--ink);
  background: linear-gradient(160deg, #ebe7df 0%, var(--bg) 40%, #e8e4dc 100%);
  min-height: 100vh;
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
  padding-bottom: env(safe-area-inset-bottom);
}

.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.75rem 1.25rem;
  background: rgba(250, 248, 244, 0.92);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(8px);
  position: sticky;
  top: 0;
  z-index: 10;
}
.brand { display: flex; align-items: baseline; gap: 0.75rem; }
.logo {
  font-weight: 700;
  font-size: 1.35rem;
  letter-spacing: 0.04em;
  color: var(--wood);
}
.sub { color: var(--muted); font-size: 0.85rem; }
.banner {
  background: var(--accent-real);
  color: #2a2208;
  font-weight: 600;
  font-size: 0.8rem;
  padding: 0.35rem 0.85rem;
  border-radius: 999px;
  box-shadow: 0 1px 0 rgba(0,0,0,0.06);
}

.layout {
  display: grid;
  grid-template-columns: minmax(260px, 340px) 1fr;
  gap: 0;
  min-height: calc(100vh - 56px);
}

.panel {
  padding: 1rem 1.1rem 2rem;
  background: var(--panel);
  border-right: 1px solid var(--border);
  overflow: auto;
  max-height: calc(100vh - 56px);
}
.panel h2 {
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin: 0 0 0.6rem;
}
.panel section { margin-bottom: 1.4rem; }

.samples { display: flex; flex-direction: column; gap: 0.4rem; }
.sample-btn {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  border: 1px solid var(--border);
  background: #fff;
  border-radius: 10px;
  padding: 0.4rem 0.55rem;
  cursor: pointer;
  text-align: left;
  color: inherit;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.sample-btn img {
  width: 56px; height: 48px; object-fit: cover; border-radius: 6px; background: #eee;
}
.sample-btn.active, .sample-btn:hover {
  border-color: var(--accent-real);
  box-shadow: 0 0 0 2px rgba(232, 185, 35, 0.25);
}
.sample-btn .meta { font-size: 0.8rem; }
.sample-btn .meta strong { display: block; }

.upload {
  display: inline-block;
  margin-top: 0.6rem;
  font-size: 0.85rem;
  color: var(--wood);
  cursor: pointer;
  text-decoration: underline;
}
.upload input { display: none; }
.preview {
  display: block;
  width: 100%;
  max-height: 160px;
  object-fit: contain;
  margin-top: 0.6rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #fff;
}

.providers { display: flex; flex-direction: column; gap: 0.35rem; margin-bottom: 0.75rem; }
.providers label { font-size: 0.92rem; cursor: pointer; }

.btn {
  border: none;
  border-radius: 10px;
  padding: 0.65rem 1rem;
  font-weight: 600;
  cursor: pointer;
  width: 100%;
}
.btn.primary {
  background: linear-gradient(180deg, #f0d45a, var(--accent-real));
  color: #2a2208;
  box-shadow: 0 2px 0 #c49a12;
}
.btn.primary:disabled { opacity: 0.55; cursor: wait; }
.status { font-size: 0.8rem; color: var(--muted); min-height: 1.2em; }
.status.error { color: #a33; }
.status.ok { color: #2f6b3a; }

.json-sec pre {
  background: #1e1c19;
  color: #e8e2d6;
  border-radius: 10px;
  padding: 0.75rem;
  font-size: 0.72rem;
  max-height: 280px;
  overflow: auto;
  margin: 0;
  border: 1px solid #333;
}
.json-toggle {
  all: unset;
  display: flex;
  align-items: center;
  gap: 0.35rem;
  width: 100%;
  cursor: pointer;
  font: inherit;
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  font-weight: 600;
}
.json-toggle .chev { transition: transform 0.15s; display: inline-block; }
.json-sec.open .json-toggle .chev { transform: rotate(90deg); }
.json-sec:not(.open) #json { display: none; }

/* —— Studio: 2D primary + 3D secondary —— */
.studio {
  display: flex;
  flex-direction: column;
  min-height: calc(100vh - 56px);
  background: #d8dce2;
  min-width: 0;
}
.studio-tabs {
  display: none;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  padding: 0.35rem 0.5rem;
  gap: 0.35rem;
}
.studio-tab {
  flex: 1;
  border: 1px solid var(--border);
  background: #fff;
  border-radius: 8px;
  padding: 0.55rem 0.75rem;
  font-weight: 600;
  font-size: 0.9rem;
  color: var(--muted);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.studio-tab.active {
  background: linear-gradient(180deg, #f0d45a, var(--accent-real));
  color: #2a2208;
  border-color: #c49a12;
}
.studio-body {
  flex: 1;
  display: grid;
  grid-template-columns: 1.35fr 1fr;
  min-height: 0;
  min-width: 0;
}

.pane-2d {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  border-right: 1px solid var(--border);
  background: #ece8e0;
}
.pane-3d {
  position: relative;
  min-width: 0;
  min-height: 0;
  background: radial-gradient(ellipse at 50% 30%, #dfe3e8 0%, #c8ccd2 55%, #b8bdc4 100%);
}

.editor-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.45rem 0.75rem;
  padding: 0.45rem 0.65rem;
  background: rgba(250, 248, 244, 0.95);
  border-bottom: 1px solid var(--border);
  z-index: 3;
}
.tool-group {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
  align-items: center;
}
.tool-btn {
  border: 1px solid var(--border);
  background: #fff;
  border-radius: 8px;
  padding: 0.4rem 0.65rem;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--ink);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  min-height: 36px;
}
.tool-btn.active {
  background: var(--accent-real);
  border-color: #c49a12;
  color: #2a2208;
}
.tool-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.scale-toggle, .opacity-label {
  font-size: 0.78rem;
  color: var(--muted);
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  white-space: nowrap;
}
.opacity-label input[type="range"] {
  width: 72px;
  vertical-align: middle;
}

.editor-stage {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}
.editor-stage[data-overlay="split"] {
  display: grid;
  grid-template-rows: minmax(100px, 32%) 1fr;
}
.split-original {
  background: #1e1c19;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  min-height: 0;
}
.split-original img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}
.canvas-wrap {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow: hidden;
  touch-action: none;
  background:
    linear-gradient(90deg, rgba(0,0,0,0.03) 1px, transparent 1px),
    linear-gradient(rgba(0,0,0,0.03) 1px, transparent 1px);
  background-size: 24px 24px;
  background-color: #f2efe8;
}
#plan2d {
  display: block;
  width: 100%;
  height: 100%;
  cursor: crosshair;
  touch-action: none;
}
.canvas-wrap.tool-select #plan2d { cursor: grab; }
.canvas-wrap.dragging #plan2d { cursor: grabbing; }
.editor-hint {
  position: absolute;
  top: 0.65rem;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(42, 38, 32, 0.78);
  color: #faf8f4;
  font-size: 0.75rem;
  font-weight: 600;
  padding: 0.35rem 0.75rem;
  border-radius: 999px;
  pointer-events: none;
  z-index: 2;
  white-space: nowrap;
  max-width: 92%;
  text-align: center;
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
}

.pip {
  position: absolute;
  right: 0.75rem;
  bottom: 0.75rem;
  width: min(42%, 220px);
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 6px 20px rgba(0,0,0,0.18);
  z-index: 4;
  overflow: hidden;
  touch-action: none;
}
.pip-bar {
  background: var(--wood);
  color: #fff;
  font-size: 0.72rem;
  font-weight: 600;
  padding: 0.3rem 0.55rem;
  cursor: grab;
  user-select: none;
}
.pip.dragging .pip-bar { cursor: grabbing; }
.pip img {
  display: block;
  width: 100%;
  max-height: 160px;
  object-fit: contain;
  background: #faf8f4;
  pointer-events: none;
}

#viewport { width: 100%; height: 100%; min-height: 200px; }
.legend {
  position: absolute;
  left: 0.75rem;
  bottom: 0.75rem;
  background: rgba(250,248,244,0.9);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.35rem 0.6rem;
  font-size: 0.72rem;
  display: flex;
  gap: 0.65rem;
  align-items: center;
  flex-wrap: wrap;
  max-width: calc(100% - 1.5rem);
  z-index: 2;
}
.swatch {
  display: inline-block;
  width: 12px; height: 12px;
  border-radius: 2px;
  margin-right: 0.25rem;
  vertical-align: -1px;
}
.swatch.wall { background: var(--wall); }
.swatch.room { background: var(--wood); opacity: 0.55; }
.swatch.door { background: #5b8def; }
.swatch.window { background: #7ec8e3; }
.swatch.wall-sel { background: #e8b923; box-shadow: 0 0 0 1px #a67c12; }

.label2d {
  background: rgba(42, 38, 32, 0.72);
  color: #fff;
  padding: 2px 8px;
  border-radius: 6px;
  font-size: 12px;
  white-space: nowrap;
  pointer-events: none;
  transform: translate(-50%, -50%);
}
.view-hint {
  position: absolute;
  top: 0.65rem;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(42, 38, 32, 0.78);
  color: #faf8f4;
  font-size: 0.72rem;
  font-weight: 600;
  padding: 0.3rem 0.7rem;
  border-radius: 999px;
  pointer-events: none;
  z-index: 2;
  white-space: nowrap;
  max-width: 92%;
  text-align: center;
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
}
#viewport canvas { touch-action: none; cursor: grab; }
#viewport.dragging canvas,
#viewport.selecting canvas { cursor: grabbing; }

/* Desktop: both panes visible; tabs hidden */
@media (min-width: 901px) {
  .studio-tabs { display: none; }
  .pane-2d, .pane-3d { display: flex; flex-direction: column; }
  .pane-3d { display: block; }
}

/* Phone: tabbed 2D / 3D */
@media (max-width: 900px) {
  .top {
    flex-wrap: wrap;
    padding: 0.5rem 0.85rem;
    padding-top: max(0.5rem, env(safe-area-inset-top));
    gap: 0.4rem;
  }
  .logo { font-size: 1.15rem; }
  .sub { display: none; }
  .banner {
    font-size: 0.7rem;
    padding: 0.25rem 0.65rem;
    width: 100%;
    text-align: center;
  }

  .layout {
    display: flex;
    flex-direction: column;
    min-height: calc(100dvh - 56px);
  }

  .studio {
    order: -1;
    flex: 0 0 auto;
    height: min(62dvh, 560px);
    min-height: 300px;
    border-bottom: 1px solid var(--border);
  }
  .studio-tabs { display: flex; }
  .studio-body {
    grid-template-columns: 1fr;
    flex: 1;
    min-height: 0;
  }
  .pane-2d, .pane-3d {
    display: none;
    border-right: none;
    height: 100%;
  }
  .studio[data-mobile-tab="edit2d"] .pane-2d { display: flex; }
  .studio[data-mobile-tab="preview3d"] .pane-3d { display: block; }

  .editor-toolbar {
    padding: 0.35rem 0.45rem;
    gap: 0.35rem;
  }
  .tool-btn {
    padding: 0.35rem 0.5rem;
    font-size: 0.75rem;
    min-height: 34px;
  }
  .opacity-label input[type="range"] { width: 56px; }
  .editor-hint {
    font-size: 0.68rem;
    white-space: normal;
  }
  .pip { width: min(48%, 160px); bottom: 0.5rem; right: 0.5rem; }
  .pip img { max-height: 110px; }

  .panel {
    order: 1;
    max-height: none;
    border-right: none;
    padding: 0.85rem 0.9rem 1.5rem;
    padding-bottom: max(1.5rem, env(safe-area-inset-bottom));
    overflow: visible;
  }
  .panel h2 { margin-bottom: 0.5rem; }
  .panel section { margin-bottom: 1rem; }

  .sample-btn {
    min-height: 52px;
    padding: 0.5rem 0.65rem;
    -webkit-tap-highlight-color: transparent;
  }
  .sample-btn img { width: 64px; height: 54px; }
  .sample-btn .meta { font-size: 0.85rem; }

  .providers {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.45rem 0.75rem;
  }
  .providers label {
    font-size: 0.9rem;
    min-height: 36px;
    display: flex;
    align-items: center;
    gap: 0.35rem;
  }
  .btn.primary {
    min-height: 48px;
    font-size: 1.05rem;
    padding: 0.8rem 1rem;
  }
  .preview { max-height: 120px; }
  .json-sec pre { max-height: 180px; font-size: 0.68rem; }
  .view-hint { font-size: 0.68rem; }
}

@media (max-width: 420px) {
  .studio { height: min(56dvh, 460px); }
  .providers { grid-template-columns: 1fr; }
  .tool-scale { width: 100%; }
}
