:root {
  --ink:        #14110d;
  --ink-soft:   #2c2620;
  --muted:      #786d5f;
  --muted-soft: #a89e8d;
  --line:       rgba(20, 17, 13, 0.10);
  --line-soft:  rgba(20, 17, 13, 0.06);
  --hairline:   rgba(20, 17, 13, 0.18);

  --canvas:     #f1ece2;
  --paper:      #fbf8f1;
  --ivory:      #f7f2e7;
  --soft:       #ece6d9;

  --gold:       #97774a;
  --gold-soft:  #b89773;
  --gold-deep:  #6f5634;

  --shadow-sm:  0 2px 8px rgba(28, 22, 14, 0.06);
  --shadow-md:  0 12px 36px rgba(28, 22, 14, 0.10);

  --eyebrow-track: 0.18em;

  --serif: "Cormorant Garamond", "Times New Roman", serif;
  --sans:  "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;

  --topbar-h: 64px;
  --sidebar-w: 260px;
  --inspector-w: 300px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100vh;
  font-family: var(--sans);
  color: var(--ink);
  background: var(--canvas);
  overflow: hidden;
}

button { font-family: inherit; }
input, select { font-family: inherit; color: var(--ink); }

.eyebrow {
  font-family: var(--sans);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: var(--eyebrow-track);
  text-transform: uppercase;
  color: var(--muted);
  margin: 0;
}

.muted { color: var(--muted); }

/* ---------- Topbar ---------- */
#studio-topbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 18px;
  background: var(--paper);
  border-bottom: 1px solid var(--line);
  z-index: 30;
}

.back-link {
  font-size: 13px;
  font-weight: 500;
  color: var(--ink-soft);
  text-decoration: none;
  padding: 6px 10px;
  border-radius: 8px;
  transition: background 120ms ease;
}
.back-link:hover { background: var(--soft); }

.title-block { flex: 1; min-width: 0; }
.title-block .eyebrow { font-size: 10px; }

#room-title {
  font-family: var(--serif);
  font-size: 22px;
  font-weight: 600;
  margin: 0;
  outline: none;
  padding: 2px 4px;
  border-radius: 4px;
  cursor: text;
}
#room-title:focus {
  background: var(--ivory);
  box-shadow: 0 0 0 2px var(--gold-soft);
}

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

.btn {
  font-family: var(--sans);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.04em;
  padding: 8px 14px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 120ms ease;
  border: 1px solid transparent;
  background: transparent;
  color: var(--ink);
}
.btn.ghost {
  background: var(--paper);
  border-color: var(--line);
}
.btn.ghost:hover {
  background: var(--ivory);
  border-color: var(--hairline);
}
.btn.primary {
  background: var(--ink);
  color: var(--paper);
}
.btn.primary:hover { background: var(--ink-soft); }
.btn.danger {
  background: transparent;
  color: #a02b2b;
  border-color: rgba(160, 43, 43, 0.3);
}
.btn.danger:hover { background: rgba(160, 43, 43, 0.06); }
.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

/* ---------- View toggle (2D / 3D) ---------- */
.view-toggle {
  display: inline-flex;
  background: var(--ivory);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 2px;
  margin-right: 4px;
}
.view-toggle-btn {
  font-family: var(--sans);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
  padding: 6px 12px;
  border: none;
  background: transparent;
  color: var(--muted);
  border-radius: 6px;
  cursor: pointer;
  transition: all 120ms ease;
}
.view-toggle-btn.active {
  background: var(--ink);
  color: var(--paper);
}
.view-toggle-btn:not(.active):hover {
  color: var(--ink);
  background: var(--paper);
}

/* ---------- Layout shell ---------- */
#studio-shell {
  position: fixed;
  top: var(--topbar-h);
  left: 0;
  right: 0;
  bottom: 0;
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr var(--inspector-w);
}
#studio-shell:has(#studio-inspector[hidden]) {
  grid-template-columns: var(--sidebar-w) 1fr 0;
}

