/* ==========================================================================
   Virtual fence estimator
   Map, drawing tools and estimate panel. Loaded only on /fence-estimator/.
   ========================================================================== */

.est-section {
  padding-top: clamp(28px, 4vw, 48px);
  background: var(--wash);
}

.est {
  display: grid;
  grid-template-columns: minmax(0, 1.55fr) minmax(340px, 0.85fr);
  gap: clamp(18px, 2.4vw, 30px);
  align-items: start;
}

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

.est__map-shell {
  position: relative;
  isolation: isolate;
  z-index: 0;
  border: 1px solid var(--line-dark);
  box-shadow: var(--shadow);
  background: var(--charcoal);
  overflow: hidden;
}

.est__map {
  height: clamp(430px, 58vh, 640px);
  background: var(--charcoal);
  z-index: 0;
  transition: opacity 0.2s ease;
}

/* Improve tile rendering - ensure crisp lines at all zooms */
.est__map .leaflet-tile {
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
}

.est__map.is-drawing,
.est__map.is-drawing .leaflet-interactive { cursor: crosshair; }
.est__map.is-gating,
.est__map.is-gating .leaflet-interactive { cursor: cell; }

.est__search {
  position: absolute;
  z-index: 600;
  top: 12px;
  left: 12px;
  right: 12px;
  display: flex;
  align-items: stretch;
  gap: 8px;
  pointer-events: none;
}

.est__search > * { pointer-events: auto; }

.est__search input {
  flex: 1 1 auto;
  min-width: 0;
  font: inherit;
  font-size: 0.95rem;
  padding: 10px 12px;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.97);
  border: 1px solid rgba(255, 255, 255, 0.6);
  border-radius: var(--radius);
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.2);
}

.est__search input::placeholder { color: #6d6e77; }

.est__locate {
  flex: 0 0 44px;
  width: 44px;
  font-size: 1.25rem;
  line-height: 1;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.97);
  border: 1px solid rgba(255, 255, 255, 0.6);
  border-radius: var(--radius);
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.2);
  cursor: pointer;
}

.est__locate:hover { background: var(--gold); }

