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

:root {
  --sidebar-width: 290px;
  --header-height: 52px;
  --footer-height: 28px;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --bg: #f8fafc;
  --surface: #ffffff;
  --border: #e2e8f0;
  --text: #1e293b;
  --muted: #64748b;
  --success: #16a34a;
  --danger: #dc2626;
}

body {
  font-family: system-ui, -apple-system, sans-serif;
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ── Header ── */
header {
  height: var(--header-height);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 16px;
  gap: 12px;
  flex-shrink: 0;
  z-index: 1000;
}

.app-title {
  font-weight: 700;
  font-size: 16px;
  letter-spacing: -0.3px;
  flex: 1;
}

.controls {
  display: flex;
  align-items: center;
  gap: 10px;
}

button {
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 6px;
  padding: 5px 12px;
  font-size: 13px;
  transition: background 0.15s, border-color 0.15s;
}
button:hover { background: var(--bg); border-color: #94a3b8; }
button:disabled { opacity: 0.5; cursor: default; }

#fetch-btn {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
  font-weight: 500;
}
#fetch-btn:hover:not(:disabled) { background: var(--accent-hover); border-color: var(--accent-hover); }

.time-filters { display: flex; gap: 4px; }
.filter-btn { padding: 4px 10px; }
.filter-btn.active { background: var(--accent); color: #fff; border-color: var(--accent); }

/* Theme picker */
#theme-controls { position: relative; }
#theme-btn { font-size: 15px; padding: 4px 9px; }
.theme-picker {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.12);
  padding: 6px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 120px;
  z-index: 1100;
}
.theme-option {
  text-align: left;
  border: none;
  background: none;
  padding: 6px 10px;
  border-radius: 5px;
  font-size: 13px;
  color: var(--text);
}
.theme-option:hover { background: var(--bg); }
.theme-option.active { color: var(--accent); font-weight: 600; }

/* Auth indicator */
#auth-indicator { display: flex; align-items: center; gap: 6px; margin-left: 6px; }

/* Sidebar toggle / close — hidden on desktop */
#sidebar-toggle { display: none; }
#sidebar-close   { display: none; }
#sidebar-overlay { display: none; }
.auth-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.auth-dot.authed { background: var(--success); }
.auth-dot.unauthed { background: var(--danger); }
.auth-link { border: none; background: none; color: var(--accent); padding: 0; font-size: 13px; text-decoration: underline; }
.auth-link:hover { background: none; color: var(--accent-hover); }

/* ── Layout ── */
#layout { display: flex; flex: 1; overflow: hidden; }

/* ── Sidebar ── */
#sidebar {
  width: var(--sidebar-width);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  flex-shrink: 0;
}

.sidebar-tabs {
  display: flex;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.tab-btn {
  flex: 1;
  border: none;
  border-radius: 0;
  border-bottom: 2px solid transparent;
  background: none;
  padding: 10px;
  font-size: 13px;
  font-weight: 500;
  color: var(--muted);
}
.tab-btn:hover { background: var(--bg); }
.tab-btn.active { color: var(--accent); border-bottom-color: var(--accent); }

.tab-panel { flex: 1; overflow-y: auto; display: flex; flex-direction: column; }
.hidden { display: none !important; }

/* ── Tag list ── */
#tag-list { list-style: none; padding: 6px 8px; flex: 1; }

.tag-item {
  padding: 9px 8px;
  border-radius: 6px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 3px;
  border: 1px solid transparent;
  transition: background 0.12s, border-color 0.12s;
}
.tag-item:hover { background: var(--bg); }
.tag-item.active { background: #eff6ff; border-color: #bfdbfe; }

.tag-name { font-weight: 600; font-size: 13px; display: flex; align-items: center; gap: 6px; }
.tag-name input {
  font: inherit; font-weight: 600;
  border: 1px solid var(--accent); border-radius: 4px;
  padding: 1px 4px; outline: none; width: 100%;
}
.tag-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; background: var(--accent); }
.tag-meta { font-size: 12px; color: var(--muted); padding-left: 14px; }
.tag-track-toggle { font-size: 11px; color: var(--accent); padding-left: 14px; user-select: none; }
.tag-no-location { font-size: 12px; color: var(--muted); font-style: italic; padding-left: 14px; }

#fetch-status { font-size: 12px; color: var(--muted); padding: 6px 16px 12px; min-height: 24px; }

/* ── Keys panel ── */
.keys-toolbar { padding: 10px 12px 6px; flex-shrink: 0; }
#gen-key-btn {
  width: 100%;
  background: var(--accent); color: #fff; border-color: var(--accent);
  font-weight: 500;
}
#gen-key-btn:hover { background: var(--accent-hover); border-color: var(--accent-hover); }

