:root {
  --bg: #0f1420;
  --panel-bg: #161d2e;
  --panel-border: #26304a;
  --text: #e7ecf7;
  --muted: #8b97b3;
  --accent: #4f8cff;
  --danger: #ef5350;
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
  overflow-x: hidden;
}

body {
  display: flex;
  flex-direction: column;
  font-family:
    system-ui,
    -apple-system,
    "Segoe UI",
    Roboto,
    Arial,
    sans-serif;
  background: var(--bg);
  color: var(--text);
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.6rem 1rem;
  background: var(--panel-bg);
  border-bottom: 1px solid var(--panel-border);
}

.app-header h1 {
  font-size: 1.1rem;
  margin: 0;
  white-space: nowrap;
}

/* Once there is a crowd estimate the title becomes a live total. */
.title-estimate {
  display: inline-flex;
  align-items: baseline;
  gap: 0.35rem;
}

/* `hidden` needs to beat the display rule above. */
.title-estimate[hidden] {
  display: none;
}

/* The icon shares the row's baseline alignment with the smaller word below it,
   so centre it against the taller number instead. */
.title-icon {
  align-self: center;
  width: 20px;
  height: 20px;
  flex-shrink: 0;
  color: var(--accent);
}

.title-count {
  display: inline-block;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
  font-size: 1.5rem;
  font-weight: 800;
  line-height: 1.2;
}

.title-suffix {
  color: var(--text);
  font-size: 1rem;
  font-weight: 600;
}

.search {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.4rem;
  flex: 1;
  min-width: 0;
  max-width: 420px;
}

/* Collapsed: a labelled pill, so it reads as an address lookup rather than an
   unlabelled magnifier. Open: it shrinks back to a square close button. */
.search-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.3rem;
  height: 34px;
  padding: 0 0.6rem;
  border-radius: 999px;
}

.search-toggle-label {
  min-width: 0;
  overflow: hidden;
  font-size: 0.78rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.app-header.search-open .search-toggle {
  width: 34px;
  padding: 0;
}

.app-header.search-open .search-toggle-label {
  display: none;
}

.search-icon {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
}

.search-icon-close {
  display: none;
}

.app-header.search-open .search-icon-open {
  display: none;
}

.app-header.search-open .search-icon-close {
  display: block;
}

.search-form {
  display: flex;
  gap: 0.4rem;
  flex: 1;
  min-width: 0;
}

/* `hidden` needs to beat the display rule above. */
.search-form[hidden] {
  display: none;
}

.search-form input {
  flex: 1;
  min-width: 0;
  padding: 0.4rem 0.6rem;
  border-radius: 6px;
  border: 1px solid var(--panel-border);
  background: #0c1120;
  color: var(--text);
}

.search-form button {
  padding: 0.4rem 0.8rem;
}

button {
  cursor: pointer;
  border: 1px solid var(--panel-border);
  background: #1d2740;
  color: var(--text);
  border-radius: 6px;
  padding: 0.45rem 0.6rem;
  font-size: 0.85rem;
}

button:hover {
  background: #26304a;
}

button.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

button.danger {
  background: transparent;
  border-color: var(--danger);
  color: var(--danger);
}

button.danger:hover {
  background: rgba(239, 83, 80, 0.15);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

button:disabled:hover {
  background: #1d2740;
}

button.danger:disabled:hover {
  background: transparent;
}

.app-main {
  flex: 1;
  display: flex;
  min-height: 0;
}

.map-wrap {
  position: relative;
  flex: 1;
  min-width: 0;
}

#map-canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  background: #0a0e18;
}

.map-controls {
  position: absolute;
  top: 12px;
  right: 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.map-controls button {
  width: 34px;
  height: 34px;
  font-size: 1.1rem;
  line-height: 1;
}

/* Icon-only control, so it needs flex centring rather than the baseline layout
   the textual +/− buttons get away with. */
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.control-icon {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
}

/* Lock toggle: shows the current state and fills in while the map is locked. */
.lock-icon-locked {
  display: none;
}

body.map-locked .lock-icon-open {
  display: none;
}

body.map-locked .lock-icon-locked {
  display: block;
}

.icon-btn[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.hint {
  position: absolute;
  left: 12px;
  bottom: 12px;
  padding: 0.4rem 0.6rem;
  background: rgba(10, 14, 24, 0.75);
  border-radius: 6px;
  font-size: 0.8rem;
  color: var(--muted);
  pointer-events: none;
  max-width: 60%;
}

.tile-loading {
  position: absolute;
  top: 12px;
  left: 12px;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.4rem 0.7rem;
  background: rgba(10, 14, 24, 0.75);
  border-radius: 6px;
  font-size: 0.8rem;
  color: var(--text);
  pointer-events: none;
  opacity: 0;
  transform: translateY(-4px);
  transition:
    opacity 0.15s ease,
    transform 0.15s ease;
}

.tile-loading.visible {
  opacity: 1;
  transform: translateY(0);
}

.spinner {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.25);
  border-top-color: var(--accent);
  animation: spin 0.7s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.panel {
  width: 300px;
  flex-shrink: 0;
  background: var(--panel-bg);
  border-left: 1px solid var(--panel-border);
  padding: 1rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 1.1rem;
}

.panel-section h2 {
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  margin: 0 0 0.5rem;
}

.btn-group {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}

.mode-btn {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  gap: 0.4rem;
  justify-content: center;
}

.mode-icon {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
}

/* Step numbers on the mode buttons: the three modes are meant to be used in order. */
.step-badge {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.14);
  font-size: 0.65rem;
  font-weight: 600;
  line-height: 1;
}

/* Controls for the selected mode, nested directly under its mode button. */
.mode-sub {
  flex: 1 1 100%;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  padding: 0.5rem;
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  background: rgba(10, 14, 24, 0.45);
}

/* `hidden` needs to beat the display rule above. */
.mode-sub[hidden] {
  display: none;
}

.mode-sub:not([hidden]) {
  animation: sub-in 0.18s ease;
}

@keyframes sub-in {
  from {
    opacity: 0;
    transform: translateY(-4px);
  }
}

.sub-row {
  display: flex;
  gap: 0.35rem;
}

/* Denser than the mode buttons so the nested controls stay secondary. */
.sub-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.4rem 0.5rem;
  font-size: 0.78rem;
}

.sub-btn.primary {
  flex: 1;
  justify-content: center;
  border-color: var(--accent);
  color: var(--accent);
}

.sub-btn.primary:hover {
  background: #26304a;
}

.sub-icon {
  width: 13px;
  height: 13px;
  flex-shrink: 0;
}

.sub-note {
  margin: 0;
  color: var(--muted);
  font-size: 0.72rem;
}

/* Live guidance for the current step, driven by app state (see updateWorkflow). */
.next-step {
  display: flex;
  align-items: baseline;
  gap: 0.4rem;
  margin-top: 0.75rem;
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--panel-border);
  border-left: 3px solid var(--accent);
  border-radius: 6px;
  background: rgba(79, 140, 255, 0.09);
  font-size: 0.78rem;
  line-height: 1.35;
}

