:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --ink: #1f2a37;
  --muted: #5b6676;
  --line: #e3ded4;
  --accent: #f26b3a;
  --accent-ink: #ffffff;
  --accent-soft: #fde6dc;
  --focus: #2563eb;
  --error-bg: #fdecec;
  --error-ink: #a12020;
  --viewer-bg: #eef2f6;
  --radius: 14px;
  font-family: "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", system-ui, sans-serif;
  color: var(--ink);
  background: var(--bg);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  height: 100vh;
  height: 100dvh;
  background: var(--bg);
  display: flex;
  flex-direction: column;
}

button, input { font: inherit; }

:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}

.brand {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 800;
  white-space: nowrap;
}

.brand-mark { color: var(--accent); }

.template-tabs {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  scrollbar-width: none;
  flex: 1;
}

.template-tabs button {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  border-radius: 999px;
  padding: 8px 16px;
  white-space: nowrap;
  cursor: pointer;
  font-weight: 600;
}

.template-tabs button[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}

.workspace {
  flex: 1;
  overflow: hidden;
  display: grid;
  grid-template-columns: minmax(320px, 400px) 1fr;
  grid-template-areas: "controls viewer";
  gap: 16px;
  padding: 16px;
  min-height: 0;
}

.viewer-panel {
  grid-area: viewer;
  position: relative;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  min-height: 420px;
}

.viewer {
  flex: 1;
  min-height: 300px;
  background: var(--viewer-bg);
  touch-action: none;
}

.viewer canvas { display: block; width: 100%; height: 100%; }

.viewer-status {
  position: absolute;
  top: 12px;
  left: 12px;
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--line);
  padding: 6px 12px;
  border-radius: 999px;
  font-size: 0.9rem;
}

.viewer-status[hidden] { display: none; }

.spinner {
  width: 14px;
  height: 14px;
  border: 2px solid var(--accent-soft);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

.viewer-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  border-top: 1px solid var(--line);
  flex-wrap: wrap;
}

.dimensions {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
}

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

.primary-button, .ghost-button {
  border-radius: 10px;
  padding: 10px 16px;
  cursor: pointer;
  font-weight: 700;
}

.primary-button {
  border: none;
  background: var(--accent);
  color: var(--accent-ink);
}

.primary-button:disabled { opacity: 0.45; cursor: not-allowed; }

.ghost-button {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
}

.controls-panel {
  grid-area: controls;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 20px;
  overflow-y: auto;
}

.panel-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.panel-heading h2 { margin: 0; font-size: 1.4rem; }

.panel-heading .ghost-button { padding: 6px 12px; font-size: 0.9rem; }

.tips {
  margin: 8px 0 16px;
  padding: 10px 12px;
  background: var(--accent-soft);
  border-radius: 10px;
  font-size: 0.95rem;
}

.param-form { display: flex; flex-direction: column; gap: 16px; }

.field { display: flex; flex-direction: column; gap: 6px; }

.field-label {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-weight: 600;
}

.field-label .unit { color: var(--muted); font-weight: 400; font-size: 0.9rem; }

.field input[type="text"] {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 1.05rem;
}

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

.slider-row input[type="range"] { flex: 1; accent-color: var(--accent); min-height: 28px; }

.slider-row input[type="number"] {
  width: 76px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 6px 8px;
  font-variant-numeric: tabular-nums;
}

.switch-field {
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
}

.switch-field input[type="checkbox"] { width: 24px; height: 24px; accent-color: var(--accent); }

.error-message {
  margin: 16px 0 0;
  padding: 10px 12px;
  background: var(--error-bg);
  color: var(--error-ink);
  border-radius: 10px;
}

.error-message[hidden] { display: none; }

.hint { color: var(--muted); font-size: 0.85rem; margin: 16px 0 0; }

/* Tablets and phones: 3D view on top, controls below. */
@media (max-width: 900px) {
  body { height: auto; min-height: 100vh; }
  .workspace {
    overflow: visible;
    grid-template-columns: 1fr;
    grid-template-areas: "viewer" "controls";
    padding: 12px;
  }
  .viewer-panel { min-height: 0; }
  .viewer { height: 50vh; flex: none; }
  .topbar { padding: 10px 12px; }
  .template-tabs { flex-basis: 100%; }
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14171c;
    --surface: #1d2229;
    --ink: #e8ecf1;
    --muted: #9aa6b5;
    --line: #2e3540;
    --accent-soft: #3a2419;
    --error-bg: #3b1d1d;
    --error-ink: #ffb4b4;
    --viewer-bg: #232a33;
  }
}
