/* Bat Sensor - hand-written stylesheet, no build step.
   Plain HTML elements are styled by selector wherever possible; the small
   set of classes below only exist for things a bare element can't express
   (status badges, cards, buttons, the media gallery grid). */

:root {
  --color-bg: #f3f4f6;
  --color-surface: #ffffff;
  --color-border: #e5e7eb;
  --color-text: #111827;
  --color-text-muted: #6b7280;
  --color-primary: #1f2937;
  --color-primary-hover: #374151;
  --color-danger: #dc2626;
  --color-danger-hover: #b91c1c;
  --color-success-bg: #f0fdf4;
  --color-success-border: #bbf7d0;
  --color-success-text: #166534;
  --color-green-bg: #dcfce7;
  --color-green-text: #166534;
  --color-gray-bg: #f3f4f6;
  --color-gray-text: #4b5563;
  --color-red-bg: #fee2e2;
  --color-red-text: #991b1b;
  --color-amber-bg: #fffbeb;
  --color-amber-border: #fde68a;
  --color-amber-text: #92400e;
  --radius: 0.5rem;
  --shadow: 0 1px 2px 0 rgb(0 0 0 / 0.05);
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* {
  box-sizing: border-box;
}

html {
  font-family: var(--font-sans);
}

body {
  margin: 0;
  background: var(--color-bg);
  color: var(--color-text);
  line-height: 1.5;
  font-size: 15px;
}

h1, h2, h3 {
  font-weight: 600;
  line-height: 1.25;
  margin: 0;
}

h2 {
  font-size: 1.125rem;
}

p {
  margin: 0 0 0.5rem;
}

a {
  color: inherit;
}

img {
  max-width: 100%;
  display: block;
}

/* Layout */

.site-header {
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
}

.site-nav {
  max-width: 80rem;
  margin: 0 auto;
  padding: 0 1.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 4rem;
}

.site-nav__group {
  display: flex;
  align-items: center;
  gap: 1.5rem;
}

.site-nav__brand {
  font-weight: 700;
  font-size: 1.1rem;
  text-decoration: none;
  color: var(--color-text);
}

.site-nav__links {
  display: flex;
  align-items: center;
  gap: 1.5rem;
}

.site-nav__link {
  color: var(--color-text-muted);
  text-decoration: none;
  font-size: 0.9rem;
  padding: 0.25rem 0;
  border-bottom: 2px solid transparent;
}

.site-nav__link:hover {
  color: var(--color-text);
}

.site-nav__link[aria-current="page"] {
  color: var(--color-text);
  border-bottom-color: var(--color-primary);
}

.page-header {
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
}

.page-header__inner {
  max-width: 80rem;
  margin: 0 auto;
  padding: 1.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
}

main {
  display: block;
}

.container {
  max-width: 80rem;
  margin: 0 auto;
  padding: 2rem 1.5rem;
}

.container--narrow {
  max-width: 32rem;
}

.container--medium {
  max-width: 64rem;
}

.stack {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.hidden {
  display: none !important;
}

.text-muted {
  color: var(--color-text-muted);
  font-size: 0.9rem;
}

.header-title {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.actions-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
}

.inline-form-row {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 0.75rem;
  margin-top: 1rem;
  padding-top: 1rem;
  border-top: 1px solid var(--color-border);
}

.inline-form-row .field {
  margin: 0;
}

.inline-form-row label {
  font-size: 0.8rem;
  color: var(--color-text-muted);
}

.inline-form-row input[type="number"] {
  width: 6rem;
}

/* Cards */

.card {
  background: var(--color-surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.5rem;
}

.card > * + * {
  margin-top: 1rem;
}

.card > section + section {
  margin-top: 1.5rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--color-border);
}

/* Flash / status messages */

.flash {
  background: var(--color-success-bg);
  border: 1px solid var(--color-success-border);
  color: var(--color-success-text);
  border-radius: var(--radius);
  padding: 1rem;
  font-size: 0.9rem;
}

.flash--warning {
  background: var(--color-amber-bg);
  border-color: var(--color-amber-border);
  color: var(--color-amber-text);
}

.token-box {
  display: block;
  margin-top: 0.5rem;
  padding: 0.6rem 0.75rem;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.85rem;
  word-break: break-all;
}

.flash-saved {
  font-size: 0.9rem;
  color: var(--color-text-muted);
  animation: fade-out 0.5s ease-in 1.5s forwards;
}

@keyframes fade-out {
  to {
    opacity: 0;
  }
}

/* Buttons */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  border: 1px solid transparent;
  border-radius: var(--radius);
  padding: 0.5rem 1rem;
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  text-decoration: none;
  line-height: 1.25;
}

.btn-primary {
  background: var(--color-primary);
  color: #fff;
}

.btn-primary:hover {
  background: var(--color-primary-hover);
}

.btn-secondary {
  background: var(--color-gray-bg);
  color: var(--color-text);
}

.btn-secondary:hover {
  background: #e5e7eb;
}

.btn-danger {
  background: var(--color-danger);
  color: #fff;
}

.btn-danger:hover {
  background: var(--color-danger-hover);
}

.btn-link {
  background: none;
  border: none;
  padding: 0;
  color: var(--color-text-muted);
  text-decoration: underline;
  font-size: 0.875rem;
}

.btn-link--danger {
  color: var(--color-danger);
}

/* Forms */

form .field + .field {
  margin-top: 1.25rem;
}

label {
  display: block;
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--color-text);
  margin-bottom: 0.25rem;
}

