/* ===== Base ===== */
:root {
  --primary: #0d6efd;
  --primary-dark: #0a58ca;
  --bg: #f4f6fa;
  --surface: #ffffff;
  --surface-2: #f8f9fc;
  --border: #e3e6ee;
  --text: #1e2a3a;
  --text-muted: #5b6b80;
  --success: #198754;
  --danger: #dc3545;
  --warning: #ffc107;
  --radius: 8px;
  --radius-lg: 12px;
  --shadow-sm: 0 1px 2px rgba(20,30,50,.06);
  --shadow: 0 4px 12px rgba(20,30,50,.08);
  --shadow-lg: 0 12px 32px rgba(20,30,50,.18);
  --topbar-h: 64px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: 'Heebo', 'Assistant', 'Segoe UI', Tahoma, Arial, sans-serif;
  font-size: 15px;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

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

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ===== Auth gate ===== */
/* [hidden] must win against the .auth-gate display rule */
[hidden] { display: none !important; }

.auth-gate {
  position: fixed; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, #0d6efd 0%, #6f42c1 100%);
  z-index: 9999;
}

.auth-card {
  background: var(--surface);
  padding: 36px 32px;
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  width: min(380px, 92vw);
  text-align: center;
}

.auth-icon { font-size: 48px; margin-bottom: 8px; }
.auth-card h1 { margin: 0 0 6px; font-size: 20px; }
.auth-sub { color: var(--text-muted); margin: 0 0 20px; font-size: 14px; }
.auth-card input {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 12px;
  font-size: 16px;
}
.auth-card input:focus { outline: 2px solid var(--primary); border-color: var(--primary); }
.auth-card button {
  width: 100%;
  padding: 12px;
  background: var(--primary);
  color: white;
  border: none;
  border-radius: var(--radius);
  font-size: 16px;
  font-weight: 600;
}
.auth-card button:hover { background: var(--primary-dark); }
.auth-error { color: var(--danger); margin-top: 12px; font-size: 14px; }

/* ===== Layout ===== */
.app { display: flex; flex-direction: column; height: 100vh; }

.topbar {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 16px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
  z-index: 100;
  height: var(--topbar-h);
}

.topbar-right { display: flex; align-items: center; gap: 12px; }
.brand { margin: 0; font-size: 17px; font-weight: 700; color: var(--text); white-space: nowrap; }
.icon-btn {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  width: 38px; height: 38px;
  font-size: 18px;
}

.topbar-center { display: flex; justify-content: center; }
#search-input {
  width: min(560px, 100%);
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-2);
}
#search-input:focus { outline: 2px solid var(--primary); border-color: var(--primary); background: var(--surface); }

.tabs { display: flex; gap: 4px; }
.tab {
  background: transparent;
  border: 1px solid transparent;
  padding: 8px 14px;
  border-radius: var(--radius);
  color: var(--text-muted);
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
}
.tab:hover { background: var(--surface-2); color: var(--text); }
.tab.active { background: var(--primary); color: white; border-color: var(--primary); }

.main {
  flex: 1;
  display: flex;
  overflow: hidden;
}

/* ===== Sidebar (filters) ===== */
.sidebar {
  width: 280px;
  background: var(--surface);
  border-left: 1px solid var(--border);
  overflow-y: auto;
  padding: 16px;
  flex-shrink: 0;
  transition: margin .2s ease;
}

.sidebar.collapsed { margin-right: -280px; }

.filter-group { margin-bottom: 18px; }
.filter-group h3 {
  margin: 0 0 8px;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: .03em;
  color: var(--text-muted);
}

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips.scrollable { max-height: 200px; overflow-y: auto; padding-bottom: 2px; }