/* ---------- Sidebar ---------- */
#studio-sidebar {
  background: var(--paper);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sidebar-head {
  padding: 14px 16px 10px;
  border-bottom: 1px solid var(--line-soft);
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.sidebar-head .eyebrow { font-size: 10px; }

#sidebar-search {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 10px;
  font-size: 13px;
  background: var(--ivory);
}
#sidebar-search:focus {
  outline: none;
  border-color: var(--gold-soft);
  box-shadow: 0 0 0 2px rgba(151, 119, 74, 0.18);
}

#sidebar-list {
  flex: 1;
  overflow-y: auto;
  padding: 10px 12px 24px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
#sidebar-list::-webkit-scrollbar { width: 6px; }
#sidebar-list::-webkit-scrollbar-thumb {
  background: var(--hairline);
  border-radius: 3px;
}

.sidebar-group {
  padding: 12px 4px 4px;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: var(--eyebrow-track);
  text-transform: uppercase;
  color: var(--muted-soft);
}

.sidebar-empty { padding: 14px 4px; font-size: 13px; color: var(--muted); }

.product-card {
  display: grid;
  grid-template-columns: 56px 1fr;
  gap: 10px;
  padding: 8px;
  background: var(--paper);
  border: 1px solid var(--line-soft);
  border-radius: 10px;
  cursor: grab;
  user-select: none;
  transition: transform 120ms ease, border-color 120ms ease, box-shadow 120ms ease;
}
.product-card:hover {
  border-color: var(--hairline);
  transform: translateY(-1px);
  box-shadow: var(--shadow-sm);
}
.product-card:active { cursor: grabbing; }
.product-card.dragging { opacity: 0.4; }

.product-card .photo {
  width: 56px;
  height: 56px;
  border-radius: 6px;
  background: var(--soft) center/cover no-repeat;
  flex-shrink: 0;
}
.product-card .body {
  min-width: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 2px;
}
.product-card .name {
  font-size: 13px;
  font-weight: 600;
  color: var(--ink);
  line-height: 1.25;
  margin: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.product-card .meta {
  font-size: 11px;
  color: var(--muted);
  display: flex;
  justify-content: space-between;
  gap: 6px;
}
.product-card .price { color: var(--gold-deep); font-weight: 600; }

/* ---------- Canvas area ---------- */
#studio-canvas-area {
  display: flex;
  flex-direction: column;
  background:
    linear-gradient(0deg, var(--canvas), var(--canvas)),
    repeating-linear-gradient(45deg, var(--ivory) 0 2px, transparent 2px 12px);
  position: relative;
  overflow: hidden;
}

#canvas-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 18px;
  font-size: 12px;
  color: var(--muted);
  border-bottom: 1px solid var(--line-soft);
  background: var(--paper);
}
#room-dim-label {
  font-family: var(--serif);
  font-size: 16px;
  font-weight: 600;
  color: var(--ink);
}

#canvas-stage {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  overflow: hidden;
  position: relative;
}

#canvas-svg {
  background: var(--paper);
  border: 1px solid var(--hairline);
  border-radius: 4px;
  box-shadow: var(--shadow-md);
  max-width: 100%;
  max-height: 100%;
  touch-action: none;
}

/* ---------- 3D canvas ---------- */
#canvas-3d {
  position: absolute;
  inset: 24px;
  background: var(--paper);
  border: 1px solid var(--hairline);
  border-radius: 4px;
  box-shadow: var(--shadow-md);
  overflow: hidden;
}
#canvas-3d canvas {
  display: block;
  width: 100% !important;
  height: 100% !important;
  touch-action: none;
}
.canvas-3d-hint {
  position: absolute;
  bottom: 14px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(20, 17, 13, 0.78);
  color: var(--paper);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.06em;
  padding: 6px 12px;
  border-radius: 999px;
  pointer-events: none;
  z-index: 2;
  white-space: nowrap;
}
#studio-canvas-area.view-3d #canvas-svg { display: none; }
#studio-canvas-area.view-3d #canvas-3d { display: block; }