input[type="text"],
input[type="email"],
input[type="password"],
input[type="number"],
textarea,
select {
  width: 100%;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  font-size: 0.9rem;
  font-family: inherit;
  background: var(--color-surface);
  color: var(--color-text);
}

input:focus,
textarea:focus,
select:focus {
  outline: 2px solid var(--color-primary);
  outline-offset: 1px;
}

input[type="checkbox"] {
  width: auto;
  margin-right: 0.4rem;
}

.field-error {
  color: var(--color-danger);
  font-size: 0.8rem;
  margin: 0.35rem 0 0;
  padding: 0;
  list-style: none;
}

.field-hint {
  color: var(--color-text-muted);
  font-size: 0.8rem;
  margin-top: 0.35rem;
}

.checkbox-list {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  margin-top: 0.5rem;
}

.checkbox-list label {
  display: flex;
  align-items: center;
  font-weight: 400;
  margin-bottom: 0;
}

.form-actions {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-top: 1.5rem;
}

/* Tables */

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.875rem;
}

th {
  text-align: left;
  color: var(--color-text-muted);
  font-weight: 500;
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--color-border);
}

td {
  padding: 0.75rem;
  border-bottom: 1px solid var(--color-border);
}

tr:last-child td {
  border-bottom: none;
}

.table-actions {
  text-align: right;
}

.table-actions form {
  display: inline;
}

.table-actions > * + * {
  margin-left: 0.75rem;
}

.table-wrap {
  overflow-x: auto;
}

/* Pagination */

.pagination {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-top: 1rem;
}

.pagination__status {
  font-size: 0.85rem;
}

.btn.is-disabled {
  opacity: 0.5;
  pointer-events: none;
  cursor: not-allowed;
}

/* Badges */

.badge {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 0.15rem 0.6rem;
  font-size: 0.75rem;
  font-weight: 500;
}

.badge-green {
  background: var(--color-green-bg);
  color: var(--color-green-text);
}

.badge-gray {
  background: var(--color-gray-bg);
  color: var(--color-gray-text);
}

.badge-red {
  background: var(--color-red-bg);
  color: var(--color-red-text);
}

/* Signal strength bars */

.signal-bars {
  display: inline-flex;
  align-items: flex-end;
  gap: 2px;
  height: 0.8rem;
  vertical-align: middle;
}

.signal-bars__bar {
  width: 3px;
  background: var(--color-border);
  border-radius: 1px;
}

