:root {
  --paper: #e4e7ec;
  --sheet: #ffffff;
  --ink: #1c2430;
  --muted: #3e4a5c;
  --line: #c5ced8;
  --accent: #1f4d8c;
  --accent-deep: #183f73;
  --grout: #8d877e;
  --tile: #f3efe8;
  --shadow: 0 14px 30px rgba(28, 36, 48, 0.16);
  --ease: cubic-bezier(0.16, 1, 0.3, 1);
}

* { box-sizing: border-box; }

html { color-scheme: light; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--paper);
  color: var(--ink);
  font-family: "Segoe UI", system-ui, sans-serif;
  font-size: 16px;
  line-height: 1.45;
  caret-color: var(--accent);
}

::selection {
  background: var(--accent);
  color: #fff;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

* {
  scrollbar-color: #8e99a8 transparent;
}

button,
input {
  font: inherit;
  color: inherit;
}

.wrap {
  max-width: 1120px;
  margin: 0 auto;
  padding: 28px 24px 32px;
}

.header-top {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px 20px;
  margin-bottom: 8px;
}

.header-top h1 {
  margin: 0;
}

h1 {
  margin: 0 0 8px;
  font-size: 2rem;
  font-weight: 650;
  letter-spacing: -0.03em;
  line-height: 1.1;
  text-wrap: balance;
}

.lead {
  margin: 0;
  max-width: 40rem;
  color: var(--muted);
}

header .row-between {
  align-items: flex-start;
  gap: 16px;
}

header .row-between .lead {
  flex: 1;
}

main {
  display: grid;
  grid-template-columns: 300px minmax(0, 1fr);
  gap: 36px;
  align-items: start;
  margin-top: 28px;
}

.controls-toggle {
  display: none;
}

.controls {
  display: flex;
  flex-direction: column;
  gap: 22px;
  background: var(--sheet);
  border-radius: 10px;
  padding: 20px;
  box-shadow: 0 8px 24px rgba(28, 36, 48, 0.06);
}

.row-between {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
}

h2 {
  margin: 0;
  font-size: 1rem;
  font-weight: 650;
}

.count {
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.primary,
.secondary {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-height: 44px;
  padding: 0 16px;
  border-radius: 8px;
  cursor: pointer;
}

.primary {
  border: 0;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
}

.primary:hover { background: var(--accent-deep); }

.primary:disabled {
  background: #d5dbe3;
  color: var(--muted);
  cursor: not-allowed;
}

.secondary {
  border: 1px solid var(--accent);
  background: transparent;
  color: var(--accent);
  font-weight: 600;
}

.secondary:hover { background: #e7eef8; }

.secondary:disabled {
  border-color: var(--line);
  color: #5c6b7e;
  background: transparent;
  cursor: not-allowed;
}

.thumbs {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.pattern-gallery {
  display: flex;
  flex-direction: column;
  gap: 14px;
  max-height: min(52vh, 480px);
  overflow: auto;
  padding-right: 4px;
}

.gallery-section-title {
  margin: 0 0 8px;
  font-size: 0.92rem;
  font-weight: 650;
  color: var(--muted);
}

.preset-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.preset-grid--dense {
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.preset-grid--dense .preset-card {
  padding: 8px;
  gap: 6px;
}

.preset-grid--dense .preset-name {
  font-size: 0.82rem;
  line-height: 1.2;
}

.preset-card {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  cursor: pointer;
  text-align: left;
  transition: border-color 160ms var(--ease), box-shadow 160ms var(--ease), transform 160ms var(--ease);
}

.preset-card:hover {
  border-color: #93a2b8;
  box-shadow: 0 6px 18px rgba(28, 36, 48, 0.08);
  transform: translateY(-1px);
}

.preset-card.is-active {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(31, 77, 140, 0.12);
}

.preset-thumb {
  display: block;
  width: 100%;
  aspect-ratio: 1;
  border-radius: 8px;
  overflow: hidden;
  background: var(--tile);
  border: 1px solid #dbe1e8;
}

.preset-name {
  display: block;
  font-size: 0.92rem;
  font-weight: 600;
  color: var(--ink);
  line-height: 1.25;
}

.thumb {
  position: relative;
  aspect-ratio: 1;
  margin: 0;
  border-radius: 6px;
  overflow: hidden;
  background: #e7ebf0;
}

.thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.thumb-open {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  padding: 0;
  border: 0;
  background: transparent;
  cursor: pointer;
}

.thumb [data-remove] {
  position: absolute;
  z-index: 1;
  top: 4px;
  right: 4px;
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 0;
  border-radius: 6px;
  background: rgba(28, 36, 48, 0.82);
  color: #fff;
  cursor: pointer;
}

.thumb [data-remove]:hover { background: #1c2430; }

dialog {
  border: 0;
  padding: 20px;
  border-radius: 10px;
  max-width: calc(100vw - 32px);
  max-height: calc(100vh - 32px);
  overflow: auto;
  color: var(--ink);
  background: var(--sheet);
  box-shadow: 0 16px 40px rgba(28, 36, 48, 0.18);
}

dialog::backdrop {
  background: rgba(28, 36, 48, 0.48);
}

dialog h2 { margin: 0 0 8px; }

.crop-help {
  margin: 0 0 16px;
  max-width: 36rem;
  color: var(--muted);
}

.crop-stage {
  position: relative;
  width: fit-content;
  max-width: min(720px, 72vw);
  margin: 0 auto;
  overflow: hidden;
  line-height: 0;
  background: var(--tile);
}

.crop-stage img {
  display: block;
  max-width: min(720px, 72vw);
  max-height: 60vh;
  width: auto;
  height: auto;
}

.crop-box {
  position: absolute;
  box-sizing: border-box;
  border: 2px solid #fff;
  box-shadow:
    0 0 0 1px rgba(28, 36, 48, 0.9),
    0 0 0 9999px rgba(28, 36, 48, 0.55);
  cursor: grab;
  touch-action: none;
}

.crop-box:active { cursor: grabbing; }

.crop-handle {
  position: absolute;
  right: 4px;
  bottom: 4px;
  width: 28px;
  height: 28px;
  padding: 0;
  border: 2px solid var(--accent);
  border-radius: 4px;
  background: #fff;
  cursor: nwse-resize;
}

.crop-actions { margin-top: 16px; }

.empty-list {
  margin: 0;
  color: var(--muted);
}

.note {
  margin: 0;
  color: var(--muted);
}

.note:empty { display: none; }

.field {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.field-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

input[type="range"] {
  flex: 1;
  width: 100%;
  accent-color: var(--accent);
}

output {
  min-width: 4.5rem;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
  text-align: right;
}

.split {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.split label {
  display: flex;
  flex-direction: column;
  gap: 8px;
  color: var(--muted);
}

input[type="number"] {
  width: 100%;
  min-height: 44px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}

.grid-select {
  display: none;
  width: 100%;
  min-height: 44px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  font: inherit;
  font-size: 16px;
}

.file-input {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.work {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  min-width: 0;
}

.print-sheet-piece,
.print-sheet-specs {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.viewport {
  position: relative;
  width: 100%;
  height: calc(100vh - 280px);
  min-height: 280px;
  max-height: min(768px, calc(100vh - 200px));
  display: grid;
  place-items: center;
  overflow: hidden;
  padding: 10px 8px 12px;
  box-sizing: border-box;
}

body.is-drag .viewport {
  outline: 2px dashed var(--accent);
  outline-offset: -10px;
  border-radius: 8px;
}

.framed {
  display: grid;
  grid-template-columns: auto 1fr auto;
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    "top top top"
    "left mosaic right"
    "bottom bottom bottom";
  max-width: 100%;
  max-height: 100%;
  box-shadow: var(--shadow);
  border: 2px solid var(--grout);
  box-sizing: border-box;
}

.framed.no-border {
  grid-template-columns: 1fr;
  grid-template-rows: 1fr;
  grid-template-areas: "mosaic";
}

.framed.no-border .border {
  display: none;
}

.border {
  background-color: var(--tile);
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

.border-top { grid-area: top; min-width: 0; }
.border-left { grid-area: left; min-height: 0; }
.border-right { grid-area: right; min-height: 0; }
.border-bottom { grid-area: bottom; min-width: 0; }

.fabric-thumb {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  margin: 0;
  border-radius: 6px;
  overflow: hidden;
  background: #e7ebf0;
  list-style: none;
}

.fabric-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.fabric-thumb [data-remove] {
  position: absolute;
  z-index: 1;
  top: 4px;
  right: 4px;
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 0;
  border-radius: 6px;
  background: rgba(28, 36, 48, 0.82);
  color: #fff;
  cursor: pointer;
}

.fabric-thumb [data-remove]:hover { background: #1c2430; }

.mosaic {
  grid-area: mosaic;
  display: grid;
  gap: 2px;
  background: var(--grout);
  min-width: 0;
  min-height: 0;
}

.framed:not(.no-border) .mosaic {
  position: relative;
  z-index: 1;
  box-shadow: 0 0 0 2px var(--grout);
}

.mosaic.is-fresh {
  animation: settle 280ms var(--ease);
}

.cell {
  position: relative;
  min-width: 0;
  min-height: 0;
  background: var(--tile);
  overflow: hidden;
  cursor: pointer;
}

  .cell.locked {
outline: 2px dashed var(--accent);
outline-offset: -3px;
  }

  .cell.locked::before {
content: "";
position: absolute;
top: 5px;
left: 5px;
width: 9px;
height: 9px;
border: 2px solid var(--accent);
border-radius: 2px;
background: #fff;
z-index: 1;
pointer-events: none;
  }

input[type="text"],
textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  resize: vertical;
}

textarea { min-height: 72px; }

select {
  width: 100%;
  min-height: 44px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
}

.stack {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.palette-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.palette-row {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 10px;
  align-items: center;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
}

.palette-chip {
  width: 24px;
  height: 24px;
  padding: 0;
  border-radius: 999px;
  border: 1px solid rgba(28, 36, 48, 0.16);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.28);
  cursor: pointer;
  background: transparent;
  flex-shrink: 0;
}

.palette-chip:hover {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.palette-chip:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.palette-chip::-webkit-color-swatch-wrapper {
  padding: 0;
}

.palette-chip::-webkit-color-swatch {
  border: none;
  border-radius: 999px;
}

.palette-chip::-moz-color-swatch {
  border: none;
  border-radius: 999px;
}

.palette-body {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.palette-name {
  font-weight: 600;
  color: var(--ink);
}

.palette-meta {
  color: var(--muted);
  font-size: 0.88rem;
}

.palette-fabric-meta {
  color: var(--muted);
  font-size: 0.88rem;
}

.palette-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.palette-actions .secondary {
  width: auto;
  min-height: 36px;
  padding: 0 12px;
}

.palette-preview {
  display: block;
  width: 72px;
  aspect-ratio: 1;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid #dbe1e8;
  background: #e7ebf0;
  padding: 0;
  cursor: pointer;
}

.palette-preview img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

#border-fabric-state:empty {
  display: none;
}

#border-fabric-state .palette-fabric-meta {
  margin-top: 8px;
}

.work-toolbar {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

.reshuffle-switch {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 14px;
}

.reshuffle-switch .secondary {
  width: auto;
  min-width: 180px;
  min-height: 40px;
}

.reshuffle-switch + .canvas-size {
  margin-top: 30px;
}

.mode-switch {
  display: inline-flex;
  gap: 8px;
}

.mode-switch .secondary {
  width: auto;
  min-width: 112px;
}

.mode-switch .secondary[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.mode-switch .secondary[aria-pressed="true"]:hover {
  background: var(--accent-deep);
}

.mode-hint {
  margin: 0;
  color: var(--muted);
  text-align: right;
}

.note-warn {
  color: #7a3d14;
}

.hint-manual {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
}

.help-list {
  margin: 0;
  padding-left: 1.2rem;
  color: var(--muted);
}

.help-list li + li {
  margin-top: 8px;
}

.cell img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.cell svg {
  width: 100%;
  height: 100%;
  display: block;
}

.caption {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  text-align: center;
}

.canvas-size {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 650;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
}

.canvas-detail,
.scale-note {
  margin: 0;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.caption .secondary {
  width: min(100%, 280px);
  margin-top: 4px;
}

.materials {
  width: min(100%, 520px);
  margin-top: 12px;
  text-align: left;
}

.materials h3 {
  margin: 0 0 6px;
  font-size: 0.95rem;
  font-weight: 650;
  text-align: center;
}

.material-note {
  margin: 8px 0 0;
  font-size: 0.82rem;
  color: var(--muted);
  text-align: center;
}

.material-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 0;
  border-top: 1px solid var(--line);
}

.material-row:first-child {
  border-top: 0;
}

.material-thumb {
  width: 46px;
  height: 46px;
  flex-shrink: 0;
  border-radius: 6px;
  overflow: hidden;
  background: var(--tile);
  border: 1px solid var(--line);
}

.material-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.material-body {
  flex: 1;
  min-width: 0;
  font-size: 0.88rem;
  color: var(--muted);
  line-height: 1.35;
}

.material-name {
  display: block;
  color: var(--ink);
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.material-area {
  flex-shrink: 0;
  font-size: 0.95rem;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
  color: var(--accent);
}

@keyframes settle {
  from { opacity: 0.45; }
  to { opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
  .mosaic.is-fresh { animation: none; }
}

@media (min-width: 861px) {
  .controls-toggle { display: none; }
}

@media (max-width: 860px) {
  .wrap { padding: 12px 16px 28px; }
  main {
    display: flex;
    flex-direction: column;
    gap: 16px;
  }
  header .lead { display: none; }
  .controls-toggle {
    display: flex;
    order: -2;
    width: 100%;
  }
  .controls { order: -1; }
  .controls.is-collapsed { display: none; }
  .work { order: 0; gap: 10px; }
  .grid-number { display: none; }
  .grid-select { display: block; }
  .viewport {
    height: 320px;
    min-height: 320px;
    max-height: 320px;
  }
  .framed { overflow: hidden; }
  dialog {
    width: calc(100vw - 16px);
    max-height: calc(100svh - 16px);
    padding: 12px;
  }
  .crop-stage,
  .crop-stage img {
    max-width: 100%;
    max-height: calc(100svh - 190px);
  }
  .crop-handle {
    width: 44px;
    height: 44px;
  }
  .preset-grid,
  .preset-grid--dense {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .work-toolbar {
    align-items: stretch;
  }
  .mode-hint {
    text-align: left;
  }
}

.saved-schemes {
  margin-top: 36px;
  padding: 20px;
  background: var(--sheet);
  border-radius: 10px;
  box-shadow: 0 8px 24px rgba(28, 36, 48, 0.06);
}

.saved-schemes h2 {
  margin-bottom: 12px;
}

.scheme-save {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: end;
  margin-bottom: 16px;
}

.scheme-save label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex: 1 1 220px;
  color: var(--muted);
}

.scheme-save input {
  min-height: 44px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  font: inherit;
  font-size: 16px;
  color: var(--ink);
}

.scheme-save .primary {
  width: auto;
  min-width: 180px;
}

.scheme-public {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  flex: 0 0 auto;
}

.scheme-public input {
  width: 18px;
  height: 18px;
  min-height: 0;
  padding: 0;
}

.scheme-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 12px;
}

.scheme-card {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 0;
}

.scheme-link {
  display: flex;
  flex-direction: column;
  gap: 8px;
  color: inherit;
  text-decoration: none;
}

.scheme-thumb {
  display: block;
  aspect-ratio: 1;
  border-radius: 8px;
  overflow: hidden;
  background: var(--tile);
  border: 1px solid var(--line);
}

.scheme-thumb svg,
.scheme-thumb img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.scheme-thumb.is-empty {
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
}

.scheme-name {
  font-weight: 650;
  line-height: 1.25;
}

.scheme-delete {
  min-height: 36px;
}

@media print {
  body {
    background: #fff;
    print-color-adjust: exact;
    -webkit-print-color-adjust: exact;
  }
  .controls,
  .controls-toggle,
  .saved-schemes,
  header,
  .work-toolbar,
  .reshuffle-switch,
  .caption button,
  .scale-note,
  dialog {
    display: none !important;
  }
  main {
    display: block;
    margin-top: 0;
  }
  .wrap {
    max-width: none;
    padding: 0;
  }
  .print-sheet-piece {
    break-after: page;
    page-break-after: always;
    break-inside: avoid;
    page-break-inside: avoid;
  }
  .print-sheet-specs {
    break-before: page;
    page-break-before: always;
    break-inside: avoid;
    page-break-inside: avoid;
  }
  .print-sheet-specs:has(#materials[hidden]) {
    display: none !important;
  }
  .work:has(#materials[hidden]) .print-sheet-piece {
    break-after: auto;
    page-break-after: auto;
  }
  .print-sheet-piece .canvas-size,
  .print-sheet-piece .canvas-detail {
    margin-top: 8px;
    text-align: center;
  }
  .viewport {
    height: auto !important;
    max-height: none !important;
    overflow: visible !important;
  }
  .framed {
    box-shadow: none;
    border: 2px solid var(--grout);
    margin: 0 auto;
    print-color-adjust: exact;
    -webkit-print-color-adjust: exact;
  }

  .framed:not(.no-border) .mosaic {
    outline: 2px solid var(--grout);
    outline-offset: 0;
  }
  .border {
    print-color-adjust: exact;
    -webkit-print-color-adjust: exact;
  }
  .mosaic,
  .cell {
    print-color-adjust: exact;
    -webkit-print-color-adjust: exact;
  }
  .caption {
    margin-top: 12px;
  }
  @page {
    margin: 12mm;
  }
}

.site-nav {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
  padding: 6px 10px;
  border-radius: 999px;
  background: var(--sheet);
  border: 1px solid var(--line);
}
.site-nav a {
  color: var(--muted);
  text-decoration: none;
  font-weight: 600;
  padding: 4px 10px;
  border-radius: 999px;
}
.site-nav a:hover {
  color: var(--ink);
  background: #eef2f7;
}
.site-nav a[aria-current="page"] {
  color: var(--accent);
}