:root {
  --bg: #0f1115;
  --panel: #1a1e27;
  --panel-2: #232936;
  --text: #e7ebf2;
  --muted: #9aa4b2;
  --accent: #3b82f6;
  --accent-2: #2563eb;
  --border: #2c3342;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  background: var(--bg); color: var(--text);
  display: flex; flex-direction: column; height: 100vh;
}
.hidden { display: none !important; }

/* App fills the viewport as a flex column so #map can grow to fill it. */
#app { flex: 1; display: flex; flex-direction: column; min-height: 0; }

/* Login */
.overlay {
  position: fixed; inset: 0; background: rgba(8,10,14,.9);
  display: flex; align-items: center; justify-content: center; z-index: 1000;
}
.card {
  background: var(--panel); border: 1px solid var(--border); border-radius: 12px;
  padding: 28px; width: min(360px, 90vw); display: flex; flex-direction: column; gap: 12px;
}
.card h1 { font-size: 20px; margin: 0; }
.card p { color: var(--muted); margin: 0; font-size: 14px; }
.error { color: #f87171; font-size: 13px; min-height: 16px; }
.guide-link { color: var(--muted); font-size: 13px; text-align: center; text-decoration: underline; }
.guide-link:hover { color: var(--text); }

/* Layout */
.topbar {
  display: flex; align-items: center; gap: 14px; padding: 10px 16px;
  background: var(--panel); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; letter-spacing: .3px; white-space: nowrap; }
.inputs { display: flex; gap: 8px; flex: 1; align-items: center; flex-wrap: wrap; }
#address { flex: 1; min-width: 240px; }
.county-note {
  flex-basis: 100%; margin: 2px 2px 0; color: var(--muted); font-size: 12px; line-height: 1.4;
}
.county-note strong { color: var(--text); font-weight: 600; }
.file-label { cursor: pointer; }
.file-label span {
  display: inline-block; padding: 8px 12px; background: var(--panel-2);
  border: 1px solid var(--border); border-radius: 8px; font-size: 13px; white-space: nowrap;
}

.modes {
  display: flex; gap: 8px; padding: 8px 16px; background: var(--panel);
  border-bottom: 1px solid var(--border); align-items: center;
}
.mode {
  background: var(--panel-2); border: 1px solid var(--border); color: var(--text);
}
.mode.active { background: var(--accent); border-color: var(--accent); }
.mode:disabled { opacity: .4; cursor: not-allowed; }
.hint, .status { color: var(--muted); font-size: 13px; }

.controls {
  display: flex; align-items: center; gap: 14px; padding: 10px 16px;
  background: var(--panel); border-top: 1px solid var(--border); flex-wrap: wrap;
}
.controls label { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
.spacer { flex: 1; }

/* Rotate controls: a wider slider (less touchy) plus 1° nudge + reset buttons. */
.rot-control { flex-wrap: nowrap; }
#img-rotate { width: 170px; }
#img-rotate-control #img-rotate-val { min-width: 34px; text-align: right; color: var(--text); }
.rot-nudge, .rot-reset {
  padding: 3px 8px; min-width: 26px; font-size: 14px; line-height: 1;
}

input[type="text"], input[type="password"] {
  padding: 9px 11px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--panel-2); color: var(--text); font-size: 14px;
}
button, .button {
  padding: 9px 14px; border-radius: 8px; border: 1px solid var(--accent);
  background: var(--accent); color: white; font-size: 13px; cursor: pointer;
  text-decoration: none; display: inline-block;
}
button:hover, .button:hover { background: var(--accent-2); }
button.ghost, .button.ghost {
  background: var(--panel-2); border-color: var(--border); color: var(--text);
}
button.ghost:hover, .button.ghost:hover { background: #2b3342; }
input[type="color"] { width: 34px; height: 28px; padding: 0; border: none; background: none; }
select {
  padding: 7px 9px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--panel-2); color: var(--text); font-size: 13px; cursor: pointer;
}
.export-fmt { color: var(--muted); }

#transform-btn.active, #reference-btn.active, #keypoint-btn.active, #boxwarp-btn.active {
  background: var(--accent); border-color: var(--accent); color: #fff;
}

/* Free-corner "box warp" handles (cyan squares). */
.box-handle {
  background: #22d3ee; border: 2px solid #fff; box-sizing: border-box;
  width: 16px; height: 16px; border-radius: 3px;
  box-shadow: 0 1px 4px rgba(0,0,0,.5); cursor: grab;
}
.box-handle:active { cursor: grabbing; }
/* Mid-edge handles: round, so they read as "move a side" vs the square corners. */
.box-edge-handle { width: 14px; height: 14px; border-radius: 50%; }

