/* ---------------------------------------------------------------------------
   Application shell inspired by the legacy SB Admin 2 layout.
   Sidebar + topbar + cards + data tables, built with Grid/Flexbox only.
   --------------------------------------------------------------------------- */

/* --- Shell ---------------------------------------------------------------- */

.app-shell {
  background: var(--body-bg);
}

.app {
  min-height: 100svh;
}
.sidebar {
  position: fixed;
  inset-block: 0;
  inset-inline-start: 0;
  z-index: 40;
  display: flex;
  flex-direction: column;
  width: var(--sidebar-width);
  padding: 1rem 0.75rem;
  overflow-y: auto;
  background: linear-gradient(180deg, #e7f3ed 0%, #d7ebe0 100%);
  border-inline-end: 1px solid #c2dfd0;
  color: #26493b;
  transition: width 0.18s ease, transform 0.18s ease;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  min-height: 3rem;
  padding: 0.35rem 0.5rem;
  color: #164e36;
  font-size: 1.05rem;
  font-weight: 800;
  letter-spacing: 0.01em;
  text-transform: uppercase;
}

.sidebar-brand:hover {
  text-decoration: none;
  color: #0e3826;
}

.sidebar-logo {
  flex: none;
  width: 1.9rem;
  height: 1.9rem;
  color: #247a55;
}

.sidebar-nav {
  margin-block-start: 1rem;
}

.sidebar-heading {
  margin-block: 1rem 0.4rem;
  padding-inline: 0.65rem;
  font-size: 0.66rem;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: #547869;
  white-space: nowrap;
}

.sidebar-list {
  list-style: none;
}

.sidebar-link {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  padding: 0.6rem 0.65rem;
  border-inline-start: 3px solid transparent;
  border-radius: 0 var(--radius) var(--radius) 0;
  color: #244739;
  font-size: 0.86rem;
  font-weight: 600;
  white-space: nowrap;
}

.sidebar-link:hover {
  background: rgba(36, 122, 85, 0.11);
  color: #123627;
  text-decoration: none;
}

.sidebar-link.is-active {
  border-inline-start-color: #1f7a53;
  background: rgba(36, 122, 85, 0.18);
  color: #123627;
  font-weight: 700;
}

.sidebar-icon {
  flex: none;
  width: 1.15rem;
  height: 1.15rem;
}

.sidebar-foot {
  margin-block-start: auto;
  padding: 1rem 0.65rem 0;
  border-block-start: 1px solid rgba(22, 78, 54, 0.14);
}

.sidebar-foot-text {
  font-size: 0.68rem;
  line-height: 1.5;
  color: #4d7061;
}

.sidebar-backdrop {
  position: fixed;
  inset: 0;
  z-index: 35;
  background: rgba(58, 59, 69, 0.5);
}

.app-main {
  display: flex;
  flex-direction: column;
  min-height: 100svh;
  margin-inline-start: var(--sidebar-width);
  transition: margin-inline-start 0.18s ease;
}

.topbar {
  position: sticky;
  inset-block-start: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  min-height: var(--topbar-height);
  padding-inline: 1.25rem;
  background: var(--surface);
  border-block-end: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
}

.topbar-brand {
  display: none;
  color: var(--primary);
  font-size: 1rem;
  font-weight: 800;
  text-transform: uppercase;
}

.topbar-brand:hover {
  text-decoration: none;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-inline-start: auto;
}

.topbar-link {
  color: var(--text-muted);
  font-size: 0.82rem;
  font-weight: 600;
}

.topbar-link.is-disabled {
  cursor: not-allowed;
  opacity: 0.65;
}

.icon-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.5rem;
  height: 2.5rem;
  padding: 0;
  border: 0;
  border-radius: var(--radius);
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
}

.icon-button svg {
  width: 1.35rem;
  height: 1.35rem;
}

.icon-button:hover {
  background: var(--border-soft);
  color: var(--dark);
}

.content {
  flex: 1;
  width: 100%;
  max-width: 100rem;
  padding: 1.5rem 1.5rem 0;
}

