@import url('https://fonts.googleapis.com/css2?family=Fira+Code:wght@400;500;600&family=Fira+Sans:wght@400;500;600;700&display=swap');
@import 'tokens.css';

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font-sans);
  font-size: var(--text-base);
  line-height: 1.5;
  background: var(--color-bg);
  color: var(--color-fg);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--color-primary); }
.mono { font-family: var(--font-mono); }
.muted { color: var(--color-fg-muted); }
.subtle { color: var(--color-fg-subtle); }
.small { font-size: var(--text-sm); }
.hidden { display: none !important; }

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

/* ---- Layout ---- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: var(--space-3) var(--space-6);
  border-bottom: 1px solid var(--color-border-subtle);
  background: var(--color-bg-elevated);
  position: sticky; top: 0; z-index: 10;
}
.brand { display: flex; align-items: center; gap: var(--space-3); }
.brand-icon {
  width: 34px; height: 34px; border-radius: var(--radius-md);
  background: linear-gradient(135deg, var(--p-green-500), var(--p-green-950));
  display: flex; align-items: center; justify-content: center;
  box-shadow: var(--shadow-glow-primary);
}
.brand-title { font-weight: 700; font-size: var(--text-lg); letter-spacing: -0.01em; }
.brand-sub { font-size: var(--text-xs); }
.topbar-right { display: flex; align-items: center; gap: var(--space-4); }

.layout { display: flex; min-height: calc(100vh - 61px); }
.sidebar {
  width: 220px; flex-shrink: 0; padding: var(--space-4);
  border-right: 1px solid var(--color-border-subtle);
  display: flex; flex-direction: column; gap: var(--space-1);
}
.nav-item {
  display: flex; align-items: center; gap: var(--space-3);
  padding: var(--space-3) var(--space-3);
  border-radius: var(--radius-md);
  color: var(--color-fg-muted);
  background: transparent; border: none; cursor: pointer;
  font-family: inherit; font-size: var(--text-sm); font-weight: 500;
  text-align: left; width: 100%;
  transition: background var(--duration-fast) var(--ease-standard), color var(--duration-fast) var(--ease-standard);
}
.nav-item svg { width: 18px; height: 18px; flex-shrink: 0; }
.nav-item:hover { background: var(--color-surface-hover); color: var(--color-fg); }
.nav-item.active { background: var(--color-surface); color: var(--color-fg); box-shadow: inset 2px 0 0 var(--color-primary); }
.nav-group-label {
  font-size: 10px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase;
  color: var(--color-fg-subtle); padding: var(--space-3) var(--space-3) 2px;
}
.nav-group-label:not(:first-child) { margin-top: var(--space-2); }

.content { flex: 1; padding: var(--space-6); max-width: 1280px; }
.view { display: none; animation: fade-in var(--duration-standard) var(--ease-standard); }
.view.active { display: block; }
@keyframes fade-in { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: translateY(0); } }

.view-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: var(--space-5); gap: var(--space-4); flex-wrap: wrap; }
.view-title { font-size: var(--text-xl); font-weight: 700; margin: 0; letter-spacing: -0.01em; }
.view-title .mono { font-weight: 500; color: var(--color-fg-muted); font-size: var(--text-base); }

/* ---- Stat tiles ---- */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: var(--space-3); margin-bottom: var(--space-5); }
.stat {
  background: var(--color-surface); border: 1px solid var(--color-border-subtle);
  border-radius: var(--radius-lg); padding: var(--space-4);
}
.stat-label { font-size: var(--text-xs); color: var(--color-fg-muted); text-transform: uppercase; letter-spacing: .04em; margin-bottom: var(--space-1); }
.stat-value { font-size: var(--text-2xl); font-weight: 700; font-family: var(--font-mono); }

/* ---- Cards ---- */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: var(--space-4); }
.card {
  background: var(--color-surface); border: 1px solid var(--color-border-subtle);
  border-radius: var(--radius-lg); padding: var(--space-4);
  margin-bottom: var(--space-4);
  transition: border-color var(--duration-fast) var(--ease-standard);
}
.card-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: var(--space-3); gap: var(--space-3); }
.card-title { font-weight: 600; font-size: var(--text-base); display: flex; align-items: center; gap: var(--space-2); }
.card-title svg { width: 18px; height: 18px; color: var(--color-fg-muted); }
.card-row { display: flex; align-items: center; justify-content: space-between; padding: var(--space-2) 0; border-bottom: 1px solid var(--color-border-subtle); gap: var(--space-3); }
.card-row:last-child { border-bottom: none; }
.sub { font-size: var(--text-xs); text-transform: uppercase; letter-spacing: .05em; color: var(--color-fg-muted); margin: var(--space-4) 0 var(--space-2); }

