:root {
  --bg: #edf1f4;
  --panel: #ffffff;
  --ink: #17212b;
  --muted: #45515d;
  --line: #b9c4ce;
  --head: #1d2b38;
  --head-ink: #f3f6f8;
  --accent: #a8322d;
  --accent-ink: #ffffff;
  --link: #1b5d61;
  --ok: #1d6a3a;
  --warn: #7c5200;
  --err: #a8322d;
  --sash: #f6f3ea;
  --borderc: #3a4652;
  --clash: #c4001a;
  --focus: #1b5d61;
  --radius: 6px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12171c;
    --panel: #1b232b;
    --ink: #e8edf1;
    --muted: #b6c0c9;
    --line: #3e4c59;
    --head: #0b0f13;
    --head-ink: #e8edf1;
    --accent: #f08a7e;
    --accent-ink: #1b0f0e;
    --link: #8fd3d0;
    --ok: #7fd49c;
    --warn: #f0c060;
    --err: #ff9a8f;
    --sash: #d9d4c7;
    --borderc: #8a96a2;
    --clash: #ff5a6a;
    --focus: #8fd3d0;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 17px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
a { color: var(--link); }
h1, h2, h3 { line-height: 1.2; letter-spacing: -0.01em; }
h1 { font-size: clamp(1.9rem, 5vw, 2.6rem); margin: 22px 0 8px; font-weight: 800; }
h2 { font-size: 1.3rem; margin: 0 0 10px; font-weight: 750; }
h3 { font-size: 1.08rem; margin: 22px 0 8px; }
.wrap { max-width: 1180px; margin: 0 auto; padding: 0 16px; }
.skip { position: absolute; left: -999px; top: 8px; background: var(--panel); color: var(--ink); padding: 10px 14px; z-index: 20; }
.skip:focus { left: 8px; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

.top { background: var(--head); color: var(--head-ink); border-bottom: 2px dashed #e7d9b8; }
.brand { display: inline-flex; align-items: center; gap: 10px; color: inherit; text-decoration: none; font-weight: 750; font-size: 1.05rem; min-height: 54px; }
.lede { max-width: 62ch; margin: 0 0 18px; font-size: 1.06rem; }
.note { background: var(--panel); border: 1px solid var(--line); padding: 10px 14px; border-radius: var(--radius); }
.banner { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; margin: 0 0 16px; }
.banner p { margin: 0 0 8px; }

.tool { display: grid; gap: 28px; margin-bottom: 40px; }
@media (min-width: 1000px) {
  .tool { grid-template-columns: minmax(380px, 440px) minmax(0, 1fr); grid-template-rows: auto 1fr; grid-template-areas: "fab res" "size res"; align-items: start; column-gap: 32px; row-gap: 0; }
  .fab-sect { grid-area: fab; }
  .result { grid-area: res; }
  .size-sect { grid-area: size; }
  .to-size, .to-layout { display: none; }
}
.sect { margin-bottom: 26px; }
.sect-head h2 { margin-bottom: 2px; }
/* Its own line with a fixed height, so a changed count never moves the buttons below. */
.have { margin: 0 0 6px; color: var(--muted); min-height: 1.5em; }
.have.short { color: var(--err); font-weight: 650; }

input, select, textarea, button { font: inherit; color: inherit; }
input:not([type=checkbox]):not([type=radio]):not([type=color]), select, textarea {
  font-size: 17px; min-height: 44px; padding: 8px 10px; width: 100%;
  background: var(--panel); border: 1.5px solid var(--line); border-radius: var(--radius);
}
input[aria-invalid=true] { border-color: var(--err); box-shadow: 0 0 0 1px var(--err); }
textarea { resize: vertical; }

.fab-cols, .fab { display: grid; grid-template-columns: 34px minmax(0, 1fr) 58px 44px 44px; gap: 5px; align-items: center; }
.fab-cols { font-size: 0.9rem; color: var(--muted); padding: 0 0 2px; }
.fab-cols span:first-child { grid-column: 2; }
.fab-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.tag {
  display: inline-grid; place-items: center; width: 34px; height: 34px; border-radius: 4px;
  font: 800 15px/1 ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace; border: 1.5px solid var(--line);
}
.tag.sm { width: 28px; height: 28px; font-size: 13px; vertical-align: middle; margin-right: 6px; }
.f-count { text-align: center; }
.f-color { width: 44px; height: 44px; padding: 2px; border: 1.5px solid var(--line); border-radius: var(--radius); background: var(--panel); cursor: pointer; }
.del { width: 44px; height: 44px; display: grid; place-items: center; border: 0; background: none; border-radius: var(--radius); cursor: pointer; color: var(--muted); }
.del:hover { background: color-mix(in srgb, var(--ink) 8%, transparent); color: var(--ink); }
@media (max-width: 380px) { .fab-cols, .fab { grid-template-columns: 28px minmax(0, 1fr) 52px 44px 40px; gap: 3px; } .tag { width: 28px; } .fab .f-name { padding: 8px 6px; } }

.btns { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 10px; }
.btn {
  min-height: 44px; padding: 8px 16px; border-radius: var(--radius); border: 1.5px solid var(--ink);
  background: var(--panel); cursor: pointer; font-weight: 600;
}
.btn:hover { background: color-mix(in srgb, var(--ink) 7%, var(--panel)); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { background: color-mix(in srgb, var(--accent) 88%, #000); }
.btn.quiet { border-color: transparent; background: transparent; text-decoration: underline; text-underline-offset: 3px; padding: 8px 10px; }
.btn.danger { background: var(--err); border-color: var(--err); color: var(--accent-ink); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.msg { min-height: 0; margin: 6px 0 0; }
.msg:empty { display: none; }
.paste { margin-top: 12px; display: grid; gap: 8px; }
.paste .btn { justify-self: start; }

.seg { display: inline-flex; border: 1.5px solid var(--ink); border-radius: var(--radius); overflow: hidden; margin-bottom: 12px; }
.seg label { position: relative; }
.seg input { position: absolute; opacity: 0; inset: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.seg span { display: block; padding: 9px 14px; min-height: 44px; font-weight: 600; white-space: nowrap; }
.seg input:checked + span { background: var(--ink); color: var(--bg); }
.seg input:focus-visible + span { outline: 3px solid var(--focus); outline-offset: -3px; }
.pair { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 8px; margin-bottom: 10px; }
.pair .fld { flex: 1 1 110px; }
.pair .unit { flex: 0 1 120px; }
.x { padding-bottom: 10px; color: var(--muted); }
.fld { display: grid; gap: 3px; }
.fld > label { font-size: 0.95rem; color: var(--muted); }
.fld.wide { margin: 6px 0 10px; }
.check { display: flex; align-items: center; gap: 10px; min-height: 44px; cursor: pointer; }
.check input { width: 22px; height: 22px; accent-color: var(--accent); }
.more { margin-top: 8px; border-top: 1px solid var(--line); padding-top: 4px; }
.more summary { display: list-item; padding: 10px 0; cursor: pointer; font-weight: 650; }
.more[open] summary { margin-bottom: 4px; }

.result { min-width: 0; }
.status { font-weight: 650; margin: 0 0 10px; max-width: 70ch; min-height: 3em; }
.fix { margin: -4px 0 12px; }
.to-layout { display: inline-flex; align-items: center; min-height: 44px; margin-top: 6px; color: var(--link); font-weight: 600; }
.searching { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 12px; margin: -4px 0 12px; color: var(--muted); }
.status.ok { color: var(--ok); }
.status.warn { color: var(--warn); }
.status.err { color: var(--err); }
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }
.link-box { margin-top: 6px; }

.grid-wrap { overflow-x: auto; max-width: 100%; padding: 2px 2px 8px; }
.quilt { display: grid; grid-template-columns: var(--lab) auto; grid-template-rows: var(--lab) auto; width: max-content; }
.colnums { grid-column: 2; display: grid; grid-template-columns: repeat(var(--cols), var(--cw)); column-gap: var(--gap); padding: 0 var(--bord); }
.rownums { grid-row: 2; display: grid; grid-auto-rows: var(--ch); row-gap: var(--gap); padding: var(--bord) 0; }
.colnums span, .rownums span { display: grid; place-items: center; font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.blocks {
  grid-column: 2; grid-row: 2; display: grid; grid-template-columns: repeat(var(--cols), var(--cw)); grid-auto-rows: var(--ch);
  gap: var(--gap); background: var(--sash);
}
.quilt.bordered .blocks { border: var(--bord) solid var(--borderc); }
.cell {
  position: relative; border: 0; padding: 0; margin: 0; cursor: pointer; display: grid; place-items: center;
  box-shadow: inset 0 0 0 0.5px rgba(0, 0, 0, 0.35);
}
.lt {
  background: #fff; color: #111; border-radius: 3px; padding: 2px 4px; min-width: 1.4em; text-align: center;
  font: 800 var(--fs)/1.1 ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace; box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.45);
}
.cell.lk::after {
  content: ""; position: absolute; top: 3px; right: 3px; width: 9px; height: 9px; border-radius: 50%;
  background: #fff; box-shadow: 0 0 0 2px #111;
}
.cell.cl { outline: 3px dashed #000; outline-offset: -3px; box-shadow: inset 0 0 0 3px #fff; z-index: 1; }
/* Touching blocks: black and white dashes plus a "!" badge, so the mark never reads as a fabric colour. */
.cell.cl::before {
  content: "!"; position: absolute; left: 3px; bottom: 3px; width: 14px; height: 14px; border-radius: 50%;
  background: #111; color: #fff; font: 800 11px/14px system-ui, sans-serif; text-align: center; box-shadow: 0 0 0 1.5px #fff;
}
.cell.hole .lt { display: none; }
.cell.hole { background: repeating-linear-gradient(45deg, var(--panel) 0 5px, var(--line) 5px 7px); outline: 2px dashed var(--muted); outline-offset: -3px; }
/* A fabric whose colour repeats an earlier one is striped, so the two never look like one fabric. */
.cell.rep, .tag.rep { background-image: repeating-linear-gradient(135deg, rgba(0, 0, 0, 0.32) 0 2px, transparent 2px 7px); }
.cell.sel { box-shadow: inset 0 0 0 3px #111, inset 0 0 0 5px #fff; z-index: 2; }
.cell:focus-visible { outline: 3px solid var(--focus); outline-offset: 1px; z-index: 3; }
/* Small blocks on a big grid: letters without the badge padding, smaller lock dot and numbers. */
.quilt.tiny .lt { padding: 0 1px; min-width: 0; border-radius: 2px; }
.quilt.tiny .cell.lk::after { top: 1px; right: 1px; width: 5px; height: 5px; box-shadow: 0 0 0 1.5px #111; }
.quilt.tiny .cell.cl { outline-width: 2px; outline-offset: -2px; box-shadow: inset 0 0 0 2px #fff; }
.quilt.tiny .cell.cl::before { content: ""; width: 5px; height: 5px; left: 1px; bottom: 1px; box-shadow: 0 0 0 1px #fff; }
.quilt.tiny .colnums span, .quilt.tiny .rownums span { font-size: 10px; }
.quilt.sparse .colnums span:not(.k5) { visibility: hidden; }
.quilt.sparse .colnums span.k5 { overflow: visible; white-space: nowrap; }

.cell-panel { background: var(--panel); border: 1.5px solid var(--line); border-radius: var(--radius); padding: 12px 14px; margin: 8px 0 4px; max-width: 640px; }
.cell-panel p { margin: 0; font-weight: 600; }
.cell-panel .fld { min-width: 200px; }
.lockbar { display: flex; flex-wrap: wrap; gap: 4px; margin: 6px 0 0; }
.size-line { margin: 4px 0 0; }

.counts-wrap { overflow-x: auto; }
.counts { border-collapse: collapse; min-width: min(100%, 420px); }
.counts th, .counts td { padding: 6px 10px; border-bottom: 1px solid var(--line); text-align: right; font-variant-numeric: tabular-nums; }
.counts th:first-child, .counts td:first-child { text-align: left; }
.counts thead th { font-size: 0.95rem; color: var(--muted); font-weight: 600; }
.counts tbody th { font-weight: 500; max-width: 26ch; overflow-wrap: anywhere; }

.hint { color: var(--muted); margin: 0 0 6px; }
.rows { list-style: none; padding: 0; margin: 0; }
.rows li { display: grid; grid-template-columns: 8.2em minmax(0, 1fr); align-items: center; border-bottom: 1px solid var(--line); }
.row-chk { display: flex; align-items: center; gap: 10px; min-height: 44px; cursor: pointer; font-weight: 650; }
.row-chk input { width: 22px; height: 22px; accent-color: var(--accent); }
.seq { font: 600 16px/1.5 ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace; overflow-wrap: anywhere; padding: 6px 0; }
.rows li.done .seq { color: var(--muted); text-decoration: line-through; }
.earlier { margin-top: 18px; }
.earlier summary { display: list-item; padding: 10px 0; cursor: pointer; font-weight: 650; }
.earlier ul { list-style: none; padding: 0; margin: 0; }
.earlier li { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 6px 12px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.earlier li .btns { margin: 0; }
.restart { margin-top: 18px; }

.about { max-width: 72ch; padding-bottom: 32px; }
.about h2 { margin-top: 30px; }
.faq h3 { margin-top: 20px; }
.faq p { margin: 0; }

.undo {
  position: fixed; left: 50%; bottom: 14px; transform: translateX(-50%); z-index: 30; width: min(560px, calc(100% - 24px));
  display: flex; flex-wrap: wrap; align-items: center; gap: 6px 10px; padding: 10px 12px;
  background: var(--head); color: var(--head-ink); border-radius: var(--radius); box-shadow: 0 6px 24px rgba(0, 0, 0, 0.3);
}
.undo { flex-wrap: nowrap; }
.undo span { flex: 1 1 0; min-width: 0; }
.undo .btn { flex: 0 0 auto; }
@media (max-width: 600px) { .undo { bottom: 8px; padding: 6px 8px; gap: 6px; } .undo .btn { padding-left: 12px; padding-right: 12px; } }
.undo .btn { background: var(--head-ink); color: var(--head); border-color: var(--head-ink); }
.undo .btn.quiet { background: transparent; color: var(--head-ink); }

.foot { border-top: 1px solid var(--line); padding: 20px 0 80px; color: var(--muted); font-size: 0.95rem; }
.sb-more { line-height: 1.9; }
.sb-privacy, .sb-credit { margin: 8px 0; }
.foot a { color: var(--link); }

.print-root { display: none; }

@media (prefers-reduced-motion: reduce) { * { scroll-behavior: auto !important; transition: none !important; } }

@media print {
  @page { margin: 0.5in; }
  body { background: #fff; color: #000; font: 11pt/1.4 system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif; }
  body > :not(.print-root) { display: none !important; }
  .print-root { display: block; }
  .p-head h1 { font-size: 18pt; margin: 0 0 2pt; }
  .p-head p { margin: 0 0 8pt; }
  .p-warn { border: 1.5pt solid #000; padding: 4pt 6pt; margin: 0 0 8pt; font-weight: 600; }
  .p-grid { border-collapse: separate; border-spacing: 0; table-layout: fixed; margin: 0 0 10pt; break-inside: avoid; }
  .p-grid th { font-size: 7.5pt; font-weight: 600; color: #333; width: calc(var(--pc) * 0.7); padding: 0; }
  .p-grid thead th { height: calc(var(--pc) * 0.6); }
  .p-grid td {
    width: var(--pc); height: calc(var(--pc) * var(--pr)); padding: 0; text-align: center; vertical-align: middle; line-height: 0;
    border: 0.5pt solid #444; -webkit-print-color-adjust: exact; print-color-adjust: exact;
  }
  .p-grid td b {
    display: inline-block; background: #fff; color: #000; border: 0.6pt solid #000; border-radius: 2pt; padding: 0 1.5pt;
    min-width: 1.3em; vertical-align: middle; font: 800 max(6.5pt, calc(var(--pc) * 0.34))/1.15 ui-monospace, Menlo, Consolas, monospace;
    -webkit-print-color-adjust: exact; print-color-adjust: exact;
  }
  .p-grid.sashed { border-spacing: 2pt; }
  .p-grid td.blank { background: #fff; border-color: #000; }
  .p-line { border-bottom: 0.6pt solid #000; min-width: 2.2in; height: 14pt; flex: 1 1 auto; }
  .p-line.short { min-width: 0.6in; }
  .p-grid td.cl { outline: 2.5pt dashed #000; outline-offset: -3pt; box-shadow: inset 0 0 0 2.5pt #fff; }
  .p-grid td.rep, .p-sw.rep { background-image: repeating-linear-gradient(135deg, rgba(0, 0, 0, 0.35) 0 1.5pt, transparent 1.5pt 5pt); }
  .p-legend { display: flex; gap: 0 14pt; align-items: flex-start; margin: 0 0 6pt; break-inside: avoid; }
  .p-legend table { border-collapse: collapse; font-size: 10pt; flex: 1 1 0; min-width: 0; }
  .p-legend.c1 table { flex: 0 1 auto; }
  .p-legend.c4 table { font-size: 9pt; }
  .p-legend th, .p-legend td { border-bottom: 0.5pt solid #999; padding: 2pt 6pt 2pt 0; text-align: left; overflow-wrap: anywhere; }
  .p-legend th { white-space: nowrap; }
  .p-legend td:nth-child(n+3), .p-legend th:nth-child(n+3) { text-align: right; width: 3.2em; white-space: nowrap; overflow-wrap: normal; }
  .p-legend tr { break-inside: avoid; }
  .p-rows.newpage { break-before: page; }
  .p-rows li.brk { break-before: page; }
  .p-sw {
    display: inline-grid; place-items: center; width: 18pt; height: 14pt; border: 0.6pt solid #000;
    -webkit-print-color-adjust: exact; print-color-adjust: exact;
  }
  .p-sw b { background: #fff; padding: 0 1.5pt; font: 800 8.5pt/1.1 ui-monospace, Menlo, Consolas, monospace; border-radius: 1.5pt; }
  .p-rows h2 { font-size: 13pt; margin: 6pt 0 2pt; break-after: avoid; }
  .p-rows > p { margin: 0 0 4pt; break-after: avoid; }
  .p-rows ol { list-style: none; margin: 0; padding: 0; }
  .p-rows li { display: flex; align-items: baseline; gap: 8pt; padding: 4pt 0; border-bottom: 0.5pt solid #999; break-inside: avoid; }
  .p-rows li b { flex: 0 0 4.2em; }
  .p-rows li span:last-child { font: 600 11pt/1.4 ui-monospace, Menlo, Consolas, monospace; overflow-wrap: anywhere; }
  .p-box { flex: 0 0 auto; width: 10pt; height: 10pt; border: 1pt solid #000; position: relative; top: 1pt; }
  .p-box.on::after { content: ""; position: absolute; left: 3pt; top: 0; width: 3pt; height: 6pt; border: solid #000; border-width: 0 1.5pt 1.5pt 0; transform: rotate(45deg); }
}
a.btn { display: inline-flex; align-items: center; text-decoration: none; color: inherit; }
a.btn.primary { color: var(--accent-ink); }
