* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: linear-gradient(180deg, #87ceeb 0%, #b3e5fc 60%, #e1f5fe 100%);
  color: #1a237e;
}

#app {
  position: fixed;
  inset: 0;
}

canvas {
  display: block;
}

:root {
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --edge-gap: 20px;
  --mobile-edge-gap: 10px;
}

#brand-bar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: calc(10px + var(--safe-top)) calc(24px + var(--safe-right)) 10px calc(24px + var(--safe-left));
  /* IWMI signature blue — the white logo reads cleanly on top */
  background: linear-gradient(135deg, #0d47a1 0%, #1565c0 50%, #1976d2 100%);
  border-bottom: 3px solid #f57c00;
  z-index: 20;
  box-shadow: 0 2px 12px rgba(0,0,0,0.25);
}

#iwmi-logo,
.logo-fallback {
  height: 44px;
  width: auto;
}

.logo-fallback {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: white;
  color: #0d47a1;
  font-weight: 800;
  font-size: 18px;
  letter-spacing: 1.5px;
  padding: 8px 14px;
  border-radius: 6px;
  height: 44px;
  box-sizing: border-box;
}

.brand-text h1 {
  font-size: 19px;
  font-weight: 700;
  color: #ffffff;
  margin: 0 0 2px 0;
  letter-spacing: 0.2px;
}

.brand-text p {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.85);
  margin: 0;
  letter-spacing: 0.3px;
}

#ui {
  position: fixed;
  top: calc(86px + var(--safe-top));
  left: calc(var(--edge-gap) + var(--safe-left));
  background: rgba(248, 252, 255, 0.92);
  backdrop-filter: blur(12px);
  border: 1px solid rgba(13, 71, 161, 0.14);
  border-radius: 10px;
  padding: 14px 16px;
  box-shadow: 0 12px 30px rgba(13, 71, 161, 0.18), 0 2px 8px rgba(0, 0, 0, 0.08);
  max-width: 300px;
  max-height: calc(100dvh - 112px - var(--safe-top) - var(--safe-bottom));
  overflow: auto;
  z-index: 10;
  transition: opacity 0.22s, transform 0.22s, box-shadow 0.22s;
}

#ui h1 {
  font-size: 22px;
  font-weight: 700;
  margin-bottom: 2px;
  color: #0d47a1;
}

#ui .subtitle {
  font-size: 12px;
  color: #546e7a;
  margin-bottom: 14px;
}

.legend {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  margin-bottom: 14px;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 7px;
  min-width: 0;
  min-height: 30px;
  padding: 5px 7px;
  background: rgba(255, 255, 255, 0.72);
  border: 1px solid rgba(13, 71, 161, 0.08);
  border-radius: 7px;
  font-size: 11.5px;
  line-height: 1.15;
  color: #37474f;
}

.dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  display: inline-block;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}

.abc-legend {
  background: rgba(13, 71, 161, 0.08);
  border-radius: 8px;
  padding: 8px 10px;
  margin-bottom: 12px;
}

.abc-title {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: #0d47a1;
  margin-bottom: 5px;
}

.abc-row {
  font-size: 11px;
  color: #37474f;
  margin: 3px 0;
  display: flex;
  align-items: flex-start;
  gap: 6px;
  line-height: 1.3;
}

.abc-letter {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  background: #0d47a1;
  color: white;
  font-weight: 700;
  font-size: 10px;
  border-radius: 4px;
  flex-shrink: 0;
}

.controls {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  margin-bottom: 10px;
}

.controls button {
  background: #0d47a1;
  color: white;
  border: none;
  border-radius: 6px;
  min-height: 32px;
  padding: 6px 8px;
  font-size: 11.5px;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.2s, transform 0.08s, box-shadow 0.12s;
  box-shadow: 0 2px 6px rgba(13, 71, 161, 0.18);
}

.controls button:hover {
  background: #1565c0;
  box-shadow: 0 3px 9px rgba(13, 71, 161, 0.24);
}

.controls button:active {
  transform: translateY(1px);
}

.controls button:focus-visible {
  outline: 3px solid rgba(245, 124, 0, 0.4);
  outline-offset: 2px;
}

