:root {
  --ink: #1c1c1b;
  --muted: #8b8b87;
  --line: #e5e5e2;
  --track: #d6d6d2;
  --surround: #f5f5f4;
  font: 12px/1.4 -apple-system, system-ui, sans-serif;
  color: var(--ink);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; margin: 0; }
body { display: flex; overflow: hidden; background: #fff; }

/* ---- Panel ---- */

#panel {
  flex: none;
  width: 300px;
  height: 100%;
  overflow-y: auto;
  border-right: 1px solid var(--line);
  background: #fff;
}

.tool-picker {
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
}
.tool-picker select { width: 100%; }

.section {
  padding: 10px 14px 12px;
  border-bottom: 1px solid var(--line);
}
.section h2 {
  margin: 0 0 8px;
  font-size: 11px;
  font-weight: 500;
  color: var(--muted);
}

.row { display: block; margin: 0 0 8px; }
.row:last-child { margin-bottom: 0; }
.row > .label, .row .head .label { display: block; margin-bottom: 3px; }

.row .head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.row .head .label { margin: 0; }

.row.inline {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.row.inline > .label { margin: 0; }
.row.check { display: flex; align-items: center; gap: 6px; }
.row > .head + textarea { margin-top: 3px; }

/* A switch, with its label to the left; on another control's label row it
   sits at the right. */
.switch-field { display: flex; align-items: center; gap: 6px; }
.row .head .switch-field .label { cursor: pointer; }
.switch {
  display: flex;
  align-items: center;
  width: 24px;
  height: 14px;
  padding: 0;
  border: 1.5px solid currentColor;
  border-radius: 7px;
  background: none;
  color: #7f8386;
  cursor: pointer;
  transition: color 0.12s;
}
.switch span {
  flex: none;
  width: 8px;
  height: 8px;
  margin-left: 2px;
  border-radius: 50%;
  background: currentColor;
  transition: transform 0.12s;
}
.switch[aria-checked="true"] { color: var(--ink); }
.switch[aria-checked="true"] span { transform: translateX(9px); }
.switch:focus-visible { outline: none; }
.switch:hover, .switch:focus-visible { color: #55595c; }

/* ---- Inputs ---- */

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

select,
textarea,
input[type="number"] {
  border: 1px solid var(--line);
  border-radius: 2px;
  background: #fff;
}
select { width: 100%; height: 24px; padding: 0 4px; }
textarea {
  display: block;
  width: 100%;
  padding: 4px 6px;
  resize: vertical;
}
input[type="number"] {
  width: 58px;
  height: 20px;
  padding: 0 4px;
  text-align: right;
}
select:focus, textarea:focus, input[type="number"]:focus {
  outline: none;
  border-color: var(--muted);
}

input[type="file"] { width: 100%; font-size: 11px; }

input[type="checkbox"] { margin: 0; accent-color: var(--ink); }

input[type="color"] {
  width: 36px;
  height: 20px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 2px;
  background: none;
}
input[type="color"]::-webkit-color-swatch-wrapper { padding: 2px; }
input[type="color"]::-webkit-color-swatch { border: none; border-radius: 1px; }

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  display: block;
  width: 100%;
  height: 14px;
  margin: 2px 0 0;
  background: none;
}
input[type="range"]:focus { outline: none; }
input[type="range"]::-webkit-slider-runnable-track { height: 1px; background: var(--track); }
input[type="range"]::-moz-range-track { height: 1px; background: var(--track); }
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 10px;
  height: 10px;
  margin-top: -4.5px;
  border: none;
  border-radius: 50%;
  background: var(--ink);
}
input[type="range"]::-moz-range-thumb {
  width: 10px;
  height: 10px;
  border: none;
  border-radius: 50%;
  background: var(--ink);
}

/* Panel buttons (Save preset, Copy SVG). */
.button {
  display: block;
  width: 100%;
  height: 24px;
  padding: 0 6px;
  border: 1px solid var(--line);
  border-radius: 2px;
  background: #fff;
  font: inherit;
  color: inherit;
  cursor: pointer;
}
.button:hover, .button:focus-visible { outline: none; border-color: var(--muted); }

