* { box-sizing: border-box; }

:root {
  --green: #1D9E75;
  --green-dark: #167A5B;
  --orange: #D85A30;
  --blue: #378ADD;
  --ink: #1A1D1E;
  --text: #2B2E30;
  --text-secondary: #6B7075;
  --text-muted: #9AA0A6;
  --border: #E8E9EA;
  --border-strong: #D8DADC;
  --surface: #FFFFFF;
  --surface-2: #F7F8F8;
  --surface-3: #F0F1F2;
  --shadow-sm: 0 1px 3px rgba(20, 22, 24, 0.06), 0 1px 2px rgba(20, 22, 24, 0.04);
  --shadow-md: 0 4px 16px rgba(20, 22, 24, 0.10);
  --shadow-lg: 0 -4px 24px rgba(20, 22, 24, 0.14);
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 18px;
}

html, body {
  margin: 0;
  height: 100%;
}
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  color: var(--text);
  height: 100vh;
  height: 100dvh;
  height: var(--app-vh, 100dvh);
  overflow: hidden;
}

button {
  font-family: inherit;
  cursor: pointer;
}
input, select {
  font-family: inherit;
}

/* ---------- логин ---------- */

#login-screen {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  background: linear-gradient(180deg, #FAFBFA 0%, #F2F4F3 100%);
  z-index: 1000;
  padding: 24px;
}
#login-screen h1 {
  font-size: 19px;
  font-weight: 600;
  margin: 0 0 18px;
  color: var(--ink);
  letter-spacing: -0.01em;
}
#login-screen input {
  width: 280px;
  padding: 12px 14px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  font-size: 15px;
  background: var(--surface);
  transition: border-color 0.15s ease;
}
#login-screen input:focus {
  outline: none;
  border-color: var(--green);
}
#login-screen button {
  width: 280px;
  padding: 12px 14px;
  border: none;
  border-radius: var(--radius-sm);
  background: var(--green);
  color: #fff;
  font-size: 15px;
  font-weight: 500;
  margin-top: 4px;
  transition: background 0.15s ease;
}
#login-screen button:hover { background: var(--green-dark); }
#login-error { color: var(--orange); font-size: 13px; min-height: 18px; }

/* ---------- каркас приложения ---------- */

#app { display: none; height: 100%; flex-direction: column; background: var(--surface-2); }

.app-nav {
  display: flex;
  gap: 2px;
  padding: 8px 8px 0;
  background: var(--ink);
  flex-shrink: 0;
}
.app-nav a {
  color: #999;
  text-decoration: none;
  font-size: 13px;
  font-weight: 500;
  padding: 10px 18px;
  border-radius: 8px 8px 0 0;
  transition: color 0.15s ease, background 0.15s ease;
}
.app-nav a:hover { color: #ddd; }
.app-nav a.active {
  background: var(--surface-2);
  color: var(--ink);
}

#topbar {
  display: flex;
  gap: 8px;
  padding: 10px 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  z-index: 500;
  flex-shrink: 0;
}
#topbar input {
  flex: 1;
  padding: 9px 14px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  font-size: 14px;
  background: var(--surface-2);
  transition: border-color 0.15s ease, background 0.15s ease;
}
#topbar input:focus {
  outline: none;
  border-color: var(--green);
  background: var(--surface);
}
#topbar button {
  padding: 9px 16px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  white-space: nowrap;
  transition: background 0.15s ease, border-color 0.15s ease;
}
#topbar button:hover { background: var(--surface-3); }
#add-farm-btn {
  background: var(--green) !important;
  color: #fff !important;
  border-color: var(--green) !important;
}
#add-farm-btn:hover { background: var(--green-dark) !important; }

#status-bar {
  font-size: 12px;
  font-weight: 500;
  padding: 6px 14px;
  background: #FDF3E3;
  color: #8A5A0B;
  display: none;
  flex-shrink: 0;
}
#status-bar.visible { display: block; }
#status-bar.offline { background: #FCECEA; color: #9B2C1F; }

#main-area {
  flex: 1;
  display: flex;
  min-height: 0;
  position: relative;
}

