:root {
  --navy: #052b5b;
  --navy-deep: #031d40;
  --blue: #0a4d87;
  --gold: #dda82d;
  --gold-light: #f5d57a;
  --cream: #fffaf0;
  --paper: #f9f0dc;
  --ink: #173154;
  --muted: #60718a;
  --line: rgba(112, 80, 22, 0.22);
  --shadow: 0 18px 55px rgba(1, 24, 55, 0.16);
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; }

body {
  margin: 0;
  color: var(--ink);
  background:
    radial-gradient(circle at 8% 0%, rgba(255,255,255,.95), transparent 24rem),
    linear-gradient(180deg, #dff3ff 0, #f8fbff 32rem, #eef7fb 100%);
  font-family: Georgia, "Times New Roman", serif;
  min-height: 100vh;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  opacity: .22;
  background-image:
    radial-gradient(circle, #fff 0 2px, transparent 2.5px),
    radial-gradient(circle, #fff 0 1.5px, transparent 2px);
  background-position: 0 0, 38px 45px;
  background-size: 86px 90px, 110px 118px;
  z-index: -1;
}

button, input, a { font: inherit; }

button, a { -webkit-tap-highlight-color: transparent; }

button:focus-visible, input:focus-visible, a:focus-visible {
  outline: 4px solid #65c7ff;
  outline-offset: 3px;
}

.skip-link {
  position: fixed;
  left: 1rem;
  top: -5rem;
  z-index: 20;
  background: white;
  color: var(--navy);
  padding: .8rem 1rem;
  border-radius: .6rem;
}

.skip-link:focus { top: 1rem; }

.site-header {
  min-height: 76px;
  padding: .75rem clamp(1rem, 4vw, 4rem);
  background: linear-gradient(135deg, var(--navy-deep), var(--navy));
  border-bottom: 3px solid var(--gold);
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: white;
}

.back-button {
  border: 1px solid rgba(245, 213, 122, .72);
  color: white;
  background: rgba(255,255,255,.07);
  border-radius: 999px;
  min-height: 44px;
  padding: .65rem 1.05rem;
  cursor: pointer;
}

.back-button:hover { background: rgba(255,255,255,.15); }

.mini-crest {
  width: 50px;
  height: 50px;
  display: grid;
  place-items: center;
  font-weight: 800;
  color: var(--gold-light);
  border: 2px solid var(--gold);
  border-radius: 50% 50% 46% 46%;
  box-shadow: inset 0 0 0 3px var(--navy), inset 0 0 0 4px var(--gold);
}

main {
  width: min(1500px, calc(100% - 2rem));
  margin: 1.25rem auto 4rem;
}

.welcome-shell,
.search-panel,
.results-section {
  border: 1px solid rgba(255,255,255,.95);
  box-shadow: var(--shadow);
}

.welcome-shell {
  display: grid;
  grid-template-columns: minmax(0, 1.05fr) minmax(360px, .95fr);
  min-height: 300px;
  overflow: hidden;
  border-radius: 24px 24px 12px 12px;
  background: linear-gradient(125deg, rgba(255,250,240,.98), rgba(255,255,255,.9));
  position: relative;
}

.welcome-shell::after {
  content: "";
  position: absolute;
  inset: 10px;
  border: 2px solid rgba(221,168,45,.54);
  border-radius: 17px 17px 8px 8px;
  pointer-events: none;
}

.welcome-copy {
  padding: clamp(2rem, 5vw, 5.5rem);
  align-self: center;
  position: relative;
  z-index: 2;
}

.eyebrow, .step-label, .dialog-kicker {
  margin: 0 0 .55rem;
  color: #8f6210;
  font-family: Arial, Helvetica, sans-serif;
  font-size: .78rem;
  font-weight: 800;
  letter-spacing: .13em;
  text-transform: uppercase;
}

h1, h2, h3, p { text-wrap: pretty; }

h1 {
  font-size: clamp(2.2rem, 5vw, 4.75rem);
  line-height: .98;
  margin: 0;
  color: var(--navy);
  letter-spacing: -.035em;
}

.intro {
  max-width: 45rem;
  margin: 1.1rem 0 0;
  font-size: clamp(1.05rem, 1.6vw, 1.35rem);
  line-height: 1.55;
  color: #455f7d;
}

.snow-divider {
  display: flex;
  align-items: center;
  gap: .65rem;
  margin-top: 1.25rem;
  color: var(--gold);
}

.welcome-art {
  min-height: 300px;
  overflow: hidden;
  position: relative;
}

.welcome-art::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 1;
  background: linear-gradient(90deg, var(--cream) 0, transparent 28%);
}

.welcome-art img {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: cover;
  object-position: 52% 20%;
  filter: saturate(.87) brightness(1.05);
}

.search-panel {
  margin-top: 1.25rem;
  border-radius: 12px;
  background: rgba(255,255,255,.92);
  padding: clamp(1.25rem, 3vw, 2.5rem);
}

.search-heading, .results-heading {
  display: flex;
  justify-content: space-between;
  align-items: end;
  gap: 1rem;
}

h2 {
  color: var(--navy);
  font-size: clamp(1.65rem, 3vw, 2.55rem);
  margin: 0;
}

.clear-button {
  appearance: none;
  border: 0;
  background: none;
  color: var(--blue);
  padding: .5rem;
  min-height: 44px;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 3px;
}

.search-box {
  margin-top: 1.25rem;
  height: 64px;
  display: flex;
  align-items: center;
  gap: .85rem;
  border: 2px solid #b9c9d9;
  background: white;
  border-radius: 14px;
  padding: 0 1.15rem;
  transition: border-color .2s, box-shadow .2s;
}

.search-box:focus-within {
  border-color: var(--blue);
  box-shadow: 0 0 0 4px rgba(40,145,210,.15);
}

.search-box svg {
  width: 25px;
  flex: 0 0 25px;
  fill: none;
  stroke: var(--blue);
  stroke-width: 2;
  stroke-linecap: round;
}

.search-box input {
  width: 100%;
  border: 0;
  outline: 0;
  color: var(--ink);
  font-family: Arial, Helvetica, sans-serif;
  font-size: 1.08rem;
  background: transparent;
}

.browse-controls {
  display: grid;
  grid-template-columns: 1.6fr .8fr;
  gap: 1.75rem;
  margin-top: 1.75rem;
}

fieldset {
  min-width: 0;
  margin: 0;
  padding: 0;
  border: 0;
}

legend {
  width: 100%;
  margin: 0 0 .8rem;
  color: var(--navy);
  font-family: Arial, Helvetica, sans-serif;
  font-size: .92rem;
  font-weight: 800;
}

.filter-hint {
  margin: -.35rem 0 .8rem;
  color: var(--muted);
  font-family: Arial, Helvetica, sans-serif;
  font-size: .86rem;
  line-height: 1.35;
}

.chip-row {
  display: flex;
  flex-wrap: wrap;
  gap: .55rem;
}

.filter-chip {
  min-height: 44px;
  border: 1px solid #adc1d2;
  border-radius: 999px;
  color: #244463;
  background: #f7fbfe;
  padding: .58rem .92rem;
  font-family: Arial, Helvetica, sans-serif;
  font-size: .88rem;
  font-weight: 700;
  cursor: pointer;
  transition: transform .15s, background .15s, color .15s;
}

.filter-chip:hover { transform: translateY(-1px); border-color: var(--blue); }

.filter-chip[aria-pressed="true"] {
  color: white;
  background: var(--navy);
  border-color: var(--navy);
  box-shadow: inset 0 0 0 2px var(--navy), inset 0 0 0 3px var(--gold-light);
}

.results-section {
  margin-top: 1.25rem;
  border-radius: 12px 12px 24px 24px;
  background: linear-gradient(180deg, rgba(250,242,224,.97), rgba(255,252,246,.98));
  padding: clamp(1.25rem, 3vw, 2.5rem);
}

.results-count {
  margin: 0;
  color: var(--muted);
  font-family: Arial, Helvetica, sans-serif;
  font-weight: 700;
}

.active-filter-line {
  margin-top: 1rem;
  border-left: 4px solid var(--gold);
  padding: .5rem .8rem;
  color: #52677f;
  font-family: Arial, Helvetica, sans-serif;
  font-size: .92rem;
}

.character-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 1rem;
  margin-top: 1.5rem;
}

.character-card {
  --school-accent: #1c6594;
  appearance: none;
  text-align: left;
  padding: 0;
  border: 1px solid rgba(20,55,87,.15);
  background: #fff;
  border-radius: 15px;
  overflow: hidden;
  cursor: pointer;
  box-shadow: 0 8px 24px rgba(11,38,68,.09);
  transition: transform .18s ease, box-shadow .18s ease;
}

.character-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 15px 35px rgba(11,38,68,.16);
}

