/* ==========================================================
   Zutrittskontrolle – zentrales Stylesheet
   ========================================================== */

:root {
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --primary-soft: #dbeafe;
  --danger: #dc2626;
  --danger-dark: #b91c1c;
  --danger-soft: #fee2e2;
  --success: #16a34a;
  --success-soft: #dcfce7;
  --warning: #d97706;
  --warning-soft: #fef3c7;

  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;

  --header-bg: linear-gradient(135deg, #1e3a5f 0%, #2563eb 100%);
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 3px rgba(15, 23, 42, .08), 0 4px 14px rgba(15, 23, 42, .06);
  --shadow-lg: 0 10px 40px rgba(15, 23, 42, .25);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --transition: .18s ease;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f172a;
    --surface: #1e293b;
    --surface-2: #273449;
    --border: #334155;
    --text: #e2e8f0;
    --muted: #94a3b8;
    --primary-soft: #1e3a8a;
    --danger-soft: #4c0f0f;
    --success-soft: #0a3a1e;
    --warning-soft: #4a2604;
    --shadow: 0 1px 3px rgba(0, 0, 0, .4), 0 4px 14px rgba(0, 0, 0, .3);
  }
}

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

html, body { margin: 0; padding: 0; }

body {
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

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

h1, h2, h3 { margin: 0; line-height: 1.25; }

.muted { color: var(--muted); }
.pre { white-space: pre-line; }
[hidden] { display: none !important; }

/* ---------- Icons ---------- */
.icon { display: inline-flex; align-items: center; justify-content: center; }
.icon svg {
  width: 1.25em; height: 1.25em;
  fill: none; stroke: currentColor; stroke-width: 2;
  stroke-linecap: round; stroke-linejoin: round;
}

/* ---------- Header ---------- */
.app-header {
  position: sticky; top: 0; z-index: 10;
  background: var(--header-bg);
  color: #fff;
  box-shadow: 0 2px 10px rgba(0, 0, 0, .15);
}
.app-header .bar {
  max-width: 1200px; margin: 0 auto;
  display: flex; align-items: center; gap: .75rem;
  padding: .75rem 1rem;
}
.app-header h1 { font-size: 1.25rem; font-weight: 600; flex: 1; }
.app-header .logo { width: 36px; height: 36px; }
.app-header .icon-btn { color: #fff; }
.app-header .icon-btn:hover { background: rgba(255, 255, 255, .15); }

.nfc-editor-trigger { margin-top: .5rem; }
.nfc-status { min-height: 3em; }
#nfc-dialog .modal-footer { flex-wrap: wrap; }
#nfc-key { font-family: ui-monospace, "Cascadia Code", Consolas, monospace; font-size: 1.25rem; }

.tabs {
  max-width: 1200px; margin: 0 auto;
  display: flex; gap: .25rem; padding: 0 1rem;
  overflow-x: auto;
}
.tabs a {
  color: rgba(255, 255, 255, .8);
  padding: .5rem 1rem;
  border-radius: var(--radius-sm) var(--radius-sm) 0 0;
  font-weight: 500;
  white-space: nowrap;
  transition: background var(--transition), color var(--transition);
}
.tabs a:hover { color: #fff; background: rgba(255, 255, 255, .1); text-decoration: none; }
.tabs a.active { background: var(--bg); color: var(--text); }

/* ---------- Layout ---------- */
.container { max-width: 1200px; margin: 0 auto; padding: 1.5rem 1rem 3rem; }
.container-narrow { max-width: 720px; }
.spacer { flex: 1; }
.section-gap { margin-top: 1.5rem; }
.hint-block { font-size: .8rem; margin: -.5rem 0 1rem; }
.alert.flush { margin: 0; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.card-body { padding: 1.25rem 1.5rem; }
.card-title { font-size: 1.1rem; margin-bottom: 1rem; }

/* ---------- Kacheln (Hauptseite) ---------- */
.hero { text-align: center; margin: 1rem 0 2rem; }
.hero img { width: 96px; height: 96px; }
.hero h2 { font-size: 1.6rem; margin-top: .5rem; }
.hero p { color: var(--muted); margin: .25rem 0 0; }

.tiles {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 1.25rem;
}
.tile {
  display: flex; flex-direction: column; gap: .5rem;
  padding: 1.5rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  color: var(--text);
  transition: transform var(--transition), box-shadow var(--transition), border-color var(--transition);
}
.tile:hover {
  text-decoration: none;
  transform: translateY(-3px);
  box-shadow: 0 10px 30px rgba(37, 99, 235, .18);
  border-color: var(--primary);
}
.tile-icon {
  width: 56px; height: 56px;
  border-radius: 14px;
  background: var(--primary-soft);
  color: var(--primary);
  font-size: 1.6rem;
}
.tile:nth-child(2) .tile-icon { background: var(--warning-soft); color: var(--warning); }
.tile:nth-child(3) .tile-icon { background: var(--success-soft); color: var(--success); }
.tile:nth-child(4) .tile-icon { background: var(--surface-2); color: var(--muted); }
.tile h3 { font-size: 1.15rem; }
.tile p { margin: 0; color: var(--muted); font-size: .9rem; }
.tile-count { margin-top: auto; font-size: .85rem; font-weight: 600; color: var(--primary); }

/* ---------- Toolbar ---------- */
.toolbar {
  display: flex; flex-wrap: wrap; align-items: center; gap: .6rem;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface-2);
}
.toolbar .spacer { flex: 1; }

.search-box { position: relative; flex: 1 1 220px; max-width: 360px; }
.search-box .icon { position: absolute; left: .7rem; top: 50%; transform: translateY(-50%); color: var(--muted); }
.search-box input[type="search"] { padding-left: 2.3rem; }

/* ---------- Formulare ---------- */
input[type="text"], input[type="search"], input[type="url"], input[type="password"], input[type="number"],
input:not([type]), textarea, select {
  width: 100%;
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: .55rem .75rem;
  transition: border-color var(--transition), box-shadow var(--transition);
}
select { cursor: pointer; }
.toolbar select { width: auto; max-width: 100%; }
input:focus, textarea:focus, select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, .2);
}
input:user-invalid { border-color: var(--danger); }
textarea { resize: vertical; }

.field { margin-bottom: 1rem; border: 0; padding: 0; }
.field > label, .field .label { display: block; font-weight: 600; font-size: .875rem; margin-bottom: .35rem; }
.field .hint { font-size: .8rem; color: var(--muted); margin-top: .25rem; }
.field-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 0 1rem; }

