:root{
  --muted: #86d8d4;
  --bg: #f1fdfb;
  --text: #020a09;
  --primary: #3bddca;
  --secondary: #7fc7e9;
  --accent: #6398e4;
  --container-max: 90vw;
}

/* Reset / base */
* { box-sizing: border-box; }
html,body { height: 100%; }
body{
  margin:0;
  font-family: Inter, system-ui, -apple-system, "Segoe UI", Roboto, Arial;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  line-height: 1.35;
}

/* Header */
header{
  background: radial-gradient(circle at center, var(--primary), var(--secondary));
  color: var(--text);
  padding: 25px 12px;
}
.header-inner{
  display:flex;
  justify-content:space-between;
  align-items:center;
  gap:12px;
  max-width: var(--container-max);
  margin:0 auto;
  width:100%;
}
.header-left h1 { margin:0; font-size:1.1rem; }
.header-left .sub { margin:4px 0 0; font-size:0.85rem; opacity:0.95;}
.btn{ border:0; background:transparent; color:var(--text); cursor:pointer; font-weight:600; padding:6px 8px; border-radius:6px; }
.btn.small{ font-size:0.86rem; padding:4px 6px; }

/* Rules panel */
.rules-panel{
  max-width: var(--container-max);
  margin:8px auto 0;
  padding:12px;
  background: var(--bg);
  border-radius:10px;
  color:var(--text);
  box-shadow:0 10px 30px rgba(12,16,34,0.12);
  position:relative;
  z-index:20;
}
.rules-header{ display:flex; justify-content:space-between; align-items:center; margin-bottom:8px; }

/* Main container */
.container{
  width: min(94vw, var(--container-max));
  max-width: var(--container-max);
  margin:14px auto;
  padding:0 16px 60px;
}

/* Run details banner (responsive) */
.run-details{
  display:flex;
  flex-direction: row;
  align-items:center;
  justify-content:space-between;
  gap:16px;
  width:100%;
  background: radial-gradient(circle at center, var(--primary), var(--secondary));
  border-radius:12px;
  padding:12px;
  box-shadow:0 6px 18px rgba(127,199,233,0.25);
  margin-bottom:14px;
  position:relative;
  z-index:3;
  flex-wrap: wrap;
}

/* Left column expands; right column hugs content */
.run-left { display:flex; align-items:center; gap:12px; flex:1 1 60%; min-width:0; justify-content:flex-start; }
.run-right { display:flex; align-items:center; gap:12px; flex:0 0 auto; min-width:0; justify-content:flex-end; }

.person-info { display:flex; flex-direction:column; gap:4px; min-width:0; overflow:hidden; }
.person-info.reverse { align-items:flex-end; text-align:right; }
@media (max-width:900px) { .person-info.reverse { align-items:flex-start; text-align:left; } }

.person-sprite{
  width: clamp(48px,14vw,120px);
  height:auto;
  object-fit:contain;
  padding:4px;
  flex:0 0 auto;
  border-radius:6px;
}
.person-name{
  font-weight:700;
  font-size: clamp(1rem,2.6vw,1.6rem);
  white-space:nowrap;
  overflow:hidden;
  text-overflow:ellipsis;
}
.person-sub, .rival-sub {
  font-size: clamp(0.85rem,1.6vw,1rem);
  color:#052423;
  white-space:nowrap;
  overflow:hidden;
  text-overflow:ellipsis;
}

/* Controls */
.controls{
  display:flex;
  gap:12px;
  align-items:center;
  flex-wrap:wrap;
  margin:14px 0 22px;
}
.controls label{ font-size:0.9rem; color:var(--text); }
.controls select, .controls input{
  margin-left:8px;
  padding:6px 8px;
  border-radius:8px;
  color: var(--text);
  border:1px solid #d6d6df;
  background: white;
  font-size:0.95rem;
}
#jump-episode{
  margin-left:8px;
  padding:6px 10px;
  border-radius:8px;
  border:1px solid #2f364f;
  background:var(--accent);
  color:var(--text);
  cursor:pointer;
}

/* Timeline & center line */
.timeline{
  position:relative;
  padding:30px 0;
  margin:0 auto;
  width:100%;
  min-height:200px;
}
.timeline::before{
  content:"";
  position:absolute;
  left:50%;
  transform:translateX(-50%);
  top:0;
  bottom:0;
  width:4px;
  background:var(--secondary);
  border-radius:4px;
  box-shadow:0 0 0 2px rgba(127,199,233,0.06);
  z-index:1;
}

