:root {
  /* Canvas background */
  --bg-canvas: #1a1a2e;
  --bg-surface: rgba(30, 30, 50, 0.85);
  --bg-panel: rgba(25, 25, 45, 0.92);
  --bg-hover: rgba(255, 255, 255, 0.08);

  /* Glassmorphism */
  --glass-blur: 20px;
  --glass-border: rgba(255, 255, 255, 0.08);

  /* Accent */
  --accent-primary: #6c63ff;
  --accent-secondary: #a78bfa;
  --accent-gradient: linear-gradient(135deg, #6c63ff, #a78bfa);

  /* Text */
  --text-primary: #e8e8f0;
  --text-secondary: #9ca3af;
  --text-muted: #6b7280;

  /* Borders & Shadows */
  --border-subtle: rgba(255, 255, 255, 0.06);
  --shadow-panel: 0 8px 32px rgba(0, 0, 0, 0.4);
  --shadow-paper: 0 4px 24px rgba(0, 0, 0, 0.5);

  /* Micro-animations */
  --transition-fast: 150ms ease;
  --transition-smooth-ms: 300ms;
  --transition-smooth: var(--transition-smooth-ms) cubic-bezier(0.4, 0, 0.2, 1);

  /* Drawer */
  --backdrop-bg: rgba(10, 10, 20, 0.45);
  --panel-max-height-mobile: 80vh;

  /* Docs-style shell */
  --toolbar-h: 48px;
  --sidebar-w: 240px;
  --sidebar-rail-w: 88px;
  --inspector-w: 320px;
  --stage-bg: transparent;
  --thumb-paper: #ffffff;
  --thumb-card: #f6f2e9;
  --thumb-ink: #3a3a4c;
  --thumb-edge: rgba(24, 24, 40, 0.18);

  /* Inspector field system */
  --field-h: 28px;
  --field-radius: 6px;
  --field-bg: rgba(255, 255, 255, 0.05);
  --field-border: rgba(255, 255, 255, 0.1);
  --field-border-hover: rgba(255, 255, 255, 0.18);
  --focus-ring: 0 0 0 2px rgba(167, 139, 250, 0.55);
  --label-w: 34%;
  --bg-panel-solid: #19192d;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: 'Inter', sans-serif;
  background-color: var(--bg-canvas);
  color: var(--text-primary);
  overflow: hidden;
  margin: 0;
  padding: 0;
  width: 100vw;
  height: 100vh;
}

#app {
  position: fixed;
  inset: 0;
  overflow: hidden;
}

/* ============================================
   Docs-Style Shell Overlay
   ============================================ */
.shell {
  position: fixed;
  inset: 0;
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0,1fr) var(--inspector-w);
  grid-template-rows: var(--toolbar-h) minmax(0,1fr);
  grid-template-areas: "toolbar toolbar toolbar" "sidebar stage inspector";
  pointer-events: none;
  z-index: 10;
}

/* Explicit chrome stacking order. `.toolbar` carries backdrop-filter, so it is its own
   stacking context: the toolbar cell has to sit above the sidebar and inspector for an
   open popover to paint over them, independent of DOM order. */
.shell__toolbar {
  grid-area: toolbar;
  pointer-events: auto;
  position: relative;
  z-index: 20;
}

.shell__sidebar {
  grid-area: sidebar;
  pointer-events: auto;
  position: relative;
  z-index: 10;
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 12px;
  overflow-x: hidden;
  overflow-y: auto;
  background: var(--bg-panel);
  border-right: 1px solid var(--border-subtle);
  scrollbar-width: thin;
  scrollbar-color: var(--text-muted) transparent;
}

.shell__sidebar::-webkit-scrollbar {
  width: 8px;
}

.shell__sidebar::-webkit-scrollbar-track {
  background: transparent;
}

.shell__sidebar::-webkit-scrollbar-thumb {
  background: var(--text-muted);
  border-radius: 4px;
}

.shell__stage {
  grid-area: stage;
  pointer-events: none;
  position: relative;
  z-index: 0;
}

.shell__inspector {
  grid-area: inspector;
  pointer-events: auto;
}

.shell--sidebar-collapsed {
  grid-template-columns: 0px minmax(0,1fr) var(--inspector-w);
}