.device-card { cursor: pointer; }
.device-card:hover { border-color: var(--color-border); background: var(--color-surface-hover); }
.device-card.selected { border-color: var(--color-primary); box-shadow: var(--shadow-glow-primary); }
.device-name { font-weight: 600; font-size: var(--text-lg); }
.device-meta { color: var(--color-fg-muted); font-size: var(--text-sm); margin-top: 2px; }

/* ---- Badges ---- */
.badge {
  display: inline-flex; align-items: center; gap: var(--space-1);
  padding: 3px 10px; border-radius: 999px;
  font-size: var(--text-xs); font-weight: 600;
  background: var(--color-surface); border: 1px solid var(--color-border);
  color: var(--color-fg-muted);
}
.badge.ok { background: var(--p-green-950); color: var(--p-green-400); border-color: rgba(34,197,94,.35); }
.badge.warn { background: var(--p-amber-950); color: var(--color-warn); border-color: rgba(245,158,11,.35); }
.badge.off { background: var(--p-red-950); color: var(--p-red-400); border-color: rgba(239,68,68,.35); }
.badge-dot { width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge.sm { padding: 1px 7px; font-size: 10px; margin-left: 6px; background: var(--p-red-950); color: var(--p-red-400); border-color: rgba(239,68,68,.35); }

/* ---- Switch (an/aus Toggle) ---- */
.switch { position: relative; display: inline-block; width: 44px; height: 26px; flex-shrink: 0; cursor: pointer; }
.switch input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.switch-track { position: absolute; inset: 0; background: var(--color-surface-hover); border: 1px solid var(--color-border); border-radius: 999px; transition: background .15s, border-color .15s; }
.switch-thumb { position: absolute; top: 2px; left: 2px; width: 20px; height: 20px; border-radius: 50%; background: var(--color-fg-subtle); transition: transform .15s, background .15s; }
.switch input:checked + .switch-track { background: var(--p-green-950); border-color: rgba(34,197,94,.5); }
.switch input:checked + .switch-track .switch-thumb { transform: translateX(18px); background: var(--color-primary); }
.switch input:focus-visible + .switch-track { outline: 2px solid var(--color-primary); outline-offset: 2px; }

/* ---- Buttons ---- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--space-2);
  font-family: inherit; font-size: var(--text-sm); font-weight: 600;
  padding: 9px 16px; border-radius: var(--radius-md); border: 1px solid transparent;
  cursor: pointer; min-height: 38px;
  transition: background var(--duration-fast) var(--ease-standard), border-color var(--duration-fast) var(--ease-standard), transform var(--duration-fast) var(--ease-standard);
}
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .5; cursor: not-allowed; transform: none; }
.btn svg { width: 16px; height: 16px; }
.btn-primary { background: var(--color-primary); color: var(--color-primary-fg); }
.btn-primary:hover:not(:disabled) { background: var(--color-primary-hover); }
.btn-secondary { background: var(--color-surface); color: var(--color-fg); border-color: var(--color-border); }
.btn-secondary:hover:not(:disabled) { background: var(--color-surface-hover); border-color: var(--color-fg-subtle); }
.btn-danger { background: transparent; color: var(--color-danger); border-color: rgba(239,68,68,.4); }
.btn-danger:hover:not(:disabled) { background: var(--p-red-950); border-color: var(--color-danger); }
.btn-ghost { background: transparent; color: var(--color-fg-muted); }
.btn-ghost:hover:not(:disabled) { color: var(--color-fg); background: var(--color-surface-hover); }
.btn-sm { min-height: 32px; padding: 6px 12px; font-size: var(--text-xs); }
.btn-lg { min-height: 46px; font-size: var(--text-base); }
.btn-block { width: 100%; }
.btn-row { display: flex; gap: var(--space-2); flex-wrap: wrap; margin-top: var(--space-3); }

/* ---- Forms ---- */
.fld { display: flex; flex-direction: column; gap: var(--space-1); font-size: var(--text-sm); color: var(--color-fg-muted); margin-bottom: var(--space-3); }
.input, .fld input[type="text"], .fld textarea, .fld select {
  font-family: inherit; font-size: var(--text-sm); color: var(--color-fg);
  background: var(--color-bg-elevated); border: 1px solid var(--color-border);
  border-radius: var(--radius-sm); padding: 9px 12px;
  transition: border-color var(--duration-fast) var(--ease-standard);
}
.fld input:focus, .fld textarea:focus, .fld select:focus { border-color: var(--color-primary); }
.fld textarea { font-family: var(--font-mono); resize: vertical; }
.field-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: var(--space-3); }

