:root {
  --red: #d7263d;
  --ink: #1c1f24;
  --muted: #6b7280;
  --border: #e5e7eb;
  --bg: #f7f8fa;
  --panel: #ffffff;
  --radius: 10px;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

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

a { color: var(--red); text-decoration: none; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}

.brand { font-size: 18px; font-weight: 700; white-space: nowrap; }
.brand-link { color: var(--ink); }

.topbar-actions { display: flex; gap: 10px; }

.topbar-menu-toggle {
  display: none;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
}
.topbar-menu-toggle:hover { opacity: 0.85; }

@media (max-width: 640px) {
  .topbar { position: relative; padding: 10px 14px; }
  .brand { font-size: 16px; }

  .topbar-menu-toggle { display: inline-flex; }

  .topbar-actions {
    display: none;
    position: absolute;
    top: calc(100% + 6px);
    right: 14px;
    flex-direction: column;
    align-items: stretch;
    gap: 6px;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: 0 6px 20px rgba(0,0,0,0.15);
    padding: 10px;
    z-index: 1500;
    min-width: 190px;
  }
  .topbar-actions.open { display: flex; }
  .topbar-actions .btn {
    padding: 9px 12px;
    font-size: 14px;
    text-align: center;
    white-space: nowrap;
  }
}

.btn {
  border: none;
  border-radius: 8px;
  padding: 9px 16px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.15s ease;
  display: inline-block;
}
.btn:hover { opacity: 0.85; }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn-primary { background: var(--red); color: #fff; }
.btn-secondary { background: var(--ink); color: #fff; }
.btn-ghost { background: transparent; color: var(--ink); border: 1px solid var(--border); }
.btn-block { width: 100%; }

.layout {
  display: grid;
  grid-template-columns: 1fr 380px;
  height: calc(100vh - 57px);
  transition: grid-template-columns 0.2s ease;
}
.layout.sidebar-closed { grid-template-columns: 1fr; }
.layout.sidebar-closed .side-panel { display: none; }

.map-panel { position: relative; }
#map { width: 100%; height: 100%; }

.draw-toolbar {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--panel);
  border-radius: var(--radius);
  box-shadow: 0 4px 16px rgba(0,0,0,0.15);
  padding: 10px 14px;
  display: flex;
  align-items: center;
  gap: 10px;
  z-index: 1000;
  flex-wrap: wrap;
  max-width: 90%;
}
.draw-hint { font-size: 13px; color: var(--muted); }
.draw-toolbar-buttons { display: flex; align-items: center; gap: 10px; }

.map-overlay-btn {
  position: absolute;
  top: 12px;
  right: 12px;
  z-index: 1000;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0,0,0,0.12);
}
.map-overlay-btn:hover { opacity: 0.85; }
.map-overlay-btn.active {
  background: #eaf5ff;
  border-color: #7cc2f2;
  color: #0a5f9c;
}
.map-overlay-btn:disabled { opacity: 0.6; cursor: default; }

.map-locate-btn {
  display: none;
  top: 58px;
  width: 38px;
  height: 38px;
  padding: 0;
  font-size: 17px;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
}
.map-locate-btn.locating { opacity: 0.6; cursor: default; }

.fountain-icon {
  font-size: 18px;
  line-height: 1;
  text-align: center;
  filter: drop-shadow(0 1px 1px rgba(0,0,0,0.5));
}

.locate-me-dot {
  display: block;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #1a73e8;
  border: 3px solid #fff;
  box-shadow: 0 0 0 2px rgba(26,115,232,0.5), 0 1px 4px rgba(0,0,0,0.4);
}

@media (max-width: 640px) {
  .map-locate-btn { display: flex; }
  .draw-toolbar {
    bottom: 10px;
    left: 8px;
    right: 8px;
    transform: none;
    max-width: none;
    width: auto;
    padding: 6px 8px;
    gap: 4px;
    flex-wrap: wrap;
    justify-content: flex-start;
  }
  .draw-toolbar-buttons {
    display: flex;
    gap: 6px;
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  .draw-hint {
    order: -1;
    width: 100%;
    font-size: 11px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .draw-toolbar .btn {
    padding: 7px 10px;
    font-size: 12px;
    flex: 0 0 auto;
    white-space: nowrap;
  }
}

.side-panel {
  position: relative;
  background: var(--panel);
  border-left: 1px solid var(--border);
  padding: 20px;
  overflow-y: auto;
}

.side-panel-close {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  color: var(--muted);
  font-size: 15px;
  line-height: 1;
  cursor: pointer;
  z-index: 5;
}
.side-panel-close:hover { color: var(--ink); border-color: var(--ink); }

.side-panel h2 { margin-top: 0; font-size: 20px; padding-right: 38px; }
.side-panel h3 { font-size: 15px; margin-bottom: 8px; }
.muted { color: var(--muted); font-size: 13px; }

.route-description {
  font-size: 14px;
  background: var(--bg);
  border-radius: 8px;
  padding: 10px 12px;
}

.stat-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin: 16px 0;
}
.stat {
  flex: 1 1 calc(50% - 10px);
  min-width: 90px;
  background: var(--bg);
  border-radius: 8px;
  padding: 10px;
  text-align: center;
}
.stat-value { display: block; font-size: 17px; font-weight: 700; }
.stat-label { display: block; font-size: 11px; color: var(--muted); margin-top: 2px; }

.pace-control {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: -6px 0 16px;
  font-size: 12px;
  color: var(--muted);
}
.pace-control label { flex: 0 0 auto; }
.pace-control input {
  width: 64px;
  padding: 5px 7px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
  font-family: inherit;
}

.strava-segment-link {
  margin: 0 0 16px;
}
.strava-segment-link a {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: #fc4c02;
  color: #fff;
  font-weight: 600;
  font-size: 13px;
  padding: 8px 14px;
  border-radius: 8px;
}
.strava-segment-link a:hover { opacity: 0.88; }

#elevation-chart-wrap, #draw-elevation-chart-wrap { margin: 16px 0; position: relative; height: 180px; }

.routes-list { display: flex; flex-direction: column; gap: 10px; margin-top: 14px; }
.route-card {
  display: block;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  color: var(--ink);
}
.route-card:hover { border-color: var(--red); }
.route-card-name { font-weight: 700; font-size: 14px; }
.route-card-meta { font-size: 12px; color: var(--muted); margin-top: 4px; }

#save-form label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 12px;
}
#save-form input, #save-form textarea {
  width: 100%;
  margin-top: 4px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 14px;
  font-family: inherit;
}

