:root {
  --bg-base: #eef0f4;
  --bg-panel: #f2f4f7;
  --bg-surface: #ffffff;
  --bg-card: #ffffff;
  --bg-hover: #e8ebf0;
  --border: rgba(0, 0, 0, 0.1);
  --border-soft: rgba(0, 0, 0, 0.07);
  --text: #1c2333;
  --text-2: #4a5568;
  --text-3: #8896a8;
  --accent: #3a6ea8;
  --accent-lt: #5585c0;
  --accent-bg: rgba(58, 110, 168, 0.08);
  --green: #1f8c5a;
  --green-bg: rgba(31, 140, 90, 0.09);
  --red: #b83232;
  --red-bg: rgba(184, 50, 50, 0.08);
  --orange: #b86020;
  --orange-bg: rgba(184, 96, 32, 0.1);
  --header-bg: #1e2535;
  --header-text: #dde3f0;
  --header-sub: #8896b0;
  --font-ui: "DM Sans", "Hiragino Sans", sans-serif;
  --font-title: "Syne", "Hiragino Sans", sans-serif;
  --font-mono: "JetBrains Mono", monospace;
  --radius: 6px;
  --radius-sm: 4px;
  --ease: cubic-bezier(0.22, 1, 0.36, 1);
  --shadow-sm:
    0 1px 3px rgba(0, 0, 0, 0.08), 0 1px 2px rgba(0, 0, 0, 0.06);
  --shadow-card:
    0 1px 4px rgba(0, 0, 0, 0.07), 0 0 0 1px rgba(0, 0, 0, 0.04);
}

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

body {
  display: flex;
  flex-direction: column;
  height: 100vh;
  font-family: var(--font-ui);
  font-size: 13px;
  background: var(--bg-base);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

/* ── HEADER ──────────────────────────────────────── */
header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 18px;
  height: 50px;
  background: var(--header-bg);
  border-bottom: 1px solid rgba(0, 0, 0, 0.2);
  flex-shrink: 0;
  position: relative;
}

header::after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 2px;
  background: linear-gradient(90deg, var(--accent) 0%, transparent 60%);
}

.header-icon {
  width: 24px;
  height: 24px;
  flex-shrink: 0;
  color: var(--accent-lt);
  opacity: 0.9;
}

.header-title {
  font-family: var(--font-title);
  font-size: 14.5px;
  font-weight: 700;
  letter-spacing: 0.2px;
  color: var(--header-text);
}

.header-badge {
  font-family: var(--font-mono);
  font-size: 9px;
  color: var(--header-sub);
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.12);
  padding: 2px 6px;
  border-radius: 3px;
  letter-spacing: 0.8px;
  text-transform: uppercase;
  margin-left: 2px;
}

#panel-toggle {
  display: none;
  margin-left: auto;
  background: none;
  border: 1px solid rgba(255, 255, 255, 0.15);
  color: var(--header-sub);
  font-size: 14px;
  cursor: pointer;
  padding: 5px 10px;
  border-radius: var(--radius-sm);
  line-height: 1.4;
  transition:
    border-color 0.18s,
    color 0.18s;
}

#panel-toggle:hover {
  border-color: rgba(255, 255, 255, 0.4);
  color: var(--header-text);
}

/* ── LAYOUT ───────────────────────────────────────── */
#main {
  display: flex;
  flex: 1;
  overflow: hidden;
  position: relative;
}

#map {
  flex: 3;
  min-width: 0;
}

/* ── PANEL ────────────────────────────────────────── */
#panel {
  flex: 0 0 272px;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 14px 12px;
  background: var(--bg-panel);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 10px;
  scrollbar-width: thin;
  scrollbar-color: rgba(0, 0, 0, 0.12) transparent;
}

#panel::-webkit-scrollbar {
  width: 3px;
}
#panel::-webkit-scrollbar-track {
  background: transparent;
}
#panel::-webkit-scrollbar-thumb {
  background: rgba(0, 0, 0, 0.12);
  border-radius: 2px;
}

