:root {
  color-scheme: dark;
  --bg: #111318;
  --panel: #181b22;
  --panel-2: #20242d;
  --stroke: #2d3340;
  --stroke-strong: #3d4658;
  --text: #eef3f7;
  --muted: #97a3b3;
  --accent: #36d399;
  --accent-2: #55a8ff;
  --warn: #ffce5c;
  --bad: #ff6b7a;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font: 13px/1.4 Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button, input, select {
  font: inherit;
  color: inherit;
}

button {
  height: 30px;
  border: 1px solid var(--stroke);
  border-radius: 6px;
  background: #232832;
  cursor: pointer;
}
button:hover { border-color: var(--stroke-strong); background: #2a303c; }
button.active { border-color: #2fcf97; background: #17372f; color: #bfffe8; }

.app-shell {
  position: relative;
  display: grid;
  grid-template-rows: 42px 48px 1fr 28px;
  height: 100%;
}

.topbar, .footerbar {
  display: flex;
  align-items: center;
  gap: 10px;
  border-color: var(--stroke);
  background: #14171d;
}
.topbar {
  justify-content: space-between;
  padding: 0 10px;
  border-bottom: 1px solid var(--stroke);
}
.footerbar {
  padding: 0 12px;
  border-top: 1px solid var(--stroke);
  color: var(--muted);
}
.footerbar span + span { margin-left: 18px; }

.brand, .top-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}
.logo-mark {
  width: 18px;
  height: 18px;
  border-radius: 5px;
  background:
    radial-gradient(circle at 5px 5px, #111318 0 2px, transparent 2px),
    radial-gradient(circle at 13px 13px, #111318 0 2px, transparent 2px),
    linear-gradient(135deg, var(--accent), var(--accent-2));
  flex: 0 0 auto;
}
#projectName {
  width: min(34vw, 280px);
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--text);
  font-weight: 650;
}
.status-pill {
  border: 1px solid var(--stroke);
  border-radius: 999px;
  padding: 2px 8px;
  color: var(--muted);
  white-space: nowrap;
}
.status-pill.subtle {
  color: #b9c7d8;
  background: rgba(255,255,255,.035);
}
.icon-btn, .avatar {
  width: 30px;
  padding: 0;
}
.avatar {
  border-radius: 50%;
  background: #26374c;
  overflow: hidden;
}
.avatar img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.zoom-label {
  min-width: 46px;
  text-align: right;
  color: var(--muted);
}

.tool-strip {
  display: flex;
  align-items: center;
  gap: 6px;
  overflow-x: auto;
  overflow-y: hidden;
  padding: 6px 10px;
  border-bottom: 1px solid var(--stroke);
  background: #171a21;
  scrollbar-width: thin;
}
.tool-strip::-webkit-scrollbar { height: 8px; }
.tool-strip::-webkit-scrollbar-thumb { background: #303847; border-radius: 999px; }
.tool {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  flex: 0 0 auto;
  min-width: 76px;
  height: 34px;
  padding: 0 10px;
  font-weight: 650;
}
.tool-icon {
  display: inline-grid;
  place-items: center;
  width: 18px;
  height: 18px;
  font-weight: 800;
}
.tool.danger { color: #ffc1c8; }

.workspace {
  display: grid;
  grid-template-columns: 230px minmax(360px, 1fr) 290px;
  min-height: 0;
}
.layers-panel, .options-panel, .three-panel {
  border-right: 1px solid var(--stroke);
  background: var(--panel);
  min-height: 0;
}

.layers-panel, .options-panel {
  overflow: auto;
  padding: 12px;
}
.panel-title {
  margin-bottom: 10px;
  color: #dce6ef;
  font-weight: 700;
  letter-spacing: 0;
}
.panel-title.secondary {
  margin-top: 18px;
}
.layer-group {
  margin-bottom: 10px;
}
.group-head, .layer-row {
  display: grid;
  grid-template-columns: 18px 18px 1fr 18px;
  align-items: center;
  gap: 6px;
  min-height: 28px;
}
.group-head {
  color: #c8d2df;
  font-weight: 650;
}
.layer-row {
  border-radius: 6px;
  padding: 0 6px;
  color: var(--muted);
  cursor: pointer;
}
.layer-row.active {
  background: linear-gradient(90deg, #20362f, #1d2734);
  box-shadow: inset 3px 0 0 var(--accent);
  color: #eafff7;
}
.eye { opacity: .8; }
.swatch {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: 1px solid rgba(255,255,255,.28);
}
.project-structure {
  display: grid;
  gap: 6px;
  color: var(--muted);
}
.structure-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 26px;
  padding: 0 6px;
  border-radius: 6px;
  background: rgba(255,255,255,.025);
}
.structure-row strong {
  color: #dce6ef;
  font-weight: 650;
}

.canvas-area {
  position: relative;
  min-width: 0;
  min-height: 0;
  background: #0f1116;
}
.component-popup {
  position: absolute;
  left: 16px;
  top: 16px;
  z-index: 8;
  width: min(640px, calc(100% - 32px));
  max-height: min(660px, calc(100% - 32px));
  padding: 14px;
  border: 1px solid var(--stroke-strong);
  border-radius: 8px;
  background: rgba(22,27,36,.94);
  box-shadow: 0 20px 70px rgba(0,0,0,.38);
  backdrop-filter: blur(16px);
  overflow: hidden;
}
.popup-head {
  display: flex;
  align-items: start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}
.popup-head div {
  display: grid;
  gap: 2px;
}
.popup-head span {
  color: var(--muted);
  font-size: 12px;
}
.component-popup input {
  width: 100%;
  height: 34px;
  border: 1px solid var(--stroke);
  border-radius: 7px;
  background: #10151d;
  padding: 0 10px;
}
.component-quick-results {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 8px;
  margin-top: 10px;
  max-height: 430px;
  overflow: auto;
}
.component-category-tabs {
  display: flex;
  gap: 6px;
  margin-top: 10px;
  overflow-x: auto;
  padding-bottom: 2px;
}
.component-category-tabs button {
  flex: 0 0 auto;
  height: 28px;
  padding: 0 10px;
  border-radius: 999px;
}
.quick-part {
  display: grid;
  grid-template-columns: 76px 1fr;
  align-items: center;
  gap: 2px;
  height: auto;
  min-height: 92px;
  padding: 9px;
  text-align: left;
}
.part-preview {
  display: grid;
  width: 66px;
  height: 56px;
  place-items: center;
  border: 1px solid var(--stroke);
  border-radius: 7px;
  background: #10151d;
  overflow: hidden;
}
.part-preview svg, .part-preview img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.part-preview .preview-board {
  fill: rgba(19,124,89,.42);
  stroke: rgba(191,255,232,.16);
}
.part-preview rect:not(.preview-board) {
  fill: #f2c14e;
}
.part-copy {
  display: grid;
  gap: 3px;
  min-width: 0;
}
.quick-part small, .quick-part em {
  color: var(--muted);
  font-style: normal;
}
.quick-part small, .quick-part em, .quick-part strong {
  overflow: hidden;
  text-overflow: ellipsis;
}
.quick-part em {
  font-size: 11px;
  white-space: nowrap;
}
.component-loading {
  display: grid;
  min-height: 64px;
  place-items: center;
  border: 1px dashed var(--stroke);
  border-radius: 7px;
  color: var(--muted);
}
.minimap {
  position: absolute;
  right: 14px;
  bottom: 14px;
  z-index: 6;
  width: 180px;
  height: 126px;
  border: 1px solid var(--stroke-strong);
  border-radius: 8px;
  background: rgba(15,18,24,.86);
  box-shadow: 0 12px 42px rgba(0,0,0,.32);
}
.minimap-toggle {
  position: absolute;
  right: 14px;
  bottom: 148px;
  z-index: 7;
  height: 28px;
  padding: 0 9px;
}
.minimap.is-hidden { display: none; }
.minimap.is-hidden + * { display: none; }
#pcbCanvas {
  display: block;
  width: 100%;
  height: 100%;
}
.floating-hint {
  position: absolute;
  left: 14px;
  bottom: 14px;
  max-width: calc(100% - 28px);
  padding: 6px 9px;
  border: 1px solid rgba(255,255,255,.1);
  border-radius: 6px;
  background: rgba(17,19,24,.82);
  color: var(--muted);
  pointer-events: none;
}

.options-panel {
  border-left: 1px solid var(--stroke);
}
.field {
  display: grid;
  gap: 6px;
  margin-bottom: 12px;
}
.field label {
  color: var(--muted);
  font-size: 12px;
}
.field input, .field select {
  width: 100%;
  height: 32px;
  border: 1px solid var(--stroke);
  border-radius: 6px;
  background: #11151c;
  padding: 0 9px;
}
.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}
.segmented {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  margin-bottom: 14px;
}
.muted-note {
  margin: 0 0 12px;
  color: var(--muted);
}
.component-results {
  display: grid;
  gap: 7px;
  margin-bottom: 14px;
}
.header-preset {
  display: grid;
  gap: 10px;
  margin-bottom: 14px;
  padding: 10px;
  border: 1px solid var(--stroke);
  border-radius: 7px;
  background: #10151d;
}
.header-preset strong, .header-preset span {
  display: block;
}
.header-preset span {
  color: var(--muted);
  font-size: 12px;
}
.header-controls {
  display: grid;
  grid-template-columns: 1fr 72px;
  gap: 8px;
}
.header-controls input {
  width: 100%;
  height: 30px;
  border: 1px solid var(--stroke);
  border-radius: 6px;
  background: #11151c;
  padding: 0 9px;
}
.part-result {
  display: grid;
  height: auto;
  min-height: 48px;
  justify-items: start;
  align-content: center;
  gap: 2px;
  padding: 7px 9px;
  text-align: left;
}
.part-result span {
  font-weight: 650;
}
.part-result small {
  max-width: 100%;
  overflow: hidden;
  color: var(--muted);
  text-overflow: ellipsis;
  white-space: nowrap;
}
.wide-action {
  width: 100%;
  margin-bottom: 10px;
}

.three-panel {
  border: 1px solid var(--stroke-strong);
  border-radius: 8px;
  background: #0d1015;
  box-shadow: 0 18px 60px rgba(0,0,0,.38);
}
.view-layer {
  position: absolute;
  right: 14px;
  bottom: 42px;
  z-index: 10;
  display: grid;
  grid-template-rows: 34px minmax(0, 1fr);
  width: min(430px, calc(100vw - 28px));
  height: min(330px, calc(100vh - 150px));
  overflow: hidden;
}
.view-layer.is-hidden {
  display: none;
}
.view-layer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 0 8px 0 11px;
  border-bottom: 1px solid var(--stroke);
  background: #151922;
  color: #dce6ef;
  font-weight: 700;
}
#threeViewport {
  width: 100%;
  height: 100%;
}