#map { flex: 1; min-width: 0; }
.leaflet-container { font-family: inherit; }

/* ---------- панель "рядом" ---------- */

#nearby-panel {
  width: 300px;
  flex-shrink: 0;
  background: var(--surface);
  border-left: 1px solid var(--border);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}
#nearby-header {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  font-weight: 600;
  color: var(--ink);
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-shrink: 0;
}
.mobile-only { display: none; }

#manager-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 12px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  font-size: 11.5px;
  color: var(--text-secondary);
  flex-shrink: 0;
}
#manager-legend .legend-item { display: flex; align-items: center; gap: 5px; }
#manager-legend .dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }

.nearby-item {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  font-size: 13.5px;
  cursor: pointer;
  transition: background 0.12s ease;
}
.nearby-item:hover { background: var(--surface-2); }
.nearby-item .top-row { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.nearby-item .name { font-weight: 500; color: var(--ink); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nearby-item .dist { color: var(--text-muted); font-size: 12px; white-space: nowrap; font-variant-numeric: tabular-nums; }
.nearby-item .address { color: var(--text-secondary); font-size: 12px; margin-top: 2px; }

@media (max-width: 768px) {
  #nearby-panel {
    position: absolute;
    left: 0; right: 0; bottom: 0;
    width: auto;
    height: 60px;
    border-left: none;
    border-top: 1px solid var(--border);
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    box-shadow: var(--shadow-lg);
    transition: height 0.25s cubic-bezier(0.4, 0, 0.2, 1);
    z-index: 850;
  }
  #nearby-panel::before {
    content: "";
    display: block;
    width: 36px;
    height: 4px;
    background: var(--border-strong);
    border-radius: 2px;
    margin: 8px auto 0;
  }
  #nearby-panel.dragging { transition: none; }
  #nearby-panel.expanded { height: 58vh; }
  #nearby-panel:not(.expanded) { overflow: hidden; }
  #nearby-panel:not(.expanded) #nearby-list { overflow: hidden; }
  #nearby-panel.expanded #nearby-list { overflow-y: auto; }
  #nearby-header { cursor: pointer; padding: 8px 16px 12px; touch-action: none; }
  .mobile-only { display: block; font-size: 14px; color: var(--text-muted); transition: transform 0.2s ease; }
  #nearby-panel.expanded #nearby-chevron { transform: rotate(180deg); }
}

/* ---------- карточка хозяйства ---------- */

#detail-panel {
  position: fixed;
  left: 50%; bottom: 0;
  transform: translate(-50%, 100%);
  width: 100%;
  max-width: 520px;
  background: var(--surface);
  border-top-left-radius: var(--radius-lg);
  border-top-right-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  padding: 10px 20px 20px;
  max-height: 72vh;
  overflow-y: auto;
  transition: transform 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  z-index: 900;
}
#detail-panel::before {
  content: "";
  display: block;
  width: 36px;
  height: 4px;
  background: var(--border-strong);
  border-radius: 2px;
  margin: 0 auto 14px;
}
#detail-panel.open { transform: translate(-50%, 0); }
#detail-panel h2 { margin: 0 0 4px; font-size: 18px; font-weight: 600; color: var(--ink); letter-spacing: -0.01em; }
#detail-panel .sub { color: var(--text-secondary); font-size: 13px; margin: 0 0 16px; }
#detail-panel .row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  font-size: 13.5px;
}
#detail-panel .row > span:first-child { color: var(--text-secondary); }
#detail-panel button.primary {
  width: 100%;
  margin-top: 14px;
  padding: 13px;
  border: none;
  border-radius: var(--radius-sm);
  background: var(--green);
  color: #fff;
  font-size: 14.5px;
  font-weight: 500;
  transition: background 0.15s ease;
}
#detail-panel button.primary:hover { background: var(--green-dark); }
#detail-panel button.close {
  position: absolute;
  top: 14px; right: 16px;
  border: none;
  background: var(--surface-3);
  width: 28px; height: 28px;
  border-radius: 50%;
  font-size: 16px;
  line-height: 1;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  justify-content: center;
}
#visit-list, #order-list { margin-top: 4px; }
.visit-item, .order-item {
  font-size: 13.5px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  color: var(--text);
}
.visit-item:last-child, .order-item:last-child { border-bottom: none; }
#visit-list p.sub, #order-list p.sub { margin: 0 0 8px; }

