:root {
  --paper: #F4F6F3;
  --paper-raised: #FFFFFF;
  --ink: #1D2B27;
  --ink-soft: #55645D;
  --line: #DAD9D0;

  --west: #4E7FA6;
  --west-deep: #375D80;
  --midwest: #C99A3E;
  --midwest-deep: #9C7526;
  --south: #D98B54;
  --south-deep: #A8613A;
  --northeast: #8C5B7A;
  --northeast-deep: #663F58;
  --bg-state: #E4E1D8;
  --bg-state-line: #C9C6BA;

  --good: #3F8F5F;
  --good-bg: #E4F2E9;
  --bad: #C1594A;
  --bad-bg: #F7E7E4;

  --radius-s: 8px;
  --radius-m: 14px;
  --radius-l: 22px;

  --font-display: "Fraunces", Georgia, serif;
  --font-body: "Atkinson Hyperlegible", "Segoe UI", Arial, sans-serif;

  --shadow: 0 8px 24px rgba(29, 43, 39, 0.12);
  --shadow-soft: 0 2px 8px rgba(29, 43, 39, 0.08);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-body);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: contain;
}

button {
  font-family: inherit;
  cursor: pointer;
}

.app {
  max-width: 1100px;
  margin: 0 auto;
  padding: 18px 20px 40px;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* ---------- Header ---------- */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.header__title {
  display: flex;
  align-items: center;
  gap: 12px;
}

.compass {
  width: 34px;
  height: 34px;
  color: var(--west-deep);
  flex-shrink: 0;
}

.header h1 {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: clamp(1.25rem, 1rem + 1.4vw, 1.9rem);
  margin: 0;
  line-height: 1.05;
}

.header__sub {
  margin: 2px 0 0;
  font-size: 0.85rem;
  color: var(--ink-soft);
  letter-spacing: 0.02em;
}

.header__score {
  font-family: var(--font-display);
  background: var(--paper-raised);
  border: 1px solid var(--line);
  border-radius: var(--radius-m);
  padding: 8px 16px;
  box-shadow: var(--shadow-soft);
  white-space: nowrap;
}

.header__score-num {
  font-size: 1.3rem;
  font-weight: 600;
  color: var(--good);
}

.header__score-total {
  font-size: 1rem;
  color: var(--ink-soft);
}

/* ---------- Control bar ---------- */
.controlbar {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.segmented {
  display: inline-flex;
  background: var(--paper-raised);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 3px;
  box-shadow: var(--shadow-soft);
}

.segmented__btn {
  border: none;
  background: transparent;
  padding: 9px 18px;
  border-radius: 999px;
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--ink-soft);
  min-height: 40px;
  transition: background 0.18s ease, color 0.18s ease;
}

.segmented__btn.is-active {
  background: var(--ink);
  color: var(--paper);
}

.chipset {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  flex: 1 1 auto;
  -ms-overflow-style: none;
  scrollbar-width: none;
}
.chipset::-webkit-scrollbar { display: none; }

.chip {
  border: 1.5px solid var(--line);
  background: var(--paper-raised);
  color: var(--ink);
  padding: 9px 16px;
  border-radius: 999px;
  font-size: 0.9rem;
  font-weight: 700;
  white-space: nowrap;
  min-height: 40px;
  transition: border-color 0.18s ease, background 0.18s ease, color 0.18s ease;
}

.chip.is-active {
  background: var(--west-deep);
  border-color: var(--west-deep);
  color: #fff;
}

.numbers-toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1.5px solid var(--line);
  background: var(--paper-raised);
  color: var(--ink-soft);
  padding: 9px 14px;
  border-radius: 999px;
  font-size: 0.88rem;
  font-weight: 700;
  min-height: 40px;
  margin-left: auto;
  transition: border-color 0.18s ease, color 0.18s ease;
}

.numbers-toggle[aria-pressed="true"] {
  border-color: var(--west-deep);
  color: var(--west-deep);
}

.chipset--region {
  margin-top: -4px;
}