.shell--inspector-closed {
  grid-template-columns: var(--sidebar-w) minmax(0,1fr) 0px;
}

/* Style sidebar (docs-style shell) */
.style-sidebar__group {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  align-content: start;
}

.style-sidebar__group-heading {
  grid-column: 1 / -1;
  padding: 0 2px;
  font-size: 0.68rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.style-thumb {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
  padding: 6px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: var(--thumb-card);
  color: var(--thumb-ink);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.18);
  font: inherit;
  cursor: pointer;
  transition:
    background var(--transition-fast),
    border-color var(--transition-fast),
    box-shadow var(--transition-fast),
    color var(--transition-fast),
    transform var(--transition-fast);
}

.style-thumb:hover {
  background: var(--thumb-paper);
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.3);
  opacity: 1;
  transform: translateY(-1px);
}

.style-thumb:focus-visible {
  outline: 2px solid var(--accent-secondary);
  outline-offset: 2px;
}

/* The 1px border is always reserved, so selecting a style never shifts the grid. */
.style-thumb[aria-selected="true"] {
  border-color: var(--accent-primary);
  background: var(--thumb-paper);
  color: var(--thumb-ink);
  box-shadow:
    0 0 0 2px color-mix(in srgb, var(--accent-primary) 55%, transparent),
    0 4px 14px rgba(0, 0, 0, 0.35);
}

.style-thumb__preview {
  display: block;
  overflow: hidden;
  border: 1px solid var(--thumb-edge);
  border-radius: 3px;
  background: var(--thumb-paper);
  line-height: 0;
}

.style-thumb__preview img {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 140 / 198;
}

.style-thumb__label {
  min-height: 2.5em;
  font-size: 0.72rem;
  line-height: 1.25;
  text-align: center;
  overflow-wrap: anywhere;
}

@media (prefers-reduced-motion: reduce) {
  .style-thumb {
    transition: none;
  }
}

/* Narrow desktop: the sidebar is an 88px rail of thumbnails only. The label stays in the
   accessibility tree and appears as a hover/focus tooltip; the shell column follows
   --sidebar-rail-w through --sidebar-w, so collapsed/open inspector combinations inherit it.
   The tier runs below 1280px so the 1000px acceptance width also lands on the rail. */
@media (max-width: 1279px) {
  :root {
    --sidebar-w: var(--sidebar-rail-w);
  }

  .shell__sidebar {
    gap: 10px;
    padding: 8px;
  }

  .style-sidebar__group {
    grid-template-columns: minmax(0, 1fr);
    gap: 8px;
  }

  .style-sidebar__group-heading {
    font-size: 0.6rem;
    letter-spacing: 0.06em;
    text-align: center;
  }

  .style-thumb {
    padding: 4px;
    gap: 0;
  }

  .style-thumb__label {
    position: absolute;
    left: 50%;
    bottom: 4px;
    z-index: 1;
    max-width: calc(100% - 8px);
    min-height: 0;
    padding: 2px 4px;
    border-radius: 4px;
    background: var(--bg-panel);
    color: var(--text-primary);
    font-size: 0.62rem;
    line-height: 1.2;
    white-space: normal;
    opacity: 0;
    pointer-events: none;
    transform: translateX(-50%);
    transition: opacity var(--transition-fast);
  }

  .style-thumb:hover .style-thumb__label,
  .style-thumb:focus-visible .style-thumb__label {
    opacity: 1;
  }
}

/* Phone width: a horizontal strip under the toolbar, 72px cards, labels visually hidden
   but still accessible. Only this strip scrolls; no scrollbar is hidden globally. The grid
   override lives at the end of the file, after the <=1023px drawer columns. */
