:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --text: #1a1d21;
  --muted: #5c6570;
  --border: #d8dee6;
  --primary: #b22234;
  --primary-hover: #8f1b29;
  --dem: #2454a8;
  --rep: #c41e3a;
  --other: #6b7280;
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  --radius: 10px;
  --party-size: 44px;
}

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

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
  min-height: 100dvh;
}

body:not(.detail-active) {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  height: 100svh;
}

.site-header {
  flex-shrink: 0;
  background: linear-gradient(135deg, #b22234 0%, #3c3b6e 100%);
  color: #fff;
  padding: 1.25rem 1rem 1rem;
  text-align: center;
}

.site-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  text-align: left;
}

.site-header-logo-link {
  flex-shrink: 0;
  display: block;
  line-height: 0;
  border-radius: 6px;
}

.site-header-logo-link:hover {
  opacity: 0.9;
}

.site-header-logo {
  display: block;
  width: 56px;
  height: 56px;
  object-fit: contain;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.92);
  padding: 2px;
}

.site-header-titles {
  flex: 1;
  min-width: 0;
}

.site-header h1 {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.tagline {
  margin: 0.25rem 0 0;
  font-size: 0.9rem;
  opacity: 0.9;
}

.header-help {
  flex-shrink: 0;
  display: inline-block;
  padding: 0.45rem 0.85rem;
  background: rgba(255, 255, 255, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.55);
  border-radius: var(--radius);
  color: #fff;
  font-size: 0.9rem;
  font-weight: 600;
  text-decoration: none;
}

.header-help:hover {
  background: rgba(255, 255, 255, 0.32);
}

.header-help-inline {
  background: var(--surface);
  color: var(--text);
  border-color: var(--border);
}

.header-help-inline:hover {
  background: #eef1f5;
}

.header-help-dark {
  background: #fff;
  color: #000;
  border-color: #000;
}

.help-page {
  max-width: 640px;
  margin: 0 auto;
  padding: 1.25rem 1rem 2rem;
}

.help-page h2 {
  margin: 0 0 1rem;
  font-size: 1.15rem;
}

.help-page ul {
  margin: 0;
  padding-left: 1.25rem;
}

.help-page li {
  margin-bottom: 1rem;
}

.help-page code {
  background: #e8ecf1;
  padding: 0.1em 0.35em;
  border-radius: 4px;
  font-size: 0.9em;
}

.help-contact {
  margin-top: 1.5rem;
  color: var(--muted);
  font-size: 0.95rem;
}

.help-contact a {
  color: #3c3b6e;
}

.view {
  display: none;
  max-width: 640px;
  margin: 0 auto;
  padding: 1rem;
}

.view.active {
  display: block;
}

.view.active:not(.view-detail) {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  min-height: 0;
  width: 100%;
  max-width: none;
  margin: 0;
}

.search-form label {
  display: block;
  margin-bottom: 0.85rem;
}

.search-form span {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 0.3rem;
  color: var(--muted);
}

.search-form input {
  width: 100%;
  padding: 0.65rem 0.75rem;
  font-size: 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}

.search-form input:focus {
  outline: 2px solid #3c3b6e;
  outline-offset: 1px;
  border-color: #3c3b6e;
}

.hint {
  font-size: 0.8rem;
  color: var(--muted);
  margin: 0.5rem 0 1rem;
}

.hint code {
  background: #e8ecf1;
  padding: 0.1em 0.35em;
  border-radius: 4px;
  font-size: 0.9em;
}

.actions {
  display: flex;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.btn {
  flex: 1;
  min-width: 120px;
  padding: 0.75rem 1rem;
  font-size: 1rem;
  font-weight: 600;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
}

.btn.primary {
  background: var(--primary);
  color: #fff;
}

.btn.primary:hover {
  background: var(--primary-hover);
}

.btn.primary:disabled {
  opacity: 0.6;
  cursor: wait;
}

.btn.secondary {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
}

.btn.back {
  flex: 0 0 auto;
  min-width: auto;
  padding: 0.5rem 0.85rem;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 0.95rem;
}

.error {
  color: #b91c1c;
  font-size: 0.9rem;
  margin-top: 0.75rem;
}

.results-bar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}

.results-summary {
  margin: 0;
  font-size: 0.9rem;
  color: var(--muted);
}

.results-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}