.signal-bars__bar--1 {
  height: 25%;
}

.signal-bars__bar--2 {
  height: 50%;
}

.signal-bars__bar--3 {
  height: 75%;
}

.signal-bars__bar--4 {
  height: 100%;
}

.signal-bars__bar.is-filled {
  background: var(--color-green-text);
}

/* Device location map (Leaflet) */

.device-map {
  height: 320px;
  border-radius: var(--radius);
  overflow: hidden;
}

/* Device list (dashboard) */

.device-list {
  display: flex;
  flex-direction: column;
  background: var(--color-surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.device-list__item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1.5rem;
  text-decoration: none;
  color: inherit;
  border-bottom: 1px solid var(--color-border);
}

.device-list__item:last-child {
  border-bottom: none;
}

.device-list__item:hover {
  background: #fafafa;
}

.device-list__name {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-weight: 500;
}

.device-list__meta {
  color: var(--color-text-muted);
  font-size: 0.85rem;
  margin-top: 0.25rem;
}

.device-list__arrow {
  color: var(--color-text-muted);
}

/* Media gallery */

/* Stacked on narrow screens; a two-column split on desktop (see media query
   below). The right column (spectrogram + before/after) keeps the same width
   and left edge whether or not the detection has a video, so spectrograms line
   up across detections. */
.detection__media {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  margin-top: 0.875rem;
}

.detection__primary,
.detection__side {
  min-width: 0;
}

.detection__side {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.media-tile {
  display: flex;
  flex-direction: column;
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 0.5rem;
}

.media-tile--video video {
  display: block;
  width: 100%;
  border-radius: calc(var(--radius) - 2px);
}

.media-tile--audio audio {
  width: 100%;
}

@media (min-width: 72rem) {
  .detection__media {
    flex-direction: row;
    align-items: flex-start;
  }

  /* Left column holds the video near its native size (the camera is 640x480);
     an audio-only detection's player fills the same column so the right column
     lines up in both cases. */
  .detection__primary {
    flex: 1 1 40rem;
    max-width: 40rem;
  }

  .detection__side {
    flex: 1 1 24rem;
    max-width: 30rem;
  }

  /* With a video present, stretch the side column to the video's height and let
     the before/after tile grow to fill whatever the spectrogram leaves, so the
     two stacked panels together match the video's height. */
  .detection__media.has-video {
    align-items: stretch;
  }

  /* flex-basis 0 (not auto) so the tile takes exactly the height the
     spectrogram leaves rather than its full image height - that's what makes
     the column collapse to the video's height instead of overshooting it. */
  .detection__media.has-video .media-tile--frames {
    flex: 1 1 0;
    min-height: 0;
  }

  .detection__media.has-video .media-tile--frames .frame-swap {
    flex: 1 1 0;
    min-height: 0;
  }

  .detection__media.has-video .media-tile--frames .frame-swap__img {
    height: 100%;
    object-fit: cover;
  }
}

/* Detection video clips never have an embedded audio track (audio is always
   a separate file - see detection-media-sync.js), so the native mute/volume
   button can never do anything and browsers grey it out. Hide it rather than
   leave a permanently non-functional control on screen. */
.media-tile--video video::-webkit-media-controls-mute-button,
.media-tile--video video::-webkit-media-controls-volume-slider,
.media-tile--video video::-webkit-media-controls-volume-control-container {
  display: none !important;
}

.media-tile__footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin-top: 0.5rem;
  font-size: 0.75rem;
  color: var(--color-text-muted);
}

.media-tile__links {
  display: inline-flex;
  gap: 0.75rem;
}

.media-audio-controls {
  display: flex;
  align-items: center;
  gap: 0.375rem;
  margin-top: 0.5rem;
  padding: 0.25rem 0.5rem;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: calc(var(--radius) - 2px);
}

.media-audio-controls__mute {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
  padding: 0.25rem;
  color: var(--color-text-muted);
}

.media-audio-controls__mute:hover {
  color: var(--color-text);
}

.media-audio-controls__volume {
  flex: 1;
  width: auto;
}

/* Before / at-detection frame comparator */

.frame-swap {
  position: relative;
  cursor: pointer;
  border-radius: calc(var(--radius) - 2px);
  overflow: hidden;
}

.frame-swap__img {
  display: block;
  width: 100%;
}

.frame-swap__img.is-hidden {
  display: none;
}

.frame-swap__controls {
  gap: 0;
  border: 1px solid var(--color-border);
  border-radius: calc(var(--radius) - 2px);
  overflow: hidden;
  padding: 0;
  justify-content: stretch;
}

.frame-swap__btn {
  flex: 1;
  padding: 0.3rem 0.5rem;
  background: var(--color-surface);
  border: none;
  cursor: pointer;
  font: inherit;
  font-size: 0.75rem;
  color: var(--color-text-muted);
}

.frame-swap__btn + .frame-swap__btn {
  border-left: 1px solid var(--color-border);
}

.frame-swap__btn.is-active {
  background: var(--color-primary);
  color: #fff;
}

.spectrogram {
  display: flex;
  flex-direction: column;
  width: 100%;
}

.spectrogram__plot {
  display: flex;
  gap: 0.4rem;
}

.spectrogram__freq-axis {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  font-size: 0.65rem;
  line-height: 1;
  color: var(--color-text-muted);
  text-align: right;
  white-space: nowrap;
}

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

.spectrogram__img {
  display: block;
  width: 100%;
  border-radius: calc(var(--radius) - 2px);
}

.spectrogram__time-axis {
  position: relative;
  height: 0.8rem;
  font-size: 0.65rem;
  color: var(--color-text-muted);
}

.spectrogram__time-axis span {
  position: absolute;
  top: 0;
  white-space: nowrap;
}

.spectrogram__legend {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.65rem;
  color: var(--color-text-muted);
}

.spectrogram__legend-bar {
  flex: 1;
  height: 0.55rem;
  border-radius: 2px;
  border: 1px solid var(--color-border);
  background: linear-gradient(to right, #000004, #420a68, #932667, #dd513a, #fca50a, #fcffa4);
}

.detection {
  padding: 1rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
}

.detection + .detection {
  margin-top: 0.75rem;
}

.detection:target {
  background: var(--color-amber-bg);
  border-color: var(--color-amber-bg);
}

.detection__header {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.detection__time {
  font-size: 0.875rem;
  font-weight: 500;
}

.detection__chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.375rem;
  margin-top: 0.625rem;
}

.detection__notes {
  display: block;
  margin-top: 0.625rem;
  padding: 0.25rem 0.5rem;
  border-radius: calc(var(--radius) - 2px);
  background: var(--color-bg);
  color: var(--color-text-muted);
  font-size: 0.75rem;
  overflow-wrap: anywhere;
}

/* Live view */

.live-view-frame {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 8rem;
}

#live-view img {
  max-width: 100%;
  border-radius: var(--radius);
  border: 1px solid var(--color-border);
}

.spinner {
  width: 2rem;
  height: 2rem;
  border: 3px solid var(--color-border);
  border-top-color: var(--color-primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

/* WiFi provisioning */

.wifi-network-list {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  margin: 0.75rem 0;
  max-width: 28rem;
}

.wifi-network {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.45rem 0.65rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  cursor: pointer;
}

.wifi-network input[type="radio"] {
  width: auto;
  margin: 0;
}

.wifi-network__signal {
  margin-left: auto;
  color: var(--color-text-muted);
  font-size: 0.8rem;
  white-space: nowrap;
}

#wifi-connect-form .field {
  max-width: 20rem;
}

.wifi-status--ok {
  color: var(--color-success-text);
}

.wifi-status--error {
  color: var(--color-danger);
}

/* Metric charts */

.metric-charts {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 1rem;
}

.metric-chart {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.metric-chart h4 {
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--color-text-muted);
  margin: 0;
}

.metric-chart__svg {
  width: 100%;
  height: 80px;
  display: block;
}

.metric-chart__line {
  fill: none;
  stroke: var(--color-primary);
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.metric-chart__end-dot {
  fill: var(--color-primary);
}

.metric-chart__crosshair {
  stroke: var(--color-text-muted);
  stroke-width: 1;
  pointer-events: none;
}

.metric-chart__axis {
  display: flex;
  justify-content: space-between;
  font-size: 0.65rem;
  color: var(--color-text-muted);
}

.metric-chart__tooltip {
  position: absolute;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 0.3rem 0.55rem;
  font-size: 0.75rem;
  pointer-events: none;
  white-space: nowrap;
}

.metric-chart__empty {
  color: var(--color-text-muted);
  font-size: 0.85rem;
}

/* Details/summary based nav dropdown + mobile menu (replaces Alpine) */

.user-menu {
  position: relative;
}

.user-menu > summary {
  list-style: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.875rem;
  color: var(--color-text-muted);
  padding: 0.4rem 0.75rem;
  border-radius: var(--radius);
}

.user-menu > summary:hover {
  color: var(--color-text);
  background: var(--color-gray-bg);
}

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

.user-menu[open] > summary {
  background: var(--color-gray-bg);
}

.user-menu__panel {
  position: absolute;
  right: 0;
  margin-top: 0.5rem;
  min-width: 12rem;
  background: var(--color-surface);
  border-radius: var(--radius);
  box-shadow: 0 10px 15px -3px rgb(0 0 0 / 0.1);
  border: 1px solid var(--color-border);
  overflow: hidden;
  z-index: 50;
}

.user-menu__panel a,
.user-menu__panel button {
  display: block;
  width: 100%;
  text-align: left;
  padding: 0.6rem 1rem;
  font-size: 0.875rem;
  color: var(--color-text);
  text-decoration: none;
  background: none;
  border: none;
  cursor: pointer;
}

.user-menu__panel a:hover,
.user-menu__panel button:hover {
  background: var(--color-gray-bg);
}

.nav-toggle {
  display: none;
  position: relative;
}

.nav-toggle > summary {
  list-style: none;
  cursor: pointer;
  padding: 0.5rem;
  font-size: 1.25rem;
}

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

.nav-toggle__panel {
  position: absolute;
  top: 100%;
  right: 0;
  min-width: 12rem;
  max-width: calc(100vw - 2rem);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  box-shadow: 0 10px 15px -3px rgb(0 0 0 / 0.1);
  padding: 0.5rem;
  z-index: 50;
}

.nav-toggle__panel a,
.nav-toggle__panel button {
  display: block;
  width: 100%;
  text-align: left;
  padding: 0.5rem 0.75rem;
  color: var(--color-text);
  text-decoration: none;
  background: none;
  border: none;
  font-size: 0.9rem;
  cursor: pointer;
  border-radius: var(--radius);
}

.nav-toggle__panel a:hover,
.nav-toggle__panel button:hover {
  background: var(--color-gray-bg);
}

@media (max-width: 640px) {
  .site-nav__links,
  .site-nav__user {
    display: none;
  }

  .nav-toggle {
    display: block;
  }
}

/* Guest (auth) layout */

.guest-layout {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  gap: 1.5rem;
}

.guest-layout__brand {
  font-weight: 700;
  font-size: 1.25rem;
  text-decoration: none;
  color: var(--color-text);
}

.guest-layout__card {
  width: 100%;
  max-width: 24rem;
  background: var(--color-surface);
  padding: 1.5rem;
  border-radius: var(--radius);
  box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1);
}

/* Native <dialog> based modal (replaces Alpine modal) */

dialog {
  border: none;
  border-radius: var(--radius);
  box-shadow: 0 25px 50px -12px rgb(0 0 0 / 0.25);
  padding: 0;
  max-width: 28rem;
  width: calc(100% - 2rem);
}

dialog::backdrop {
  background: rgb(0 0 0 / 0.5);
}

dialog .card {
  box-shadow: none;
}