/* Collapsed desktop sidebar. */
@media (min-width: 768px) {
  .sidebar-collapsed .sidebar {
    width: var(--sidebar-collapsed);
  }

  .sidebar-collapsed .app-main {
    margin-inline-start: var(--sidebar-collapsed);
  }

  .sidebar-collapsed .sidebar-heading,
  .sidebar-collapsed .sidebar-brand-text,
  .sidebar-collapsed .sidebar-link-text,
  .sidebar-collapsed .sidebar-foot-text {
    display: none;
  }

  .sidebar-collapsed .sidebar-link,
  .sidebar-collapsed .sidebar-brand {
    justify-content: center;
    padding-inline: 0;
    border-inline-start-color: transparent;
  }

  .sidebar-collapsed .sidebar-link.is-active {
    background: rgba(36, 122, 85, 0.2);
  }
}

@media (max-width: 767.98px) {
  .sidebar {
    width: min(16rem, 82vw);
    transform: translateX(-100%);
  }

  .sidebar-open .sidebar {
    transform: translateX(0);
  }

  .app-main,
  .sidebar-collapsed .app-main {
    margin-inline-start: 0;
  }

  .sidebar-collapsed .sidebar-heading,
  .sidebar-collapsed .sidebar-brand-text,
  .sidebar-collapsed .sidebar-link-text,
  .sidebar-collapsed .sidebar-foot-text {
    display: initial;
  }

  .topbar-brand {
    display: inline;
  }

  .content {
    padding: 1rem 1rem 0;
  }
}

/* --- Page header ---------------------------------------------------------- */

.page-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  margin-block-end: 1.25rem;
}

.page-title {
  font-size: clamp(1.35rem, 1.15rem + 0.9vw, 1.75rem);
  font-weight: 700;
  color: var(--dark);
}

.page-subtitle {
  max-width: 52rem;
  margin-block-start: 0.35rem;
  color: var(--text-muted);
  font-size: 0.9rem;
}

.page-actions {
  display: flex;
  flex: none;
  align-items: center;
  gap: 0.5rem;
}

/* --- Cards ---------------------------------------------------------------- */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}

.card + .card {
  margin-block-start: 1.25rem;
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.85rem 1.15rem;
  background: var(--light);
  border-block-end: 1px solid var(--border);
}

.card-title {
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--primary);
}

.card-meta {
  color: var(--text-muted);
  font-size: 0.78rem;
  text-align: end;
}

.card-body {
  padding: 1.15rem;
}

/* --- Forms ---------------------------------------------------------------- */

.field {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  min-width: 0;
}

.field label {
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.form-control {
  width: 100%;
  min-height: 2.2rem;
  padding: 0.35rem 0.6rem;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font-size: 0.86rem;
}

.form-control:focus {
  border-color: var(--primary);
  outline: none;
  box-shadow: 0 0 0 0.15rem rgba(78, 115, 223, 0.18);
}

.form-control:disabled {
  background: var(--border-soft);
  cursor: not-allowed;
}

.range {
  width: 100%;
  accent-color: var(--primary);
}

.card-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 0.85rem;
  padding: 0.85rem 1.15rem;
  border-block-end: 1px solid var(--border-soft);
}

.card-toolbar .field-search {
  flex: 1 1 18rem;
}

.card-toolbar .field-page-size {
  flex: 0 0 8rem;
}

/* --- Tables --------------------------------------------------------------- */

.table-wrapper {
  container-type: inline-size;
  max-block-size: 65vh;
  overflow: auto;
  overscroll-behavior: contain;
}

.table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: 0.85rem;
}

.table caption {
  text-align: start;
}

.table th,
.table td {
  padding: 0.55rem 0.8rem;
  border-block-end: 1px solid var(--border-soft);
  text-align: start;
  vertical-align: middle;
}

.table thead {
  position: sticky;
  inset-block-start: 0;
  z-index: 2;
}

.table thead th {
  background: var(--light);
  border-block-end: 1px solid var(--border);
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-muted);
  white-space: nowrap;
}

.table tbody th {
  position: sticky;
  inset-inline-start: 0;
  z-index: 1;
  background: var(--surface);
  font-weight: 600;
  color: var(--dark);
}

.table tbody tr:hover td,
.table tbody tr:hover th {
  background: #f4f6fd;
}

.table td.numeric {
  font-variant-numeric: tabular-nums;
}

.table .cell-sub {
  display: block;
  color: var(--text-muted);
  font-family: var(--font-mono);
  font-size: 0.72rem;
  font-weight: 400;
}