.controls button[aria-pressed="false"] {
  background: #607d8b;
}

.hint {
  font-size: 11px;
  color: #78909c;
  font-style: italic;
  line-height: 1.4;
}

.contact {
  font-size: 11px;
  color: #37474f;
  margin-top: 10px;
  padding-top: 8px;
  border-top: 1px solid rgba(13, 71, 161, 0.12);
  font-weight: 500;
}
.contact a {
  color: #0d47a1;
  text-decoration: none;
}
.contact a:hover {
  text-decoration: underline;
}

.copyright {
  font-size: 10px;
  color: #607d8b;
  margin-top: 4px;
  font-style: italic;
}

.credits {
  font-size: 10px;
  color: #90a4ae;
  margin-top: 6px;
  line-height: 1.35;
}

.credits a,
.contact a {
  color: #546e7a;
  text-decoration: underline;
}

#tour-start {
  position: fixed;
  right: calc(var(--edge-gap) + var(--safe-right));
  bottom: calc(var(--edge-gap) + var(--safe-bottom));
  background: #0d47a1;
  color: white;
  border: none;
  padding: 14px 26px;
  border-radius: 8px;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 6px 18px rgba(13, 71, 161, 0.45);
  z-index: 30;
  transition: transform 0.15s, background 0.2s, box-shadow 0.2s;
  animation: tour-pulse 2.2s ease-in-out infinite;
}
#tour-start:hover {
  background: #1565c0;
  transform: translateY(-2px) scale(1.03);
  animation-play-state: paused;
}
#tour-start.tour-hidden { display: none; }

body.welcome-active #brand-bar,
body.welcome-active #ui,
body.welcome-active #cam-controls,
body.welcome-active #tour-start {
  opacity: 0;
  pointer-events: none;
}

body.tour-active #cam-controls,
body.wasa-active #cam-controls {
  opacity: 0.2;
  pointer-events: none;
  transform: translateY(8px);
}

body.wasa-active #tour-start {
  opacity: 0.35;
  pointer-events: none;
}

@keyframes tour-pulse {
  0%, 100% { box-shadow: 0 6px 18px rgba(13, 71, 161, 0.45), 0 0 0 0 rgba(245, 124, 0, 0.55); }
  50%      { box-shadow: 0 6px 18px rgba(13, 71, 161, 0.45), 0 0 0 14px rgba(245, 124, 0, 0); }
}

/* ---------- WELCOME MODAL ---------- */
#welcome {
  position: fixed;
  inset: 0;
  padding: calc(16px + var(--safe-top)) calc(16px + var(--safe-right)) calc(16px + var(--safe-bottom)) calc(16px + var(--safe-left));
  background: rgba(13, 71, 161, 0.4);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  transition: opacity 0.3s;
}
#welcome.welcome-hidden { opacity: 0; pointer-events: none; }
.welcome-card {
  background: white;
  border-radius: 8px;
  padding: 28px 32px;
  width: min(440px, 100%);
  max-height: calc(100dvh - 32px - var(--safe-top) - var(--safe-bottom));
  overflow: auto;
  margin: 0 16px;
  box-shadow: 0 20px 50px rgba(0,0,0,0.35);
  text-align: center;
}
.welcome-card h2 {
  color: #0d47a1;
  font-size: 24px;
  margin-bottom: 10px;
}
.welcome-card p {
  color: #37474f;
  font-size: 14px;
  line-height: 1.55;
  margin-bottom: 12px;
}
.welcome-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
  flex-wrap: wrap;
  margin-top: 16px;
}

#welcome-close,
#welcome-tour {
  background: #0d47a1;
  color: white;
  border: none;
  border-radius: 8px;
  padding: 10px 22px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}

#welcome-close {
  background: #607d8b;
}

#welcome-close:hover,
#welcome-tour:hover { background: #1565c0; }

