:root {
  --bg: #f6f7f9;
  --card: #ffffff;
  --ink: #16181d;
  --muted: #6b7280;
  --line: #e2e5ea;
  --accent: #2f6df6;
  --accent-ink: #ffffff;
  --danger: #c8341f;
  --danger-bg: #fdf0ee;
  --chip: #eef1f5;
  --radius: 10px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --card: #1c1f25;
    --ink: #e9ebef;
    --muted: #99a1ad;
    --line: #2b2f37;
    --accent: #5b8cff;
    --accent-ink: #0b1020;
    --danger: #ff7c66;
    --danger-bg: #33201c;
    --chip: #262a32;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

main { max-width: 900px; margin: 0 auto; padding: 16px 16px 48px; }

/* ---------------------------------------------------------------- chrome */

.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px;
  background: var(--card);
  border-bottom: 1px solid var(--line);
}
.tabs { display: flex; gap: 4px; }
.tab {
  padding: 7px 14px; border-radius: 999px;
  color: var(--muted); text-decoration: none; font-weight: 600;
}
.tab.on { background: var(--accent); color: var(--accent-ink); }
.logout button {
  background: none; border: 0; color: var(--muted);
  font: inherit; font-size: 14px; cursor: pointer; padding: 6px;
}
.foot { text-align: center; padding: 0 16px 32px; }
.foot a { color: var(--muted); font-size: 13px; }

.pagehead {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; flex-wrap: wrap; margin: 8px 0 16px;
}
.pagehead h1 { font-size: 22px; margin: 0; }
.pagehead .actions { display: flex; gap: 8px; }

/* --------------------------------------------------------------- controls */

button, .btn {
  font: inherit; font-weight: 600; cursor: pointer;
  padding: 9px 14px; border-radius: var(--radius);
  border: 1px solid var(--line); background: var(--card); color: var(--ink);
  text-decoration: none; display: inline-block;
}
button:hover, .btn:hover { border-color: var(--muted); }
.btn-primary, button.primary {
  background: var(--accent); border-color: var(--accent); color: var(--accent-ink);
}
button.danger { color: var(--danger); border-color: var(--danger); background: var(--danger-bg); }
button:disabled { opacity: .45; cursor: not-allowed; }

/* `input:not([type])` matters: a bare <input name="..."> is a text input but
   matches none of the [type=...] selectors, so it would render unstyled. */
input:not([type]), input[type=text], input[type=search], input[type=password], select, textarea {
  font: inherit; color: var(--ink); background: var(--card);
  border: 1px solid var(--line); border-radius: var(--radius);
  padding: 9px 10px; width: 100%; min-width: 0;
}
input:focus-visible, select:focus-visible, button:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}
label { display: block; font-size: 13px; font-weight: 600; color: var(--muted); }
label input, label select, label textarea { margin-top: 4px; font-weight: 400; color: var(--ink); }

.card {
  background: var(--card); border: 1px solid var(--line);
  border-radius: 14px; padding: 16px; margin-bottom: 16px;
}
.card h2 { margin: 0 0 12px; font-size: 16px; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; }
.row > * { flex: 1 1 160px; }
.row > .shrink { flex: 0 0 auto; }
.hint { color: var(--muted); font-size: 13px; }
.error { color: var(--danger); font-size: 14px; margin: 0 0 10px; }
[hidden] { display: none !important; }

/* ------------------------------------------------------------------ chips */

.chip {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 2px 9px; border-radius: 999px;
  background: var(--chip); font-size: 12.5px; white-space: nowrap;
}
.chip .glyph { font-size: 15px; }
.chip.tag { color: var(--muted); }

/* ------------------------------------------------------- multi-select --- */