/* Leaflet controls sit below the search row. */
.est__map .leaflet-top.leaflet-right { top: 58px; right: 4px; }
.est__map .leaflet-bar { border: 0; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.28); }
.est__map .leaflet-bar a {
  width: 36px;
  height: 36px;
  line-height: 36px;
  font-size: 1.2rem;
  background: #fff;
  color: #1b1c1f;
  border-bottom-color: var(--line);
}
.est__map .leaflet-bar a:hover { background: var(--wash); color: #1b1c1f; }

.est__map.leaflet-container {
  font-family: var(--font);
  background: var(--charcoal);
}

.est__map .leaflet-control-attribution {
  font-size: 10px;
  line-height: 1.35;
  padding: 2px 6px;
  color: #2a2b2f;
  background: rgba(255, 255, 255, 0.86);
}

.est__map .leaflet-control-attribution a { color: #2a2b2f; }

.est__legend {
  position: absolute;
  z-index: 600;
  left: 12px;
  bottom: 26px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: #fff;
  background: rgba(27, 28, 31, 0.86);
  border-radius: var(--radius);
  pointer-events: none;
  display: block;
}

.est__legend.hidden {
  display: none;
}

.est__swatch {
  display: inline-block;
  width: 22px;
  height: 4px;
  background: var(--gold);
}

/* Drawing handles and gate labels */
.est-vertex { background: transparent; border: 0; }
.est-vertex span {
  display: block;
  width: 14px;
  height: 14px;
  margin: 1px;
  background: var(--gold);
  border: 2px solid #1b1c1f;
  border-radius: 50%;
  box-shadow: 0 0 0 2px rgba(239, 187, 32, 0.35), 0 2px 6px rgba(0, 0, 0, 0.4);
}
.est-vertex.is-first span { background: #fff; }

.est-gate-icon { background: transparent; border: 0; }
.est-gate-icon span {
  display: block;
  padding: 0 4px;
  font-size: 10px;
  font-weight: 800;
  line-height: 18px;
  text-align: center;
  white-space: nowrap;
  color: var(--gold);
  background: rgba(27, 28, 31, 0.94);
  border: 2px solid var(--gold);
  border-radius: 2px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.35);
}

.est__toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}

.est__hint {
  min-height: 1.4em;
  margin: 10px 0 0;
  font-size: 0.9rem;
  color: var(--body);
  background: rgba(255, 255, 255, 0.8);
  padding: 6px 10px;
  border-radius: var(--radius);
  display: inline-block;
}

/* Make the hint persist and be readable */
.est__hint[aria-live="polite"] {
  animation: fadein 0.3s ease-out;
}

@keyframes fadein {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ============================================================
   ENHANCED VISUAL EFFECTS
   Progressively enhanced; all respecting prefers-reduced-motion
   ============================================================ */

/* --- Line draw pulse when active --- */
.est__map.is-drawing .leaflet-overlay-pane path {
  animation: linePulse 2.2s ease-in-out infinite;
}

@keyframes linePulse {
  0%, 100% { filter: drop-shadow(0 0 0 rgba(239,187,32,0)); }
  50% { filter: drop-shadow(0 0 4px rgba(239,187,32,0.8)); }
}

/* --- Gate icon bounce on placement --- */
.est-gate-icon {
  animation: gatePopIn 0.3s cubic-bezier(0.68, -0.55, 0.27, 1.55);
}

@keyframes gatePopIn {
  0% { transform: scale(0.4); opacity: 0; }
  60% { transform: scale(1.2); opacity: 1; }
  100% { transform: scale(1); opacity: 1; }
}

/* --- Vertex size pulse when drawing (helps users see click targets) --- */
.est__map.is-drawing .est-vertex span {
  animation: vertexPulse 1.6s ease-in-out infinite;
}

@keyframes vertexPulse {
  0%, 100% { transform: scale(1); box-shadow: 0 0 0 2px rgba(239,187,32,0.35), 0 2px 6px rgba(0,0,0,0.4); }
  50% { transform: scale(1.25); box-shadow: 0 0 0 3px rgba(239,187,32,0.55), 0 3px 8px rgba(0,0,0,0.5); }
}

/* --- Total price slide-up when value changes --- */
.est__total {
  animation: totalSlide 0.35s ease-out;
}

@keyframes totalSlide {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

/* --- Summary card entrance --- */
.est__summary {
  animation: summaryIn 0.4s ease-out;
}

@keyframes summaryIn {
  from { opacity: 0; transform: translateY(-8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* --- Search loading spinner on Find button --- */
.est__search .btn.btn--gold.is-loading {
  position: relative;
  color: transparent;
  pointer-events: none;
}

.est__search .btn.btn--gold.is-loading::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 16px;
  height: 16px;
  margin: -8px 0 0 -8px;
  border: 2px solid rgba(27,23,0,0.25);
  border-top-color: #1c1700;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

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

/* --- Success flash on status text --- */
.est__status.is-ok {
  animation: successFlash 0.5s ease-out;
}

@keyframes successFlash {
  0% { background: rgba(239,187,32,0.3); }
  100% { background: transparent; }
}

/* --- Subtle map shell hover lift --- */
.est__map-shell:hover {
  box-shadow: 0 16px 48px rgba(17, 18, 21, 0.18);
}

/* --- Vertex hover feedback --- */
.est-vertex:hover span {
  transform: scale(1.3);
  transition: transform 0.15s ease;
}

.est-vertex span {
  transition: transform 0.15s ease;
}

/* --- Summary block on hover — subtle gold accent --- */
.est__summary {
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.est__summary:hover {
  box-shadow: inset 0 0 0 1px rgba(239, 187, 32, 0.2);
}

/* --- Gate list item hover --- */
.est__gate-list li {
  transition: background 0.15s ease, transform 0.15s ease;
}

.est__gate-list li:hover {
  background: var(--wash);
  transform: translateX(2px);
}

/* --- Search input focus glow --- */
.est__search input:focus,
.est__locate:focus {
  outline: none;
  border-color: var(--gold);
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.2), 0 0 0 2px rgba(239, 187, 32, 0.35);
}

/* --- Toolbar button hover lift --- */
.est__toolbar .btn:not([disabled]):hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(17, 18, 21, 0.12);
}

.est__toolbar .btn:not([disabled]):active {
  transform: translateY(0);
  box-shadow: 0 1px 4px rgba(17, 18, 21, 0.15);
}

/* ============================================================
   REDUCED MOTION — disable all animations above
   ============================================================ */
@media (prefers-reduced-motion: reduce) {
  .est__map.is-drawing .leaflet-overlay-pane path,
  .est-gate-icon,
  .est__map.is-drawing .est-vertex span,
  .est__total,
  .est__summary,
  .est__status.is-ok,
  .est__search .btn.btn--gold.is-loading::after {
    animation: none !important;
  }

  .est__search input:focus,
  .est__locate:focus,
  .est-vertex:hover span,
  .est-vertex span,
  .est__summary,
  .est__gate-list li,
  .est__map-shell:hover,
  .est__toolbar .btn:not([disabled]):hover,
  .est__toolbar .btn:not([disabled]):active {
    transition: none !important;
    transform: none !important;
  }
}

.est__map-note {
  margin: 8px 0 0;
  font-size: 0.8rem;
  color: var(--muted);
}

.est__map-note a { color: var(--gold-ink); }

.est__noscript {
  margin: 0 0 18px;
  padding: 12px 16px;
  background: #fff4cf;
  border-left: 4px solid var(--gold);
}

.est__map--off {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.est__map-fallback {
  max-width: 420px;
  margin: 0;
  font-weight: 600;
  color: #d7d8de;
  text-align: center;
}

/* ----------------------------------------------------------------- panel */

.est__panel {
  position: sticky;
  top: 96px;
  padding: clamp(16px, 2vw, 22px);
  background: var(--white);
  border: 1px solid var(--line);
  border-top: 4px solid var(--gold);
  box-shadow: var(--shadow);
}

.est__fields { display: flex; flex-direction: column; gap: 14px; }

.est__gates {
  margin: 2px 0 0;
  padding: 14px 0 0;
  border: 0;
  border-top: 1px solid var(--line);
}

.est__legend-title {
  margin: 0 0 8px;
  padding: 0;
  font-size: 0.8rem;
  font-weight: 800;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--ink);
}

#est-place-gate { width: 100%; margin-top: 8px; }

.est .btn[disabled] {
  opacity: 0.55;
  cursor: not-allowed;
  box-shadow: none;
  transform: none;
}

.est .btn[disabled]:hover { transform: none; }

.est__gate-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin: 10px 0 0;
  padding: 0;
  list-style: none;
}

.est__gate-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin: 0;
  padding: 7px 10px;
  font-size: 0.86rem;
  background: var(--wash);
  border-left: 3px solid var(--gold);
}

.est__gate-list button {
  padding: 2px 0;
  font: inherit;
  font-size: 0.74rem;
  font-weight: 800;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--gold-ink);
  background: none;
  border: 0;
  cursor: pointer;
}

.est__empty { margin: 8px 0 0; font-size: 0.84rem; color: var(--muted); }

.est__muted { color: var(--muted); font-weight: 600; }

.est__manual {
  padding-top: 12px;
  font-size: 0.88rem;
  border-top: 1px solid var(--line);
}

.est__manual summary {
  font-weight: 800;
  color: var(--ink);
  cursor: pointer;
}

.est__manual-body {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding-top: 12px;
}

.est__manual-gates {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.est__manual-gates .field:last-child { grid-column: 1 / -1; }

.est__manual-actions { display: flex; flex-wrap: wrap; gap: 8px; }

/* --------------------------------------------------------------- summary */

.est__summary {
  margin: 0 0 18px;
  padding: 18px;
  background: var(--charcoal);
  border-radius: var(--radius);
}

.est__label {
  margin: 0 0 4px;
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--gold);
}

.est__total {
  display: flex;
  flex-wrap: wrap;
  column-gap: 0.28em;
  margin: 0;
  font-size: clamp(1.85rem, 3.4vw, 2.4rem);
  font-weight: 900;
  line-height: 1.1;
  letter-spacing: -0.03em;
  font-variant-numeric: tabular-nums;
  color: var(--white);
}

.est__perft {
  margin: 6px 0 0;
  font-size: 0.86rem;
  color: #c7c8cf;
}

.est__stats {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  margin: 14px 0 0;
  padding: 12px 0 0;
  border-top: 1px solid var(--line-dark);
}

.est__stats div { margin: 0; }

.est__stats dt {
  margin: 0 0 2px;
  font-size: 0.64rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: #9d9ea8;
}

.est__stats dd {
  margin: 0;
  font-size: 0.98rem;
  font-weight: 800;
  color: var(--white);
}

.est__breakdown {
  min-height: 1.2em;
  margin: 10px 0 0;
  font-size: 0.8rem;
  color: #b9bac3;
}

.est__actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 14px;
}

.est__actions .btn { width: 100%; }

.est__summary .btn--ghost-dark {
  color: var(--white);
  border-color: rgba(255, 255, 255, 0.45);
}

.est__summary .btn--ghost-dark:hover {
  color: var(--white);
  border-color: var(--white);
  background: rgba(255, 255, 255, 0.08);
}

.est__status {
  min-height: 1.1em;
  margin: 10px 0 0;
  font-size: 0.82rem;
  color: var(--gold);
}

.est__fine {
  margin: 12px 0 0;
  font-size: 0.78rem;
  color: var(--muted);
}

.est__fine a { color: var(--gold-ink); }

/* ----------------------------------------------------------- assumptions */

.est__assumptions {
  margin-top: 26px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
}

.est__assumptions summary {
  font-weight: 800;
  color: var(--ink);
  cursor: pointer;
}

.est__assumptions-body {
  max-width: 820px;
  padding-top: 12px;
}

.est__assumptions-body ul { margin: 0; padding-left: 20px; }
.est__assumptions-body li { font-size: 0.9rem; color: var(--body); }

/* ------------------------------------------------------------ responsive */

@media (max-width: 1000px) {
  .est { grid-template-columns: 1fr; }
  .est__panel { position: static; }
}

@media (max-width: 760px) {
  .est__map { height: 58vh; min-height: 380px; }
  .est__search { flex-wrap: wrap; }
  .est__search input { flex: 1 1 100%; font-size: 16px; }
  .est__search .btn { flex: 1 1 auto; }
  .est__toolbar .btn { flex: 1 1 calc(50% - 8px); }
  /* The search bar takes two rows below 760px; keep the zoom buttons clear of it
     and make them a proper touch target. */
  .est__map .leaflet-top.leaflet-right { top: 108px; right: 6px; }
  .est__map .leaflet-bar a { width: 46px; height: 46px; line-height: 46px; font-size: 1.5rem; }
}

@media (max-width: 620px) {
  .est__map { height: 54vh; min-height: 330px; }
  .est__legend { display: none; }
  .est__manual-gates { grid-template-columns: 1fr; }
  .est__manual-gates .field:last-child { grid-column: auto; }
  .est__stats { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

@media (prefers-reduced-motion: reduce) {
  .est__map { scroll-behavior: auto; }
}
