:root {
  color-scheme: dark;
  --bg: #0b0d10;
  --panel: #111419;
  --surface: #1a1f26;
  --surface-hover: #20262f;
  --border: rgba(255,255,255,.085);
  --border-strong: rgba(255,255,255,.15);
  --text: #f2f5f7;
  --muted: #8d97a4;
  --subtle: #66717e;
  --accent: #23a9ff;
  --accent-hover: #46b8ff;
  --accent-soft: rgba(35,169,255,.12);
  --success: #64d8a5;
  --warning: #ffca68;
  --danger: #ff7a7a;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; background: var(--bg); color: var(--text); }
body { min-width: 1060px; overflow: hidden; }
button, input { font: inherit; }
button { color: inherit; }
svg { width: 1.1em; height: 1.1em; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

.topbar {
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  border-bottom: 1px solid var(--border);
  background: rgba(11,13,16,.94);
  backdrop-filter: blur(16px);
}
.brand { display: flex; align-items: center; gap: 11px; }
.brand-icon { width: 34px; height: 34px; display: grid; place-items: center; border-radius: 10px; background: var(--accent-soft); color: var(--accent); }
.brand-icon svg { width: 21px; height: 21px; }
.brand > div:last-child { display: grid; gap: 1px; }
.brand strong { font-size: 15px; font-weight: 650; }
.brand span { font-size: 12px; color: var(--muted); }
.top-actions, .viewer-actions { display: flex; align-items: center; gap: 8px; }
.version { font-size: 11px; color: var(--muted); padding: 5px 8px; border: 1px solid var(--border); border-radius: 999px; }

.button {
  min-height: 36px;
  border: 1px solid transparent;
  border-radius: 9px;
  padding: 0 13px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s ease, border-color .15s ease, color .15s ease, opacity .15s ease;
}
.button svg { width: 16px; height: 16px; }
.button.primary { background: var(--accent); color: #051018; }
.button.primary:hover { background: var(--accent-hover); }
.button.subtle { background: var(--surface); border-color: var(--border); color: #d8dde2; }
.button.subtle:hover { background: var(--surface-hover); border-color: var(--border-strong); }
.button:disabled { opacity: .5; cursor: wait; }

.workspace { height: calc(100vh - 64px); display: grid; grid-template-columns: 350px minmax(0, 1fr); }
.panel { background: var(--panel); border-right: 1px solid var(--border); }
.controls { min-width: 0; }
.control-scroll { height: 100%; overflow-y: auto; padding: 18px 14px 28px; scrollbar-width: thin; scrollbar-color: #333a44 transparent; }
.group { padding: 15px 12px; border-bottom: 1px solid var(--border); }
.group:first-child { padding-top: 4px; }
.group:last-child { border-bottom: 0; }
.group-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.eyebrow { display: block; margin-bottom: 5px; color: var(--muted); font-size: 11px; font-weight: 650; letter-spacing: .08em; text-transform: uppercase; }
h1 { margin: 0; font-size: 18px; font-weight: 650; letter-spacing: -.015em; }
.icon-button { width: 32px; height: 32px; display: grid; place-items: center; border: 1px solid var(--border); border-radius: 9px; background: var(--surface); color: var(--muted); cursor: pointer; }
.icon-button:hover { color: var(--text); border-color: var(--border-strong); }
.icon-button svg { width: 16px; height: 16px; }

.input-grid, .detail-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.input-grid .field:first-child { grid-column: 1 / -1; }
.field { display: grid; gap: 6px; min-width: 0; }
.field > span { font-size: 12px; color: #c4cbd2; }
.unit-input { height: 38px; display: flex; align-items: center; gap: 6px; padding: 0 9px 0 0; border: 1px solid var(--border); border-radius: 9px; background: var(--surface); transition: border-color .15s ease, background .15s ease; }
.unit-input:focus-within { border-color: rgba(35,169,255,.65); background: #1b2129; box-shadow: 0 0 0 3px rgba(35,169,255,.08); }
.unit-input input { width: 100%; min-width: 0; height: 100%; padding: 0 0 0 10px; border: 0; outline: 0; background: transparent; color: var(--text); font-size: 13px; }
.unit-input em { white-space: nowrap; color: var(--muted); font-size: 10px; font-style: normal; }
input[type=number] { appearance: textfield; }
input[type=number]::-webkit-inner-spin-button, input[type=number]::-webkit-outer-spin-button { appearance: none; margin: 0; }

.segmented { display: grid; padding: 3px; border: 1px solid var(--border); border-radius: 10px; background: #0e1115; }
.segmented.four { grid-template-columns: repeat(4, 1fr); }
.segmented button, .view-switch button { min-height: 33px; border: 0; border-radius: 7px; background: transparent; color: var(--muted); font-size: 12px; font-weight: 600; cursor: pointer; }
.segmented button:hover, .view-switch button:hover { color: var(--text); }
.segmented button.active, .view-switch button.active { background: var(--surface-hover); color: var(--text); box-shadow: inset 0 0 0 1px var(--border); }

.mount-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.mount-card { min-height: 82px; padding: 10px 5px 9px; display: grid; place-items: center; align-content: center; gap: 6px; border: 1px solid var(--border); border-radius: 11px; background: var(--surface); color: var(--muted); cursor: pointer; }
.mount-card:hover { border-color: var(--border-strong); color: #dbe1e6; }
.mount-card.active { border-color: rgba(35,169,255,.62); background: var(--accent-soft); color: var(--text); }
.mount-card svg { width: 33px; height: 33px; stroke-width: 1.4; }
.mount-card span { font-size: 11px; font-weight: 600; text-align: center; }

.switch-row { margin-top: 11px; min-height: 48px; display: grid; grid-template-columns: 1fr auto; align-items: center; gap: 12px; cursor: pointer; }
.switch-row > span { display: grid; gap: 2px; }
.switch-row strong { font-size: 12px; font-weight: 600; }
.switch-row small { color: var(--muted); font-size: 10px; }
.switch-row input { position: absolute; opacity: 0; pointer-events: none; }
.switch-row i { width: 36px; height: 21px; padding: 3px; border-radius: 999px; background: #313842; transition: background .15s ease; }
.switch-row i::after { content: ""; display: block; width: 15px; height: 15px; border-radius: 50%; background: #d5dae0; transition: transform .15s ease; }
.switch-row input:checked + i { background: var(--accent); }
.switch-row input:checked + i::after { transform: translateX(15px); background: #fff; }

.expert-group { padding-left: 0; padding-right: 0; }
.expert-toggle { width: 100%; min-height: 50px; padding: 0 12px; display: flex; align-items: center; justify-content: space-between; gap: 12px; border: 0; background: transparent; cursor: pointer; text-align: left; }
.expert-toggle > span { display: grid; gap: 2px; }
.expert-toggle strong { font-size: 12px; font-weight: 600; }
.expert-toggle small { color: var(--muted); font-size: 10px; }
.expert-toggle svg { width: 16px; height: 16px; color: var(--muted); transition: transform .15s ease; }
.expert-toggle[aria-expanded=true] svg { transform: rotate(180deg); }
.expert-content { padding: 2px 12px 10px; }
details { border-top: 1px solid var(--border); }
summary { padding: 12px 0; color: #cbd2d9; font-size: 11px; font-weight: 650; cursor: pointer; }
.detail-grid { padding: 0 0 13px; }

.viewer-panel { min-width: 0; height: 100%; display: grid; grid-template-rows: 56px 38px minmax(0,1fr) auto; background: radial-gradient(circle at 50% 42%, #171d24 0, #0d1014 58%, #090b0e 100%); }
.viewer-toolbar { padding: 0 18px; display: grid; grid-template-columns: minmax(150px,1fr) auto minmax(190px,1fr); align-items: center; gap: 16px; border-bottom: 1px solid var(--border); background: rgba(12,15,19,.78); }
.model-status { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 12px; min-width: 0; }
.model-status span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.status-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--subtle); flex: 0 0 auto; }
.model-status[data-state=ready] .status-dot { background: var(--success); box-shadow: 0 0 0 4px rgba(100,216,165,.08); }
.model-status[data-state=dirty] .status-dot { background: var(--warning); }
.model-status[data-state=rendering] .status-dot { background: var(--accent); animation: pulse 1s infinite; }
.model-status[data-state=error] .status-dot { background: var(--danger); }
.viewer-actions { justify-self: end; }
.view-switch { justify-self: center; display: grid; grid-template-columns: repeat(3, minmax(74px, auto)); gap: 3px; padding: 3px; border: 1px solid var(--border); border-radius: 10px; background: #0e1115; }
.view-switch button { padding: 0 12px; }
.view-switch button[hidden] { display: none; }

.validation-strip { min-width: 0; padding: 0 18px; display: flex; align-items: center; border-bottom: 1px solid var(--border); background: rgba(12,15,19,.6); }
.validation { min-width: 0; display: flex; align-items: center; gap: 9px; color: #b7c0c8; font-size: 11px; }
.validation span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.validation-icon { width: 20px; height: 20px; display: grid; place-items: center; flex: 0 0 auto; border-radius: 50%; background: rgba(100,216,165,.12); color: var(--success); font-size: 11px; font-weight: 700; }
.validation.warning .validation-icon { background: rgba(255,202,104,.12); color: var(--warning); }
.validation.error .validation-icon { background: rgba(255,122,122,.12); color: var(--danger); }

.viewport { position: relative; min-height: 0; overflow: hidden; }
#threeCanvas { width: 100%; height: 100%; display: block; outline: none; }
.viewer-message { position: absolute; z-index: 2; inset: 0; display: grid; place-content: center; justify-items: center; gap: 10px; color: var(--muted); pointer-events: none; text-align: center; background: rgba(10,13,17,.42); backdrop-filter: blur(2px); }
.viewer-message strong { color: var(--text); font-size: 14px; }
.viewer-message > span { font-size: 11px; }
.viewer-message b { color: #cbd4dc; font-weight: 650; }
.viewer-message.hidden { display: none; }
.progress-track { width: min(320px, 42vw); height: 8px; overflow: hidden; border-radius: 999px; background: rgba(255,255,255,.1); box-shadow: inset 0 0 0 1px rgba(255,255,255,.05); }
.progress-fill { width: 0%; height: 100%; border-radius: inherit; background: var(--accent); transition: width .3s ease; position: relative; overflow: hidden; }
.progress-fill::after { content: ""; position: absolute; inset: 0; transform: translateX(-100%); background: linear-gradient(90deg, transparent, rgba(255,255,255,.38), transparent); animation: shimmer 1.15s linear infinite; }
.interaction-hint { position: absolute; left: 16px; bottom: 14px; color: rgba(215,222,229,.48); font-size: 10px; pointer-events: none; }

.metrics { display: grid; grid-template-columns: repeat(5, minmax(100px, 1fr)); gap: 1px; border-top: 1px solid var(--border); background: var(--border); }
.metrics > div { min-height: 62px; padding: 11px 14px; display: grid; align-content: center; gap: 4px; background: rgba(15,18,23,.94); }
.metrics span { color: var(--muted); font-size: 10px; }
.metrics strong { font-size: 14px; font-weight: 630; }

.toast { position: fixed; right: 20px; bottom: 20px; z-index: 20; max-width: 340px; padding: 11px 14px; border: 1px solid var(--border-strong); border-radius: 10px; background: #1b2027; color: var(--text); font-size: 12px; opacity: 0; transform: translateY(8px); pointer-events: none; transition: .18s ease; }
.toast.show { opacity: 1; transform: translateY(0); }

@keyframes pulse { 50% { opacity: .35; } }
@keyframes shimmer { to { transform: translateX(100%); } }

@media (max-width: 1240px) {
  .workspace { grid-template-columns: 330px minmax(0,1fr); }
  .viewer-toolbar { grid-template-columns: minmax(120px,1fr) auto minmax(160px,1fr); gap: 10px; padding: 0 12px; }
  .view-switch button { padding: 0 8px; }
  .metrics > div { padding-left: 10px; padding-right: 10px; }
}

/* v0.4 fixes and additions */
.workspace, .panel, .controls, .control-scroll, .viewer-panel { min-height: 0; }
.controls { overflow: hidden; }
.control-scroll {
  overflow-y: scroll;
  overflow-x: hidden;
  overscroll-behavior: contain;
  scrollbar-gutter: stable;
}
.button.active-toggle {
  color: var(--text);
  border-color: rgba(35,169,255,.48);
  background: var(--accent-soft);
}
.metrics { grid-template-columns: repeat(6, minmax(92px, 1fr)); }
.metric-compact { white-space: nowrap; }
.dimension-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
}
.dimension-label {
  padding: 3px 7px;
  border: 1px solid rgba(35,169,255,.42);
  border-radius: 6px;
  background: rgba(9,13,17,.88);
  color: #dbeeff;
  font-size: 10px;
  font-weight: 650;
  white-space: nowrap;
  box-shadow: 0 2px 8px rgba(0,0,0,.28);
  backdrop-filter: blur(5px);
}
@media (max-width: 1320px) {
  .metrics { grid-template-columns: repeat(3, minmax(120px, 1fr)); }
  .metrics > div { min-height: 52px; }
}

/* v0.5: compact expert toggle for automatic slit spacing */
.detail-grid .compact-switch { grid-column: 1 / -1; margin: 0 0 2px; min-height: 38px; padding: 2px 0; }
.detail-grid .compact-switch strong { font-size: 11px; }
.detail-grid .compact-switch small { font-size: 9px; }
.unit-input input:disabled { color: var(--muted); cursor: not-allowed; opacity: .72; }

/* ==========================================================================
   v0.7: mobile / tablet layout
   Everything below is additive. The desktop layout above is untouched: the
   new elements are hidden by default and only the <=900px media query
   rearranges anything.
   ========================================================================== */

/* Hidden on desktop, revealed on small screens. */
.sheet-head,
.mobile-bar,
.mobile-backdrop,
.interaction-hint .hint-touch { display: none; }

/* Only affects touch input; a mouse ignores it. Without this the browser
   claims drag gestures for scrolling and OrbitControls barely works. */
#threeCanvas { touch-action: none; }

@media (max-width: 900px) {
  /* The hard blocker for phones: the desktop layout has a fixed minimum. */
  body { min-width: 0; }

  /* ---------------------------------------------------------- top bar --- */
  .topbar { height: 56px; padding: 0 12px; gap: 8px; }
  .brand span { display: none; }
  .brand strong { font-size: 14px; }
  .brand-icon { width: 30px; height: 30px; }
  .version,
  #downloadConfig { display: none; }
  .top-actions { gap: 6px; }
  .top-actions .button { min-height: 40px; padding: 0 11px; }
  /* Keep the two download buttons as icons only - the labels do not fit. */
  #downloadStlTop, #downloadScadTop { font-size: 0; gap: 0; padding: 0 12px; }
  #downloadStlTop svg, #downloadScadTop svg { width: 18px; height: 18px; }

  /* --------------------------------------------------------- workspace --- */
  .workspace {
    grid-template-columns: 1fr;
    height: calc(100vh - 56px - 60px);
    height: calc(100dvh - 56px - 60px - env(safe-area-inset-bottom, 0px));
  }

  /* ------------------------------------------- controls as bottom sheet --- */
  .controls {
    position: fixed;
    left: 0; right: 0; bottom: 0;
    z-index: 30;
    height: 82dvh;
    display: grid;
    grid-template-rows: auto minmax(0, 1fr);
    border-right: 0;
    border-top: 1px solid var(--border-strong);
    border-radius: 16px 16px 0 0;
    box-shadow: 0 -12px 40px rgba(0,0,0,.55);
    transform: translateY(103%);
    transition: transform .22s ease;
  }
  body.params-open .controls { transform: translateY(0); }

  .sheet-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    position: relative;
    padding: 14px 14px 10px;
    border-bottom: 1px solid var(--border);
  }
  .sheet-head::before {
    content: "";
    position: absolute; top: 6px; left: 50%;
    transform: translateX(-50%);
    width: 38px; height: 4px;
    border-radius: 999px;
    background: var(--border-strong);
  }
  .sheet-head strong { font-size: 13px; font-weight: 650; }
  .control-scroll { padding: 14px 12px calc(24px + env(safe-area-inset-bottom, 0px)); }

  /* Driven by the [hidden] attribute from JS. It must be matched with
     :not([hidden]) - a plain `.mobile-backdrop { display: block }` would
     override the browser's `[hidden] { display: none }` and leave the
     blurred overlay permanently on top of the whole UI. */
  .mobile-backdrop:not([hidden]) {
    display: block;
    position: fixed; inset: 0;
    z-index: 28;
    background: rgba(0,0,0,.5);
    backdrop-filter: blur(2px);
  }

  /* ------------------------------------------------------- bottom bar --- */
  .mobile-bar {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    position: fixed;
    left: 0; right: 0; bottom: 0;
    z-index: 25;
    height: calc(60px + env(safe-area-inset-bottom, 0px));
    padding: 9px 12px calc(9px + env(safe-area-inset-bottom, 0px));
    border-top: 1px solid var(--border);
    background: rgba(11,13,16,.96);
    backdrop-filter: blur(14px);
  }
  .mobile-bar .button { min-height: 42px; }

  /* ---------------------------------------------------- viewer toolbar --- */
  .viewer-panel { grid-template-rows: auto auto minmax(0,1fr) auto; }
  .viewer-toolbar {
    height: auto;
    padding: 8px 10px;
    gap: 8px;
    grid-template-columns: 1fr auto;
    grid-template-areas:
      "switch switch"
      "status actions";
  }
  .view-switch {
    grid-area: switch;
    justify-self: stretch;
    grid-template-columns: repeat(3, 1fr);
  }
  .view-switch button { padding: 0 6px; min-height: 38px; }
  .model-status { grid-area: status; font-size: 11px; }
  .viewer-actions { grid-area: actions; justify-self: end; }
  /* Generate 3D lives in the bottom bar on mobile, so it is not duplicated
     in the toolbar. Dimensions / Fit view shrink to icons. */
  .viewer-toolbar #renderButton { display: none; }
  .viewer-actions .icon-text { font-size: 0; gap: 0; padding: 0 13px; min-height: 40px; }
  .viewer-actions .icon-text svg { width: 18px; height: 18px; }

  .validation-strip { padding: 0 12px; min-height: 34px; }

  .interaction-hint .hint-pointer { display: none; }
  .interaction-hint .hint-touch { display: inline; }
  .interaction-hint { left: 12px; bottom: 10px; font-size: 9px; }
  .progress-track { width: min(240px, 60vw); }

  /* ------------------------------------------------------------ metrics --- */
  .metrics { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .metrics > div { min-height: 46px; padding: 7px 9px; }
  .metrics span { font-size: 9px; }
  .metrics strong { font-size: 12px; }

  /* -------------------------------------------------- touch ergonomics --- */
  .button { min-height: 42px; }
  .icon-button { width: 38px; height: 38px; }
  .unit-input { height: 44px; }
  /* 16px is the threshold below which iOS Safari zooms the page on focus. */
  .unit-input input { font-size: 16px; }
  .segmented button { min-height: 40px; font-size: 13px; }
  .mount-card { min-height: 88px; }
  .switch-row { min-height: 52px; }
  .expert-toggle { min-height: 54px; }
  summary { padding: 14px 0; font-size: 12px; }
  .group { padding: 14px 10px; }
}

/* Narrow phones: paired number fields stop fitting side by side. */
@media (max-width: 430px) {
  .input-grid, .detail-grid { grid-template-columns: 1fr; }
  .metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .mount-card { min-height: 78px; }
  .mount-card svg { width: 26px; height: 26px; }
}