@media (max-width: 767px) {
  .shell--sidebar-collapsed .shell__sidebar {
    visibility: hidden;
  }

  .shell__sidebar {
    flex-direction: row;
    align-items: center;
    gap: 8px;
    /* The full-canvas fit leaves 92px for all chrome. A 36px toolbar plus this 56px
       strip leaves the paper's top and bottom corners on the canvas. */
    height: 56px;
    max-height: 56px;
    padding: 4px 8px;
    overflow-x: auto;
    overflow-y: hidden;
    border-right: none;
    border-bottom: none;
  }

  .shell__sidebar::-webkit-scrollbar {
    width: auto;
    height: 8px;
  }

  .style-sidebar__group {
    display: flex;
    flex: 0 0 auto;
    flex-direction: row;
    align-items: center;
    gap: 8px;
  }

  .style-sidebar__group-heading {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
    border: 0;
  }

  .style-thumb {
    flex: 0 0 72px;
    width: 72px;
    height: 48px;
    padding: 2px;
    gap: 0;
  }

  .style-thumb__preview {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 42px;
  }

  .style-thumb__preview img {
    width: auto;
    height: 100%;
    max-width: 100%;
    aspect-ratio: 140 / 198;
  }

  .style-thumb__label {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
    border: 0;
  }
}

/* Export button pulse */
.export-btn:hover {
  animation: pulse 1.5s infinite;
}

@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(108, 99, 255, 0.4); }
  70% { box-shadow: 0 0 0 10px rgba(108, 99, 255, 0); }
  100% { box-shadow: 0 0 0 0 rgba(108, 99, 255, 0); }
}

/* Paper shadow on zoom */
.canvas-container canvas {
  transition: box-shadow var(--transition-smooth);
}

/* Tooltip fade */
.tooltip-popup {
  transition: opacity var(--transition-fast);
}

/* Button hover transitions */
button {
  transition: opacity var(--transition-fast), transform var(--transition-fast);
}
button:hover:not(:disabled) {
  opacity: 0.85;
}
button:active:not(:disabled) {
  transform: scale(0.98);
}

/* ============================================
   R7: Extracted Component Styles
   ============================================ */

/* Glass Panel */
.glass-panel {
  background: var(--bg-panel);
  backdrop-filter: blur(var(--glass-blur));
  -webkit-backdrop-filter: blur(var(--glass-blur));
  border: 1px solid var(--glass-border);
  box-shadow: var(--shadow-panel);
}

/* Canvas */
.canvas-container {
  position: fixed;
  inset: 0;
  overflow: hidden;
  background-color: var(--bg-canvas);
  z-index: 0;
}

.canvas-container__canvas {
  display: block;
  width: 100%;
  height: 100%;
  cursor: grab;
  touch-action: none;
}

/* Sections */
.section {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.section__subtitle {
  margin: 10px 0 2px;
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.5px;
  text-transform: uppercase;
  color: var(--text-muted);
}

.section__subtitle:first-child {
  margin-top: 0;
}

.section__divider {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--border-subtle);
}

/* Color picker (toolbar) */
.color-picker__input {
  width: 32px;
  height: 32px;
  padding: 0;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

/* ============================================
   Inspector field system
   ============================================ */

/* One label/control row: a two-column grid shared by every inspector control. */
.field {
  display: grid;
  grid-template-columns: minmax(0, var(--label-w)) minmax(0, 1fr);
  align-items: center;
  column-gap: 10px;
  min-height: var(--field-h);
  border-radius: var(--field-radius);
  transition:
    background-color 400ms ease,
    box-shadow 400ms ease;
}

.field--stacked {
  grid-template-columns: minmax(0, 1fr);
  row-gap: 4px;
}

.field__label {
  min-width: 0;
  font-size: 0.78rem;
  line-height: 1.25;
  color: var(--text-secondary);
  overflow-wrap: anywhere;
}

.field__control {
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: flex-end;
}

.field--highlight,
.margin-box__field.field--highlight {
  background: rgba(108, 99, 255, 0.18);
  box-shadow: 0 0 0 4px rgba(108, 99, 255, 0.18);
}

/* Number-slider: range plus editable number field. */
.num-slider {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  min-width: 0;
}

.num-slider--field-only {
  width: auto;
}

.num-slider__range {
  appearance: none;
  flex: 1 1 auto;
  min-width: 48px;
  height: 16px;
  background: transparent;
  margin: 0;
  cursor: pointer;
}

.num-slider__range::-webkit-slider-runnable-track {
  height: 4px;
  border-radius: 2px;
  background: linear-gradient(
    to right,
    var(--accent-primary) var(--fill, 0%),
    rgba(255, 255, 255, 0.14) var(--fill, 0%)
  );
}

.num-slider__range::-moz-range-track {
  height: 4px;
  border-radius: 2px;
  background: linear-gradient(
    to right,
    var(--accent-primary) var(--fill, 0%),
    rgba(255, 255, 255, 0.14) var(--fill, 0%)
  );
}

.num-slider__range::-webkit-slider-thumb {
  appearance: none;
  margin-top: -5px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--text-primary);
  border: 2px solid var(--accent-primary);
  outline: none;
}

