:root,
:root[data-theme="light"] {
  --bg: #f4f5f7;
  --card-bg: #ffffff;
  --border: #dcdfe4;
  --text: #1f2430;
  --muted: #6b7280;
  --accent: #2563eb;
  --danger: #dc2626;
  --success: #16a34a;
  --pin-bg: #fef3c7;
  --pin-text: #92400e;
}

:root[data-theme="dark"] {
  --bg: #14161c;
  --card-bg: #1c1f28;
  --border: #333844;
  --text: #e5e7eb;
  --muted: #9199a8;
  --accent: #5b8bf0;
  --danger: #f87171;
  --success: #4ade80;
  --pin-bg: #3a2f0f;
  --pin-text: #fbbf24;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  transition: background-color 0.15s ease, color 0.15s ease;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 1.5rem;
  background: var(--card-bg);
  border-bottom: 1px solid var(--border);
}

.topbar h1 { font-size: 1.1rem; margin: 0; }

.topbar-actions { display: flex; align-items: center; gap: 0.75rem; }

.theme-toggle {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.35rem 0.55rem;
  font-size: 1rem;
  line-height: 1;
}
.theme-toggle:hover { opacity: 0.8; }

.user-bar { display: flex; align-items: center; gap: 0.75rem; }
.user-bar.hidden, .hidden { display: none !important; }

.main-nav { display: flex; gap: 0.5rem; flex-wrap: wrap; align-items: center; }
.nav-btn {
  background: transparent;
  color: var(--muted);
  border: 1px solid transparent;
  padding: 0.4rem 0.8rem;
  white-space: nowrap;
}
.nav-btn.active { color: var(--accent); border-color: var(--accent); background: var(--bg); }

.nav-dropdown { position: relative; }
.nav-dropdown-btn { cursor: pointer; }
.nav-dropdown-btn.active { color: var(--accent); border-color: var(--accent); background: var(--bg); }
.nav-dropdown-menu {
  position: absolute;
  top: calc(100% + 0.25rem);
  left: 0;
  z-index: 20;
  display: flex;
  flex-direction: column;
  min-width: 12rem;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
  padding: 0.35rem;
}
.nav-dropdown-item {
  background: transparent;
  color: var(--text);
  border: none;
  border-radius: 6px;
  text-align: left;
  padding: 0.5rem 0.6rem;
  white-space: nowrap;
}
.nav-dropdown-item:hover { background: var(--bg); }
.nav-dropdown-item.active { color: var(--accent); font-weight: 600; }

.admin-table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
.admin-table th, .admin-table td {
  text-align: left;
  padding: 0.5rem;
  border-bottom: 1px solid var(--border);
}
.admin-table select { padding: 0.25rem; }

main {
  max-width: 860px;
  margin: 2rem auto;
  padding: 0 1rem;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.breadcrumb { display: flex; align-items: center; gap: 0.35rem; margin-bottom: 1rem; flex-wrap: wrap; }
.breadcrumb .crumb {
  background: transparent;
  border: none;
  padding: 0.2rem 0.3rem;
  color: var(--accent);
  font-size: 0.9rem;
  cursor: pointer;
}
.breadcrumb .crumb:hover { text-decoration: underline; }
.breadcrumb .crumb.current { color: var(--text); font-weight: 600; cursor: default; }
.breadcrumb .crumb.current:hover { text-decoration: none; }
.breadcrumb .crumb-sep { color: var(--muted); }

.filter-label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.85rem;
  color: var(--muted);
  margin: 0.75rem 0;
}
.filter-label select { padding: 0.35rem 0.5rem; }

.customer-detail dl { display: grid; grid-template-columns: auto 1fr; gap: 0.35rem 1rem; margin: 0 0 1rem; }
.customer-detail dt { color: var(--muted); font-size: 0.85rem; }
.customer-detail dd { margin: 0; }
.customer-detail h4 { margin: 1rem 0 0.5rem; font-size: 0.95rem; }
.clickable-row { cursor: pointer; }
.clickable-row:hover { background: var(--bg); }

.debug-stats {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 0.35rem 1rem;
  margin: 0 0 1rem;
  max-width: 24rem;
}
.debug-stats dt { color: var(--muted); font-size: 0.85rem; }
.debug-stats dd { margin: 0; font-weight: 600; }

.card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1.5rem;
}

.tabs { display: flex; gap: 0.5rem; margin-bottom: 1rem; }
.tab-btn {
  flex: 1;
  padding: 0.5rem;
  border: 1px solid var(--border);
  background: var(--bg);
  border-radius: 6px;
  cursor: pointer;
}
.tab-btn.active { background: var(--accent); color: white; border-color: var(--accent); }

form label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  margin-bottom: 0.75rem;
  font-size: 0.9rem;
  color: var(--muted);
}

input, select {
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 1rem;
  background: var(--card-bg);
  color: var(--text);
}

button {
  padding: 0.55rem 1rem;
  border: none;
  border-radius: 6px;
  background: var(--accent);
  color: white;
  font-size: 0.95rem;
  cursor: pointer;
}
button:hover { opacity: 0.9; }

.hint { font-size: 0.85rem; color: var(--muted); min-height: 1.2em; }

.search-form {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 0.5rem;
  margin-bottom: 1rem;
}
.search-form > input[type="text"],
.search-form > input[type="email"] {
  flex: 1 1 12rem;
  min-width: 10rem;
}
.search-form label {
  margin-bottom: 0;
  flex: 0 0 auto;
}
.search-form select,
.search-form button {
  flex-shrink: 0;
  white-space: nowrap;
}

