:root {
  color-scheme: dark light;
  --bg: #121214;
  --bg-2: #1a1a1f;
  --bg-3: #22222a;
  --fg: #e8e8ea;
  --muted: #9a9aa3;
  --border: #2a2a32;
  --border-strong: #3a3a45;
  --accent: #c84b4b;

  /* Sanction colors — readable on dark bg, distinct hue per severity */
  --s-caution: #5b9dd9;
  --s-warning: #e0a93b;
  --s-gameloss: #d97a3b;
  --s-dq: #c84b4b;
  --s-dqnp: #8b1e3f;
  --s-other: #6b6b75;
  /* Accent colors for the four card sections (VEKN structure: Definition /
   * Example(s) / Philosophy / Penalty). Picked from hue families that the
   * sanction palette does not occupy (teal, magenta/pink, purple, green) so
   * no section can ever read as a Caution/Warning/Game-Loss/DQ badge. */
  --sec-def: #2aa1b3;
  --sec-example: #d086a8;
  --sec-philosophy: #8a5cb0;
  --sec-penalty: #3da664;

  /* Mark highlight (search hits): same hue as caution but tuned for body
   * legibility — yellow gives the strongest scan-ability on dark bg. */
  --mark-bg: rgba(224, 169, 59, 0.32);
  --mark-fg: #fff7e0;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f7f7f8;
    --bg-2: #ffffff;
    --bg-3: #ececf0;
    --fg: #1a1a1f;
    --muted: #5b5b66;
    --border: #d9d9e0;
    --border-strong: #b6b6c0;
    --s-caution: #1f6cb0;
    --s-warning: #b87a07;
    --s-gameloss: #b7531a;
    --s-dq: #a8333a;
    --s-dqnp: #5a0f29;
    --s-other: #7a7a85;
    /* Light-mode section accents: same hues as dark mode, slightly deeper
     * so they read on a white background while staying clear of the
     * sanction palette. */
    --sec-def: #1d8090;
    --sec-example: #b25881;
    --sec-philosophy: #6c4498;
    --sec-penalty: #1f8146;
    --mark-bg: rgba(184, 122, 7, 0.28);
    --mark-fg: #2a1a00;
  }
}

* {
  box-sizing: border-box;
}

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

html,
body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--fg);
  font:
    16px/1.5 system-ui,
    -apple-system,
    "Segoe UI",
    Roboto,
    sans-serif;
  -webkit-font-smoothing: antialiased;
}

a {
  color: var(--s-caution);
}

:focus-visible {
  outline: 2px solid var(--s-caution);
  outline-offset: 2px;
  border-radius: 2px;
}

.skip {
  position: absolute;
  left: -9999px;
}
.skip:focus {
  left: 0.5rem;
  top: 0.5rem;
  background: var(--bg-2);
  padding: 0.5rem 0.75rem;
  border-radius: 0.25rem;
  z-index: 100;
}

/* ---------- Topbar ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  padding-top: env(safe-area-inset-top);
  transition: padding 0.15s ease;
}

.topbar header {
  padding: 0.5rem 1rem 0.25rem;
  max-width: 60rem;
  margin: 0 auto;
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.5rem 1rem;
  flex-wrap: wrap;
  transition:
    padding 0.15s ease,
    margin 0.15s ease;
}

.topbar h1 {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 700;
  letter-spacing: 0.01em;
  flex: 1 1 auto;
  transition: font-size 0.15s ease;
}

.topbar .sub {
  margin: 0;
  color: var(--muted);
  font-size: 0.75rem;
  flex: 0 0 auto;
  transition:
    opacity 0.15s ease,
    max-height 0.15s ease;
}

.topbar .sub a {
  color: var(--muted);
  text-decoration: underline;
  text-decoration-color: var(--border-strong);
  text-underline-offset: 2px;
}

.topbar .sub a:hover,
.topbar .sub a:focus-visible {
  color: var(--s-caution);
  text-decoration-color: currentColor;
}

/* On scroll, condense the header on small screens: the sub line collapses
 * and the title shrinks slightly so the search/chip strip stays close to
 * the viewport top. Skipped on desktop where vertical space isn't tight. */