.sort-button {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.4rem;
  width: 100%;
  min-height: 24px;
  padding: 0;
  border: 0;
  background: transparent;
  color: inherit;
  font: inherit;
  letter-spacing: inherit;
  text-transform: inherit;
  text-align: inherit;
  cursor: pointer;
}

.sort-button::after {
  content: "";
  flex: none;
  width: 0;
  height: 0;
  border-inline: 4px solid transparent;
  border-block-end: 6px solid var(--border-strong);
  opacity: 0.55;
}

th[aria-sort="ascending"] .sort-button::after {
  border-block-end-color: var(--primary);
  opacity: 1;
}

th[aria-sort="descending"] .sort-button::after {
  border-block-end: 0;
  border-block-start: 6px solid var(--primary);
  opacity: 1;
}

.numeric .sort-button {
  justify-content: flex-end;
}

.table-footer {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.75rem 1.15rem;
  border-block-start: 1px solid var(--border-soft);
}

.table-summary {
  color: var(--text-muted);
  font-size: 0.78rem;
}

.pager {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.pager-page {
  color: var(--text-muted);
  font-size: 0.78rem;
  font-variant-numeric: tabular-nums;
}

/* Stacked cards on narrow containers, driven by the table's own width. */
@container (width < 760px) {
  .table-wrapper {
    max-block-size: none;
  }

  .data-table,
  .data-table thead,
  .data-table tbody,
  .data-table tr,
  .data-table th,
  .data-table td {
    display: block;
  }

  .data-table thead {
    display: none;
  }

  .data-table tbody tr {
    margin-block-end: 0.85rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
    background: var(--surface);
  }

  .data-table tbody th {
    position: static;
    padding: 0.65rem 0.85rem;
    background: var(--light);
    border-block-end: 1px solid var(--border);
  }

  .data-table tbody td {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 1rem;
    padding: 0.45rem 0.85rem;
    border-block-end: 1px solid var(--border-soft);
  }

  .data-table tbody td:last-child {
    border-block-end: 0;
  }

  .data-table tbody td::before {
    flex: none;
    font-size: 0.68rem;
    font-weight: 700;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--text-muted);
  }

  .station-table {
    --label-2: "Province";
    --label-3: "Temperature";
    --label-4: "Humidity";
    --label-5: "Pressure";
    --label-6: "DOA";
    --label-7: "Observed";
  }

  .data-table td:nth-child(2)::before { content: var(--label-2) ": "; }
  .data-table td:nth-child(3)::before { content: var(--label-3) ": "; }
  .data-table td:nth-child(4)::before { content: var(--label-4) ": "; }
  .data-table td:nth-child(5)::before { content: var(--label-5) ": "; }
  .data-table td:nth-child(6)::before { content: var(--label-6) ": "; }
  .data-table td:nth-child(7)::before { content: var(--label-7) ": "; }

  .data-table td.numeric {
    text-align: end;
  }
}

/* --- Empty and error states ---------------------------------------------- */

.empty-state {
  padding: 2.5rem 1.5rem;
  text-align: center;
  color: var(--text-muted);
}

.empty-state h3 {
  margin-block-end: 0.5rem;
  color: var(--dark);
}

.empty-state p {
  max-width: 42rem;
  margin-inline: auto;
  font-size: 0.88rem;
}

.empty-state-error {
  border-block-start: 1px solid var(--border-soft);
  background: #fdf5f4;
  color: #8a3a31;
}

/* --- Map ------------------------------------------------------------------ */

.map-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 22rem;
  gap: 1.25rem;
  align-items: start;
}

.map-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 0.85rem;
  padding: 0.85rem 1.15rem;
  border-block-end: 1px solid var(--border-soft);
}

.map-toolbar .field {
  flex: 1 1 10rem;
  max-width: 15rem;
}

.map-toolbar .field-opacity {
  flex: 1 1 10rem;
}

.map-toolbar .field-action {
  flex: 0 0 auto;
  max-width: none;
}

.map {
  width: 100%;
  height: clamp(380px, 62vh, 720px);
  background: #dbe4ef;
  z-index: 0;
}

.map-help {
  color: var(--text-muted);
  font-size: 0.76rem;
}

.map-help[data-state="error"] {
  color: #a83227;
  font-weight: 600;
}