.save-form-actions {
  display: flex;
  gap: 10px;
}
.save-form-actions .btn { flex: 1; }

.chat-panel {
  border-top: 1px solid var(--border);
  margin-top: 20px;
  padding-top: 16px;
}

.route-delete-link {
  margin-top: 24px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  text-align: center;
}
.btn-link-danger {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 12px;
  cursor: pointer;
  text-decoration: underline;
  padding: 4px;
}
.btn-link-danger:hover { color: var(--red); }
.btn-link-danger:disabled { opacity: 0.5; cursor: default; }
.chat-messages {
  max-height: 260px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 12px;
}
.chat-message {
  background: var(--bg);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 13px;
}
.chat-author { font-weight: 700; }
.chat-time { color: var(--muted); font-size: 11px; margin-left: 6px; }
.chat-text { margin-top: 2px; white-space: pre-wrap; word-break: break-word; }

.chat-form { display: flex; flex-direction: column; gap: 8px; }
.chat-author-input {
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
}
.chat-input-row { display: flex; gap: 8px; }
.chat-input-row input {
  flex: 1;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 14px;
}

.notfound {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
  text-align: center;
}

@media (max-width: 860px) {
  .layout { grid-template-columns: 1fr; grid-template-rows: 55vh 1fr; height: auto; }
  .layout.sidebar-closed { grid-template-rows: 1fr; height: calc(100vh - 57px); }
  .side-panel { border-left: none; border-top: 1px solid var(--border); }
}

.btn-danger { background: var(--red); color: #fff; }

.footer-admin-link {
  text-align: center;
  padding: 10px;
  font-size: 12px;
}
.footer-admin-link a { color: var(--muted); }

.admin-login-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background: var(--bg);
}
.admin-login-form {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px;
  width: 320px;
  text-align: center;
}
.admin-login-form h1 { margin: 0 0 4px; font-size: 20px; }
.admin-login-form label {
  display: block;
  text-align: left;
  font-size: 13px;
  font-weight: 600;
  margin: 16px 0 4px;
}
.admin-login-form input {
  width: 100%;
  padding: 9px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 14px;
}
.admin-login-form .btn { margin-top: 16px; }
.admin-error { color: var(--red); font-size: 13px; margin: 10px 0 0; }

.admin-main { max-width: 1240px; margin: 24px auto; padding: 0 20px; }
.admin-table-wrap {
  overflow-x: auto;
  scrollbar-width: thin;
}
.admin-table-wrap::-webkit-scrollbar { height: 8px; }
.admin-table-wrap::-webkit-scrollbar-track { background: transparent; }
.admin-table-wrap::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
.admin-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.admin-table th, .admin-table td {
  text-align: left;
  padding: 9px 10px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  white-space: nowrap;
}
.admin-table th { background: var(--bg); font-weight: 700; }
.admin-table tr:last-child td { border-bottom: none; }
.admin-table .btn { padding: 6px 10px; font-size: 12px; }
.admin-table-name {
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.km-tooltip {
  background: var(--ink);
  color: #fff;
  border: none;
  border-radius: 4px;
  padding: 1px 6px;
  font-size: 11px;
  font-weight: 700;
  box-shadow: none;
}
.km-tooltip::before { border-top-color: var(--ink); }

.draw-point-marker {
  width: 16px;
  height: 16px;
  background: #fff;
  border: 3px solid var(--red);
  border-radius: 50%;
  box-shadow: 0 1px 3px rgba(0,0,0,0.4);
  cursor: grab;
}
.draw-point-marker:active { cursor: grabbing; }