#canvas-svg .grid-line { stroke: rgba(20, 17, 13, 0.06); stroke-width: 1; vector-effect: non-scaling-stroke; }
#canvas-svg .grid-line.major { stroke: rgba(20, 17, 13, 0.13); }
#canvas-svg .ruler-tick { font-family: var(--sans); font-size: 9px; fill: var(--muted); }
#canvas-svg .room-rect {
  fill: none;
  stroke: var(--ink);
  stroke-width: 2;
  vector-effect: non-scaling-stroke;
}
#canvas-svg .north {
  font-family: var(--serif);
  font-size: 11px;
  font-weight: 600;
  fill: var(--muted);
  letter-spacing: 0.18em;
}

#canvas-svg .item {
  cursor: grab;
}
#canvas-svg .item.dragging { cursor: grabbing; }
#canvas-svg .item rect.body {
  fill: transparent;
  stroke: transparent;
  stroke-width: 1.4;
  vector-effect: non-scaling-stroke;
  pointer-events: all;
}
#canvas-svg .item.selected rect.body {
  stroke: var(--ink);
  stroke-width: 2;
  stroke-dasharray: 4 3;
  fill: rgba(20, 17, 13, 0.04);
}
#canvas-svg .item.overlapping rect.body {
  stroke: var(--gold-deep);
  stroke-width: 2;
  stroke-dasharray: 3 2;
  fill: rgba(111, 86, 52, 0.08);
}
#canvas-svg .overlap-badge {
  font-family: var(--sans);
  font-size: 8px;
  font-weight: 700;
  fill: var(--gold-deep);
  pointer-events: none;
  letter-spacing: 0.04em;
}
#canvas-svg .item text.label {
  font-family: var(--sans);
  font-size: 9px;
  font-weight: 600;
  fill: var(--ink);
  pointer-events: none;
  text-anchor: middle;
  dominant-baseline: middle;
}
#canvas-svg .item .handle {
  fill: var(--paper);
  stroke: var(--ink);
  stroke-width: 1.4;
  vector-effect: non-scaling-stroke;
  cursor: nwse-resize;
}
#canvas-svg .item .handle.rotate {
  fill: var(--gold);
  stroke: var(--paper);
  stroke-width: 1.5;
  cursor: grab;
}
#canvas-svg .item .handle.rotate-line {
  stroke: var(--gold);
  stroke-width: 1.4;
  stroke-dasharray: 3 3;
  vector-effect: non-scaling-stroke;
  fill: none;
}

#canvas-svg .wall-distance {
  font-family: var(--sans);
  font-size: 9px;
  fill: var(--gold-deep);
  font-weight: 600;
}
#canvas-svg .wall-distance-line {
  stroke: var(--gold-soft);
  stroke-width: 1;
  stroke-dasharray: 2 2;
  vector-effect: non-scaling-stroke;
}

#canvas-svg .drop-target { fill: rgba(151, 119, 74, 0.08); stroke: var(--gold-soft); stroke-dasharray: 4 4; }

/* ---------- Inspector ---------- */
#studio-inspector {
  background: var(--paper);
  border-left: 1px solid var(--line);
  padding: 18px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
#studio-inspector .eyebrow { font-size: 10px; }
#insp-name {
  font-family: var(--serif);
  font-size: 22px;
  font-weight: 600;
  margin: 0;
}
#insp-dims { font-size: 12px; margin: 0; }

.insp-fields { margin: 8px 0 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.insp-fields > div {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  padding: 6px 0;
  border-bottom: 1px dashed var(--line-soft);
  font-size: 12px;
}
.insp-fields dt { color: var(--muted); margin: 0; }
.insp-fields dd { margin: 0; font-weight: 600; }

.insp-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 10px;
}
.insp-actions .btn { flex: 1 1 auto; min-width: 88px; }