.popover {
  position: fixed;
  top: 38px;
  right: 94px;
  z-index: 20;
  display: grid;
  gap: 4px;
  min-width: 178px;
  padding: 6px;
  border: 1px solid var(--stroke);
  border-radius: 8px;
  background: #181c24;
  box-shadow: 0 14px 50px rgba(0,0,0,.32);
}
.popover button {
  justify-content: flex-start;
  text-align: left;
  background: transparent;
  border-color: transparent;
}
.recovery-prompt {
  position: fixed;
  left: 50%;
  bottom: 42px;
  z-index: 80;
  display: flex;
  align-items: center;
  gap: 10px;
  max-width: calc(100vw - 28px);
  padding: 10px 12px;
  border: 1px solid var(--stroke-strong);
  border-radius: 10px;
  background: #171c25;
  box-shadow: 0 18px 60px rgba(0,0,0,.34);
  transform: translateX(-50%);
}
.recovery-prompt span { color: var(--muted); }
.toast-stack {
  position: fixed;
  right: 16px;
  top: 58px;
  z-index: 90;
  display: grid;
  gap: 8px;
  width: min(360px, calc(100vw - 32px));
}
.toast {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px;
  border: 1px solid var(--stroke);
  border-radius: 9px;
  background: #181d27;
  box-shadow: 0 14px 45px rgba(0,0,0,.3);
  opacity: 0;
  transform: translateY(-6px);
  transition: opacity .18s ease, transform .18s ease;
}
.toast.is-visible {
  opacity: 1;
  transform: translateY(0);
}
.toast-success { border-color: rgba(54,211,153,.45); }
.toast-error { border-color: rgba(255,107,122,.55); }
.toast button {
  width: 24px;
  height: 24px;
  padding: 0;
}
.project-error {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(8,10,14,.72);
  backdrop-filter: blur(8px);
}
.project-error > div {
  width: min(440px, 100%);
  padding: 24px;
  border: 1px solid var(--stroke-strong);
  border-radius: 12px;
  background: #171c25;
  box-shadow: 0 24px 80px rgba(0,0,0,.42);
}
.project-error h2 {
  margin: 0 0 8px;
}
.project-error p {
  color: var(--muted);
}
.project-error a, .project-error button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 34px;
  margin-right: 8px;
  padding: 0 12px;
  border: 1px solid var(--stroke);
  border-radius: 7px;
  background: #232832;
  color: var(--text);
  text-decoration: none;
}
.account-popover {
  right: 12px;
  min-width: 220px;
}
.context-popover {
  right: auto;
  min-width: 190px;
}
.account-summary {
  padding: 8px 9px 10px;
  border-bottom: 1px solid var(--stroke);
  color: var(--muted);
}
.account-summary strong {
  display: block;
  color: var(--text);
}
.account-summary img {
  width: 28px;
  height: 28px;
  margin-right: 8px;
  border-radius: 50%;
  vertical-align: middle;
}

@media (max-width: 1100px) {
  .workspace {
    grid-template-columns: 200px minmax(320px, 1fr) 250px;
  }
  .tool {
    min-width: 68px;
  }
}

@media (max-width: 760px) {
  .workspace {
    grid-template-columns: minmax(0, 1fr);
  }
  .layers-panel, .options-panel { display: none; }
  .top-actions button:not(#saveBtn):not(#view3dBtn), .zoom-label { display: none; }
  #projectName { width: 150px; }
  .tool {
    min-width: 54px;
    padding: 0 8px;
  }
  .tool span:last-child {
    display: none;
  }
}
