/**
 * Park Spot — centralized responsive design system
 * Breakpoints tested: 320, 360, 390, 412, 768, 800, 1024+
 */

/* ── Design tokens ── */
:root {
  /* Breakpoints (reference only — use in @media) */
  --bp-xs: 320px;
  --bp-sm: 360px;
  --bp-md: 390px;
  --bp-lg: 412px;
  --bp-tablet-sm: 768px;
  --bp-tablet: 800px;
  --bp-tablet-lg: 1024px;

  /* Safe areas */
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);

  /* Fluid spacing */
  --space-2xs: clamp(4px, 1vw, 6px);
  --space-xs: clamp(8px, 2vw, 12px);
  --space-sm: clamp(12px, 3vw, 16px);
  --space-md: clamp(16px, 4vw, 24px);
  --space-lg: clamp(20px, 5vw, 32px);
  --space-xl: clamp(24px, 6vw, 40px);

  /* Fluid typography (respects user font scaling via rem + clamp) */
  --font-2xs: clamp(0.625rem, 2vw, 0.6875rem);
  --font-xs: clamp(0.6875rem, 2.5vw, 0.8125rem);
  --font-sm: clamp(0.8125rem, 3vw, 0.875rem);
  --font-base: clamp(0.875rem, 3.5vw, 1rem);
  --font-md: clamp(1rem, 4vw, 1.125rem);
  --font-lg: clamp(1.125rem, 4.5vw, 1.375rem);
  --font-xl: clamp(1.25rem, 5vw, 1.625rem);
  --font-2xl: clamp(1.375rem, 5.5vw, 1.75rem);

  /* Layout */
  --content-max-width: min(100%, 390px);
  --content-max-width-tablet: min(100%, 560px);
  --content-max-width-desktop: min(100%, 640px);
  --card-padding: clamp(16px, 4vw, 32px);
  --card-radius: clamp(14px, 3vw, 20px);
  --panel-max-height: min(92dvh, 92vh);
  --touch-target: 44px;
  --btn-height: clamp(48px, 12vw, 56px);
  --btn-height-sm: clamp(44px, 10vw, 48px);
  --nav-topbar-height: calc(56px + var(--safe-top));
  --map-min-height: clamp(140px, 30vh, 200px);
  --map-nav-min-height: 120px;
  --map-ctrl-size: 44px;
  --map-ctrl-gap: 8px;
  --map-ctrl-stack: 4;

  /* Body shell */
  --body-padding: clamp(8px, 3vw, 24px);
}

/* ── Global responsive base ── */
html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  scroll-behavior: smooth;
}

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
}

/* ── Utility classes ── */
.u-overflow-safe {
  overflow-wrap: anywhere;
  word-break: break-word;
  hyphens: auto;
}

.u-text-balance {
  text-wrap: balance;
}