/* The link to the other page, at the foot of the panel. */
.page-link {
  display: block;
  padding: 12px 14px;
  font-size: 11px;
  color: var(--muted);
  text-decoration: none;
}
.page-link:hover, .page-link:focus-visible {
  outline: none;
  color: var(--ink);
  text-decoration: underline;
}

/* ---- Stage ---- */

#stage {
  flex: 1;
  min-width: 0;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  background: var(--surround);
}
#canvas { display: block; }

/* ---- Rug visualiser (index.html) ---- */

.brand {
  display: flex;
  align-items: center;
  gap: 11.5px;
  padding: 16px 14px;
  border-bottom: 1px solid var(--line);
  /* The mark and the name nearly span the panel, with more air on the right. */
  font: 500 22px/1 "Helvetica Neue", Helvetica, Arial, sans-serif;
  white-space: nowrap;
}
.brand svg { flex: none; width: auto; height: 40px; }

#stage.rug-stage {
  position: relative;
  display: block;
  background: #e6ebed;
}
#stage.rug-stage.dark { background: #141414; }

/* The panel is a white card floating 8px clear of the window edge. */
body.rug { background: #e6ebed; }
body.rug.dark { background: #141414; }
.rug #panel {
  height: calc(100% - 16px);
  margin: 8px;
  border: 0;
  border-radius: 16px;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06), 0 4px 16px rgba(0, 0, 0, 0.08);
}

/* Each section is a light grey shape inside the card. --shape is its one tone. */
.rug #panel { --shape: var(--surround); }
.rug #settings {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 8px 8px 0;
}
.rug .section {
  padding: 10px 10px 12px;
  border-bottom: 0;
  border-radius: 10px;
  background: var(--shape);
}
/* A collapsible section's heading is a button across the whole shape, with
   even air above and below the label when collapsed. */
.rug .section.collapsible { padding-top: 0; padding-bottom: 0; }
.rug .section.collapsible h2 { margin: 0 -10px; }
.rug .section .toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 10px 10px 8px;
  border: 0;
  border-radius: 10px;
  background: none;
  font: inherit;
  color: inherit;
  text-align: left;
  cursor: pointer;
}
.rug .section .toggle[aria-expanded="false"] { padding-bottom: 10px; }
.rug .section .toggle:focus-visible { outline: none; }
.rug .section .toggle:hover, .rug .section .toggle:focus-visible { color: var(--ink); }
.rug .section .toggle svg { flex: none; transition: transform 0.12s; }
.rug .section .toggle[aria-expanded="false"] svg { transform: rotate(-90deg); }
.rug .section-body { padding-bottom: 12px; }
@media (prefers-reduced-motion: reduce) {
  .rug .section .toggle svg { transition: none; }
}

#gl {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: default;
}

.swatches { display: flex; flex-wrap: wrap; gap: 6px; }

#tools {
  position: absolute;
  left: 50%;
  bottom: 16px;
  transform: translateX(-50%);
  display: flex;
  gap: 4px;
}
#tools button {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 48px;
  height: 48px;
  padding: 0;
  border: 0;
  background: none;
  color: #7f8386;
  cursor: pointer;
  transition: color 0.12s;
}
#tools button svg { width: 36px; height: 36px; }
#tools button:focus-visible { outline: none; }
#tools button:hover, #tools button:focus-visible { color: #55595c; }
#tools button[aria-pressed="true"] { color: var(--ink); }
.dark #tools button { color: #55554f; }
.dark #tools button:hover, .dark #tools button:focus-visible { color: #8b8b87; }
.dark #tools button[aria-pressed="true"] { color: #e5e5e2; }

/* Light / dark background switch, bottom right, level with the tools. */
#bg-toggle {
  position: absolute;
  right: 20px;
  bottom: 29px;
  display: flex;
  align-items: center;
  width: 40px;
  height: 22px;
  padding: 0;
  border: 1.5px solid currentColor;
  border-radius: 11px;
  background: none;
  color: #7f8386;
  cursor: pointer;
  transition: color 0.12s;
}
#bg-toggle span {
  flex: none;
  width: 14px;
  height: 14px;
  margin-left: 2.5px;
  border-radius: 50%;
  background: currentColor;
  transition: transform 0.12s;
}
#bg-toggle[aria-checked="true"] span { transform: translateX(18px); }
#bg-toggle:focus-visible { outline: none; }
#bg-toggle:hover, #bg-toggle:focus-visible { color: #55595c; }
.dark #bg-toggle { color: #55554f; }
.dark #bg-toggle:hover, .dark #bg-toggle:focus-visible { color: #8b8b87; }