.voter-tile {
  display: flex;
  align-items: stretch;
  gap: 0.75rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 0.75rem;
  cursor: pointer;
  text-align: left;
  width: 100%;
  font: inherit;
  color: inherit;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.voter-tile:hover,
.voter-tile:focus-visible {
  border-color: #3c3b6e;
  box-shadow: 0 4px 12px rgba(60, 59, 110, 0.15);
  outline: none;
}

.party-badge {
  flex-shrink: 0;
  width: var(--party-size);
  height: var(--party-size);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  overflow: hidden;
}

.party-badge img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.party-badge.unaffiliated {
  background: #e8ecf1;
  color: var(--other);
  font-weight: 800;
  font-size: 1.25rem;
}

.tile-body {
  flex: 1;
  min-width: 0;
}

.tile-confirmation {
  flex-shrink: 0;
  align-self: center;
  background: #c8f5d0;
  color: #b22234;
  font-weight: 700;
  font-size: 0.7rem;
  line-height: 1.15;
  padding: 0.4rem 0.45rem;
  border-radius: 4px;
  text-align: center;
  letter-spacing: 0.03em;
}

.tile-name {
  font-weight: 700;
  font-size: 1.05rem;
  margin: 0 0 0.2rem;
  word-break: break-word;
}

.tile-address,
.tile-location {
  margin: 0;
  font-size: 0.9rem;
  color: var(--muted);
  word-break: break-word;
}

/* Results view: 25% smaller type than search/detail */
#view-results .results-summary {
  font-size: 0.675rem;
}

#view-results .btn.back {
  font-size: 0.71rem;
}

#view-results .header-help {
  font-size: 0.675rem;
}

#view-results .tile-name {
  font-size: 0.79rem;
}

#view-results .tile-address,
#view-results .tile-location {
  font-size: 0.675rem;
}

#view-results .party-badge {
  width: 33px;
  height: 33px;
}

#view-results .party-badge.unaffiliated {
  font-size: 0.94rem;
}

#view-results .tile-confirmation {
  font-size: 0.55rem;
  padding: 0.3rem 0.35rem;
}

body.detail-active {
  background: #121212;
}

body.detail-active .site-header {
  display: none;
}

.view-detail {
  max-width: none;
  margin: 0;
  padding: 0;
  min-height: 100dvh;
  background: #121212;
  color: #f5f5f5;
}

.view-detail.active {
  display: flex;
  flex-direction: column;
}

.detail-toolbar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  background: #2dd36f;
  color: #000;
  padding: 0.65rem 0.75rem;
  min-height: 48px;
}

.detail-toolbar .detail-toolbar-title {
  flex: 1;
}

.detail-back {
  background: transparent;
  border: none;
  font-size: 1.5rem;
  line-height: 1;
  padding: 0.15rem 0.5rem 0.15rem 0.25rem;
  cursor: pointer;
  color: #000;
}

.detail-toolbar-title {
  font-size: 1.1rem;
  font-weight: 500;
  text-transform: lowercase;
}

.detail-loading,
.detail-error {
  padding: 1rem;
  text-align: center;
}

.detail-error {
  color: #ff6b6b;
}

.detail-header {
  display: flex;
  gap: 0.85rem;
  align-items: flex-start;
  background: #2dd36f;
  color: #000;
  padding: 0.85rem 1rem 1rem;
  border-bottom: 2px solid #000;
}

