/* Stage 4 item 2: reordered to the estate standard - sidebar/controls
   LEFT, map RIGHT (every other tool page runs this way, e.g.
   public/tools/map-radius/map-radius.css's .tool-sidebar/.map-container
   order; this page put them the wrong way round in Stage 3). */
.tm-layout {
  display: grid;
  grid-template-columns: 340px 1fr;
  gap: 1rem;
  align-items: start;
}

.tm-map-wrap {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  min-width: 0;
}

/* Dan's ruling 2026-09-04: a stated limitation, not hidden small print -
   legible at a glance, not a footnote. Plain factual tone matching the
   site's own established house wording for this exact fact (see the
   note's own comment at its markup for the source line), not styled as
   a warning/error (no amber/red treatment - it isn't a defect). */
.tm-map-coverage-note {
  font-size: 0.8rem;
  color: var(--text-muted, #6b7280);
  margin: 0;
}

/* Stage 9, launch-blocker fix (Dan's ruling 2026-09-04): the sidebar ran
   far taller than the 640px map, leaving the right two-thirds of the
   page blank below it once scrolled past - not fixed by moving output
   below the map (measured: only a partial reduction, 2070px -> ~1503px,
   still ~863px taller than the map) but by adopting the pattern 4 of 6
   comparable tools already use (postcode-mapper/route-optimiser/
   flood-risk/nearest-postcodes's own `.tool-container { max-height:
   700px }` + `.tool-sidebar { overflow-y: auto }`) - the SAME 700px
   value, not invented here. Applied to the sidebar directly rather than
   the `.tm-layout` grid container: those four tools cap the flex PARENT
   because a flex child's overflow only clips within a bounded parent,
   but `.tm-layout` is a CSS grid with `align-items: start` (deliberately
   NOT stretch, so the 640px map is never force-stretched to match a
   taller sidebar) - capping the grid ITEM directly is the grid-native
   equivalent of the same mechanism, same value, same effect. Desktop
   only: reset inside the existing mobile breakpoint below, where the
   layout already stacks map above sidebar and a second, nested scroll
   region inside a single narrow column would be worse, not better. */
.tm-sidebar {
  max-height: 700px;
  overflow-y: auto;
}

.tm-edit-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
  margin-top: var(--spacing-md, 12px);
}

/* .tm-below-map (Stage 9, 2026-09-03) moved import-summary/export out of
   the sidebar into a full-width row below the map. RE-REVERTED the same
   day: capping .tm-sidebar's height (below) already solves the void that
   move was for, without inventing a layout no other tool in the estate
   uses - checked directly, all 6 comparable tools keep every panel
   (input and output alike) in the sidebar column, and drive-time-map's
   own Export section (the one other real export feature in the estate)
   sits as the LAST section in ITS sidebar too. Both panels are back in
   `.tm-sidebar`, in their original order; this class and its markup are
   gone, not just unused. */

#tm-map {
  height: 640px;
  width: 100%;
  border-radius: 8px;
  border: 1px solid var(--border, #e5e7eb);
}

.tm-sidebar {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

/* Stage 9, item 1 (Dan's ruling 2026-09-03): solve-configuration panels
   (split criterion, coverage mode, exclusions) are meaningless before
   there is any data to configure, and left visible they were the reason
   the empty-state sidebar ran ~750px taller than the map, leaving a
   large blank gap in the layout beside/below it - not because the map
   itself was undersized (measured directly: it renders at its normal,
   unconditional 640px on every load, solved or not), but because the
   sidebar had nothing to lose height on until this. Same show/hide
   convention as #tm-import-summary/#tm-export-panel below - hidden by
   default here, shown by the existing `#tm-coverage, #tm-exclusions`
   layout rule further down this file gated on `.tm-visible`, and
   `.tm-controls`'s own layout rule gated the same way. */
.tm-controls,
#tm-coverage,
#tm-exclusions {
  display: none;
}

.tm-controls.tm-visible {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 0.75rem;
  border: 1px solid var(--border, #e5e7eb);
  border-radius: 8px;
}

.tm-controls label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.9rem;
}

