:root {
  --bg: #f7f8fa;
  --panel: #ffffff;
  --border: #e3e6eb;
  --text: #1a1d22;
  --text-muted: #6b7280;
  --accent: #2563eb;
  --danger: #dc2626;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.05), 0 4px 12px rgba(0, 0, 0, 0.05);
  --radius: 8px;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
}

body {
  display: flex;
  overflow: hidden;
}

#sidebar {
  width: 340px;
  flex: 0 0 340px;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

.sidebar-header {
  padding: 20px 20px 12px;
  border-bottom: 1px solid var(--border);
}

.sidebar-header h1 {
  margin: 0 0 4px;
  font-size: 18px;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

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

.header-btn {
  background: none;
  border: 1px solid var(--border);
  color: var(--text);
  padding: 4px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 12px;
  transition:
    border-color 100ms,
    background 100ms;
}

.header-btn:hover:not(:disabled) {
  border-color: var(--accent);
  background: var(--bg);
}

.header-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.share-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  padding: 0;
  background: none;
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  color: var(--text);
  transition:
    border-color 120ms,
    background 120ms,
    color 120ms,
    transform 120ms;
}

.share-btn svg {
  width: 22px;
  height: 22px;
  display: block;
}

.share-btn:hover:not(:disabled) {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--bg);
}

.share-btn:active:not(:disabled) {
  transform: scale(0.96);
}

.share-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.lang-picker {
  position: relative;
}

.lang-menu {
  position: absolute;
  top: calc(100% + 6px);
  inset-inline-end: 0;
  z-index: 1500;
  min-width: 160px;
  margin: 0;
  padding: 4px 0;
  list-style: none;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.lang-menu li {
  padding: 8px 14px;
  cursor: pointer;
  font-size: 13px;
  white-space: nowrap;
  display: flex;
  align-items: center;
  gap: 8px;
}

.lang-menu li:hover {
  background: var(--bg);
}

.lang-menu li.current {
  font-weight: 600;
  color: var(--accent);
}

.lang-menu li.current::before {
  content: '✓';
  font-size: 11px;
  width: 12px;
  display: inline-block;
}

.lang-menu li:not(.current)::before {
  content: '';
  width: 12px;
  display: inline-block;
}

.auth-control {
  font-size: 13px;
}

.auth-signin {
  background: none;
  border: 1px solid var(--border);
  color: var(--text);
  padding: 4px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 12px;
  transition:
    border-color 100ms,
    background 100ms;
}

.auth-signin:hover {
  border-color: var(--accent);
  background: var(--bg);
}

.auth-user {
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  padding: 2px 4px;
  border-radius: 6px;
  transition: background 100ms;
}

.auth-user:hover {
  background: var(--bg);
}

.auth-avatar {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--bg);
}

.auth-name {
  color: var(--text-muted);
  font-size: 12px;
  max-width: 120px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.stats {
  margin: 0;
  font-size: 13px;
  color: var(--text-muted);
}

.searchbox-wrap {
  position: relative;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}

#search-input {
  width: 100%;
  padding: 10px 12px;
  font-size: 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  outline: none;
  transition:
    border-color 120ms,
    background 120ms;
}

#search-input:focus {
  border-color: var(--accent);
  background: #fff;
}

.search-results {
  position: absolute;
  left: 16px;
  right: 16px;
  top: calc(100% - 4px);
  margin: 0;
  padding: 4px 0;
  list-style: none;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  z-index: 1000;
  max-height: 320px;
  overflow-y: auto;
}

.search-results li {
  padding: 8px 12px;
  font-size: 14px;
  cursor: pointer;
  display: flex;
  align-items: baseline;
  gap: 8px;
}

.search-results li:hover,
.search-results li.active {
  background: var(--bg);
}

.search-results .result-name {
  font-weight: 500;
}

.search-results .result-country {
  color: var(--text-muted);
  font-size: 12px;
}

.search-results .result-flag {
  font-size: 14px;
}

.search-results .loading,
.search-results .empty {
  padding: 10px 12px;
  font-size: 13px;
  color: var(--text-muted);
  cursor: default;
}

.city-list {
  flex: 1;
  overflow-y: auto;
  padding: 12px 8px 24px;
}