.card-image {
  display: block;
  position: relative;
  aspect-ratio: 1.18 / 1;
  overflow: hidden;
  background: #f4ead4;
  border-bottom: 5px solid var(--school-accent);
}

.card-image img {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: cover;
  object-position: top;
}
.card-image img[hidden], .image-load-status[hidden] { display: none; }
.image-load-status { display: grid; place-items: center; min-height: 150px; padding: 1.2rem; text-align: center; color: #163e32; font: 600 1rem/1.45 Georgia, serif; }
.card-image .image-load-status { height: 100%; }
.results-tools { display: flex; flex-wrap: wrap; align-items: center; gap: .6rem; }
.results-tools button[hidden], .dialog-actions button[hidden], #dialogSheet[hidden] { display: none; }

.card-image::after {
  content: "View sheet";
  position: absolute;
  right: .6rem;
  bottom: .6rem;
  color: white;
  background: rgba(3,29,64,.88);
  border: 1px solid rgba(245,213,122,.8);
  border-radius: 999px;
  padding: .32rem .58rem;
  font-family: Arial, Helvetica, sans-serif;
  font-size: .7rem;
  font-weight: 700;
  opacity: 0;
  transform: translateY(4px);
  transition: opacity .18s, transform .18s;
}

.character-card:hover .card-image::after,
.character-card:focus-visible .card-image::after { opacity: 1; transform: none; }

.card-copy { padding: .9rem 1rem 1.05rem; }

.card-meta {
  display: flex;
  justify-content: space-between;
  gap: .5rem;
  font-family: Arial, Helvetica, sans-serif;
  color: var(--muted);
  font-size: .74rem;
  font-weight: 800;
  letter-spacing: .03em;
  text-transform: uppercase;
}

.card-name {
  margin: .35rem 0 .2rem;
  color: var(--navy);
  font-size: clamp(1.25rem, 2vw, 1.6rem);
  line-height: 1.1;
}

.card-title {
  margin: 0;
  color: #536a83;
  font-family: Arial, Helvetica, sans-serif;
  font-size: .85rem;
  line-height: 1.35;
}

.empty-state {
  margin: 2rem auto 0;
  padding: 3rem 1rem;
  text-align: center;
  color: var(--muted);
}

.empty-state > span { color: var(--gold); font-size: 2.5rem; }
.empty-state h3 { margin: .55rem 0; color: var(--navy); font-size: 1.55rem; }
.empty-state p { margin: 0 auto 1rem; }
.empty-state button { border: 0; border-radius: 999px; padding: .8rem 1rem; background: var(--navy); color: white; cursor: pointer; }

footer {
  padding: 1.5rem;
  text-align: center;
  color: #64768a;
  font-style: italic;
}

.profile-dialog {
  width: min(1180px, calc(100% - 2rem));
  max-height: calc(100vh - 2rem);
  padding: 0;
  border: 0;
  border-radius: 18px;
  color: var(--ink);
  background: var(--cream);
  box-shadow: 0 28px 90px rgba(0,14,36,.45);
}

.profile-dialog::backdrop { background: rgba(1,17,39,.76); backdrop-filter: blur(5px); }

.dialog-frame {
  display: grid;
  grid-template-columns: minmax(0, 1.35fr) minmax(280px, .65fr);
  min-height: 70vh;
  position: relative;
}

.dialog-close {
  position: absolute;
  right: .75rem;
  top: .75rem;
  z-index: 3;
  width: 46px;
  height: 46px;
  border: 2px solid rgba(255,255,255,.8);
  border-radius: 50%;
  color: white;
  background: rgba(3,29,64,.9);
  font-family: Arial, Helvetica, sans-serif;
  font-size: 1.7rem;
  line-height: 1;
  cursor: pointer;
}

.dialog-sheet-wrap {
  overflow: auto;
  padding: 1rem;
  background: #daeaf2;
  display: flex;
  justify-content: center;
  align-items: flex-start;
}

.dialog-sheet-wrap img {
  width: min(100%, 780px);
  height: auto;
  display: block;
  background: white;
  box-shadow: 0 8px 25px rgba(5,43,91,.14);
}

.dialog-details {
  padding: clamp(2rem, 4vw, 4rem) clamp(1.5rem, 3vw, 3rem);
  background:
    radial-gradient(circle at 100% 0, rgba(221,168,45,.18), transparent 15rem),
    var(--cream);
  border-left: 3px solid var(--gold);
}

.dialog-details h2 { font-size: clamp(2rem, 4vw, 3.4rem); line-height: 1; }
.dialog-school { margin: .8rem 0 0; color: #825d19; font-weight: 700; }
.dialog-title { margin: .7rem 0 0; color: #425c79; font-size: 1.08rem; line-height: 1.45; }
.dialog-help { margin: 1.3rem 0 0; color: var(--muted); font-family: Arial, Helvetica, sans-serif; line-height: 1.5; }

.dialog-actions { display: grid; gap: .7rem; margin-top: 1.5rem; }
.dialog-actions button, .dialog-actions a { min-height: 48px; border-radius: 999px; display: grid; place-items: center; padding: .75rem 1rem; cursor: pointer; text-decoration: none; text-align: center; }
.primary-action { color: white; background: var(--navy); border: 2px solid var(--gold); box-shadow: inset 0 0 0 2px var(--navy), inset 0 0 0 3px var(--gold-light); }
.secondary-action { color: var(--navy); background: white; border: 2px solid #9eb3c7; }
.text-action { color: var(--blue); background: transparent; border: 0; }

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

@media (max-width: 1050px) {
  .character-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .welcome-shell { grid-template-columns: 1.25fr .75fr; }
  .browse-controls { grid-template-columns: 1fr; }
}

@media (max-width: 760px) {
  main { width: min(100% - 1rem, 680px); margin-top: .5rem; }
  .site-header { min-height: 64px; padding: .55rem .75rem; }
  .mini-crest { width: 44px; height: 44px; font-size: .85rem; }
  .welcome-shell { grid-template-columns: 1fr; min-height: auto; }
  .welcome-copy { padding: 2.1rem 1.4rem 1.5rem; }
  .welcome-art { min-height: 150px; max-height: 180px; order: -1; }
  .welcome-art::before { background: linear-gradient(0deg, var(--cream) 0, transparent 45%); }
  .welcome-art img { object-position: 50% 18%; }
  h1 { font-size: clamp(2.15rem, 11vw, 3.4rem); }
  .intro { font-size: 1rem; }
  .search-heading, .results-heading { align-items: start; }
  .search-box { height: 58px; }
  .character-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .7rem; }
  .card-copy { padding: .75rem; }
  .card-meta { display: grid; gap: .18rem; }
  .card-name { font-size: 1.15rem; }
  .card-title { font-size: .76rem; }
  .dialog-frame { grid-template-columns: 1fr; }
  .dialog-details { border-left: 0; border-top: 3px solid var(--gold); padding: 1.4rem; }
  .dialog-sheet-wrap { max-height: 58vh; }
  .profile-dialog { max-height: calc(100vh - 1rem); width: calc(100% - 1rem); }
}

@media (max-width: 430px) {
  .back-button { font-size: .85rem; padding-inline: .8rem; }
  .character-grid { grid-template-columns: 1fr; }
  .card-image { aspect-ratio: 1.35 / 1; }
  .filter-chip { font-size: .82rem; padding-inline: .75rem; }
}

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

@media print {
  body.printing-card { background: white; }
  body.printing-card > *:not(.profile-dialog) { display: none !important; }
  body.printing-card .profile-dialog { position: static; width: 100%; max-height: none; box-shadow: none; }
  body.printing-card .profile-dialog::backdrop,
  body.printing-card .dialog-details,
  body.printing-card .dialog-close { display: none !important; }
  body.printing-card .dialog-frame { display: block; min-height: 0; }
  body.printing-card .dialog-sheet-wrap { display: block; overflow: visible; padding: 0; background: white; }
  body.printing-card .dialog-sheet-wrap img { width: 100%; max-height: 100vh; object-fit: contain; box-shadow: none; }
}