.toggle-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: var(--space-2); }
.chk { display: flex; align-items: center; gap: var(--space-2); font-size: var(--text-sm); cursor: pointer; padding: var(--space-2); border-radius: var(--radius-sm); }
.chk:hover { background: var(--color-surface-hover); }
.chk input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--color-primary); cursor: pointer; }

/* ---- Data table ---- */
.data-table { width: 100%; border-collapse: collapse; font-size: var(--text-sm); }
.data-table th {
  text-align: left; padding: var(--space-3) var(--space-4);
  font-size: var(--text-xs); text-transform: uppercase; letter-spacing: .04em;
  color: var(--color-fg-muted); border-bottom: 1px solid var(--color-border);
  white-space: nowrap;
}
.data-table td { padding: var(--space-3) var(--space-4); border-bottom: 1px solid var(--color-border-subtle); vertical-align: middle; }
.data-table tbody tr { cursor: pointer; transition: background var(--duration-fast) var(--ease-standard); }
.data-table tbody tr:hover { background: var(--color-surface-hover); }
.data-table tbody tr:last-child td { border-bottom: none; }
.data-table td.mono { font-family: var(--font-mono); font-size: var(--text-xs); color: var(--color-fg-muted); }

/* ---- Enrollment link box ---- */
.link-box {
  display: flex; align-items: center; gap: var(--space-2);
  background: var(--color-bg-elevated); border: 1px solid var(--color-border);
  border-radius: var(--radius-sm); padding: var(--space-3); margin: var(--space-3) 0;
}
.link-box code { flex: 1; font-family: var(--font-mono); font-size: var(--text-sm); overflow-wrap: anywhere; }

/* ---- Log / activity feed ---- */
.log { font-family: var(--font-mono); font-size: var(--text-xs); background: #000814; border: 1px solid var(--color-border-subtle); border-radius: var(--radius-md); padding: var(--space-3); max-height: 320px; overflow: auto; white-space: pre-wrap; word-break: break-all; }
.activity-item { display: flex; gap: var(--space-3); padding: var(--space-3) 0; border-bottom: 1px solid var(--color-border-subtle); }
.activity-item:last-child { border-bottom: none; }
.activity-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--color-primary); margin-top: 6px; flex-shrink: 0; box-shadow: 0 0 8px rgba(34,197,94,.6); }
.activity-dot.error { background: var(--color-danger); box-shadow: 0 0 8px rgba(239,68,68,.6); }
.activity-body { flex: 1; }
.activity-time { font-size: var(--text-xs); color: var(--color-fg-subtle); font-family: var(--font-mono); }

