/* ========================================
   Farm App - Corn Maze Design & Navigation
   ======================================== */

:root {
  --color-primary: #1b5e20;
  --color-primary-light: #2e7d32;
  --color-accent: #ff9500;
  --color-danger: #ff3b30;
  --color-bg: #111111;
  --color-surface: rgba(30, 30, 30, 0.95);
  --color-surface-light: rgba(50, 50, 50, 0.95);
  --color-text: #ffffff;
  --color-text-muted: #aaaaaa;
  --color-border: rgba(255, 255, 255, 0.15);
  --top-bar-height: 48px;
  --status-bar-height: 28px;
  --search-top-offset: 60px;
  --panel-top-offset: 158px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --touch-min: 48px;
}

/* Field mode overrides */
body.field-mode {
  --color-surface: rgba(0, 0, 0, 0.9);
  --color-surface-light: rgba(10, 10, 10, 0.95);
  --color-text: #00ff88;
  --color-text-muted: #00cc66;
  --color-border: rgba(0, 255, 136, 0.3);
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

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

/* Offline banner */
.offline-banner {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 10000;
  background: var(--color-danger);
  color: white;
  text-align: center;
  padding: 6px 12px;
  font-size: 13px;
  font-weight: 600;
  transition: transform 0.3s ease;
}

.offline-banner.hidden {
  transform: translateY(-100%);
}

/* Top bar */
.top-bar {
  position: fixed;
  top: var(--safe-top);
  left: 0;
  right: 0;
  height: var(--top-bar-height);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 8px;
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

.top-bar-left {
  flex: 0 0 auto;
  min-width: 80px;
}

.app-title {
  font-size: 16px;
  font-weight: 700;
  color: var(--color-text);
  white-space: nowrap;
}

.top-bar-center {
  flex: 0 0 auto;
  display: flex;
  gap: 2px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  padding: 2px;
}

.mode-tab {
  background: none;
  border: none;
  color: var(--color-text-muted);
  font-size: 14px;
  font-weight: 600;
  padding: 6px 16px;
  border-radius: 6px;
  cursor: pointer;
  min-height: var(--touch-min);
  display: flex;
  align-items: center;
  transition: all 0.2s;
}

.mode-tab.active {
  background: var(--color-primary);
  color: white;
}

.top-bar-right {
  flex: 0 0 auto;
  display: flex;
  gap: 4px;
  min-width: 132px;
  justify-content: flex-end;
}

.top-btn {
  background: none;
  border: none;
  color: var(--color-text);
  width: var(--touch-min);
  height: var(--touch-min);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.2s;
}

.top-btn:active {
  background: rgba(255, 255, 255, 0.15);
}

.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;
}

/* Map */
#map {
  position: fixed;
  top: calc(var(--safe-top) + var(--top-bar-height));
  left: 0;
  right: 0;
  bottom: calc(var(--safe-bottom) + var(--status-bar-height));
  z-index: 1;
}

#map.draw-active {
  cursor: crosshair;
}

.map-quick-controls {
  position: fixed;
  right: 16px;
  bottom: calc(var(--safe-bottom) + var(--status-bar-height) + 112px);
  z-index: 1003;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 10px;
  pointer-events: none;
}

.quick-map-btn {
  width: 58px;
  height: 58px;
  border: 1px solid var(--color-border);
  border-radius: 14px;
  background: var(--color-surface);
  color: var(--color-text);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  pointer-events: auto;
}

.quick-map-btn:active {
  transform: scale(0.96);
}

.quick-map-btn.active {
  background: var(--color-primary);
  border-color: var(--color-primary-light);
  color: white;
}

.exit-map-only-btn {
  display: none;
}

.field-status {
  display: none;
  max-width: min(260px, calc(100vw - 32px));
  padding: 8px 10px;
  border-radius: 10px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  color: var(--color-text);
  font-size: 12px;
  line-height: 1.35;
  text-align: right;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.35);
  pointer-events: none;
}

.field-status.visible {
  display: block;
}

.field-status.error {
  color: #ff8a80;
}

body.map-only #map {
  top: 0;
  bottom: 0;
}