.result-list { list-style: none; padding: 0; margin: 0; }
.result-list li {
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  margin-bottom: 0.5rem;
  cursor: pointer;
}
.result-list li:hover { background: var(--bg); }
.result-list li .sn { font-weight: 600; }
.result-list li .meta { font-size: 0.85rem; color: var(--muted); }

.device-detail {
  margin-top: 1.5rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--border);
}
.device-detail dl { display: grid; grid-template-columns: auto 1fr; gap: 0.35rem 1rem; margin: 0; }
.device-detail dt { color: var(--muted); font-size: 0.85rem; }
.device-detail dd { margin: 0; }
.pin-badge {
  display: inline-block;
  font-family: ui-monospace, monospace;
  font-size: 1.1rem;
  font-weight: 700;
  background: var(--pin-bg);
  color: var(--pin-text);
  padding: 0.15rem 0.5rem;
  border-radius: 4px;
}
.error-msg { color: var(--danger); font-size: 0.9rem; }

.pin-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
  margin-top: 0.5rem;
}
.pin-controls button { padding: 0.35rem 0.6rem; font-size: 0.85rem; }
.pin-controls #pinHistoryBox { flex-basis: 100%; margin-top: 0.5rem; }

#deviceStatusSelect { padding: 0.3rem 0.4rem; font-size: 0.9rem; }

.device-detail dd input[type="text"] { width: 12rem; margin-right: 0.4rem; }

.device-comments {
  margin-top: 1.5rem;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
}
.device-comments h4 { margin: 0 0 0.5rem; font-size: 0.95rem; }
.device-comments .result-list li { cursor: default; }
.device-comments .result-list li:hover { background: none; }

.overdue-badge {
  display: inline-block;
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  background: var(--danger);
  color: white;
  padding: 0.1rem 0.4rem;
  border-radius: 4px;
}

.pagination {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-top: 1rem;
}

.archived-badge {
  display: inline-block;
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  background: var(--muted);
  color: white;
  padding: 0.1rem 0.4rem;
  border-radius: 4px;
}
tr.archived-row, li.archived-row { opacity: 0.6; }

.appointment-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.5rem;
}
.appointment-actions button { padding: 0.35rem 0.6rem; font-size: 0.85rem; }
.reschedule-box {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-top: 0.5rem;
}

.user-calendar-box {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
  margin-top: 0.4rem;
}
.user-calendar-box .ics-url-input { flex-basis: 100%; min-width: 16rem; }

.user-name-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.35rem 0.6rem;
  border-radius: 6px;
  cursor: pointer;
}
.user-name-btn:hover { opacity: 0.85; }
.pagination button:disabled { opacity: 0.4; cursor: default; }
#mapContainer {
  height: 70vh;
  min-height: 400px;
  border-radius: 8px;
  border: 1px solid var(--border);
}

.marker-popup h4 { margin: 0 0 0.35rem; }
.marker-popup .hint { margin: 0.35rem 0 0.5rem; }

.admin-table code {
  font-size: 0.8rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.1rem 0.35rem;
}

.ics-url-label {
  display: block;
  font-size: 0.85rem;
  color: var(--muted);
  margin-bottom: 0.35rem;
}
.ics-url-row { display: flex; gap: 0.5rem; }
.ics-url-input {
  flex: 1;
  font-family: ui-monospace, monospace;
  font-size: 0.85rem;
  background: var(--bg);
}

/* --- Termine: Planungsansicht (Wochenraster) --- */
.view-toggle { display: flex; gap: 0.5rem; margin-bottom: 1rem; }
.view-toggle-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  padding: 0.4rem 0.8rem;
  border-radius: 6px;
}
.view-toggle-btn.active { color: var(--accent); border-color: var(--accent); background: var(--bg); }

.grid-week-nav { display: flex; align-items: center; gap: 0.75rem; margin-bottom: 0.75rem; }

.appointment-grid {
  display: flex;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow-x: auto;
  background: var(--card-bg);
}

.grid-time-column { flex: 0 0 3.5rem; border-right: 1px solid var(--border); }
.grid-day { flex: 1; min-width: 8rem; border-right: 1px solid var(--border); }
.grid-day:last-child { border-right: none; }

.grid-day-header {
  height: 2.25rem;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.8rem;
  font-weight: 600;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  position: sticky;
  top: 0;
}

.grid-hour-label {
  font-size: 0.7rem;
  color: var(--muted);
  border-top: 1px solid var(--border);
  padding: 0.15rem 0.3rem;
  box-sizing: border-box;
}
.grid-hour-label:first-child { border-top: none; }

.grid-day-body {
  position: relative;
  background-image: repeating-linear-gradient(to bottom, var(--border) 0, var(--border) 1px, transparent 1px, transparent 48px);
}

.grid-appt {
  position: absolute;
  left: 2px;
  right: 2px;
  overflow: hidden;
  border-radius: 4px;
  padding: 0.15rem 0.3rem;
  font-size: 0.7rem;
  line-height: 1.25;
  cursor: pointer;
  border: 1px solid transparent;
}
.grid-appt-planned { background: var(--accent); color: white; opacity: 0.85; }
.grid-appt-confirmed { background: var(--success); color: white; }
.grid-appt-cancelled { background: transparent; border-color: var(--muted); color: var(--muted); text-decoration: line-through; }
.grid-appt-meta { opacity: 0.85; }
.grid-appt:hover { opacity: 1; z-index: 5; }
