:root {
  --bg-toolbar: rgba(255, 255, 255, 0.95);
  --bg-secondary: #f1f5f9;
  --bg-hover: #e2e8f0;
  --bg-input: #f8fafc;
  --bg-active: #ffffff;
  --text-primary: #1e293b;
  --text-secondary: #64748b;
  --text-muted: #94a3b8;
  --border-color: #e2e8f0;
  --divider-color: #e2e8f0;
  --shadow-color: rgba(0, 0, 0, 0.12);
  --toolbar-max-width: 1107px;
  --map-filter: none;
}

html.hide-instructions #instructions {
  display: none;
}

html.hide-seo-banner #seo-banner {
  display: none;
}

[data-theme="dark"] {
  --bg-toolbar: rgba(30, 41, 59, 0.95);
  --bg-secondary: #334155;
  --bg-hover: #475569;
  --bg-input: #1e293b;
  --bg-active: #0f172a;
  --text-primary: #f8fafc;
  --text-secondary: #cbd5e1;
  --text-muted: #64748b;
  --border-color: #475569;
  --divider-color: #475569;
  --shadow-color: rgba(0, 0, 0, 0.5);
  --map-filter: none;
}

[data-theme="dark"] .leaflet-tile-pane {
  filter: var(--map-filter);
}

[data-theme="dark"] .leaflet-control-attribution {
  background: rgba(30, 41, 59, 0.8);
  color: #cbd5e1;
}

[data-theme="dark"] .leaflet-control-attribution a {
  color: #60a5fa;
}

[data-theme="dark"] .leaflet-control-scale-line {
  background: rgba(15, 23, 42, 0.9);
  border-color: #f8fafc;
  color: #f8fafc;
  text-shadow: none;
}

.leaflet-control-scale {
  text-align: right;
}

.leaflet-control-scale-line {
  width: fit-content;
  margin-left: auto;
  direction: ltr;
  text-align: right;
  border-left: 2px solid #777;
  border-right: 2px solid #777;
}

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

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

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  position: fixed; /* Prevent scrolling/bouncing */
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: #f1f5f9;
}

main {
  width: 100%;
  height: 100%;
  height: 100dvh;
  position: fixed;
  top: 0;
  left: 0;
  overflow: hidden;
}

#map {
  width: 100%;
  height: 100%;
}

#drawing-canvas {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 999;
  pointer-events: none;
  transform-origin: 0 0;
  will-change: transform;
  width: 100%;
  height: 100%;
}

#drawing-canvas.drawing-mode {
  pointer-events: auto;
  cursor: crosshair;
}

.top-controls {
  position: fixed;
  top: 8px;
  left: 16px;
  right: 16px;
  z-index: 1001;
  display: flex;
  align-items: center;
  gap: 10px;
  justify-content: flex-start;
  pointer-events: none;
}

.toolbar {
  position: fixed;
  top: 8px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1001;
  background: var(--bg-toolbar);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-radius: 12px;
  box-shadow: 0 4px 24px var(--shadow-color), 0 1px 2px rgba(0, 0, 0, 0.08);
  padding: 8px 12px;
  display: flex;
  gap: 6px;
  align-items: center;
  border: 1px solid var(--border-color);
  /* Avoid Firefox shrink-to-fit clipping for fixed centered flex containers. */
  width: calc(100vw - 24px);
  max-width: var(--toolbar-max-width);
}

.mobile-draw-toggle {
  display: none;
}

.mobile-share-toggle {
  display: none;
}

.mobile-draw-toggle svg {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
}

.mobile-share-toggle svg {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
}

.toolbar button {
  padding: 7px 9px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  transition: all 0.15s ease;
}

.toolbar button:hover {
  transform: translateY(-1px);
}

.toolbar button:active {
  transform: translateY(0);
}

.toolbar button.secondary {
  background: var(--bg-secondary);
  color: var(--text-secondary);
}

.toolbar button.secondary:hover {
  background: var(--bg-hover);
}

.color-group {
  display: flex;
  gap: 4px;
  align-items: center;
}

