:root {
  /* —— Brand & ink —— */
  --ink: #26242b;
  --muted: #686572;
  --line: #d6dae2;
  --panel: #ffffff;
  --page-base: #eef2f4;
  --page-glow-a: rgba(87, 184, 167, 0.14);
  --page-glow-b: rgba(231, 100, 95, 0.11);
  --table: #dfe7e8;
  --table-deep: #c4d4d7;
  --mint: #57b8a7;
  --coral: #e7645f;
  --amber: #d99b3d;
  --blue: #4d77b8;
  --violet: #7b6fb1;
  --brand: var(--mint);
  --brand-ink: #1f6153;
  --brand-edge: #3f988b;

  /* —— Surfaces (converged neutrals) —— */
  --surface: #ffffff;
  --surface-2: #f6f8fc;
  --surface-3: #edf2f5;
  --paper: var(--surface-2);

  /* —— Radius scale —— */
  --r-xs: 8px;
  --r-sm: 12px;
  --r-md: 16px;
  --r-lg: 20px;
  --r-pill: 999px;

  /* —— Spacing scale —— */
  --sp-1: 4px;
  --sp-2: 8px;
  --sp-3: 12px;
  --sp-4: 16px;
  --sp-5: 24px;
  --sp-6: 32px;

  /* —— Elevation scale (soft, clay-like) —— */
  --sh-1: 0 2px 8px rgba(49, 54, 68, 0.06);
  --sh-2: 0 10px 24px rgba(49, 54, 68, 0.09);
  --sh-3: 0 18px 46px rgba(49, 54, 68, 0.14);
  --sh-pop: 0 22px 56px rgba(38, 36, 43, 0.22);
  --sh-inset: inset 0 1px 0 rgba(255, 255, 255, 0.7);
  --shadow: var(--sh-3);

  /* —— Type scale —— */
  --fs-xs: 11px;
  --fs-sm: 13px;
  --fs-md: 15px;
  --fs-lg: 18px;
  --fs-xl: 22px;

  font-family: "Nunito", "Varela Round", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
  margin: 0;
}

body {
  color: var(--ink);
  background:
    linear-gradient(135deg, var(--page-glow-a), transparent 38%),
    linear-gradient(315deg, var(--page-glow-b), transparent 34%),
    var(--page-base);
  overscroll-behavior-y: contain;
  transition: background 0.24s ease;
  -webkit-font-smoothing: antialiased;
  text-rendering: geometricPrecision;
}

button,
input,
select {
  font: inherit;
}

button {
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  color: var(--ink);
  background: #fff;
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  transition: transform 0.1s ease, border-color 0.14s ease, background 0.14s ease, box-shadow 0.14s ease;
}

button:hover {
  transform: translateY(-0.5px);
  border-color: #aeb6c4;
  box-shadow: var(--sh-1);
}

button:active {
  transform: translateY(0.5px) scale(0.995);
  box-shadow: none;
}

button:focus-visible {
  outline: 2px solid color-mix(in srgb, var(--brand) 72%, transparent);
  outline-offset: 2px;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.48;
  transform: none;
  box-shadow: none;
}

.app-shell {
  width: min(1640px, 100%);
  min-height: 100vh;
  margin: 0 auto;
  padding: 18px;
}

.topbar {
  display: grid;
  grid-template-columns: minmax(240px, 1fr) minmax(420px, 2fr) minmax(320px, auto);
  align-items: center;
  gap: 18px;
  min-height: 70px;
  padding: 10px 16px;
  border: 1px solid rgba(139, 148, 166, 0.42);
  border-radius: var(--r-lg);
  background: rgba(255, 255, 255, 0.85);
  box-shadow: var(--sh-2);
  backdrop-filter: blur(16px);
}

.topbar-lead {
  display: flex;
  align-items: center;
  gap: 14px;
  min-width: 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
  flex: 0 0 auto;
}

.brand-text {
  min-width: 0;
}

.brand h1 {
  margin: 0;
  font-family: "Varela Round", "Nunito", "PingFang SC", sans-serif;
  font-size: clamp(20px, 2.4vw, 28px);
  line-height: 1;
  letter-spacing: 0.2px;
  color: var(--brand-ink);
}

.brand p {
  max-width: 320px;
  margin: 5px 0 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.35;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#statusLine {
  display: none;
}

.current-pattern {
  display: none;
  align-items: center;
  gap: 10px;
  min-height: 56px;
  padding: 6px 10px 6px 6px;
  border: 1px solid rgba(87, 184, 167, 0.42);
  border-radius: var(--r-md);
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.95), rgba(245, 251, 249, 0.92));
  text-align: left;
  cursor: default;
  transition: border-color 0.14s ease;
}

.current-pattern[data-visible="true"] {
  display: inline-flex;
}

.current-pattern:hover {
  border-color: var(--mint);
}

.current-pattern-thumb {
  width: 44px;
  height: 44px;
  border: 1px solid #dce1e9;
  border-radius: var(--r-xs);
  background: var(--surface-2);
  flex: 0 0 auto;
}

.current-pattern-text {
  display: grid;
  gap: 2px;
  min-width: 0;
}