/* The lamp across the top of the stage: drag it to move the key light.
   spotlight.js sets its x (transform) and turns its icon towards the rug. */
#spotlight {
  position: absolute;
  top: 12px;
  left: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 56px;
  height: 56px;
  padding: 0;
  border: 0;
  background: none;
  color: #7f8386;
  cursor: ew-resize;
  touch-action: none;
  transition: color 0.12s;
}
#spotlight svg { width: 44px; height: 44px; }
#spotlight:focus-visible { outline: none; }
#spotlight:hover, #spotlight:focus-visible { color: #55595c; }
#spotlight.dragging { color: var(--ink); }
.dark #spotlight { color: #55554f; }
.dark #spotlight:hover, .dark #spotlight:focus-visible { color: #8b8b87; }
.dark #spotlight.dragging { color: #e5e5e2; }

/* Presets section: the list of presets. */
.rug .item { display: flex; align-items: center; gap: 8px; padding: 4px 0; }
/* White, so the hovered and the dragged row read on the grey shape. */
.rug .item:has(.pick:hover, .pick:focus-visible) { background: #fff; }
.rug .item .pick {
  flex: none;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0;
  border: 0;
  background: none;
  font: inherit;
  color: inherit;
  text-align: left;
  cursor: pointer;
  /* A touch on the image drags the row instead of scrolling the panel. */
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
.rug .item .pick:focus-visible { outline: none; }
.rug .item img {
  flex: none;
  width: 42px;
  height: 30px;
  border: 1px solid var(--line);
  border-radius: 2px;
}
/* The name reads as plain text until hovered or focused. */
.rug .item .name {
  flex: 1;
  min-width: 0;
  height: 20px;
  padding: 0 4px;
  border: 1px solid transparent;
  border-radius: 2px;
  background: none;
  text-overflow: ellipsis;
}
.rug .item input.name:hover { border-color: var(--track); }
.rug .item input.name:focus { outline: none; border-color: var(--muted); }
/* A fixed preset's name: the same text, not editable. */
.rug .item span.name { line-height: 18px; white-space: nowrap; overflow: hidden; }
.rug .item .remove {
  flex: none;
  padding: 0 4px;
  border: 0;
  background: none;
  font: inherit;
  color: var(--muted);
  cursor: pointer;
}
.rug .item .remove:hover { color: var(--ink); }
/* Groups: a heading, then the user's rows and the built-in presets' tiles,
   three to a row (84 px each in the 264 px section). */
.rug .items h3 { margin: 14px 0 6px; font-size: 12px; font-weight: 500; color: var(--ink); }
/* The first group follows the Save preset button. */
.rug .items .group:not(.group ~ .group) > h3 { margin-top: 12px; }
.rug .tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px 6px; }
.rug .tile {
  min-width: 0;
  padding: 0;
  border: 0;
  background: none;
  font: inherit;
  color: inherit;
  text-align: left;
  cursor: pointer;
}
.rug .tile:focus-visible { outline: none; }
.rug .tile img {
  display: block;
  width: 100%;
  aspect-ratio: 7 / 5;
  object-fit: cover;
  border: 1px solid var(--line);
  border-radius: 4px;
}
.rug .tile .caption {
  display: block;
  margin-top: 3px;
  font-size: 11px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.rug .tile:hover img, .rug .tile:focus-visible img { border-color: var(--muted); }
.rug .tile:hover .caption, .rug .tile:focus-visible .caption { color: var(--ink); }
/* Reordering: the dragged row lifts and follows the pointer, the others make
   room for it. */
.rug .items.sorting .item { transition: transform 0.15s; }
.rug .items.sorting .item.dragging {
  position: relative;
  z-index: 1;
  transition: none;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
}
.rug .items.sorting, .rug .items.sorting * { cursor: grabbing; }
@media (prefers-reduced-motion: reduce) {
  .rug .items.sorting .item { transition: none; }
}
