@font-face {
  font-family: "Oswald";
  src: url("fonts/oswald-latin-500-normal.woff2") format("woff2");
  font-weight: 500;
  font-display: swap;
}
@font-face {
  font-family: "Oswald";
  src: url("fonts/oswald-latin-700-normal.woff2") format("woff2");
  font-weight: 700;
  font-display: swap;
}
@font-face {
  font-family: "Inter";
  src: url("fonts/inter-latin-400-normal.woff2") format("woff2");
  font-weight: 400;
  font-display: swap;
}
@font-face {
  font-family: "Inter";
  src: url("fonts/inter-latin-600-normal.woff2") format("woff2");
  font-weight: 600;
  font-display: swap;
}
@font-face {
  font-family: "Inter";
  src: url("fonts/inter-latin-700-normal.woff2") format("woff2");
  font-weight: 700;
  font-display: swap;
}

:root {
  --bg: #f3f1ec;
  --surface: #ffffff;
  --text: #1c1b1a;
  --muted: #66625c;
  --line: #dcd8d0;
  --accent: #d7263d;
  --accent-ink: #ffffff;
  --focus: #1f6feb;
  --radius: 12px;

  /* Nummerlappens färger, sätts från app.js */
  --band: #d7263d;
  --band-ink: #ffffff;
  --ink: #111111;
  --paper: #ffffff;

  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161514;
    --surface: #211f1d;
    --text: #f1eee9;
    --muted: #a8a29a;
    --line: #3a3733;
    --focus: #6ea8ff;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 16px;
  line-height: 1.5;
}

/* ---------- Sidhuvud ---------- */
.site-header {
  max-width: 1280px;
  margin: 0 auto;
  padding: 24px 16px 8px;
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 4px 20px;
}
.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: inherit;
  text-decoration: none;
}
.brand-mark {
  display: inline-grid;
  place-items: center;
  width: 44px;
  height: 32px;
  border-radius: 4px;
  background: #fff;
  color: #111;
  border-top: 8px solid var(--accent);
  font: 700 18px/1 "Oswald", sans-serif;
  box-shadow: 0 1px 3px rgb(0 0 0 / .25);
  transform: rotate(-4deg);
}
.brand-name {
  font: 700 28px/1 "Oswald", sans-serif;
  letter-spacing: .01em;
}
.brand-name span { color: var(--accent); }
.tagline { margin: 0; color: var(--muted); }

/* ---------- Layout ---------- */
.layout {
  max-width: 1280px;
  margin: 0 auto;
  padding: 16px;
  display: grid;
  grid-template-columns: minmax(300px, 380px) 1fr;
  gap: 24px;
  align-items: start;
}
@media (max-width: 860px) {
  .layout { grid-template-columns: 1fr; }
  .preview { order: -1; }
}

/* ---------- Formulär ---------- */
.editor {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 8px 16px 16px;
}
fieldset {
  border: 0;
  border-bottom: 1px solid var(--line);
  margin: 0;
  padding: 12px 0 16px;
  display: grid;
  gap: 12px;
}
legend {
  float: left;
  width: 100%;
  padding: 0;
  margin-bottom: 4px;
  font: 700 13px/1.2 "Inter", sans-serif;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--muted);
}
legend + * { clear: both; }

.field { display: grid; gap: 4px; margin: 0; }
.field > span { font-weight: 600; font-size: 14px; }
.field small { font-weight: 400; color: var(--muted); }

input[type="text"], input:not([type]), select {
  width: 100%;
  min-width: 0;
  font: inherit;
  color: inherit;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 9px 11px;
}
input:focus-visible, select:focus-visible, .btn:focus-visible, .file-btn:focus-within, .swatch:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}
#number {
  font: 700 22px/1.1 "Oswald", sans-serif;
  letter-spacing: .04em;
}

.row { display: flex; gap: 10px; align-items: end; flex-wrap: wrap; }
.grow { flex: 1 1 140px; }
.narrow { flex: 0 0 96px; }