/* ── SECTION ──────────────────────────────────────── */
.section {
  background: var(--bg-surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-card);
  padding: 11px 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.section-title {
  font-family: var(--font-mono);
  font-size: 9.5px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 1.1px;
  color: var(--text-3);
  padding-bottom: 7px;
  border-bottom: 1px solid var(--border-soft);
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  user-select: none;
}

.section-title::before {
  content: "";
  display: inline-block;
  width: 10px;
  height: 2px;
  background: var(--accent);
  border-radius: 1px;
  flex-shrink: 0;
}

/* ── REF CARDS ────────────────────────────────────── */
#ref-spots-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.ref-card {
  background: var(--bg-base);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm);
  padding: 9px 9px 8px;
  transition:
    border-color 0.18s,
    box-shadow 0.18s;
  animation: slideIn 0.22s var(--ease) both;
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(-5px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.ref-card:hover {
  border-color: rgba(58, 110, 168, 0.3);
  box-shadow: 0 0 0 3px rgba(58, 110, 168, 0.06);
}

.ref-card-header {
  display: flex;
  align-items: center;
  gap: 7px;
  margin-bottom: 8px;
}

.color-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
  box-shadow:
    0 0 0 2px rgba(255, 255, 255, 0.6),
    0 1px 4px rgba(0, 0, 0, 0.2);
}

.ref-name-input {
  flex: 1;
  font-family: var(--font-ui);
  font-weight: 500;
  font-size: 12.5px;
  border: none;
  border-bottom: 1px solid var(--border);
  padding: 2px 3px;
  background: transparent;
  color: var(--text);
  transition: border-color 0.18s;
}

.ref-name-input:focus {
  outline: none;
  border-bottom-color: var(--accent);
}

.btn-remove {
  background: none;
  border: none;
  color: var(--text-3);
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
  padding: 0 3px;
  transition: color 0.15s;
  flex-shrink: 0;
}

.btn-remove:hover {
  color: var(--red);
}

.ref-row {
  display: flex;
  align-items: center;
  gap: 7px;
}

.ref-row + .ref-row {
  margin-top: 5px;
}

.ref-row label {
  font-family: var(--font-mono);
  font-size: 9px;
  color: var(--text-3);
  text-transform: uppercase;
  letter-spacing: 0.6px;
  white-space: nowrap;
  min-width: 42px;
}

.ref-row input[type="number"] {
  flex: 1;
  min-width: 0;
  width: 0;
  padding: 4px 7px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-family: var(--font-mono);
  font-size: 12px;
  transition:
    border-color 0.18s,
    box-shadow 0.18s;
}

.ref-row input[type="number"]:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(58, 110, 168, 0.12);
}

.ref-loc {
  font-family: var(--font-mono);
  font-size: 9.5px;
  color: var(--text-2);
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.btn-set-map {
  padding: 4px 8px;
  font-size: 9.5px;
  font-family: var(--font-mono);
  background: var(--bg-surface);
  color: var(--text-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  white-space: nowrap;
  letter-spacing: 0.3px;
  transition:
    border-color 0.15s,
    color 0.15s,
    background 0.15s;
}

.btn-set-map:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-bg);
}

.btn-set-map.active {
  background: var(--accent-bg);
  border-color: var(--accent);
  color: var(--accent);
  font-weight: 500;
}

/* ── TOLERANCE ────────────────────────────────────── */
.tol-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.tol-row label {
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--text-2);
  white-space: nowrap;
}

.tol-row input {
  flex: 1;
  min-width: 0;
  width: 0;
  padding: 5px 8px;
  background: var(--bg-base);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-family: var(--font-mono);
  font-size: 12px;
  transition:
    border-color 0.18s,
    box-shadow 0.18s;
}

.tol-row input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(58, 110, 168, 0.12);
}

/* ── BUTTONS ──────────────────────────────────────── */
.btn {
  width: 100%;
  padding: 7px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-family: var(--font-ui);
  font-size: 12px;
  font-weight: 500;
  letter-spacing: 0.2px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition:
    background 0.15s,
    box-shadow 0.15s,
    transform 0.1s;
  border: 1px solid transparent;
}