/* ---------- LOADER ---------- */
#loader {
  position: fixed;
  inset: 0;
  background: linear-gradient(180deg, #b3e5fc 0%, #87ceeb 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  z-index: 110;
  transition: opacity 0.5s;
}
#loader.loader-hidden { opacity: 0; pointer-events: none; }
#loader p { color: #0d47a1; font-weight: 600; letter-spacing: 0.5px; }
.loader-spinner {
  width: 48px;
  height: 48px;
  border: 4px solid rgba(13, 71, 161, 0.18);
  border-top-color: #0d47a1;
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- COLLAPSIBLE SIDE PANEL ---------- */
.ui-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 4px;
}
#ui-collapse {
  background: rgba(13, 71, 161, 0.1);
  color: #0d47a1;
  border: none;
  width: 28px;
  height: 28px;
  border-radius: 6px;
  font-size: 18px;
  font-weight: 700;
  cursor: pointer;
  line-height: 1;
}
#ui-collapse:hover { background: rgba(13, 71, 161, 0.2); }
#ui.ui-collapsed { padding: 14px 18px; }
#ui.ui-collapsed #ui-body { display: none; }

/* ---------- KBD BADGES IN HINT ---------- */
.hint kbd {
  background: #455a64;
  color: white;
  font-family: inherit;
  font-size: 9px;
  padding: 1px 5px;
  border-radius: 3px;
  margin: 0 1px;
  font-weight: 600;
  vertical-align: 1px;
}

#tour-panel {
  position: fixed;
  right: calc(var(--edge-gap) + var(--safe-right));
  bottom: calc(var(--edge-gap) + var(--safe-bottom));
  width: min(320px, calc(100vw - 40px));
  max-height: calc(100dvh - 112px - var(--safe-top) - var(--safe-bottom));
  overflow-y: auto;
  background: rgba(255,255,255,0.95);
  backdrop-filter: blur(8px);
  border-radius: 8px;
  padding: 14px 18px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.2);
  z-index: 30;
  font-family: inherit;
}
#tour-panel.tour-hidden { display: none; }
.tour-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 11px;
  color: #78909c;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 4px;
}
.tour-header-actions {
  display: flex;
  gap: 4px;
  align-items: center;
}
#tour-close, #tour-voice {
  background: none;
  border: none;
  font-size: 12px;
  font-weight: 700;
  color: #607d8b;
  cursor: pointer;
  line-height: 1;
  padding: 6px 8px;
  border-radius: 4px;
  transition: background 0.15s, color 0.15s;
}
#tour-close { font-size: 16px; }
#tour-close:hover { color: #d32f2f; }
#tour-voice:hover { background: rgba(13, 71, 161, 0.1); }
#tour-voice.voice-off { opacity: 0.4; }
#tour-title {
  font-size: 18px;
  font-weight: 700;
  color: #0d47a1;
  margin-bottom: 8px;
}
#tour-text {
  font-size: 13px;
  line-height: 1.5;
  color: #37474f;
  margin-bottom: 12px;
  min-height: 60px;
}
.tour-buttons {
  display: flex;
  gap: 6px;
}
.tour-buttons button {
  flex: 1;
  background: #0d47a1;
  color: white;
  border: none;
  border-radius: 6px;
  padding: 8px 6px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}
.tour-buttons button:hover { background: #1565c0; }
.tour-buttons button:disabled { background: #b0bec5; cursor: not-allowed; }

/* ---------- WASA INTERVENTIONS PANEL ---------- */
.wasa-open-btn {
  background: #f57c00 !important;
  font-weight: 700;
  letter-spacing: 0.3px;
}
.wasa-open-btn:hover { background: #ef6c00 !important; }

#wasa-panel {
  position: fixed;
  top: calc(86px + var(--safe-top));
  right: calc(var(--edge-gap) + var(--safe-right));
  width: min(380px, calc(100vw - 40px));
  max-height: calc(100dvh - 112px - var(--safe-top) - var(--safe-bottom));
  display: flex;
  flex-direction: column;
  background: rgba(255, 255, 255, 0.96);
  backdrop-filter: blur(10px);
  border-radius: 10px;
  padding: 18px 20px;
  box-shadow: 0 12px 32px rgba(13, 71, 161, 0.28);
  border-top: 4px solid #f57c00;
  z-index: 25;
  transform: translateX(0);
  transition: transform 0.32s cubic-bezier(.22,.61,.36,1), opacity 0.25s;
}
#wasa-panel.wasa-hidden {
  transform: translateX(calc(100% + 30px));
  opacity: 0;
  pointer-events: none;
}

.wasa-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 8px;
}
.wasa-eyebrow {
  display: block;
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: #f57c00;
  margin-bottom: 2px;
}
.wasa-title {
  font-size: 17px;
  font-weight: 700;
  color: #0d47a1;
  line-height: 1.25;
  margin: 0;
}
#wasa-close {
  background: none;
  border: none;
  font-size: 18px;
  font-weight: 700;
  color: #607d8b;
  cursor: pointer;
  line-height: 1;
  padding: 4px 8px;
  border-radius: 4px;
  flex-shrink: 0;
}
#wasa-close:hover { color: #d32f2f; background: rgba(211, 47, 47, 0.08); }

