/* style.css — how the page looks */

/* Make the "hidden" attribute always win. Without this, a rule like
   .toolbar { display: flex; } would keep a hidden toolbar on screen. */
[hidden] {
  display: none !important;
}

/* Dark theme. "color-scheme: dark" also tells the browser to draw its own
   parts (scrollbars, dropdown menus) in dark colors. */
:root {
  color-scheme: dark;
}

body {
  margin: 0;
  font-family: "Libre Franklin", system-ui, -apple-system, "Segoe UI", sans-serif;
  background: #14191c;
  color: #e4e8e6;
}

main {
  max-width: 960px;
  margin: 0 auto;
  padding: 24px 16px 48px;
}

/* ---------- Wide screens: sidebar + map, no scrolling ----------
   A grid splits the page into named areas. On wide screens:

       | top    | map |
       | bottom | map |

   The sidebar is 320px wide and the map takes the rest. On narrow
   screens none of this applies, so the three parts simply stack. */
@media (min-width: 900px) {
  main {
    max-width: 1760px;
    padding: 24px;
    /* Exactly the window's height (padding included), so the bottom row has a
       fixed size to fit in, instead of growing to fit whatever is inside. */
    height: 100vh;
    box-sizing: border-box;
    display: grid;
    grid-template-columns: 320px 1fr;
    /* minmax(0, 1fr) lets the bottom row be SHORTER than its contents, so
       it ends where the map ends and scrolls inside instead (see below). */
    grid-template-rows: auto minmax(0, 1fr);
    grid-template-areas:
      "top    map"
      "bottom map";
    column-gap: 28px;
    align-items: start;
  }

  #panel-top    { grid-area: top; }

  /* The bottom of the sidebar fills the space down to the bottom of the map.
     If what's inside is taller (the open county list), it scrolls on its
     own instead of making the whole page scroll. */
  #panel-bottom {
    grid-area: bottom;
    align-self: stretch;
    overflow-y: auto;
    /* Mac scrollbars float on top of the content, so leave room on the
       right or the scrollbar covers the vote counts. */
    padding-right: 14px;
  }

  #map-area {
    grid-area: map;
    /* Make the map exactly as wide as it can be while still fitting the
       window's height. The map is 900 wide by 687 tall, so its width is
       1.31 x its height. Available height = window height (100vh) minus
       the page's top+bottom padding (48px) and the map box's own
       padding (16px). */
    max-width: calc((100vh - 64px) * 1.31);
    width: 100%;
    justify-self: center;
  }

  /* In the narrow sidebar, the dropdowns fill the full width */
  select {
    width: 100%;
    min-width: 0;
  }
}

h1 {
  margin: 0 0 4px;
  font-size: 2rem;
}

/* On phones, size the title by the screen's width so it stays on one line.
   5.6vw means 5.6% of the screen's width. It never gets bigger than 2rem. */
@media (max-width: 600px) {
  h1 {
    font-size: min(5.6vw, 2rem);
  }
}

.intro {
  margin: 0 0 16px;
  color: #9aa6a2;
}

/* ---------- Toolbar above the map ---------- */

.toolbar {
  display: flex;
  /* Keep the count and the buttons on one line. If they were allowed to
     wrap, the buttons would jump below the text whenever the count got
     wider (e.g. going from "9 counties" to "10 counties"). */
  flex-wrap: nowrap;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
  font-size: 0.95rem; /* a touch smaller so the count and buttons fit the 320px sidebar */
}

#selected-count {
  font-weight: 600;
  white-space: nowrap;
  /* Every digit gets the same width, so the text doesn't wiggle as the count changes */
  font-variant-numeric: tabular-nums;
}

#hover-name {
  margin: 0 0 8px;
  min-height: 1.5em; /* keeps the space even when empty, so the map below never moves */
  color: #9aa6a2;
}

.toolbar-buttons {
  display: flex;
  gap: 8px;
  flex-shrink: 0; /* the buttons never get squeezed */
}

/* Slightly narrower buttons here so everything fits in the 320px sidebar */
.toolbar-buttons button {
  padding: 6px 9px;
}

button {
  font: inherit;
  padding: 6px 14px;
  border: 1px solid #3d474b;
  border-radius: 6px;
  background: #222a2e;
  color: #e4e8e6;
  cursor: pointer;
}

@media (hover: hover) and (pointer: fine) {
  button:hover {
    background: #2c363a;
  }
}

/* ---------- The map ---------- */

#map-area {
  position: relative;      /* lets the NYC box be placed on top of the map */
  padding: 8px;
  touch-action: manipulation; /* scrolling and pinching are fine, but no double-tap zoom */
  user-select: none;       /* stops the browser highlighting things while you drag */
  -webkit-user-select: none;
}