.current-pattern-text strong {
  font-size: 13px;
  font-weight: 800;
  line-height: 1.1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.current-pattern-text span {
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.current-pattern-swap {
  padding: 3px 8px;
  border-radius: var(--r-pill);
  background: rgba(87, 184, 167, 0.16);
  color: #2f7d70;
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.2px;
}

.brand-mark {
  display: grid;
  grid-template-columns: repeat(2, 16px);
  grid-auto-rows: 16px;
  gap: 4px;
  flex: 0 0 auto;
  padding: 7px;
  border: 1px solid rgba(87, 184, 167, 0.34);
  border-radius: var(--r-md);
  background: linear-gradient(180deg, #ffffff, var(--surface-2));
  box-shadow: var(--sh-1), var(--sh-inset);
}

.brand-mark span {
  border-radius: 50%;
  box-shadow:
    inset 0 -2px 3px rgba(0, 0, 0, 0.14),
    inset 0 2px 3px rgba(255, 255, 255, 0.7),
    inset 0 0 0 3px rgba(255, 255, 255, 0.4);
}

.brand-mark span:nth-child(1) {
  background: var(--coral);
}

.brand-mark span:nth-child(2) {
  background: var(--mint);
}

.brand-mark span:nth-child(3) {
  background: var(--amber);
}

.brand-mark span:nth-child(4) {
  background: var(--blue);
}

.top-stats {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 0 0 auto;
  justify-content: flex-end;
  flex-wrap: wrap;
}

.workflow-progress {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0;
  min-width: 0;
  padding: 6px 10px;
  border: 1px solid rgba(139, 148, 166, 0.32);
  border-radius: var(--r-pill);
  background: rgba(248, 249, 251, 0.7);
  box-shadow: var(--sh-1);
  scroll-padding-inline: 12px;
}

.workflow-step {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 44px;
  padding: 4px 10px;
  border-radius: var(--r-pill);
  color: var(--muted);
  font-size: 12px;
  font-weight: 750;
  white-space: nowrap;
  transition: color 0.18s ease, background 0.18s ease;
}

.workflow-step .step-dot {
  display: grid;
  place-items: center;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #cbd1dc;
  color: #fff;
  font-size: 10px;
  font-weight: 800;
}

.workflow-step.done {
  color: #3f7d72;
}

.workflow-step.done .step-dot {
  background: var(--mint);
}

.workflow-step.active {
  color: var(--ink);
  background: rgba(87, 184, 167, 0.16);
}

.workflow-step.active .step-dot {
  background: var(--mint);
  box-shadow: 0 0 0 3px rgba(87, 184, 167, 0.25);
}

.workflow-sep {
  flex: 0 0 14px;
  width: 14px;
  height: 1px;
  background: #cdd3de;
}

.icon-button {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-height: 44px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  background: #fff;
  font-size: 13px;
  font-weight: 750;
  color: var(--ink);
}

.icon-button-glyph {
  display: grid;
  place-items: center;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: rgba(87, 184, 167, 0.18);
  position: relative;
}

.icon-button-glyph::before {
  content: "";
  width: 8px;
  height: 8px;
  border-radius: 2px;
  background: var(--mint);
}

.icon-button[data-icon="status"] .icon-button-glyph::before {
  border-radius: 50%;
  background: var(--blue);
}

.icon-button-label {
  font-weight: 750;
}

.icon-button-count {
  min-width: 22px;
  padding: 0 6px;
  border-radius: var(--r-pill);
  background: rgba(77, 119, 184, 0.12);
  color: var(--blue);
  font-size: 12px;
  font-weight: 800;
  text-align: center;
  line-height: 18px;
}

.icon-button-dot {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--coral);
  box-shadow: 0 0 0 2px #fff;
}

.icon-button-dot[hidden] {
  display: none;
}

.icon-button.alert {
  border-color: rgba(231, 100, 95, 0.5);
  background: rgba(231, 100, 95, 0.08);
}

.bg-picker {
  display: flex;
  align-items: center;
  gap: 7px;
  min-height: 44px;
  padding: 0 9px 0 11px;
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  background: rgba(255, 255, 255, 0.9);
}

.bg-picker span {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.bg-picker select {
  min-height: 28px;
  border: 0;
  color: var(--ink);
  background: transparent;
  font: inherit;
  font-size: 13px;
  font-weight: 750;
  outline: none;
}

.tool-style-picker {
  display: flex;
  align-items: center;
  gap: 7px;
  min-height: 44px;
  padding: 0 9px 0 11px;
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  background: rgba(255, 255, 255, 0.9);
}

.tool-style-picker span {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.tool-style-picker select {
  min-height: 28px;
  border: 0;
  color: var(--ink);
  background: transparent;
  font: inherit;
  font-size: 13px;
  font-weight: 750;
  outline: none;
}

.ghost-button {
  padding: 0 14px;
}

.ghost-button.active {
  border-color: #3f988b;
  background: rgba(87, 184, 167, 0.16);
  box-shadow: inset 0 0 0 2px rgba(87, 184, 167, 0.16);
}

.sandbox-sticker {
  min-height: 44px;
  width: 44px;
  min-width: 44px;
  padding: 0;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  color: #4a5160;
  background: #fff;
  font-size: 16px;
  font-weight: 600;
}

.sandbox-sticker.active {
  border-color: #3f988b;
  color: #1f6153;
  background: rgba(87, 184, 167, 0.14);
  box-shadow: inset 0 0 0 1px rgba(87, 184, 167, 0.32);
}

.studio-grid {
  display: grid;
  grid-template-columns: 300px minmax(520px, 1fr) 320px;
  gap: 16px;
  margin-top: 16px;
  align-items: stretch;
}

.studio-grid[data-phase="choose"] [data-stage="working"] {
  display: none;
}

.studio-grid:not([data-phase="choose"]) [data-stage="choose"] {
  display: none;
}

.studio-grid:not([data-phase="choose"]) .right-panel {
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

/* Hide right-panel content in iron/cool/finish without collapsing the grid track,
   so the workbench (and the board inside it) keep a stable width across phase changes. */
.studio-grid:not([data-phase="place"]):not([data-phase="inspect"]) .right-panel {
  visibility: hidden;
  pointer-events: none;
}

.studio-grid[data-phase="choose"] .right-panel {
  display: none;
}

.studio-grid[data-phase="choose"] {
  grid-template-columns: minmax(280px, 360px) 1fr;
}

.side-panel {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.side-panel section {
  padding: 13px;
  border: 1px solid rgba(139, 148, 166, 0.42);
  border-radius: var(--r-md);
  background: rgba(255, 255, 255, 0.87);
  box-shadow: var(--sh-2);
}

.right-panel section {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.section-title {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
  font-size: 15px;
  font-weight: 750;
}

.section-title small {
  min-width: 0;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  text-align: right;
  overflow-wrap: anywhere;
}

.side-reference {
  display: grid;
  gap: 8px;
  margin-bottom: 10px;
}

.side-reference-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  font-size: 13px;
  font-weight: 760;
  color: #3a404f;
}

.side-reference-head small {
  color: var(--muted);
  font-size: 11px;
  font-weight: 650;
}

#sideReferenceCanvas {
  width: 100%;
  height: auto;
  aspect-ratio: 1 / 1;
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  background: var(--surface-2);
}

.side-reference-legend {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

.side-reference-chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  min-height: 20px;
  padding: 2px 6px;
  border: 1px solid #d9dfea;
  border-radius: var(--r-xs);
  background: rgba(255, 255, 255, 0.92);
  color: #5a6070;
  font-size: 11px;
  font-weight: 700;
}

.side-reference-chip i {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  border: 1px solid rgba(0, 0, 0, 0.16);
  flex: 0 0 auto;
}

.side-reference-chip b {
  font-weight: 760;
}

.side-reference-chip em {
  margin-left: auto;
  font-style: normal;
  color: #7f8596;
  font-weight: 700;
}

.pattern-list {
  display: grid;
  gap: 8px;
  max-height: 360px;
  overflow: auto;
  padding-right: 2px;
}

.custom-inline {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px dashed #d7dde7;
}

.studio-grid[data-phase="choose"] .left-panel {
  min-height: calc(100vh - 164px);
}

.studio-grid[data-phase="choose"] .left-panel section[data-stage="choose"] {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.studio-grid[data-phase="choose"] .left-panel .pattern-list {
  flex: 1 1 auto;
  min-height: 0;
  /* Cap so the page doesn't expand vertically when many patterns are added. */
  max-height: min(72vh, 720px);
}

.pattern-card {
  display: grid;
  grid-template-columns: 58px 1fr;
  gap: 10px;
  width: 100%;
  min-height: 74px;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  background: #fff;
  text-align: left;
}

.pattern-card.active {
  border-color: var(--mint);
  box-shadow: inset 0 0 0 2px rgba(87, 184, 167, 0.16);
}

.pattern-thumb {
  width: 58px;
  height: 58px;
  border: 1px solid #dce1e9;
  border-radius: var(--r-xs);
  background: var(--surface-2);
}

.pattern-card strong {
  display: block;
  margin-top: 2px;
  font-size: 14px;
  line-height: 1.2;
}

.pattern-card span {
  display: block;
  margin-top: 7px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.3;
}

#previewCanvas {
  display: block;
  width: 100%;
  max-width: none;
  margin: 0;
  aspect-ratio: 1 / 1;
  height: auto;
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  background: var(--surface-2);
}

.preview-canvas-frame {
  position: relative;
  min-width: 0;
  aspect-ratio: 1 / 1;
  display: grid;
  place-items: center;
  padding: 12px;
  border-radius: var(--r-lg);
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.5), rgba(255, 255, 255, 0.16)),
    var(--table);
  border: 1px solid rgba(94, 114, 124, 0.36);
  box-shadow: 0 10px 22px rgba(49, 54, 68, 0.12), inset 0 0 0 1px rgba(255, 255, 255, 0.42);
}

.preview-canvas-frame::before {
  content: "";
  position: absolute;
  inset: 8px;
  border-radius: var(--r-md);
  border: 1px solid rgba(255, 255, 255, 0.5);
  pointer-events: none;
}

.preview-canvas-frame::after {
  content: none;
}

.choose-preview-main {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 320px;
  gap: 16px;
  align-items: start;
}

.preview-canvas-wrap {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 44px;
  gap: 12px;
  align-items: start;
}

.preview-side {
  display: grid;
  gap: 10px;
  align-content: start;
  min-width: 0;
}

.workbench {
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: center;
  min-width: 0;
  padding: 14px;
  border: 1px solid rgba(139, 148, 166, 0.42);
  border-radius: var(--r-md);
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.56), rgba(255, 255, 255, 0.13)),
    var(--table);
  box-shadow: var(--shadow);
}

.choose-preview-panel {
  width: 100%;
  margin: 0;
  padding: 18px;
  border: 1px solid rgba(139, 148, 166, 0.42);
  border-radius: var(--r-lg);
  background: rgba(255, 255, 255, 0.9);
  box-shadow: var(--sh-2);
}

.preview-canvas-frame #previewCanvas {
  display: block;
  width: 100%;
  max-width: 480px;
  height: auto;
  aspect-ratio: 1 / 1;
  margin: 0 auto;
}

