/* ===================================================================
   USA Dodgeball Rules PWA — Scrollable Rulebook, mobile-first
   =================================================================== */

:root {
  --bg-deep: #081226;
  --bg-mid: #0f223f;
  --bg-light: #1b3a66;
  --paper: #f8f9fc;
  --paper-alt: #eef2f8;
  --ink: #132238;
  --ink-soft: #4e6073;
  --line: #d5deeb;
  --accent: #e31837;
  --accent-ink: #7a1023;
  --accent-bg: rgba(227, 24, 55, 0.16);
  --card: #ffffff;
  --focus-ring: 0 0 0 2.5px var(--accent);
  --transition: 180ms ease-out;
  --header-h: 120px; /* approximate, recalculated by JS when needed */
  --toc-w: 240px;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
  scroll-padding-top: calc(var(--header-h) + 16px);
}

html,
body {
  margin: 0;
  min-height: 100%;
}

body {
  font-family: "Source Sans 3", "Segoe UI", sans-serif;
  color: var(--ink);
  background: linear-gradient(140deg, var(--bg-deep), var(--bg-mid) 42%, var(--bg-light));
}

h1, h2, h3, h4 {
  font-family: "Barlow Condensed", "Franklin Gothic Medium", sans-serif;
  margin: 0;
}

/* ---- Skip link ---- */
.skip-link {
  position: absolute;
  left: -9999px;
  top: 8px;
  z-index: 9999;
  background: var(--accent);
  color: var(--accent-ink);
  padding: 8px 16px;
  border-radius: 8px;
  font-weight: 700;
  text-decoration: none;
}
.skip-link:focus {
  left: 16px;
}

/* ---- Focus outlines ---- */
:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
  border-radius: 4px;
}

/* ==================================================================
   Sticky header
   ================================================================== */
.site-header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--paper);
  border-bottom: 1px solid var(--line);
  box-shadow: 0 2px 12px rgba(8, 18, 38, 0.10);
  padding: 10px 14px 0;
}

.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 8px;
}

.header-brand {
  display: flex;
  align-items: baseline;
  gap: 8px;
  flex-wrap: wrap;
}

.eyebrow {
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ink-soft);
  font-size: 0.7rem;
  font-weight: 700;
  margin: 0;
}

h1 {
  font-size: 1.35rem;
  line-height: 1;
}

.header-controls {
  display: flex;
  align-items: center;
  gap: 10px;
}

.header-actions {
  display: flex;
  gap: 6px;
}

/* ---- Ball toggle ---- */
.ball-toggle {
  display: flex;
  background: var(--paper-alt);
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
}

.toggle-btn {
  border: none;
  background: transparent;
  padding: 5px 12px;
  font-size: 0.8rem;
  font-weight: 700;
  font-family: inherit;
  color: var(--ink-soft);
  cursor: pointer;
  transition: background var(--transition), color var(--transition);
}

.toggle-btn:hover {
  background: rgba(0, 0, 0, 0.04);
}

.toggle-btn.active,
.toggle-btn[aria-pressed="true"] {
  background: var(--bg-mid);
  color: #fff;
}

/* ---- Install / update buttons ---- */
.btn {
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
  font-weight: 700;
  font-family: inherit;
  transition: background var(--transition);
}

.btn-sm {
  padding: 5px 10px;
  font-size: 0.78rem;
  background: var(--paper-alt);
  color: var(--ink);
}
.btn-sm:hover {
  background: #dfe6f0;
}

/* ---- Search bar ---- */
.search-bar {
  position: relative;
  padding-bottom: 10px;
}

.search-wrap {
  position: relative;
  display: flex;
  align-items: center;
}

.search-icon {
  position: absolute;
  left: 12px;
  color: var(--ink-soft);
  pointer-events: none;
}