.next-step-label {
  flex-shrink: 0;
  color: var(--accent);
  font-size: 0.65rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  white-space: nowrap;
}

.density-group {
  flex-direction: column;
}

/* Density swatches stay locked until there is a grid to paint. */
.density-group.locked {
  opacity: 0.45;
  pointer-events: none;
}

.locked-note {
  margin: 0.5rem 0 0;
  padding: 0.4rem 0.55rem;
  border: 1px dashed var(--panel-border);
  border-radius: 6px;
  color: var(--muted);
  font-size: 0.75rem;
}

.density-btn {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  text-align: left;
  padding: 0.5rem;
  width: 100%;
}

.density-preview {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  border-radius: 6px;
  border: 1px solid var(--panel-border);
  background-color: #0c1120;
  background-repeat: repeat;
  background-position: center;
}

/* Dot patterns approximate real person-per-m^2 spacing at each density level:
   smaller/closer dots = more people packed into the same area. */
.density-preview-empty {
  background-image: none;
}
.density-preview-light {
  background-image: radial-gradient(
    circle,
    rgba(66, 165, 245, 0.9) 2.5px,
    transparent 2.6px
  );
  background-size: 16px 16px;
}
.density-preview-moderate {
  background-image: radial-gradient(
    circle,
    rgba(102, 187, 106, 0.9) 2.8px,
    transparent 2.9px
  );
  background-size: 13px 13px;
}
.density-preview-dense {
  background-image: radial-gradient(
    circle,
    rgba(255, 167, 38, 0.9) 3px,
    transparent 3.1px
  );
  background-size: 10px 10px;
}
.density-preview-verydense {
  background-image: radial-gradient(
    circle,
    rgba(239, 83, 80, 0.95) 3.4px,
    transparent 3.5px
  );
  background-size: 7px 7px;
}

.density-text {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  min-width: 0;
}

.density-name {
  font-weight: 600;
  font-size: 0.85rem;
}

.density-name em {
  font-style: normal;
  font-weight: 400;
  color: var(--muted);
}

.density-desc {
  font-size: 0.75rem;
  color: var(--muted);
  line-height: 1.3;
}

.density-btn.active .density-name em,
.density-btn.active .density-desc {
  color: rgba(255, 255, 255, 0.92);
}

.density-btn.active .density-preview {
  border-color: rgba(255, 255, 255, 0.6);
}

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