#tm-tier2-progress {
  font-size: 0.85rem;
  color: var(--text-muted, #6b7280);
  min-height: 1.2em;
}

#tm-legend {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 0.75rem;
  border: 1px solid var(--border, #e5e7eb);
  border-radius: 8px;
}

.tm-legend-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.85rem;
  padding-bottom: 0.4rem;
  border-bottom: 1px solid var(--border, #f3f4f6);
}

.tm-legend-row:last-child {
  border-bottom: none;
}

.tm-swatch {
  width: 14px;
  height: 14px;
  border-radius: 3px;
  flex-shrink: 0;
}

.tm-legend-name {
  font-weight: 600;
}

.tm-legend-figures {
  color: var(--text-muted, #6b7280);
  width: 100%;
}

.tm-breach {
  width: 100%;
  color: #b45309;
  background: #fffbeb;
  border: 1px solid #fde68a;
  border-radius: 4px;
  padding: 0.25rem 0.4rem;
  font-size: 0.8rem;
}

#tm-stats {
  padding: 0.75rem;
  border: 1px solid var(--border, #e5e7eb);
  border-radius: 8px;
}

.tm-totals {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  font-size: 0.85rem;
}

.territory-label span {
  font-weight: 700;
  font-size: 0.8rem;
  color: #1f2937;
  text-shadow: 0 0 3px #fff, 0 0 3px #fff, 0 0 3px #fff;
  white-space: nowrap;
}

@media (max-width: 640px) {
  .tm-layout {
    grid-template-columns: 1fr;
  }

  #tm-map {
    height: 420px;
    order: 1;
  }

  .tm-sidebar {
    order: 2;
    /* Desktop-only cap, reset here: mobile already stacks map above
       sidebar in a single column with the whole page scrolling - a
       second, nested scroll region inside that narrow column would
       trap content behind a tiny internal scrollbar instead of letting
       the page itself scroll to it, worse than the void this fixes. */
    max-height: none;
    overflow-y: visible;
  }
}

/* ------------------------------------------------------------------
   Stage 4: coverage modes, exclusions, water-crossing flag panels.
   ------------------------------------------------------------------ */
#tm-coverage.tm-visible,
#tm-exclusions.tm-visible {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 0.75rem;
  border: 1px solid var(--border, #e5e7eb);
  border-radius: 8px;
}

#tm-coverage h3,
#tm-exclusions h3 {
  margin: 0 0 0.25rem;
  font-size: 0.85rem;
  font-weight: 700;
}

.tm-coverage-mode-row {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.85rem;
}

.tm-coverage-mode-row input[type='radio'] {
  flex-shrink: 0;
}

.tm-coverage-mode-row.disabled {
  color: var(--text-muted, #6b7280);
}

.tm-base-radius-row {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.8rem;
  padding-left: 1.4rem;
}

.tm-base-radius-row input[type='number'] {
  width: 4.5rem;
}

.tm-outcode-input {
  display: flex;
  gap: 0.4rem;
}

.tm-outcode-input input[type='text'] {
  flex: 1;
  min-width: 0;
}

.tm-island-preset-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.4rem;
  font-size: 0.82rem;
}

.tm-island-preset-row .tm-preset-count {
  color: var(--text-muted, #6b7280);
  font-size: 0.78rem;
}

#tm-water-flag {
  padding: 0.6rem 0.75rem;
  border: 1px solid #fde68a;
  background: #fffbeb;
  color: #92400e;
  border-radius: 8px;
  font-size: 0.82rem;
  display: none;
}

#tm-water-flag.visible {
  display: block;
}

#tm-balance-panel {
  padding: 0.6rem 0.75rem;
  border: 1px solid #fde68a;
  background: #fffbeb;
  color: #92400e;
  border-radius: 8px;
  font-size: 0.82rem;
  display: none;
}

#tm-balance-panel.visible {
  display: block;
}

#tm-balance-panel ul {
  margin: 0.4rem 0 0;
  padding-left: 1.2rem;
}

#tm-balance-panel li {
  margin: 0.15rem 0;
}