/* ---- Login ---- */
.center { display: flex; align-items: center; justify-content: center; min-height: 100vh; }
.login-card {
  width: 380px; background: var(--color-surface); border: 1px solid var(--color-border-subtle);
  border-radius: var(--radius-lg); padding: var(--space-8); text-align: center;
  box-shadow: var(--shadow-md);
}
.login-icon { width: 56px; height: 56px; margin: 0 auto var(--space-4); border-radius: var(--radius-lg); background: linear-gradient(135deg, var(--p-green-500), var(--p-green-950)); display: flex; align-items: center; justify-content: center; box-shadow: var(--shadow-glow-primary); }
.login-icon svg { width: 28px; height: 28px; color: #04140a; }
.login-card h1 { font-size: var(--text-xl); margin: 0 0 var(--space-1); }
.alert { background: var(--p-red-950); border: 1px solid rgba(239,68,68,.35); color: var(--p-red-400); border-radius: var(--radius-sm); padding: var(--space-3); font-size: var(--text-sm); margin: var(--space-4) 0; text-align: left; }

/* ---- Toast ---- */
.toast {
  position: fixed; bottom: var(--space-6); left: 50%; transform: translateX(-50%);
  background: var(--color-bg-elevated); border: 1px solid var(--color-border);
  padding: var(--space-3) var(--space-5); border-radius: var(--radius-md);
  font-size: var(--text-sm); box-shadow: var(--shadow-md); z-index: 100;
  transition: opacity var(--duration-standard) var(--ease-standard), transform var(--duration-standard) var(--ease-standard);
}
.toast.ok { border-color: rgba(34,197,94,.4); color: var(--p-green-400); }
.toast.err { border-color: rgba(239,68,68,.4); color: var(--p-red-400); }

/* ---- Lock Hero (Sperren) ---- */
.lock-hero {
  display: flex; align-items: center; justify-content: space-between; gap: var(--space-5);
  padding: var(--space-5); border-radius: var(--radius-lg);
  border: 1px solid var(--color-border-subtle);
  background: linear-gradient(135deg, var(--color-surface), var(--color-bg-elevated));
  margin-bottom: var(--space-5); flex-wrap: wrap;
}
.lock-hero.is-locked { border-color: rgba(239,68,68,.4); box-shadow: var(--shadow-glow-danger); }
.lock-hero.is-unlocked { border-color: rgba(34,197,94,.35); box-shadow: var(--shadow-glow-primary); }
.lock-hero-info { display: flex; align-items: center; gap: var(--space-4); }
.lock-hero-icon {
  width: 52px; height: 52px; border-radius: var(--radius-lg); flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
}
.lock-hero.is-locked .lock-hero-icon { background: var(--p-red-950); color: var(--p-red-400); }
.lock-hero.is-unlocked .lock-hero-icon { background: var(--p-green-950); color: var(--p-green-400); }
.lock-hero-icon svg { width: 26px; height: 26px; }
.lock-hero-title { font-weight: 700; font-size: var(--text-lg); }
.lock-hero-sub { color: var(--color-fg-muted); font-size: var(--text-sm); margin-top: 2px; }

/* ---- Standort / Find-My-artige Karte ---- */
.live-indicator {
  display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px 4px 8px;
  border-radius: 999px; background: var(--p-green-950); color: var(--p-green-400);
  border: 1px solid rgba(34,197,94,.35); font-size: var(--text-xs); font-weight: 700;
}
.live-dot { width: 7px; height: 7px; border-radius: 50%; background: currentColor; position: relative; }
.live-dot::after { content: ""; position: absolute; inset: -4px; border-radius: 50%; background: currentColor; opacity: .5; animation: livepulse 1.8s ease-out infinite; }
@keyframes livepulse { 0% { transform: scale(.6); opacity: .6; } 100% { transform: scale(2.2); opacity: 0; } }

.findmy-shell {
  display: grid; grid-template-columns: 1fr 340px; gap: var(--space-4);
  height: calc(100vh - 220px); min-height: 420px;
}
.findmy-map {
  border-radius: var(--radius-lg); overflow: hidden; border: 1px solid var(--color-border-subtle);
  background: var(--color-surface);
}
/* Dunkle Karte im Find-My-Stil, ohne bezahlten Dark-Tile-Anbieter: invertierte+neu
   eingefaerbte OpenStreetMap-Kacheln per CSS-Filter. */
.findmy-map .leaflet-tile-pane { filter: invert(1) hue-rotate(180deg) brightness(.95) contrast(.9) saturate(.7); }
.findmy-sheet {
  background: var(--color-bg-elevated); border: 1px solid var(--color-border-subtle);
  border-radius: var(--radius-lg); padding: var(--space-4); overflow-y: auto;
  display: flex; flex-direction: column; gap: var(--space-3);
}
.findmy-sheet-head { display: flex; align-items: center; gap: var(--space-3); }
.findmy-device-icon {
  width: 40px; height: 40px; border-radius: 50%; flex-shrink: 0;
  background: linear-gradient(135deg, var(--p-green-500), var(--p-green-950));
  display: flex; align-items: center; justify-content: center; color: #04140a; font-weight: 700;
}
.findmy-device-name { font-weight: 700; }
.findmy-device-sub { font-size: var(--text-xs); }
.findmy-stats { display: flex; flex-direction: column; gap: var(--space-2); padding: var(--space-3) 0; border-top: 1px solid var(--color-border-subtle); border-bottom: 1px solid var(--color-border-subtle); }
.findmy-stat { display: flex; align-items: center; justify-content: space-between; font-size: var(--text-sm); }

.findmy-mini-map { height: 160px; border-radius: var(--radius-md); overflow: hidden; margin-bottom: var(--space-3); border: 1px solid var(--color-border-subtle); }
.findmy-mini-map .leaflet-tile-pane { filter: invert(1) hue-rotate(180deg) brightness(.95) contrast(.9) saturate(.7); }

/* Pulsierender Standort-Marker (Find-My-Look), als L.divIcon statt Bild-Pin */
.findmy-marker { width: 22px; height: 22px; position: relative; }
.findmy-marker .dot { position: absolute; inset: 4px; border-radius: 50%; background: var(--color-primary); border: 3px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,.5); }
.findmy-marker .ring { position: absolute; inset: 0; border-radius: 50%; background: var(--color-primary); opacity: .35; animation: livepulse 2.4s ease-out infinite; }