.density-intro {
  margin-bottom: 0.75rem;
}

.totals dl {
  margin: 0;
  display: grid;
  grid-template-columns: 1fr auto;
  row-gap: 0.35rem;
  column-gap: 0.5rem;
  align-items: baseline;
}

.totals dt {
  color: var(--muted);
  font-size: 0.85rem;
}

.totals dd {
  margin: 0;
  font-weight: 600;
  text-align: right;
}

.crowd-total {
  font-size: 1.4rem;
  color: var(--accent);
}

.share-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  width: 100%;
  margin-top: 0.75rem;
}

.share-icon {
  width: 14px;
  height: 14px;
  flex-shrink: 0;
}

/* Collapsed by default: the method behind the estimate, for anyone curious. */
.calc-details {
  margin-top: 0.75rem;
  padding-top: 0.6rem;
  border-top: 1px solid var(--panel-border);
  color: var(--muted);
  font-size: 0.78rem;
}

.calc-details summary {
  cursor: pointer;
  color: var(--accent);
  font-size: 0.78rem;
}

.calc-body {
  margin-top: 0.5rem;
}

.calc-body p {
  margin: 0 0 0.5rem;
  line-height: 1.45;
}

.calc-body ul {
  margin: 0 0 0.5rem;
  padding-left: 1.1rem;
}

.calc-body li {
  margin-bottom: 0.25rem;
  line-height: 1.45;
}

.calc-formula {
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.app-footer {
  text-align: center;
  font-size: 0.75rem;
  color: var(--muted);
  padding: 0.4rem;
  background: var(--panel-bg);
  border-top: 1px solid var(--panel-border);
}

.app-footer p {
  margin: 0;
}

.app-footer a {
  color: var(--accent);
}

/* Small "buy me a coffee" link, tucked under the issue button. */
.app-support {
  align-self: flex-end;
  margin: 0;
  font-size: 0.75rem;
}

.app-support a {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  color: var(--accent);
  text-decoration: none;
}

.app-support a:hover {
  text-decoration: underline;
}

.support-icon {
  width: 13px;
  height: 13px;
  flex-shrink: 0;
}

.panel-actions {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.panel-actions-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.4rem;
}

.issue-link {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.45rem 0.6rem;
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  background: #1d2740;
  color: var(--text);
  text-decoration: none;
  font-size: 0.85rem;
  white-space: nowrap;
}

.issue-link:hover {
  background: #26304a;
  border-color: var(--accent);
}

.issue-icon {
  width: 14px;
  height: 14px;
  flex-shrink: 0;
}

/* Mobile-only density palette; the panel's swatch list stays the desktop UI. */
.density-bar {
  display: none;
}

@media (max-width: 720px) {
  html,
  body {
    height: auto;
    min-height: 100%;
  }
  .app-main {
    flex-direction: column;
    min-height: 0;
  }
  .map-wrap {
    flex: 0 0 auto;
    height: 62vh;
  }
  /* Nested controls get a slightly larger tap target on touch screens. */
  .sub-btn {
    padding: 0.45rem 0.55rem;
  }

  .panel {
    width: 100%;
    border-left: none;
    border-top: 1px solid var(--panel-border);
    flex: 0 0 auto;
    max-height: none;
    overflow-y: visible;
  }

  /* Keep the full "Search Address" label readable on small screens. */
  .search-toggle {
    gap: 0.25rem;
    padding: 0 0.45rem;
  }

  .search-toggle-label {
    font-size: 0.72rem;
  }

  /* While the address field is open it takes the header, so drop the title. */
  .app-header.search-open h1 {
    display: none;
  }

  /* Swatch + short label per density, thumb-sized and always on screen while
     painting. The buttons share `data-density` with the panel list, so
     setDensity keeps both in sync. */
  .density-bar:not([hidden]) {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 30;
    display: flex;
    gap: 0.4rem;
    padding: 0.5rem 0.5rem calc(0.5rem + env(safe-area-inset-bottom));
    background: var(--panel-bg);
    border-top: 1px solid var(--panel-border);
  }

  .density-bar .density-btn {
    flex: 1;
    width: auto;
    min-width: 0;
    flex-direction: column;
    gap: 0.25rem;
    padding: 0.35rem 0.2rem;
    text-align: center;
  }

  .density-bar .density-preview {
    width: 26px;
    height: 26px;
  }

  .density-bar-label {
    color: var(--muted);
    font-size: 0.62rem;
    line-height: 1.1;
  }

  .density-bar .density-btn.active .density-bar-label {
    color: rgba(255, 255, 255, 0.92);
  }

  /* Keep the page clear of the fixed palette while it is on screen. */
  body.has-density-bar .app-footer {
    padding-bottom: calc(4.5rem + env(safe-area-inset-bottom));
  }
}
