/* Monochrome palette: black sections, white type, gray (#969696) secondary
   text, white buttons. Type matches the Mad Man Data site (madmandata.pages.dev):
   Montserrat 600-800 for headings/labels (uppercase), Inter for body. */
:root {
  --bg: #000000;
  --surface: #161616;
  --surface-2: #1b1b1b;
  --border: #3a3a3a;
  --rule: #898989;
  --text: #ffffff;
  --muted: #969696;
  --accent: #ffffff;
  --accent-ink: #000000;
  --danger: #e5484d;
  --aoi: #ffffff;
  --radius: 4px;
  --shadow: 0 6px 24px rgba(0, 0, 0, .55);
  --panel-w: 340px;
  --topbar-h: 60px;
  --font-body: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --font-head: "Montserrat", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --font-label: var(--font-head);
  font-family: var(--font-body);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }

/* ---------- top bar ---------- */
.topbar {
  height: var(--topbar-h);
  display: flex; align-items: center; gap: 16px;
  padding: 0 16px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  position: relative; z-index: 1001;
}
.brand { display: flex; align-items: center; gap: 12px; white-space: nowrap; color: var(--text); text-decoration: none; }
/* Same lockup as the site header: bold wordmark over a small tracked subtitle. */
.brand-logo { width: 46px; height: 46px; border-radius: 8px; flex: none; }
.brand-mark {
  display: block; margin-bottom: 2px;
  font-family: var(--font-head); font-weight: 800; font-size: 16.8px; letter-spacing: .02em;
  text-transform: uppercase;
}
.brand-app {
  display: block; font-family: var(--font-body); font-size: 9.6px; color: var(--muted);
  text-transform: uppercase; letter-spacing: .24em;
}

.search { position: relative; flex: 1; min-width: 0; max-width: 520px; display: flex; }
.search input {
  flex: 1; min-width: 0;
  height: 38px; padding: 0 12px;
  background: var(--surface-2); border: 1px solid var(--border); border-right: 0;
  border-radius: var(--radius) 0 0 var(--radius);
  outline: none;
}
.search input:focus { border-color: var(--accent); }
.search button {
  width: 44px; height: 38px;
  background: var(--accent); color: var(--accent-ink);
  border: 0; border-radius: 0 var(--radius) var(--radius) 0;
  display: grid; place-items: center;
}
.results {
  position: absolute; top: 44px; left: 0; right: 0;
  margin: 0; padding: 4px; list-style: none;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); max-height: 60vh; overflow: auto;
}
.results li { padding: 8px 10px; border-radius: 6px; cursor: pointer; font-size: 14px; line-height: 1.35; }
.results li:hover, .results li.active { background: var(--surface-2); }
.results li small { display: block; color: var(--muted); font-size: 12px; text-transform: capitalize; }
.results li.msg { cursor: default; color: var(--muted); }

.panel-toggle {
  display: none; margin-left: auto; flex: none; white-space: nowrap;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius);
  height: 36px; padding: 0 12px;
}
.panel-toggle span { background: var(--accent); color: var(--accent-ink); border-radius: 999px; padding: 0 7px; margin-left: 4px; font-size: 12px; font-weight: 700; }

/* ---------- layout ---------- */
.layout { position: relative; height: calc(100% - var(--topbar-h)); display: flex; }
#map { flex: 1; height: 100%; background: #0b1016; }