.choose-preview-actions {
  display: flex;
  justify-content: center;
  margin-top: 12px;
}

.choose-start-button {
  min-width: 152px;
}

.choose-size-bar {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}

.size-rail {
  align-self: stretch;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  justify-items: center;
  gap: 8px;
  box-sizing: border-box;
  height: 100%;
  padding: 8px 4px;
  border: 1px solid rgba(139, 148, 166, 0.16);
  border-radius: var(--r-md);
  background: rgba(255, 255, 255, 0.48);
  overscroll-behavior: contain;
  touch-action: none;
}

.size-rail-label {
  color: #666d7c;
  font-size: 11px;
  font-weight: 760;
  text-align: center;
}

.size-rail-value {
  min-width: 28px;
  min-height: 20px;
  color: #345684;
  font-size: 13px;
  font-weight: 800;
  display: inline-grid;
  place-items: center;
}

.size-slider {
  --size-progress: 36%;
  width: 28px;
  height: 100%;
  min-height: 0;
  margin: 0;
  appearance: none;
  -webkit-appearance: none;
  writing-mode: vertical-lr;
  direction: rtl;
  background: transparent;
  cursor: pointer;
  touch-action: none;
}

.size-slider::-webkit-slider-runnable-track {
  width: 6px;
  height: 100%;
  border-radius: 999px;
  background: linear-gradient(to top, var(--blue) 0 var(--size-progress), #dfe6ee var(--size-progress) 100%);
}

.size-slider::-webkit-slider-thumb {
  width: 22px;
  height: 22px;
  margin-left: -8px;
  border: 3px solid #fff;
  border-radius: 50%;
  background: var(--blue);
  box-shadow: 0 2px 8px rgba(77, 119, 184, 0.28);
  -webkit-appearance: none;
}

.size-slider::-moz-range-track {
  width: 6px;
  height: 100%;
  border: 0;
  border-radius: 999px;
  background: linear-gradient(to top, var(--blue) 0 var(--size-progress), #dfe6ee var(--size-progress) 100%);
}

.size-slider::-moz-range-thumb {
  width: 16px;
  height: 16px;
  border: 3px solid #fff;
  border-radius: 50%;
  background: var(--blue);
  box-shadow: 0 2px 8px rgba(77, 119, 184, 0.28);
}

.size-slider:focus-visible {
  outline: 2px solid color-mix(in srgb, var(--blue) 55%, transparent);
  outline-offset: 2px;
}

.board-zoom-controls[hidden] {
  display: none;
}

.board-zoom-controls {
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  z-index: 7;
  display: grid;
  gap: 6px;
  padding: 6px;
  border: 1px solid rgba(139, 148, 166, 0.42);
  border-radius: var(--r-sm);
  background: rgba(255, 255, 255, 0.5);
  backdrop-filter: blur(10px);
  box-shadow: var(--sh-2);
}

.board-zoom-button {
  width: 36px;
  min-width: 36px;
  min-height: 36px;
  font-size: 18px;
  line-height: 1;
}

.board-zoom-reset {
  min-height: 30px;
  padding: 0 7px;
  border-radius: var(--r-sm);
  font-size: 11px;
  font-weight: 760;
}

.studio-grid:not([data-phase="choose"]) .choose-preview-panel {
  display: none;
}

.studio-grid[data-phase="choose"] #sceneCanvas {
  display: none;
}

#sceneCanvas {
  display: block;
  width: 100%;
  height: min(78vh, 760px);
  min-height: 600px;
  border: 1px solid rgba(99, 112, 132, 0.36);
  border-radius: var(--r-md);
  background: var(--table);
  touch-action: none;
  cursor: crosshair;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.stage-controls,
.collection-panel,
.share-panel {
  display: grid;
  gap: 9px;
}

.tool-rack {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  margin-top: 9px;
}

.tool-card {
  width: 100%;
  display: grid;
  gap: 6px;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  background: linear-gradient(180deg, rgba(255,255,255,0.96), rgba(247,249,251,0.92));
  text-align: left;
  box-shadow: var(--sh-1);
}

.tool-card.active {
  border-color: var(--blue);
  background: linear-gradient(180deg, rgba(255,255,255,0.98), rgba(235, 244, 248, 0.95));
  box-shadow: inset 0 0 0 2px rgba(77, 119, 184, 0.1), 0 8px 18px rgba(77, 119, 184, 0.08);
}

.tool-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: var(--ink);
  font-size: 12px;
  font-weight: 700;
}

.tool-count {
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
}

.bead-strip {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 5px;
}

.bead-slot {
  height: 14px;
  border: 1px solid #ced5df;
  border-radius: var(--r-pill);
  background: var(--surface-3);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.7);
}

.bead-slot.loaded {
  border-color: rgba(20, 22, 26, 0.32);
}

.tool-foot {
  color: #8a8792;
  font-size: 11px;
  font-weight: 700;
  line-height: 1.2;
}