.detail-party-icon {
  flex-shrink: 0;
  width: 72px;
  height: 72px;
  background: #fff;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.detail-party-icon img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.detail-party-icon.unaffiliated {
  font-weight: 800;
  font-size: 2rem;
  color: #6b7280;
}

.detail-header-text {
  flex: 1;
  min-width: 0;
}

.detail-name,
.detail-address,
.detail-address2,
.detail-city-line {
  margin: 0;
  font-weight: 700;
  text-transform: uppercase;
  line-height: 1.35;
  word-break: break-word;
}

.detail-body {
  flex: 1;
  padding: 1rem 1rem 2rem;
  min-width: 0;
}

.detail-fields {
  margin: 0;
}

.detail-field {
  margin-bottom: 0.55rem;
}

.detail-field:not(.detail-field-block) dt,
.detail-field:not(.detail-field-block) dd {
  display: inline;
  margin: 0;
  font-weight: 400;
}

.detail-field:not(.detail-field-block) dt {
  font-weight: 700;
  text-transform: lowercase;
}

.detail-field-block {
  margin-bottom: 1rem;
}

.detail-field-block dt {
  display: block;
  font-weight: 700;
  margin: 0 0 0.25rem;
  text-transform: lowercase;
}

.detail-field-block dd {
  margin: 0;
  font-weight: 400;
}

.status-badge {
  display: inline-block;
  background: #2dd36f;
  color: #000;
  font-weight: 700;
  padding: 0.1rem 0.45rem;
  border-radius: 3px;
  text-transform: uppercase;
}

.district-list {
  list-style: none;
  margin: 0.25rem 0 0;
  padding: 0;
}

.district-list li {
  margin-bottom: 0.2rem;
}

.district-list span:first-child {
  font-weight: 600;
}

.btn-show-history {
  display: inline-block;
  margin-top: 0.35rem;
  padding: 0.45rem 1.25rem;
  background: #4285f4;
  color: #fff;
  border: none;
  border-radius: 4px;
  font-weight: 700;
  font-size: 0.95rem;
  letter-spacing: 0.04em;
  cursor: pointer;
}

.btn-show-history:hover {
  background: #3367d6;
}