.color-swatch {
  width: 32px;
  height: 32px;
  border-radius: 6px;
  cursor: pointer;
  border: 2px solid transparent;
  transition: all 0.15s ease;
  flex-shrink: 0;
  margin: 0 2px;
}

.color-swatch:hover {
  transform: scale(1.1);
}

.color-swatch.bordered {
  box-shadow: inset 0 0 0 1px var(--border-color);
}

.color-swatch.active {
  border-color: #1e293b;
  box-shadow: 0 0 0 2px white, 0 0 0 4px #3b82f6;
}

.color-picker-wrapper {
  position: relative;
  width: 32px;
  height: 32px;
}

.color-picker-wrapper.active {
  border-radius: 6px;
  box-shadow: 0 0 0 2px white, 0 0 0 4px #3b82f6;
}

.color-picker {
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  padding: 0;
  background: conic-gradient(red, yellow, lime, aqua, blue, magenta, red);
}

.color-picker::-webkit-color-swatch-wrapper {
  padding: 0;
}

.color-picker::-webkit-color-swatch {
  border: none;
  border-radius: 6px;
}

.color-picker-icon {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 18px;
  height: 18px;
  pointer-events: none;
  color: white;
  filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.5));
}

.brush-size-group {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--bg-secondary);
  padding: 4px 8px;
  border-radius: 8px;
}

.brush-size-group label {
  font-size: 12px;
  color: var(--text-secondary);
  font-weight: 500;
}

.brush-size {
  width: 48px;
  height: 28px;
  border: none;
  border-radius: 6px;
  padding: 0 6px;
  font-size: 13px;
  font-weight: 500;
  text-align: center;
  background: var(--bg-active);
  color: var(--text-primary);
}

.brush-size:focus {
  outline: 2px solid #3b82f6;
  outline-offset: 1px;
}

.search-container {
  display: flex;
  align-items: center;
  gap: 6px;
  position: relative;
  box-sizing: border-box;
  height: 36px;
  background: #fff;
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-radius: 6px;
  box-shadow: none;
  border: 1px solid #e5e7eb;
  padding: 0 6px 0 10px;
  flex: 0 0 360px;
  max-width: 38vw;
  pointer-events: auto;
}

.search-suggestions {
  display: none;
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  min-width: 100%;
  background: var(--bg-input);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
  z-index: 1000;
  overflow: hidden;
}

.search-suggestions.open {
  display: block;
}

.search-suggestion {
  padding: 9px 12px;
  font-size: 13px;
  color: var(--text-primary);
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  border-bottom: 1px solid var(--border-color);
}

.search-suggestion:last-child {
  border-bottom: none;
}

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

.search-input {
  width: 100%;
  min-width: 0;
  height: 100%;
  border: none;
  border-radius: 12px;
  padding: 0 6px 0 0;
  font-size: 14px;
  background: transparent;
  color: var(--text-primary);
  transition: color 0.15s ease;
}

.search-input:focus {
  outline: none;
}

.search-input::placeholder {
  color: #9ca3af;
}

.search-container #search-btn {
  width: 28px;
  height: 28px;
  border-radius: 10px;
  color: #6b7280;
  background: transparent;
  flex-shrink: 0;
}

.search-container #search-btn:hover {
  background: #f3f4f6;
  color: #4b5563;
  transform: none;
}

.search-container #search-btn:active {
  transform: none;
}

.search-container #search-btn svg {
  width: 18px;
  height: 18px;
}

.search-container:focus-within {
  border-color: #d1d5db;
  box-shadow: none;
}

.tool-group {
  display: flex;
  gap: 2px;
  background: var(--bg-secondary);
  padding: 3px;
  border-radius: 8px;
}

.toolbar {
  pointer-events: auto;
}

.tool-btn {
  width: 32px;
  height: 32px;
  padding: 0;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  background: transparent;
  color: var(--text-secondary);
  transition: all 0.15s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}

.tool-btn:hover {
  background: var(--bg-hover);
  color: var(--text-secondary);
}

.tool-btn.active {
  background: var(--bg-active);
  color: #3b82f6;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.tool-btn svg {
  display: block;
  width: 20px;
  height: 20px;
}

