:root {
  --bg: #0f172a;
  --bg-alt: #111827;
  --surface: #1e293b;
  --surface-2: #273449;
  --surface-3: #334155;
  --text: #e5e7eb;
  --text-muted: #94a3b8;
  --primary: #10b981;
  --primary-hover: #059669;
  --primary-dark: #047857;
  --danger: #ef4444;
  --warning: #f59e0b;
  --border: #334155;
  --success: #10b981;

  --tap: 48px;
  --radius-sm: 8px;
  --radius: 14px;
  --radius-lg: 20px;
  --shadow: 0 6px 20px rgba(0,0,0,.35);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bot: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }
html { height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  font-size: 16px;
  line-height: 1.45;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bot);
  overflow-x: hidden;
}

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

h1, h2, h3, h4 { margin: 0 0 16px; font-weight: 700; color: var(--text); }
h1 { font-size: 22px; }
h2 { font-size: 19px; }
h3 { font-size: 17px; }
p { margin: 0 0 12px; color: var(--text); }

.container {
  max-width: 720px;
  margin: 0 auto;
  padding: 16px;
}

.app-header {
  position: sticky;
  top: var(--safe-top);
  z-index: 50;
  background: linear-gradient(180deg, #0b1220 0%, rgba(15,23,42,.92) 80%);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
  padding: 12px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-height: 56px;
}
.app-header .back-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  min-width: 40px;
  border-radius: 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  text-decoration: none;
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  transition: transform .05s ease, background .15s, color .15s, border-color .15s;
}
.app-header .back-btn:active { transform: scale(.95); }
.app-header .back-btn:hover, .app-header .back-btn:focus {
  color: var(--primary);
  border-color: var(--primary);
  background: rgba(16,185,129,.1);
}
.app-header .header-left {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}
.app-header .header-right {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex-shrink: 0;
}
.app-header .brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  font-size: 17px;
  color: var(--primary);
  min-width: 0;
}
.app-header .brand span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.app-header .brand .logo-dot {
  width: 28px; height: 28px;
  border-radius: 8px;
  background: radial-gradient(circle at 30% 30%, #34d399 0%, #059669 60%, #065f46 100%);
  box-shadow: 0 0 0 2px rgba(16,185,129,.25), var(--shadow);
}
.app-header .user-chip {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 6px 12px;
  border-radius: 999px;
  font-size: 13px;
  color: var(--text-muted);
  min-height: var(--tap);
}
.app-header .user-chip .role {
  color: var(--primary);
  font-weight: 600;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .5px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 14px;
  box-shadow: var(--shadow);
}
.card.tight { padding: 12px; }
.card-flat { background: transparent; border: none; box-shadow: none; padding: 0; }

.form-group { margin-bottom: 14px; }
label {
  display: block;
  margin-bottom: 6px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
}
input[type="text"], input[type="password"], input[type="email"], input[type="number"], textarea, select {
  width: 100%;
  min-height: var(--tap);
  padding: 0 14px;
  background: var(--surface-2);
  border: 1.5px solid var(--border);
  color: var(--text);
  border-radius: var(--radius-sm);
  font-size: 15px;
  font-family: inherit;
  transition: border-color .15s, box-shadow .15s;
}
textarea { min-height: 90px; padding: 10px 14px; }
input:focus, textarea:focus, select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(16,185,129,.18);
}
input::placeholder, textarea::placeholder { color: #64748b; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: var(--tap);
  padding: 0 18px;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 15px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  text-decoration: none;
  transition: transform .05s ease, background-color .15s, opacity .15s;
  user-select: none;
  width: 100%;
}
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary {
  background: linear-gradient(180deg, #10b981 0%, #059669 100%);
  color: white;
  box-shadow: 0 4px 14px rgba(16,185,129,.3), inset 0 1px 0 rgba(255,255,255,.15);
}
.btn-primary:hover, .btn-primary:focus { background: linear-gradient(180deg, #059669 0%, #047857 100%); }
.btn-secondary {
  background: var(--surface-3);
  color: var(--text);
  border: 1.5px solid var(--border);
}
.btn-secondary:hover { background: #475569; }
.btn-danger {
  background: linear-gradient(180deg, #ef4444 0%, #dc2626 100%);
  color: white;
}
.btn-ghost {
  background: transparent;
  color: var(--text-muted);
}
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }
.btn-group { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.btn-group.three { grid-template-columns: 1fr 1fr 1fr; }

.grid-tiles {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}
.tile {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 112px;
  padding: 14px 10px;
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  text-decoration: none;
  transition: transform .05s, border-color .15s, background .15s;
  cursor: pointer;
}
.tile:active { transform: scale(.97); }
.tile:hover, .tile:focus { border-color: var(--primary); background: var(--surface-2); }
.tile .tile-icon {
  width: 44px; height: 44px;
  border-radius: 12px;
  background: rgba(16,185,129,.14);
  color: var(--primary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
}
.tile .tile-title { font-size: 13px; font-weight: 600; text-align: center; }
.tile.primary {
  background: linear-gradient(160deg, rgba(16,185,129,.18), rgba(16,185,129,.04));
  border-color: rgba(16,185,129,.35);
}
.tile.primary .tile-icon { background: rgba(16,185,129,.25); }
.tile.danger .tile-icon { background: rgba(239,68,68,.14); color: var(--danger); }

.alert {
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  margin-bottom: 14px;
  border: 1px solid transparent;
}
.alert-error { background: rgba(239,68,68,.1); color: #fecaca; border-color: rgba(239,68,68,.3); }
.alert-success { background: rgba(16,185,129,.1); color: #a7f3d0; border-color: rgba(16,185,129,.3); }
.alert-info { background: rgba(59,130,246,.1); color: #bfdbfe; border-color: rgba(59,130,246,.3); }

.divider { height: 1px; background: var(--border); margin: 14px 0; }

.row { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.muted { color: var(--text-muted); font-size: 13px; }
.small { font-size: 12px; }
.badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .3px;
  text-transform: uppercase;
  background: var(--surface-3);
  color: var(--text-muted);
}
.badge-ok { background: rgba(16,185,129,.18); color: #6ee7b7; }
.badge-warn { background: rgba(245,158,11,.18); color: #fcd34d; }
.badge-danger { background: rgba(239,68,68,.18); color: #fca5a5; }
.badge-primary { background: rgba(16,185,129,.22); color: #6ee7b7; }

.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; min-width: 480px; font-size: 14px; }
th, td {
  padding: 10px 12px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
th { color: var(--text-muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: .4px; }
tr:last-child td { border-bottom: none; }
tr:hover td { background: rgba(255,255,255,.02); }

.list-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px;
  border-bottom: 1px solid var(--border);
  gap: 10px;
}
.list-row:last-child { border-bottom: none; }
.list-row .meta { min-width: 0; flex: 1; }
.list-row .meta .t { font-weight: 600; font-size: 15px; color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list-row .meta .s { font-size: 12px; color: var(--text-muted); margin-top: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list-row .actions { display: flex; gap: 6px; flex-shrink: 0; }
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px; height: 40px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text-muted);
  font-size: 18px;
  cursor: pointer;
  padding: 0;
}
.icon-btn:hover, .icon-btn:focus { color: var(--primary); border-color: var(--primary); }
.icon-btn.danger:hover { color: var(--danger); border-color: var(--danger); }
.icon-btn:disabled { opacity: .4; cursor: not-allowed; }

.bottom-sheet {
  position: fixed;
  left: 0; right: 0;
  bottom: 0;
  background: var(--surface);
  border-top: 1px solid var(--border);
  border-top-left-radius: var(--radius-lg);
  border-top-right-radius: var(--radius-lg);
  padding: 16px 16px calc(16px + var(--safe-bot));
  box-shadow: 0 -8px 40px rgba(0,0,0,.45);
  z-index: 100;
  transform: translateY(105%);
  transition: transform .25s ease;
}
.bottom-sheet.open { transform: translateY(0); }
.sheet-handle {
  width: 48px; height: 4px; background: var(--surface-3);
  border-radius: 2px; margin: 0 auto 12px;
}

.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.65);
  backdrop-filter: blur(4px);
  z-index: 200;
  display: none;
  align-items: flex-end;
  justify-content: center;
}
.modal-backdrop.open { display: flex; }
.modal {
  width: 100%;
  max-width: 520px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-top-left-radius: var(--radius-lg);
  border-top-right-radius: var(--radius-lg);
  padding: 20px 16px calc(20px + var(--safe-bot));
  box-shadow: 0 -8px 40px rgba(0,0,0,.5);
  animation: slideUp .22s ease;
}
@keyframes slideUp { from { transform: translateY(100%); } to { transform: translateY(0); } }
.modal h3 { margin-bottom: 10px; }
.modal .actions { margin-top: 16px; display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.auth-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px 16px;
  background:
    radial-gradient(1200px 600px at 80% -10%, rgba(16,185,129,.15), transparent 60%),
    radial-gradient(800px 500px at -10% 110%, rgba(16,185,129,.08), transparent 60%),
    var(--bg);
}
.auth-card {
  width: 100%;
  max-width: 420px;
}
.auth-card .logo {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin-bottom: 22px;
}
.auth-card .logo .mark {
  width: 52px; height: 52px; border-radius: 14px;
  background: radial-gradient(circle at 30% 30%, #34d399 0%, #059669 60%, #065f46 100%);
  box-shadow: 0 0 0 4px rgba(16,185,129,.15), 0 10px 30px rgba(16,185,129,.25);
}
.auth-card .logo h1 {
  margin: 0;
  font-size: 24px;
  background: linear-gradient(90deg, #6ee7b7 0%, #10b981 50%, #059669 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.auth-card .subtitle {
  text-align: center;
  color: var(--text-muted);
  margin-bottom: 22px;
  font-size: 14px;
}

.scanner-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 4 / 5;
  background: #000;
  border-radius: var(--radius);
  overflow: hidden;
  margin-bottom: 14px;
}
#camera-feed, #scanner-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.scan-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background:
    linear-gradient(180deg, rgba(0,0,0,.55), transparent 25%, transparent 75%, rgba(0,0,0,.55));
}
.scan-frame {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 68%;
  aspect-ratio: 1;
  transform: translate(-50%, -50%);
  border: 2.5px solid transparent;
  border-radius: 18px;
  box-shadow: 0 0 0 9999px rgba(0,0,0,.45);
}
.scan-frame::before {
  content: "";
  position: absolute; inset: -2px;
  border: 2.5px solid var(--primary);
  border-radius: 18px;
  clip-path: polygon(0 28%, 22% 28%, 22% 0, 78% 0, 78% 22%, 100% 22%, 100% 78%, 78% 78%, 78% 100%, 22% 100%, 22% 78%, 0 78%);
  box-shadow: 0 0 20px rgba(16,185,129,.5);
}
.scan-line {
  position: absolute;
  left: 10%;
  width: 80%;
  height: 2px;
  background: linear-gradient(90deg, transparent, #10b981, transparent);
  box-shadow: 0 0 12px rgba(16,185,129,.8);
  top: 16%;
  animation: scan 2.4s ease-in-out infinite;
}
@keyframes scan {
  0%, 100% { top: 18%; }
  50% { top: 82%; }
}
.scanner-status {
  text-align: center;
  color: var(--text-muted);
  font-size: 13px;
  margin-bottom: 10px;
}
.scanner-status.ok { color: #6ee7b7; }
.scanner-status.err { color: #fca5a5; }

.perm-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin: 12px 0 16px;
}
.perm-box {
  background: var(--surface-2);
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px;
  text-align: center;
}
.perm-box .ic { font-size: 22px; margin-bottom: 4px; }
.perm-box .l { font-size: 12px; color: var(--text-muted); }
.perm-box.ok { border-color: rgba(16,185,129,.35); background: rgba(16,185,129,.06); }
.perm-box.ok .l { color: #6ee7b7; }
.perm-box.no { border-color: rgba(239,68,68,.35); background: rgba(239,68,68,.06); }
.perm-box.no .l { color: #fca5a5; }

.history-item .loc {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 11px;
  color: var(--text-muted);
  background: var(--surface-2);
  padding: 2px 8px;
  border-radius: 999px;
  margin-top: 4px;
}
.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: var(--text-muted);
}
.empty-state .big { font-size: 44px; margin-bottom: 8px; opacity: .5; }

@media (min-width: 720px) {
  .container { padding: 24px; }
  .grid-tiles { grid-template-columns: repeat(4, 1fr); }
  .btn { width: auto; }
  .btn.block { width: 100%; }
  .app-header { padding: 14px 24px; }
  .modal { margin-bottom: 24px; border-radius: var(--radius-lg); }
  .modal-backdrop { align-items: center; }
}

.hidden { display: none !important; }

/* ==========================================================
   Checkbox lista modułów / grup - mobile friendly
   ========================================================== */
.check-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 420px;
  overflow-y: auto;
  padding: 6px 4px 6px 2px;
  -webkit-overflow-scrolling: touch;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
}
.check-list:empty {
  padding: 20px 10px;
  text-align: center;
  color: var(--text-muted);
  font-size: 13px;
}
.check-list:empty::after {
  content: attr(data-empty-label) || 'Brak elementów';
  display: block;
}
.check-item {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 10px 12px;
  min-height: 48px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  background: transparent;
  transition: background-color .12s, border-color .12s;
  cursor: pointer;
  user-select: none;
}
.check-item:hover { background: rgba(16,185,129,.06); }
.check-item input[type="checkbox"] {
  width: 24px;
  height: 24px;
  min-width: 24px;
  margin-top: 2px;
  accent-color: var(--primary);
  cursor: pointer;
}
.check-item .ci-body {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.check-item .ci-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 8px;
}
.check-item .ci-title .emoji { font-size: 18px; }
.check-item .ci-sub {
  font-size: 12px;
  color: var(--text-muted);
  line-height: 1.4;
}
.check-item.disabled {
  opacity: .5;
  cursor: not-allowed;
}
.check-item.disabled input[type="checkbox"] { cursor: not-allowed; }
.check-item.checked {
  border-color: rgba(16,185,129,.4);
  background: rgba(16,185,129,.08);
}

.stat-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin-top: 6px;
}
.stat {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
}
.stat .n { font-size: 20px; font-weight: 800; color: var(--primary); }
.stat .l { font-size: 11px; color: var(--text-muted); text-transform: uppercase; letter-spacing: .4px; margin-top: 2px; }

.section { margin-bottom: 18px; }
.section-title {
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .5px;
  color: var(--text-muted);
  margin-bottom: 8px;
}
