/* All sizes taken 1:1 from Tag_Inserts_Template.pdf (units in pt).
   oat.ink's rules all live in @layer, so the unlayered rules below always win. */

@page { size: letter; margin: 0.4in; }

:root {
  color-scheme: light; /* oat's colours are light-dark(); this pins them to light */
  --ink: #000;
  --warn: #e60;
  --pattern-bg: #4a4a4a; /* the SVG pattern is black on transparent; this is what shows through */
  --hide: 3mm;   /* the 3D-printed case border hides this much at each covered edge */
  --win: 14mm;   /* what stays visible on the latch (20mm - 2 x 3mm): name / stats / blank live here */
  --frame: rgba(220, 40, 40, .45);
  --pattern-size: 40pt;   /* one 80x80 tile; centred so its cross lands mid-band */

  /* editor UI (toolbar + tag cards) */
  --ui-ink: #2b2623; --ui-muted: #4b4139; --ui-faint: #4b4139; --ui-accent: #4a3a30;
  --ui-line: #d9d2c9; --ui-hover: #f3eee8; --ui-focus: #8f4a0f; --ui-danger: #8c2d23;
}

body { max-width: 1280px; margin: 20px auto; padding-inline: 20px; }

/* ---------- Toolbar: header row + options strip ---------- */