.map-footer {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.65rem 1.15rem;
  border-block-start: 1px solid var(--border-soft);
}

.map-legend {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.72rem;
  color: var(--text-muted);
}

.legend-title {
  font-weight: 700;
}

.legend-scale {
  display: inline-block;
  width: 7rem;
  height: 0.5rem;
  border-radius: 999px;
  background: linear-gradient(
    90deg,
    #30123b 0%,
    #4458cb 22%,
    #1fa187 45%,
    #a2d729 68%,
    #f7b52c 84%,
    #e74a3b 100%
  );
}

.legend-range {
  font-variant-numeric: tabular-nums;
}

.point-panel .card-header {
  flex-wrap: wrap;
}

.chart-wrap {
  position: relative;
  min-height: 220px;
  margin-block: 0.75rem;
}

/* Collapse the reserved chart area until a point has been queried. */
.chart-wrap:has(> canvas[hidden]) {
  display: none;
}

.point-values,
.bbox-stats {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.6rem;
  margin-block-start: 0.75rem;
}

.point-values > div,
.bbox-stats > div {
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  background: var(--light);
}

.point-values dt,
.bbox-stats dt {
  font-size: 0.66rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.point-values dd,
.bbox-stats dd {
  margin: 0;
  font-size: 0.9rem;
  font-weight: 700;
  color: var(--dark);
  font-variant-numeric: tabular-nums;
}

.leaflet-popup-content {
  margin: 0.75rem 0.9rem;
  font-size: 0.85rem;
}

.leaflet-popup-content h3 {
  margin-block-end: 0.25rem;
  font-size: 0.9rem;
  color: var(--primary);
}

/* --- Downloads: clipping workspace ---------------------------------------- */

.download-layout {
  grid-template-columns: minmax(0, 1fr) 24rem;
}

.clip-form {
  display: grid;
  gap: 1.1rem;
}

.panel-section {
  display: grid;
  gap: 0.6rem;
  min-width: 0;
  margin: 0;
  padding: 0 0 1.1rem;
  border: 0;
  border-block-end: 1px solid var(--border-soft);
}

.panel-section:last-of-type {
  padding-block-end: 0;
  border-block-end: 0;
}

.panel-section legend {
  padding: 0;
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.bbox-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.6rem;
}

.bbox-grid .form-control {
  font-variant-numeric: tabular-nums;
}

.bbox-grid .form-control[aria-invalid="true"] {
  border-color: var(--danger);
  background: #fdf5f4;
}

.form-hint {
  font-size: 0.76rem;
  line-height: 1.45;
  color: var(--text-muted);
}

.form-hint[data-state="error"] {
  color: #a83227;
}

.form-hint[data-state="success"] {
  color: #0f7b57;
}

.bbox-stats {
  margin-block-start: 0;
}

.btn-block {
  width: 100%;
}

.job-panel {
  display: grid;
  gap: 0.55rem;
  padding: 0.85rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--light);
}

.progress {
  height: 0.5rem;
  overflow: hidden;
  border-radius: 999px;
  background: var(--border);
}

.progress-bar {
  width: 0;
  height: 100%;
  border-radius: inherit;
  background: var(--primary);
  transition: width 0.25s ease;
}

.progress-bar[data-state="error"] {
  background: var(--danger);
}

.progress-bar[data-state="success"] {
  background: var(--success);
}

.job-status {
  font-size: 0.8rem;
  color: var(--text-muted);
}

.job-status[data-state="error"] {
  color: #a83227;
}

.job-status[data-state="success"] {
  color: #0f7b57;
}

.job-result {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.82rem;
  color: var(--dark);
}

/* Rectangle handles: plain Leaflet div icons, no drawing plugin. */
.clip-handle {
  border: 2px solid var(--primary-dark);
  border-radius: 2px;
  background: var(--surface);
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.9);
  cursor: grab;
}

.clip-handle:active {
  cursor: grabbing;
}

.leaflet-container .clip-rect {
  cursor: move;
}

.leaflet-container.is-drawing,
.leaflet-container.is-drawing .leaflet-interactive {
  cursor: crosshair;
}

/* --- DOA time series ------------------------------------------------------- */

.map-series {
  height: clamp(320px, 46vh, 520px);
}

.series-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  padding: 0.85rem 1.15rem;
  border-block-start: 1px solid var(--border-soft);
}