.leaflet-bar.locate-control {
  margin-top: 8px;
  border-radius: 4px;
  overflow: hidden;
  border: 1px solid #3f89ff;
  box-shadow: 5px 5px 5px 5px rgba(0, 0, 0, 0.1);
}

.leaflet-bar.locate-control #locate-btn {
  width: 35px;
  height: 35px;
  border-radius: 0;
  background: #4a90f7;
  color: #fff;
}

.leaflet-bar.locate-control #locate-btn svg {
  width: 24px;
  height: 24px;
  stroke-width: 2;
}

.leaflet-control-zoom a {
  width: 35px !important;
  height: 35px !important;
  line-height: 35px !important;
  font-size: 28px !important;
}

.action-btn {
  width: 32px;
  height: 32px;
  padding: 0;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s ease;
}

.action-btn svg {
  width: 20px;
  height: 20px;
}

.feedback-btn-label {
  display: none;
}

.action-btn.undo,
.action-btn.redo,
.action-btn.theme {
  background: var(--bg-secondary);
  color: var(--text-secondary);
}

@media (min-width: 769px) {
  #faq-btn {
    position: fixed;
    left: 16px;
    right: auto;
    top: auto;
    bottom: 16px;
    z-index: 1002;
  }

  #feedback-btn {
    position: fixed;
    left: 56px;
    right: auto;
    top: auto;
    bottom: 16px;
    z-index: 1002;
    width: auto;
    padding: 0 10px;
    gap: 6px;
  }

  #feedback-btn .feedback-btn-label {
    display: inline;
    font-size: 13px;
    font-weight: 600;
    line-height: 1;
  }
}

.action-btn.undo:hover,
.action-btn.redo:hover,
.action-btn.theme:hover {
  background: var(--bg-hover);
  color: var(--text-secondary);
}

.action-btn.clear {
  background: #fef2f2;
  color: #dc2626;
}

.action-btn.clear:hover {
  background: #fee2e2;
}

[data-theme="dark"] .action-btn.clear {
  background: #7f1d1d;
  color: #fecaca;
}

[data-theme="dark"] .action-btn.clear:hover {
  background: #991b1b;
}

.action-btn.share {
  background: linear-gradient(135deg, #22c55e 0%, #16a34a 100%);
  color: white;
  box-shadow: 0 2px 8px rgba(34, 197, 94, 0.3);
}

.action-btn.share:hover {
  box-shadow: 0 4px 12px rgba(34, 197, 94, 0.4);
  transform: translateY(-1px);
}

.divider {
  width: 1px;
  height: 32px;
  background: var(--divider-color);
  margin: 0 2px;
}

.toast {
  position: absolute;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%) translateY(200px);
  background: #1e293b;
  color: white;
  padding: 16px 26px;
  border-radius: 12px;
  z-index: 1003;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  font-size: 16px;
  font-weight: 500;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
  display: flex;
  align-items: center;
  gap: 10px;
}

.toast::before {
  content: '';
  width: 8px;
  height: 8px;
  background: #22c55e;
  border-radius: 50%;
  flex-shrink: 0;
}

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

.instructions {
  position: absolute;
  bottom: 60px;
  left: 15px;
  right: auto;
  z-index: 1001;
  background: var(--bg-toolbar);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-radius: 12px;
  box-shadow: 0 4px 24px var(--shadow-color);
  padding: 14px 16px;
  font-size: 12px;
  color: var(--text-secondary);
  max-width: 220px;
  line-height: 1.5;
  border: 1px solid var(--border-color);
  transition: all 0.3s ease;
}

.instructions.hidden {
  opacity: 0;
  transform: translateY(10px);
  pointer-events: none;
}

.instructions-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.instructions-header strong {
  color: var(--text-primary);
  font-size: 13px;
}

.instructions-close {
  width: 20px;
  height: 20px;
  border: none;
  background: var(--bg-secondary);
  border-radius: 4px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  transition: all 0.15s ease;
}

.instructions-close:hover {
  background: var(--bg-hover);
  color: var(--text-secondary);
}

.seo-banner .instructions-close {
  position: absolute;
  top: 8px;
  right: 8px;
}

.seo-banner.hidden {
  display: none;
}