/* Episode banner */
.episode-section { margin-bottom:12px; position:relative; z-index:4; }
.episode-banner{
  clear:both;
  display:flex;
  align-items:center;
  justify-content:center;
  gap:8px;
  width:100%;
  text-align:center;
  margin:22px 0;
  padding:10px 14px;
  background: radial-gradient(circle at center, var(--primary), var(--secondary));
  color:var(--text);
  border-radius:10px;
  font-weight:600;
  box-shadow:0 6px 18px rgba(127,199,233,0.12);
  position:relative;
  z-index:4;
}
.episode-banner .controls {
  position: absolute;
  right: 12px;
  top: 25%;
  transform: translateY(-50%);
  display: flex;
  gap: 8px;
  align-items: center; /* keep icons aligned on their own cross axis */
  height: auto;        /* ensure it doesn’t stretch oddly */
}
.episode-banner:focus { outline:3px solid rgba(74,99,214,0.18); outline-offset:2px; }
@media (max-width:900px) { .episode-banner { justify-content:flex-start; padding-left:18px; } }

/* Event card */
.event {
  width:46%;
  padding:12px 14px;
  position:relative;
  margin-bottom:22px;
  border-radius:10px;
  background: var(--muted);
  box-shadow:0 6px 18px rgba(16,18,36,0.06);
  display:block;
  box-sizing:border-box;
  z-index:6; /* ensure above episode banner */
}

/* left/right placement */
.event.left { float:left; clear:both; transform: translateX(20px); margin-right:4%; }
.event.right { float:right; clear:both; transform: translateX(-20px); margin-left:4%; }

/* Event header & pseudo-element ribbon (sticking out) */
.event-header {
  display:block;
  width:100%;
  box-sizing:border-box;
  padding:8px 12px;
  margin-bottom:8px;
  background: var(--secondary);
  border-radius:8px;
  text-align:center;
  font-weight:700;
  font-size:1.05rem;
  color:var(--text);
  line-height:1.2;
  position:relative; /* pseudo-element positioned relative to header */
  z-index:7; /* above event body and timeline line */
}