.empty-state {
  color: var(--text-muted);
  font-size: 13px;
  text-align: center;
  padding: 32px 24px;
  margin: 0;
  line-height: 1.5;
}

.country-group {
  margin-bottom: 14px;
}

.country-header {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 6px 12px;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  background: transparent;
  border: 0;
  border-radius: 6px;
  cursor: pointer;
  text-align: left;
  font-family: inherit;
  transition: background 100ms;
}

.country-header:hover {
  background: var(--bg);
}

.highlight-toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-top: 8px;
  font-size: 12px;
  color: var(--text-muted);
  cursor: pointer;
  user-select: none;
}

.highlight-toggle input {
  margin: 0;
  cursor: pointer;
  accent-color: var(--accent);
}

.country-chevron {
  display: inline-block;
  width: 12px;
  font-size: 10px;
  color: var(--text-muted);
  transition: transform 120ms ease;
  text-align: center;
}

.country-group.collapsed .country-chevron {
  transform: rotate(-90deg);
}

.country-group.collapsed .city-row {
  display: none;
}

.country-flag {
  font-size: 16px;
}

.country-name {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.country-count {
  font-weight: 500;
  color: var(--text-muted);
  background: var(--bg);
  padding: 1px 8px;
  border-radius: 10px;
  font-size: 11px;
}

.city-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  border-radius: 6px;
  cursor: pointer;
  transition: background 100ms;
}

.city-row:hover {
  background: var(--bg);
}

.city-name {
  flex: 1;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.city-remove {
  flex: 0 0 auto;
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 18px;
  line-height: 1;
  padding: 2px 6px;
  border-radius: 4px;
  cursor: pointer;
  opacity: 0;
  transition:
    opacity 100ms,
    color 100ms,
    background 100ms;
}

.city-row:hover .city-remove {
  opacity: 1;
}

.city-remove:hover {
  color: var(--danger);
  background: rgba(220, 38, 38, 0.08);
}

#map {
  flex: 1;
  height: 100vh;
}

.share-banner {
  background: #fef3c7;
  border-bottom: 1px solid #fde68a;
  padding: 12px 16px;
  font-size: 13px;
  color: #78350f;
}

.share-banner-text {
  margin-bottom: 8px;
  line-height: 1.4;
}

.share-banner-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

body.view-mode #share-btn,
body.view-mode .searchbox-wrap {
  display: none !important;
}

body.view-mode .city-remove {
  display: none !important;
}

body.view-mode .city-row {
  cursor: default;
}

.toast-container {
  position: fixed;
  bottom: 24px;
  right: 24px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 2000;
  pointer-events: none;
}

.toast {
  background: #1f2937;
  color: #fff;
  padding: 10px 16px;
  border-radius: var(--radius);
  font-size: 13px;
  box-shadow: var(--shadow);
  pointer-events: auto;
  opacity: 0;
  transform: translateY(8px);
  transition:
    opacity 180ms,
    transform 180ms;
  max-width: 320px;
}

.toast.show {
  opacity: 1;
  transform: translateY(0);
}

.toast.success {
  background: #16a34a;
}

.toast.error {
  background: #dc2626;
}

.toast.info {
  background: #2563eb;
}

/* ── Login-prompt balloon ────────────────────────────────────────────────── */
/* Shown in the bottom-right corner; clear of the sidebar, Leaflet zoom      */
/* controls (top-left), and search box (inside sidebar on the left).         */
/* Positioned above the toast container so the two never overlap.            */

.login-prompt {
  position: fixed;
  bottom: 80px;
  right: 24px;
  z-index: 1100;
  width: 264px;
  padding: 20px 20px 18px;
  background: #1f2937;
  color: #f9fafb;
  border-radius: 12px;
  box-shadow:
    0 8px 32px rgba(0, 0, 0, 0.4),
    0 2px 8px rgba(0, 0, 0, 0.2);
  display: flex;
  flex-direction: column;
  gap: 12px;
  /* Balloon does not block pointer events on the map behind it except for */
  /* its own interactive area.                                              */
  pointer-events: auto;
}

.login-prompt[hidden] {
  display: none;
}

.login-prompt__message {
  margin: 0;
  font-size: 0.925rem;
  line-height: 1.45;
}

.login-prompt__sign-in {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  background: #4285f4;
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 10px 16px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  width: 100%;
  transition: background 0.15s;
}