.instructions ol {
  margin: 0;
  padding-left: 18px;
}

.instructions li {
  margin-bottom: 4px;
}

.instructions li:last-child {
  margin-bottom: 0;
}


[data-tooltip] {
  position: relative;
}

[data-tooltip]::after {
  content: attr(data-tooltip);
  position: absolute;
  bottom: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%) scale(0.8);
  background: var(--text-primary);
  color: var(--bg-toolbar);
  padding: 6px 10px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 500;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s ease, transform 0.15s ease;
  z-index: 1100;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

[data-tooltip]:hover::after {
  opacity: 1;
  transform: translateX(-50%) scale(1);
}

[data-tooltip]::before {
  content: '';
  position: absolute;
  bottom: calc(100% + 2px);
  left: 50%;
  transform: translateX(-50%) scale(0.8);
  border: 6px solid transparent;
  border-top-color: var(--text-primary);
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s ease, transform 0.15s ease;
  z-index: 1100;
}

[data-tooltip]:hover::before {
  opacity: 1;
  transform: translateX(-50%) scale(1);
}

[data-tooltip-position="bottom"]::after {
  bottom: auto;
  top: calc(100% + 8px);
}

[data-tooltip-position="bottom"]::before {
  bottom: auto;
  top: calc(100% + 2px);
  border-top-color: transparent;
  border-bottom-color: var(--text-primary);
}

.faq-dialog {
  border: none;
  border-radius: 16px;
  padding: 0;
  max-width: 560px;
  width: calc(100% - 32px);
  margin: auto;
  position: fixed;
  inset: 0;
  height: fit-content;
  background: var(--bg-toolbar);
  color: var(--text-primary);
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.25);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}