body.map-only .top-bar,
body.map-only .address-search,
body.map-only .design-toolbar,
body.map-only .nav-hud,
body.map-only .nav-controls,
body.map-only .status-bar,
body.map-only .leaflet-control-zoom {
  display: none;
}

body.map-only .map-quick-controls {
  right: 16px;
  bottom: calc(var(--safe-bottom) + 16px);
}

body.map-only #btn-map-only {
  display: none;
}

body.map-only .exit-map-only-btn {
  display: flex;
}

/* Address search */
.address-search {
  position: fixed;
  top: calc(var(--safe-top) + var(--search-top-offset));
  left: 12px;
  right: 12px;
  z-index: 1002;
  max-width: 560px;
  margin: 0 auto;
  pointer-events: auto;
}

.address-search-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 52px;
  min-height: 52px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.35);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

.address-search input {
  min-width: 0;
  width: 100%;
  border: none;
  outline: none;
  background: transparent;
  color: var(--color-text);
  font: inherit;
  font-size: 16px;
  padding: 0 14px;
}

.address-search input::placeholder {
  color: var(--color-text-muted);
}

.address-search-btn {
  width: 52px;
  min-height: 52px;
  border: none;
  border-left: 1px solid var(--color-border);
  background: var(--color-primary);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.address-search-btn:active {
  transform: scale(0.96);
}

.address-search-btn:disabled {
  opacity: 0.65;
  cursor: progress;
}

.address-search-status {
  display: none;
  margin: 6px 8px 0;
  padding: 6px 10px;
  border-radius: 8px;
  background: var(--color-surface);
  color: var(--color-text-muted);
  font-size: 12px;
  line-height: 1.35;
  border: 1px solid var(--color-border);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.address-search.has-status .address-search-status {
  display: block;
}

.address-search-status.error {
  color: #ff8a80;
}

/* Design toolbar */
.design-toolbar {
  position: fixed;
  top: calc(var(--safe-top) + var(--panel-top-offset));
  left: 12px;
  z-index: 1001;
  background: var(--color-surface);
  border-radius: 12px;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  border: 1px solid var(--color-border);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  max-height: calc(100vh - var(--panel-top-offset) - var(--status-bar-height) - var(--safe-top) - var(--safe-bottom) - 20px);
  overflow-y: auto;
}

.design-toolbar.hidden {
  display: none;
}

.toolbar-section {
  display: flex;
  flex-direction: column;
  gap: 4px;
  align-items: center;
}

.toolbar-divider {
  height: 1px;
  background: var(--color-border);
  margin: 4px 0;
}

.toolbar-label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--color-text-muted);
  margin-bottom: 2px;
}

.tool-btn {
  width: var(--touch-min);
  height: var(--touch-min);
  background: none;
  border: 2px solid transparent;
  border-radius: 10px;
  color: var(--color-text);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s;
}

.tool-btn:active {
  background: rgba(255, 255, 255, 0.1);
}

.tool-btn.active {
  background: var(--color-primary);
  border-color: var(--color-primary-light);
}

.tool-btn.eraser-active {
  background: var(--color-danger);
  border-color: #ff6b6b;
}

.tool-btn-danger {
  color: var(--color-danger);
}

.tool-btn:disabled {
  opacity: 0.3;
  cursor: default;
}

/* Color picker */
.color-picker {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
}

.color-swatch {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 2px solid transparent;
  cursor: pointer;
  transition: transform 0.15s, border-color 0.15s;
}

.color-swatch.active {
  border-color: white;
  transform: scale(1.15);
}

.color-swatch:active {
  transform: scale(0.9);
}

/* Width picker */
.width-picker {
  display: flex;
  gap: 4px;
}

.width-btn {
  width: 40px;
  height: 36px;
  background: none;
  border: 2px solid transparent;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.15s;
}

.width-btn.active {
  background: var(--color-primary);
  border-color: var(--color-primary-light);
}

.width-preview {
  display: block;
  width: 24px;
  background: var(--color-text);
  border-radius: 2px;
}

/* Navigate HUD */
.nav-hud {
  position: fixed;
  top: calc(var(--safe-top) + var(--panel-top-offset));
  left: 50%;
  transform: translateX(-50%);
  z-index: 1001;
  background: var(--color-surface);
  border-radius: 12px;
  padding: 12px 20px;
  display: flex;
  gap: 24px;
  border: 1px solid var(--color-border);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

.nav-hud.hidden {
  display: none;
}

.hud-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.hud-label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--color-text-muted);
}