@media (max-width: 719px) {
  .is-scrolled .topbar header {
    padding-top: 0.25rem;
    padding-bottom: 0;
  }
  .is-scrolled .topbar h1 {
    font-size: 0.95rem;
  }
  .is-scrolled .topbar .sub {
    max-height: 0;
    opacity: 0;
    overflow: hidden;
    margin: 0;
  }
}

/* ---------- Controls ---------- */

.controls {
  padding: 0.25rem 1rem 0.5rem;
  max-width: 60rem;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.controls input[type="search"] {
  width: 100%;
  font: inherit;
  font-size: 16px;
  padding: 0.625rem 0.75rem;
  background: var(--bg-2);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  min-height: 44px;
}

.controls input[type="search"]:focus {
  outline: none;
  border-color: var(--s-caution);
}

/* ---------- Meta row ---------- */

.meta-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  font-size: 0.8rem;
  color: var(--muted);
}

.reset {
  font: inherit;
  font-size: 0.8rem;
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 0.375rem;
  padding: 0.5rem 0.75rem;
  min-height: 44px;
  cursor: pointer;
}

.reset:hover {
  color: var(--fg);
  border-color: var(--border-strong);
}

/* ---------- Main / list ---------- */

main {
  padding: 0.5rem 1rem max(1rem, env(safe-area-inset-bottom));
  max-width: 60rem;
  margin: 0 auto;
}

.list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding-top: 0.5rem;
  scroll-margin-top: 200px;
}

/* ---------- Category accordion ---------- */

.category {
  background: var(--bg-3);
  border: 1px solid var(--border-strong);
  border-radius: 0.5rem;
  overflow: hidden;
}

.category-summary {
  list-style: none;
  cursor: pointer;
  padding: 0.85rem 1rem;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  min-height: 48px;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  background: linear-gradient(to right, color-mix(in srgb, var(--accent) 8%, transparent), transparent 60%);
}

.category-summary::-webkit-details-marker {
  display: none;
}

.category-summary::before {
  content: "";
  display: inline-block;
  width: 0;
  height: 0;
  border-top: 5px solid transparent;
  border-bottom: 5px solid transparent;
  border-left: 6px solid var(--muted);
  transition: transform 0.15s ease;
  flex: 0 0 auto;
}

.category[open] > .category-summary::before {
  transform: rotate(90deg);
}

.category-summary:hover {
  background: var(--bg-2);
}

.category-title {
  flex: 1 1 auto;
  font-size: 1rem;
  font-weight: 700;
  letter-spacing: 0.01em;
  color: var(--fg);
  text-transform: uppercase;
}

.category-count {
  flex: 0 0 auto;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.05);
  padding: 0.15rem 0.55rem;
  border-radius: 999px;
  min-width: 1.75rem;
  text-align: center;
}

@media (prefers-color-scheme: light) {
  .category-count {
    background: rgba(0, 0, 0, 0.06);
  }
}

.category-body {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 0.5rem;
  background: var(--bg);
}

/* ---------- Item card ---------- */

.item {
  position: relative;
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 0.375rem;
  -webkit-tap-highlight-color: transparent;
  scroll-margin-top: 200px;
}

.item:target {
  border-color: var(--s-caution);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--s-caution) 40%, transparent);
}

.item-summary {
  list-style: none;
  cursor: pointer;
  padding: 0.7rem 0.875rem;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 0.45rem;
  min-height: 44px;
  user-select: none;
}

.item-summary::-webkit-details-marker {
  display: none;
}

.item-summary:hover {
  background: var(--bg-3);
}

.item-title {
  font-size: 0.95rem;
  font-weight: 600;
  line-height: 1.4;
  color: var(--fg);
}

.item-badges {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
  align-items: center;
  position: relative;
}

/* Sanction-to-color mapping driven by data-* attributes on the item card.
 * Static rules in CSS keep the page CSP-friendly: no inline style attribute
 * is needed in the HTML. Single-sanction items only set data-s1; multi
 * items set both data-s1 and data-s2 (the bar shows a gradient between
 * the two endpoints). */