/* ---------- Dialogs ---------- */
dialog {
  border: none;
  border-radius: 12px;
  padding: 22px 24px;
  max-width: 360px;
  background: var(--paper);
  box-shadow: var(--shadow-md);
  font-family: var(--sans);
  color: var(--ink);
}
dialog::backdrop { background: rgba(20, 17, 13, 0.32); backdrop-filter: blur(2px); }
dialog h2 {
  font-family: var(--serif);
  font-size: 24px;
  font-weight: 600;
  margin: 0 0 6px;
}
dialog p { font-size: 13px; margin: 0 0 12px; }
dialog label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
  margin: 12px 0 4px;
}
dialog input[type="number"], dialog input[type="text"] {
  width: 100%;
  padding: 9px 11px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--ivory);
  font-size: 14px;
  margin-top: 4px;
}
dialog menu {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin: 18px 0 0;
  padding: 0;
}

.layout-list {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 320px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.layout-list li {
  padding: 10px 12px;
  border: 1px solid var(--line-soft);
  border-radius: 8px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}
.layout-list .ll-name { font-weight: 600; font-size: 13px; }
.layout-list .ll-meta { font-size: 11px; color: var(--muted); }
.layout-list .ll-actions { display: flex; gap: 6px; }
.layout-list .ll-actions button { font-size: 11px; padding: 5px 9px; }
.layout-list .empty { color: var(--muted); padding: 14px; font-size: 13px; text-align: center; }

/* ---------- Fabric configuration dialog ---------- */
.dlg-fabric { max-width: 560px; width: min(560px, 92vw); }
.fabric-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(72px, 1fr));
  gap: 10px;
  margin-top: 10px;
  max-height: 60vh;
  overflow-y: auto;
  padding: 4px;
}
.fabric-swatch {
  position: relative;
  aspect-ratio: 1 / 1;
  border-radius: 8px;
  border: 2px solid transparent;
  background-position: center;
  background-size: cover;
  cursor: pointer;
  transition: transform 100ms ease, border-color 100ms ease;
  outline: none;
  padding: 0;
}
.fabric-swatch:hover { transform: scale(1.04); border-color: var(--gold-soft); }
.fabric-swatch.selected {
  border-color: var(--ink);
  box-shadow: 0 0 0 3px var(--gold-soft);
}
.fabric-swatch .fab-label {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: linear-gradient(to top, rgba(20, 17, 13, 0.78), rgba(20, 17, 13, 0));
  color: var(--paper);
  font-size: 9px;
  font-weight: 600;
  padding: 12px 4px 4px;
  border-radius: 0 0 6px 6px;
  text-align: center;
  letter-spacing: 0.04em;
  pointer-events: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ---------- Toast ---------- */
#toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--ink);
  color: var(--paper);
  padding: 10px 18px;
  border-radius: 8px;
  font-size: 13px;
  box-shadow: var(--shadow-md);
  display: none;
  z-index: 100;
}

/* ---------- Mobile chrome — hidden by default on desktop ---------- */
#m-add-btn,
#m-action-bar,
#m-catalog-modal,
#m-fabric-modal { display: none; }

/* Compact desktop topbar between 720 and 1024 */
@media (max-width: 1024px) and (min-width: 721px) {
  :root { --sidebar-w: 220px; --inspector-w: 256px; }
  #studio-topbar { gap: 8px; padding: 0 12px; }
  .actions .btn { padding: 7px 10px; font-size: 11px; }
  #room-title { font-size: 18px; }
}