/* Both copies of the SVG stretch to fill their box */
#map-area svg {
  display: block;
  width: 100%;
  height: auto;
}

/* The zoomed-in NYC box, sitting in the empty area at the bottom left
   (where Pennsylvania would be). Sizes are % of the map, so it scales with it. */
#nyc-box {
  position: absolute;
  left: 3%;
  bottom: 2%;
  width: 22%;
  background: #14191c;
  border: 1px solid #3d474b;
  border-radius: 8px;
  overflow: hidden;
}

/* The label floats on top of the zoomed map instead of taking up its own
   space, so the box stays the same shape at every screen size. */
.nyc-label {
  position: absolute;
  top: 0;
  left: 0;
  font-size: 0.75rem;
  color: #9aa6a2;
  background: rgba(20, 25, 28, 0.85);
  padding: 2px 8px;
  border-bottom-right-radius: 6px;
  pointer-events: none; /* taps go straight through to the counties underneath */
}

/* On narrow screens (phones), the box needs to be bigger so it's easy to tap.
   The negative top margin pulls it up into the empty space at the bottom left
   of the map, so it only sticks out a little below. That keeps the vote
   button as high on the page as possible. */
@media (max-width: 600px) {
  #nyc-box {
    position: relative;
    left: auto;
    bottom: auto;
    width: 42%;
    margin: -22% 0 0;
  }
}

/* ---------- Tooltip (desktop) ---------- */

#map-tooltip {
  position: fixed;        /* placed using the mouse's position in the window */
  z-index: 10;            /* above everything else on the page */
  pointer-events: none;   /* the mouse "sees through" it to the county below */
  max-width: 240px;
  padding: 6px 10px;
  border-radius: 6px;
  background: #2c363a;
  border: 1px solid #3d474b;
  color: #e4e8e6;
  font-size: 0.85rem;
  line-height: 1.4;
}

/* Name on the first line, numbers on the second */
#map-tooltip strong {
  display: block;
}

/* On computers with a mouse, the tooltip replaces the line of text above
   the map. "hover: hover" and "pointer: fine" mean "has a mouse or trackpad",
   so phones and tablets keep the line (they show it when you tap). */
@media (hover: hover) and (pointer: fine) {
  #hover-name {
    display: none;
  }
}

/* The instructions say "Click" on computers with a mouse, and "Tap" on
   everything else. */
.mouse-only {
  display: none;
}

@media (hover: hover) and (pointer: fine) {
  .mouse-only {
    display: inline;
  }

  .touch-only {
    display: none;
  }
}

/* ---------- County shapes ---------- */

.county {
  fill: #3a454a;
  stroke: #14191c; /* same as the page background, so counties look like separate tiles */
  stroke-width: 0.8;
  /* Keeps borders thin in the zoomed NYC box instead of scaling up 10x */
  vector-effect: non-scaling-stroke;
  cursor: pointer;
  touch-action: none; /* on phones: dragging on a county paints */
}

/* Hover colors are only for computers with a mouse. On phones, the browser
   leaves the last tapped thing stuck in its hover look. */
@media (hover: hover) and (pointer: fine) {
  .county:hover {
    fill: #4a575d;
  }
}

.county.selected {
  fill: #3c8571;
}

@media (hover: hover) and (pointer: fine) {
  .county.selected:hover {
    fill: #4b9c85;
  }
}

#map-status {
  color: #9aa6a2;
}

/* ---------- Voting form (step 4) ---------- */

#vote-section {
  margin-top: 8px;
}

#vote-section label {
  display: block;
  font-weight: 600;
  margin-bottom: 6px;
}

.vote-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

select {
  font: inherit;
  padding: 6px 10px;
  border: 1px solid #3d474b;
  border-radius: 6px;
  background: #222a2e;
  color: #e4e8e6;
  min-width: 240px;
}

/* The main button gets the same green as selected counties */
button.primary {
  background: #3c8571;
  border-color: #3c8571;
  color: white;
  font-weight: 600;
}

@media (hover: hover) and (pointer: fine) {
  button.primary:hover {
    background: #4b9c85;
  }
}

button:disabled {
  opacity: 0.6;
  cursor: default;
}

/* Room above the Turnstile checkbox, for the rare times it appears */
#turnstile-box {
  margin-top: 8px;
}

#vote-message {
  min-height: 1.5em;
  color: #f08a76; /* red-ish, for problems */
}

/* After voting there's nothing to paint, so dragging on the map scrolls the page */
#map-area.locked .county {
  touch-action: manipulation;
}

/* On phones, the voting page stays still. map.js adds the "no-scroll" class
   before voting (when everything fits) and removes it afterwards. */