.item[data-s1="caution"] {
  --edge-from: var(--s-caution);
}
.item[data-s1="warning"] {
  --edge-from: var(--s-warning);
}
.item[data-s1="game-loss"] {
  --edge-from: var(--s-gameloss);
}
.item[data-s1="dq"] {
  --edge-from: var(--s-dq);
}
.item[data-s1="dqnp"] {
  --edge-from: var(--s-dqnp);
}
.item[data-s1="other"] {
  --edge-from: var(--s-other);
}
.item[data-s2="caution"] {
  --edge-to: var(--s-caution);
}
.item[data-s2="warning"] {
  --edge-to: var(--s-warning);
}
.item[data-s2="game-loss"] {
  --edge-to: var(--s-gameloss);
}
.item[data-s2="dq"] {
  --edge-to: var(--s-dq);
}
.item[data-s2="dqnp"] {
  --edge-to: var(--s-dqnp);
}

/* Colored bar that sits flush against the sanction name area. Single
 * sanctions paint a solid color from --edge-from. Multi cards (those with
 * data-s2) paint a gradient between --edge-from and --edge-to.
 *
 * On mobile (badges below the title, full width) the bar is a thin
 * horizontal strip across the top so it visually leads into the badges.
 * On desktop (badges to the right of the title) it becomes a thin vertical
 * rail to the left of the badges. The gradient axis follows the bar's
 * long axis at each breakpoint. */
.badges-edge {
  display: block;
  border-radius: 999px;
  flex: 0 0 auto;
  width: 100%;
  height: 4px;
  background: var(--edge-from, transparent);
}

.item[data-s2] .badges-edge {
  background: linear-gradient(to right, var(--edge-from, var(--s-other)), var(--edge-to, var(--s-other)));
}

.badge {
  display: inline-block;
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  padding: 0.22rem 0.55rem;
  border-radius: 0.25rem;
  white-space: nowrap;
  background: rgba(255, 255, 255, 0.05);
  color: var(--muted);
  border: 1px solid transparent;
}

@media (prefers-color-scheme: light) {
  .badge {
    background: rgba(0, 0, 0, 0.04);
  }
}

.badge-sep {
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 600;
  padding: 0 0.05rem;
}

.badge[data-sanction="CAUTION"] {
  color: var(--s-caution);
  border-color: color-mix(in srgb, var(--s-caution) 40%, transparent);
  background: color-mix(in srgb, var(--s-caution) 10%, transparent);
}
.badge[data-sanction="WARNING"] {
  color: var(--s-warning);
  border-color: color-mix(in srgb, var(--s-warning) 40%, transparent);
  background: color-mix(in srgb, var(--s-warning) 10%, transparent);
}
.badge[data-sanction="GAME LOSS"] {
  color: var(--s-gameloss);
  border-color: color-mix(in srgb, var(--s-gameloss) 40%, transparent);
  background: color-mix(in srgb, var(--s-gameloss) 10%, transparent);
}
.badge[data-sanction="DISQUALIFICATION"] {
  color: var(--s-dq);
  border-color: color-mix(in srgb, var(--s-dq) 40%, transparent);
  background: color-mix(in srgb, var(--s-dq) 10%, transparent);
}
.badge[data-sanction="DISQUALIFICATION WITHOUT PRIZE"] {
  color: #e8a0b3;
  border-color: color-mix(in srgb, var(--s-dqnp) 60%, transparent);
  background: color-mix(in srgb, var(--s-dqnp) 25%, transparent);
}

@media (prefers-color-scheme: light) {
  .badge[data-sanction="DISQUALIFICATION WITHOUT PRIZE"] {
    color: #5a0f29;
  }
}

.badge-tbd {
  font-style: italic;
  font-weight: 600;
  letter-spacing: 0;
  color: var(--muted);
  border-color: var(--border);
  background: transparent;
}

/* ---------- Item body (expanded) ---------- */

.item-body {
  padding: 0 0.875rem 0.875rem;
}

.item-refs {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
  margin: 0.7rem 0 0;
  padding-top: 0.6rem;
  border-top: 1px dashed var(--border);
}

.item-ref {
  display: inline-block;
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.01em;
  color: var(--s-caution);
  text-decoration: none;
  padding: 0.2rem 0.5rem;
  border: 1px solid color-mix(in srgb, var(--s-caution) 40%, transparent);
  background: color-mix(in srgb, var(--s-caution) 8%, transparent);
  border-radius: 0.25rem;
  white-space: nowrap;
}