.wasa-intro {
  font-size: 12px;
  line-height: 1.5;
  color: #455a64;
  margin-bottom: 12px;
}

.wasa-cards {
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow-y: auto;
  padding-right: 4px;
  flex: 1 1 auto;
  min-height: 0;
}

.wasa-card {
  background: linear-gradient(180deg, #f5f9ff 0%, #ffffff 100%);
  border: 1px solid rgba(13, 71, 161, 0.12);
  border-left: 4px solid #1565c0;
  border-radius: 8px;
  padding: 10px 12px;
  transition: transform 0.12s, box-shadow 0.12s, border-left-color 0.12s;
}
.wasa-card:hover {
  transform: translateX(2px);
  box-shadow: 0 4px 14px rgba(13, 71, 161, 0.15);
  border-left-color: #f57c00;
}

.wasa-card-head {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 4px;
}
.wasa-icon {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #1565c0;
  color: white;
  font-size: 12px;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  box-shadow: 0 2px 4px rgba(13, 71, 161, 0.3);
}
.wasa-card h3 {
  font-size: 13px;
  font-weight: 700;
  color: #0d47a1;
  margin: 0;
  line-height: 1.25;
}

.wasa-card p {
  font-size: 11.5px;
  line-height: 1.5;
  color: #37474f;
  margin: 4px 0 8px 0;
}
.wasa-card .wasa-impact {
  font-size: 10.5px;
  color: #2e7d32;
  font-weight: 600;
  margin-bottom: 8px;
  display: flex;
  align-items: flex-start;
  gap: 4px;
}
.wasa-card .wasa-impact::before {
  content: "↑";
  font-weight: 700;
  flex-shrink: 0;
}
.wasa-focus-btn {
  background: #0d47a1;
  color: white;
  border: none;
  border-radius: 5px;
  padding: 5px 10px;
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}
.wasa-focus-btn:hover { background: #f57c00; }

.wasa-foot {
  margin-top: 10px;
  padding-top: 8px;
  border-top: 1px solid rgba(13, 71, 161, 0.1);
  font-size: 10px;
  color: #78909c;
  font-style: italic;
}

@media (max-width: 760px) {
  #wasa-panel {
    top: calc(68px + var(--safe-top));
    right: calc(var(--mobile-edge-gap) + var(--safe-right));
    left: calc(var(--mobile-edge-gap) + var(--safe-left));
    width: auto;
    max-height: calc(70dvh - var(--safe-top));
    padding: 14px 16px;
  }
  .wasa-title { font-size: 15px; }
}

/* ---------- CAMERA CONTROL CLUSTER ---------- */
#cam-controls {
  position: fixed;
  left: calc(16px + var(--safe-left));
  bottom: calc(16px + var(--safe-bottom));
  z-index: 25;
  display: flex;
  align-items: stretch;
  gap: 8px;
  background: rgba(248, 252, 255, 0.94);
  backdrop-filter: blur(12px);
  border: 1px solid rgba(13, 71, 161, 0.14);
  border-radius: 12px;
  padding: 10px;
  max-width: calc(100vw - 32px - var(--safe-left) - var(--safe-right));
  box-shadow: 0 12px 30px rgba(13, 71, 161, 0.22), 0 2px 8px rgba(0, 0, 0, 0.08);
  transition: opacity 0.22s, transform 0.22s;
  user-select: none;
}
.cam-group {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  gap: 6px;
  min-width: 48px;
  padding: 7px;
  background: rgba(13, 71, 161, 0.055);
  border: 1px solid rgba(13, 71, 161, 0.08);
  border-radius: 10px;
}
.cam-group-label {
  font-size: 9.5px;
  font-weight: 700;
  letter-spacing: 0.7px;
  color: #0d47a1;
  text-transform: uppercase;
  line-height: 1;
}
.cam-row {
  display: flex;
  gap: 5px;
}
#cam-controls .cam-group:nth-of-type(2) .cam-row {
  display: grid;
  grid-template-columns: repeat(2, 30px);
  gap: 5px;
}
#cam-controls .cam-group:nth-of-type(3) .cam-row {
  flex-direction: column;
}
.cam-pan-grid {
  display: grid;
  grid-template-columns: repeat(3, 30px);
  grid-template-rows: repeat(3, 30px);
  gap: 3px;
}
.cam-pan-up    { grid-area: 1 / 2; }
.cam-pan-left  { grid-area: 2 / 1; }
.cam-pan-right { grid-area: 2 / 3; }
.cam-pan-down  { grid-area: 3 / 2; }
.cam-btn {
  width: 30px;
  height: 30px;
  border: none;
  border-radius: 8px;
  background: linear-gradient(180deg, #1565c0 0%, #0d47a1 100%);
  color: white;
  font-size: 0;
  font-weight: 800;
  line-height: 1;
  cursor: pointer;
  transition: transform 0.08s, background 0.12s, box-shadow 0.12s;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.24), 0 2px 6px rgba(13, 71, 161, 0.24);
}
.cam-btn::before {
  display: inline-block;
  font-size: 15px;
  line-height: 1;
}
.cam-btn[data-action="pan-up"]::before { content: "\2191"; }
.cam-btn[data-action="pan-left"]::before { content: "\2190"; }
.cam-btn[data-action="pan-right"]::before { content: "\2192"; }
.cam-btn[data-action="pan-down"]::before { content: "\2193"; }
.cam-btn[data-action="rot-left"]::before { content: "\21ba"; }
.cam-btn[data-action="rot-right"]::before { content: "\21bb"; }
.cam-btn[data-action="tilt-up"]::before { content: "\21e7"; }
.cam-btn[data-action="tilt-down"]::before { content: "\21e9"; }
.cam-btn[data-action="zoom-in"]::before { content: "+"; font-size: 17px; }
.cam-btn[data-action="zoom-out"]::before { content: "-"; font-size: 19px; transform: translateY(-1px); }
.cam-btn:hover {
  background: linear-gradient(180deg, #1976d2 0%, #1565c0 100%);
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.3), 0 3px 8px rgba(13, 71, 161, 0.3);
}
.cam-btn:focus-visible {
  outline: 3px solid rgba(245, 124, 0, 0.45);
  outline-offset: 2px;
}
.cam-btn:active { transform: scale(0.94); background: #f57c00; }
@media (max-width: 480px) {
  #cam-controls {
    left: calc(8px + var(--safe-left));
    bottom: calc(70px + var(--safe-bottom));
    padding: 8px;
    gap: 5px;
  }
  .cam-group { padding: 5px; }
  .cam-btn { width: 28px; height: 28px; }
  .cam-btn::before { font-size: 14px; }
  .cam-pan-grid { grid-template-columns: repeat(3, 28px); grid-template-rows: repeat(3, 28px); }
  #cam-controls .cam-group:nth-of-type(2) .cam-row { grid-template-columns: repeat(2, 28px); }
}
@media (hover: none) and (pointer: coarse) {
  .cam-btn { width: 36px; height: 36px; }
  .cam-btn::before { font-size: 16px; }
  .cam-pan-grid { grid-template-columns: repeat(3, 36px); grid-template-rows: repeat(3, 36px); }
  #cam-controls .cam-group:nth-of-type(2) .cam-row { grid-template-columns: repeat(2, 36px); }
}