.login-prompt__sign-in:hover {
  background: #3367d6;
}

.login-prompt__sign-in:focus-visible {
  outline: 2px solid #93c5fd;
  outline-offset: 2px;
}

.login-prompt__dismiss {
  position: absolute;
  top: 8px;
  right: 10px;
  background: none;
  border: none;
  color: #9ca3af;
  font-size: 1.25rem;
  line-height: 1;
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 4px;
  transition:
    color 0.12s,
    background 0.12s;
}

.login-prompt__dismiss:hover {
  color: #f9fafb;
  background: rgba(255, 255, 255, 0.12);
}

.login-prompt__dismiss:focus-visible {
  outline: 2px solid #93c5fd;
  outline-offset: 2px;
}

/* ── Visually hidden (accessible text used inside icon buttons) ──────────── */

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* ── Hamburger button — hidden on desktop, shown by the mobile query below ── */

.hamburger-btn {
  display: none;
}

/* ── Mobile responsive layout ────────────────────────────────────────────── */
/* Single @media block per ARCHITECTURE.md §3.2.                             */
/* Desktop layout (≥ 769 px) is unchanged — all rules are additive overrides. */

@media (max-width: 768px) {
  /* Full-bleed map; sidebar floats over it as a drawer */
  body {
    display: block;
    overflow-x: hidden;
    overflow-y: hidden;
  }

  #map {
    width: 100%;
    height: 100vh;
    /* Use dynamic viewport on browsers that support it */
    height: 100dvh; /* stylelint-disable-line declaration-block-no-duplicate-properties */
    padding-bottom: env(safe-area-inset-bottom);
  }

  /* Sidebar: hidden by default, slides in from the left */
  #sidebar {
    position: fixed;
    inset-block: 0;
    inset-inline-start: 0;
    width: min(320px, 85vw);
    height: 100%;
    height: 100dvh; /* stylelint-disable-line declaration-block-no-duplicate-properties */
    z-index: 1200;
    transform: translateX(-110%);
    transition: transform 280ms cubic-bezier(0.4, 0, 0.2, 1);
    /* Respect iOS notch / home indicator */
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
    padding-inline-start: env(safe-area-inset-left);
    box-shadow: 4px 0 24px rgba(0, 0, 0, 0.25);
    overflow-y: auto;
  }

  /* RTL: slide in from the right */
  [dir='rtl'] #sidebar {
    transform: translateX(110%);
  }

  #sidebar.is-open {
    transform: translateX(0);
  }

  /* Overlay — dims the map area while the drawer is open */
  .drawer-overlay {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 1100;
    background: rgba(0, 0, 0, 0.45);
  }

  .drawer-overlay.is-visible {
    display: block;
  }

  /* Hamburger button */
  .hamburger-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    position: fixed;
    top: calc(env(safe-area-inset-top) + 12px);
    left: calc(env(safe-area-inset-left) + 12px);
    z-index: 1050;
    width: 44px;
    height: 44px;
    padding: 0;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    cursor: pointer;
    color: var(--text);
    box-shadow: var(--shadow);
  }

  .hamburger-btn svg {
    display: block;
  }

  /* Toast: constrain to viewport width at 360 px */
  .toast-container {
    right: 12px;
    left: 12px;
    bottom: calc(env(safe-area-inset-bottom) + 16px);
    align-items: stretch;
  }

  .toast {
    max-width: 100%;
  }

  /* Login-prompt balloon: constrain to viewport width at 360 px */
  .login-prompt {
    right: 12px;
    left: 12px;
    width: auto;
    bottom: calc(env(safe-area-inset-bottom) + 80px);
  }

  /* Tap targets — all interactive controls ≥ 44 × 44 px */
  .country-header,
  .city-row {
    min-height: 44px;
  }

  .city-remove {
    min-width: 44px;
    min-height: 44px;
    opacity: 1; /* always visible on touch devices */
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
  }

  .share-btn {
    width: 44px;
    height: 44px;
  }

  .auth-signin {
    min-height: 44px;
    padding: 0 16px;
  }

  #search-input {
    min-height: 44px;
  }

  /* Ensure search results don't overflow the sidebar on narrow screens */
  .search-results {
    left: 0;
    right: 0;
  }
}