.num-slider__range::-moz-range-thumb {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--text-primary);
  border: 2px solid var(--accent-primary);
  outline: none;
}

.num-slider__range:focus-visible {
  outline: none;
}

.num-slider__range:focus-visible::-webkit-slider-thumb {
  box-shadow: var(--focus-ring);
}

.num-slider__range:focus-visible::-moz-range-thumb {
  box-shadow: var(--focus-ring);
}

.num-field {
  display: inline-flex;
  align-items: center;
  flex: 0 0 72px;
  width: 72px;
  height: var(--field-h);
  padding: 0 8px;
  background: var(--field-bg);
  border: 1px solid var(--field-border);
  border-radius: var(--field-radius);
}

.num-field__input {
  flex: 1 1 auto;
  min-width: 0;
  width: 100%;
  background: transparent;
  border: none;
  outline: none;
  color: var(--text-primary);
  font: inherit;
  font-size: 0.8rem;
  font-variant-numeric: tabular-nums;
  text-align: right;
  padding: 0;
}

.num-field__unit {
  flex: 0 0 auto;
  margin-left: 4px;
  font-size: 0.72rem;
  color: var(--text-muted);
}

/* Shared field hover/focus treatment for boxed text controls. */
.num-field:hover,
.select__input:hover,
.text-field:hover,
.swatch__hex:hover {
  border-color: var(--field-border-hover);
}

.num-field:focus-within,
.select__input:focus,
.text-field:focus,
.swatch__hex:focus {
  outline: none;
  border-color: var(--accent-secondary);
  box-shadow: var(--focus-ring);
}

/* Segmented control. */
.segmented {
  display: flex;
  width: 100%;
  padding: 2px;
  gap: 2px;
  background: var(--field-bg);
  border: 1px solid var(--field-border);
  border-radius: var(--field-radius);
}

.segmented__option {
  flex: 1 1 0;
  min-width: 0;
  height: 22px;
  padding: 0 4px;
  border: none;
  border-radius: 4px;
  background: transparent;
  color: var(--text-secondary);
  font: inherit;
  font-size: 0.72rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
}

.segmented__option:hover:not(:disabled) {
  opacity: 1;
  color: var(--text-primary);
}

.segmented__option[aria-checked="true"] {
  background: var(--accent-primary);
  color: #fff;
}

.segmented__option:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}

/* Switch. */
.switch {
  position: relative;
  width: 32px;
  height: 18px;
  flex: 0 0 auto;
  padding: 0;
  border: none;
  border-radius: 9px;
  background: rgba(255, 255, 255, 0.18);
  cursor: pointer;
  transition: background var(--transition-fast);
}

.switch[aria-checked="true"] {
  background: var(--accent-primary);
}

.switch__thumb {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #fff;
  transition: transform var(--transition-fast);
}

.switch[aria-checked="true"] .switch__thumb {
  transform: translateX(14px);
}

.switch:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}

/* Styled select. */
.select {
  position: relative;
  display: flex;
  width: 100%;
  min-width: 0;
}

.select__input {
  appearance: none;
  width: 100%;
  min-width: 0;
  height: var(--field-h);
  padding: 0 26px 0 8px;
  background: var(--field-bg);
  border: 1px solid var(--field-border);
  border-radius: var(--field-radius);
  color: var(--text-primary);
  font: inherit;
  font-size: 0.8rem;
  cursor: pointer;
  text-overflow: ellipsis;
}

.select__input option {
  background: #1f1f35;
  color: var(--text-primary);
}

.select__chevron {
  position: absolute;
  right: 8px;
  top: 50%;
  width: 12px;
  height: 12px;
  transform: translateY(-50%);
  color: var(--text-secondary);
  pointer-events: none;
}