#bar {
  margin-bottom: 16px; border: 1px solid #e2dbd2; border-radius: 12px; background: #fbf9f6;
  color: var(--ui-ink); font: .875rem/1.5 system-ui, sans-serif;
}
#bar button, #bar label { font: inherit; }
.bar-top, .bar-opts { display: flex; flex-wrap: wrap; align-items: center; gap: 12px 20px; padding: 12px 20px; }
.bar-opts { justify-content: space-between; column-gap: 14px; } /* one row on desktop: see also #bar .seg span */
#bar .seg span { padding: 0 10px; }
.bar-top { justify-content: space-between; border-bottom: 1px solid #eee8e0; }
.bar-title { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 12px; }
.bar-divider { align-self: center; width: 1px; height: 16px; background: #e2dbd2; }
.bar-link {
  min-height: 44px; display: inline-flex; align-items: center; gap: 6px; font-size: .8125rem; color: var(--ui-muted);
  text-decoration: underline; text-underline-offset: 3px; text-decoration-color: currentColor;
}
.bar-link:hover { color: var(--ui-ink); text-decoration-color: currentColor; }
.bar-title h1 { margin: 0; font-size: 1.125rem; font-weight: 600; }
#count { font-size: .8125rem; color: var(--ui-faint); }
.bar-actions, .opt { display: flex; align-items: center; gap: 10px; }
.bar-actions { gap: 8px; }
.opt-title { font-weight: 500; }

.btn {
  display: inline-flex; align-items: center; gap: 8px; min-height: 44px; padding: 0 16px;
  border: 1px solid var(--ui-line); border-radius: 8px; background: #fff; color: inherit;
  font-weight: 500; white-space: nowrap; cursor: pointer; box-shadow: none;
}
.btn svg { flex: none; }
.btn:hover { background: var(--ui-hover); }
.btn.primary { background: var(--ui-accent); border-color: var(--ui-accent); color: #fff; }
.btn.primary:hover { background: #372a22; }
.btn.ghost { border-color: transparent; background: transparent; color: var(--ui-muted); }
.btn.ghost:hover { background: #f3eee8; }
.btn.icon { padding: 0 10px; min-width: 44px; justify-content: center; }
.btn.toggle { position: relative; }
.btn.toggle input { position: absolute; inset: 0; width: 100%; height: 100%; margin: 0; opacity: 0; cursor: pointer; }
.btn.toggle .on, .btn.toggle:has(input:checked) .off { display: none; } /* icon shows the current state */
.btn.toggle:has(input:checked) .on { display: block; }
.btn.toggle:has(input:checked) { background: var(--ui-accent); border-color: var(--ui-accent); color: #fff; }
.btn.toggle:has(input:focus-visible) { outline: 2px solid var(--ui-focus); outline-offset: 2px; }
#bar :focus-visible, .tag-card :focus-visible, .tag-add:focus-visible, .skip:focus { outline: 3px solid var(--ui-focus); outline-offset: 2px; }
.tag:has(:focus-visible) { outline: 3px solid var(--ui-focus); outline-offset: 2px; } /* name text + stat boxes have no outline of their own */
.img:focus-visible { outline-offset: -3px; }
.skip { position: absolute; left: 8px; top: -100px; z-index: 10; padding: 12px 16px; background: #fff; color: var(--ui-ink); font: .875rem/1.5 system-ui, sans-serif; }
.skip:focus { top: 8px; }
.sr { position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
.pad { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.hint { max-width: 80ch; }

/* controls popover (native [popover]: opens from the Controls button, closes on outside click / Esc) */
#help, #privacy {
  max-width: calc(100vw - 40px); position: fixed; inset: 84px max(20px, calc(50vw - 640px + 20px)) auto auto; margin: 0; width: 300px; padding: 16px 18px;
  border: 1px solid #e2dbd2; border-radius: 12px; background: #fff; color: var(--ui-ink);
  box-shadow: 0 8px 24px rgba(43, 38, 35, .14); font: .875rem/1.5 system-ui, sans-serif;
}
/* Preferred: hang the popover's top-right corner off the action group's bottom-right corner (the header's right edge) */
.bar-actions { anchor-name: --controls; }
@supports (anchor-name: --a) {
  #help, #privacy {
    position-anchor: --controls; inset: auto; top: calc(anchor(bottom) + 8px); right: anchor(right);
    max-width: calc(100vw - 40px); position-try-fallbacks: --from-left;
  }
  /* when the button sits near the left edge (wrapped header), open rightwards instead */
  @position-try --from-left { inset: auto; top: calc(anchor(bottom) + 8px); left: anchor(left); right: auto; }
}
#privacy { width: 380px; }
#privacy p { margin: 0 0 10px; color: var(--ui-muted); }
#privacy p:last-child { margin: 0; }
#privacy .lnk { min-height: 0; padding: 0; font-size: inherit; color: var(--ui-ink); }
#help h2, #privacy h2 { margin: 0 0 10px; font-size: .875rem; font-weight: 600; }
#help dl { display: grid; grid-template-columns: auto 1fr; gap: 8px 14px; margin: 0; }
#help dt { font-weight: 500; }
#help dd { margin: 0; color: var(--ui-muted); }
#help kbd { padding: 1px 6px; border: 1px solid var(--ui-line); border-radius: 4px; background: var(--ui-hover); font: inherit; font-size: .75rem; }

/* segmented radio group */
.seg { display: inline-flex; gap: 2px; padding: 3px; border-radius: 10px; background: #ebe5dd; }
.seg label { position: relative; display: block; margin: 0; }
.seg input { position: absolute; inset: 0; width: 100%; height: 100%; margin: 0; opacity: 0; cursor: pointer; }
.seg span { display: flex; align-items: center; min-height: 44px; padding: 0 12px; border-radius: 7px; font-weight: 500; color: var(--ui-muted); }
.seg input:checked + span { background: var(--ui-accent); color: #fff; }
.seg input:disabled { cursor: not-allowed; }
.seg input:disabled + span { opacity: .45; }
.seg input:focus-visible + span { outline: 2px solid var(--ui-focus); outline-offset: 1px; }

/* colour chip: the native colour input is the swatch */
.chip {
  display: inline-flex; align-items: center; gap: 8px; min-height: 44px; margin: 0; padding: 0 10px 0 8px;
  border: 1px solid var(--ui-line); border-radius: 8px; background: #fff; font-weight: 500; cursor: pointer;
}
.chip input[type=color] { width: 22px; height: 22px; padding: 0; border: 1px solid #6f635a; border-radius: 5px; }
.chip input[type=color]::-webkit-color-swatch-wrapper { padding: 0; }
.chip input[type=color]::-webkit-color-swatch { border: 0; border-radius: 4px; }
.chip input[type=color]::-moz-color-swatch { border: 0; border-radius: 4px; }

.sw { display: inline-flex; align-items: center; gap: 10px; margin: 0; cursor: pointer; }
.sw input {
  appearance: none; -webkit-appearance: none; flex: none; position: relative;
  width: 38px; height: 22px; margin: 0; border: 0; border-radius: 11px; background: #6f635a;
  cursor: pointer; transition: background .15s;
}
.sw input::before { content: none; } /* oat draws its own thumb here and a mask-image tick on :checked::after; both are replaced below */
.sw input::after {
  mask: none; content: ""; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px;
  border-radius: 50%; background: #fff; transition: transform .15s;
}
.sw input:checked { background: var(--ui-accent); }
.sw input:checked::after { transform: translateX(16px); }

/* generic colour input, used by the per-tag tools */
input[type=color] {
  width: 24px; height: 24px; padding: 2px;
  border: 1px solid var(--border); border-radius: var(--radius-small);
  background: none; cursor: pointer;
}

/* Bundled so every OS lays the name out with identical metrics (Helvetica-compatible, SIL OFL) */
@font-face { font-family: "Liberation Sans"; font-weight: bold; src: url(../assets/LiberationSans-Bold.ttf); font-display: block; }

/* ---------- Sheet ---------- */

#sheet {
  display: flex; flex-wrap: wrap; align-items: stretch; gap: 16px; padding: 20px; counter-reset: tag;
  border: 1px solid #e2dbd2; border-radius: 14px; background: #fff;
}
#sheet.bg-custom .bar { background: var(--custom) center / cover no-repeat; }
#sheet.bg-white .bar { background: #fff; }
#sheet.bg-white .nm { color: var(--name-color, var(--ink)); }

.tag { position: relative; flex: none; width: 88.874pt; box-sizing: content-box; break-inside: avoid; }
.tag:has(input:invalid, .nm.empty) { outline: 2px dashed var(--warn); outline-offset: 2px; }

/* ---------- Tag card: editor UI around the tag artwork ---------- */

.tag-card {
  counter-increment: tag;
  display: flex; flex-direction: column; flex: none; width: 204px; overflow: hidden;
  border: 1px solid #e2dbd2; border-radius: 12px; background: #fbf9f6; color: var(--ui-ink);
  font: .875rem/1.5 system-ui, sans-serif;
}
.tag-card button, .tag-card label { font: inherit; }
.card-head {
  display: flex; align-items: center; justify-content: space-between;
  min-height: 52px; padding: 0 4px 0 14px; border-bottom: 1px solid #eee8e0;
}
.card-label::after { content: " " counter(tag); }
.lbl { margin: 0; font-size: .75rem; font-weight: 600; letter-spacing: .08em; text-transform: uppercase; color: var(--ui-faint); }
.card-actions { display: flex; gap: 2px; }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center; width: 44px; height: 44px; padding: 0;
  border: 0; border-radius: 8px; background: transparent; color: var(--ui-muted); cursor: pointer;
}
.icon-btn:hover { background: var(--ui-hover); color: var(--ui-ink); }
.icon-btn.danger { color: var(--ui-danger); }
.icon-btn.danger:hover { background: #f6ecea; color: var(--ui-danger); }

/* the tag artwork sits on a neutral stage; its own size is fixed below (.tag) */
.stage { display: flex; justify-content: center; padding: 16px 0; background: #f1ede7; }

.panel { display: flex; flex-direction: column; gap: 20px; padding: 16px; }
.group { display: flex; flex-direction: column; gap: 10px; }
.group.wide-gap { gap: 18px; }
.row { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 4px 8px; }
.inline { display: flex; flex: 1 1 100%; align-items: center; gap: 8px; } /* wrapped under its label: reset button + swatch fill the row */
.inline input[type=color].clr { flex: 1; width: auto; }
.pair { display: flex; gap: 8px; }
.btn-group { display: flex; }
.btn-group .btn { flex: 1; min-width: 0; justify-content: center; gap: 5px; padding: 0 6px; font-size: .8125rem; }
.btn-group .btn svg { display: none; } /* 204px card: text only, so "Add image" fits */
.btn-group .btn:first-child { border-radius: 8px 0 0 8px; }
.btn-group .btn:last-child { margin-left: -1px; border-radius: 0 8px 8px 0; }
.group.tall { gap: 16px; }
.tag-card .seg { display: flex; }
.tag-card .seg label { flex: 1; }
.tag-card .seg span { justify-content: center; min-height: 44px; padding: 0 4px; font-size: .8125rem; }
.rule { height: 0; margin: 0 -16px; border: 0; border-top: 1px solid #e2dbd2; }
.btn.wide { width: 100%; justify-content: center; font-size: .875rem; }
.pair .btn.wide { flex: 1; width: auto; }
.btn.danger { border-color: #ecd5d1; color: var(--ui-danger); }
.lnk { min-height: 44px; padding: 2px 6px; border: 0; background: transparent; color: var(--ui-muted); font-size: .8125rem; text-decoration: underline; text-underline-offset: 3px; cursor: pointer; }
.rng { width: 100%; height: 44px; margin: 0; accent-color: var(--ui-accent); }
input[type=color].clr { width: 48px; height: 44px; padding: 4px; border: 1px solid #6f635a; border-radius: 8px; background: #fff; cursor: pointer; }
.sw { min-height: 44px; }
/* Adjust-image accordion (native <details>, closed by default; oat supplies the chevron, we strip its card look).
   Full-bleed so the whole row is one big hover/click target; margins cancel the panel's padding and gap. */
.adjust { margin: -20px -16px -16px; border: 0; border-radius: 0; overflow: visible; }
.adjust > summary {
  min-height: 52px; padding: 0 16px; border: 0; cursor: pointer; font-size: .75rem;
  transition: background-color .15s;
}
.adjust > summary:hover { background: #f3eee8; }
.adjust > summary:active { background: #ece5dc; }
.adjust > summary:focus-visible { outline-offset: -2px; }
.adjust > summary::after { width: 18px; height: 18px; color: var(--ui-muted); }
.adjust[open] > summary { border-bottom: 1px solid #eee8e0; }
.adjust > .hint { margin: 14px 16px 0; font-size: .75rem; line-height: 1.5; color: var(--ui-muted); }
.adjust > .group { margin: 16px; }
.hint kbd { padding: 0 5px; border: 1px solid var(--ui-line); border-radius: 4px; background: var(--ui-hover); font: inherit; font-size: .6875rem; }


/* Skeleton card at the end of the sheet: stretches to the tallest card in its row */
.tag-add {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px;
  flex: none; width: 204px; min-height: 200px; margin: 0; padding: 0; box-sizing: border-box;
  border: 2px dashed #6f635a; border-radius: 12px; background: transparent; color: var(--ui-faint);
  font-size: .875rem; font-weight: 500; cursor: pointer;
}
.tag-add span { font-size: 2rem; line-height: 1; }
.tag-add:hover, .tag-add:focus-visible { border-color: var(--ui-accent); color: var(--ui-accent); background: #f6f2ec; }

/* Borders are drawn as overlays (::after) on top of content that runs underneath them.
   A real border next to the image/pattern leaves an anti-aliased white seam in print/PDF. */
.img, .name { position: relative; }
.img::after, .name::after { content: ""; position: absolute; inset: 0; border: 1pt solid var(--ink); pointer-events: none; }
.img::after { border-bottom: 0; }
/* the image/latch seam is the fold: a hairline dotted line (black + white dots, so it shows on the dark pattern and on a white latch alike) instead of a solid border.
   Its own 0.5pt-tall box: the same gradient as a sized background on .name::after turned the whole latch white in Firefox's print output. */
.name::after { border-top: 0; }
.name .bar::before {
  content: ""; position: absolute; left: 0; right: 0; top: 0; height: .5pt;
  background: repeating-linear-gradient(90deg, var(--ink) 0 1pt, #fff 1pt 2pt);
}

/* ---------- Image area ---------- */

.img {
  display: block; height: 172.913pt; /* template path length; its 1pt top border is drawn inside this */
  cursor: pointer; overflow: hidden; background: #fff;
  print-color-adjust: exact; -webkit-print-color-adjust: exact;
}
.img.over { background: #def; }
.img input { display: none; }
.img span { position: absolute; inset: 0; display: grid; place-items: center; align-content: center; gap: 6px; color: #595959; font-size: .75rem; text-align: center; }
.img span small { font-size: .6875rem; opacity: .8; }
.img:has(img[src]) { cursor: grab; touch-action: none; }
.img:has(img[src]):active { cursor: grabbing; }
.img:has(img[src]) span { display: none; }

/* full uncropped image, centred; only .img's overflow:hidden masks it */
.img img { position: absolute; left: 50%; top: 50%; max-width: none; display: block; pointer-events: none; user-select: none; }
.img img:not([src]) { display: none; }

/* ---------- Name band ---------- */

.name { height: calc(var(--hide) * 2 + var(--win)); overflow: hidden; display: flex; flex-direction: column; } /* latch = 3mm hidden + 14mm visible + 3mm hidden = 20mm */
.name .bar { box-sizing: border-box; padding-block: var(--hide); } /* .nm fills only the visible window */
.bar {
  flex: 1; min-height: 0; overflow: hidden;
  background: var(--pattern-bg) url(../assets/pattern.svg) center / var(--pattern-size) repeat;
  print-color-adjust: exact; -webkit-print-color-adjust: exact;
}
.nm {
  /* PDF text matrix 16 0 0 20 = 20pt Helvetica Bold at 80% width; fitText() only shrinks below 20pt */
  width: 125%; margin-left: -12.5%; transform: scaleX(.8);
  height: 100%; box-sizing: border-box; padding: 1pt 5pt; overflow: hidden; /* keeps text off the overlaid border */
  display: flex; align-items: center; justify-content: center;
  color: var(--name-color, #fff); font: bold 20pt/1 "Liberation Sans", Helvetica, Arial, sans-serif;
  position: relative; top: .018em; /* centres the cap height in the line box (Liberation Sans sits slightly high) */
  text-align: center; outline: none; overflow-wrap: normal; word-break: normal; cursor: text;
}
.nm.empty::before { content: "Name"; opacity: .85; }

/* ---------- Mirror: copy of the name band at the bottom of the image area, upside down like the image ---------- */

.mirror {
  box-sizing: content-box; /* oat's border-box would eat the 1pt divider out of the height */
  display: none; position: absolute; left: 0; right: 0; bottom: -1pt; height: calc(var(--win) + 1pt); /* = the latch's visible window; touches the latch, its lowest 3mm sit under the case border (see .nm padding below). The extra 1pt runs under the latch's border: at some zooms a flush edge leaves a sliver of image between them (clipped by .img, text keeps its place) */
  border-bottom: 1pt solid var(--ink); transform: rotate(180deg); pointer-events: none;
}
.tag.mirrored .mirror { display: block; }
.tag.slim .mirror { height: calc(15pt + var(--hide) + 1pt); }
.tag.slim .mirror .nm { white-space: nowrap; }
.mirror .nm { padding-block: 0; top: -.018em; }
.tag:not(.mirror-top) .mirror .nm { padding-top: calc(var(--hide) + 1pt); } /* rotated 180°: this pad is the box's bottom, the hidden strip, so the text is centred in what shows */ /* the mirror is rotated 180°, so its shift flips */
/* name at the top of the box (= below the image, since the image is printed upside down): same band, pinned under the top outline (1pt) with its divider on the lower edge */
.tag.mirror-top .mirror { top: 0; bottom: auto; height: calc(var(--win) + 1pt); border-bottom: 0; border-top: 1pt solid var(--ink); }
.tag.slim.mirror-top .mirror { height: 16pt; }
/* the band starts at the very edge, under the outline (drawn on top by .img::after): at some zooms/print
   resolutions a band that starts 1pt lower leaves a sliver of image between them. The extra 1pt is hidden, so text keeps its place. */
.tag.mirror-top .mirror .nm { padding-bottom: 1pt; }

/* ---------- Stats variant: pattern sliver with a one-line name, AC/HP/PP below ---------- */

.st { display: none; }
.tag.stats .name .bar { flex: 0 0 calc(var(--hide) + 15pt); padding-bottom: 0; } /* hidden 3mm + a 15pt name row */
.tag.stats .name .nm { white-space: nowrap; }
/* the divider lines run to the bottom, but the text sits above the hidden strip at the bottom (see .st label) */
.tag.stats .name .nm { padding-bottom: 0; }
.tag.stats .st { flex: 1; display: grid; grid-template-columns: repeat(3, 1fr); padding-inline: 2pt; border-top: 1pt solid var(--ink); }
.st label {
  position: relative; display: flex; align-items: center; justify-content: center; padding: 5pt 0 var(--hide); box-sizing: border-box; /* value centred below the label, in the visible part; dividers still run into the hidden strip */
  color: var(--stat-color, var(--ink)); font: bold 6pt/14pt "Liberation Sans", Helvetica, Arial, sans-serif;
}
.st label span { position: absolute; top: 2pt; left: 3pt; line-height: 1; } /* AC / HP / PP: top-left of its box */
.st label + label { border-left: 1pt solid var(--ink); }
.tag.blank .st label { display: none; } /* blank info: the strip stays, its boxes go */
.st input {
  flex: 1; width: 0; min-width: 0; height: 14pt; margin: 0; padding: 0; border: 0; border-radius: 0; outline: none;
  box-shadow: none !important; color: inherit; background: transparent;
  text-align: center; font: bold 10.5pt/1 "Liberation Sans", Helvetica, Arial, sans-serif;
}

/* ---------- Toast (first-use tip, undo) ---------- */

#toast {
  position: fixed; inset: auto auto 24px 50%; transform: translateX(-50%); margin: 0;
  display: none; align-items: center; gap: 16px; width: max-content; max-width: min(520px, calc(100vw - 40px));
  padding: 14px 16px 14px 18px; border: 0; border-radius: 12px;
  background: var(--ui-accent); color: #fff; box-shadow: 0 8px 24px rgba(43, 38, 35, .28);
  font: .875rem/1.5 system-ui, sans-serif;
}
#toast:popover-open { display: flex; }
#toast p { margin: 0; }
#toast kbd { padding: 0 6px; border: 1px solid rgba(255, 255, 255, .45); border-radius: 4px; font: inherit; font-size: .75rem; }
#toast .btn { flex: none; min-height: 44px; color: var(--ui-ink); }

/* ---------- Frame overlay (toolbar toggle): the three strips the case hides ---------- */

.tag::before, .img::before, .name::before { content: none; position: absolute; z-index: 3; pointer-events: none; }
body:has(#frame:checked) :is(.tag, .img, .name)::before { content: ""; }
.tag::before { inset: 0; box-shadow: inset 0 0 0 .5mm var(--frame); } /* outer 0.5mm all round */
.img::before { left: 0; right: 0; bottom: 0; height: var(--hide); background: var(--frame); }
.name::before {
  inset: 0;
  background: linear-gradient(var(--frame), var(--frame)) top / 100% var(--hide) no-repeat,
              linear-gradient(var(--frame), var(--frame)) bottom / 100% var(--hide) no-repeat;
}

/* ---------- Print ---------- */

@media print {
  body { max-width: none; margin: 0; padding: 0; background: none; } /* print uses the full page width */
  #sheet { padding: 0; gap: 6pt; border: 0; }
  .tag-card, .stage { display: contents; } /* only the tag artwork prints, laid out directly in the sheet */
  #bar, #toast, .tag-add, .card-head, .panel, .img span { display: none !important; }
  .nm.empty::before { content: none; }
  .tag::before, .img::before, .name::before { content: none !important; }
  .tag:has(input:invalid, .nm.empty) { outline: none; }
}

/* ---------- Accessibility preferences ---------- */

@media (prefers-reduced-motion: reduce) {
  *, ::before, ::after { transition: none !important; animation: none !important; }
}
@media (forced-colors: active) {
  .seg input:checked + span, .sw input:checked, .btn.toggle:has(input:checked) { outline: 2px solid Highlight; }
}