.record-timestamps { margin: 1rem 0 0; padding-top: .75rem; border-top: 1px solid var(--border); }
.record-timestamps dt { color: var(--muted); font-size: .8rem; font-weight: 600; }
.record-timestamps dd { margin: .25rem 0 0; font-size: .875rem; overflow-wrap: anywhere; }
.allow-row > .record-timestamps { grid-column: 1 / -1; margin: 0; }

.input-group { display: flex; gap: .4rem; }
.input-group input { flex: 1; }

/* Schalter */
.switch-row { display: flex; flex-wrap: wrap; gap: 1.5rem; margin-bottom: 1rem; }
.switch { display: inline-flex; align-items: center; gap: .6rem; cursor: pointer; user-select: none; font-weight: 500; }
.switch input { position: absolute; opacity: 0; pointer-events: none; }
.switch .slider {
  position: relative; width: 42px; height: 24px; flex-shrink: 0;
  background: var(--border); border-radius: 999px;
  transition: background var(--transition);
}
.switch .slider::after {
  content: ""; position: absolute; top: 3px; left: 3px;
  width: 18px; height: 18px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, .3);
  transition: transform var(--transition);
}
.switch input:checked + .slider { background: var(--success); }
.switch input:checked + .slider::after { transform: translateX(18px); }
.switch input:focus-visible + .slider { box-shadow: 0 0 0 3px rgba(37, 99, 235, .35); }