.chip {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 5px 10px;
  border-radius: 999px;
  font-size: 13px;
  cursor: pointer;
  transition: all .15s ease;
  user-select: none;
}
.chip:hover { background: #eef2fa; border-color: var(--primary); }
.chip.active {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

.filter-actions {
  display: flex; justify-content: space-between; align-items: center;
  padding-top: 12px; border-top: 1px solid var(--border);
  margin-top: 12px;
}
.link-btn {
  background: none; border: none; color: var(--primary);
  text-decoration: underline; cursor: pointer; padding: 0; font-size: 14px;
}
.result-count { color: var(--text-muted); font-size: 13px; font-weight: 500; }

/* ===== Content area ===== */
.content { flex: 1; position: relative; overflow: hidden; }
.view { display: none; height: 100%; overflow: auto; }
.view.active { display: block; }

#view-map { overflow: hidden; }
#map { width: 100%; height: 100%; }

/* ===== List view ===== */
.list-container {
  padding: 16px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 14px;
  align-content: start;
}

.clinic-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 14px 16px;
  box-shadow: var(--shadow-sm);
  display: flex; flex-direction: column; gap: 8px;
  transition: transform .15s ease, box-shadow .15s ease;
  cursor: pointer;
}
.clinic-card:hover { transform: translateY(-1px); box-shadow: var(--shadow); }

.clinic-card h3 {
  margin: 0; font-size: 16px;
  display: flex; align-items: center; gap: 8px;
}
.type-badge {
  display: inline-block; font-size: 11px; font-weight: 600;
  padding: 2px 8px; border-radius: 999px; flex-shrink: 0;
}
.type-hospital { background: #dbeafe; color: #1d4ed8; }
.type-kupat-holim { background: #d1fae5; color: #065f46; }
.type-private { background: #fef3c7; color: #92400e; }
.type-other { background: #e5e7eb; color: #374151; }

.clinic-meta {
  display: flex; flex-wrap: wrap; gap: 6px 12px;
  font-size: 13px; color: var(--text-muted);
}
.clinic-meta span { display: inline-flex; align-items: center; gap: 4px; }

.clinic-treatments {
  display: flex; flex-wrap: wrap; gap: 4px;
}
.treat-chip {
  background: var(--surface-2); border: 1px solid var(--border);
  padding: 2px 8px; border-radius: 999px; font-size: 12px;
  color: var(--text);
}

.clinic-card .doctors {
  font-size: 13px; color: var(--text);
  border-top: 1px dashed var(--border); padding-top: 6px;
}

/* ===== Add form ===== */
.form-wrap, .settings-wrap {
  max-width: 840px; margin: 0 auto; padding: 24px 20px;
}
.form-wrap h2, .settings-wrap h2 { margin-top: 0; }

.clinic-form { display: flex; flex-direction: column; gap: 14px; background: var(--surface); padding: 20px; border-radius: var(--radius-lg); border: 1px solid var(--border); }
.clinic-form label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--text-muted); font-weight: 500; }
.clinic-form label small { font-weight: 400; }
.clinic-form input, .clinic-form select, .clinic-form textarea {
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font-size: 14px;
}
.clinic-form input:focus, .clinic-form select:focus, .clinic-form textarea:focus {
  outline: 2px solid var(--primary); border-color: var(--primary);
}

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 12px; }
.row { display: flex; gap: 8px; }
.row > input { flex: 1; }

.form-actions { display: flex; gap: 8px; justify-content: flex-start; padding-top: 8px; }
.hint { color: var(--text-muted); font-size: 12px; margin: 0; }

button.primary {
  background: var(--primary); color: white; border: none;
  padding: 10px 18px; border-radius: var(--radius); font-weight: 600;
}
button.primary:hover { background: var(--primary-dark); }
button.secondary {
  background: var(--surface-2); color: var(--text); border: 1px solid var(--border);
  padding: 10px 14px; border-radius: var(--radius); font-weight: 500;
}
button.secondary:hover { background: #eef2fa; }
button.danger {
  background: var(--danger); color: white; border: none;
  padding: 10px 14px; border-radius: var(--radius); font-weight: 500;
}
button.danger:hover { background: #b02a37; }

/* ===== Settings ===== */
.settings-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px;
  margin-bottom: 16px;
}
.settings-card h3 { margin-top: 0; }
.settings-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.file-btn {
  display: inline-block;
  background: var(--surface-2);
  border: 1px solid var(--border);
  padding: 10px 14px;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
}
.file-btn:hover { background: #eef2fa; }
.schema {
  background: var(--surface-2); border: 1px solid var(--border);
  padding: 12px; border-radius: var(--radius);
  font-size: 13px; overflow-x: auto;
}
.stats-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 12px;
}
.stat-card {
  background: var(--surface-2); padding: 12px 14px;
  border-radius: var(--radius); border: 1px solid var(--border);
}
.stat-card .num { font-size: 24px; font-weight: 700; color: var(--primary); }
.stat-card .lbl { font-size: 12px; color: var(--text-muted); }

/* ===== Modal ===== */
.modal {
  position: fixed; inset: 0;
  display: flex; align-items: center; justify-content: center;
  z-index: 1000;
}
.modal-backdrop { position: absolute; inset: 0; background: rgba(20,30,50,.55); }
.modal-card {
  position: relative;
  background: var(--surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  width: min(560px, 92vw);
  max-height: 86vh;
  overflow-y: auto;
  padding: 22px 24px;
}
.modal-card h2 { margin-top: 0; padding-left: 32px; }
.modal-close {
  position: absolute; top: 12px; left: 12px;
  background: var(--surface-2); border: 1px solid var(--border);
  width: 28px; height: 28px; border-radius: 50%; cursor: pointer;
}
.modal-section { margin-top: 14px; }
.modal-section h4 { margin: 0 0 6px; font-size: 13px; color: var(--text-muted); text-transform: uppercase; letter-spacing: .02em; }
.modal-section p, .modal-section ul { margin: 0; }
.modal-section ul { padding-right: 18px; }
.modal-actions { display: flex; gap: 8px; margin-top: 18px; padding-top: 14px; border-top: 1px solid var(--border); }

/* ===== Toast ===== */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: white;
  padding: 10px 18px; border-radius: 999px;
  box-shadow: var(--shadow);
  z-index: 2000;
  font-size: 14px;
}
.toast.success { background: var(--success); }
.toast.error { background: var(--danger); }

/* ===== Leaflet popup styling ===== */
.leaflet-popup-content { font-family: inherit; min-width: 220px; }
.leaflet-popup-content h4 { margin: 0 0 6px; font-size: 15px; }
.leaflet-popup-content p { margin: 2px 0; font-size: 13px; }
.leaflet-popup-content .popup-actions { margin-top: 8px; display: flex; gap: 6px; }
.leaflet-popup-content button {
  background: var(--primary); color: white; border: none;
  padding: 5px 10px; border-radius: var(--radius); font-size: 12px;
}

/* ===== Mobile ===== */
@media (max-width: 880px) {
  .topbar {
    grid-template-columns: 1fr auto;
    grid-template-rows: auto auto auto;
    gap: 8px;
    height: auto;
    padding: 10px 12px;
  }
  .topbar-right { grid-column: 1 / -1; }
  .topbar-center { grid-column: 1 / -1; }
  .tabs {
    grid-column: 1 / -1;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  .brand { font-size: 14px; }
  .tab { font-size: 13px; padding: 6px 10px; }

  .sidebar {
    position: absolute; inset: 0 0 0 auto;
    width: min(320px, 88vw);
    z-index: 50;
    box-shadow: var(--shadow-lg);
    transform: translateX(100%);
    transition: transform .25s ease;
    margin: 0;
  }
  .sidebar.open { transform: translateX(0); }

  .grid-2, .grid-3 { grid-template-columns: 1fr; }
}