.search-wrap input {
  width: 100%;
  border: 2px solid var(--line);
  border-radius: 10px;
  padding: 10px 44px 10px 38px;
  font: inherit;
  font-size: 0.95rem;
  color: var(--ink);
  background: #fff;
  transition: border-color var(--transition), box-shadow var(--transition);
}

.search-wrap input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(227, 24, 55, 0.30);
}

.search-wrap input::placeholder {
  color: #8ba0b8;
}

.search-kbd {
  position: absolute;
  right: 12px;
  background: #e8edf5;
  color: #5a6e82;
  border: 1px solid #cbd5e1;
  border-radius: 5px;
  padding: 2px 7px;
  font-family: "SFMono-Regular", Consolas, monospace;
  font-size: 0.7rem;
  pointer-events: none;
}

/* ---- Search dropdown ---- */
.search-dropdown {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 8px 28px rgba(8, 18, 38, 0.18);
  max-height: 360px;
  overflow-y: auto;
  list-style: none;
  margin: 4px 0 0;
  padding: 6px;
  z-index: 110;
}

.search-dropdown .dd-item {
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  transition: background var(--transition);
}

.search-dropdown .dd-item:hover,
.search-dropdown .dd-item.focused {
  background: var(--paper-alt);
}

.search-dropdown .dd-item[aria-selected="true"] {
  background: var(--accent-bg);
}

.search-dropdown .dd-title {
  font-weight: 700;
  font-size: 0.92rem;
  margin: 0;
}

.search-dropdown .dd-snippet {
  font-size: 0.82rem;
  color: var(--ink-soft);
  margin: 2px 0 0;
  line-height: 1.3;
}

.search-dropdown mark {
  background: rgba(227, 24, 55, 0.28);
  color: inherit;
  border-radius: 2px;
  padding: 0 1px;
}

/* ---- Match navigation bar ---- */
.match-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 12px;
  background: var(--accent-bg);
  border-top: 1px solid rgba(227, 24, 55, 0.3);
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--accent-ink);
}

.match-nav {
  display: flex;
  gap: 4px;
}

.match-nav button {
  border: 1px solid rgba(0, 0, 0, 0.12);
  border-radius: 6px;
  background: #fff;
  cursor: pointer;
  padding: 3px 10px;
  font-size: 0.82rem;
  font-family: inherit;
  font-weight: 700;
  color: var(--ink);
  transition: background var(--transition);
}
.match-nav button:hover:not(:disabled) {
  background: var(--paper-alt);
}
.match-nav button:disabled {
  opacity: 0.4;
  cursor: default;
}

.match-clear {
  margin-left: auto;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 1rem;
  color: var(--ink-soft);
  padding: 2px 6px;
}
.match-clear:hover {
  color: var(--ink);
}

/* ==================================================================
   Layout: TOC + Rulebook
   ================================================================== */
.layout {
  display: flex;
  max-width: 1100px;
  margin: 0 auto;
  padding: 0 12px;
  gap: 0;
}

/* ---- Hamburger button (mobile only) ---- */
.toc-hamburger {
  display: flex;
  align-items: center;
  justify-content: center;
  background: none;
  border: none;
  color: var(--ink);
  padding: 6px;
  margin: -6px 0 -6px -6px;
  border-radius: 6px;
  cursor: pointer;
  transition: background var(--transition);
  flex-shrink: 0;
}
.toc-hamburger:hover {
  background: var(--paper-alt);
}

/* ---- Table of Contents ---- */
.toc {
  /* Mobile: slide-in drawer from left */
  position: fixed;
  top: 0;
  left: 0;
  z-index: 200;
  width: min(300px, 85vw);
  height: 100vh;
  height: 100dvh;
  background: var(--bg-mid);
  transform: translateX(-100%);
  transition: transform 0.25s ease;
  overflow-y: auto;
  scrollbar-width: none;
  padding: 20px 12px 32px;
}
.toc::-webkit-scrollbar { display: none; }
.toc.open {
  transform: translateX(0);
}