/* Checkliste (Berechtigungen) */
.check-list {
  max-height: 240px; overflow-y: auto;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface-2);
}
.check-list-head {
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: .35rem;
}
.check-list-head .label { margin: 0; }
.check-item {
  display: flex; align-items: center; gap: .6rem;
  padding: .5rem .75rem; cursor: pointer;
  border-bottom: 1px solid var(--border);
}
.check-item:last-child { border-bottom: 0; }
.check-item:hover { background: var(--primary-soft); }
.check-item input { width: 18px; height: 18px; accent-color: var(--primary); }
.check-item span { flex: 1; }
.check-item.is-inactive span { color: var(--muted); }
.check-list .muted { padding: .75rem; margin: 0; }

.allow-list { display: grid; gap: .5rem; }
.allow-row { display: grid; grid-template-columns: 48px minmax(0, 1fr) minmax(0, 1.2fr) 36px; align-items: center; gap: .4rem; }
.allow-row select { min-width: 0; }
.allow-id { font-size: .75rem; overflow-wrap: anywhere; }
.rule-summary { display: flex; flex-wrap: wrap; gap: .25rem; }
.rule-summary .badge { white-space: normal; }
.rule-summary small { flex-basis: 100%; }

@media (max-width: 480px) {
  .allow-row { grid-template-columns: minmax(0, 1fr) 36px; }
  .allow-row .allow-id { grid-column: 1; }
  .allow-row .allow-entry, .allow-row .allow-state { grid-column: 1; }
  .allow-row .icon-btn { grid-column: 2; grid-row: 1 / 4; }
}

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  font: inherit; font-weight: 600; font-size: .9rem;
  padding: .55rem 1rem;
  border: 1px solid transparent; border-radius: var(--radius-sm);
  cursor: pointer; white-space: nowrap;
  transition: background var(--transition), border-color var(--transition), transform .05s;
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-dark); }
.btn-secondary { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn-secondary:hover:not(:disabled) { background: var(--surface-2); border-color: var(--muted); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover:not(:disabled) { background: var(--danger-dark); }
.btn-link { background: none; border: 0; color: var(--primary); padding: .2rem .4rem; font-size: .8rem; }
.btn-link:hover { text-decoration: underline; }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 36px; height: 36px; padding: 0;
  background: transparent; color: var(--muted);
  border: 0; border-radius: var(--radius-sm);
  font-size: 1.1rem; cursor: pointer;
  transition: background var(--transition), color var(--transition);
}
.icon-btn:hover { background: var(--surface-2); color: var(--primary); text-decoration: none; }
.icon-btn.danger:hover { background: var(--danger-soft); color: var(--danger); }

/* ---------- Tabelle ---------- */
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { padding: .7rem 1rem; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table th {
  font-size: .75rem; font-weight: 700; text-transform: uppercase; letter-spacing: .04em;
  color: var(--muted); background: var(--surface-2);
}
.table tbody tr { transition: background var(--transition); }
.table tbody tr:hover { background: var(--surface-2); }
.table tbody tr.is-inactive td:not(.actions) { opacity: .6; }
.table .num { width: 1%; white-space: nowrap; color: var(--muted); font-variant-numeric: tabular-nums; }
.table .actions { width: 1%; white-space: nowrap; text-align: right; }
.table .state-row td { text-align: center; padding: 2.5rem 1rem; color: var(--muted); }
.table .state-row.error td { color: var(--danger); }
.table .mono { font-family: ui-monospace, "Cascadia Code", Consolas, monospace; }
.table-footer { padding: .6rem 1.25rem; font-size: .85rem; color: var(--muted); background: var(--surface-2); }

/* Badges */
.badge {
  display: inline-block; padding: .15rem .6rem;
  font-size: .75rem; font-weight: 600;
  border-radius: 999px;
}
.badge-on { background: var(--success-soft); color: var(--success); }
.badge-off { background: var(--surface-2); color: var(--muted); border: 1px solid var(--border); }
.badge-info { background: var(--primary-soft); color: var(--primary); }
.badge-warn { background: var(--warning-soft); color: var(--warning); }

/* Spinner */
.spinner {
  display: inline-block; width: 22px; height: 22px;
  border: 3px solid var(--border); border-top-color: var(--primary);
  border-radius: 50%; animation: spin .8s linear infinite;
  vertical-align: middle; margin-right: .5rem;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Hinweise ---------- */
.alert { padding: .9rem 1.1rem; border-radius: var(--radius-sm); margin-bottom: 1.25rem; border: 1px solid transparent; }
.alert-warning { background: var(--warning-soft); color: var(--warning); border-color: currentColor; }
.alert-info { background: var(--primary-soft); color: var(--primary); }
.alert a { color: inherit; font-weight: 700; text-decoration: underline; }

/* ---------- Modal ---------- */
dialog.modal {
  width: min(560px, calc(100vw - 2rem));
  max-height: calc(100vh - 2rem);
  padding: 0; border: 0;
  border-radius: var(--radius);
  background: var(--surface); color: var(--text);
  box-shadow: var(--shadow-lg);
}
dialog.modal-sm { width: min(420px, calc(100vw - 2rem)); }
dialog.modal[open] { animation: pop .18s ease-out; }
dialog.modal::backdrop { background: rgba(15, 23, 42, .55); backdrop-filter: blur(2px); }
@keyframes pop { from { opacity: 0; transform: translateY(12px) scale(.98); } }

.modal-header { display: flex; align-items: center; justify-content: space-between; padding: 1rem 1.5rem; border-bottom: 1px solid var(--border); }
.modal-header h2 { font-size: 1.15rem; }
.modal-body { padding: 1.25rem 1.5rem; overflow-y: auto; max-height: calc(100vh - 12rem); }
.modal-footer { display: flex; justify-content: flex-end; gap: .6rem; padding: 1rem 1.5rem; border-top: 1px solid var(--border); background: var(--surface-2); }

/* ---------- Toasts ---------- */
#toasts {
  position: fixed; right: 1rem; bottom: 1rem; z-index: 1000;
  display: flex; flex-direction: column; gap: .5rem;
  max-width: min(400px, calc(100vw - 2rem));
}
.toast {
  padding: .8rem 1rem; border-radius: var(--radius-sm);
  background: var(--text); color: var(--surface);
  box-shadow: var(--shadow-lg);
  border-left: 5px solid var(--primary);
  animation: slide-in .2s ease-out;
}
.toast.success { border-left-color: var(--success); }
.toast.error { border-left-color: var(--danger); }
.toast.warning { border-left-color: var(--warning); }
.toast.hide { opacity: 0; transform: translateX(20px); transition: all .3s; }
@keyframes slide-in { from { opacity: 0; transform: translateX(30px); } }

/* ---------- Mobil: Tabelle als Karten ---------- */
@media (max-width: 720px) {
  .container { padding: 1rem .6rem 3rem; }
  .toolbar { padding: .75rem; }
  .search-box { max-width: none; }
  .table thead { display: none; }
  .table, .table tbody, .table tr, .table td { display: block; width: 100%; }
  .table tr { padding: .6rem .9rem; border-bottom: 1px solid var(--border); }
  .table td { border: 0; padding: .2rem 0; display: flex; justify-content: space-between; gap: 1rem; }
  .table td::before { content: attr(data-label); font-weight: 600; color: var(--muted); font-size: .8rem; }
  .table .num, .table .actions { width: 100%; }
  .table .actions { justify-content: flex-end; }
  .table .actions::before { content: none; }
  .table .state-row td { display: block; }
  .table .state-row td::before { content: none; }
  .modal-body { padding: 1rem; }
}
