/* =========================================================
   KisanLand — Responsive hardening
   Keeps the existing visual design while preventing mobile
   overflow, cramped actions and unstable layouts.
   ========================================================= */

/* Safe sizing defaults for every page. */
html, body {
  max-width: 100%;
  overflow-x: hidden;
}

.container,
.nav__inner,
.grid,
.flex,
.form-row,
.auth-layout,
.dash-layout {
  min-width: 0;
}

.grid > *,
.flex > *,
.form-row > *,
.nav__inner > * {
  min-width: 0;
}

img,
video,
iframe,
canvas,
svg {
  max-width: 100%;
}

input,
select,
textarea,
button {
  max-width: 100%;
}

input,
select,
textarea {
  min-width: 0;
}

h1,
h2,
h3,
h4,
h5,
h6,
p,
label,
a,
button,
span,
td,
th {
  overflow-wrap: anywhere;
}

/* Data tables can scroll inside their own container, never the page. */
.table-wrap {
  max-width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

/* Modal and toast content must shrink cleanly on narrow screens. */
.modal,
.modal__body,
.modal__header,
.modal__footer,
.toast,
.toast__content {
  min-width: 0;
}

.modal__footer {
  flex-wrap: wrap;
}

.modal__footer .btn {
  flex: 0 1 auto;
}

@media (max-width: 768px) {
  /* ─── Top navigation ─────────────────────────────────────── */
  .nav {
    height: 3.5rem;
  }

  .nav__inner {
    padding-inline: 0.75rem;
    gap: 0.5rem;
  }

  .nav__logo {
    flex: 1 1 auto;
    min-width: 0;
    white-space: nowrap;
    font-size: 0.98rem;
    gap: 0.45rem;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .nav__logo-icon {
    flex: 0 0 1.8rem;
    width: 1.8rem;
    height: 1.8rem;
    font-size: 0.9rem;
  }

  .nav__actions {
    flex: 0 0 auto;
    min-width: 0;
    gap: 0.25rem;
  }

  /* Desktop auth/session controls are intentionally moved into
     the full-screen mobile menu so the header never overflows. */
  .nav__actions > .flex,
  .nav__actions > #nav-auth-buttons,
  .nav__actions > #nav-user-menu {
    display: none !important;
  }

  .nav__actions .theme-toggle {
    flex: 0 0 2rem;
    width: 2rem;
    height: 2rem;
  }

  .nav__hamburger {
    display: flex;
    flex: 0 0 2.25rem;
    width: 2.25rem;
    height: 2.25rem;
    align-items: center;
    justify-content: center;
    padding: 0.55rem;
  }

  .nav__hamburger span {
    width: 20px;
  }

  /* ─── Mobile menu ────────────────────────────────────────── */
  .mobile-menu {
    padding: 1rem;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .mobile-menu__close {
    padding: 0.55rem;
  }

  .mobile-menu__links {
    margin-top: 1.5rem;
    gap: 0.5rem;
    padding-bottom: 1.5rem;
  }

  .mobile-menu__link {
    padding: 0.85rem 0.9rem;
    font-size: 1rem;
  }

  .mobile-menu__account {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    padding: 0.9rem;
    margin-bottom: 0.75rem;
    background: var(--surface-card);
    border: 1px solid var(--surface-border);
    border-radius: var(--radius-xl);
  }

  .mobile-menu__account-label {
    font-size: var(--text-xs);
    color: var(--text-muted);
  }

  .mobile-menu__account-name {
    margin-top: 0.15rem;
    font-size: var(--text-sm);
    font-weight: var(--fw-bold);
    color: var(--text-primary);
    overflow-wrap: anywhere;
  }

  .mobile-menu__account-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.5rem;
  }

  .mobile-menu__account-actions .btn,
  .mobile-menu__account-actions button {
    width: 100%;
  }

  /* ─── Generic responsive spacing ─────────────────────────── */
  .section {
    padding: 3.5rem 0;
  }

  .section--sm {
    padding: 2.75rem 0;
  }

  .section--lg {
    padding: 4.5rem 0;
  }

  .section__header {
    margin-bottom: 2rem;
  }

  /* ─── Homepage hero ─────────────────────────────────────── */
  .hero {
    min-height: auto;
  }

  .hero__content {
    padding: 3rem 0 3.5rem;
    max-width: none;
  }

  .hero__tag {
    max-width: 100%;
    letter-spacing: 0.06em;
  }

  .hero__title {
    font-size: clamp(2.25rem, 10.5vw, 3.2rem);
    letter-spacing: -0.035em;
  }

  .hero__desc {
    max-width: none;
    font-size: 1rem;
  }

  .hero__cta {
    flex-direction: column;
    align-items: stretch;
    gap: 0.75rem;
  }

  .hero__cta .btn {
    width: 100%;
    max-width: 24rem;
  }

  .hero__stats {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 1.1rem 0.75rem;
    margin-top: 2.25rem;
  }

  .hero__stat {
    min-width: 0;
  }

  .hero__stat-value {
    font-size: clamp(1.1rem, 5vw, 1.5rem);
    white-space: nowrap;
  }

  .hero__stat-label {
    line-height: 1.35;
  }

  .hero__float-card {
    display: none;
  }

  /* ─── Search / marketplace ───────────────────────────────── */
  .search-sidebar {
    position: static;
  }

  .search-results-header {
    align-items: stretch;
  }

  .search-results-header > * {
    min-width: 0;
  }

  .search-results-header select,
  #sort-select,
  #crop-sort,
  #state-filter-mandi,
  #state-filter {
    width: 100% !important;
    max-width: 100%;
  }

  .land-grid,
  .mandi-grid {
    grid-template-columns: 1fr;
  }

  .land-card__meta {
    gap: 0.375rem;
  }

  .crop-card__image-wrap {
    min-width: 0;
  }

  /* ─── Filter rows / tabs ─────────────────────────────────── */
  .tabs {
    width: 100%;
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
  }

  .tabs .tab {
    flex: 0 0 auto;
    white-space: nowrap;
  }

  .mandi-filters {
    flex-wrap: nowrap;
    overflow-x: auto;
    max-width: 100%;
    padding-bottom: 0.25rem;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
  }

  .mandi-filter-btn {
    flex: 0 0 auto;
    white-space: nowrap;
  }

  .mandi-header {
    padding: 3rem 0;
  }

  .mandi-header__title {
    font-size: clamp(2rem, 9vw, 2.75rem);
  }

  /* ─── Forms ───────────────────────────────────────────────── */
  .form-row {
    grid-template-columns: 1fr;
  }

  .form-control {
    width: 100%;
  }

  .file-upload {
    padding: 2rem 1rem;
  }

  /* ─── Auth ────────────────────────────────────────────────── */
  .auth-panel {
    padding: 1.25rem;
  }

  .auth-form {
    max-width: 100%;
  }

  /* Registration header button: keep the mobile header clean. */
  .nav__actions .nav__auth-link {
    max-width: 7rem;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  /* ─── Dashboard ───────────────────────────────────────────── */
  .dash-content {
    padding: 1rem;
  }

  .page-header {
    padding: 1.5rem 0 1rem;
    margin-bottom: 1.5rem;
  }

  .page-header__title {
    font-size: clamp(1.35rem, 6vw, 1.7rem);
  }

  /* Dashboard action groups should stack/wrap rather than force width. */
  .dash-content .flex.justify-between,
  .dash-content .flex.items-center {
    flex-wrap: wrap;
  }

  /* ─── Add-land map / polygon editor ──────────────────────── */
  #map,
  #polygon-map {
    height: 320px !important;
  }

  .draw-toolbar {
    align-items: stretch !important;
  }

  .draw-toolbar__title {
    width: 100%;
    margin-right: 0 !important;
  }

  .draw-toolbar .btn {
    flex: 1 1 calc(50% - 0.25rem);
    min-width: 0;
  }

  .draw-toolbar > div:last-child {
    width: 100%;
    margin-left: 0 !important;
  }

  .polygon-area-display {
    flex-wrap: wrap;
  }

  .polygon-area-display > div:last-child {
    width: 100%;
    margin-left: 0 !important;
  }

  .step-wizard .step__label {
    font-size: 0.58rem;
  }

  /* ─── Contract ────────────────────────────────────────────── */
  .contract-preview {
    padding: 1.25rem;
    max-width: 100%;
    font-size: 13px;
  }

  .contract-preview__signatures {
    grid-template-columns: 1fr;
    gap: 2rem;
  }

  .contract-parties-grid {
    grid-template-columns: 1fr !important;
  }

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

  /* ─── Phase 2 pages ───────────────────────────────────────── */
  .phase2-hero {
    padding: 4rem 0;
  }

  .phase2-hero h1 {
    font-size: clamp(2rem, 9vw, 2.75rem) !important;
  }

  .phase2-hero p {
    max-width: 100% !important;
    font-size: 1rem !important;
  }

  /* Leaflet popups/labels should never force the viewport wider. */
  .leaflet-popup-content {
    min-width: 0 !important;
    max-width: calc(100vw - 4rem) !important;
  }

  .leaflet-popup-content-wrapper {
    max-width: calc(100vw - 2rem) !important;
  }
}

