:root {
  color-scheme: light;
  --bg: #f3f0eb;
  --fg: #1a1a1a;
  --muted: #5c5c5c;
  --accent: #2f5d50;
  --accent-soft: #dfe8e3;
  --line: #d4cfc6;
  --panel: rgba(255, 252, 248, 0.88);
  --font: "IBM Plex Sans", "Segoe UI", sans-serif;
  --mono: "IBM Plex Mono", ui-monospace, Menlo, monospace;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  overflow: hidden; /* app owns its own scroll regions; no outer page scroll */
  font-family: var(--font);
  background:
    radial-gradient(ellipse 70% 45% at 0% 0%, #dde8e2 0%, transparent 55%),
    radial-gradient(ellipse 55% 40% at 100% 100%, #e8e0d6 0%, transparent 50%),
    var(--bg);
  color: var(--fg);
}

a {
  color: var(--accent);
}

code {
  font-family: var(--mono);
  font-size: 0.85em;
  color: var(--accent);
}

.hidden {
  display: none !important;
}

.shell {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.top {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem 2rem;
  align-items: flex-end;
  justify-content: space-between;
  padding: 1.25rem 1.5rem 1rem;
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}

.brand-name {
  font-size: 1.65rem;
  font-weight: 650;
  letter-spacing: -0.03em;
  text-decoration: none;
  color: var(--fg);
}

.tagline {
  margin: 0.2rem 0 0;
  color: var(--muted);
  font-size: 0.95rem;
}

/* Header document bar: project name + open/new + save state. */
.doc-bar {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  flex-wrap: wrap;
}

.project-name {
  font-weight: 650;
  max-width: 22ch;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.save-state {
  font-size: 0.8rem;
  color: var(--muted);
  min-width: 5.5ch;
}

/* Splash / first-run overlay. */
.splash {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  background:
    radial-gradient(ellipse 70% 45% at 0% 0%, #dde8e2 0%, transparent 55%),
    radial-gradient(ellipse 55% 40% at 100% 100%, #e8e0d6 0%, transparent 50%),
    var(--bg);
}

.splash-card {
  max-width: 30rem;
  width: 100%;
  text-align: center;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 1rem;
  padding: 2.5rem 2rem;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.08);
}

.splash-title {
  margin: 0 0 0.5rem;
  font-size: 2rem;
  letter-spacing: -0.03em;
}

.splash-sub {
  margin: 0 0 1.5rem;
  color: var(--muted);
  line-height: 1.5;
}

.splash-actions {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  align-items: stretch;
}

.primary-btn {
  border: 1px solid var(--accent);
  background: var(--accent);
  color: #fff;
  border-radius: 0.5rem;
  padding: 0.7rem 1rem;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

.primary-btn:hover {
  filter: brightness(1.05);
}

.splash-error {
  margin: 1rem 0 0;
  color: #b3202e;
  font-size: 0.85rem;
  min-height: 1em;
}

.dropbox-modal-card {
  max-width: 34rem;
  text-align: left;
}

.dropbox-file-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  max-height: 18rem;
  overflow-y: auto;
}

.dropbox-file-list:empty {
  display: none;
}

.dropbox-file-btn {
  width: 100%;
  text-align: left;
  border: 1px solid var(--line);
  background: #fffdf9;
  border-radius: 0.5rem;
  padding: 0.75rem 0.8rem;
  cursor: pointer;
  font: inherit;
  color: inherit;
}

.dropbox-file-btn:hover {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.dropbox-file-name {
  display: block;
  font-weight: 600;
}

.dropbox-file-meta {
  display: block;
  margin-top: 0.15rem;
  color: var(--muted);
  font-size: 0.8rem;
}

.dropbox-modal-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 1rem;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  margin-bottom: 0.5rem;
}

.field > span {
  font-size: 0.68rem;
  font-weight: 650;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--muted);
}

.card-body input[type="text"],
.card-body input[type="number"] {
  font: inherit;
  font-size: 0.9rem;
  color: var(--fg);
  border: 1px solid var(--line);
  background: #fffdf9;
  border-radius: 0.35rem;
  padding: 0.35rem 0.5rem;
  width: 100%;
}

.card-body input:focus,
.card-body select:focus {
  outline: none;
  border-color: var(--accent);
}

.dims {
  display: flex;
  align-items: flex-end;
  gap: 0.4rem;
}

.dims .field {
  flex: 1;
}

.dims-x {
  padding-bottom: 0.55rem;
  color: var(--muted);
}

.features-edit {
  margin-bottom: 0.5rem;
}

.feature-row {
  display: grid;
  grid-template-columns: 3.4rem 1fr 1fr 1fr;
  gap: 0.35rem;
  align-items: end;
  margin-bottom: 0.4rem;
}

.feature-kind {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--fg);
  padding-bottom: 0.4rem;
}

.feature-row .field {
  margin-bottom: 0;
}

.card-body select {
  font: inherit;
  font-size: 0.85rem;
  color: var(--fg);
  border: 1px solid var(--line);
  background: #fffdf9;
  border-radius: 0.35rem;
  padding: 0.32rem 0.3rem;
  width: 100%;
}

.feature-swing {
  margin: 0 0 0.4rem 3.75rem;
}

.feature-swing .field {
  max-width: 8rem;
  margin-bottom: 0;
}

.feature-hint {
  margin: 0.2rem 0 0;
  font-size: 0.72rem;
  color: var(--muted);
  line-height: 1.4;
}

.shell-error {
  margin: 0 0 0.4rem;
  font-size: 0.78rem;
  color: #a3451a;
  line-height: 1.4;
}

.shell-error:empty {
  display: none;
}

#shell-save-btn {
  width: 100%;
  justify-content: center;
  margin-top: 0.2rem;
}

.notes {
  margin: 0.6rem 0 0;
  color: var(--muted);
  font-size: 0.85rem;
  line-height: 1.5;
  max-width: 60ch;
  flex-shrink: 0;
  max-height: 4.5rem;
  overflow-y: auto;
}

.layout-list button.unrendered .item-title {
  color: var(--muted);
}

.new-layout-btn {
  width: 100%;
  justify-content: center;
  margin-top: 0.6rem;
  border-style: dashed;
}


.workspace {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(14rem, 20rem) 1fr;
  min-height: 0;
}

.sidebar {
  border-right: 1px solid var(--line);
  padding: 1rem 1rem 1rem;
  background: var(--panel);
  backdrop-filter: blur(8px);
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* Head / hint / actions / status stay pinned; only the list scrolls. */
.sidebar-head,
.compare-hint,
.sidebar-actions,
.sidebar > .status,
.new-layout-btn {
  flex-shrink: 0;
}

.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.35rem;
}

.sidebar-head h2 {
  margin: 0;
  font-size: 0.8rem;
  font-weight: 650;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}

.status {
  margin: 0 0 0.75rem;
  font-size: 0.85rem;
  color: var(--muted);
}

.layout-list {
  list-style: none;
  margin: 0;
  padding: 0 0.15rem;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
}

.compare-hint {
  margin: 0 0 0.6rem;
  font-size: 0.8rem;
  color: var(--muted);
  line-height: 1.4;
}

.sidebar-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-bottom: 0.75rem;
}

.layout-row {
  display: grid;
  grid-template-columns: auto auto 1fr auto auto;
  gap: 0.35rem;
  align-items: stretch;
}

.layout-row.dragging-row {
  opacity: 0.5;
}

.compare-check {
  margin: 0.85rem 0 0 0.15rem;
  accent-color: var(--accent);
}

.drag-grip {
  align-self: center;
  color: var(--muted);
  cursor: grab;
  font-size: 0.9rem;
  line-height: 1;
  padding: 0 0.15rem;
  user-select: none;
}

.drag-grip:active {
  cursor: grabbing;
}

.row-clone,
.item-clone,
.row-delete,
.item-delete {
  align-self: center;
  width: auto;
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
  padding: 0 0.3rem;
}

.row-clone:hover,
.item-clone:hover {
  background: transparent;
  color: var(--accent);
}

.row-delete:hover,
.item-delete:hover {
  background: transparent;
  color: var(--danger, #b3202e);
}

.layout-list button {
  width: 100%;
  text-align: left;
  border: 1px solid transparent;
  background: transparent;
  border-radius: 0.4rem;
  padding: 0.7rem 0.75rem;
  cursor: pointer;
  font: inherit;
  color: inherit;
}

.layout-list button:hover {
  background: var(--accent-soft);
}

.layout-list button.active,
.layout-list button.in-compare {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.layout-list .item-title {
  display: block;
  font-weight: 600;
  margin-bottom: 0.15rem;
}

.layout-list .item-meta {
  display: block;
  font-size: 0.8rem;
  color: var(--muted);
}

.viewer {
  padding: 1rem 1.25rem 1.25rem;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.viewer-panel,
.compare-panel {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.empty {
  max-width: 28rem;
  margin: 3rem auto;
  text-align: center;
  color: var(--muted);
}

.empty h2 {
  color: var(--fg);
  margin-bottom: 0.5rem;
}

.viewer-panel.hidden,
.compare-panel.hidden,
.empty.hidden {
  display: none;
}

.viewer-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem 1.25rem;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: 0.75rem;
  flex-shrink: 0;
}

.viewer-meta h2 {
  margin: 0 0 0.25rem;
  font-size: 1.35rem;
  letter-spacing: -0.02em;
}

.title-block {
  flex: 1;
  min-width: 0;
}

.title-input {
  font: inherit;
  font-size: 1.35rem;
  font-weight: 600;
  letter-spacing: -0.02em;
  color: var(--fg);
  width: 100%;
  max-width: 32rem;
  border: 1px solid transparent;
  background: transparent;
  border-radius: 0.35rem;
  padding: 0.2rem 0.4rem;
  margin: 0 0 0.25rem -0.4rem;
}

.title-input:hover {
  border-color: var(--line);
}

.title-input:focus {
  outline: none;
  border-color: var(--accent);
  background: #fffdf9;
}

.viewer-actions {
  display: flex;
  gap: 0.4rem;
  flex-shrink: 0;
  align-items: center;
}

.zoom-controls {
  display: inline-flex;
  align-items: stretch;
  border: 1px solid var(--line);
  background: #fffdf9;
  border-radius: 0.35rem;
  overflow: hidden;
}

.zoom-controls button {
  border: none;
  background: transparent;
  color: var(--fg);
  font: inherit;
  font-size: 0.85rem;
  cursor: pointer;
  padding: 0.4rem 0.6rem;
}

.zoom-controls button:hover {
  background: var(--accent-soft);
  color: var(--accent);
}

.zoom-controls .zoom-reset {
  min-width: 3.4rem;
  text-align: center;
  border-left: 1px solid var(--line);
  border-right: 1px solid var(--line);
  font-variant-numeric: tabular-nums;
}

.svg-frame {
  position: relative;
}

.unrendered-note {
  margin: 0;
  padding: 2.5rem 1rem;
  text-align: center;
  color: var(--muted);
}

.unrendered-note.hidden {
  display: none;
}

/* Single-view body: SVG on the left, feature editor on the right. */
.viewer-body {
  flex: 1;
  min-height: 0;
  display: flex;
  gap: 1rem;
}

.viewer-main {
  flex: 1;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.layout-editor {
  width: 18rem;
  flex-shrink: 0;
  min-height: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

/* Collapsible Shell / Features cards. Each open card scrolls its own body so
   both headers stay visible when content is long. */
.editor-card {
  border: 1px solid var(--line);
  background: #fffdf9;
  border-radius: 0.5rem;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.editor-card.open {
  flex: 0 1 auto; /* size to content, shrink + scroll body when it overflows */
}

.card-head {
  width: 100%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  border: none;
  background: transparent;
  cursor: pointer;
  font: inherit;
  padding: 0.6rem 0.75rem;
}

.card-head:hover {
  background: var(--accent-soft);
}

.card-title {
  font-size: 0.72rem;
  font-weight: 650;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.card-caret {
  color: var(--muted);
  font-size: 0.7rem;
  transition: transform 0.15s ease;
}

.editor-card.open .card-caret {
  transform: rotate(90deg);
}

.card-body {
  display: none;
  padding: 0.25rem 0.75rem 0.75rem;
  border-top: 1px solid var(--line);
}

.editor-card.open .card-body {
  display: block;
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
}

.editor-head {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-bottom: 0.6rem;
}

.item-list {
  list-style: none;
  margin: 0 0 0.6rem;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.item-empty {
  font-size: 0.82rem;
  color: var(--muted);
}

.item-row {
  border: 1px solid var(--line);
  border-radius: 0.4rem;
  overflow: hidden;
}

.item-head {
  display: flex;
  align-items: center;
  gap: 0.2rem;
  padding: 0 0.3rem;
}

.item-row.dragging-row {
  opacity: 0.5;
}

.item-toggle {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  border: none;
  background: transparent;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
  padding: 0.5rem 0.35rem;
  border-radius: 0.35rem;
}

.item-toggle:hover,
.item-row.open .item-toggle {
  background: var(--accent-soft);
}

.item-info {
  min-width: 0;
}

.item-name {
  display: block;
  font-weight: 600;
  font-size: 0.88rem;
}

.item-sub {
  display: block;
  font-size: 0.74rem;
  color: var(--muted);
}

.item-caret {
  flex-shrink: 0;
  color: var(--muted);
  font-size: 0.7rem;
  transition: transform 0.15s ease;
}

.item-row.open .item-caret {
  transform: rotate(90deg);
}

.item-body {
  display: none;
  padding: 0.55rem 0.55rem 0.6rem;
  border-top: 1px solid var(--line);
}

.item-row.open .item-body {
  display: block;
}

.item-row.adding .item-body {
  border-top: none;
}

.item-body .field {
  margin-bottom: 0.5rem;
}

.item-body input,
.item-body select {
  font: inherit;
  font-size: 0.88rem;
  color: var(--fg);
  border: 1px solid var(--line);
  background: #fffdf9;
  border-radius: 0.35rem;
  padding: 0.34rem 0.45rem;
  width: 100%;
}

.item-body input:focus,
.item-body select:focus {
  outline: none;
  border-color: var(--accent);
}

.form-error {
  margin: 0 0 0.45rem;
  font-size: 0.78rem;
  color: #a3451a;
  line-height: 1.4;
}

.form-error:empty {
  display: none;
}

.form-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}

.form-actions .ghost-btn {
  flex: 1 1 4rem;
  justify-content: center;
}

.editor-status {
  margin: 0.5rem 0 0;
  font-size: 0.78rem;
  color: var(--muted);
}

.editor-status:empty {
  display: none;
}

.meta-line {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.45;
}

.ghost-btn {
  display: inline-flex;
  align-items: center;
  border: 1px solid var(--line);
  background: #fffdf9;
  color: var(--fg);
  text-decoration: none;
  border-radius: 0.35rem;
  padding: 0.4rem 0.7rem;
  font: inherit;
  font-size: 0.85rem;
  cursor: pointer;
}

.ghost-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.ghost-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.danger-btn:not(:disabled) {
  border-color: #c45c26;
  color: #8a3a12;
}

.danger-btn:not(:disabled):hover {
  border-color: #a3451a;
  color: #a3451a;
  background: #f8ebe3;
}

.svg-frame {
  background: #fffdf9;
  border: 1px solid var(--line);
  border-radius: 0.35rem;
  padding: 0.75rem;
  overflow: auto;
  flex: 1;
  min-height: 0;
}

.svg-frame img,
.svg-frame svg {
  display: block;
  width: 100%;
  height: auto;
  max-width: 960px;
  margin: 0 auto;
}

.svg-host {
  width: 100%;
}

.svg-frame svg.dragging {
  cursor: grabbing;
}

.svg-frame svg.dragging text {
  user-select: none;
}

.svg-frame svg.measuring,
.svg-frame svg.measuring .item-group,
.svg-frame svg.measuring .wall-light {
  cursor: crosshair;
}

.svg-frame svg .ruler-line {
  stroke: #b3202e;
  stroke-width: 1.5;
  stroke-dasharray: 5 4;
}

.svg-frame svg .ruler-dot {
  fill: #b3202e;
}

.svg-frame svg .ruler-label {
  font: 600 12px "IBM Plex Sans", "Segoe UI", sans-serif;
  fill: #b3202e;
  text-anchor: middle;
  paint-order: stroke;
  stroke: #fffdf9;
  stroke-width: 3px;
  stroke-linejoin: round;
}

/* Existing measure lines are clickable to select (when not measuring). */
.svg-frame svg:not(.measuring) .ruler-hit {
  cursor: pointer;
}

.svg-frame svg .ruler-seg.selected .ruler-label {
  fill: #7a1620;
}

/* Selected endpoints can be dragged to reposition the measure. */
.svg-frame svg .ruler-handle {
  cursor: grab;
}

.svg-frame svg.dragging .ruler-handle {
  cursor: grabbing;
}

#measure-btn.active {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-soft);
}

#measure-undo.hidden,
#measure-clear.hidden {
  display: none;
}

.compare-grid {
  display: grid;
  gap: 0.85rem;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  grid-auto-rows: minmax(0, 1fr);
  flex: 1;
  min-height: 0;
}

.compare-grid[data-count="3"] {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.compare-col {
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.compare-col-head h3 {
  margin: 0 0 0.25rem;
  font-size: 1rem;
  letter-spacing: -0.01em;
}

.compare-col-head {
  margin-bottom: 0.55rem;
  flex-shrink: 0;
}

.compare-frame {
  flex: 1;
  min-height: 0;
}

.compare-frame img,
.compare-frame svg {
  max-width: none;
}

@media (max-width: 800px) {
  /* Stacked layout: fall back to normal document scrolling. */
  body {
    overflow: auto;
  }

  .shell {
    height: auto;
    min-height: 100vh;
  }

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

  .sidebar {
    border-right: none;
    border-bottom: 1px solid var(--line);
    overflow-y: visible;
  }

  /* Stacked: fall back to page scroll, no inner scroll regions. */
  .layout-list {
    flex: none;
    overflow-y: visible;
  }

  .editor-card.open {
    flex: none;
  }

  .editor-card.open .card-body {
    overflow-y: visible;
  }

  .viewer {
    overflow: visible;
  }

  .svg-frame {
    flex: none;
    max-height: 70vh;
  }

  .viewer-body {
    flex-direction: column;
  }

  .layout-editor {
    width: auto;
    overflow-y: visible;
  }

  .compare-grid,
  .compare-grid[data-count="3"] {
    grid-template-columns: 1fr;
    grid-auto-rows: auto;
  }

  .compare-frame {
    flex: none;
    max-height: 70vh;
  }
}