.msel { position: relative; }
.msel-toggle {
  width: 100%; display: flex; align-items: center; justify-content: space-between;
  gap: 8px; font-weight: 400; text-align: left;
}
.msel-toggle .msel-label {
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.msel-toggle .msel-caret { color: var(--muted); font-size: 11px; flex: none; }
.msel-toggle[aria-expanded="true"] { border-color: var(--accent); }

.msel-panel {
  position: absolute; top: calc(100% + 6px); left: 0; z-index: 30;
  width: max(100%, 240px); max-width: 320px;
  background: var(--card); border: 1px solid var(--line); border-radius: 12px;
  padding: 10px; box-shadow: 0 10px 28px rgba(0, 0, 0, 0.16);
}
.msel-search { margin-bottom: 8px; }
.msel-actions { display: flex; justify-content: flex-end; margin-bottom: 4px; }
.msel-clear {
  background: none; border: 0; padding: 2px 4px;
  font-size: 13px; font-weight: 600; color: var(--accent); cursor: pointer;
}
.msel-clear:hover { text-decoration: underline; }

.msel-options { max-height: 220px; overflow-y: auto; display: flex; flex-direction: column; gap: 1px; }
.msel-option {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 8px; border-radius: 8px; font-size: 14px; font-weight: 400; cursor: pointer;
}
.msel-option:hover { background: var(--chip); }
.msel-option input { width: 16px; height: 16px; flex: none; margin: 0; }
.msel-option .glyph { font-size: 16px; }

/* ------------------------------------------------------------------ items */

.items { list-style: none; margin: 0; padding: 0; }
.items li {
  display: flex; gap: 12px; align-items: flex-start;
  background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 12px; margin-bottom: 8px;
}
.items li.sel { border-color: var(--accent); }
.items input[type=checkbox] { margin-top: 3px; width: 18px; height: 18px; flex: none; }
.item-main { flex: 1; min-width: 0; }
.item-name { font-weight: 600; }
.item-note { color: var(--muted); font-size: 14px; margin-top: 2px; overflow-wrap: anywhere; }
.item-meta { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; }
.empty { text-align: center; color: var(--muted); padding: 40px 0; }

.selbar {
  position: sticky; bottom: 12px; z-index: 10;
  display: flex; gap: 8px; flex-wrap: wrap; align-items: center;
  background: var(--card); border: 1px solid var(--accent);
  border-radius: 14px; padding: 12px; margin-top: 12px;
  box-shadow: 0 6px 20px rgba(0,0,0,.12);
}
.selbar .count { font-weight: 600; margin-right: auto; }
.selbar select { width: auto; }
.confirm { border-color: var(--danger); background: var(--danger-bg); }
.confirm .names { margin: 8px 0; font-size: 14px; overflow-wrap: anywhere; }

/* ------------------------------------------------------------------ boxes */

.grid {
  display: grid; gap: 10px;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
}
.box-tile {
  position: relative;
  background: var(--card); border: 1px solid var(--line);
  border-radius: 14px; padding: 14px; text-align: center;
}
.box-tile.sel { border-color: var(--accent); }
.box-tile.default { border-style: dashed; }
.box-tile a { text-decoration: none; color: inherit; display: block; }
.box-tile .glyph { font-size: 42px; line-height: 1.2; }
.box-tile .name { font-weight: 600; margin-top: 4px; }
.box-tile .count { color: var(--muted); font-size: 13px; }
.box-tile .tags { display: flex; gap: 4px; flex-wrap: wrap; justify-content: center; margin-top: 8px; }
.box-tile .pick { position: absolute; top: 10px; left: 10px; width: 18px; height: 18px; }
.box-tile .desc {
  color: var(--muted); font-size: 12.5px; line-height: 1.3; margin-top: 4px;
  overflow-wrap: anywhere;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
/* `.box-tile a` above makes every link a block; this one is a small pill in the
   corner opposite the print checkbox. */
.box-tile a.tile-edit {
  position: absolute; top: 8px; right: 8px; display: inline-block;
  padding: 2px 9px; border-radius: 999px; background: var(--chip);
  color: var(--muted); font-size: 12px; font-weight: 600;
}
.box-tile a.tile-edit:hover { color: var(--ink); }

/* ------------------------------------------------------- add box preview */

.id-preview { text-align: center; padding: 8px 0 4px; }
.id-preview .glyph { font-size: 56px; line-height: 1.1; }
.id-preview .name { font-weight: 600; font-size: 18px; }

/* --------------------------------------------------------- edit box page */

.edit-ident { text-align: center; padding: 4px 0 16px; }
.edit-ident .glyph { font-size: 64px; line-height: 1.1; }
.edit-ident .name { font-weight: 600; font-size: 20px; }

/* ------------------------------------------------------------ print sheet */
/* Avery 15264 / 5164 / 5264 / 8164: 4in x 3-1/3in, 6 per US Letter sheet.
   These numbers are the template grid - do not "tidy" them. */

.printbar {
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  max-width: 8.5in; margin: 16px auto; padding: 14px 16px;
  background: var(--card); border: 1px solid var(--line); border-radius: 14px;
}
.printbar > div:first-child { margin-right: auto; }
.guidetoggle {
  display: flex; align-items: center; gap: 6px;
  font-size: 14px; color: var(--ink); font-weight: 500;
}
.guidetoggle input { width: 16px; height: 16px; }

.sheets { overflow-x: auto; padding-bottom: 24px; }

.sheet {
  box-sizing: border-box;
  width: 8.5in;
  height: 11in;
  margin: 0 auto 20px;
  padding: 0.5in 0.15625in;          /* top/bottom 1/2in, sides 5/32in */
  display: grid;
  grid-template-columns: 4in 4in;
  grid-template-rows: repeat(3, 3.3333in);
  column-gap: 0.1875in;              /* 3/16in between the two columns */
  row-gap: 0;                        /* rows butt against each other */
  background: #fff;
}

.label {
  box-sizing: border-box;
  width: 4in; height: 3.3333in;
  padding: 0.22in;
  display: flex; align-items: center; gap: 0.14in;
  overflow: hidden;
  color: #000;
}
.label .ident { flex: 1 1 auto; min-width: 0; text-align: center; }
.label .glyph { font-size: 1.15in; line-height: 1; }
.label .name {
  font-size: 0.22in; font-weight: 700; line-height: 1.15;
  margin-top: 0.07in; overflow-wrap: anywhere;
}
/* Optional. Five lines at this size fit in the ~1.4in left under the picture and
   name, so the QR column and the Avery grid geometry are never affected; anything
   longer is clipped rather than reflowed. */
.label .desc {
  font-size: 0.13in; line-height: 1.2; margin-top: 0.06in;
  overflow-wrap: anywhere;
  display: -webkit-box; -webkit-line-clamp: 5; -webkit-box-orient: vertical; overflow: hidden;
}
.label .qr { flex: 0 0 1.85in; text-align: center; }
.label .qr svg { width: 1.85in; height: 1.85in; display: block; }
.label .code {
  font-size: 0.1in; letter-spacing: .07em; color: #666; margin-top: 0.06in;
}

/* On screen the outlines show where the die cuts fall; in print they appear
   only if the user ticks "Print cut guides" for an alignment test. */
.sheet { outline: 1px solid var(--line); }
.label { outline: 1px dashed #ccd2da; outline-offset: -1px; }

@media print {
  @page { size: 8.5in 11in; margin: 0; }
  html, body { margin: 0; padding: 0; background: #fff; }
  body { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  /* <main> always wraps page content (layout()'s `chrome` option only toggles
     the header/footer), and its normal 16px/48px padding is otherwise unreset
     here - stacked on a .sheet that is exactly one page tall, that pushed every
     print into a second, near-blank page regardless of label count. */
  main { margin: 0; padding: 0; }
  .noprint { display: none !important; }
  .sheets { overflow: visible; padding: 0; }
  .sheet {
    margin: 0; outline: none;
    break-after: page; page-break-after: always;
  }
  .sheet:last-child { break-after: auto; page-break-after: auto; }
  .label { outline: none; }
  .guides .label { outline: 1px dashed #bbb; outline-offset: -1px; }
}