#detail-panel .row select {
  border: 1px solid var(--border-strong);
  border-radius: 7px;
  padding: 6px 8px;
  font-size: 13px;
  max-width: 170px;
  background: var(--surface);
  color: var(--text);
}

/* ---------- вкладки ---------- */

.tabs {
  display: flex;
  gap: 4px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 14px;
}
.tab {
  padding: 9px 14px;
  font-size: 13.5px;
  font-weight: 500;
  color: var(--text-muted);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  transition: color 0.15s ease;
}
.tab:hover { color: var(--text-secondary); }
.tab.active {
  color: var(--green);
  border-bottom-color: var(--green);
}

/* ---------- техника ---------- */

.equipment-item {
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 12px 14px;
  margin-bottom: 10px;
  font-size: 13.5px;
  background: var(--surface-2);
}
.equipment-item .eq-top { display: flex; justify-content: space-between; align-items: start; gap: 10px; }
.equipment-item .eq-model { font-weight: 600; margin: 0; color: var(--ink); }
.equipment-item .eq-sub { color: var(--text-secondary); font-size: 12px; margin: 3px 0 0; }
.equipment-item .eq-badge {
  font-size: 10.5px;
  font-weight: 600;
  padding: 3px 9px;
  border-radius: 20px;
  background: #E4F0FA;
  color: #16588F;
  white-space: nowrap;
  flex-shrink: 0;
}
.equipment-item .eq-pairing {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
  font-size: 12px;
  color: var(--text-secondary);
}
.equipment-item .eq-delete {
  border: none;
  background: none;
  color: var(--orange);
  font-size: 12px;
  font-weight: 500;
  padding: 6px 0 0;
}

#show-add-equipment-btn {
  width: 100%;
  padding: 12px;
  border: 1.5px dashed var(--border-strong);
  border-radius: var(--radius-md);
  background: var(--surface);
  font-size: 13.5px;
  font-weight: 500;
  color: var(--text-secondary);
  transition: border-color 0.15s ease, color 0.15s ease;
}
#show-add-equipment-btn:hover { border-color: var(--green); color: var(--green); }

#add-equipment-form {
  margin-top: 10px;
  padding: 14px;
  background: var(--surface-2);
  border-radius: var(--radius-md);
  display: flex;
  flex-direction: column;
  gap: 8px;
}
#add-equipment-form input, #add-equipment-form select {
  padding: 10px 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  font-size: 14px;
  background: var(--surface);
}
#add-equipment-form input:focus, #add-equipment-form select:focus {
  outline: none;
  border-color: var(--green);
}
.form-actions { display: flex; gap: 8px; margin-top: 2px; }
.form-actions button { flex: 1; padding: 11px; border-radius: var(--radius-sm); font-size: 14px; font-weight: 500; }
.form-actions button.primary { border: none; }
#eq-cancel-btn { border: 1px solid var(--border-strong); background: var(--surface); color: var(--text-secondary); }

/* ---------- разное ---------- */

.new-farm-hint {
  position: fixed;
  top: 68px; left: 50%;
  transform: translateX(-50%);
  background: var(--ink);
  color: #fff;
  padding: 9px 16px;
  border-radius: 20px;
  font-size: 13px;
  font-weight: 500;
  box-shadow: var(--shadow-md);
  z-index: 800;
  display: none;
}

/* ---------- маркеры карты ---------- */

.you-marker, .farm-marker-wrap {
  background-color: transparent;
  border: none;
}
.you-marker {
  width: 16px; height: 16px;
  border-radius: 50%;
  background: var(--blue);
  border: 3px solid #fff;
  box-shadow: 0 0 0 2px var(--blue), 0 2px 6px rgba(0,0,0,0.25);
}
.farm-dot {
  width: 14px; height: 14px;
  border-radius: 50%;
  border: 2px solid #fff;
  box-sizing: border-box;
  box-shadow: 0 1px 4px rgba(0,0,0,0.25);
}