.control-row {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.control-row.three {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.primary-button {
  color: #fff;
  border-color: var(--brand-edge);
  background: linear-gradient(180deg, #66c6b5, var(--mint));
  box-shadow: var(--sh-inset), 0 6px 16px rgba(87, 184, 167, 0.28);
}

.primary-button:hover {
  border-color: var(--brand-edge);
  background: linear-gradient(180deg, #6ecdbc, #54b3a2);
  box-shadow: var(--sh-inset), 0 10px 22px rgba(87, 184, 167, 0.32);
}

.danger-button {
  color: #fff;
  border-color: #c9433e;
  background: linear-gradient(180deg, #ef736e, var(--coral));
  box-shadow: var(--sh-inset), 0 6px 16px rgba(231, 100, 95, 0.26);
}

.danger-button:hover {
  border-color: #c9433e;
  background: linear-gradient(180deg, #f17f7a, #e1554f);
  box-shadow: var(--sh-inset), 0 10px 22px rgba(231, 100, 95, 0.3);
}

.tool-toggle {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.tool-toggle button.active,
.control-row button.active,
.craft-toggle button.active {
  border-color: var(--blue);
  background: rgba(77, 119, 184, 0.13);
  box-shadow: inset 0 0 0 2px rgba(77, 119, 184, 0.12);
}

.hint-box {
  padding: 9px 10px;
  border: 1px solid #dce1e9;
  border-radius: var(--r-sm);
  color: #504d59;
  background: var(--surface-2);
  font-size: 13px;
  line-height: 1.45;
}

.controls-entry {
  width: 100%;
}

.icon-only-button {
  position: relative;
  min-height: 44px;
  width: 44px;
  min-width: 44px;
  padding: 0;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  color: #4a5160;
  background: #fff;
}

.icon-only-button:hover {
  border-color: #aeb6c4;
  color: #1f2433;
  background: var(--surface-2);
}

.icon-text-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 0 12px;
}

.icon-text-button .btn-glyph {
  display: inline-grid;
  place-items: center;
  width: 18px;
  height: 18px;
  color: inherit;
}

.icon-text-button .btn-label {
  font-weight: 720;
  font-size: 13px;
}

.icon-toggle {
  min-height: 36px;
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  background: #fff;
  color: #4a5160;
}

.icon-toggle.active {
  border-color: #3f988b;
  color: #1f6153;
  background: rgba(87, 184, 167, 0.14);
  box-shadow: inset 0 0 0 1px rgba(87, 184, 167, 0.32);
}

.icon-pill {
  display: grid;
  place-items: center;
  min-height: 56px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  background: #fff;
  color: #4a5160;
}

.icon-pill .btn-glyph {
  display: inline-grid;
  place-items: center;
  width: 28px;
  height: 28px;
  color: inherit;
}

.icon-pill .btn-glyph svg {
  width: 28px;
  height: 28px;
}

.icon-pill:hover {
  border-color: #aeb6c4;
  color: #1f2433;
  background: var(--surface-2);
}

.icon-pill.active {
  border-color: #3f988b;
  color: #1f6153;
  background: rgba(87, 184, 167, 0.14);
  box-shadow: inset 0 0 0 1px rgba(87, 184, 167, 0.32);
}

.control-row-icons {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.remap-modal {
  position: fixed;
  inset: 0;
  z-index: 28;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 18px;
  background: rgba(28, 31, 38, 0.34);
  backdrop-filter: blur(4px);
  pointer-events: auto;
}

#remapModal {
  justify-content: center;
  align-items: center;
  padding: 18px;
  pointer-events: auto;
}

#remapModal .remap-modal-card {
  width: min(680px, calc(100vw - 24px));
  max-height: min(86vh, 760px);
  box-shadow: var(--sh-pop);
}

.remap-modal.show {
  display: flex;
}

.remap-modal-card {
  position: relative;
  width: min(700px, calc(100vw - 24px));
  max-height: min(82vh, 840px);
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  border: 1px solid rgba(139, 148, 166, 0.5);
  border-radius: var(--r-lg);
  background: #fff;
  box-shadow: var(--sh-pop);
  overflow: hidden;
}

.collection-modal-card {
  width: min(1280px, calc(100vw - 24px));
  height: min(92vh, calc(100vh - 24px));
  display: flex;
  flex-direction: column;
}

.collection-modal-card .remap-modal-body {
  flex: 1 1 auto;
  min-height: 0;
}

.share-modal-card {
  width: min(520px, calc(100vw - 24px));
}

.settings-modal-card {
  width: min(440px, calc(100vw - 24px));
}

.settings-modal-body {
  display: flex;
  flex-direction: column;
  gap: var(--sp-3);
  padding: var(--sp-3);
}

.settings-modal-body .settings-collection-row,
.settings-modal-body .settings-field {
  width: 100%;
  justify-content: space-between;
}

.settings-modal-body .bg-picker select,
.settings-modal-body .tool-style-picker select {
  flex: 1 1 auto;
  min-height: 36px;
  text-align: right;
}

.settings-link {
  min-height: 44px;
  padding: 0 11px;
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  background: rgba(255, 255, 255, 0.9);
  color: var(--ink);
  text-decoration: none;
  display: flex;
  align-items: center;
  gap: 7px;
}

.settings-link span {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.settings-link strong {
  margin-left: auto;
  color: #345684;
  font-size: 13px;
}

.settings-sandbox-row {
  display: flex;
  align-items: center;
  gap: 7px;
  min-height: 44px;
  padding: 0 9px 0 11px;
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  background: rgba(255, 255, 255, 0.9);
}

.settings-sandbox-row span {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.collection-modal-card .collection-panel {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 4px 2px;
}

.collection-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.collection-toolbar-count {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.collection-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 14px;
}

.collection-tile {
  position: relative;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  background: #fff;
  transition: transform 0.14s ease, border-color 0.14s ease, box-shadow 0.14s ease;
}

.collection-tile:hover {
  border-color: var(--mint);
  transform: translateY(-2px);
  box-shadow: 0 12px 26px rgba(49, 54, 68, 0.1);
}

.collection-tile-body {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 8px;
  width: 100%;
  padding: 10px;
  border: 0;
  border-radius: inherit;
  background: transparent;
  text-align: left;
  cursor: pointer;
}

.collection-tile-body:hover {
  transform: none;
  box-shadow: none;
}

.collection-tile-delete {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 28px;
  height: 28px;
  padding: 0;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: var(--r-pill);
  background: rgba(231, 100, 95, 0.86);
  color: #fff;
  opacity: 0;
  transition: opacity 0.14s ease, background 0.14s ease;
}

.collection-tile:hover .collection-tile-delete {
  opacity: 1;
}

.collection-tile-delete:hover {
  background: var(--coral);
  transform: none;
  box-shadow: 0 6px 14px rgba(231, 100, 95, 0.32);
}

.collection-thumb {
  width: 100%;
  aspect-ratio: 1 / 1;
  height: auto;
  border-radius: var(--r-md);
  background: var(--surface-2);
}

.collection-tile-meta {
  display: grid;
  gap: 2px;
  min-width: 0;
}

.collection-tile-meta strong {
  font-size: 13px;
  font-weight: 760;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.collection-tile-meta span {
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.collection-enlarged {
  position: absolute;
  inset: 0;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 20px;
  background: rgba(20, 24, 32, 0.78);
  backdrop-filter: blur(6px);
  border-radius: inherit;
  z-index: 2;
}

.collection-enlarged.show {
  display: flex;
}

.collection-enlarged-canvas {
  background: var(--surface-2);
  border-radius: var(--r-lg);
  box-shadow: 0 20px 40px rgba(0,0,0,0.3);
}

.collection-enlarged-meta {
  color: #fff;
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.collection-enlarged-close {
  position: absolute;
  top: 14px;
  right: 14px;
  width: 38px;
  height: 38px;
  padding: 0;
  border: 0;
  border-radius: var(--r-pill);
  background: rgba(255, 255, 255, 0.18);
  color: #fff;
  font-size: 22px;
  line-height: 1;
}

.collection-enlarged-close:hover {
  background: rgba(255, 255, 255, 0.32);
}

.collection-enlarged-actions {
  display: flex;
  gap: 10px;
}

.remap-modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 12px 14px;
  border-bottom: 1px solid #e0e5ee;
  background: linear-gradient(180deg, #ffffff, var(--surface-2));
}

.remap-modal-head strong {
  font-size: 14px;
  font-weight: 800;
}

.remap-modal-head button {
  min-height: 34px;
  min-width: 34px;
  padding: 0;
  border-radius: var(--r-pill);
  border-color: transparent;
  background: rgba(101, 115, 130, 0.08);
  color: #4a5160;
  font-size: 22px;
  line-height: 1;
  font-weight: 500;
}

.remap-modal-head button:hover {
  background: rgba(101, 115, 130, 0.16);
  color: #1f2433;
  transform: none;
  box-shadow: none;
}

.remap-modal-body {
  display: grid;
  gap: 8px;
  padding: 12px;
  overflow: auto;
}

.remap-modal-foot {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 10px 12px 12px;
  border-top: 1px solid #e6eaf0;
  background: #fff;
}

.remap-panel {
  display: grid;
  gap: 7px;
  padding: 9px;
  border: 1px solid #dce1e9;
  border-radius: var(--r-md);
  background: linear-gradient(180deg, #ffffff, var(--surface-2));
}

.remap-head {
  color: var(--muted);
  font-size: 12px;
  font-weight: 750;
}

.remap-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(160px, 1fr);
  align-items: center;
  gap: 8px;
  min-height: 40px;
}

.remap-from {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-width: 0;
}

.remap-swatch {
  width: 16px;
  height: 16px;
  border: 1px solid rgba(0, 0, 0, 0.16);
  border-radius: 4px;
  box-shadow: inset 0 0 0 3px rgba(255, 255, 255, 0.44);
  flex: 0 0 auto;
}

.remap-label {
  font-size: 12px;
  font-weight: 700;
  color: #504d59;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.remap-arrow {
  color: #9b9aa3;
  font-size: 12px;
  font-weight: 800;
}

.remap-target-area {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
  min-height: 0;
}

.remap-source-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.remap-source-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 6px;
  min-height: 32px;
  border: 1px solid var(--line);
  border-radius: var(--r-pill);
  background: var(--surface-2);
  cursor: pointer;
}

.remap-source-chip:hover {
  border-color: #aeb6c4;
}

.remap-source-chip.active {
  border-color: #3f988b;
  box-shadow: inset 0 0 0 2px rgba(63, 152, 139, 0.18);
  background: rgba(87, 184, 167, 0.08);
}

.remap-source-chip.remapped {
  background: rgba(77, 119, 184, 0.08);
}

.remap-source-chip-swatch {
  width: 18px;
  height: 18px;
  border: 1px solid rgba(0, 0, 0, 0.16);
  border-radius: 5px;
  flex: 0 0 auto;
}

.remap-source-chip-arrow {
  color: #9b9aa3;
  font-size: 11px;
  font-weight: 800;
}

.remap-hint {
  color: var(--muted);
  font-size: 12px;
  padding: 4px 2px;
}

.remap-target-label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  font-weight: 700;
  color: #504d59;
  padding: 4px 0;
}

.remap-open-picker {
  min-height: 34px;
  padding: 0 10px;
  border-radius: var(--r-sm);
  font-size: 12px;
  font-weight: 760;
}

.remap-card {
  display: grid;
  gap: 12px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  background: #fff;
}

.remap-card + .remap-card {
  margin-top: 10px;
}

.remap-card-head {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: center;
  gap: 12px;
  padding-bottom: 10px;
  border-bottom: 1px dashed rgba(139, 148, 166, 0.32);
}

.remap-to,
.remap-from {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.remap-swatch {
  width: 26px;
  height: 26px;
  border: 1px solid rgba(0, 0, 0, 0.16);
  border-radius: var(--r-xs);
  flex: 0 0 auto;
}

.remap-arrow {
  color: #9b9aa3;
  font-size: 14px;
  font-weight: 800;
}

.remap-label {
  font-size: 12px;
  font-weight: 700;
  color: #504d59;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.swatch-grid {
  display: grid;
  grid-template-columns: repeat(12, minmax(0, 1fr));
  gap: 4px;
  width: 100%;
  min-width: 0;
}

.remap-picker-modal-card {
  width: min(680px, calc(100vw - 24px));
  max-height: min(86vh, 760px);
}

.swatch-cell {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  min-height: 0;
  padding: 0;
  border: 1px solid rgba(0, 0, 0, 0.14);
  border-radius: var(--r-sm);
  cursor: pointer;
  transition: transform 0.12s ease, box-shadow 0.14s ease, border-color 0.14s ease;
}

.swatch-cell,
.swatch-cell:hover,
.swatch-cell:active,
.remap-source-chip,
.remap-source-chip:hover,
.remap-source-chip:active {
  transform: none;
}

.swatch-cell:hover {
  box-shadow: 0 4px 10px rgba(0,0,0,0.18);
  border-color: #aeb6c4;
  z-index: 1;
}

.swatch-cell.active {
  border-color: #2f7d6e;
  box-shadow: 0 0 0 2px rgba(63, 152, 139, 0.5), 0 6px 12px rgba(0,0,0,0.16);
  z-index: 1;
}

.swatch-cell.active::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background:
    linear-gradient(135deg, transparent 55%, rgba(255,255,255,0.65) 55% 62%, transparent 62%);
  pointer-events: none;
}

.custom-panel {
  display: grid;
  gap: 9px;
}

.palette-size-picker,
.settings-palette-picker {
  display: flex;
  align-items: center;
  gap: 6px;
}

.palette-size-picker {
  padding: 6px;
  border: 1px solid rgba(139, 148, 166, 0.36);
  border-radius: var(--r-sm);
  background: rgba(255, 255, 255, 0.82);
}

.palette-size-picker span {
  padding: 0 4px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.palette-size-picker button,
.settings-palette-picker button {
  min-height: 30px;
  padding: 0 9px;
  border: 1px solid #d8dee8;
  border-radius: var(--r-sm);
  background: #fff;
  color: #4a5160;
  font-size: 12px;
  font-weight: 800;
}

.palette-size-picker button.active,
.settings-palette-picker button.active {
  border-color: #2f7d6e;
  background: rgba(87, 184, 167, 0.16);
  color: #1f6153;
  box-shadow: inset 0 0 0 1px rgba(87, 184, 167, 0.28);
}

.settings-palette-row {
  align-items: center;
}

#customImageInput {
  display: none;
}

.check-row {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 32px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.check-row input {
  width: 16px;
  height: 16px;
  accent-color: var(--mint);
}

.custom-stats {
  display: grid;
  gap: 3px;
  padding: 9px;
  border: 1px dashed #bdc5d0;
  border-radius: var(--r-sm);
  color: var(--muted);
  background: var(--surface-2);
  font-size: 12px;
  line-height: 1.35;
}

.custom-stats.is-empty {
  display: none;
}

.pattern-color-stats {
  display: grid;
  grid-template-columns: 1fr;
  gap: 6px;
}

.pattern-recalc-pending {
  min-height: 22px;
  display: flex;
  align-items: center;
  padding: 0 8px;
  border: 1px solid rgba(105, 126, 156, 0.28);
  border-radius: var(--r-sm);
  background: rgba(242, 246, 252, 0.95);
  color: #5d708f;
  font-size: 11px;
  font-weight: 760;
}

.pattern-color-reset {
  min-height: 24px;
  padding: 0 8px;
  border: 1px dashed #c4ccda;
  border-radius: var(--r-sm);
  background: rgba(248, 250, 253, 0.96);
  color: #5f6880;
  font-size: 11px;
  font-weight: 760;
}

.pattern-color-reset,
.pattern-color-reset:hover,
.pattern-color-reset:active {
  transform: none;
  box-shadow: none;
}

.pattern-color-reset:hover {
  border-color: #aeb9cb;
  background: var(--surface-2);
}

.pattern-color-chip {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  min-height: 38px;
  padding: 7px 10px;
  border: 1px solid #d8dee8;
  border-radius: var(--r-sm);
  background: rgba(255, 255, 255, 0.92);
  color: inherit;
  text-align: left;
  cursor: pointer;
  transition: border-color 120ms ease, background-color 120ms ease, opacity 120ms ease;
}

.pattern-color-chip,
.pattern-color-chip:hover,
.pattern-color-chip:active {
  min-height: 38px;
  transform: none;
  box-shadow: none;
}

.pattern-color-chip:hover {
  border-color: #b8c3d3;
  background: rgba(255, 255, 255, 0.98);
}

.pattern-color-chip.is-hidden {
  opacity: 0.84;
  border-style: dashed;
  border-color: #aeb9ca;
}

.pattern-color-chip .map {
  color: #7b8597;
  font-size: 10px;
  font-weight: 700;
}

.pattern-color-chip .map-arrow {
  color: #7b8597;
  font-size: 10px;
  font-weight: 800;
}

.pattern-color-chip .dot {
  width: 14px;
  height: 14px;
  border: 1px solid rgba(0, 0, 0, 0.18);
  border-radius: 50%;
  flex: 0 0 auto;
}

.pattern-color-chip .dot.target {
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.56);
}

.pattern-color-chip .code {
  color: #535061;
  font-size: 13px;
  font-weight: 760;
}

.pattern-color-chip .count {
  margin-left: auto;
  color: #7a7787;
  font-size: 13px;
  font-weight: 700;
}

.custom-stats strong {
  color: var(--ink);
  font-size: 13px;
}

.custom-stats span {
  overflow-wrap: anywhere;
}

.share-card {
  display: grid;
  gap: 4px;
  padding: 10px;
  border: 1px solid rgba(87, 184, 167, 0.38);
  border-radius: var(--r-sm);
  background: linear-gradient(180deg, rgba(87, 184, 167, 0.13), rgba(255, 255, 255, 0.9));
}

.share-card strong {
  font-size: 14px;
}

.share-card span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.color-palette {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
  flex: 1 1 auto;
  min-height: 0;
  max-height: none;
  overflow: auto;
  padding-right: 2px;
  align-content: start;
}

.color-palette.inspect-mode {
  display: block;
  padding-right: 0;
}

.inspect-assist {
  display: grid;
  gap: 10px;
  align-content: start;
}

.inspect-card {
  margin: 0;
  border: 1px solid rgba(139, 148, 166, 0.42);
  border-radius: var(--r-md);
  background: rgba(255, 255, 255, 0.92);
  padding: 8px;
  box-shadow: var(--sh-1);
}

.inspect-card-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 7px;
}

.inspect-card-head strong {
  font-size: 13px;
  font-weight: 780;
}

.inspect-card-head span {
  color: var(--muted);
  font-size: 11px;
  font-weight: 620;
}

.inspect-canvas {
  display: block;
  width: 100%;
  height: 264px;
  border: 1px solid rgba(122, 132, 146, 0.22);
  border-radius: var(--r-md);
  background: var(--surface-2);
}

.color-chip {
  position: relative;
  min-height: 48px;
  padding: 3px 4px 2px;
  border: 1px solid var(--line);
  border-radius: var(--r-xs);
  background: #fff;
  box-shadow: none;
}

.color-chip.active {
  border-color: var(--ink);
  box-shadow: inset 0 0 0 2px rgba(38, 36, 43, 0.1);
}

.color-chip.needed {
  border-color: rgba(86, 184, 166, 0.66);
  box-shadow:
    inset 0 0 0 1px rgba(86, 184, 166, 0.52),
    0 0 0 1px rgba(86, 184, 166, 0.38),
    0 0 16px rgba(86, 184, 166, 0.42),
    0 0 24px rgba(86, 184, 166, 0.24);
  animation: needed-chip-glow 1.15s ease-in-out infinite;
}

.color-chip.needed::before {
  content: "";
  position: absolute;
  inset: -3px;
  border-radius: calc(var(--r-xs) + 3px);
  pointer-events: none;
  background: radial-gradient(circle at 50% 50%, rgba(86, 184, 166, 0.34), rgba(86, 184, 166, 0) 70%);
  opacity: 0.8;
  animation: needed-chip-aura 1.15s ease-in-out infinite;
}

.color-chip.active.needed {
  border-color: #258878;
  box-shadow:
    inset 0 0 0 2px rgba(37, 136, 120, 0.36),
    0 0 0 1px rgba(86, 184, 166, 0.52),
    0 0 20px rgba(86, 184, 166, 0.5),
    0 0 28px rgba(86, 184, 166, 0.34);
  animation: needed-chip-glow 0.95s ease-in-out infinite;
}

.color-chip.active.needed::before {
  opacity: 1;
  animation-duration: 0.95s;
}

.color-chip.held {
  border-color: var(--coral);
  box-shadow: inset 0 0 0 2px rgba(231, 100, 95, 0.16);
}

.swatch {
  display: block;
  width: 100%;
  height: 27px;
  border: 1px solid rgba(0, 0, 0, 0.13);
  border-radius: var(--r-xs);
  box-shadow:
    inset 0 0 0 2px rgba(255, 255, 255, 0.36),
    inset 0 -2px 4px rgba(0, 0, 0, 0.12);
}

/* H1 transparent bead — checkered pattern, no tinted background */
.swatch.is-transparent,
.swatch-cell.is-transparent,
.remap-swatch.is-transparent {
  background:
    repeating-conic-gradient(#d8dde4 0% 25%, #f2f4f7 0% 50%)
    0 0 / 8px 8px !important;
  border-color: rgba(0, 0, 0, 0.08);
  box-shadow: none;
}

.chip-label {
  display: block;
  margin-top: 3px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 760;
  line-height: 1;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  letter-spacing: 0;
}

.chip-count {
  position: absolute;
  top: 4px;
  right: 4px;
  min-width: 16px;
  height: 16px;
  padding: 0 3px;
  display: grid;
  place-items: center;
  border: 1px solid rgba(38, 36, 43, 0.16);
  border-radius: var(--r-pill);
  color: #4f4c57;
  background: rgba(255, 255, 255, 0.9);
  font-size: 10px;
  font-weight: 800;
  line-height: 1;
}

@keyframes needed-chip-glow {
  0% {
    box-shadow:
      inset 0 0 0 1px rgba(86, 184, 166, 0.34),
      0 0 0 1px rgba(86, 184, 166, 0.24),
      0 0 10px rgba(86, 184, 166, 0.32),
      0 0 20px rgba(86, 184, 166, 0.2);
  }

  50% {
    box-shadow:
      inset 0 0 0 1px rgba(86, 184, 166, 0.62),
      0 0 0 1px rgba(86, 184, 166, 0.54),
      0 0 20px rgba(86, 184, 166, 0.56),
      0 0 34px rgba(86, 184, 166, 0.34);
  }

  100% {
    box-shadow:
      inset 0 0 0 1px rgba(86, 184, 166, 0.34),
      0 0 0 1px rgba(86, 184, 166, 0.24),
      0 0 10px rgba(86, 184, 166, 0.32),
      0 0 20px rgba(86, 184, 166, 0.2);
  }
}

@keyframes needed-chip-aura {
  0% {
    opacity: 0.62;
    transform: scale(0.98);
  }

  50% {
    opacity: 1;
    transform: scale(1.02);
  }

  100% {
    opacity: 0.62;
    transform: scale(0.98);
  }
}

@media (prefers-reduced-motion: reduce) {
  .color-chip.needed,
  .color-chip.needed::before,
  .color-chip.active.needed {
    animation: none;
  }
}

@media (min-width: 1181px) {
  html,
  body {
    height: 100%;
    overflow: hidden;
  }

  .app-shell {
    height: 100dvh;
    min-height: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }

  .topbar {
    flex: 0 0 auto;
  }

  .studio-grid {
    flex: 1 1 auto;
    min-height: 0;
    overflow: hidden;
  }

  .side-panel,
  .workbench,
  .right-panel {
    min-height: 0;
    overflow: hidden;
  }

  .side-panel section {
    min-height: 0;
    overflow: hidden;
  }

  .color-palette {
    overflow-y: auto;
    overscroll-behavior: contain;
  }

  .pattern-list {
    overscroll-behavior: contain;
  }
}

.slider-field {
  display: grid;
  gap: 6px;
}

.slider-field label {
  display: flex;
  justify-content: space-between;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.slider-field input {
  width: 100%;
  accent-color: var(--mint);
}

.craft-toggle {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.collection-item {
  display: grid;
  gap: 4px;
  padding: 9px;
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  background: #fff;
  font-size: 12px;
}

.collection-item strong {
  font-size: 13px;
}

.collection-item span {
  color: var(--muted);
}

.collection-item-button {
  width: 100%;
  text-align: left;
}

.empty-state {
  padding: 10px;
  border: 1px dashed #bdc5d0;
  border-radius: var(--r-sm);
  color: var(--muted);
  font-size: 13px;
  text-align: center;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  z-index: 20;
  max-width: min(460px, calc(100vw - 32px));
  padding: 11px 14px;
  border: 1px solid rgba(38, 36, 43, 0.18);
  border-radius: var(--r-sm);
  color: #fff;
  background: rgba(38, 36, 43, 0.92);
  box-shadow: 0 18px 42px rgba(38, 36, 43, 0.2);
  opacity: 0;
  transform: translate(-50%, 18px);
  pointer-events: none;
  transition: opacity 0.18s ease, transform 0.18s ease;
  font-size: 14px;
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

.place-hint {
  position: fixed;
  left: 50%;
  top: 98px;
  z-index: 26;
  max-width: min(560px, calc(100vw - 28px));
  padding: 10px 14px;
  border: 1px solid rgba(38, 36, 43, 0.14);
  border-radius: var(--r-md);
  color: rgba(38, 36, 43, 0.9);
  background: rgba(255, 255, 255, 0.94);
  box-shadow: 0 10px 26px rgba(38, 36, 43, 0.12);
  opacity: 0;
  transform: translate(-50%, -8px);
  pointer-events: none;
  transition: opacity 0.22s ease, transform 0.22s ease;
  font-size: 13px;
  line-height: 1.35;
}

.place-hint.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

.achievement-toast {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 32;
  min-width: 228px;
  max-width: min(360px, calc(100vw - 24px));
  padding: 10px 12px;
  border: 1px solid rgba(38, 36, 43, 0.2);
  border-radius: var(--r-xs);
  background: rgba(252, 252, 253, 0.97);
  color: #26242b;
  box-shadow: 0 12px 34px rgba(38, 36, 43, 0.14);
  opacity: 0;
  transform: translateX(20px);
  pointer-events: none;
  transition: opacity 0.16s ease, transform 0.18s ease;
  display: grid;
  gap: 2px;
}

.achievement-toast .label {
  color: #6a6674;
  font-size: 11px;
  letter-spacing: 0;
  font-weight: 700;
}

.achievement-toast strong {
  font-size: 14px;
  font-weight: 800;
}

.achievement-toast.show {
  opacity: 1;
  transform: translateX(0);
}

@media (max-width: 1280px) {
  .topbar {
    grid-template-columns: minmax(220px, 1fr) minmax(380px, 1.6fr) auto;
    gap: 12px;
    padding: 10px 14px;
  }

  .brand p {
    display: none;
  }
}

@media (max-width: 1180px) {
  .studio-grid {
    grid-template-columns: 240px minmax(420px, 1fr);
  }

  .right-panel {
    grid-column: 1 / -1;
  }

  .right-panel section {
    min-height: auto;
    display: block;
  }

  .color-palette {
    grid-template-columns: repeat(auto-fit, minmax(56px, 1fr));
    max-height: 320px;
    flex: initial;
  }

  .topbar {
    grid-template-columns: 1fr auto;
    grid-template-rows: auto auto;
  }

  .workflow-progress {
    grid-column: 1 / -1;
    grid-row: 2;
    justify-self: stretch;
    overflow-x: auto;
  }
}

@media (max-width: 860px) {
  #boardZoomControls {
    display: none !important;
  }

  .app-shell {
    padding: max(10px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right))
      10px max(10px, env(safe-area-inset-left));
  }

  .topbar,
  .studio-grid,
  .right-panel {
    grid-template-columns: 1fr;
  }

  .topbar {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-rows: auto auto;
    gap: 6px 8px;
    padding: 7px 10px;
    min-height: 0;
    border-radius: var(--r-sm);
  }

  .topbar-lead {
    grid-column: 1;
    grid-row: 1;
    justify-content: flex-start;
  }

  .brand-mark {
    display: none;
  }

  .brand h1 {
    font-size: 20px;
  }

  .workflow-progress {
    grid-column: 1 / -1;
    grid-row: 2;
    justify-content: flex-start;
    overflow-x: auto;
    scrollbar-width: none;
    padding: 3px 6px;
    min-height: 34px;
  }

  .workflow-progress::-webkit-scrollbar {
    display: none;
  }

  .top-stats {
    grid-column: 2;
    grid-row: 1;
    justify-content: flex-end;
    width: auto;
    margin-top: 8px;
  }

  .top-stats .icon-button-label {
    display: none;
  }

  .current-pattern[data-visible="true"] {
    display: none;
  }

  .brand p {
    font-size: 13px;
  }

  .studio-grid {
    gap: 12px;
  }

  .studio-grid[data-phase="choose"] {
    grid-template-columns: minmax(0, 1fr);
  }

  .side-panel section {
    padding: 11px;
  }

  .pattern-list {
    max-height: 260px;
  }

  .studio-grid[data-phase="choose"] .left-panel {
    min-height: auto;
  }

  .studio-grid[data-phase="choose"] .left-panel .pattern-list {
    max-height: 260px;
  }

  .workbench {
    order: -1;
    padding: 8px;
  }

  .studio-grid:not([data-phase="choose"]) .left-panel {
    order: 1;
  }



  .studio-grid:not([data-phase="choose"]) #sceneCanvas {
    height: min(50vh, 440px);
    min-height: 300px;
  }



  .studio-grid:not([data-phase="choose"]) .left-panel section[data-stage="working"] {
    display: flex;
    flex-direction: column;
  }

  .studio-grid:not([data-phase="choose"]) .left-panel section[data-stage="working"] .section-title {
    order: 0;
  }

  .studio-grid:not([data-phase="choose"]) #stageControls {
    order: 3;
  }

  .studio-grid:not([data-phase="choose"]) #toolRack {
    order: 1;
  }

  .studio-grid:not([data-phase="choose"]) .side-reference {
    order: 2;
    margin-top: 10px;
    margin-bottom: 16px;
  }

  #sceneCanvas {
    height: min(78vh, 640px);
    min-height: 520px;
  }

  .color-palette {
    max-height: 300px;
    grid-template-columns: repeat(auto-fit, minmax(54px, 1fr));
  }

  .choose-preview-main {
    grid-template-columns: 1fr;
  }

  .preview-canvas-wrap {
    grid-template-columns: minmax(0, 1fr) 44px;
    gap: 8px;
  }

  .studio-grid[data-phase="choose"] .choose-preview-actions {
    position: sticky;
    bottom: 10px;
    z-index: 8;
    margin: 12px -4px -4px;
    padding: 8px;
    border: 1px solid rgba(139, 148, 166, 0.28);
    border-radius: var(--r-md);
    background: rgba(255, 255, 255, 0.86);
    backdrop-filter: blur(12px);
    box-shadow: var(--sh-2);
  }

  .choose-start-button {
    width: min(100%, 220px);
    min-height: 48px;
  }

  .preview-canvas-frame {
    padding: 10px;
  }

  .preview-side {
    grid-template-columns: 1fr;
  }

  .pattern-color-stats {
    grid-template-columns: repeat(auto-fit, minmax(128px, 1fr));
  }

  button {
    min-height: 44px;
  }

  .pattern-color-chip {
    min-height: 44px;
    padding: 8px 10px;
  }

  .palette-size-picker button,
  .settings-palette-picker button {
    min-height: 44px;
    padding: 0 12px;
  }

  .check-row {
    min-height: 44px;
  }

  .remap-source-chip {
    min-width: 36px;
    min-height: 36px;
    padding: 3px 5px;
  }

  .remap-target-area {
    min-height: 0;
  }

  .swatch-grid {
    grid-template-columns: repeat(9, minmax(0, 1fr));
    gap: 5px;
  }

  .board-zoom-controls {
    position: static;
    transform: none;
    margin: 8px 0 0 auto;
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 4px;
    background: rgba(255, 255, 255, 0.72);
  }

  .board-zoom-reset {
    min-height: 36px;
    padding: 0 8px;
  }

  /* ── Mobile palette: give chips room so box-shadow/::before glow isn't clipped ── */
  .color-palette {
    padding: 4px;
  }

  /* ── Active chip on mobile: outline is never clipped by overflow:auto ── */
  .color-chip.active {
    outline: 2px solid var(--ink);
    outline-offset: 2px;
    box-shadow:
      inset 0 0 0 2px rgba(38, 36, 43, 0.08),
      0 0 0 4px rgba(38, 36, 43, 0.06);
  }

  .color-chip.active.needed {
    outline-color: #258878;
  }
}

/* ═══════════════════════════════════════════════════════
   Orientation overlay
   Phones must be portrait; tablets must be landscape.
   Web can't lock OS rotation — show a full-screen prompt.
═══════════════════════════════════════════════════════ */
.orientation-overlay {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 9000;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 20px;
  background: var(--bg);
  color: var(--ink);
  text-align: center;
  padding: 32px;
  font-size: 17px;
  font-weight: 600;
  line-height: 1.5;
}

.orientation-overlay .msg-portrait,
.orientation-overlay .msg-landscape {
  display: none;
  margin: 0;
}

.orientation-overlay svg {
  opacity: 0.72;
  animation: rotate-hint 2s ease-in-out infinite;
}

@keyframes rotate-hint {
  0%, 100% { transform: rotate(0deg); }
  40%       { transform: rotate(18deg); }
  60%       { transform: rotate(-18deg); }
}

/* Phone in landscape → ask to go portrait */
@media (hover: none) and (pointer: coarse) and (max-width: 860px) and (orientation: landscape) {
  .orientation-overlay {
    display: flex;
  }
  .orientation-overlay .msg-portrait { display: block; }
  .orientation-overlay .msg-landscape { display: none; }
}

/* Tablet (touch) in portrait → ask to go landscape */
@media (hover: none) and (pointer: coarse) and (min-width: 861px) and (orientation: portrait) {
  .orientation-overlay {
    display: flex;
  }
  .orientation-overlay .msg-portrait { display: none; }
  .orientation-overlay .msg-landscape { display: block; }
}

@media (max-width: 620px) {
  .app-shell {
    padding: max(8px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right))
      8px max(8px, env(safe-area-inset-left));
  }

  .section-title {
    font-size: 14px;
  }

  .topbar {
    border-radius: var(--r-sm);
    padding: 6px 8px;
  }

  .brand p {
    display: none;
  }

  .choose-size-bar {
    gap: 7px;
  }

  .right-panel {
    gap: 10px;
  }

  .remap-row {
    grid-template-columns: minmax(0, 1fr);
    gap: 5px;
  }

  .remap-arrow {
    display: none;
  }

  .color-palette {
    grid-template-columns: repeat(auto-fit, minmax(48px, 1fr));
    max-height: 240px;
  }

  #sceneCanvas {
    height: min(80vh, 620px);
    min-height: 500px;
  }
}

@media (max-width: 420px) {
  .topbar {
    gap: 4px 6px;
  }


  .workflow-progress {
    justify-content: flex-start;
    overflow-x: auto;
  }

  .workflow-step {
    width: 44px;
    justify-content: center;
    gap: 0;
    padding: 4px 0;
  }

  .workflow-step > span:not(.step-dot) {
    display: none;
  }

  .workflow-step.active {
    width: auto;
    gap: 6px;
    padding: 4px 9px;
  }

  .workflow-step.active > span:not(.step-dot) {
    display: inline;
  }

  .workflow-sep {
    flex-basis: 8px;
    width: 8px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    scroll-behavior: auto !important;
    transition-duration: 0.01ms !important;
  }

  button:hover,
  button:active,
  .collection-tile:hover,
  .swatch-cell:hover {
    transform: none !important;
  }
}