/* ---- App-Zeilen (Apps-Karte) ---- */
.app-row { display: flex; align-items: center; gap: var(--space-3); padding: var(--space-3) 0; border-bottom: 1px solid var(--color-border-subtle); }
.app-row:last-child { border-bottom: none; }
.app-avatar {
  width: 40px; height: 40px; border-radius: var(--radius-md); flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: var(--text-base); color: var(--color-fg);
  background: linear-gradient(135deg, var(--p-slate-700), var(--p-slate-800));
}
.app-info { flex: 1; min-width: 0; }
.app-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.app-meta { color: var(--color-fg-muted); font-size: var(--text-xs); display: flex; gap: var(--space-2); flex-wrap: wrap; }
.app-actions { display: flex; gap: var(--space-2); flex-shrink: 0; }

/* ---- Section-Card (info-lastig, z.B. Bildschirmzeit) ---- */
.info-card { border-left: 3px solid var(--color-info); }
.info-card .card-title svg { color: var(--color-info); }

/* ---- Einschränkungen: Kategorie-Tabs + Allow/Deny-Regler ---- */
.cat-tabs { display: flex; gap: var(--space-2); flex-wrap: wrap; margin-bottom: var(--space-4); overflow-x: auto; padding-bottom: 2px; }
.cat-tab {
  font-family: inherit; font-size: var(--text-sm); font-weight: 600; white-space: nowrap;
  padding: 8px 14px; border-radius: 999px; border: 1px solid var(--color-border);
  background: var(--color-surface); color: var(--color-fg-muted); cursor: pointer;
  transition: all var(--duration-fast) var(--ease-standard);
}
.cat-tab:hover { color: var(--color-fg); border-color: var(--color-fg-subtle); }
.cat-tab.active { background: var(--color-primary); color: var(--color-primary-fg); border-color: var(--color-primary); }

.restriction-row {
  display: flex; align-items: center; justify-content: space-between; gap: var(--space-4);
  padding: var(--space-3) 0; border-bottom: 1px solid var(--color-border-subtle);
}
.restriction-row:last-child { border-bottom: none; }
.restriction-label { font-size: var(--text-sm); }
.restriction-key { display: block; font-family: var(--font-mono); font-size: 11px; color: var(--color-fg-subtle); margin-top: 1px; }

.seg-toggle { display: inline-flex; border: 1px solid var(--color-border); border-radius: var(--radius-sm); overflow: hidden; flex-shrink: 0; }
.seg-toggle button {
  font-family: inherit; font-size: var(--text-xs); font-weight: 700; text-transform: uppercase; letter-spacing: .03em;
  padding: 7px 14px; border: none; background: var(--color-bg-elevated); color: var(--color-fg-muted); cursor: pointer;
  transition: all var(--duration-fast) var(--ease-standard);
}
.seg-toggle button:first-child { border-right: 1px solid var(--color-border); }
.seg-toggle button.on.allow { background: var(--p-green-950); color: var(--p-green-400); }
.seg-toggle button.on.deny { background: var(--p-red-950); color: var(--p-red-400); }
.seg-toggle button:hover:not(.on) { color: var(--color-fg); }
.seg-toggle button:disabled { cursor: not-allowed; opacity: .4; }
.restriction-row-locked { opacity: .55; }

/* ---- Responsive ---- */
@media (max-width: 768px) {
  .layout { flex-direction: column; }
  .sidebar { width: 100%; flex-direction: row; overflow-x: auto; border-right: none; border-bottom: 1px solid var(--color-border-subtle); }
  .nav-item { width: auto; white-space: nowrap; }
  .content { padding: var(--space-4); }
  .view-head { flex-direction: column; align-items: stretch; }
  .view-head label.fld { min-width: 0 !important; }
  .lock-hero { flex-direction: column; align-items: stretch; text-align: center; }
  .lock-hero-info { flex-direction: column; }
  .restriction-row { flex-direction: column; align-items: flex-start; gap: var(--space-2); }
  .app-row { flex-wrap: wrap; }
  .app-actions { width: 100%; justify-content: flex-end; }
  .nav-group-label { display: none; }
  .findmy-shell { grid-template-columns: 1fr; height: auto; }
  .findmy-map { height: 320px; }
}