.u-truncate {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.u-truncate-2 {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
}

.u-truncate-3 {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  overflow: hidden;
}

.u-touch-target {
  min-width: var(--touch-target);
  min-height: var(--touch-target);
}

.u-fluid-btn {
  min-height: var(--btn-height);
  height: auto;
  padding-block: clamp(10px, 2.5vw, 14px);
  padding-inline: clamp(12px, 3vw, 20px);
  line-height: 1.35;
  text-align: center;
  white-space: normal;
}

.u-safe-x {
  padding-left: max(var(--space-sm), var(--safe-left));
  padding-right: max(var(--space-sm), var(--safe-right));
}

.u-safe-bottom {
  padding-bottom: max(var(--space-sm), var(--safe-bottom));
}

.u-scroll-y {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

.u-min-w-0 {
  min-width: 0;
}

.u-flex-1 {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
}

/* ── Ad banner (home screen only) ── */
.ad-banner-slot {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 50;
  min-height: 50px;
  max-height: 90px;
  display: none;
  justify-content: center;
  align-items: center;
  padding-bottom: max(4px, var(--safe-bottom));
  padding-left: max(8px, var(--safe-left));
  padding-right: max(8px, var(--safe-right));
  background: rgba(11, 15, 26, 0.92);
  border-top: 1px solid var(--border);
  pointer-events: auto;
  overflow: hidden;
}

.ad-banner-slot.visible {
  display: flex;
}

.ad-banner-inner {
  width: 100%;
  max-width: var(--content-max-width-desktop);
  min-height: 50px;
  max-height: 80px;
  overflow: hidden;
}

body.ad-banner-active:not(.nav-open) {
  padding-bottom: calc(max(var(--body-padding), var(--safe-bottom)) + 56px);
}

body.ad-banner-active:not(.nav-open) .card {
  margin-bottom: 8px;
}

.premium-ad-cta {
  margin-top: 12px;
  text-align: center;
}

.premium-ad-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  font-family: inherit;
  font-size: var(--font-xs);
  text-decoration: underline;
  cursor: pointer;
  padding: 8px 12px;
  min-height: var(--touch-target);
}

.premium-ad-btn:hover {
  color: var(--accent);
}

.premium-ad-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

@media (max-width: 389px) {
  .nav-bottom {
    max-height: min(36dvh, 36vh);
    padding: 10px 12px;
    padding-bottom: calc(10px + var(--safe-bottom));
  }

  .nav-mode-tabs {
    margin-bottom: 10px;
  }

  .nav-info-row {
    margin-bottom: 8px;
  }
}

@media (max-width: 359px) {
  :root {
    --card-padding: 14px;
    --body-padding: 8px;
  }

  .brand { margin-bottom: 20px; }

  .brand-icon {
    width: 44px;
    height: 44px;
    font-size: 20px;
  }

  .nav-mode-tab {
    font-size: 10px;
    padding: 6px 2px;
  }

  .nav-info-line1 { font-size: var(--font-md); }

  .nav-eta-pill {
    font-size: 11px;
    padding: 4px 8px;
  }

  .pill-btn { font-size: 10px; padding: 6px 10px; }

  .toast {
    white-space: normal;
    text-align: center;
    border-radius: 12px;
    max-width: calc(100vw - 16px);
  }
}

/* ── Standard phones (360px – 767px) ── */
@media (min-width: 360px) and (max-width: 767px) {
  .toast {
    max-width: calc(100vw - 24px);
  }
}

/* ── Large phones (412px+) ── */
@media (min-width: 412px) and (max-width: 767px) {
  :root {
    --content-max-width: min(100%, 420px);
  }
}

/* ── Landscape phones ── */
@media (orientation: landscape) and (max-height: 500px) {
  body:not(.nav-open) {
    align-items: flex-start;
    overflow-y: auto;
    padding: var(--space-xs);
    padding-top: max(var(--space-xs), var(--safe-top));
    padding-bottom: max(var(--space-xs), var(--safe-bottom));
  }

  .card {
    max-height: calc(100dvh - var(--space-sm) * 2);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: var(--space-sm) var(--space-md);
    padding-bottom: calc(var(--space-sm) + var(--safe-bottom));
    margin-block: var(--space-xs);
  }

  .brand { margin-bottom: 16px; }

  .brand-icon {
    width: 40px;
    height: 40px;
    margin-bottom: 8px;
  }

  .brand-name { font-size: var(--font-lg); }

  .divider { margin: 12px 0; }

  .photo-box { min-height: 72px; margin-bottom: 12px; }

  .nav-screen {
    flex-direction: row;
    flex-wrap: wrap;
  }

  .nav-topbar {
    width: 100%;
    flex: 0 0 auto;
  }

  .nav-body {
    flex: 1 1 55%;
    min-width: 0;
    min-height: 0;
  }

  .nav-bottom {
    flex: 0 0 45%;
    max-width: 45%;
    max-height: 100%;
    border-radius: 0;
    border-top: none;
    border-left: 1px solid var(--border);
    overflow-y: auto;
  }

  .map-controls-right {
    bottom: max(12px, var(--safe-bottom));
    right: max(12px, var(--safe-right));
  }

  .map-layer-btn {
    right: max(12px, var(--safe-right));
    bottom: calc(max(12px, var(--safe-bottom)) + var(--map-ctrl-stack) * var(--map-ctrl-size) + (var(--map-ctrl-stack) - 1) * var(--map-ctrl-gap) + 12px);
  }

  #nav-recenter,
  .accuracy-btn,
  .gps-btn {
    top: 12px;
    right: max(12px, var(--safe-right));
  }

  .nav-bottom {
    max-height: 100%;
    padding: 10px 12px;
    padding-bottom: calc(10px + var(--safe-bottom));
  }

  #map {
    height: clamp(120px, 40vh, 180px);
  }

  .panel {
    max-height: 95dvh;
  }
}