#labels {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 5;
}

.label {
  position: absolute;
  transform: translate(-50%, -50%);
  background: rgba(13, 71, 161, 0.92);
  color: white;
  padding: 4px 10px;
  border-radius: 8px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.5px;
  text-transform: uppercase;
  white-space: nowrap;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
  border: 1px solid rgba(255, 255, 255, 0.3);
  transition: opacity 0.3s;
}

.label::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 100%;
  width: 1px;
  height: 14px;
  background: rgba(13, 71, 161, 0.6);
  transform: translateX(-50%);
}

.label.hidden {
  opacity: 0;
}

.label.tour-dimmed {
  opacity: 0.18;
  transform: translate(-50%, -50%) scale(0.85);
}

.label.tour-highlighted {
  background: #f57c00;
  transform: translate(-50%, -50%) scale(1.18);
  box-shadow: 0 0 14px rgba(245, 124, 0, 0.7), 0 4px 12px rgba(0, 0, 0, 0.25);
  border: 1px solid rgba(255, 255, 255, 0.5);
  z-index: 10;
}
.label.tour-highlighted::after {
  background: rgba(245, 124, 0, 0.8);
}

@media (max-width: 760px) {
  #brand-bar {
    align-items: flex-start;
    gap: 10px;
    padding: calc(8px + var(--safe-top)) calc(12px + var(--safe-right)) 8px calc(12px + var(--safe-left));
  }

  #iwmi-logo,
  .logo-fallback {
    height: 34px;
  }

  .brand-text h1 {
    font-size: 15px;
  }

  .brand-text p {
    font-size: 10px;
  }

  #ui {
    top: calc(68px + var(--safe-top));
    left: calc(var(--mobile-edge-gap) + var(--safe-left));
    max-width: min(300px, calc(100vw - 20px - var(--safe-left) - var(--safe-right)));
    max-height: calc(44dvh - var(--safe-top));
    padding: 12px 14px;
  }

  #ui h1 {
    font-size: 18px;
  }

  .legend {
    grid-template-columns: 1fr 1fr;
    gap: 6px 10px;
  }

  .legend-item {
    font-size: 12px;
  }

  .controls { grid-template-columns: 1fr 1fr; }

  #tour-panel {
    left: calc(var(--mobile-edge-gap) + var(--safe-left));
    right: calc(var(--mobile-edge-gap) + var(--safe-right));
    bottom: calc(var(--mobile-edge-gap) + var(--safe-bottom));
    width: auto;
    max-height: calc(56dvh - var(--safe-bottom));
    padding: 12px 14px;
  }

  #tour-start {
    right: calc(var(--mobile-edge-gap) + var(--safe-right));
    bottom: calc(var(--mobile-edge-gap) + var(--safe-bottom));
    padding: 12px 18px;
  }

  .label {
    font-size: 9px;
    padding: 3px 7px;
    max-width: 42vw;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}