.hud-value {
  font-size: 20px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--color-text);
}

body.field-mode .hud-value {
  color: #00ff88;
  text-shadow: 0 0 10px rgba(0, 255, 136, 0.5);
}

/* Navigate controls */
.nav-controls {
  position: fixed;
  bottom: calc(var(--safe-bottom) + var(--status-bar-height) + 16px);
  right: 16px;
  z-index: 1001;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 12px;
}

.nav-controls.hidden {
  display: none;
}

.nav-action-btn {
  background: var(--color-primary);
  color: white;
  border: none;
  border-radius: 28px;
  padding: 14px 28px;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  min-height: var(--touch-min);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
  transition: all 0.2s;
}

.nav-action-btn:active {
  transform: scale(0.96);
}

.nav-action-btn.nav-active {
  background: var(--color-danger);
}

body.field-mode .nav-action-btn {
  background: #00cc44;
  color: black;
}

body.field-mode .nav-action-btn.nav-active {
  background: #ff3b30;
  color: white;
}

.field-mode-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--color-surface);
  padding: 8px 14px;
  border-radius: 20px;
  cursor: pointer;
  border: 1px solid var(--color-border);
  user-select: none;
}

.field-mode-toggle input {
  accent-color: var(--color-primary);
  width: 18px;
  height: 18px;
}

.toggle-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--color-text);
}

/* Status bar */
.status-bar {
  position: fixed;
  bottom: var(--safe-bottom);
  left: 0;
  right: 0;
  height: var(--status-bar-height);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px;
  background: var(--color-surface);
  border-top: 1px solid var(--color-border);
  font-size: 11px;
  color: var(--color-text-muted);
}

/* Dialog overlay */
.dialog-overlay {
  position: fixed;
  inset: 0;
  z-index: 5000;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.dialog-overlay.hidden {
  display: none;
}

.dialog {
  background: var(--color-surface-light);
  border-radius: 16px;
  padding: 24px;
  width: 100%;
  max-width: 420px;
  max-height: 80vh;
  overflow-y: auto;
  border: 1px solid var(--color-border);
}

.dialog-small {
  max-width: 320px;
  text-align: center;
}

.dialog h2 {
  font-size: 18px;
  margin-bottom: 16px;
}

.dialog p {
  font-size: 15px;
  line-height: 1.5;
  margin-bottom: 16px;
}

.dialog-field {
  margin-bottom: 14px;
}

.dialog-field label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 6px;
  color: var(--color-text-muted);
}

.dialog-field input,
.dialog-field textarea {
  width: 100%;
  padding: 10px 12px;
  background: rgba(0, 0, 0, 0.3);
  border: 1px solid var(--color-border);
  border-radius: 8px;
  color: var(--color-text);
  font-size: 15px;
  font-family: inherit;
  outline: none;
}

.dialog-field input:focus,
.dialog-field textarea:focus {
  border-color: var(--color-primary-light);
}

.dialog-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  margin-top: 20px;
}

.dialog-btn {
  padding: 10px 20px;
  border: none;
  border-radius: 8px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  min-height: var(--touch-min);
  display: flex;
  align-items: center;
  transition: opacity 0.2s;
}

.dialog-btn:active {
  opacity: 0.7;
}

.dialog-btn-primary {
  background: var(--color-primary);
  color: white;
}

.dialog-btn-secondary {
  background: rgba(255, 255, 255, 0.1);
  color: var(--color-text);
}

.dialog-btn-danger {
  background: var(--color-danger);
  color: white;
}

.dialog-status {
  margin-top: 10px;
  font-size: 13px;
  color: var(--color-text-muted);
  text-align: center;
  min-height: 18px;
}

.dialog-status.error {
  color: var(--color-danger);
}

/* Load list */
.load-list {
  max-height: 50vh;
  overflow-y: auto;
}

.load-empty {
  text-align: center;
  padding: 24px;
  color: var(--color-text-muted);
  font-size: 14px;
}