.btn:active {
  transform: scale(0.98);
}

.btn-success {
  background: var(--green-bg);
  color: var(--green);
  border-color: rgba(31, 140, 90, 0.2);
}
.btn-success:hover {
  background: rgba(31, 140, 90, 0.15);
  border-color: rgba(31, 140, 90, 0.35);
}

.btn-secondary {
  background: var(--bg-base);
  color: var(--text-2);
  border-color: var(--border);
}
.btn-secondary:hover {
  background: var(--bg-hover);
  color: var(--text);
  border-color: rgba(0, 0, 0, 0.18);
}

.btn-danger {
  background: var(--red-bg);
  color: var(--red);
  border-color: rgba(184, 50, 50, 0.2);
}
.btn-danger:hover {
  background: rgba(184, 50, 50, 0.14);
  border-color: rgba(184, 50, 50, 0.35);
}

.btn-add {
  background: transparent;
  color: var(--text-3);
  border: 1px dashed rgba(0, 0, 0, 0.15);
}
.btn-add:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-bg);
}

.btn-warning {
  background: var(--orange-bg);
  color: var(--orange);
  border-color: rgba(184, 96, 32, 0.22);
}
.btn-warning:hover {
  background: rgba(184, 96, 32, 0.17);
  border-color: rgba(184, 96, 32, 0.38);
}

/* ── MEASURE LABEL ────────────────────────────────── */
.measure-label {
  background: rgba(255, 255, 255, 0.94);
  color: var(--text);
  border: 1px solid rgba(0, 0, 0, 0.14);
  padding: 3px 9px;
  border-radius: 10px;
  font-family: var(--font-mono);
  font-size: 10px;
  font-weight: 500;
  white-space: nowrap;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.14);
  transform: translate(-50%, -50%);
  display: inline-block;
  backdrop-filter: blur(4px);
}

.measure-label .del {
  cursor: pointer;
  opacity: 0.4;
  margin-left: 5px;
  transition:
    opacity 0.15s,
    color 0.15s;
}
.measure-label .del:hover {
  opacity: 1;
  color: var(--red);
}

/* ── STATUS BAR ───────────────────────────────────── */
#status-bar {
  padding: 6px 18px 6px 26px;
  font-family: var(--font-mono);
  font-size: 10.5px;
  min-height: 28px;
  background: var(--bg-surface);
  border-top: 1px solid var(--border-soft);
  color: var(--text-3);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  position: relative;
  transition: color 0.22s;
}

#status-bar::before {
  content: "";
  position: absolute;
  left: 11px;
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--text-3);
  transition:
    background 0.22s,
    box-shadow 0.22s;
}

#status-bar.waiting {
  color: var(--orange);
}
#status-bar.waiting::before {
  background: var(--orange);
  box-shadow: 0 0 6px rgba(184, 96, 32, 0.5);
  animation: pulse 1.5s ease-in-out infinite;
}

#status-bar.error {
  color: var(--red);
}
#status-bar.error::before {
  background: var(--red);
  box-shadow: 0 0 6px rgba(184, 50, 50, 0.5);
}

#status-bar.ok {
  color: var(--green);
}
#status-bar.ok::before {
  background: var(--green);
  box-shadow: 0 0 6px rgba(31, 140, 90, 0.5);
}

@keyframes pulse {
  0%,
  100% {
    opacity: 1;
  }
  50% {
    opacity: 0.3;
  }
}

/* ── SPOT ICON ────────────────────────────────────── */
.spot-icon {
  width: 17px;
  height: 17px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-family: var(--font-mono);
  font-weight: 500;
  font-size: 8px;
  border: 2px solid rgba(255, 255, 255, 0.6);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.35);
}

/* ── CIRCLES BLEND ────────────────────────────────── */
.leaflet-overlay-pane {
  mix-blend-mode: multiply;
}