@media (max-width: 480px) {
  .container {
    padding-inline: 0.875rem;
  }

  .hero__title {
    font-size: clamp(2.15rem, 10.5vw, 2.9rem);
  }

  .hero__stats {
    column-gap: 0.6rem;
  }

  .hero__stat-value {
    font-size: clamp(1rem, 4.8vw, 1.35rem);
  }

  .modal__header,
  .modal__body,
  .modal__footer {
    padding: 1rem;
  }

  .modal__footer .btn {
    flex: 1 1 100%;
    width: 100%;
  }

  .card__body {
    padding: 1rem;
  }

  .card__footer {
    padding: 0.85rem 1rem;
    flex-wrap: wrap;
  }

  .card__footer .btn {
    flex: 1 1 100%;
  }

  .stat-card {
    padding: 1rem;
    gap: 0.75rem;
  }

  .stat-card__icon {
    width: 2.5rem;
    height: 2.5rem;
    font-size: 1.15rem;
  }

  .step-wizard {
    margin-bottom: 1.5rem;
  }
}

@media (max-width: 360px) {
  .nav__inner {
    padding-inline: 0.625rem;
  }

  .nav__logo {
    font-size: 0.92rem;
  }

  .nav__logo-icon {
    flex-basis: 1.7rem;
    width: 1.7rem;
    height: 1.7rem;
  }

  .nav__actions .theme-toggle {
    width: 1.9rem;
    height: 1.9rem;
    flex-basis: 1.9rem;
  }

  .nav__hamburger {
    flex-basis: 2rem;
    width: 2rem;
    height: 2rem;
  }

  /* Very narrow auth headers: show only the useful action text. */
  .nav__actions .nav__auth-link {
    font-size: 0 !important;
    width: 2.25rem;
    min-width: 2.25rem;
    padding-inline: 0 !important;
  }

  .nav__actions .nav__auth-link::after {
    content: 'Sign In';
    font-size: 0.7rem;
  }

  .draw-toolbar .btn {
    flex-basis: 100%;
  }

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

@media (prefers-reduced-motion: reduce) {
  .ticker,
  .animate-fade-in,
  .animate-fade-in-right,
  .animate-pulse,
  .animate-spin {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    scroll-behavior: auto !important;
    transition-duration: 0.001ms !important;
  }
}
@media (max-width: 640px) {
  .mandi-about-grid {
    grid-template-columns: 1fr !important;
  }
}

/* Leaflet land labels and controls stay readable at every viewport size. */
.kl-map-fullscreen-control {
  margin-top: 0.45rem !important;
}

.kl-map-fullscreen-btn {
  width: 34px;
  height: 34px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid rgba(0,0,0,0.18);
  border-radius: 4px;
  background: #fff;
  color: #1f2937;
  box-shadow: 0 1px 5px rgba(0,0,0,0.28);
  font: 700 1.2rem/1 Arial, sans-serif;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.kl-map-fullscreen-btn:hover {
  background: #f5f5f5;
}

.kl-map-fullscreen-btn:focus-visible {
  outline: 3px solid rgba(45,122,69,0.45);
  outline-offset: 1px;
}

#map:fullscreen,
#map:-webkit-full-screen,
#polygon-map:fullscreen,
#polygon-map:-webkit-full-screen {
  width: 100vw !important;
  height: 100vh !important;
  max-width: none !important;
  max-height: none !important;
  border-radius: 0 !important;
  margin: 0 !important;
  z-index: 2147483647 !important;
}

body.kl-map-fullscreen-active {
  overflow: hidden;
}

@media (max-width: 768px) {
  .kl-land-label-inner {
    width: min(175px, 46vw);
    max-width: calc(100vw - 32px);
    padding: 5px 8px 4px;
    border-radius: 9px;
  }

  .kl-land-label-title {
    font-size: 10.5px;
    line-height: 1.25;
  }

  .kl-land-label-meta {
    font-size: 9px;
  }

  .leaflet-control-layers {
    font-size: 12px;
    max-width: calc(100vw - 4rem);
  }

  .leaflet-control-layers-expanded {
    max-width: min(220px, 62vw);
  }

  .contract-generator-layout {
    grid-template-columns: 1fr !important;
    gap: 1.5rem !important;
  }

  /* Secondary header actions remain tappable without forcing a horizontal
     scroll at phone widths. */
  .nav__actions > .btn:not(.theme-toggle) {
    max-width: 11rem;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}

@media (max-width: 420px) {
  .kl-land-label-inner {
    width: min(155px, 52vw);
  }

  .kl-map-fullscreen-btn {
    width: 32px;
    height: 32px;
    font-size: 1.05rem;
  }

  .nav__actions > .btn:not(.theme-toggle) {
    padding-inline: 0.65rem;
    font-size: 0.74rem;
  }

  .mobile-menu {
    padding: calc(0.75rem + env(safe-area-inset-top)) 0.875rem calc(0.875rem + env(safe-area-inset-bottom));
  }

  .mobile-menu__links {
    margin-top: 1rem;
  }
}

