:root {
  --bg: #ffffff;
  --bg-elevated: #f6f6f7;
  --fg: #171717;
  --fg-muted: #5c5c5f;
  --border: #dcdcde;
  --accent: #b3101f;
  --accent-fg: #ffffff;
  --error-bg: #fdeceb;
  --error-fg: #7a1810;
  --success-bg: #f2f2f3;
  --success-fg: #171717;
  --focus-ring: #b3101f;
  --radius: 0.5rem;
  --shadow: 0 1px 3px rgba(0, 0, 0, .12), 0 6px 16px rgba(0, 0, 0, .08);
  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-5: 1.5rem;
  --space-6: 2.5rem;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d0d0e;
    --bg-elevated: #18181a;
    --fg: #f2f0ef;
    --fg-muted: #a8a6a5;
    --border: #302f31;
    --accent: #ff5b5b;
    --accent-fg: #1a0a0a;
    --error-bg: #3a1613;
    --error-fg: #ff9a90;
    --success-bg: #1c1c1e;
    --success-fg: #f2f0ef;
    --focus-ring: #ff5b5b;
    --shadow: none;
  }
}

:root[data-theme="dark"] {
  --bg: #0d0d0e;
  --bg-elevated: #18181a;
  --fg: #f2f0ef;
  --fg-muted: #a8a6a5;
  --border: #302f31;
  --accent: #ff5b5b;
  --accent-fg: #1a0a0a;
  --error-bg: #3a1613;
  --error-fg: #ff9a90;
  --success-bg: #1c1c1e;
  --success-fg: #f2f0ef;
  --focus-ring: #ff5b5b;
  --shadow: none;
}

:root[data-theme="light"] {
  --bg: #ffffff;
  --bg-elevated: #f6f6f7;
  --fg: #171717;
  --fg-muted: #5c5c5f;
  --border: #dcdcde;
  --accent: #b3101f;
  --accent-fg: #ffffff;
  --error-bg: #fdeceb;
  --error-fg: #7a1810;
  --success-bg: #f2f2f3;
  --success-fg: #171717;
  --focus-ring: #b3101f;
  --shadow: 0 1px 3px rgba(0, 0, 0, .12), 0 6px 16px rgba(0, 0, 0, .08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  line-height: 1.5;
}

h1 {
  font-size: 1.75rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  margin: 0 0 var(--space-4);
}

h2 {
  font-size: 1.25rem;
  font-weight: 600;
  margin: 0 0 var(--space-3);
}

.skip-link {
  position: absolute;
  left: -9999px;
  top: 0;
  background: var(--accent);
  color: var(--accent-fg);
  padding: 0.5rem 1rem;
  z-index: 100;
}

.skip-link:focus {
  left: 0.5rem;
  top: 0.5rem;
}

header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-4);
  border-bottom: 2px solid var(--accent);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  font-weight: 700;
  font-size: 1.125rem;
  color: var(--fg);
}

.brand svg {
  color: var(--accent);
  flex-shrink: 0;
}

nav[aria-label="Main"] {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
}

nav[aria-label="Main"] a,
nav[aria-label="Settings tabs"] a {
  color: var(--fg);
  text-decoration: none;
  font-size: 0.9375rem;
  font-weight: 500;
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius);
}

nav[aria-label="Main"] a:hover,
nav[aria-label="Settings tabs"] a:hover {
  background: var(--bg-elevated);
}

nav[aria-label="Main"] a[aria-current="page"],
nav[aria-label="Settings tabs"] a[aria-current="page"] {
  background: var(--accent);
  color: var(--accent-fg);
  font-weight: 700;
}

nav[aria-label="Settings tabs"] {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  margin-bottom: var(--space-5);
}