.chipset--region .chip.is-active[data-region="west"] { background: var(--west-deep); border-color: var(--west-deep); color: #fff; }
.chipset--region .chip.is-active[data-region="midwest"] { background: var(--midwest-deep); border-color: var(--midwest-deep); color: #fff; }
.chipset--region .chip.is-active[data-region="south"] { background: var(--south-deep); border-color: var(--south-deep); color: #fff; }
.chipset--region .chip.is-active[data-region="northeast"] { background: var(--northeast-deep); border-color: var(--northeast-deep); color: #fff; }
.chipset--region .chip.is-active[data-region="all"] { background: var(--ink); border-color: var(--ink); color: #fff; }

/* ---------- Map ---------- */
.mapwrap {
  position: relative;
  background: var(--paper-raised);
  border: 1px solid var(--line);
  border-radius: var(--radius-l);
  padding: 10px;
  box-shadow: var(--shadow-soft);
}

.legend-key {
  position: absolute;
  top: 20px;
  left: 24px;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 4px 10px;
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--ink-soft);
  background: rgba(255,255,255,0.9);
  padding: 6px 10px;
  border-radius: 12px;
  z-index: 2;
  max-width: calc(100% - 40px);
}
.legend-key__item {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  white-space: nowrap;
}
.legend-key__dot {
  width: 11px; height: 11px; border-radius: 50%;
  display: inline-block;
}

#mapSvg {
  width: 100%;
  height: auto;
  display: block;
  touch-action: manipulation;
}

.state-bg {
  fill: var(--bg-state);
  stroke: var(--bg-state-line);
  stroke-width: 0.75;
}

.state-shape {
  stroke: var(--paper-raised);
  stroke-width: 1.1;
  cursor: pointer;
  transition: filter 0.15s ease, transform 0.15s ease;
  transform-box: fill-box;
  transform-origin: center;
}

.state-shape--west { fill: var(--west); }
.state-shape--midwest { fill: var(--midwest); }
.state-shape--south { fill: var(--south); }
.state-shape--northeast { fill: var(--northeast); }

.state-shape--muted {
  fill: var(--bg-state);
  stroke: var(--bg-state-line);
  cursor: default;
}

.state-shape:not(.state-shape--muted):hover,
.state-shape:not(.state-shape--muted):focus-visible {
  filter: brightness(1.12);
  transform: scale(1.015);
}

.state-shape:focus-visible {
  outline: 3px solid var(--ink);
  outline-offset: 2px;
}

.state-shape.is-done {
  opacity: 0.45;
}

.state-shape.is-active-pulse {
  animation: pulse 1.1s ease-in-out infinite;
  stroke: var(--ink);
  stroke-width: 2.5;
}

@keyframes pulse {
  0%, 100% { filter: brightness(1); }
  50% { filter: brightness(1.3); }
}

.state-num {
  font-family: var(--font-body);
  font-weight: 700;
  font-size: 12px;
  fill: #fff;
  text-anchor: middle;
  dominant-baseline: middle;
  pointer-events: none;
  paint-order: stroke;
  stroke: rgba(0,0,0,0.35);
  stroke-width: 2.2px;
  stroke-linejoin: round;
}

.mapwrap__hint {
  text-align: center;
  color: var(--ink-soft);
  font-size: 0.85rem;
  margin: 8px 0 2px;
}

/* entrance animation, once */
@media (prefers-reduced-motion: no-preference) {
  .state-shape {
    animation: riseIn 0.5s both;
  }
}
@keyframes riseIn {
  from { opacity: 0; transform: translateY(6px) scale(0.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

/* ---------- Legend list ---------- */
.legend {
  background: var(--paper-raised);
  border: 1px solid var(--line);
  border-radius: var(--radius-m);
  box-shadow: var(--shadow-soft);
  overflow: hidden;
}

.legend__toggle {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border: none;
  background: transparent;
  padding: 14px 18px;
  font-weight: 700;
  font-size: 0.95rem;
  color: var(--ink);
}

.legend__toggle svg {
  transition: transform 0.2s ease;
}
.legend__toggle[aria-expanded="false"] svg {
  transform: rotate(-90deg);
}

.legend__body {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 24px;
  padding: 0 18px 18px;
}

.legend__body.is-collapsed { display: none; }

.legend__heading {
  font-family: var(--font-display);
  font-size: 1.05rem;
  font-weight: 600;
  margin: 0 0 8px;
  padding-bottom: 6px;
  border-bottom: 2px solid var(--line);
}
.legend__heading--west { color: var(--west-deep); border-color: var(--west); }
.legend__heading--midwest { color: var(--midwest-deep); border-color: var(--midwest); }
.legend__heading--south { color: var(--south-deep); border-color: var(--south); }
.legend__heading--northeast { color: var(--northeast-deep); border-color: var(--northeast); }

.legend__list {
  list-style: none;
  margin: 0;
  padding: 0;
  counter-reset: none;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.legend__list li {
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 6px 4px;
  border-radius: var(--radius-s);
  font-size: 0.92rem;
}

.legend__list li:hover { background: var(--paper); }

.legend__list .num {
  font-weight: 700;
  color: var(--ink-soft);
  min-width: 1.4em;
}

.legend__list .cap {
  color: var(--ink-soft);
  font-size: 0.85em;
}

/* ---------- Study card ---------- */
.studycard {
  position: fixed;
  left: 50%;
  bottom: 22px;
  transform: translateX(-50%);
  width: min(360px, calc(100vw - 32px));
  background: var(--paper-raised);
  border-radius: var(--radius-l);
  box-shadow: var(--shadow);
  padding: 20px 22px;
  z-index: 30;
  animation: slideUp 0.22s ease both;
}

@keyframes slideUp {
  from { opacity: 0; transform: translate(-50%, 16px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}

.studycard__close {
  position: absolute;
  top: 10px;
  right: 12px;
  border: none;
  background: transparent;
  font-size: 1.4rem;
  line-height: 1;
  color: var(--ink-soft);
  width: 36px;
  height: 36px;
}

.studycard__num {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: var(--ink);
  color: var(--paper);
  font-weight: 700;
  font-size: 0.9rem;
  margin-bottom: 8px;
}

.studycard__name {
  font-family: var(--font-display);
  font-size: 1.35rem;
  margin: 0 0 10px;
}

.studycard__facts {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin: 0;
}
.studycard__facts > div {
  display: flex;
  justify-content: space-between;
  border-bottom: 1px dashed var(--line);
  padding-bottom: 5px;
}
.studycard__facts dt {
  color: var(--ink-soft);
  font-size: 0.85rem;
}
.studycard__facts dd {
  margin: 0;
  font-weight: 700;
}

/* ---------- Quiz panel (inline, below map — never covers it) ---------- */
.quizpanel {
  background: var(--paper-raised);
  border: 1px solid var(--line);
  border-radius: var(--radius-l);
  box-shadow: var(--shadow-soft);
  padding: 20px 22px 22px;
  animation: panelIn 0.25s ease both;
  text-align: left;
}

@keyframes panelIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

.quizpanel__top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 6px;
}

.quizpanel__exit {
  border: none;
  background: transparent;
  color: var(--ink-soft);
  font-weight: 700;
  font-size: 0.85rem;
  text-decoration: underline;
  padding: 6px 4px;
  min-height: 36px;
}

.quizpanel__doneRow {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 4px;
}

.modal__state {
  display: inline-block;
  font-weight: 700;
  font-size: 0.8rem;
  letter-spacing: 0.02em;
  color: var(--ink-soft);
  background: var(--paper);
  border-radius: 999px;
  padding: 5px 12px;
}

.modal__question {
  font-family: var(--font-display);
  font-size: 1.25rem;
  margin: 0 0 18px;
  line-height: 1.3;
}

.modal__options {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.modal__options button {
  border: 1.5px solid var(--line);
  background: var(--paper);
  border-radius: var(--radius-m);
  padding: 14px 10px;
  font-size: 1rem;
  font-weight: 700;
  min-height: 52px;
  color: var(--ink);
  transition: border-color 0.15s ease, background 0.15s ease, transform 0.1s ease;
}

.modal__options button:active { transform: scale(0.97); }

.modal__options button.is-correct {
  background: var(--good-bg);
  border-color: var(--good);
  color: var(--good);
}
.modal__options button.is-wrong {
  background: var(--bad-bg);
  border-color: var(--bad);
  color: var(--bad);
}
.modal__options button:disabled { cursor: default; }

.modal__feedback {
  min-height: 1.4em;
  margin: 14px 0 0;
  font-weight: 700;
  text-align: center;
}
.modal__feedback.good { color: var(--good); }
.modal__feedback.bad { color: var(--bad); }

.modal__next {
  display: block;
  width: 100%;
  margin-top: 14px;
  background: var(--ink);
  color: var(--paper);
  border: none;
  border-radius: var(--radius-m);
  padding: 14px;
  font-size: 1rem;
  font-weight: 700;
  min-height: 50px;
}

#quizDone { text-align: center; }
#quizDone h2 {
  font-family: var(--font-display);
  margin-top: 0;
}
.modal__bigscore {
  font-family: var(--font-display);
  font-size: 2.6rem;
  font-weight: 600;
  margin: 6px 0;
  color: var(--good);
}

/* ---------- Responsive ---------- */
@media (max-width: 640px) {
  .app { padding: 14px 12px 32px; gap: 12px; }
  .legend__body { grid-template-columns: 1fr; gap: 14px; }
  .legend-key { font-size: 0.7rem; padding: 4px 8px; top: 10px; left: 10px; }
  .header h1 { font-size: 1.15rem; }
  .numbers-toggle span { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  .state-shape, .studycard, .modal, .modal-overlay * { animation: none !important; }
}