.series-chips-empty {
  color: var(--text-muted);
  font-size: 0.82rem;
}

.series-chip {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.35rem 0.5rem;
  border: 1px solid var(--border);
  border-inline-start: 4px solid var(--chip-color, var(--primary));
  border-radius: var(--radius);
  background: var(--surface);
}

.series-chip-dot {
  flex: none;
  width: 0.7rem;
  height: 0.7rem;
  border-radius: 50%;
  background: var(--chip-color, var(--primary));
}

.series-chip-name {
  max-width: 11rem;
  overflow: hidden;
  font-size: 0.82rem;
  font-weight: 700;
  color: var(--dark);
  text-overflow: ellipsis;
  white-space: nowrap;
}

.series-chip-input {
  width: 6.5rem;
  min-height: 1.9rem;
  padding: 0.2rem 0.4rem;
  font-size: 0.78rem;
  font-variant-numeric: tabular-nums;
}

.series-chip-remove {
  width: 1.9rem;
  height: 1.9rem;
}

.series-chip-remove svg {
  width: 0.95rem;
  height: 0.95rem;
}

.chart-series {
  min-height: 260px;
  max-height: 420px;
}

.series-table-wrapper {
  max-block-size: 20rem;
  margin-block-start: 1rem;
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
}

/* --- About ---------------------------------------------------------------- */

.about-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 21rem;
  gap: 1.25rem;
  align-items: start;
}

.about-main > .card + .card {
  margin-block-start: 1rem;
}

.disclosure {
  overflow: hidden;
}

.disclosure > summary {
  cursor: pointer;
  list-style: none;
}

.disclosure > summary::-webkit-details-marker {
  display: none;
}

.disclosure-icon {
  flex: none;
  width: 1.1rem;
  height: 1.1rem;
  color: var(--text-muted);
  transition: transform 0.15s ease;
}

.disclosure[open] .disclosure-icon {
  transform: rotate(180deg);
}

.credit-list,
.pill-list,
.check-list {
  list-style: none;
}

.credit-list li {
  padding-block: 0.55rem;
  border-block-end: 1px solid var(--border-soft);
  font-size: 0.9rem;
}

.credit-list li:last-child {
  border-block-end: 0;
}

.reference-list {
  padding-inline-start: 1.15rem;
  font-size: 0.88rem;
}

.reference-list li + li {
  margin-block-start: 0.65rem;
}

.pill-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-block: 1rem;
}

.check-list {
  display: grid;
  gap: 0.45rem;
  margin-block: 0.75rem;
}

.check-list li {
  position: relative;
  padding-inline-start: 1.4rem;
  font-size: 0.9rem;
}

.check-list li::before {
  content: "";
  position: absolute;
  inset-block-start: 0.45rem;
  inset-inline-start: 0.2rem;
  width: 0.5rem;
  height: 0.28rem;
  border-inline-start: 2px solid var(--success);
  border-block-end: 2px solid var(--success);
  transform: rotate(-45deg);
}

/* --- Footer --------------------------------------------------------------- */

.app-footer {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin-block-start: 1.5rem;
  padding-block: 1rem;
  border-block-start: 1px solid var(--border);
  color: var(--text-muted);
  font-size: 0.72rem;
}

/* --- Responsive ----------------------------------------------------------- */

@media (max-width: 1100px) {
  .map-layout,
  .about-layout {
    grid-template-columns: minmax(0, 1fr);
  }

  .point-panel {
    max-width: none;
  }
}

@media (max-width: 640px) {
  .page-header {
    flex-direction: column;
  }

  .page-actions {
    flex-wrap: wrap;
  }

  .card-header,
  .card-toolbar,
  .map-toolbar,
  .table-footer {
    align-items: flex-start;
    flex-direction: column;
  }

  .card-meta {
    text-align: start;
  }

  .card-toolbar .field-search,
  .card-toolbar .field-page-size {
    flex: 0 0 auto;
    width: 100%;
  }

  .map-toolbar .field,
  .map-toolbar .field-opacity,
  .map-toolbar .field-action {
    flex: 1 1 100%;
    max-width: none;
  }

  .map {
    height: 68vh;
  }

  .point-values {
    grid-template-columns: minmax(0, 1fr);
  }

  .app-footer {
    flex-direction: column;
    align-items: flex-start;
  }
}