@media (max-width: 480px) {
  #ui:not(.ui-collapsed) {
    right: calc(var(--mobile-edge-gap) + var(--safe-right));
    max-width: none;
  }

  .legend,
  .controls {
    grid-template-columns: 1fr;
  }

  .welcome-card {
    padding: 22px 18px;
    margin: 0;
  }

  .welcome-actions {
    flex-direction: column;
  }

  .tour-buttons {
    flex-wrap: wrap;
  }

  .tour-buttons button {
    flex: 1 1 30%;
  }

  #tour-start {
    left: calc(var(--mobile-edge-gap) + var(--safe-left));
    right: calc(var(--mobile-edge-gap) + var(--safe-right));
    width: auto;
  }

  #tour-panel {
    max-height: calc(62dvh - var(--safe-bottom));
  }
}

/* ---------- LARGE DESKTOP (>= 1600px) ---------- */
@media (min-width: 1600px) {
  #ui { max-width: 320px; padding: 22px 26px; }
  #ui h1 { font-size: 24px; }
  .legend-item { font-size: 14px; }
  #wasa-panel { width: min(440px, calc(100vw - 40px)); }
  .wasa-card p { font-size: 12.5px; }
}

/* ---------- TABLET LANDSCAPE (760 – 1100) ---------- */
@media (min-width: 760px) and (max-width: 1100px) {
  #ui { max-width: 240px; padding: 14px 16px; }
  #ui h1 { font-size: 19px; }
  .legend-item { font-size: 12px; }
  #wasa-panel { width: min(340px, calc(100vw - 40px)); padding: 16px 18px; }
}