.load-item {
  display: flex;
  align-items: center;
  padding: 12px;
  border: 1px solid var(--color-border);
  border-radius: 10px;
  margin-bottom: 8px;
  cursor: pointer;
  transition: background 0.15s;
}

.load-item:active {
  background: rgba(255, 255, 255, 0.08);
}

.load-item-info {
  flex: 1;
  min-width: 0;
}

.load-item-name {
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.load-item-desc {
  font-size: 12px;
  color: var(--color-text-muted);
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.load-item-delete {
  background: none;
  border: none;
  color: var(--color-danger);
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  cursor: pointer;
  flex-shrink: 0;
  margin-left: 8px;
}

.load-item-delete:active {
  background: rgba(255, 59, 48, 0.2);
}

/* Leaflet overrides */
.leaflet-control-zoom {
  border: none !important;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.4) !important;
}

.leaflet-control-zoom a {
  background: var(--color-surface) !important;
  color: var(--color-text) !important;
  border: none !important;
  width: 40px !important;
  height: 40px !important;
  line-height: 40px !important;
  font-size: 18px !important;
}

.leaflet-control-zoom a:first-child {
  border-radius: 8px 8px 0 0 !important;
}

.leaflet-control-zoom a:last-child {
  border-radius: 0 0 8px 8px !important;
}

/* Field mode map styling */
body.field-mode .leaflet-tile-pane {
  filter: brightness(0.4) contrast(1.2);
}

body.field-mode .top-bar,
body.field-mode .status-bar {
  background: rgba(0, 0, 0, 0.9);
  border-color: rgba(0, 255, 136, 0.2);
}

body.field-mode .app-title {
  color: #00ff88;
}

/* GPS marker styles */
.gps-marker {
  position: relative;
}

.gps-dot {
  width: 24px;
  height: 24px;
  background: #007aff;
  border: 3px solid white;
  border-radius: 50%;
  box-shadow: 0 0 12px rgba(0, 122, 255, 0.6);
}

body.field-mode .gps-dot {
  background: #00ff88;
  box-shadow: 0 0 20px rgba(0, 255, 136, 0.8);
}

.gps-heading {
  position: absolute;
  top: -12px;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 6px solid transparent;
  border-right: 6px solid transparent;
  border-bottom: 12px solid #007aff;
}

body.field-mode .gps-heading {
  border-bottom-color: #00ff88;
}

.gps-accuracy {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  background: rgba(0, 122, 255, 0.15);
  border: 1px solid rgba(0, 122, 255, 0.3);
  pointer-events: none;
}

.search-marker {
  position: relative;
}

.search-pin {
  width: 28px;
  height: 28px;
  background: #ff9500;
  border: 3px solid white;
  border-radius: 50% 50% 50% 0;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.45);
  transform: rotate(-45deg);
}

.search-pin::after {
  content: '';
  position: absolute;
  width: 8px;
  height: 8px;
  left: 7px;
  top: 7px;
  background: white;
  border-radius: 50%;
}

.leaflet-popup-content {
  font-size: 13px;
  line-height: 1.35;
}

/* Responsive adjustments for small screens */
@media (max-width: 380px) {
  :root {
    --panel-top-offset: 158px;
  }

  .top-bar-left {
    min-width: 60px;
  }

  .top-bar-right {
    min-width: 120px;
  }

  .app-title {
    font-size: 14px;
  }

  .mode-tab {
    padding: 6px 10px;
  }

  .top-btn {
    width: 40px;
  }

  .nav-hud {
    gap: 12px;
    padding: 8px 12px;
  }

  .hud-value {
    font-size: 16px;
  }

  .design-toolbar {
    left: 8px;
  }
}

@media (max-width: 480px) {
  #btn-map-only-top {
    display: none;
  }

  .top-bar-right {
    min-width: 100px;
  }
}

@media (max-height: 600px) {
  .design-toolbar {
    flex-direction: row;
    flex-wrap: wrap;
    top: calc(var(--safe-top) + var(--panel-top-offset));
    left: 8px;
    right: 60px;
  }

  .toolbar-divider {
    width: 1px;
    height: auto;
    margin: 0 4px;
  }

  .toolbar-section {
    flex-direction: row;
  }
}