a.item-ref::after {
  content: " ↗";
  opacity: 0.6;
  font-size: 0.85em;
}

a.item-ref:hover,
a.item-ref:focus-visible {
  background: color-mix(in srgb, var(--s-caution) 18%, transparent);
}

.item-notes {
  font-size: 0.9rem;
  color: var(--fg);
  line-height: 1.55;
  white-space: pre-line;
}

.item-notes.muted {
  color: var(--muted);
  font-style: italic;
}

/* Labeled sections inside the expanded card. Each section is colour-coded
 * with its own accent so the eye groups text by purpose at a glance:
 * Descrizione = blue-violet (definition), Esempio = sand (illustrative),
 * Correzione = green (corrective action). Accent colors live outside the
 * sanction palette so they never read as a CAUTION/WARNING/etc. badge. */
.item-section {
  margin: 0 0 0.6rem;
  border-left: 3px solid var(--border-strong);
  border-radius: 4px;
  padding: 0.35rem 0.6rem;
  background: color-mix(in srgb, var(--bg-2) 92%, transparent);
}

.item-section:last-of-type {
  margin-bottom: 0;
}

.item-section-label {
  margin: 0 0 0.2rem;
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

/* Per-section accent: tinted left border + label color + faint background
 * tint so the four sections are visually distinct without shouting. */
.item-section-def {
  border-left-color: color-mix(in srgb, var(--sec-def) 65%, transparent);
  background: color-mix(in srgb, var(--sec-def) 7%, var(--bg-2));
}
.item-section-def .item-section-label {
  color: color-mix(in srgb, var(--sec-def) 75%, var(--muted));
}

.item-section-example {
  border-left-color: color-mix(in srgb, var(--sec-example) 65%, transparent);
  background: color-mix(in srgb, var(--sec-example) 7%, var(--bg-2));
}
.item-section-example .item-section-label {
  color: color-mix(in srgb, var(--sec-example) 75%, var(--muted));
}
/* Example body is italic to signal "this is illustrative" without competing
 * with the rule wording. */
.item-section-example .item-section-body {
  font-style: italic;
  color: color-mix(in srgb, var(--fg) 92%, var(--muted));
}

.item-section-philosophy {
  border-left-color: color-mix(in srgb, var(--sec-philosophy) 65%, transparent);
  background: color-mix(in srgb, var(--sec-philosophy) 7%, var(--bg-2));
}
.item-section-philosophy .item-section-label {
  color: color-mix(in srgb, var(--sec-philosophy) 75%, var(--muted));
}

.item-section-penalty {
  border-left-color: color-mix(in srgb, var(--sec-penalty) 65%, transparent);
  background: color-mix(in srgb, var(--sec-penalty) 7%, var(--bg-2));
}
.item-section-penalty .item-section-label {
  color: color-mix(in srgb, var(--sec-penalty) 75%, var(--muted));
}

/* Penalità is always rendered. When the entry has no specific procedure,
 * the body is a "no extra action required" message — drop the accent so the
 * empty state doesn't read as if it requires attention. */
.item-section-penalty.item-section-penalty-empty {
  border-left-color: var(--border-strong);
  background: color-mix(in srgb, var(--bg-2) 92%, transparent);
}
.item-section-penalty.item-section-penalty-empty .item-section-label {
  color: var(--muted);
}

.item-section-body {
  font-size: 0.9rem;
  color: var(--fg);
  line-height: 1.55;
  white-space: pre-line;
}

.item-notes mark,
.item-title mark,
.item-section-body mark {
  background: var(--mark-bg);
  color: var(--mark-fg);
  padding: 0.05em 0.15em;
  border-radius: 0.15em;
}

.item-actions {
  margin-top: 0.6rem;
  display: flex;
  justify-content: flex-end;
}

.item-share {
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--muted);
  text-decoration: none;
  padding: 0.4rem 0.65rem;
  border: 1px solid var(--border);
  border-radius: 0.25rem;
  background: transparent;
  min-height: 36px;
  display: inline-flex;
  align-items: center;
}