/* ============================================================ */
/*  MOBILE — single-surface UX (≤720px)                          */
/*  ONE surface visible at a time. No stacked panels. No sheets. */
/* ============================================================ */
@media (max-width: 720px) {
  :root { --topbar-h: 44px; }

  html, body { height: 100svh; min-height: 100svh; overflow: hidden; }

  /* ----- Topbar (44px, three slots) ----- */
  #studio-topbar {
    gap: 8px;
    padding: 0 10px;
    padding-top: env(safe-area-inset-top);
    height: calc(var(--topbar-h) + env(safe-area-inset-top));
  }
  .back-link { padding: 4px 6px; font-size: 12px; flex-shrink: 0; }
  .title-block { text-align: center; min-width: 0; flex: 1; }
  .title-block .eyebrow { display: none; }
  #room-title {
    font-size: 16px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    padding: 2px 6px;
  }
  /* Hide all topbar action buttons except the 2D/3D toggle */
  #studio-topbar .actions .btn { display: none; }
  #studio-topbar .actions { gap: 4px; flex-shrink: 0; }
  .view-toggle { padding: 1px; }
  .view-toggle-btn { padding: 5px 9px; font-size: 10px; letter-spacing: 0.06em; }

  /* ----- Shell: canvas-only column ----- */
  #studio-shell {
    top: calc(var(--topbar-h) + env(safe-area-inset-top));
    grid-template-columns: 1fr;
    bottom: 0;
  }

  /* Hide desktop sidebar + inspector entirely on mobile */
  #studio-sidebar { display: none; }
  #studio-inspector { display: none; }

  /* Hide the existing <dialog>-based modals on mobile (replaced by full-screen
     mobile modals below). The Save/Load/Room flows aren't surfaced on mobile
     in this revision — designers compose on desktop, review on phone. */
  dialog#dlg-room, dialog#dlg-save, dialog#dlg-load, dialog#dlg-fabric { display: none; }

  /* Canvas-meta: trim to just the room dim chip */
  #canvas-meta {
    padding: 6px 10px;
    font-size: 10px;
  }
  #canvas-hint { display: none; }

  /* Stage: full bleed, no extra padding for fixed chrome (action bar is thin
     and sits above the canvas; canvas remains visible behind it). */
  #canvas-stage {
    padding: 6px;
    padding-bottom: calc(72px + env(safe-area-inset-bottom));
    transition: padding-bottom 200ms ease-out;
  }
  #canvas-3d {
    inset: 6px;
    bottom: calc(72px + env(safe-area-inset-bottom));
    transition: bottom 200ms ease-out;
  }
  /* When no selection, canvas can use the full bottom region (no action bar). */
  body:not(.m-has-selection) #canvas-stage {
    padding-bottom: calc(96px + env(safe-area-inset-bottom));
  }
  body:not(.m-has-selection) #canvas-3d {
    bottom: calc(96px + env(safe-area-inset-bottom));
  }

  /* ===========================================================
     Floating "+ Add Furniture" button (z-index 60)
     - Default: bottom-center
     - When item selected: bottom-right corner above the action bar
     =========================================================== */
  #m-add-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    position: fixed;
    bottom: calc(20px + env(safe-area-inset-bottom));
    left: 50%;
    transform: translateX(-50%);
    height: 56px;
    padding: 0 22px;
    background: var(--gold);
    color: var(--paper);
    border: none;
    border-radius: 999px;
    font-family: var(--sans);
    font-size: 14px;
    font-weight: 700;
    letter-spacing: 0.04em;
    cursor: pointer;
    box-shadow: 0 8px 24px rgba(20, 17, 13, 0.22);
    z-index: 60;
    white-space: nowrap;
    transition: bottom 200ms ease-out, left 200ms ease-out, transform 200ms ease-out, padding 200ms ease-out, height 200ms ease-out;
  }
  #m-add-btn:active { box-shadow: 0 4px 12px rgba(20, 17, 13, 0.22); }
  body.m-has-selection #m-add-btn {
    bottom: calc(80px + env(safe-area-inset-bottom));
    left: auto;
    right: 12px;
    transform: none;
    height: 44px;
    padding: 0 14px;
    font-size: 12px;
  }
  /* Hide the add button while a modal is open (modal is full-screen) */
  body.m-cat-open #m-add-btn,
  body.m-fab-open #m-add-btn { display: none; }

  /* ===========================================================
     Inline action bar (z-index 50) — STATE 3
     Thin, ≤64px content + safe-area. Not a sheet.
     =========================================================== */
  #m-action-bar {
    display: flex;
    align-items: center;
    position: fixed;
    left: 0; right: 0;
    bottom: 0;
    height: calc(56px + env(safe-area-inset-bottom));
    padding: 0 12px env(safe-area-inset-bottom);
    background: var(--paper);
    border-top: 1px solid var(--line);
    box-shadow: 0 -4px 14px rgba(20, 17, 13, 0.08);
    z-index: 50;
    box-sizing: border-box;
    gap: 8px;
    transform: translateY(110%);
    transition: transform 200ms ease-out;
    pointer-events: none;
  }
  body.m-has-selection #m-action-bar { transform: translateY(0); pointer-events: auto; }
  #m-action-bar[hidden] { display: flex; } /* override [hidden] so transition works */

  .mab-text {
    flex: 1;
    min-width: 0;
    padding-right: 4px;
  }
  .mab-name {
    font-family: var(--serif);
    font-size: 15px;
    font-weight: 600;
    color: var(--ink);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 1.15;
  }
  .mab-dims {
    font-family: var(--sans);
    font-size: 11px;
    color: var(--muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-top: 1px;
  }
  .mab-actions {
    display: flex;
    align-items: center;
    gap: 0;
    flex-shrink: 0;
  }
  .mab-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    padding: 0;
    border: none;
    background: transparent;
    color: var(--ink);
    border-radius: 10px;
    cursor: pointer;
    transition: background-color 100ms ease;
  }
  .mab-btn:active { background: rgba(20, 17, 13, 0.08); }
  .mab-btn.danger { color: #a02b2b; }
  .mab-btn:disabled { opacity: 0.32; cursor: not-allowed; }

  /* ===========================================================
     Selected item highlight (3px gold border, subtle shadow)
     =========================================================== */
  #canvas-svg .item.selected rect.body {
    stroke: var(--gold);
    stroke-width: 3;
    stroke-dasharray: none;
    fill: rgba(151, 119, 74, 0.08);
    filter: drop-shadow(0 2px 6px rgba(151, 119, 74, 0.25));
  }

  /* ===========================================================
     Full-screen modal shell (Catalog + Fabric, z-index 100)
     =========================================================== */
  #m-catalog-modal,
  #m-fabric-modal {
    display: flex;
    flex-direction: column;
    position: fixed;
    inset: 0;
    background: var(--paper);
    z-index: 100;
    transform: translateY(100%);
    transition: transform 240ms cubic-bezier(0.22, 0.6, 0.36, 1);
    pointer-events: none;
    overflow: hidden;
  }
  body.m-cat-open #m-catalog-modal,
  body.m-fab-open #m-fabric-modal {
    transform: translateY(0);
    pointer-events: auto;
  }
  .m-modal-head {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    height: 52px;
    padding: env(safe-area-inset-top) 12px 0;
    border-bottom: 1px solid var(--line);
  }
  .m-modal-head h2 {
    font-family: var(--serif);
    font-size: 19px;
    font-weight: 600;
    margin: 0;
    color: var(--ink);
  }
  .m-modal-close {
    position: absolute;
    top: calc(env(safe-area-inset-top) + 8px);
    right: 8px;
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: none;
    background: transparent;
    color: var(--ink);
    border-radius: 999px;
    cursor: pointer;
  }
  .m-modal-close:active { background: var(--ivory); }

  /* ----- Catalog modal interior ----- */
  .m-cat-search {
    flex-shrink: 0;
    padding: 10px 12px 6px;
  }
  .m-cat-search input {
    width: 100%;
    height: 40px;
    padding: 0 14px;
    border: 1px solid var(--line);
    border-radius: 10px;
    background: var(--ivory);
    font-size: 14px;
    font-family: var(--sans);
    color: var(--ink);
    box-sizing: border-box;
  }
  .m-cat-search input:focus {
    outline: none;
    border-color: var(--gold-soft);
    box-shadow: 0 0 0 3px rgba(184, 151, 115, 0.18);
  }
  .m-cat-chips {
    flex-shrink: 0;
    display: flex;
    gap: 6px;
    padding: 8px 12px 10px;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    border-bottom: 1px solid var(--line);
  }
  .m-cat-chips::-webkit-scrollbar { display: none; }
  .m-cat-chip {
    flex-shrink: 0;
    padding: 8px 14px;
    background: var(--ivory);
    border: 1px solid var(--line);
    border-radius: 999px;
    font-family: var(--sans);
    font-size: 12px;
    font-weight: 600;
    letter-spacing: 0.04em;
    color: var(--ink-soft);
    cursor: pointer;
    white-space: nowrap;
  }
  .m-cat-chip.active {
    background: var(--ink);
    color: var(--paper);
    border-color: var(--ink);
  }

  .m-cat-grid {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-auto-rows: max-content; /* defeats Chromium's broken aspect-ratio
                                    intrinsic sizing in grid rows */
    gap: 12px;
    padding: 12px 12px calc(20px + env(safe-area-inset-bottom));
    align-content: start;
  }
  .m-cat-card {
    display: block;
    text-align: left;
    padding: 0;
    background: var(--paper);
    border: 1px solid var(--line);
    border-radius: 12px;
    overflow: hidden;
    cursor: pointer;
    transition: transform 100ms ease, box-shadow 100ms ease;
    font-family: var(--sans);
    color: var(--ink);
    /* Keep this card from stretching to the grid row's tallest sibling
       which can grow unbounded if a sibling has odd intrinsic sizing. */
    align-self: start;
  }
  .m-cat-card:active {
    transform: scale(0.98);
    box-shadow: 0 2px 6px rgba(20, 17, 13, 0.08);
  }
  .mcc-photo {
    width: 100%;
    aspect-ratio: 1 / 1;
    background: var(--ivory);
    border-bottom: 1px solid var(--line-soft);
    overflow: hidden;
    display: block;
  }
  .mcc-photo-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    object-position: center;
    display: block;
  }
  .mcc-name {
    font-size: 13px;
    font-weight: 600;
    color: var(--ink);
    margin: 8px 10px 2px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 1.2;
  }
  .mcc-meta {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 6px;
    margin: 0 10px 10px;
  }
  .mcc-dims {
    font-size: 10px;
    color: var(--muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
    min-width: 0;
  }
  .mcc-price {
    font-size: 11px;
    font-weight: 600;
    color: var(--ink-soft);
    flex-shrink: 0;
  }
  .m-cat-empty {
    grid-column: 1 / -1;
    text-align: center;
    color: var(--muted);
    padding: 40px 20px;
    font-size: 13px;
  }

  /* ----- Fabric modal interior ----- */
  .m-fab-grid {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    grid-auto-rows: max-content;
    gap: 12px;
    padding: 14px 12px calc(20px + env(safe-area-inset-bottom));
    align-content: start;
  }
  .m-fab-swatch {
    position: relative;
    width: 100%;
    height: 0;
    padding-top: 100%;
    background: var(--ivory) center/cover no-repeat;
    border: 2px solid transparent;
    border-radius: 10px;
    cursor: pointer;
    overflow: hidden;
    font-family: var(--sans);
  }
  .m-fab-swatch.selected {
    border-color: var(--ink);
    box-shadow: 0 0 0 3px var(--gold-soft);
  }
  .mfb-check {
    position: absolute;
    top: 6px;
    right: 6px;
    width: 22px;
    height: 22px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--ink);
    color: var(--paper);
    border-radius: 50%;
    font-size: 13px;
    font-weight: 700;
    line-height: 1;
    z-index: 2;
  }
  .m-fab-label {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 14px 6px 5px;
    font-size: 10px;
    font-weight: 600;
    color: var(--paper);
    background: linear-gradient(to top, rgba(20, 17, 13, 0.8), rgba(20, 17, 13, 0));
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  /* ===========================================================
     Toast — lift above add button / action bar
     =========================================================== */
  #toast {
    bottom: calc(96px + env(safe-area-inset-bottom));
  }
  body.m-has-selection #toast {
    bottom: calc(140px + env(safe-area-inset-bottom));
  }
}