/* Single-line text field. */
.text-field {
  appearance: none;
  width: 100%;
  min-width: 0;
  height: var(--field-h);
  padding: 0 8px;
  background: var(--field-bg);
  border: 1px solid var(--field-border);
  border-radius: var(--field-radius);
  color: var(--text-primary);
  font: inherit;
  font-size: 0.8rem;
  cursor: text;
}

/* Colour swatch plus inline palette. */
.swatch {
  display: flex;
  align-items: center;
  gap: 6px;
  width: 100%;
  justify-content: flex-end;
}

.swatch__button {
  flex: 0 0 auto;
  width: 28px;
  height: 28px;
  padding: 0;
  border-radius: var(--field-radius);
  border: 1px solid var(--field-border-hover);
  background: var(--chip);
  cursor: pointer;
}

.swatch__button:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}

.swatch__hex {
  width: 80px;
  flex: 0 1 80px;
  min-width: 0;
  height: var(--field-h);
  padding: 0 8px;
  background: var(--field-bg);
  border: 1px solid var(--field-border);
  border-radius: var(--field-radius);
  color: var(--text-primary);
  font: inherit;
  font-size: 0.78rem;
  font-variant-numeric: tabular-nums;
  text-transform: uppercase;
}

.swatch-palette {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 6px;
  padding: 6px 0 4px;
}

.swatch-palette[hidden] {
  display: none;
}

.swatch-palette__chip {
  aspect-ratio: 1;
  width: 100%;
  padding: 0;
  border-radius: 4px;
  border: 1px solid rgba(255, 255, 255, 0.18);
  background: var(--chip);
  cursor: pointer;
}

.swatch-palette__chip[aria-pressed="true"] {
  box-shadow: 0 0 0 2px var(--accent-secondary);
}

/* "More" disclosure. */
.more {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.more__toggle {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  align-self: flex-start;
  height: 24px;
  padding: 0 4px;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font: inherit;
  font-size: 0.75rem;
  cursor: pointer;
  border-radius: 4px;
}

.more__toggle:hover:not(:disabled) {
  opacity: 1;
  color: var(--text-primary);
}

.more__toggle:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}

.more__chevron {
  width: 12px;
  height: 12px;
  transition: transform var(--transition-fast);
}

.more__toggle[aria-expanded="true"] .more__chevron {
  transform: rotate(90deg);
}

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

.more__body[hidden] {
  display: none;
}

/* Margins: mini page diagram with the four side fields around it. */
.margin-box {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(64px, 96px) minmax(0, 1fr);
  grid-template-areas:
    ". top ."
    "left page right"
    ". bottom .";
  align-items: center;
  justify-items: center;
  gap: 6px;
  padding: 4px 0 8px;
}

.margin-box__field {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  border-radius: var(--field-radius);
  padding: 2px;
}

.margin-box__field--top {
  grid-area: top;
}

.margin-box__field--bottom {
  grid-area: bottom;
}

.margin-box__field--left {
  grid-area: left;
  justify-self: end;
}

.margin-box__field--right {
  grid-area: right;
  justify-self: start;
}

.margin-box__label {
  font-size: 0.7rem;
  color: var(--text-muted);
}

.margin-box__page-wrap {
  grid-area: page;
  position: relative;
  width: 100%;
  display: flex;
  justify-content: center;
}

.margin-box__page {
  position: relative;
  width: 100%;
  max-height: 120px;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 2px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.4);
}

.margin-box__content {
  position: absolute;
  border: 1px dashed var(--accent-primary);
  background: rgba(108, 99, 255, 0.1);
}

.margin-box__link {
  /* Centred with inset + auto margins, not a transform: the global button:active
     scale() would replace a translate and make the button jump away mid-click. */
  position: absolute;
  inset: 0;
  margin: auto;
  width: 26px;
  height: 26px;
  padding: 4px;
  border-radius: 50%;
  border: 1px solid var(--field-border-hover);
  background: #2a2a44;
  color: var(--text-secondary);
  cursor: pointer;
}

.margin-box__link svg {
  width: 100%;
  height: 100%;
}

.margin-box__link[aria-pressed="true"] {
  background: var(--accent-primary);
  color: #fff;
  border-color: transparent;
}