.item-share:hover,
.item-share:focus-visible {
  color: var(--fg);
  border-color: var(--border-strong);
}

.item-share.is-flashed {
  color: var(--s-caution);
  border-color: var(--s-caution);
}

/* ---------- Empty / loading / footer ---------- */

.empty,
.loading {
  padding: 2rem 1rem;
  text-align: center;
  color: var(--muted);
}

.loading.is-error {
  color: var(--fg);
  font-weight: 600;
}

.empty-actions {
  margin-top: 0.75rem;
}

.reset-empty {
  font: inherit;
  font-size: 0.85rem;
  background: var(--bg-2);
  color: var(--fg);
  border: 1px solid var(--border-strong);
  border-radius: 0.375rem;
  padding: 0.5rem 0.85rem;
  min-height: 44px;
  cursor: pointer;
}

.app-footer {
  max-width: 60rem;
  margin: 1.5rem auto 0;
  padding: 0.5rem 1rem max(1rem, env(safe-area-inset-bottom));
  text-align: center;
  font-size: 0.72rem;
  color: var(--muted);
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  align-items: center;
}

.updated[hidden],
.sw-update[hidden] {
  display: none;
}

.sw-update-btn {
  font: inherit;
  font-size: 0.78rem;
  font-weight: 600;
  background: color-mix(in srgb, var(--s-caution) 14%, var(--bg-2));
  color: var(--fg);
  border: 1px solid color-mix(in srgb, var(--s-caution) 50%, var(--border));
  border-radius: 0.375rem;
  padding: 0.55rem 0.95rem;
  min-height: 40px;
  cursor: pointer;
}

.sw-update-btn:hover,
.sw-update-btn:focus-visible {
  background: color-mix(in srgb, var(--s-caution) 24%, var(--bg-2));
}

/* ---------- Desktop ---------- */

@media (min-width: 720px) {
  .topbar h1 {
    font-size: 1.35rem;
  }
  .topbar .sub {
    font-size: 0.85rem;
  }
  .item-summary {
    flex-direction: row;
    align-items: center;
    gap: 0.75rem;
  }
  .item-title {
    flex: 1 1 auto;
  }
  .item-badges {
    flex: 0 0 auto;
  }
  /* Bar becomes a vertical rail flush against the sanction badges; the
   * gradient axis flips to top→bottom to match the rail's long axis. */
  .badges-edge {
    width: 4px;
    height: auto;
    align-self: stretch;
    min-height: 1.4rem;
  }
  .item.item-multi .badges-edge {
    background: linear-gradient(to bottom, var(--edge-from, var(--s-other)), var(--edge-to, var(--s-other)));
  }
}

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

/* ---------- Print ---------- *
 * The vademecum is a real reference document — judges sometimes want a
 * paper copy on the table. Optimize for high-contrast B/W output: light
 * background, force-open every accordion, drop the sticky chrome and
 * interaction affordances. */
@media print {
  :root {
    color-scheme: light;
    --bg: #ffffff;
    --bg-2: #ffffff;
    --bg-3: #f0f0f0;
    --fg: #000000;
    --muted: #444444;
    --border: #999999;
    --border-strong: #666666;
  }
  .topbar {
    position: static;
    border-bottom: 2px solid #000;
  }
  .controls,
  .meta-row,
  .reset,
  .item-actions {
    display: none !important;
  }
  details.category,
  details.item {
    page-break-inside: avoid;
  }
  details.category > .category-body,
  details.item > .item-body {
    display: block !important;
  }
  details.category > .category-summary::before,
  details.item > .item-summary::before {
    display: none;
  }
  details[open],
  details {
    display: block !important;
  }
  details.item {
    border: 1px solid #999;
    margin-bottom: 0.4rem;
    background: #fff !important;
    color: #000 !important;
  }
  .badges-edge {
    background: #000 !important;
  }
  .badge,
  .item-ref {
    background: #fff !important;
    color: #000 !important;
    border: 1px solid #000 !important;
  }
  a.item-ref::after {
    content: "";
  }
  .app-footer {
    border-top: 1px solid #999;
    margin-top: 1rem;
    padding-top: 0.5rem;
  }
  mark {
    background: transparent !important;
    color: inherit !important;
    text-decoration: underline;
  }
}