/* ── Tablets (768px+) ── */
@media (min-width: 768px) {
  :root {
    --content-max-width: var(--content-max-width-tablet);
    --body-padding: 24px;
    --card-padding: clamp(24px, 3vw, 36px);
  }

  body:not(.nav-open) {
    padding: var(--body-padding);
  }

  .card,
  .panel {
    max-width: var(--content-max-width-tablet);
  }

  .brand-name { font-size: var(--font-2xl); }

  .saved-headline { font-size: var(--font-xl); }

  .nav-bottom {
    max-width: var(--content-max-width-tablet);
    margin-inline: auto;
    width: 100%;
    border-radius: var(--card-radius) var(--card-radius) 0 0;
  }

  .nav-mode-tab { font-size: var(--font-sm); }

  .nav-info-line1 { font-size: var(--font-xl); }

  #map {
    height: clamp(200px, 35vh, 320px);
  }

  .panel {
    border-radius: var(--card-radius) var(--card-radius) 0 0;
  }
}

/* ── Large tablets / small desktops (1024px+) ── */
@media (min-width: 1024px) {
  :root {
    --content-max-width: var(--content-max-width-desktop);
  }

  .card,
  .panel {
    max-width: var(--content-max-width-desktop);
  }

  .nav-bottom {
    max-width: var(--content-max-width-desktop);
  }
}

/* ── Large desktops (1366px+) ── */
@media (min-width: 1366px) {
  :root {
    --body-padding: 32px;
  }

  body:not(.nav-open) {
    align-items: center;
  }
}

/* ── Full HD (1920px+) ── */
@media (min-width: 1920px) {
  :root {
    --content-max-width-desktop: 680px;
  }
}
@media (spanning: single-fold-vertical) {
  body:not(.nav-open) {
    padding-inline: max(var(--body-padding), var(--safe-left), var(--safe-right));
  }

  .card {
    max-width: min(var(--content-max-width-tablet), 90vw);
  }
}

@media (spanning: single-fold-horizontal) {
  .nav-screen {
    flex-direction: row;
    flex-wrap: wrap;
  }

  .nav-topbar { width: 100%; }

  .nav-body {
    flex: 1 1 60%;
    min-height: 0;
  }

  .nav-bottom {
    flex: 0 0 40%;
    max-width: 40%;
    max-height: 100%;
    border-radius: 0;
    border-left: 1px solid var(--border);
  }
}

/* ── Accessibility: large text / font scaling ── */
@media (prefers-contrast: more) {
  .card,
  .panel,
  .nav-bottom {
    border-width: 2px;
  }

  .btn-primary,
  .btn-success {
    font-weight: 700;
  }
}

@media (min-resolution: 2dppx) {
  .brand-icon,
  .status-icon,
  .nav-walk-icon {
    -webkit-font-smoothing: antialiased;
  }
}

/* High font scaling (approx. 150%+) */
@media (min-width: 0) {
  html[style*="font-size"],
  body[data-large-text="true"] .card {
    max-width: min(100%, 480px);
  }
}

/* Prevent horizontal overflow on small screens */
html, body {
  max-width: 100vw;
  overflow-x: hidden;
}

.nav-body,
.nav-map-wrap,
#nav-map,
#map {
  max-width: 100%;
}

.panel {
  max-width: 100vw;
}

@media (min-width: 768px) {
  body:not(.nav-open) .card {
    max-width: var(--content-max-width-tablet);
  }
}

@media (min-width: 1024px) {
  body:not(.nav-open) .card {
    max-width: var(--content-max-width-desktop);
  }
}

@media (min-width: 0) {
  html:has(body[data-large-text="true"]) {
    --btn-height: clamp(52px, 14vw, 64px);
  }
}