/* ---------- SHORT VIEWPORTS / MOBILE LANDSCAPE (height <= 500) ---------- */
@media (max-height: 500px) {
  #brand-bar { padding: calc(6px + var(--safe-top)) calc(14px + var(--safe-right)) 6px calc(14px + var(--safe-left)); }
  #iwmi-logo, .logo-fallback { height: 28px; }
  .brand-text h1 { font-size: 13px; }
  .brand-text p { font-size: 9px; }
  #ui { top: calc(56px + var(--safe-top)); max-height: calc(100dvh - 70px - var(--safe-top) - var(--safe-bottom)); padding: 10px 12px; }
  #ui h1 { font-size: 16px; }
  .legend { gap: 4px; }
  .legend-item { font-size: 11px; }
  .abc-legend { padding: 6px 8px; }
  .hint,
  .contact,
  .copyright,
  .credits { display: none; }
  #wasa-panel { top: calc(56px + var(--safe-top)); max-height: calc(100dvh - 70px - var(--safe-top) - var(--safe-bottom)); padding: 12px 14px; }
  #tour-panel { padding: 10px 12px; max-height: calc(100dvh - 70px - var(--safe-top) - var(--safe-bottom)); }
  #tour-title { font-size: 16px; margin-bottom: 4px; }
  #tour-text { font-size: 12px; min-height: 40px; margin-bottom: 8px; }
  #cam-controls { transform: scale(0.9); transform-origin: left bottom; }
}

/* ---------- VERY SMALL PHONES (<= 380px) ---------- */
@media (max-width: 380px) {
  #brand-bar { padding: calc(6px + var(--safe-top)) calc(10px + var(--safe-right)) 6px calc(10px + var(--safe-left)); gap: 8px; }
  #iwmi-logo, .logo-fallback { height: 30px; }
  .brand-text h1 { font-size: 13px; }
  .brand-text p { display: none; }
  #ui { top: calc(60px + var(--safe-top)); left: calc(8px + var(--safe-left)); right: calc(8px + var(--safe-right)); max-width: none; padding: 10px 12px; }
  #ui h1 { font-size: 16px; }
  .legend { grid-template-columns: 1fr; }
  .controls { gap: 4px; }
  .controls button { padding: 5px 8px; font-size: 11px; }
  #tour-panel { padding: 10px 12px; }
  #tour-title { font-size: 15px; }
  #tour-text { font-size: 12px; min-height: 50px; }
  .tour-buttons button { padding: 7px 4px; font-size: 11px; }
  .label { font-size: 8px; padding: 2px 5px; }
  #cam-controls {
    max-width: calc(100vw - 16px - var(--safe-left) - var(--safe-right));
    gap: 4px;
  }
  .cam-group-label { display: none; }
  .cam-group { padding: 4px; }
  .cam-btn { width: 26px; height: 26px; }
  .cam-pan-grid { grid-template-columns: repeat(3, 26px); grid-template-rows: repeat(3, 26px); }
  #cam-controls .cam-group:nth-of-type(2) .cam-row { grid-template-columns: repeat(2, 26px); }
}

/* ---------- TOUCH DEVICES — make buttons easier to tap ---------- */
@media (hover: none) and (pointer: coarse) {
  .controls button,
  .tour-buttons button,
  .wasa-focus-btn,
  #tour-start,
  #welcome-close,
  #welcome-tour {
    min-height: 36px;
  }
}

@media (hover: none) and (pointer: coarse) and (max-width: 380px) {
  .cam-btn { width: 26px; height: 26px; }
  .cam-btn::before { font-size: 13px; }
  .cam-pan-grid { grid-template-columns: repeat(3, 26px); grid-template-rows: repeat(3, 26px); }
  #cam-controls .cam-group:nth-of-type(2) .cam-row { grid-template-columns: repeat(2, 26px); }
}