/* Keypoint-pair placement: numbered pins + inline control bar. */
.kp-bar { display: inline-flex; align-items: center; gap: 8px; }
.kp-pin {
  display: flex; align-items: center; justify-content: center;
  border-radius: 50%; border: 2px solid #fff; color: #fff;
  font-size: 12px; font-weight: 700; box-shadow: 0 1px 4px rgba(0,0,0,.6);
}
.kp-pin.ref { background: var(--accent); }   /* blue  = reference (left) */
.kp-pin.photo { background: #22c55e; }        /* green = photo (right) */
.kp-pin.pending { background: #f59e0b; }       /* amber = awaiting its photo match */

/* Constrained-transform handles (Leaflet divIcons). */
.cage-handle, .cage-move, .cage-rotate, .cage-tilt {
  background: var(--accent); border: 2px solid #fff; box-sizing: border-box;
  box-shadow: 0 1px 4px rgba(0,0,0,.5); cursor: grab;
}
.cage-handle:active, .cage-move:active, .cage-rotate:active, .cage-tilt:active { cursor: grabbing; }
.cage-handle { width: 14px; height: 14px; border-radius: 3px; }
.cage-move, .cage-rotate, .cage-tilt {
  width: 20px; height: 20px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-size: 13px; line-height: 1;
}
.cage-rotate { background: #22c55e; }  /* green = rotate */
.cage-tilt { background: #a855f7; }    /* purple = tilt (legacy) */

/* Tilt-direction arrow: aim the perspective independently of the shape rotation. */
.cage-dir {
  width: 24px; height: 24px; border-radius: 50%;
  background: #f59e0b; border: 2px solid #fff; box-sizing: border-box;
  box-shadow: 0 1px 4px rgba(0,0,0,.5); cursor: grab;
  display: flex; align-items: center; justify-content: center;
}
.cage-dir:active { cursor: grabbing; }
.dir-arrow { color: #1a1e27; font-size: 15px; line-height: 1; font-weight: 700; }

/* Stage: reference panel beside the editable map. */
#stage { flex: 1; display: flex; min-height: 0; }
#map { flex: 1; min-width: 0; min-height: 0; background: #0a0c10; }
/* Wrapper around Leaflet's map-pane so we can rotate the *view* (photo + overlays)
   with a CSS transform, without rotating the zoom/draw controls and without truly
   re-projecting the map. Pointer input is inverse-rotated in app.js so editing still
   works at any angle. Fills the container; rotated content is clipped by #map. */
.leaflet-rot-wrap { position: absolute; inset: 0; transform-origin: 50% 50%; }
#reference {
  width: 40%; min-width: 260px; display: flex; flex-direction: column;
  border-right: 1px solid var(--border); overflow: hidden;
}
#reference.hidden { display: none; }
/* The map is oversized inside a clipped viewport so it still covers the panel
   when spun to an arbitrary angle (no empty corners). */
.ref-viewport { flex: 1; min-height: 0; position: relative; overflow: hidden; background: #0a0c10; }
#ref-map {
  position: absolute; top: -25%; left: -25%; width: 150%; height: 150%;
  transform-origin: center; background: #0a0c10;
}
.ref-caption {
  display: flex; align-items: center; gap: 10px; justify-content: space-between;
  padding: 6px 10px; font-size: 12px; color: var(--muted);
  background: var(--panel); border-bottom: 1px solid var(--border);
}
.ref-caption > span { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ref-rot { display: flex; align-items: center; gap: 6px; white-space: nowrap; }
.ref-rot input[type="range"] { width: 120px; }
.ref-rot #ref-rotate-val { min-width: 34px; text-align: right; }

/* ============================================================================
   Responsive / touch
   The app was desktop-only: the reference panel sat beside the map with a 260px
   floor, which left almost nothing for the editor on a phone. Everything below is
   additive and scoped to media queries, so the desktop layout is untouched.
   ========================================================================= */

/* iOS reports 100vh as the *largest* viewport (chrome hidden), so a 100vh column
   pushes the bottom controls under the browser UI where they can't be tapped.
   dvh tracks the visible viewport; the 100vh line stays as the fallback. */
@supports (height: 100dvh) {
  body { height: 100dvh; }
}

/* --- Tablet / small laptop: stack the reference panel above the editor ------ */
@media (max-width: 900px) {
  #stage { flex-direction: column; }
  #reference {
    width: auto; min-width: 0; max-height: 38vh;
    border-right: none; border-bottom: 1px solid var(--border);
  }
  /* Side by side both panes shared the height; stacked they share the width, so the
     editor needs its own floor or a tall reference squeezes it to nothing. */
  #map { min-height: 45vh; }
}

/* --- Phone ----------------------------------------------------------------- */
@media (max-width: 600px) {
  .topbar {
    flex-wrap: wrap; gap: 8px; padding: 8px 10px;
  }
  .brand { font-size: 15px; }
  .inputs { flex-basis: 100%; gap: 6px; }
  /* 240px + the Locate button overflows a 390px screen; let the field take the row. */
  #address { min-width: 0; flex: 1 1 100%; }
  .county-note { font-size: 11px; }

  .modes, .controls { padding: 8px 10px; gap: 6px; }
  .controls { gap: 8px 10px; }
  /* The status line is the app's only feedback channel — never let it be squeezed out. */
  .status { flex-basis: 100%; order: 99; }
  .spacer { display: none; }

  /* Sliders are the first thing to overflow a narrow toolbar. */
  #img-rotate { width: 100%; max-width: 200px; }
  .rot-control { flex-wrap: wrap; }
  .ref-rot input[type="range"] { width: 90px; }
  .ref-caption { padding: 6px 8px; font-size: 11px; }

  #reference { max-height: 34vh; }
}

/* --- Touch devices: hit targets ------------------------------------------- */
/* Two conditions, deliberately OR'd: `pointer: coarse` catches touchscreen laptops and
   tablets at any width, and the width clause covers phones whose browser reports a fine
   pointer (some Android and hybrid devices do). Either alone leaves a real gap. */
@media (pointer: coarse), (max-width: 600px) {
  button, .button, .file-label span, select {
    min-height: 44px; padding: 11px 14px;
  }
  /* Safari zooms the whole page when focusing an input under 16px. That reflows the
     map mid-interaction and is the single most disorienting mobile bug here. */
  input[type="text"], input[type="password"], select { font-size: 16px; }

  /* Dragging a 14px handle with a fingertip is the core editing gesture — these are
     sized for a mouse cursor, not a finger (~9mm contact patch). */
  .cage-handle { width: 22px; height: 22px; }
  .cage-move, .cage-rotate, .cage-tilt { width: 30px; height: 30px; font-size: 16px; }
  .cage-dir { width: 34px; height: 34px; }
  .box-handle { width: 24px; height: 24px; }
  .box-edge-handle { width: 22px; height: 22px; }
  .kp-pin { min-width: 26px; min-height: 26px; }

  /* Nudge buttons sit in a row and would otherwise inherit the 44px min above,
     blowing out the toolbar; keep them compact but still tappable. */
  .rot-nudge, .rot-reset { min-height: 36px; padding: 6px 10px; }

  /* A 16px-tall slider is the hardest thing on the page to grab. Fattening the thumb
     matters more than the track, so size both. */
  input[type="range"] { height: 32px; }
  input[type="range"]::-webkit-slider-thumb { width: 22px; height: 22px; }
  input[type="color"] { width: 44px; height: 36px; }
}

/* County request: a quiet inline link in the coverage note, opening a small modal. */
.linkish {
  background: none; border: none; padding: 0; margin-left: 4px;
  color: var(--accent); font-size: inherit; text-decoration: underline;
  cursor: pointer; display: inline;
}
.linkish:hover { background: none; color: var(--text); }
/* The coverage note is muted text; the shared button rule would give this a 44px
   min-height on touch and blow the paragraph apart. */
@media (pointer: coarse), (max-width: 600px) {
  .linkish { min-height: 0; padding: 0; }
}
.field-row { display: flex; gap: 8px; }
.field-row > * { flex: 1; min-width: 0; }
#county-modal .card input[type="text"] { width: 100%; }
/* Reuses .error for the result line; .ok recolors it for the success case. */
.error.ok { color: #4ade80; }

/* Feedback modal: type chips + free text. */
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  background: var(--panel-2); border: 1px solid var(--border); color: var(--muted);
  font-size: 13px; padding: 7px 11px; flex: 1; white-space: nowrap;
}
.chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.chip:hover { color: var(--text); }
.chip.active:hover { background: var(--accent-2); }
textarea {
  padding: 9px 11px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--panel-2); color: var(--text); font-size: 14px;
  font-family: inherit; width: 100%; resize: vertical; min-height: 90px;
}
#feedback-modal .card input[type="text"] { width: 100%; }
/* Transparency line: names the context being attached, so shipping the user's address and
   user agent isn't a surprise. */
.hint-sm { color: var(--muted); font-size: 11.5px; line-height: 1.45; margin: 0; }

@media (pointer: coarse), (max-width: 600px) {
  /* Same iOS zoom-on-focus trap as the other inputs. */
  textarea { font-size: 16px; }
  /* Three chips don't fit one row on a narrow phone; let them wrap to two. */
  .chip { flex: 1 1 auto; }
}