.voting-history {
  margin-top: 0.85rem;
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.vh-chart {
  --vh-cell-max: 22px;
  width: 100%;
  max-width: 100%;
}

.vh-row {
  display: grid;
  grid-template-columns: minmax(3.25rem, max-content) repeat(12, minmax(0, 1fr));
  gap: 0;
  align-items: center;
  margin-bottom: 0.25rem;
  width: 100%;
  max-width: 100%;
}

.vh-header {
  margin-bottom: 0.35rem;
}

.vh-year-label {
  font-weight: 700;
  font-size: 0.8rem;
  line-height: 1;
  padding-right: 0.35rem;
  white-space: nowrap;
}

.vh-corner {
  visibility: hidden;
}

.vh-month {
  width: 100%;
  max-width: var(--vh-cell-max);
  aspect-ratio: 1;
  justify-self: center;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fff;
  color: #000;
  font-weight: 700;
  font-size: clamp(8px, 2.2vw, 11px);
  line-height: 1;
}

.vh-cell {
  width: 100%;
  max-width: var(--vh-cell-max);
  aspect-ratio: 1;
  justify-self: center;
  display: flex;
  align-items: center;
  justify-content: center;
}

.vh-cell img {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: contain;
}

/* Wide screens: fixed cell size so the grid does not stretch across the page */
@media (min-width: 641px) {
  .vh-chart {
    width: max-content;
    max-width: 100%;
  }

  .vh-row {
    width: max-content;
    max-width: 100%;
    grid-template-columns: minmax(3.25rem, max-content) repeat(12, var(--vh-cell-max));
  }

  .vh-month,
  .vh-cell {
    width: var(--vh-cell-max);
    justify-self: stretch;
  }
}

@media (max-width: 1024px) {
  html {
    -webkit-text-size-adjust: 100%;
    /* 25% above browser default (16px → 20px) */
    font-size: 20px;
  }

  .site-header {
    padding: 1rem 1rem 0.85rem;
  }

  .site-header-logo {
    width: 56px;
    height: 56px;
  }

  .site-header h1 {
    font-size: 1.5rem;
  }

  .tagline {
    font-size: 0.95rem;
  }

  .header-help {
    font-size: 1rem;
    padding: 0.5rem 0.85rem;
  }

  .view {
    width: 100%;
    max-width: none;
    padding: 0.85rem 1rem 1.25rem;
  }

  #view-search.view.active {
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .search-form {
    flex: 0 0 auto;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    gap: 0.6rem;
  }

  .search-form label {
    display: block;
    margin: 0;
    flex: 0 0 auto;
  }

  .search-form span {
    font-size: 0.95rem;
    margin-bottom: 0.3rem;
  }

  .search-form input {
    font-size: 1.1rem;
    padding: 0.65rem 0.75rem;
    min-height: 2.75rem;
  }

  .search-form .hints-block {
    flex: 0 0 auto;
    margin: 0.25rem 0 0.5rem;
  }

  .hint {
    font-size: 0.9rem;
    line-height: 1.4;
    margin: 0.25rem 0;
  }

  .actions {
    flex: 0 0 auto;
    gap: 0.85rem;
    margin-top: 0.25rem;
  }

  .btn {
    font-size: 1.1rem;
    padding: 0.75rem 1rem;
    min-height: 2.85rem;
  }

  .btn.back {
    font-size: 1.2rem;
  }

  .error {
    font-size: 1.05rem;
  }

  .detail-toolbar-title {
    font-size: 1.65rem;
  }

  .detail-back {
    font-size: 2.25rem;
  }

  .detail-body {
    font-size: 1.35rem;
  }

  .btn-show-history {
    font-size: 1.4rem;
    padding: 0.65rem 1.5rem;
  }

  .vh-chart {
    --vh-cell-max: 18px;
  }

  .vh-year-label {
    font-size: 0.75rem;
  }

  #view-results .results-summary {
    font-size: 0.79rem;
  }

  #view-results .btn.back {
    font-size: 0.9rem;
  }

  #view-results .header-help {
    font-size: 0.75rem;
  }

  #view-results .voter-tile {
    padding: 1rem;
  }

  #view-results .tile-name {
    font-size: 0.86rem;
  }

  #view-results .tile-address,
  #view-results .tile-location {
    font-size: 0.79rem;
  }
}

/* Wider tall phones (e.g. Pixel 9): spread fields vertically, slightly larger type */
@media (max-width: 1024px) and (min-width: 410px) and (min-height: 680px) {
  html {
    font-size: 22px;
  }

  .search-form {
    flex: 1 1 auto;
    justify-content: space-between;
    gap: 0.35rem;
    min-height: 100%;
  }

  .search-form span {
    font-size: 1rem;
    margin-bottom: 0.35rem;
  }

  .search-form input {
    font-size: 1.15rem;
    padding: 0.75rem 0.8rem;
    min-height: 2.85rem;
  }

  .hint {
    font-size: 0.95rem;
  }

  .btn {
    font-size: 1.15rem;
    padding: 0.85rem 1rem;
    min-height: 3rem;
  }
}

@media (max-width: 420px) {
  .site-header h1 {
    font-size: 1.25rem;
  }

  .tagline {
    font-size: 0.8rem;
    line-height: 1.25;
  }

  .header-help {
    font-size: 0.85rem;
    padding: 0.4rem 0.65rem;
  }
}

@media (min-width: 480px) {
  .site-header-logo {
    width: 72px;
    height: 72px;
  }

  .site-header h1 {
    font-size: 1.75rem;
  }

  .view:not(.view-detail) {
    padding: 1.25rem 1.5rem 2rem;
  }
}