.field.color { grid-template-columns: auto 1fr; align-items: center; gap: 8px; flex: 1 1 120px; }
.field.color > span { order: 2; font-weight: 400; }
input[type="color"] {
  width: 44px;
  height: 36px;
  padding: 2px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  cursor: pointer;
}

.check { display: flex; gap: 8px; align-items: center; font-size: 15px; cursor: pointer; }
.check input { width: 18px; height: 18px; accent-color: var(--accent); }

.swatches { display: flex; flex-wrap: wrap; gap: 8px; }
.swatch {
  width: 40px;
  height: 30px;
  border-radius: 6px;
  border: 1px solid var(--line);
  background: linear-gradient(to bottom, var(--sw-band) 0 40%, var(--sw-paper) 40%);
  cursor: pointer;
  padding: 0;
  position: relative;
}
.swatch::after {
  content: "7";
  position: absolute;
  inset: 40% 0 0;
  display: grid;
  place-items: center;
  font: 700 12px/1 "Oswald", sans-serif;
  color: var(--sw-ink);
}
.swatch[aria-checked="true"] { box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px var(--text); }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font: 600 15px/1 "Inter", sans-serif;
  border-radius: 999px;
  padding: 11px 18px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.file-btn { position: relative; }
.file-btn input {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

.actions { display: flex; flex-wrap: wrap; gap: 10px; padding-top: 16px; }
.hint { margin: 0; font-size: 13px; color: var(--muted); min-height: 1em; }
.hint.center { text-align: center; margin-top: 12px; }

/* ---------- Förhandsvisning ---------- */
.preview { position: sticky; top: 16px; }
@media (max-width: 860px) { .preview { position: static; } }

.paper {
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 10px 30px rgb(0 0 0 / .14), 0 1px 3px rgb(0 0 0 / .12);
  aspect-ratio: 297 / 210;
  padding: calc(100% * 10 / 297);
  display: grid;
}

/* ---------- Nummerlappen ----------
   Alla mått i cqw (procent av lappens bredd) så att förhandsvisning och
   utskrift blir proportionellt identiska. Lappen är 277 × 190 mm. */
.bib {
  container-type: inline-size;
  width: 100%;
  aspect-ratio: 277 / 190;
  align-self: center;
  -webkit-print-color-adjust: exact;
  print-color-adjust: exact;
}
/* cqw gäller bara inuti containern, därför bär det inre elementet allt utseende. */
.bib-inner {
  position: relative;
  height: 100%;
  background: var(--paper);
  color: var(--ink);
  border: 1px solid rgb(0 0 0 / .18);
  border-radius: 1.6cqw;
  overflow: hidden;
  display: grid;
  grid-template-rows: 16cqw minmax(0, 1fr) 14cqw;
}

.fit {
  display: block;
  width: 100%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: clip;
}

.bib-top {
  background: var(--band);
  color: var(--band-ink);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  text-align: center;
  padding: 0 9cqw;
}
.bib-race {
  font: 700 7.2cqw/1.05 "Oswald", sans-serif;
  text-transform: uppercase;
  letter-spacing: .03em;
}
.bib-subtitle {
  font: 500 2.6cqw/1.2 "Oswald", sans-serif;
  text-transform: uppercase;
  letter-spacing: .14em;
  opacity: .92;
}
.bib-subtitle:empty { display: none; }

.bib-middle {
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  text-align: center;
  padding: 0 5cqw;
  min-height: 0;
}
.bib-number {
  font: 700 34cqw/0.92 "Oswald", sans-serif;
  letter-spacing: -.01em;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
}
.bib-runner {
  font: 700 5cqw/1.1 "Oswald", sans-serif;
  text-transform: uppercase;
  letter-spacing: .08em;
  margin-top: .6cqw;
}
.bib-runner:empty { display: none; }
.bib-group {
  position: absolute;
  top: 2.2cqw;
  left: 3cqw;
  min-width: 7cqw;
  padding: .5cqw 1.4cqw;
  border-radius: .8cqw;
  background: var(--band);
  color: var(--band-ink);
  font: 700 2.8cqw/1.1 "Oswald", sans-serif;
  text-transform: uppercase;
  text-align: center;
}
.bib-group:empty { display: none; }

.bib-bottom {
  border-top: .7cqw solid var(--band);
  display: flex;
  align-items: center;
  gap: 2.5cqw;
  padding: 1.6cqw 9cqw 1.6cqw;
  min-height: 0;
}
.bib-logo {
  height: 100%;
  max-width: 26cqw;
  object-fit: contain;
  flex: 0 0 auto;
}
.bib-sponsor {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  text-align: center;
}
.bib-sponsor-name {
  font: 700 5cqw/1.1 "Oswald", sans-serif;
  text-transform: uppercase;
  letter-spacing: .04em;
}
.bib-slogan {
  font: 500 2.2cqw/1.3 "Oswald", sans-serif;
  letter-spacing: .06em;
  opacity: .8;
}
.bib-sponsor-name:empty, .bib-slogan:empty { display: none; }
.bib-chip {
  flex: 0 0 auto;
  width: 14cqw;
  height: 6cqw;
  color: var(--ink);
}

.pin {
  position: absolute;
  width: 3.2cqw;
  height: 3.2cqw;
  border-radius: 50%;
  border: .3cqw dashed rgb(0 0 0 / .4);
  background: rgb(255 255 255 / .55);
  z-index: 2;
}
.pin.tl { top: 2.4cqw; left: 2.4cqw; }
.pin.tr { top: 2.4cqw; right: 2.4cqw; }
.pin.bl { bottom: 2.4cqw; left: 2.4cqw; }
.pin.br { bottom: 2.4cqw; right: 2.4cqw; }
.bib.no-pins .pin { display: none; }
.bib.no-chip .bib-chip { display: none; }

/* ---------- Sidfot ---------- */
.site-footer {
  max-width: 1280px;
  margin: 0 auto;
  padding: 8px 16px 40px;
  color: var(--muted);
  font-size: 14px;
}
.site-footer p { margin: 0 0 6px; max-width: 70ch; }
.site-footer a { color: var(--text); }

.tips { margin: 24px 0 20px; }
.tips h2 {
  margin: 0 0 10px;
  font: 700 13px/1.2 "Inter", sans-serif;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--muted);
}
.tips-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 12px;
}
.tip {
  display: grid;
  gap: 4px;
  padding: 14px 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-left: 4px solid var(--accent);
  border-radius: var(--radius);
  color: var(--text);
  text-decoration: none;
}
.tip:hover { border-color: var(--muted); border-left-color: var(--accent); }
.tip:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.tip-name { font: 700 18px/1.2 "Oswald", sans-serif; letter-spacing: .01em; }
.tip-name::after { content: " →"; color: var(--accent); }
.tip-text { font-size: 14px; color: var(--muted); }
.credit { margin-top: 12px; }

/* ---------- Utskrift: bara lappen, A4 liggande ---------- */
@page {
  size: A4 landscape;
  margin: 0;
}
@media print {
  html, body {
    width: 297mm;
    height: 210mm;
    margin: 0;
    padding: 0;
    background: #fff;
    color-scheme: light;
  }
  .site-header, .site-footer, .editor, .hint { display: none !important; }
  .layout, .preview {
    display: block;
    padding: 0;
    margin: 0;
    max-width: none;
    position: static;
  }
  .paper {
    width: 297mm;
    height: 209mm; /* en hårsmån under A4 så att ingen tom extrasida skapas */
    padding: 10mm;
    box-shadow: none;
    border-radius: 0;
    aspect-ratio: auto;
    display: block;
    overflow: hidden;
    break-inside: avoid;
  }
  .bib {
    width: 277mm;
    height: 189mm;
    aspect-ratio: auto;
  }
  .bib-inner { border-color: #bbb; }
}