.nav-toggle {
  display: none;
  background: var(--bg-elevated);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.5rem 0.75rem;
  font-size: 1.25rem;
  line-height: 1;
  cursor: pointer;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

@media (max-width: 40rem) {
  .nav-toggle {
    display: inline-block;
  }

  nav[aria-label="Main"] {
    display: none;
    flex-direction: column;
    width: 100%;
  }

  nav[aria-label="Main"].open {
    display: flex;
  }
}

main {
  max-width: 60rem;
  margin: 0 auto;
  padding: var(--space-5) var(--space-4);
  overflow-wrap: break-word;
}

footer {
  max-width: 60rem;
  margin: 0 auto;
  padding: var(--space-4);
  color: var(--fg-muted);
  font-size: 0.875rem;
}

@media (min-width: 80rem) {
  main,
  footer {
    max-width: 80rem;
  }
}

a {
  color: var(--accent);
}

a:visited {
  color: var(--accent);
}

button, input, select {
  font: inherit;
  color: inherit;
}

button {
  background: var(--bg-elevated);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.5rem 0.875rem;
  cursor: pointer;
}

button:hover {
  border-color: var(--accent);
}

.btn-primary {
  background: var(--accent);
  color: var(--accent-fg);
  border: 1px solid var(--accent);
}

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

.btn {
  display: inline-block;
  background: var(--bg-elevated);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.5rem 0.875rem;
  text-decoration: none;
  cursor: pointer;
}

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

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--space-3);
}

.page-header h1 {
  margin: 0;
}

input[type="text"], input[type="email"], input[type="number"], select {
  background: var(--bg);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 0.25rem;
  padding: 0.375rem 0.5rem;
  width: 100%;
  max-width: 28rem;
}

input[type="file"] {
  border: 1px solid var(--border);
  border-radius: 0.25rem;
  padding: 0.375rem 0.5rem;
}

input[type="checkbox"], input[type="radio"] {
  accent-color: var(--accent);
  width: 1.05rem;
  height: 1.05rem;
}

label {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: var(--space-1);
  margin-bottom: var(--space-3);
}

label:has(> input[type="checkbox"]),
label:has(> input[type="radio"]) {
  flex-direction: row;
  align-items: center;
  gap: var(--space-2);
}

.card {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: var(--space-5);
  margin-bottom: var(--space-5);
}

.type-fields {
  border-left: 3px solid var(--border);
  padding-left: var(--space-4);
  margin-bottom: var(--space-4);
}

.history-toolbar {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  margin-bottom: var(--space-4);
}

.filter-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  gap: var(--space-3);
  margin-bottom: var(--space-4);
}

.filter-bar select[multiple] {
  min-height: 4.5rem;
  overflow-y: auto;
}

th a {
  color: inherit;
  text-decoration: none;
}

th a:hover {
  text-decoration: underline;
}

.hint {
  background: var(--bg-elevated);
  border-left: 4px solid var(--accent);
  border-radius: var(--radius);
  padding: var(--space-2) var(--space-3);
  margin-bottom: var(--space-3);
  font-size: 0.9375rem;
}

code {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 0.25rem;
  padding: 0.1rem 0.3rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.875em;
  overflow-wrap: anywhere;
}

fieldset {
  border: none;
  padding: 0;
  margin: 0;
}

legend {
  font-weight: 600;
  margin-bottom: var(--space-3);
  padding: 0;
}

.table-scroll {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

table {
  border-collapse: collapse;
  width: 100%;
}

th, td {
  padding: var(--space-3);
  text-align: left;
  border-bottom: 1px solid var(--border);
}

tr:last-child td {
  border-bottom: none;
}

th {
  background: var(--bg-elevated);
  font-weight: 600;
}

tbody tr:hover {
  background: var(--bg-elevated);
}

tr.removed td {
  color: var(--fg-muted);
}

tr.filter-mismatch {
  opacity: 0.45;
}

.inline-status-form {
  margin: 0;
}

.status-history {
  list-style: none;
  margin: var(--space-2) 0 0;
  padding: 0;
  font-size: 0.875rem;
  color: var(--fg-muted);
}

@media (max-width: 40rem) {
  .table-scroll {
    border: none;
    overflow-x: visible;
  }

  .table-scroll table,
  .table-scroll thead,
  .table-scroll tbody,
  .table-scroll th,
  .table-scroll td,
  .table-scroll tr {
    display: block;
    width: 100%;
  }

  .table-scroll thead {
    position: absolute;
    left: -9999px;
  }

  .table-scroll tr {
    border: 1px solid var(--border);
    border-radius: var(--radius);
    margin-bottom: var(--space-3);
  }

  .table-scroll td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: var(--space-3);
    text-align: right;
    border-bottom: 1px solid var(--border);
    overflow-wrap: anywhere;
  }

  .table-scroll tr td:last-child {
    border-bottom: none;
  }

  /* tr:last-child td (global, spec 0,1,2) strips all td borders in the last
     row — correct on desktop, wrong in the mobile card layout where td borders
     are intra-card separators.  This rule (spec 0,3,2) restores them. */
  .table-scroll tr:last-child td:not(:last-child) {
    border-bottom: 1px solid var(--border);
  }

  .table-scroll td::before {
    content: attr(data-label);
    font-weight: 600;
    text-align: left;
    color: var(--fg-muted);
  }
}