.margin-box__link:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}

/* Buttons */
.btn-secondary {
  height: 28px;
  padding: 0 12px;
  border-radius: var(--field-radius);
  background: var(--field-bg);
  border: 1px solid var(--field-border);
  color: var(--text-primary);
  font: inherit;
  font-size: 0.78rem;
  cursor: pointer;
  transition:
    background var(--transition-fast),
    border-color var(--transition-fast);
}

.btn-secondary:hover:not(:disabled) {
  opacity: 1;
  border-color: var(--field-border-hover);
  background: var(--bg-hover);
}

/* Export Button */
.export-btn {
  width: 100%;
  height: 32px;
  margin-top: 6px;
  padding: 0 12px;
  border-radius: 8px;
  background: var(--accent-secondary);
  color: #fff;
  border: none;
  cursor: pointer;
  font: inherit;
  font-weight: 600;
  font-size: 0.9rem;
  transition: all var(--transition-fast);
}

/* ============================================
   Toolbar primitives (docs-style shell)
   ============================================ */
.toolbar {
  display: flex;
  align-items: center;
  gap: 4px;
  width: 100%;
  height: 100%;
  padding: 0 8px;
  background: var(--bg-surface);
  backdrop-filter: blur(var(--glass-blur));
  -webkit-backdrop-filter: blur(var(--glass-blur));
  border-bottom: 1px solid var(--glass-border);
  box-shadow: var(--shadow-panel);
  color-scheme: dark;
}

.toolbar__group {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
  height: 100%;
  padding: 0 8px;
  border-right: 1px solid var(--border-subtle);
}

.toolbar__group:last-child {
  border-right: none;
}

.toolbar__group-label {
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.5px;
  text-transform: uppercase;
  color: var(--text-muted);
  white-space: nowrap;
}

.toolbar__btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-width: 30px;
  height: 30px;
  padding: 0 8px;
  border: 1px solid transparent;
  border-radius: 6px;
  background: transparent;
  color: var(--text-primary);
  font-family: inherit;
  font-size: 0.8rem;
  line-height: 1;
  white-space: nowrap;
  cursor: pointer;
}

.toolbar__btn:hover:not(:disabled) {
  background: var(--bg-hover);
  border-color: var(--border-subtle);
  opacity: 1;
}

.toolbar__btn[aria-pressed="true"] {
  background: var(--accent-gradient);
  border-color: transparent;
  color: white;
}

.toolbar__btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.toolbar__btn[aria-haspopup]::after {
  content: "";
  width: 0;
  height: 0;
  border-left: 4px solid transparent;
  border-right: 4px solid transparent;
  border-top: 4px solid currentColor;
}

.toolbar__select {
  height: 30px;
  padding: 0 6px;
  border: 1px solid var(--border-subtle);
  border-radius: 6px;
  background: transparent;
  color: var(--text-primary);
  font-family: inherit;
  font-size: 0.8rem;
  cursor: pointer;
}

.toolbar__select:hover {
  border-color: var(--text-muted);
}

.toolbar__btn:focus-visible,
.toolbar__select:focus-visible {
  outline: 2px solid var(--accent-secondary);
  outline-offset: 2px;
}

/* Number inputs keep a narrow, engine-independent width (WebKit defaults to 96px,
   Chrome to 62px, and both crowd the Style and Margins groups). */
.toolbar__select[type="number"] {
  width: 56px;
  padding: 0 4px;
  text-align: center;
}

.toolbar-popover {
  position: relative;
  /* Keep the open panel above the rest of the toolbar row's content. */
  z-index: 1;
  display: inline-flex;
  align-items: center;
}

.toolbar-popover__panel {
  display: none;
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  min-width: 180px;
  padding: 8px;
  border: 1px solid var(--glass-border);
  border-radius: 8px;
  background: var(--bg-panel);
  backdrop-filter: blur(var(--glass-blur));
  -webkit-backdrop-filter: blur(var(--glass-blur));
  box-shadow: var(--shadow-panel);
  z-index: 20;
}

.toolbar-popover--open {
  display: block;
}