@media (max-width: 899px) {
  html.no-scroll,
  html.no-scroll body {
    overflow: hidden;
    overscroll-behavior: none;
    touch-action: pinch-zoom;
  }
}

/* After voting, the map can't be changed, so stop showing the "clickable" hand */
#map-area.locked .county {
  cursor: default;
}

/* ---------- Results (step 5) ---------- */

.results-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.results-row label {
  font-weight: 600;
}

#results-summary {
  margin: 8px 0 16px; /* a bit more room below, before the color key */
  color: #9aa6a2;
}

/* Results colors: the "inferno" palette from viridis, dark purple (0% said
   upstate) through magenta, red-orange and orange to pale yellow (100%). It
   leaves out the darkest fifth, which is nearly black and would vanish into
   the page. The colors get steadily brighter, which helps with color blindness. */
:root {
  --result-0: #5a1a80;
  --result-25: #932667;
  --result-50: #dd513a;
  --result-75: #fca50a;
  --result-100: #fcffa4;
}

/* map.js gives each county a --percent number, like 60. Each line below is one
   step of the color scale. A step counts from 0% to 100% as --percent crosses
   its quarter, and clamp() stops it from going outside that range.
   For 60: steps 1 and 2 are finished (so we're at the 50% color), step 3 is 40% of the
   way to the 75% color, and step 4 hasn't started. */
.county.has-result {
  --step-1: clamp(0%, (var(--percent) - 0) * 4%, 100%);
  --step-2: clamp(0%, (var(--percent) - 25) * 4%, 100%);
  --step-3: clamp(0%, (var(--percent) - 50) * 4%, 100%);
  --step-4: clamp(0%, (var(--percent) - 75) * 4%, 100%);

  /* Start at the 0% color, then blend in each later color by its step. */
  --blend-1: color-mix(in oklab, var(--result-25) var(--step-1), var(--result-0));
  --blend-2: color-mix(in oklab, var(--result-50) var(--step-2), var(--blend-1));
  --blend-3: color-mix(in oklab, var(--result-75) var(--step-3), var(--blend-2));
  fill: color-mix(in oklab, var(--result-100) var(--step-4), var(--blend-3));
}
.county.no-data { fill: #283034; }

/* The legend bar uses the same five colors */
.legend-bar {
  height: 12px;
  border-radius: 3px;
  background: linear-gradient(to right, var(--result-0), var(--result-25), var(--result-50), var(--result-75), var(--result-100));
}

/* Browsers that can blend the bar exactly like the map do so. */
@supports (background: linear-gradient(to right in oklab, red, blue)) {
  .legend-bar {
    background: linear-gradient(to right in oklab, var(--result-0), var(--result-25), var(--result-50), var(--result-75), var(--result-100));
  }
}

/* In results mode, hovering highlights the border instead of changing the
   color, so the color always means the same thing. */
@media (hover: hover) and (pointer: fine) {
  #map-area.locked .county:hover {
    stroke: #ffffff;
    stroke-width: 2;
  }
}

.legend {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 14px;
  font-size: 0.85rem;
  color: #9aa6a2;
}

.legend-title {
  font-weight: 600;
}

/* On phones and narrow screens the map comes right after the color key,
   so give it some breathing room underneath. */
@media (max-width: 899px) {
  .legend {
    margin-bottom: 16px;
    /* Line the label up with the colored bar, not with the bar + the
       0%/50%/100% numbers underneath it. */
    align-items: flex-start;
  }

  /* A line exactly as tall as the bar (12px), so the text's middle
     lands on the bar's middle. */
  .legend-title {
    line-height: 12px;
  }
}

.legend-scale {
  width: 200px;
}

/* 0% on the left, 50% in the middle, 100% on the right */
.legend-labels {
  display: flex;
  justify-content: space-between;
  margin-top: 2px;
  font-size: 0.8rem;
}

/* In the wide-screen sidebar, the color key reads better as a list.
   (This has to come AFTER the .legend rule above: when two rules
   conflict, the one later in the file wins.) */
@media (min-width: 900px) {
  .legend {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }
}

/* The county list */
#results-list {
  margin-top: 20px;
}

#results-list summary {
  cursor: pointer;
  font-weight: 600;
}

table {
  margin-top: 8px;
  border-collapse: collapse;
  width: 100%;
  max-width: 480px;
  font-size: 0.9rem;
}

th,
td {
  text-align: left;
  padding: 4px 8px;
  border-bottom: 1px solid #2f383c;
}

/* Keep the column headings visible while the list scrolls under them */
th {
  position: sticky;
  top: 0;
  background: #14191c; /* same as the page, so rows don't show through */
}

/* Numbers line up better on the right */
th:nth-child(2),
th:nth-child(3),
td:nth-child(2),
td:nth-child(3) {
  text-align: right;
}