/* header ribbon via data attribute (appears slightly above header) */
.event-header[data-ribbon]::before {
  content: attr(data-ribbon);
  position: absolute;
  left: 8px;
  top: -12px; /* stick out above header */
  padding:6px 10px;
  background: linear-gradient(90deg,#ffd86b,#ffb74d);
  color: #2b1700;
  font-weight:800;
  font-size:0.78rem;
  border-radius:8px;
  box-shadow:0 6px 18px rgba(255,170,20,0.08);
  transform: rotate(-6deg);
  z-index:9999; /* topmost so it sits above banner */
  white-space:nowrap;
  pointer-events:none;
}

/* Event body */
.event-body {
  display:flex;
  flex-direction:row;
  gap:12px;
  align-items:flex-start;
  width:100%;
  box-sizing:border-box;
  position:relative;
  z-index:6;
}
.event-body .visual { flex:0 0 auto; display:flex; align-items:center; justify-content:center; }
.event-body .item-body { flex:1 1 auto; min-width:0; }

/* Sprites & badges */
.sprite { width:96px; height:96px; object-fit:contain; flex-shrink:0; padding:6px; border-radius:6px; }
.badge-icon { width:72px; height:72px; object-fit:contain; flex-shrink:0; padding:6px; border-radius:8px; }

/* Text content */
.item-body { flex:1; min-width:0; }
.primary-action{ margin:0 0 6px; font-weight:700; font-size:1rem; }
.species-nick{ margin:0 0 6px; color:#222; font-size:0.98rem; }
.obtained-line{ font-size:0.9rem; color:var(--text); margin-bottom:6px; }
.item-notes{ font-size:0.9rem; color:#444 }

/* evolution row */
.evolution-row{ display:flex; align-items:center; gap:10px; margin-bottom:8px; }
.evolution-row .sprite{ width:72px; height:72px; }
.evolve-arrow{ font-size:1.25rem; color:var(--text); margin:0 6px; }

/* Episode contents (collapsible) */
/* When expanded allow visual overflow so ribbon sticking out isn't clipped */
.episode-contents {
  overflow: visible;
  transition: max-height 300ms ease, opacity 250ms ease;
  opacity:1;
  max-height:2000px;
  margin-top:12px;
}
/* When collapsed hide overflow (spoiler-safe) */
.episode-contents.collapsed {
  overflow: hidden;
  max-height:0 !important;
  opacity:0;
  margin-top:0;
}

/* run end banner */
.run-end-banner {
  clear:both;
  display:block;
  width:100%;
  text-align:center;
  margin:22px 0;
  padding:12px 16px;
  background: linear-gradient(90deg,#b00000,#ff6b6b);
  color:white;
  border-radius:10px;
  font-weight:800;
  box-shadow:0 8px 20px rgba(176,0,0,0.12);
  position:relative;
  z-index:8;
}

/* message panel */
.message{ margin-top:18px; padding:12px 14px; background:#fff7e6; border:1px solid #ffe4b5; border-radius:8px; color:#7a5a00; position:relative; z-index:3; }

/* back to top */
.back-to-top{
  position:fixed; right:22px; bottom:22px; width:46px; height:46px; border-radius:10px;
  background:var(--accent); color:var(--text); border:0; cursor:pointer; font-size:20px;
  display:none; align-items:center; justify-content:center; box-shadow:0 10px 30px rgba(16,18,36,0.16);
  z-index:40;
}

/* special / fainted visuals */
.event.fainted .sprite, .event.fainted .badge-icon { filter: grayscale(100%) contrast(85%); opacity:0.72; }
.event.fainted .event-header { background: linear-gradient(180deg,#fff0f0,#ffe6e6); color:#7a0011; border:1px solid #ffcccc; }
.event.fainted { box-shadow: 0 8px 24px rgba(160,16,16,0.04); }

.event.special .visual { position:relative; }
@media (prefers-reduced-motion: reduce) {
  .event.special .visual::after { animation:none; opacity:0.9; }
}

/* mobile adjustments */
@media (max-width:900px){
  .container{ width:94vw; }
  .event{ width:100%; float:none; transform:none; margin-left:0; margin-right:0; padding:10px; }
  .timeline::before{ left:20px; transform:none; width:3px; }
  .episode-banner { justify-content:flex-start; padding-left:18px; }
  .run-left, .run-right { flex: 1 1 100%; justify-content:flex-start; }
  .person-sprite { width: clamp(56px, 20vw, 96px); }
}

/* small helpers */
.hidden { display:none !important; }


/* ===============================
   UI polish: expand/collapse buttons, permalink icon-only, clearfix footer fix
   =============================== */

/* 1) Make sure floated events don't collapse their container:
   Put this clearfix on the timeline (so footer and other content flow correctly). */
.timeline::after {
  content: "";
  display: table;   /* creates a new block formatting context for floats */
  clear: both;
}

/* Also add a general clearfix for container just in case */
.container::after {
  content: "";
  display: table;
  clear: both;
}

/* 2) Small control buttons (Expand/Collapse) — match the rest of the UI */
.small-control {
  margin-left:8px;
  padding:6px 10px;
  border-radius:8px;
  border:1px solid #2f364f;
  background:var(--accent);
  color:var(--text);
  cursor:pointer;
}

/* hover/focus */
.small-control:hover, .small-control:focus {
  transform: translateY(-1px);
  box-shadow: 0 12px 28px rgba(47,54,79,0.10);
  outline: none;
}

/* 3) Permalink icon — icon-only button in episode banner */
.episode-banner .permalink {
  background: transparent;  /* no box or bordered rectangle */
  border: 0;
  padding: 6px;
  margin: 0;
  font-size: 1.05rem;
  line-height: 1;
  color: rgba(255,255,255,0.95); /* icon color appropriate for banner */
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  transition: background-color 140ms ease, transform 120ms ease;
}

/* small hover halo so the icon still has affordance */
.episode-banner .permalink:hover,
.episode-banner .permalink:focus {
  background: rgba(255,255,255,0.08);
  transform: translateY(-1px);
  outline: none;
}

/* Use a darker icon when banner uses light background (if needed) */
.episode-banner .permalink[aria-pressed="true"] {
  color: rgba(0,0,0,0.85);
}

/* Make .permalink visually minimal when not inside banner (fallback) */
.permalink { background: transparent; border: 0; padding: 4px; cursor: pointer; }

/* small tooltip-friendly focus ring for accessibility */
.episode-banner .permalink:focus { box-shadow: 0 0 0 3px rgba(255,200,80,0.14); border-radius:6px; }

/* 4) Ensure expand/collapse injected wrapper looks consistent with existing controls
   If the expand/collapse wrapper was appended to .controls, it will inherit spacing.
   Add a small helper for the injected wrapper so spacing matches */
.controls > div[style] { display:flex; gap:8px; align-items:center; }

/* 5) Footer placement: ensure footer sits below floats
   Footer already has clear: both; but enforce layout safety */
.footer {
  clear: both;
  width: 100%;
  text-align: center;
  margin: 36px auto 12px;
  padding: 12px 20px;
  color: var(--text);
  font-size: 1rem;
  align-self: center;
  position: relative;  /* keep it in normal flow */
  z-index: 2;
}

/* 6) Final: small responsive tweak so controls don't overflow on small screens */
@media (max-width: 640px) {
  .small-control { font-size: 0.92rem; padding: 8px 10px; }
  .episode-banner .permalink { font-size: 1rem; padding: 6px; }
}

.gender-icon {
  display: inline-block;
  font-weight: 700;
  font-size: 0.98rem;
  margin-right: 6px;
  line-height: 0.9;
  vertical-align: text-bottom;

  -webkit-text-stroke: 1px currentColor; /* outline to thicken */
}

/* color variants */
.gender-icon.male { color: #2b6cf6; }    /* blue-ish */
.gender-icon.female { color: #e84a9b; }  /* pink-ish */

/* small accessibility tweak: keep icon keyboard-focus neutral */
.gender-icon[title] { cursor: default; }

/* Illegal encounter styling (desaturated like fainted but distinct) */
.event.illegal {
  box-shadow: 0 8px 24px rgba(16,18,36,0.04);
  opacity: 0.95;
}

/* Desaturate portrait/badge */
.event.illegal .sprite,
.event.illegal .badge-icon {
  filter: grayscale(100%) contrast(85%);
  opacity: 0.62;
}

/* Header color for illegal events */
.event.illegal .event-header {
  background: linear-gradient(180deg,#f5f5f5,#ececec);
  color: #4a4a4a;
  border: 1px solid #dedede;
}

/* Ribbon appearance when using data-ribbon (muted tone) */
.event.illegal .event-header[data-ribbon]::before {
  background: linear-gradient(90deg,#d1d5db,#bdbdbd);
  color: #222;
  box-shadow: none;
}

/* Slightly dim event text */
.event.illegal .species-nick,
.event.illegal .obtained-line,
.event.illegal .item-notes {
  color: #555;
}

/* Failed encounter style */
.event.failed {
  box-shadow: 0 6px 18px rgba(0,0,0,0.04);
  opacity: 0.95;
}

/* Desaturate the sprite / badge for failed */
.event.failed .sprite,
.event.failed .badge-icon {
  filter: grayscale(100%) contrast(85%);
  opacity: 0.6;
}

/* Header color for failed events (soft muted red/orange) */
.event.failed .event-header {
  background: linear-gradient(180deg, #fff6f4, #fff0ee);
  color: #6a1b1b;
  border: 1px solid #ffdede;
}

/* Ribbon appearance for failed (slightly warm/alert) */
.event.failed .event-header[data-ribbon]::before {
  background: linear-gradient(90deg,#ffbcbc,#ff9a9a);
  color: #3a0b0b;
  box-shadow: 0 6px 18px rgba(255,110,110,0.06);
}

/* Slightly dim text for body when failed */
.event.failed .species-nick,
.event.failed .obtained-line,
.event.failed .item-notes {
  color: #5a5a5a;
}

/* ========= Gym badge milestone banner (full-width milestone) ========= */

.gym-section {
  clear: both;
  width: 100%;
  display: block;
  margin: 28px 0;
  margin-top: 50px;
  position: relative;
  z-index: 20; /* sits above timeline line */
}

/* core banner box */
.gym-banner {
  margin: 0 auto;
  max-width: var(--container-max, 1200px);
  background: radial-gradient(circle at center, var(--primary), var(--secondary));
  border-radius: 16px;
  margin-top: 8px;
  padding: 28px 20px 18px 20px; /* extra top padding so badge icon doesn't overlap content */
  box-shadow: 0 10px 30px rgba(11,40,60,0.06);
  position: relative;
  border: 2px solid rgba(34,116,175,0.06);
  box-sizing: border-box;
  overflow: visible;
}

/* the badge icon floating above the center */
.gym-badge-icon {
  position: absolute;
  top: -38px;
  left: 50%;
  transform: translateX(-50%);
  width: 92px;
  height: 92px;
  border-radius: 12px;
  padding: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 8px 22px rgba(34,116,175,0.12);
  background: linear-gradient(90deg,#ffd86b,#ffb74d);
  z-index: 30;  /* ensures above everything */
  font-weight: 800;
  color: #2b1700;
  text-align:center;
  pointer-events: none;
  line-height:1;
}

/* badge image inside floating icon */
.gym-badge-icon img {
  width:100%;
  height:100%;
  object-fit:contain;
  display:block;
  border-radius:8px;
  background: transparent;
}

/* top row: pokemon sprites across the top of the banner - evenly spaced */
.gym-pokemon-row {
  display:flex;
  gap:14px;
  justify-content: space-between;   /* evenly space items across width */
  align-items:center;
  padding: 4px 4px 12px 4px;
  flex-wrap:nowrap;
  width:100%;
  box-sizing:border-box;
}

/* if there are many pokemons, allow wrapping but keep spacing */
.gym-pokemon-row.wrap {
  flex-wrap:wrap;
  justify-content:flex-start;
}

/* each pokemon card - constrained width so spacing looks good */
.gym-pokemon {
  display:flex;
  flex-direction:column;
  align-items:center;
  gap:6px;
  width:156px; 
  height:156px;
  width: 12.5%; /* starting width, will be constrained by min/max */
  padding:8px;
  box-sizing:border-box;
  text-align:center;
}

/* when spacing across the banner, make cards flex: 1 so they distribute */
.gym-pokemon-row .gym-pokemon {
  flex: 1 1 0;
}

/* sprite inside the gym pokemon card */
.gym-pokemon .sprite {
  width:96px;
  height:96px;
  object-fit:contain;
  border-radius:6px;
  background: transparent;
}

/* small meta below the sprite */
.gym-pokemon .pk-name {
  font-size: 0.85rem;
  line-height:1;
  color:#1a2b2d;
  white-space:nowrap;
  overflow:hidden;
  text-overflow:ellipsis;
  max-width:100%;
}

/* center info row (left/right meta) */
.gym-meta {
  display:flex;
  justify-content:space-between;
  align-items:center;
  gap:12px;
  margin-top:14px;
  font-weight:600;
  color:#073642;
  font-size:0.98rem;
}

/* left label */
.gym-meta .left {
  flex:1 1 40%;
  text-align:left;
}

/* right label */
.gym-meta .right {
  flex:1 1 40%;
  text-align:right;
  color:#084b6b;
  font-weight:600;
}

/* description area */
.gym-description {
  margin-top:12px;
  padding:10px 12px;
  border-radius:10px;
  background: rgba(255,255,255,0.9);
  border:1px solid rgba(16,24,40,0.03);
  color:#123;
  font-size:0.95rem;
  text-align: center;
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.6);
}

.gym-banner a,
.gym-banner button {
  pointer-events: auto;
  cursor: pointer;
  color: inherit;           /* keep same color scheme */
  text-decoration: underline;
}

/* responsive adjustments */
@media (max-width:900px) {
  .gym-banner { padding: 24px 14px 14px 14px; }
  .gym-badge-icon { width:64px; height:64px; top:-28px; }
  .gym-pokemon-row { gap:10px; }
  .gym-pokemon { min-width:64px; max-width:120px; padding:6px; width:auto; }
  .gym-pokemon-row.wrap { justify-content:flex-start; }
  .gym-meta { flex-direction:column; align-items:flex-start; gap:6px; }
  .gym-meta .right { text-align:left; }
}

/* No-major-events placeholder */
.no-event-card {
  width: 60%;
  margin: 12px auto;
  padding: 10px 14px;
  border-radius: 10px;
  background: var(--secondary);
  color: var(--text);
  text-align:center;
  font-style: italic;
  box-shadow: 0 6px 18px rgba(8,12,24,0.03);
  border: 1px dashed rgba(16,24,40,0.04);
  z-index: 10;
}

/* smaller for narrow screens */
@media (max-width:900px) {
  .no-event-card { width: 92%; padding: 10px; }
}

/* small pill appended to episode title (banner) */
.episode-banner .no-event-pill {
  display:inline-block;
  margin-left:10px;
  padding:4px 8px;
  border-radius:999px;
  font-size:0.8rem;
  font-weight:600;
  color: #2f4854;
  background: rgba(32,48,60,0.06);
  border: 1px solid rgba(16,24,40,0.03);
}

/* Fix: make episode container transparent to pointer events, let children handle clicks */
.episode-section { pointer-events: none; }
.episode-section > .episode-banner,
.episode-section > .episode-contents,
.episode-section .event,
.episode-section .episode-banner * ,
.episode-section .episode-contents * {
  pointer-events: auto;
}