.faq-dialog::backdrop {
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

.faq-dialog-inner {
  padding: 24px;
  max-height: 80vh;
  overflow-y: auto;
}

.faq-dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.faq-dialog-header h2 {
  font-size: 18px;
  font-weight: 700;
  color: var(--text-primary);
  margin: 0;
}

.faq-dialog-close {
  width: 32px;
  height: 32px;
  border: none;
  background: var(--bg-secondary);
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  transition: all 0.15s ease;
}

.faq-dialog-close:hover {
  background: var(--bg-hover);
  color: var(--text-secondary);
}

.faq-item {
  margin-bottom: 16px;
}

.faq-item:last-child {
  margin-bottom: 0;
}

.faq-item h3 {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 6px;
}

.faq-item p {
  font-size: 13px;
  color: var(--text-secondary);
  line-height: 1.6;
  margin: 0;
}

.faq-item a {
  color: #3b82f6;
  text-decoration: none;
  font-weight: 500;
  font-size: 13px;
}

.faq-item a:hover {
  text-decoration: underline;
}

.faq-divider {
  height: 1px;
  background: var(--divider-color);
  margin: 16px 0;
}

@media (max-width: 768px) {
  .top-controls {
    top: 12px;
    left: 12px;
    right: 12px;
    display: block;
    pointer-events: auto;
  }

  body > div:first-of-type {
    display: none;
  }

  .toolbar {
    position: fixed;
    top: calc(12px + env(safe-area-inset-top) + 47px + 10px);
    bottom: auto;
    left: 12px;
    right: 12px;
    transform: none;
    flex-wrap: wrap;
    justify-content: center;
    padding: 10px;
    gap: 8px;
    display: none;
  }

  .toolbar.mobile-open {
    display: flex;
  }

  .toolbar .color-group {
    order: 3;
  }

  .toolbar .brush-size-group {
    order: 2;
  }

  .toolbar .tool-group {
    order: 1;
  }

  .toolbar #clear-btn,
  .toolbar #undo-btn,
  .toolbar #redo-btn {
    order: 4;
  }

  .color-group .color-swatch:not([data-color="#e63946"]):not([data-color="#22c55e"]):not([data-color="#3b82f6"]) {
    display: none;
  }

  .color-group .color-swatch[data-color="#e63946"] {
    order: 1;
  }

  .color-group .color-swatch[data-color="#22c55e"] {
    order: 2;
  }

  .color-group .color-swatch[data-color="#3b82f6"] {
    order: 3;
  }

  .color-group .color-picker-wrapper {
    order: 4;
  }

  .mobile-draw-toggle {
    right: 12px;
    margin-left: auto;
    position: fixed;
    top: calc(12px + env(safe-area-inset-top) + 47px + 10px);
    left: auto;
    height: 40px;
    padding: 0 14px;
    gap: 8px;
    border-radius: 10px;
    border: 1px solid var(--border-color);
    background: var(--bg-toolbar);
    color: var(--text-primary);
    font-size: 14px;
    font-weight: 600;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    z-index: 1002;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.16);
  }

  .mobile-share-toggle {
    position: fixed;
    top: auto;
    bottom: calc(25px + env(safe-area-inset-bottom) + 50px + 18px);
    right: 12px;
    width: 50px;
    height: 50px;
    border-radius: 10px;
    border: 1px solid #16a34a;
    background: #22c55e;
    color: white;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    z-index: 1002;
    box-shadow: 0 6px 14px rgba(0, 0, 0, 0.2), 0 2px 5px rgba(0, 0, 0, 0.2);
  }

  .mobile-draw-toggle.is-open {
    width: 40px;
    padding: 0;
    font-size: 24px;
    line-height: 1;
    border: none;
    background: transparent;
    box-shadow: none;
  }

  .mobile-draw-toggle.is-open svg {
    display: none;
  }

  .search-container {
    position: fixed;
    top: calc(12px + env(safe-area-inset-top));
    left: 12px;
    right: 12px;
    width: auto;
    max-width: none;
    height: 47px;
    border-radius: 50px;
    padding: 0 12px 0 18px;
    box-shadow: 0 6px 14px rgba(0, 0, 0, 0.2), 0 2px 5px rgba(0, 0, 0, 0.2);
    margin: 0;
    z-index: 1001;
  }

  .search-input {
    width: 100%;
    font-size: 16px; /* Prevent iOS zoom */
  }

  .search-container #search-btn {
    width: 32px;
    height: 32px;
    border-radius: 12px;
  }

  .toolbar #share-btn {
    display: none;
  }

  #faq-btn.mobile-floating-faq {
    position: fixed;
    left: 6px;
    right: auto;
    top: auto;
    bottom: calc(15px + env(safe-area-inset-bottom) + 34px);
    z-index: 1002;
  }

  #feedback-btn.mobile-floating-feedback {
    position: fixed;
    left: 46px;
    right: auto;
    top: auto;
    bottom: calc(15px + env(safe-area-inset-bottom) + 34px);
    z-index: 1002;
  }

  .leaflet-bar.locate-control #locate-btn {
    width: 50px;
    height: 50px;
    border-radius: 999px;
  }

  .leaflet-bar.locate-control #locate-btn svg {
    width: 32px;
    height: 32px;
    stroke-width: 1.2;
  }

  .leaflet-bar.locate-control {
    border-radius: 999px;
    overflow: visible;
    box-shadow: 0 6px 14px rgba(0, 0, 0, 0.2), 0 2px 5px rgba(0, 0, 0, 0.2);
  }

  .leaflet-control-zoom {
    display: none !important;
  }

  .leaflet-control-scale {
    text-align: left;
  }

  .leaflet-control-scale-line {
    margin-left: 0;
    margin-right: auto;
    text-align: left;
    margin-top: 0;
  }

  .brush-size {
    font-size: 16px; /* Prevent iOS zoom */
  }

  /* Prevent double-tap zoom on interactive toolbar controls */
  .toolbar button,
  .toolbar input,
  .toolbar select,
  .toolbar [role="button"] {
    touch-action: manipulation;
  }

  .instructions {
    bottom: auto;
    top: 50%;
    left: 50%;
    right: auto;
    transform: translate(-50%, -50%);
    width: calc(100% - 24px);
    max-width: 320px;
  }

  .toast {
    bottom: auto;
    top: 16px;
    left: 12px;
    right: 12px;
    transform: translateY(-200px);
    width: auto;
  }

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

  .divider {
    display: none;
  }

  [data-tooltip]::after,
  [data-tooltip]::before {
    display: none;
  }
}