/* Backdrop */
.toc-backdrop {
  position: fixed;
  inset: 0;
  z-index: 190;
  background: rgba(0, 0, 0, 0.45);
  opacity: 0;
  transition: opacity 0.25s ease;
  pointer-events: none;
}
.toc-backdrop.visible {
  opacity: 1;
  pointer-events: auto;
}

.toc-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.toc-list .toc-section {
  margin-bottom: 1px;
}

/* Section-level link */
.toc-list .toc-link {
  display: block;
  text-decoration: none;
  border-radius: 5px;
  transition: background var(--transition), color var(--transition);
}

.toc-list .toc-link--section {
  padding: 6px 10px;
  font-size: 0.78rem;
  font-weight: 700;
  color: rgba(255, 255, 255, 0.9);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.toc-list .toc-link--section:hover {
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
}
.toc-list .toc-link--section.active {
  color: var(--accent);
}

/* Subsection list — collapsed by default, shown on hover / when section is expanded */
.toc-sublist {
  list-style: none;
  margin: 0;
  padding: 0 0 0 10px;
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.25s ease;
  scrollbar-width: none; /* Firefox */
}
.toc-sublist::-webkit-scrollbar {
  display: none; /* Chrome / Safari / Edge */
}
.toc-section:hover > .toc-sublist,
.toc-section.expanded > .toc-sublist {
  max-height: 800px; /* large enough for any section */
}

/* Subsection-level link */
.toc-list .toc-link--sub {
  padding: 3px 10px;
  font-size: 0.74rem;
  font-weight: 500;
  color: rgba(255, 255, 255, 0.55);
}
.toc-list .toc-link--sub:hover {
  background: rgba(255, 255, 255, 0.08);
  color: rgba(255, 255, 255, 0.9);
}
.toc-list .toc-link--sub.active {
  color: var(--accent);
  font-weight: 600;
}

/* Always expand sublists when TOC drawer is open */
.toc.open .toc-sublist {
  max-height: 800px;
}

/* ---- Main content ---- */
.rulebook-main {
  flex: 1;
  min-width: 0;
  padding: 16px 0 32px;
}

/* ==================================================================
   Rulebook document styles
   ================================================================== */
.rulebook {
  background: var(--card);
  border: 1px solid rgba(255, 255, 255, 0.55);
  border-radius: 14px;
  box-shadow: 0 14px 36px rgba(8, 18, 38, 0.14);
  padding: 20px 16px 28px;
}

/* ---- Sections ---- */
.rb-section {
  margin-bottom: 28px;
}

.rb-section-heading {
  font-size: 1.45rem;
  color: var(--bg-mid);
  padding-bottom: 8px;
  border-bottom: 3px solid var(--accent);
  margin-bottom: 16px;
  scroll-margin-top: calc(var(--header-h) + 16px);
}

/* ---- Subsections ---- */
.rb-subsection {
  margin-bottom: 18px;
  margin-left: 4px;
}

.rb-subsection-heading {
  font-size: 1.1rem;
  color: var(--ink);
  padding-bottom: 4px;
  border-bottom: 1px solid var(--line);
  margin-bottom: 10px;
  scroll-margin-top: calc(var(--header-h) + 16px);
}

/* ---- Individual rules ---- */
.rb-rule {
  margin-bottom: 14px;
  padding: 12px 14px;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 10px;
  transition: border-color 200ms, box-shadow 200ms, opacity 200ms;
  scroll-margin-top: calc(var(--header-h) + 16px);
}

.rb-rule:hover {
  border-color: #b0c4de;
}

.rb-rule-title {
  font-size: 1rem;
  font-weight: 700;
  margin-bottom: 6px;
  color: var(--ink);
}

.rb-rule-body {
  font-size: 0.93rem;
  line-height: 1.55;
  color: var(--ink);
}

.rb-rule-body ul {
  margin: 4px 0 0;
  padding-left: 20px;
}

.rb-rule-body li {
  margin-bottom: 4px;
}

.rb-rule-body li::marker {
  color: var(--ink-soft);
}

/* Ball type indicator pill */
.rb-ball-pill {
  display: inline-block;
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 2px 8px;
  border-radius: 999px;
  background: #e6edf8;
  color: #20406a;
  margin-left: 8px;
  vertical-align: middle;
}

.rb-ball-pill.cloth { background: #fde3e8; color: #8f1028; }
.rb-ball-pill.foam  { background: #d4f0e8; color: #1a5c3a; }

/* Related rules */
.rb-related {
  margin-top: 8px;
  font-size: 0.82rem;
  color: var(--ink-soft);
}

.rb-related a {
  color: var(--bg-light);
  text-decoration: underline;
  text-decoration-color: rgba(27, 58, 102, 0.3);
  cursor: pointer;
  transition: color var(--transition);
}
.rb-related a:hover {
  color: var(--bg-mid);
}

/* ---- Search highlight on rules ---- */
.rb-rule.highlight-match {
  border-color: var(--accent);
  box-shadow: inset 0 0 0 1px var(--accent), 0 0 8px rgba(227, 24, 55, 0.25);
}

.rb-rule.highlight-active {
  border-color: var(--accent);
  box-shadow: inset 0 0 0 2px var(--accent), 0 0 16px rgba(227, 24, 55, 0.35);
  background: var(--accent-bg);
}

.rb-rule mark {
  background: rgba(227, 24, 55, 0.28);
  color: inherit;
  border-radius: 2px;
  padding: 0 1px;
}

/* ---- Ball filter: hide non-matching rules ---- */
.rb-rule.ball-hidden {
  display: none;
}

/* ---- Section hidden when all rules inside are hidden ---- */
.rb-section.section-hidden,
.rb-subsection.subsection-hidden {
  display: none;
}

/* ==================================================================
   Footer
   ================================================================== */
.footer {
  margin-top: 16px;
  padding: 10px 14px;
  font-size: 0.82rem;
  color: var(--ink-soft);
  text-align: center;
  background: var(--card);
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.55);
}

/* ==================================================================
   Utility
   ================================================================== */
.hidden {
  display: none !important;
}

/* ==================================================================
   Tablet (≥ 640px)
   ================================================================== */
@media (min-width: 640px) {
  h1 { font-size: 1.6rem; }

  .site-header { padding: 12px 20px 0; }

  .search-wrap input {
    font-size: 1.05rem;
    padding: 12px 48px 12px 42px;
  }

  .rulebook {
    padding: 28px 24px 36px;
  }

  .rb-section-heading { font-size: 1.6rem; }
  .rb-subsection-heading { font-size: 1.2rem; }
}

/* ==================================================================
   Desktop (≥ 1024px)
   ================================================================== */
@media (min-width: 1024px) {
  .layout {
    max-width: 1140px;
    padding: 0 24px;
    gap: 24px;
  }

  h1 { font-size: 1.8rem; }

  .site-header { padding: 14px 24px 0; }

  .search-wrap input {
    font-size: 1.1rem;
    padding: 14px 52px 14px 46px;
  }

  /* Hide hamburger on desktop */
  .toc-hamburger { display: none; }
  .toc-backdrop { display: none; }

  /* TOC: always visible sidebar */
  .toc {
    position: sticky;
    top: calc(var(--header-h) + 12px);
    align-self: flex-start;
    width: var(--toc-w);
    flex-shrink: 0;
    height: auto;
    max-height: calc(100vh - var(--header-h) - 24px);
    overflow-y: auto;
    scrollbar-width: none;
    padding: 12px 4px 12px 0;
    transform: none;
    transition: none;
    background: transparent;
  }
  .toc::-webkit-scrollbar {
    display: none;
  }

  .rulebook {
    padding: 36px 32px 44px;
  }

  .rb-section-heading { font-size: 1.75rem; }
  .rb-subsection-heading { font-size: 1.3rem; }
}