#key-list { list-style: none; padding: 0 8px; flex: 1; }
.key-item {
  padding: 8px;
  border-radius: 6px;
  border: 1px solid var(--border);
  margin-bottom: 6px;
  font-size: 12px;
}
.key-item-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 4px; }
.key-id { font-weight: 700; font-family: ui-monospace, monospace; font-size: 13px; }
.key-delete { border: none; background: none; color: var(--danger); padding: 0 2px; font-size: 16px; line-height: 1; }
.key-delete:hover { background: none; color: #9b1c1c; }
.key-field { color: var(--muted); margin-bottom: 2px; }
.key-field span { color: var(--text); font-family: ui-monospace, monospace; font-size: 11px; word-break: break-all; }
.keys-hint { font-size: 11px; color: var(--muted); padding: 8px 16px 12px; font-style: italic; }

/* ── Map ── */
#map { flex: 1; min-width: 0; }

/* ── Auth modal ── */
.modal {
  position: fixed; inset: 0; z-index: 2000;
  background: rgba(0,0,0,0.4);
  display: flex; align-items: center; justify-content: center;
}
.modal-box {
  background: var(--surface);
  border-radius: 12px;
  padding: 28px;
  width: 360px;
  max-width: 95vw;
  box-shadow: 0 20px 60px rgba(0,0,0,0.2);
}
.modal-box h2 { font-size: 18px; margin-bottom: 20px; }

.modal-box label {
  display: flex; flex-direction: column; gap: 4px;
  font-size: 12px; font-weight: 600; color: var(--muted);
  text-transform: uppercase; letter-spacing: 0.5px;
  margin-bottom: 14px;
}
.modal-box input[type="email"],
.modal-box input[type="password"],
.modal-box input[type="text"] {
  border: 1px solid var(--border); border-radius: 6px;
  padding: 8px 10px; font-size: 14px; outline: none;
  color: var(--text); background: var(--surface);
}
.modal-box input:focus { border-color: var(--accent); }

.method-row {
  display: flex; gap: 14px; align-items: center;
  font-size: 13px; color: var(--text); margin-bottom: 14px;
}
.method-row span { color: var(--muted); font-size: 12px; font-weight: 600; text-transform: uppercase; }
.method-row label { display: flex; flex-direction: row; gap: 4px; align-items: center; font-size: 13px; color: var(--text); margin: 0; text-transform: none; letter-spacing: 0; font-weight: 400; }

.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 4px; }
.modal-actions button { min-width: 80px; }
.modal-cancel { background: var(--bg); }

.form-error { font-size: 12px; color: var(--danger); margin-bottom: 10px; }

.success-msg { color: var(--success); font-size: 15px; margin-bottom: 16px; }

/* ── Footer ── */
#app-footer {
  height: 28px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  font-size: 11px;
  color: var(--muted);
  flex-shrink: 0;
}

/* ── Leaflet popup tweaks ── */
.leaflet-popup-content {
  font-family: system-ui, -apple-system, sans-serif;
  font-size: 13px; line-height: 1.6; min-width: 160px;
}
.popup-name { font-weight: 700; font-size: 14px; margin-bottom: 4px; }
.popup-row { color: #475569; }
.popup-row span { color: var(--text); font-weight: 500; }

/* ── Mobile (≤ 640 px) ── */
@media (max-width: 640px) {

  /* Two-row header -------------------------------------------------- */
  header {
    height: auto;
    flex-wrap: wrap;
    padding: 8px 12px;
    gap: 6px 8px;
  }

  #sidebar-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 6px 10px;
    font-size: 18px;
    min-height: 40px;
    flex-shrink: 0;
  }

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

  /* Controls (fetch + filters + theme + auth) wrap onto row 2 */
  .controls {
    order: 10;
    width: 100%;
    gap: 6px;
  }

  /* Hide "Fetch" label — keep just the ⟳ icon */
  .btn-label { display: none; }

  /* Row-2 button sizing */
  #fetch-btn    { padding: 6px 12px; }
  .filter-btn   { padding: 6px 9px; font-size: 12px; }
  #theme-btn    { padding: 6px 10px; }

  /* Ensure all header buttons are tall enough to tap comfortably */
  header button { min-height: 38px; }

  /* Sidebar drawer -------------------------------------------------- */
  #sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: calc(-1 * var(--sidebar-width));
    width: var(--sidebar-width);
    max-width: calc(100vw - 56px);
    z-index: 1500;
    transition: left 0.25s ease;
    box-shadow: 4px 0 28px rgba(0, 0, 0, 0.25);
  }

  #sidebar.open { left: 0; }

  #sidebar-close {
    display: flex;
    align-items: center;
    justify-content: center;
    border: none;
    background: none;
    color: var(--muted);
    font-size: 16px;
    padding: 0 10px;
    margin-left: auto;
    min-height: 40px;
    min-width: 40px;
    flex-shrink: 0;
  }
  #sidebar-close:hover { background: var(--bg); color: var(--text); }

  #sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 1400;
    background: rgba(0, 0, 0, 0.4);
    cursor: default;
  }
  #sidebar-overlay.visible { display: block; }

  /* Tag items — bigger tap target */
  .tag-item  { padding: 12px 8px; }
  .tab-btn   { padding: 12px 10px; }

  /* Map fills full width -------------------------------------------- */
  #map { width: 100%; }

  /* Footer ---------------------------------------------------------- */
  #app-footer { font-size: 10px; padding: 0 12px; }

  /* Auth modal — bottom sheet --------------------------------------- */
  .modal { align-items: flex-end; }

  .modal-box {
    width: 100%;
    max-width: 100%;
    border-radius: 16px 16px 0 0;
    padding: 20px 20px 32px;
  }

  /* Prevent iOS Safari from zooming in on input focus */
  .modal-box input[type="email"],
  .modal-box input[type="password"],
  .modal-box input[type="text"] {
    font-size: 16px;
    padding: 10px 12px;
  }

  .modal-actions button { min-height: 44px; flex: 1; }
}
