:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --border: #e3e6ea;
  --text: #1c232b;
  --text-muted: #6b7380;
  --primary: #3457d5;
  --primary-hover: #2a46b0;
  --danger: #c23b3b;
  --badge-bg: #e8edff;
  --badge-text: #3457d5;
  --success-bg: #e4f7ea;
  --success-text: #1e7b45;
  --error-bg: #fdeaea;
  --error-text: #b23b3b;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(20, 20, 30, 0.06), 0 2px 10px rgba(20, 20, 30, 0.04);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14171c;
    --surface: #1c2128;
    --border: #2c323b;
    --text: #e7e9ec;
    --text-muted: #9aa2ad;
    --primary: #6b8cff;
    --primary-hover: #8aa3ff;
    --badge-bg: #26305a;
    --badge-text: #aebcff;
    --success-bg: #163726;
    --success-text: #6fd99a;
    --error-bg: #3a1e1e;
    --error-text: #e39a9a;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 2px 10px rgba(0, 0, 0, 0.2);
  }
}

:root[data-theme="dark"] {
  --bg: #14171c;
  --surface: #1c2128;
  --border: #2c323b;
  --text: #e7e9ec;
  --text-muted: #9aa2ad;
  --primary: #6b8cff;
  --primary-hover: #8aa3ff;
  --badge-bg: #26305a;
  --badge-text: #aebcff;
  --success-bg: #163726;
  --success-text: #6fd99a;
  --error-bg: #3a1e1e;
  --error-text: #e39a9a;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 2px 10px rgba(0, 0, 0, 0.2);
}

* { box-sizing: border-box; }

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

.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
}

.topbar-inner {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
}

.brand {
  font-weight: 700;
  font-size: 16px;
  color: var(--text);
  text-decoration: none;
  display: flex;
  align-items: center;
  gap: 8px;
}

.brand-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--primary);
  display: inline-block;
}

.topnav { display: flex; gap: 6px; }

.topnav a {
  color: var(--text-muted);
  text-decoration: none;
  padding: 8px 14px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
}

.topnav a.active { background: var(--badge-bg); color: var(--primary); }
.topnav a:hover { color: var(--text); }

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 24px 20px 60px;
}

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

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.stats-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
  margin-bottom: 18px;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
  text-decoration: none;
  color: var(--text);
}

.stat-link:hover { border-color: var(--primary); }

.stat-value { font-size: 26px; font-weight: 700; }
.stat-label { font-size: 13px; color: var(--text-muted); margin-top: 2px; }

.toolbar {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 16px;
  margin-bottom: 18px;
}

.toolbar-search {
  display: flex;
  gap: 8px;
  flex: 1;
  min-width: 260px;
}

.toolbar-search input[type="search"] {
  flex: 1;
  min-width: 160px;
}

.toolbar-actions { display: flex; gap: 8px; flex-wrap: wrap; }

input, select {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 9px 12px;
  font-size: 14px;
}

input:focus, select:focus {
  outline: 2px solid var(--primary);
  outline-offset: -1px;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 9px 14px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
}

.btn:hover { border-color: var(--primary); }

.btn-primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

.btn-primary:hover { background: var(--primary-hover); }

.btn-ghost { background: transparent; }

.file-label { position: relative; overflow: hidden; }
.file-label input[type="file"] {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

.import-form { display: inline-block; }

.btn-icon {
  background: transparent;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 15px;
  padding: 4px 6px;
  border-radius: 6px;
}

.btn-icon:hover { background: var(--badge-bg); color: var(--primary); }
.btn-icon-danger:hover { background: var(--error-bg); color: var(--error-text); }

.inline-form { display: inline; }

.table-card { overflow: hidden; }
.table-scroll { overflow-x: auto; }

.member-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

.member-table thead th {
  text-align: left;
  padding: 12px 14px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  white-space: nowrap;
}

.sort-link { color: var(--text-muted); text-decoration: none; font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: 0.03em; }
.sort-link:hover { color: var(--text); }
.sort-arrow { color: var(--primary); }

.member-table td {
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

.member-row:hover { background: var(--bg); }

.person-name { font-weight: 600; }
.muted { color: var(--text-muted); }

.badge {
  display: inline-block;
  background: var(--badge-bg);
  color: var(--badge-text);
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
}

.contact-cell { font-size: 13px; color: var(--text-muted); }
.contact-cell a { color: var(--primary); text-decoration: none; }

.actions-cell { white-space: nowrap; }

.detail-row.hidden { display: none; }

.detail-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 10px 20px;
  padding: 10px 6px 16px;
}

.detail-item { font-size: 13px; }
.detail-label {
  display: block;
  color: var(--text-muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  margin-bottom: 2px;
}

.empty-state {
  text-align: center;
  color: var(--text-muted);
  padding: 40px 20px !important;
}

.flash-wrap { margin-bottom: 16px; display: flex; flex-direction: column; gap: 8px; }
.flash { padding: 10px 14px; border-radius: 8px; font-size: 14px; }
.flash-success { background: var(--success-bg); color: var(--success-text); }
.flash-error { background: var(--error-bg); color: var(--error-text); }

.form-card { padding: 24px; }
.form-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; }
.form-header h1 { font-size: 20px; margin: 0; }

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 14px;
}

.form-field { display: flex; flex-direction: column; gap: 6px; }
.form-field label { font-size: 12px; color: var(--text-muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.03em; }
.form-field input { width: 100%; }

.form-actions { margin-top: 24px; display: flex; gap: 10px; }

.retry-form { margin-bottom: 16px; }

.hidden-raw { display: none; }

.row-invalid { background: var(--error-bg); }
.row-invalid:hover { background: var(--error-bg); }

.warnings-cell { font-size: 12px; max-width: 260px; }
.warning-text { color: var(--error-text); margin-bottom: 4px; }
.raw-line {
  display: block;
  margin-top: 4px;
  font-size: 11px;
  color: var(--text-muted);
  word-break: break-all;
  white-space: pre-wrap;
}

.map-card { padding: 0; overflow: hidden; }
#map { height: 65vh; min-height: 420px; width: 100%; }

@media (max-width: 640px) {
  .topbar-inner { padding: 12px 14px; }
  .brand span:not(.brand-dot) { display: none; }
  .container { padding: 16px 12px 40px; }
  .toolbar { flex-direction: column; align-items: stretch; }
  .toolbar-search { flex-direction: column; }
}