/* Toolbar presets menu (PresetsMenu): built-in list, saved list, save form, reset. */
.presets-menu {
  display: flex;
  flex-direction: column;
  gap: 4px;
  width: min(280px, calc(100vw - 24px));
  max-height: min(70vh, 520px);
  overflow-y: auto;
}

.presets-menu__heading {
  padding: 6px 4px 2px;
  color: var(--text-secondary);
  font-size: 0.6875rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.presets-menu__list {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.presets-menu__item {
  justify-content: flex-start;
  width: 100%;
  overflow: hidden;
  text-align: left;
  text-overflow: ellipsis;
}

.presets-menu__saved-row {
  display: flex;
  align-items: center;
  gap: 4px;
}

.presets-menu__saved-row .presets-menu__item {
  flex: 1 1 auto;
  min-width: 0;
}

.presets-menu__delete,
.presets-menu__restore {
  flex: none;
  color: var(--text-secondary);
  font-size: 0.75rem;
}

.presets-menu__restore {
  align-self: flex-start;
}

.presets-menu__empty {
  margin: 0;
  padding: 4px;
  color: var(--text-muted);
  font-size: 0.75rem;
}

.presets-menu__save {
  display: flex;
  gap: 4px;
  margin-top: 4px;
  padding-top: 6px;
  border-top: 1px solid var(--glass-border);
}

.presets-menu__name {
  flex: 1 1 auto;
  min-width: 0;
}

.presets-menu__reset {
  justify-content: flex-start;
  margin-top: 4px;
  border-top: 1px solid var(--glass-border);
  border-radius: 0;
  padding-top: 6px;
}

/* Width tiers. The Gallery toggle hides at 1150px and below (the sidebar cannot collapse
   there); the View (zoom) buttons hide on phone widths, where pinch-zoom takes over. */
@media (max-width: 1150px) {
  .toolbar__control--sidebar-collapse {
    display: none;
  }
}

@media (max-width: 767px) {
  .toolbar__group--view {
    display: none;
  }
}

/* Narrow tablet/phone: condense spacing and number fields while keeping every remaining
   control, including Settings (inspector) and Export, on screen. The group captions go
   first; each group keeps its aria-label. */
@media (max-width: 950px) {
  .toolbar {
    gap: 2px;
    padding: 0 4px;
  }

  .toolbar__group {
    gap: 4px;
    padding: 0 4px;
  }

  .toolbar__group-label {
    display: none;
  }

  .toolbar__btn {
    min-width: 28px;
    padding: 0 6px;
    font-size: 0.75rem;
  }

  .toolbar__select {
    padding: 0 4px;
    font-size: 0.75rem;
  }

  .toolbar__select[type="number"] {
    width: 48px;
  }

  .color-picker__input {
    width: 28px;
    height: 28px;
  }
}

/* ============================================
   Settings inspector (docs-style shell)
   ============================================ */
.shell__inspector {
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
  background: var(--bg-panel);
  border-left: 1px solid var(--glass-border);
  /* Above the stage, below the toolbar so its popovers stay on top. */
  z-index: 12;
}

/* Closed on desktop: the grid column is 0px, so hide the mounted panel with it. */
@media (min-width: 1024px) {
  .shell--inspector-closed .shell__inspector {
    visibility: hidden;
  }
}

.inspector__header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px 10px 16px;
  border-bottom: 1px solid var(--border-subtle);
  flex-shrink: 0;
}

.inspector__title {
  flex: 1;
  font-size: 0.8rem;
  font-weight: 600;
  letter-spacing: 0.6px;
  text-transform: uppercase;
  color: var(--text-secondary);
}

.inspector__close {
  display: none;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border: 1px solid transparent;
  border-radius: 6px;
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
}

.inspector__close:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.inspector__close svg {
  width: 16px;
  height: 16px;
}

.inspector__body {
  flex: 1;
  overflow-y: auto;
  padding: 8px 0 24px;
}

.inspector__group {
  border-bottom: 1px solid var(--border-subtle);
}

.inspector__group:last-child {
  border-bottom: none;
}

.inspector__group-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  width: 100%;
  min-height: 40px;
  padding: 0 16px;
  border: none;
  background: transparent;
  color: var(--text-primary);
  font-family: inherit;
  font-size: 0.8rem;
  font-weight: 600;
  letter-spacing: 0.6px;
  text-transform: uppercase;
  text-align: left;
  cursor: pointer;
  transition: background var(--transition-fast);
}