.error {
  background: var(--error-bg);
  color: var(--error-fg);
  border-left: 4px solid var(--error-fg);
  border-radius: var(--radius);
  padding: var(--space-3) var(--space-4);
  margin-bottom: var(--space-4);
}

.success {
  background: var(--success-bg);
  color: var(--success-fg);
  border-left: 4px solid var(--success-fg);
  border-radius: var(--radius);
  padding: var(--space-3) var(--space-4);
  margin-bottom: var(--space-4);
}

.toast-container {
  position: fixed;
  top: var(--space-4);
  right: var(--space-4);
  z-index: 100;
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.toast {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  background: var(--success-bg);
  color: var(--success-fg);
  border-left: 4px solid var(--success-fg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: var(--space-3) var(--space-4);
}

.toast-close {
  background: none;
  border: none;
  color: inherit;
  font-size: 1.1rem;
  line-height: 1;
  cursor: pointer;
  padding: 0;
}

nav[aria-label="Pagination"] {
  display: flex;
  gap: var(--space-4);
  align-items: center;
  margin-top: var(--space-4);
}

:focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 2px;
}

@media (max-width: 30rem) {
  header {
    flex-direction: column;
    align-items: stretch;
  }
}

dialog.modal {
  border: none;
  border-radius: var(--radius);
  padding: var(--space-5);
  background: var(--bg-elevated);
  color: var(--fg);
  box-shadow: var(--shadow);
  max-width: 28rem;
  width: calc(100% - var(--space-4) * 2);
}

dialog.modal::backdrop {
  background: rgba(0, 0, 0, 0.5);
}

dialog.modal .modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-2);
  margin-top: var(--space-4);
}

.home-marker {
  background: none;
  border: none;
}

.icon-btn {
  background: none;
  border: none;
  cursor: pointer;
  padding: 0 var(--space-1);
  font-size: 1rem;
  line-height: 1;
  color: var(--fg-muted);
  opacity: 0.6;
  vertical-align: middle;
}

.icon-btn:hover {
  opacity: 1;
  color: var(--accent);
}

.location-overridden-badge {
  margin-right: var(--space-1);
  color: var(--accent);
  font-size: 0.875rem;
  vertical-align: middle;
}

.badge {
  display: inline-block;
  padding: 0.1em 0.4em;
  border-radius: 3px;
  font-size: 0.75rem;
  font-weight: 600;
  vertical-align: middle;
  line-height: 1.4;
}

.badge-secondary {
  background: var(--accent);
  color: #fff;
  opacity: 0.85;
}

.badge-duplicate {
  background: var(--fg-muted);
  color: #fff;
  opacity: 0.8;
}

dialog.modal .modal-subtitle {
  color: var(--fg-muted);
  font-size: 0.875rem;
  margin-top: calc(var(--space-1) * -1);
  margin-bottom: var(--space-3);
}

dialog.modal .modal-error {
  background: var(--error-bg);
  color: var(--error-fg);
  border-left: 4px solid var(--error-fg);
  border-radius: var(--radius);
  padding: var(--space-2) var(--space-3);
  margin-top: var(--space-3);
  font-size: 0.875rem;
}

dialog.modal .field-hint {
  color: var(--fg-muted);
  font-size: 0.8125rem;
  margin: var(--space-1) 0 0;
}