.toolbar {
  position: absolute; top: 12px; left: 12px; z-index: 1000;
  display: flex; flex-direction: column; gap: 4px; padding: 4px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.tool {
  width: 58px; padding: 7px 0 5px;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  background: transparent; border: 0; border-radius: 7px;
  font-family: var(--font-label); font-size: 9.5px; font-weight: 600;
  text-transform: uppercase; letter-spacing: .08em; color: var(--muted);
}
.tool svg { width: 22px; height: 22px; }
.tool:hover { background: var(--surface-2); color: var(--text); }
.tool.active { background: var(--accent); color: var(--accent-ink); }
.tool[data-tool="remove"].active { background: var(--danger); color: #fff; }

/* push Leaflet's zoom control out from under the toolbar */
.leaflet-top.leaflet-left { top: 256px; }

.hint {
  position: absolute; top: 12px; left: calc((100% - var(--panel-w)) / 2); transform: translateX(-50%);
  z-index: 1000; max-width: calc(100% - var(--panel-w) - 180px);
  padding: 8px 14px; border-radius: var(--radius);
  background: rgba(0, 0, 0, .85); border: 1px solid var(--border);
  font-size: 13px; text-align: center; pointer-events: none;
}
.coords {
  position: absolute; bottom: 24px; left: 12px; z-index: 1000;
  padding: 3px 8px; border-radius: 6px;
  background: rgba(0, 0, 0, .8); font: 12px ui-monospace, Consolas, monospace; color: var(--muted);
  pointer-events: none;
}
.coords:empty { display: none; }

/* ---------- side panel ---------- */
.panel {
  width: var(--panel-w); height: 100%;
  display: flex; flex-direction: column;
  background: var(--surface); border-left: 1px solid var(--border);
}
.panel-head { display: flex; align-items: flex-end; justify-content: space-between; padding: 18px 16px 10px; }
.panel-head h2 {
  margin: 0; font-family: var(--font-head); font-size: 15px; font-weight: 800; color: var(--muted);
  text-transform: uppercase; letter-spacing: .02em; line-height: 1.08;
}
.panel-head h2::after { content: ""; display: block; width: 40px; height: 1px; margin-top: 10px; background: var(--rule); }
.link-btn { background: none; border: 0; color: var(--danger); font-size: 13px; padding: 4px; }
.link-btn:disabled { color: var(--muted); opacity: .5; cursor: default; }
.totals { padding: 0 16px 8px; color: var(--muted); font-size: 12px; }
.totals:empty { display: none; }

.panel-scroll { flex: 1; min-height: 0; overflow-y: auto; }
.aoi-list { list-style: none; margin: 0; padding: 0 12px; }
.ref-section { margin-top: 8px; padding-bottom: 12px; border-top: 1px solid var(--border); }
.ref-section .panel-head { flex-wrap: wrap; gap: 12px; }
.ref-section .panel-head h2 { flex: 1 0 100%; }
.outline-btn {
  display: flex; align-items: center; gap: 6px; height: 30px; padding: 0 10px; white-space: nowrap;
  background: transparent; color: var(--text); border: 1px solid var(--text); border-radius: var(--radius);
  font-family: var(--font-label); font-weight: 700; font-size: 10px; text-transform: uppercase; letter-spacing: .1em;
}
.outline-btn:hover { background: var(--accent); color: var(--accent-ink); }
.outline-btn:disabled { opacity: .4; cursor: wait; }
.ref-swatch { width: 12px; height: 12px; border: 1.5px dashed var(--text); border-radius: 2px; flex: none; }
.ref-name { flex: 1; min-width: 0; padding: 3px 6px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.aoi.ref.off { opacity: .5; }
.icon-btn.vis[aria-pressed="false"] { color: var(--border); }
.aoi {
  margin-bottom: 10px; padding: 10px;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius);
}
.aoi.selected { border-color: var(--accent); }
.aoi-row { display: flex; align-items: center; gap: 6px; }
.aoi-swatch { width: 12px; height: 12px; border-radius: 3px; background: var(--aoi); flex: none; }
.aoi-name {
  flex: 1; min-width: 0;
  background: transparent; border: 1px solid transparent; border-radius: 6px;
  padding: 3px 6px; font-weight: 600;
}
.aoi-name:hover { border-color: var(--border); }
.aoi-name:focus { border-color: var(--accent); outline: none; background: var(--bg); }
.icon-btn {
  width: 28px; height: 28px; display: grid; place-items: center;
  background: transparent; border: 0; border-radius: 6px; color: var(--muted);
}
.icon-btn:hover { background: var(--bg); color: var(--text); }
.icon-btn.del:hover { color: var(--danger); }
.icon-btn svg { width: 16px; height: 16px; }
.aoi-stats { display: flex; flex-wrap: wrap; gap: 4px 12px; margin: 6px 0 8px 18px; font-size: 12px; color: var(--muted); }
.aoi-stats b { color: var(--text); font-weight: 600; }
.aoi-note {
  width: 100%; min-height: 54px; resize: vertical;
  padding: 6px 8px; font-size: 13px; line-height: 1.4;
  background: var(--bg); border: 1px solid var(--border); border-radius: 6px; outline: none;
}
.aoi-note:focus { border-color: var(--accent); }
.empty { padding: 0 16px; color: var(--muted); font-size: 13px; line-height: 1.5; }

.downloads { padding: 12px 16px 16px; border-top: 1px solid var(--border); }
.filename { display: block; font-size: 12px; color: var(--muted); margin-bottom: 10px; }
.filename input {
  display: block; width: 100%; margin-top: 4px; height: 34px; padding: 0 10px;
  background: var(--bg); border: 1px solid var(--border); border-radius: 6px; outline: none;
}
.filename input:focus { border-color: var(--accent); }
.dl-buttons { display: flex; gap: 8px; }
.primary {
  flex: 1; height: 38px;
  display: flex; align-items: center; justify-content: center; gap: 6px;
  background: var(--accent); color: var(--accent-ink);
  border: 0; border-radius: var(--radius);
  font-family: var(--font-label); font-weight: 700; font-size: 12px;
  text-transform: uppercase; letter-spacing: .12em;
}
.primary:hover:not(:disabled) { filter: brightness(1.08); }
.primary:disabled { opacity: .4; cursor: not-allowed; }

.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translate(-50%, 20px);
  z-index: 2000; padding: 10px 16px; border-radius: var(--radius);
  background: var(--surface); border: 1px solid var(--border); box-shadow: var(--shadow);
  font-size: 14px; opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s;
  width: max-content; max-width: min(560px, calc(100% - 32px)); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Leaflet theming ---------- */
/* Let clicks through the AOI SVG layer's empty areas to reference layers below. */
.leaflet-overlay-pane > svg { pointer-events: none; }
.leaflet-control-layers, .leaflet-bar a {
  background: var(--surface) !important; color: var(--text) !important; border-color: var(--border) !important;
}
.leaflet-control-layers { border: 1px solid var(--border) !important; border-radius: var(--radius) !important; }
.leaflet-control-layers-toggle { filter: invert(1) hue-rotate(180deg); }
.leaflet-control-layers-expanded { padding: 8px 12px; }
.leaflet-control-scale-line { background: rgba(0,0,0,.75); color: var(--text); border-color: var(--muted); }
.leaflet-control-attribution { background: rgba(0,0,0,.7) !important; color: var(--muted); }
.leaflet-control-attribution a { color: #9ecbff; }
.leaflet-tooltip.aoi-label {
  background: rgba(0,0,0,.85); color: var(--text); border: 0; box-shadow: none;
  font-family: var(--font-label); font-weight: 600; font-size: 11px; letter-spacing: .04em; padding: 2px 6px;
}
.leaflet-tooltip.aoi-label::before { display: none; }

.drop-overlay {
  position: absolute; inset: 0; right: var(--panel-w); z-index: 1500;
  display: grid; place-items: center;
  background: rgba(0, 0, 0, .7); border: 2px dashed var(--text);
  font-family: var(--font-head); font-size: 18px; font-weight: 800; text-transform: uppercase;
  text-align: center; pointer-events: none;
}
.drop-overlay small { display: block; margin-top: 6px; font-family: var(--font-body); font-size: 13px; color: var(--muted); }

.leaflet-popup-content-wrapper, .leaflet-popup-tip { background: var(--surface); color: var(--text); border-radius: var(--radius); }
.leaflet-container a.leaflet-popup-close-button { color: var(--muted); }
.leaflet-popup-content { margin: 10px 12px; font-family: var(--font-body); }
.attr-title { font-family: var(--font-label); font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: .1em; color: var(--muted); margin-bottom: 6px; }
.attr-table { border-collapse: collapse; font-size: 12px; max-width: 300px; }
.attr-wrap { max-height: 240px; overflow: auto; }
.attr-table th { text-align: left; color: var(--muted); font-weight: 600; padding: 2px 10px 2px 0; vertical-align: top; white-space: nowrap; }
.attr-table td { padding: 2px 0; word-break: break-word; }

/* ---------- small screens ---------- */
@media (max-width: 760px) {
  .brand-text { display: none; }
  .brand-logo { width: 40px; height: 40px; }
  .drop-overlay { right: 0; }
  .topbar { gap: 10px; padding: 0 12px; }
  .panel-toggle { display: block; }
  .panel {
    position: absolute; left: 0; right: 0; bottom: 0; width: auto; height: 55%;
    z-index: 1001; border-left: 0; border-top: 1px solid var(--border);
    border-radius: 14px 14px 0 0; box-shadow: var(--shadow);
    transform: translateY(100%); transition: transform .25s;
  }
  .panel.open { transform: translateY(0); }
  .hint { top: auto; bottom: 56px; left: 50%; max-width: calc(100% - 32px); }
  .coords { display: none; }
}