.inspector__group-header:hover {
  background: var(--bg-hover);
}

.inspector__group-header:focus-visible,
.inspector__close:focus-visible {
  outline: 2px solid var(--accent-secondary);
  outline-offset: -2px;
}

.inspector__chevron {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  color: var(--text-secondary);
  transition: transform var(--transition-fast);
}

.inspector__group-header[aria-expanded="false"] .inspector__chevron {
  transform: rotate(-90deg);
}

.inspector__group-body {
  padding: 0 16px 8px;
}

.inspector__group-body--collapsed {
  display: none;
}

.inspector__group-body > div > .section {
  margin-bottom: 8px;
}

/* Below 1024px the inspector is a modal drawer over the stage. */
.drawer-scrim {
  display: none;
  position: fixed;
  inset: var(--toolbar-h) 0 0 0;
  background: var(--backdrop-bg);
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--transition-smooth);
  z-index: 11;
}

@media (max-width: 1023px) {
  /* The drawer floats over the stage, so the grid reserves no inspector column. */
  .shell,
  .shell--inspector-closed {
    grid-template-columns: var(--sidebar-w) minmax(0,1fr) 0px;
  }

  .shell--sidebar-collapsed,
  .shell--sidebar-collapsed.shell--inspector-closed {
    grid-template-columns: 0px minmax(0,1fr) 0px;
  }

  .drawer-scrim {
    display: block;
  }

  .drawer-scrim--visible {
    opacity: 1;
    pointer-events: auto;
  }

  .shell__inspector {
    position: fixed;
    /* Opaque: a translucent drawer showed the page through it as a hard-edged block. */
    background: var(--bg-panel-solid);
    top: var(--toolbar-h);
    right: 0;
    bottom: 0;
    width: var(--inspector-w);
    max-width: 100vw;
    visibility: visible;
    transform: translateX(100%);
    transition:
      transform var(--transition-smooth),
      visibility 0s;
  }

  /* A closed drawer leaves the tab order and the accessibility tree, but only after its
     300ms slide-out completes; opening re-shows it in the same frame as the slide-in. */
  .shell--inspector-closed .shell__inspector {
    visibility: hidden;
    transition:
      transform var(--transition-smooth),
      visibility 0s linear var(--transition-smooth-ms);
  }

  /* Descendants with `transition: all` would otherwise animate their inherited visibility
     for a further 150ms after the drawer finishes hiding, leaving them tabbable. */
  .shell--inspector-closed .shell__inspector * {
    transition: none;
  }

  .shell:not(.shell--inspector-closed) .shell__inspector {
    transform: translateX(0);
  }

  .inspector__close {
    display: inline-flex;
  }
}

/* Below 768px the drawer keeps the slide-up presentation. */
@media (max-width: 767px) {
  .shell__inspector {
    top: auto;
    left: 0;
    width: auto;
    max-height: var(--panel-max-height-mobile);
    border-left: none;
    border-top: 1px solid var(--glass-border);
    border-radius: 16px 16px 0 0;
    transform: translateY(100%);
  }

  .shell:not(.shell--inspector-closed) .shell__inspector {
    transform: translateY(0);
  }

  /* The strip row must land after the <=1023px drawer columns so it wins the cascade. */
  :root {
    /* A compact row leaves vertical room for the strip without covering the paper. */
    --toolbar-h: 36px;
  }

  .shell,
  .shell--inspector-closed,
  .shell--sidebar-collapsed,
  .shell--sidebar-collapsed.shell--inspector-closed {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: var(--toolbar-h) auto minmax(0, 1fr);
    grid-template-areas:
      "toolbar"
      "sidebar"
      "stage";
  }

  .shell--sidebar-collapsed,
  .shell--sidebar-collapsed.shell--inspector-closed {
    grid-template-rows: var(--toolbar-h) 0 minmax(0, 1fr);
  }
}

@media (prefers-reduced-motion: reduce) {
  .shell__inspector,
  .drawer-scrim,
  .inspector__chevron,
  .field,
  .switch,
  .switch__thumb,
  .more__chevron {
    transition: none;
  }
}