/* ── COLLAPSIBLE SECTIONS ────────────────────────── */
.section-toggle {
  margin-left: auto;
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-3);
  font-size: 13px;
  padding: 0 2px;
  transition: color 0.15s;
  flex-shrink: 0;
  line-height: 1;
}
.section-toggle:hover {
  color: var(--accent);
}
.chevron {
  display: inline-block;
  transition: transform 0.2s var(--ease);
}
.section-collapsed .chevron {
  transform: rotate(-90deg);
}
.section-body {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 800px;
  overflow: hidden;
  transition:
    max-height 0.25s var(--ease),
    opacity 0.2s;
}
.section-collapsed .section-body {
  max-height: 0 !important;
  opacity: 0;
  pointer-events: none;
}

/* ── MY POSITION CARD ─────────────────────────────── */
.ref-card.my-position {
  border-color: rgba(58, 110, 168, 0.25);
  background: rgba(58, 110, 168, 0.04);
}
.my-position-badge {
  font-family: var(--font-mono);
  font-size: 9px;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--accent);
  background: var(--accent-bg);
  border: 1px solid rgba(58, 110, 168, 0.2);
  border-radius: 3px;
  padding: 1px 5px;
  margin-left: auto;
  flex-shrink: 0;
}
@keyframes gpsPulse {
  0%,
  100% {
    box-shadow:
      0 0 0 2px rgba(58, 110, 168, 0.5),
      0 2px 6px rgba(0, 0, 0, 0.35);
  }
  50% {
    box-shadow:
      0 0 0 6px rgba(58, 110, 168, 0.15),
      0 2px 6px rgba(0, 0, 0, 0.35);
  }
}
.spot-icon.gps-icon {
  background: var(--accent) !important;
  animation: gpsPulse 2s ease-in-out infinite;
  font-size: 10px;
}

/* ── TILE SWITCHER ────────────────────────────────── */
.tile-switcher {
  display: flex;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
}
.tile-btn {
  flex: 1;
  padding: 5px 8px;
  font-family: var(--font-mono);
  font-size: 10px;
  font-weight: 500;
  letter-spacing: 0.3px;
  border: none;
  background: var(--bg-base);
  color: var(--text-3);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.tile-btn + .tile-btn { border-left: 1px solid var(--border); }
.tile-btn.active {
  background: var(--accent-bg);
  color: var(--accent);
  font-weight: 700;
}
.tile-btn:not(.active):hover { background: var(--bg-hover); color: var(--text-2); }

/* ── DIRECTION BUTTONS ────────────────────────────── */
.dir-btn {
  aspect-ratio: 1;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-base);
  color: var(--text-2);
  font-size: 9px;
  font-family: var(--font-mono);
  font-weight: 500;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition:
    background 0.15s,
    border-color 0.15s,
    color 0.15s;
  padding: 4px 0;
  letter-spacing: 0.3px;
}
.dir-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-bg);
}
.dir-btn.selected {
  background: var(--accent-bg);
  border-color: var(--accent);
  color: var(--accent);
  font-weight: 700;
}
.dir-btn.center {
  cursor: default;
  color: var(--text-3);
  background: var(--bg-panel);
  border-style: dashed;
  opacity: 0.5;
}

/* ── MOBILE ───────────────────────────────────────── */
@media (max-width: 640px) {
  #panel-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-left: auto;
  }

  #map {
    width: 100%;
    flex: 1;
  }

  #panel {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    flex: none;
    width: min(288px, 90vw);
    transform: translateX(100%);
    transition: transform 0.26s var(--ease);
    box-shadow: -6px 0 24px rgba(0, 0, 0, 0.18);
    z-index: 1001;
    border-left: 1px solid var(--border);
  }

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

  .btn {
    padding: 10px 8px;
    font-size: 11.5px;
  }

  .ref-row input[type="number"],
  .tol-row input {
    padding: 7px 8px;
    font-size: 12px;
  }

  .ref-name-input {
    font-size: 12.5px;
  }
  .btn-remove {
    font-size: 18px;
  }
}