.tm-not-covered-reason {
  font-size: 0.75rem;
  color: var(--text-muted, #6b7280);
}

/* Busy state, 2026-08-24: any control that triggers a re-solve gets an
   immediate busy indicator so the UI never sits silently frozen while the
   (currently synchronous, main-thread-blocking - see the phase-4 mode-
   switch diagnosis) recompute runs. Ships regardless of the real perf fix
   landing separately; kills the "feels broken" half on its own. */
.tm-busy-indicator {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.75rem;
  margin-bottom: 0.75rem;
  border-radius: 6px;
  background: var(--surface-muted, #f3f4f6);
  border: 1px solid var(--border, #e5e7eb);
  color: var(--text-muted, #6b7280);
  font-size: 0.85rem;
}

.tm-busy-indicator[hidden] {
  display: none;
}

.tm-busy-spinner {
  width: 0.9rem;
  height: 0.9rem;
  border-radius: 50%;
  border: 2px solid var(--border, #d1d5db);
  border-top-color: var(--text-muted, #6b7280);
  animation: tm-busy-spin 0.7s linear infinite;
  flex: none;
}

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

.tm-layout.tm-busy .tm-controls,
.tm-layout.tm-busy #tm-coverage,
.tm-layout.tm-busy #tm-exclusions,
.tm-layout.tm-busy #tm-water-flag,
.tm-layout.tm-busy #tm-balance-panel {
  pointer-events: none;
  opacity: 0.6;
}

/* ------------------------------------------------------------------
   Stage 5: input handling - paste/CSV panel, import summary, column
   mapping modal, add-one-base form, base-detail expando.
   ------------------------------------------------------------------ */
.tm-panel {
  padding: 0.75rem;
  border: 1px solid var(--border, #e5e7eb);
  border-radius: 8px;
}

.tm-panel h3 {
  margin: 0 0 0.5rem;
  font-size: 0.85rem;
  font-weight: 700;
}

#tm-input-panel label {
  display: block;
  font-size: 0.8rem;
  color: var(--text-muted, #6b7280);
  margin: 0.5rem 0 0.25rem;
}

#tm-input-panel textarea,
#tm-input-panel input[type='text'] {
  width: 100%;
  box-sizing: border-box;
  font-family: var(--font-mono, monospace);
  font-size: 0.8rem;
  padding: 0.4rem 0.5rem;
  border-radius: 6px;
  border: 1px solid var(--border, #e5e7eb);
  background: var(--card, #fff);
  color: var(--text, #0f172a);
}

.tm-input-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.5rem;
  flex-wrap: wrap;
}

.tm-input-actions .btn {
  padding: 0.45rem 0.8rem;
  font-size: 0.8rem;
}

.tm-input-actions-secondary {
  margin-top: 0.35rem;
}

.tm-upload-divider {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 0.85rem 0;
  color: var(--text-muted, #6b7280);
  font-size: 0.75rem;
}

.tm-upload-divider::before,
.tm-upload-divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border, #e5e7eb);
}

.tm-csv-drop-zone {
  border: 1px dashed var(--border, #e5e7eb);
  border-radius: 8px;
  padding: 0.6rem;
  transition: border-color 0.15s ease, background 0.15s ease;
}

.tm-csv-drop-zone.tm-drag-over {
  border-color: var(--primary, #2563eb);
  background: var(--hover-bg, rgba(0, 0, 0, 0.05));
}

.tm-btn-full {
  width: 100%;
}

.tm-hint {
  font-size: 0.72rem;
  color: var(--text-muted, #6b7280);
  margin: 0.4rem 0 0;
}

/* Stage 6 (spec 4.4): manual editing - the click-to-check popup's "Move
   to:" list and the edit-outcome popup that replaces it once an edit is
   applied. Kept deliberately plain (no modal, no edit-mode chrome) per
   Dan's ruling 2 - the popup that already opens for click-to-check is
   the only surface, not a new UI mode. */
.tm-popup-move {
  margin-top: 0.5rem;
  padding-top: 0.4rem;
  border-top: 1px solid var(--border, #e5e7eb);
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

.tm-popup-move-btn {
  font-size: 0.78rem;
  padding: 0.2rem 0.5rem;
  text-align: left;
}

.tm-edit-warning {
  color: #b45309;
  font-weight: 600;
  font-size: 0.82rem;
  margin-top: 0.35rem;
  display: block;
}

.tm-popup-undo {
  margin-top: 0.4rem;
  font-size: 0.78rem;
}

.tm-add-one {
  margin-top: 0.85rem;
  font-size: 0.85rem;
}

.tm-add-one summary {
  cursor: pointer;
  font-weight: 600;
}

.tm-add-one-form {
  margin-top: 0.5rem;
}

.tm-add-one-form button {
  margin-top: 0.6rem;
}

/* Import summary */
#tm-import-summary {
  display: none;
}

#tm-import-summary.tm-visible {
  display: block;
}

/* Export panel (Stage 9, item 2, Dan's ruling 2026-09-03): hidden until a
   solve exists - three disabled download buttons with nothing to download
   read as broken on first load, not as "not yet available". Same
   show/hide convention as #tm-import-summary above. */
#tm-export-panel {
  display: none;
}

#tm-export-panel.tm-visible {
  display: block;
}

.tm-summary-line {
  font-size: 0.85rem;
  font-weight: 600;
}

.tm-summary-warning-list {
  margin: 0.5rem 0 0;
  padding-left: 1.1rem;
  font-size: 0.78rem;
  color: #92400e;
}

.tm-problem-rows {
  margin-top: 0.6rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.tm-problem-row {
  border: 1px solid #fecaca;
  background: #fef2f2;
  border-radius: 6px;
  padding: 0.5rem;
  font-size: 0.78rem;
}

html[data-theme='dark'] .tm-problem-row {
  background: rgba(239, 68, 68, 0.08);
  border-color: rgba(239, 68, 68, 0.4);
}

.tm-problem-row .tm-problem-reason {
  color: #b91c1c;
  font-weight: 600;
  margin-bottom: 0.35rem;
}

html[data-theme='dark'] .tm-problem-row .tm-problem-reason {
  color: #fca5a5;
}

.tm-problem-row-fields {
  display: flex;
  gap: 0.4rem;
  flex-wrap: wrap;
  align-items: center;
}

.tm-problem-row-fields input[type='text'] {
  width: auto;
  min-width: 8rem;
  font-size: 0.78rem;
  padding: 0.3rem 0.4rem;
}

.tm-problem-row-fields button {
  font-size: 0.75rem;
  padding: 0.3rem 0.6rem;
}

/* Base-coverage warning (case table row 5: base outside the coverage
   universe) - same amber-warning family as #tm-water-flag. */
#tm-base-coverage-warning {
  padding: 0.6rem 0.75rem;
  border: 1px solid #fde68a;
  background: #fffbeb;
  color: #92400e;
  border-radius: 8px;
  font-size: 0.82rem;
  display: none;
}

#tm-base-coverage-warning.visible {
  display: block;
}

html[data-theme='dark'] #tm-base-coverage-warning,
html[data-theme='dark'] #tm-water-flag {
  background: rgba(245, 158, 11, 0.1);
  color: #fbbf24;
  border-color: rgba(245, 158, 11, 0.4);
}

/* Territory detail expando - extra columns carried through per base
   (spec 4.7: "carried through untouched into the territory detail
   panel"). */
.tm-legend-detail-toggle {
  font-size: 0.72rem;
  background: none;
  border: none;
  color: var(--primary, #2563eb);
  padding: 0;
  cursor: pointer;
  text-decoration: underline;
}

.tm-legend-detail {
  width: 100%;
  display: none;
  margin-top: 0.35rem;
  font-size: 0.75rem;
  border-collapse: collapse;
}

.tm-legend-detail.tm-visible {
  display: table;
}

.tm-legend-detail td {
  padding: 0.2rem 0.4rem 0.2rem 0;
  vertical-align: top;
  border-bottom: 1px solid var(--border, #f3f4f6);
}

.tm-legend-detail .tm-extra-key {
  color: var(--text-muted, #6b7280);
}

/* ------------------------------------------------------------------
   Stage 5: column-mapping modal (shared by paste and CSV upload) -
   same overlay/dialog pattern the estate already uses for this UI
   (route-optimiser-au's CSV mapping modal, ported as a pattern).
   ------------------------------------------------------------------ */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.2s ease, visibility 0.2s ease;
}

.modal-overlay.active {
  opacity: 1;
  visibility: visible;
}

.modal {
  background: var(--card, #fff);
  border: 1px solid var(--border, #e5e7eb);
  border-radius: 12px;
  padding: 1.5rem;
  max-width: 420px;
  width: 90%;
  box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.2);
}

.modal-wide {
  max-width: 560px;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
}

.modal-header h3 {
  margin: 0;
  font-size: 1rem;
}

.modal-close {
  width: 32px;
  height: 32px;
  background: transparent;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 1.1rem;
  color: var(--text-muted, #6b7280);
}

.modal-close:hover {
  background: var(--hover-bg, rgba(0, 0, 0, 0.05));
}

.tm-modal-hint {
  font-size: 0.8rem;
  color: var(--text-muted, #6b7280);
  margin: 0 0 1rem;
}

.tm-mapping-table-wrap {
  max-height: 50vh;
  overflow-y: auto;
  border: 1px solid var(--border, #e5e7eb);
  border-radius: 8px;
}

.tm-mapping-table {
  width: 100%;
  border-collapse: collapse;
}

.tm-mapping-table td {
  padding: 0.6rem 0.7rem;
  border-bottom: 1px solid var(--border, #e5e7eb);
  font-size: 0.78rem;
  vertical-align: middle;
}

.tm-mapping-table tr:last-child td {
  border-bottom: none;
}

.tm-mapping-table select {
  width: 100%;
  padding: 0.35rem 0.4rem;
  border-radius: 6px;
  border: 1px solid var(--border, #e5e7eb);
  background: var(--card, #fff);
  color: var(--text, #0f172a);
  font-size: 0.78rem;
}

.tm-mapping-error {
  color: #ef4444;
  font-size: 0.75rem;
  margin: 0.6rem 0 0;
  min-height: 0.9rem;
}

.tm-modal-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 1rem;
}

/* Stage 7 (spec 6.1): the export panel's button row - a plain wrap, no
   special styling needed since .tm-panel/.btn already carry it. */
.tm-export-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

/* ------------------------------------------------------------------
   Stage 7 (spec 6.3): print stylesheet - hide this tool's own input/
   editing chrome, size the map to the page, keep the legend and stats
   alongside it. Site-wide chrome (header/footer/ad slots) is NOT
   touched here - that is a BaseLayout/shared-component change, out of
   this tool's own CSS scope and this stage's remit; `.no-print` is the
   site's existing convention for that but is not applied anywhere yet
   (see src/styles/global.css), a real limitation left for whoever picks
   up a site-wide print pass, not silently fixed here.
   ------------------------------------------------------------------ */
@media print {
  #tm-busy-indicator,
  #tm-input-panel,
  #tm-import-summary,
  .tm-controls,
  #tm-coverage,
  #tm-exclusions,
  #tm-water-flag,
  #tm-base-coverage-warning,
  #tm-export-panel,
  #tm-edit-controls {
    display: none !important;
  }

  .tm-layout {
    display: block;
  }

  .tm-sidebar {
    width: 100%;
  }

  #tm-map {
    height: 70vh;
    page-break-inside: avoid;
  }

  .tm-legend-row {
    page-break-inside: avoid;
  }
}

/* Stage 8 (Dan's ruling 2026-09-02): the error-surface pass's visible
   half - a fixed banner, deliberately independent of this tool's own
   layout (no reliance on .tm-layout/.tm-sidebar existing or having
   rendered), since it must still show up if the map itself failed to
   construct. */
.tm-fatal-error {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 10000;
  background: #b91c1c;
  color: #fff;
  padding: 0.75rem 1rem;
  font-size: 0.95rem;
  text-align: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}
