:root {
  --bg: #0b0f14;
  --panel: #141b26;
  --card: #1a2332;
  --text: #e8edf5;
  --muted: #8b9bb4;
  --accent: #3b82f6;
  --ok: #22c55e;
  --warn: #eab308;
  --err: #ef4444;
  --header-h: 52px;
  --tabs-h: 0px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  min-height: 100dvh;
  overflow-x: hidden;
}

/* —— Header —— */
.app-header {
  position: sticky;
  top: 0;
  z-index: 1000;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px 12px;
  align-items: center;
  padding: 10px 14px;
  padding-top: max(10px, env(safe-area-inset-top));
  background: rgba(20, 27, 38, 0.94);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid #243044;
}

.header-brand {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 6px 10px;
  min-width: 0;
}

.header-brand h1 {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 800;
  white-space: nowrap;
}

.header-meta {
  color: var(--muted);
  font-size: 0.8rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.badge {
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  background: #334155;
  white-space: nowrap;
}

.badge.ok { background: rgba(34, 197, 94, 0.2); color: var(--ok); }
.badge.warn { background: rgba(234, 179, 8, 0.2); color: var(--warn); }
.badge.err { background: rgba(239, 68, 68, 0.2); color: var(--err); }

.header-link {
  color: var(--muted);
  font-size: 0.82rem;
  text-decoration: none;
  padding: 6px 8px;
  border-radius: 8px;
}

.header-link:hover { color: var(--text); background: rgba(255, 255, 255, 0.06); }

.auto-conn-hint {
  grid-column: 1 / -1;
  margin: 0;
  font-size: 0.8rem;
  line-height: 1.4;
  color: var(--muted);
}

.conn-details {
  grid-column: 1 / -1;
}

.conn-details summary {
  cursor: pointer;
  font-size: 0.8rem;
  color: var(--muted);
  list-style: none;
  user-select: none;
}

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

.conn-row {
  display: flex;
  gap: 8px;
  margin-top: 8px;
  flex-wrap: wrap;
}

.conn-row input[type="text"] {
  flex: 1;
  min-width: 0;
  background: #0f1419;
  border: 1px solid #334155;
  color: var(--text);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 0.85rem;
}

button, select {
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 0.9rem;
  cursor: pointer;
  min-height: 44px;
}

button.secondary, select.secondary {
  background: #334155;
  color: var(--text);
}

/* —— Mobile bottom tabs —— */
.mobile-tabs {
  display: none;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  grid-template-columns: repeat(4, 1fr);
  gap: 2px;
  padding: 6px 8px;
  padding-bottom: max(6px, var(--safe-bottom));
  background: rgba(20, 27, 38, 0.96);
  backdrop-filter: blur(12px);
  border-top: 1px solid #243044;
}

.mobile-tabs button {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: 48px;
  padding: 6px 4px;
  background: transparent;
  color: var(--muted);
  font-size: 0.65rem;
  font-weight: 600;
  border-radius: 10px;
}

.mobile-tabs button .tab-icon {
  font-size: 1.15rem;
  line-height: 1;
}

.mobile-tabs button.active {
  background: rgba(59, 130, 246, 0.18);
  color: #93c5fd;
}

/* —— Main layout —— */
.app-main {
  display: grid;
  min-height: calc(100dvh - var(--header-h));
  grid-template-columns: minmax(260px, 320px) minmax(0, 1.4fr);
  grid-template-rows: minmax(42vh, 1fr) auto;
  grid-template-areas:
    "sidebar map"
    "sidebar charts";
}

.panel {
  min-height: 0;
  min-width: 0;
}

.panel-sidebar { grid-area: sidebar; }
.panel-map { grid-area: map; }
.panel-charts { grid-area: charts; }

.sidebar {
  background: var(--panel);
  border-right: 1px solid #243044;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.stats-strip {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  padding: 12px;
  border-bottom: 1px solid #243044;
}

.stat-card {
  background: var(--card);
  border-radius: 10px;
  padding: 10px;
  min-width: 0;
}

.stat-card .label {
  font-size: 0.68rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.stat-card .value {
  font-size: clamp(1rem, 2.5vw, 1.35rem);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  margin-top: 2px;
  line-height: 1.15;
}

.stat-card .sub {
  font-size: 0.8rem;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.signal-strip {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 0 12px 10px;
  border-bottom: 1px solid #243044;
}

.signal-pill {
  font-size: 0.78rem;
  font-weight: 600;
  padding: 8px 10px;
  border-radius: 8px;
  background: rgba(148, 163, 184, 0.12);
  color: var(--muted);
  line-height: 1.35;
  word-break: break-word;
}

.signal-pill.quality-excellent { background: rgba(34, 197, 94, 0.18); color: #4ade80; }
.signal-pill.quality-good { background: rgba(52, 211, 153, 0.16); color: #6ee7b7; }
.signal-pill.quality-medium { background: rgba(234, 179, 8, 0.18); color: #facc15; }
.signal-pill.quality-poor { background: rgba(249, 115, 22, 0.18); color: #fb923c; }
.signal-pill.quality-unknown { background: rgba(148, 163, 184, 0.12); color: var(--muted); }

.toolbar {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  padding: 10px 12px;
  border-bottom: 1px solid #243044;
  align-items: center;
}

.toolbar label {
  font-size: 0.8rem;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 6px;
}

.toolbar select { min-height: 40px; padding: 8px 10px; }

.sector-list {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 8px;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  min-height: 0;
}

.sector-row {
  display: grid;
  grid-template-columns: 4px 44px minmax(64px, 1fr) minmax(68px, auto) minmax(52px, auto);
  gap: 8px;
  align-items: center;
  padding: 10px;
  margin-bottom: 6px;
  background: var(--card);
  border-radius: 10px;
  font-variant-numeric: tabular-nums;
}

.sector-row.current {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}

.sector-row.finished .sector-time {
  font-weight: 800;
}

.sector-bar { min-height: 36px; border-radius: 3px; align-self: stretch; }
.sector-label { font-weight: 700; font-size: 0.9rem; }
.sector-time { font-size: 1.05rem; font-weight: 800; }
.sector-ref {
  font-size: 0.88rem;
  font-weight: 700;
  color: white;
  background: rgba(0, 0, 0, 0.75);
  padding: 5px 8px;
  border-radius: 6px;
  text-align: center;
}
.sector-delta { font-size: 0.78rem; font-weight: 700; text-align: right; }

.panel-map {
  grid-area: map;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.map-wrap {
  position: relative;
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 280px;
  background: #0f1419;
}

#map {
  flex: 1;
  width: 100%;
  min-height: 280px;
}

.map-controls {
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 500;
  background: rgba(20, 27, 38, 0.92);
  padding: 10px 12px;
  border-radius: 10px;
  font-size: 0.85rem;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.25);
}

.photo-panel {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  padding: 10px 12px 12px;
  border-top: 1px solid #243044;
  background: #111827;
  flex-shrink: 0;
}

.panel-photos {
  background: #111827;
  overflow-y: auto;
}

.panel-photos .photo-panel {
  border-top: none;
  min-height: 100%;
  align-content: start;
}

@media (min-width: 1025px) {
  .panel-map { display: flex !important; flex-direction: column; }
  .panel-sidebar { display: flex !important; flex-direction: column; }
  .panel-charts { display: block !important; }
  .panel-photos { display: none !important; }
  .mobile-tabs { display: none !important; }
}

@media (max-width: 1024px) {
  .panel-photos .photo-panel {
    grid-template-columns: 1fr;
    padding: 12px;
  }

  .panel-photos .photo-card img {
    height: clamp(160px, 32vh, 280px);
  }
}

.photo-card {
  background: var(--card);
  border-radius: 10px;
  overflow: hidden;
}

.photo-card .photo-head {
  padding: 6px 10px;
  font-size: 0.76rem;
  color: var(--muted);
}

.photo-card img {
  width: 100%;
  height: clamp(160px, 24vh, 280px);
  object-fit: cover;
  display: block;
  background: #0b0f14;
}

.photo-gallery {
  grid-column: 1 / -1;
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding-bottom: 2px;
  -webkit-overflow-scrolling: touch;
}

.photo-gallery img {
  width: 76px;
  height: 56px;
  object-fit: cover;
  border-radius: 6px;
  cursor: pointer;
  flex: 0 0 auto;
  border: 1px solid #334155;
}

/* —— Charts —— */
.charts {
  padding: 12px 14px 16px;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  background: #0f1419;
  border-top: 1px solid #243044;
  max-height: min(52vh, 560px);
  overflow-y: auto;
}

.chart-box {
  background: var(--card);
  border-radius: 12px;
  padding: 10px;
  height: clamp(160px, 22vh, 220px);
  min-height: 160px;
}

.analysis {
  grid-column: 1 / -1;
  background: var(--card);
  border-radius: 12px;
  padding: 12px 14px;
  display: grid;
  gap: 6px;
}

.analysis-item {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  font-size: 0.88rem;
  color: var(--muted);
}

.analysis-item span:last-child {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#toast {
  position: fixed;
  bottom: calc(16px + var(--tabs-h) + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%) translateY(80px);
  background: #1e293b;
  border: 1px solid #334155;
  padding: 12px 18px;
  border-radius: 12px;
  opacity: 0;
  transition: all 0.35s ease;
  z-index: 2000;
  max-width: min(92vw, 420px);
  text-align: center;
  font-size: 0.9rem;
}

#toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* —— Desktop wide —— */
@media (min-width: 1200px) {
  .app-main {
    grid-template-columns: minmax(280px, 340px) minmax(0, 1.5fr) minmax(300px, 380px);
    grid-template-rows: 1fr;
    grid-template-areas: "sidebar map charts";
  }

  .photo-card img {
    height: clamp(200px, 30vh, 340px);
  }

  #map {
    min-height: 100%;
  }

  .charts {
    max-height: none;
    overflow-y: auto;
    border-top: none;
    border-left: 1px solid #243044;
    grid-template-columns: 1fr;
    align-content: start;
  }

  .chart-box {
    height: 200px;
  }

  .conn-details { display: block; }
  .conn-details summary { display: none; }
  .conn-row { margin-top: 0; }
}

@media (min-width: 960px) {
  .conn-details[open] .conn-row,
  .conn-details .conn-row {
    display: flex;
  }
}

/* —— Desktop / großes Tablet quer —— */
@media (max-width: 1199px) and (min-width: 1025px) {
  .app-main {
    grid-template-columns: minmax(260px, 300px) minmax(0, 1fr);
    grid-template-rows: minmax(320px, 48vh) auto;
  }

  .map-wrap,
  #map {
    min-height: 320px;
  }

  .charts {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .analysis { grid-column: 1 / -1; }
}

/* —— iPad & Mobile (Tabs) —— */
@media (max-width: 1024px) {
  :root {
    --header-h: 88px;
    --tabs-h: 58px;
  }

  .app-header {
    grid-template-columns: 1fr auto;
    padding: 8px 12px;
    padding-top: max(8px, env(safe-area-inset-top));
  }

  .header-brand h1 { font-size: 0.95rem; }
  .header-meta { font-size: 0.72rem; max-width: 55vw; }
  .header-link span { display: none; }

  .mobile-tabs { display: grid; }

  .app-main {
    display: block;
    min-height: calc(100dvh - var(--header-h) - var(--tabs-h) - var(--safe-bottom));
    padding-bottom: 4px;
  }

  .panel {
    display: none;
    height: calc(100dvh - var(--header-h) - var(--tabs-h) - var(--safe-bottom) - 4px);
    overflow: hidden;
    min-height: 0;
  }

  .panel.active {
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }

  .panel-sidebar .sidebar,
  .panel-charts,
  .panel-photos {
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    min-height: 0;
  }

  .stats-strip {
    grid-template-columns: repeat(3, minmax(100px, 1fr));
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .sector-row {
    grid-template-columns: 4px 40px 1fr auto;
  }

  .sector-delta {
    grid-column: 4;
    grid-row: 1;
  }

  .sector-ref {
    grid-column: 3 / span 2;
    grid-row: 2;
    justify-self: start;
    margin-top: 2px;
  }

  .map-wrap {
    flex: 1;
    min-height: 0;
    height: 100%;
  }

  #map {
    min-height: 320px;
    flex: 1;
  }

  .panel-map.active .map-wrap,
  .panel-map.active #map {
    min-height: 0;
    height: 100%;
  }

  .photo-panel {
    grid-template-columns: 1fr 1fr;
    max-height: none;
    overflow: visible;
  }

  .panel-photos .photo-card img {
    height: clamp(200px, 36vh, 420px);
  }

  .panel-charts .charts {
    height: 100%;
    max-height: none;
    grid-template-columns: 1fr;
    overflow-y: auto;
    border-top: none;
  }

  .chart-box {
    height: 180px;
    min-height: 180px;
  }

  .conn-details .conn-row button {
    width: 100%;
  }
}

@media (max-width: 380px) {
  .stat-card .value { font-size: 0.95rem; }
  .sector-time { font-size: 0.95rem; }
}
