:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-alt: #f8fafc;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --text: #0f172a;
  --text-muted: #475569;
  --text-soft: #64748b;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-soft: #eff6ff;
  --success: #16a34a;
  --success-soft: #dcfce7;
  --danger: #dc2626;
  --danger-soft: #fee2e2;
  --warning: #d97706;
  --warning-soft: #fef3c7;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.05);
  --shadow: 0 6px 18px rgba(15, 23, 42, 0.08);
}

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

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, Arial, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

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

/* =========================================================
   CSS Animations (MDN Using CSS Animations – GPU friendly)
   ========================================================= */
@keyframes cardEnter {
  from {
    opacity: 0;
    transform: translateY(12px) scale(0.985);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@keyframes slideDownFade {
  from {
    opacity: 0;
    transform: translateY(-8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes shakeError {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-7px); }
  40% { transform: translateX(6px); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(3px); }
}

@keyframes pulseRing {
  0% {
    transform: scale(0.9);
    opacity: 0.75;
  }
  70% {
    transform: scale(2.1);
    opacity: 0;
  }
  100% {
    transform: scale(2.1);
    opacity: 0;
  }
}

@keyframes backdropFade {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes modalPop {
  from {
    opacity: 0;
    transform: scale(0.95) translateY(8px);
  }
  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

@keyframes sheetUp {
  from {
    opacity: 0;
    transform: translateY(100%);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes toastPop {
  0% {
    opacity: 0;
    transform: translateY(14px) scale(0.95);
  }
  12% {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
  85% {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
  100% {
    opacity: 0;
    transform: translateY(-6px) scale(0.98);
  }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* Header */
.topbar {
  position: sticky;
  top: 0;
  z-index: 50;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}

.topbar-inner {
  max-width: 1280px;
  margin: 0 auto;
  padding: 0 24px;
  min-height: 62px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  font-size: 1.12rem;
  letter-spacing: -0.02em;
  flex-shrink: 0;
}

.brand-mark {
  width: 34px;
  height: 34px;
  border-radius: 9px;
  background: linear-gradient(135deg, #2563eb, #1e40af);
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 6px rgba(37, 99, 235, 0.28);
}

.brand-sub {
  font-weight: 500;
  font-size: 0.8rem;
  color: var(--text-soft);
  padding-left: 8px;
  border-left: 1px solid var(--border);
}

.nav {
  display: flex;
  align-items: center;
  gap: 4px;
}

.nav-link {
  padding: 7px 13px;
  border-radius: var(--radius-sm);
  font-weight: 500;
  font-size: 0.9rem;
  color: var(--text-muted);
  white-space: nowrap;
  transition: background 0.15s, color 0.15s;
}

.nav-link:hover {
  background: var(--surface-alt);
  color: var(--text);
}

.nav-link.active {
  background: var(--primary-soft);
  color: var(--primary);
  font-weight: 600;
}

.user-menu {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}

.user-pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 5px 12px;
  border-radius: 999px;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  font-size: 0.85rem;
}

.role-badge {
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  padding: 2px 7px;
  border-radius: 999px;
  letter-spacing: 0.04em;
}

.role-admin {
  background: var(--primary-soft);
  color: var(--primary);
}

.role-user {
  background: #e2e8f0;
  color: var(--text-muted);
}

/* Container */
.container {
  max-width: 1280px;
  margin: 0 auto;
  padding: 28px 24px 64px;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 9px 15px;
  min-height: 38px;
  border-radius: var(--radius-sm);
  font-weight: 600;
  font-size: 0.88rem;
  border: 1px solid transparent;
  cursor: pointer;
  transition: transform 0.1s ease, background 0.15s ease, box-shadow 0.15s ease;
  font-family: inherit;
}

.btn:active {
  transform: scale(0.97);
}

.btn-primary {
  background: var(--primary);
  color: #fff;
}
.btn-primary:hover {
  background: var(--primary-hover);
}

.btn-secondary {
  background: var(--surface);
  color: var(--text);
  border-color: var(--border-strong);
}
.btn-secondary:hover {
  background: var(--surface-alt);
}

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
}
.btn-ghost:hover {
  background: var(--surface-alt);
  color: var(--text);
}

.btn-danger {
  background: var(--danger-soft);
  color: var(--danger);
}
.btn-danger:hover {
  background: #fecaca;
}

.btn-sm {
  padding: 6px 11px;
  min-height: 34px;
  font-size: 0.82rem;
}

/* Page header & Search */
.page-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 24px;
  animation: slideDownFade 0.24s ease-out both;
}

.page-title {
  margin: 0;
  font-size: 1.55rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.page-subtitle {
  margin: 3px 0 0;
  color: var(--text-soft);
  font-size: 0.92rem;
}

.head-controls {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.search-wrap {
  position: relative;
  width: 320px;
  max-width: 100%;
}

.search-input {
  width: 100%;
  padding: 9px 56px 9px 36px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--surface);
  font-size: 0.9rem;
  color: var(--text);
  outline: none;
  font-family: inherit;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.search-input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
}

.search-icon {
  position: absolute;
  left: 11px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-soft);
  pointer-events: none;
}

.kbd-hint {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 0.72rem;
  color: var(--text-soft);
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 1px 6px;
  pointer-events: none;
}

/* Category & Cards Grid */
.cat-section {
  margin-bottom: 32px;
}

.cat-title {
  margin: 0 0 14px;
  font-size: 0.84rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-soft);
  display: flex;
  align-items: center;
  gap: 8px;
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(305px, 1fr));
  gap: 16px;
  align-items: start;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  transition: transform 0.16s ease, box-shadow 0.16s ease, border-color 0.16s ease;
  animation: cardEnter 0.34s cubic-bezier(0.22, 1, 0.36, 1) both;
  animation-delay: calc(var(--i, 0) * 35ms);
}

.card:hover {
  transform: translateY(-3px);
  box-shadow: var(--shadow);
  border-color: var(--border-strong);
}

.card-accent {
  height: 4px;
  width: 100%;
}

.card-body {
  padding: 18px;
  flex: 1;
  display: flex;
  flex-direction: column;
}

.card-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}

.card-icon {
  width: 42px;
  height: 42px;
  border-radius: 10px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  flex-shrink: 0;
}

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-size: 0.76rem;
  font-weight: 600;
  padding: 3px 10px;
  border-radius: 999px;
  background: var(--surface-alt);
  color: var(--text-soft);
  border: 1px solid var(--border);
}

.status-pill.online {
  background: var(--success-soft);
  color: var(--success);
  border-color: #bbf7d0;
}

.status-pill.offline {
  background: var(--danger-soft);
  color: var(--danger);
  border-color: #fecaca;
}

.dot {
  position: relative;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: currentColor;
  flex-shrink: 0;
}

.status-pill.online .dot::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 50%;
  background: currentColor;
  animation: pulseRing 2.2s cubic-bezier(0.215, 0.61, 0.355, 1) infinite;
}

.card-title-row {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 8px;
  margin-bottom: 14px;
}

.card-title {
  margin: 0;
  font-size: 1.08rem;
  font-weight: 700;
  color: var(--text);
}

.desc-toggle {
  width: 100%;
  min-height: 34px;
  padding: 6px 10px;
  border-radius: 8px;
  border: 1px dashed var(--border-strong);
  background: var(--surface-alt);
  color: var(--text-muted);
  font-size: 0.8rem;
  font-weight: 600;
  font-family: inherit;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.desc-toggle:hover {
  background: var(--primary-soft);
  color: var(--primary);
  border-color: #93c5fd;
}

.desc-toggle svg {
  flex-shrink: 0;
  transition: transform 0.28s cubic-bezier(0.22, 1, 0.36, 1);
}

.card.desc-open .desc-toggle {
  background: var(--primary-soft);
  color: var(--primary);
  border-style: solid;
  border-color: #bfdbfe;
}

.card.desc-open .desc-toggle svg {
  transform: rotate(180deg);
}

/* Rozsuwana szufladka z opisem spod widocznej aplikacji */
.card-desc-drawer {
  display: grid;
  grid-template-rows: 0fr;
  opacity: 0;
  background: var(--surface-alt);
  border-top: 0 solid var(--border);
  transition: grid-template-rows 0.32s cubic-bezier(0.22, 1, 0.36, 1),
              opacity 0.24s ease,
              border-top-width 0.2s ease;
}

.card.desc-open .card-desc-drawer {
  grid-template-rows: 1fr;
  opacity: 1;
  border-top-width: 1px;
}

.card-desc-inner {
  overflow: hidden;
}

.card-desc {
  margin: 0;
  padding: 13px 18px;
  font-size: 0.86rem;
  line-height: 1.55;
  color: var(--text-muted);
  min-height: 46px;
}

.card-desc.scrambling {
  color: var(--primary);
}

.card-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 9px 11px;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  margin-bottom: 14px;
  font-size: 0.82rem;
}

.card-meta-label {
  color: var(--text-soft);
}

.card-meta-value {
  font-weight: 600;
  color: var(--text);
}

.card-actions {
  display: flex;
  gap: 8px;
}

.card-actions .btn-open {
  flex: 1;
}

/* Panels, Tables & Forms */
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  padding: 20px 22px;
  margin-bottom: 22px;
  animation: slideDownFade 0.25s ease-out both;
}

.panel-title {
  margin: 0 0 14px;
  font-size: 1.08rem;
  font-weight: 700;
}

.table-wrap {
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.89rem;
}

th, td {
  text-align: left;
  padding: 11px 12px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

th {
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-soft);
  background: var(--surface-alt);
  font-weight: 700;
}

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

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: 14px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.field label {
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text-muted);
}

.input, select, textarea {
  width: 100%;
  padding: 9px 11px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  font-size: 0.9rem;
  font-family: inherit;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.input:focus, select:focus, textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
}

.input-sm {
  padding: 7px 9px;
  font-size: 0.84rem;
}

/* Alerts */
.alert {
  padding: 11px 15px;
  border-radius: var(--radius-sm);
  margin-bottom: 18px;
  font-size: 0.9rem;
  font-weight: 500;
  border: 1px solid transparent;
  animation: slideDownFade 0.24s ease-out both;
}

.alert-success {
  background: var(--success-soft);
  color: #166534;
  border-color: #bbf7d0;
}

.alert-error {
  background: var(--danger-soft);
  color: #991b1b;
  border-color: #fecaca;
}

/* Login page */
.login-page {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: radial-gradient(circle at top right, #dbeafe 0%, #f4f6f9 45%);
}

.login-card {
  width: 100%;
  max-width: 400px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: 0 12px 32px rgba(15, 23, 42, 0.08);
  padding: 32px 28px;
  animation: cardEnter 0.35s cubic-bezier(0.22, 1, 0.36, 1) both;
}

.login-card.shake-error {
  animation: shakeError 0.42s cubic-bezier(0.36, 0.07, 0.19, 0.97) both;
}

.login-head {
  text-align: center;
  margin-bottom: 24px;
}

.login-logo {
  width: 52px;
  height: 52px;
  border-radius: 14px;
  background: linear-gradient(135deg, #2563eb, #1e40af);
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 12px;
  box-shadow: 0 6px 16px rgba(37, 99, 235, 0.25);
}

/* Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 100;
}

.modal-backdrop.open {
  display: flex;
  animation: backdropFade 0.18s ease-out both;
}

.modal {
  background: var(--surface);
  border-radius: 14px;
  max-width: 440px;
  width: 100%;
  padding: 24px;
  box-shadow: 0 20px 40px rgba(15, 23, 42, 0.2);
  animation: modalPop 0.22s cubic-bezier(0.22, 1, 0.36, 1) both;
}

.cred-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 9px 12px;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  margin-bottom: 10px;
}

/* Toast notifications */
.toast-container {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}

.toast {
  background: #0f172a;
  color: #fff;
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 0.86rem;
  font-weight: 600;
  box-shadow: 0 10px 25px rgba(15, 23, 42, 0.22);
  animation: toastPop 2.1s cubic-bezier(0.22, 1, 0.36, 1) forwards;
}

.mono {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.check-toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  font-weight: 600;
}

.check-toggle input[type="checkbox"] {
  width: 18px;
  height: 18px;
  accent-color: var(--primary);
  cursor: pointer;
}

/* =========================================================
   Mobile & Tablet Responsive Layout
   ========================================================= */
@media (max-width: 768px) {
  .topbar-inner {
    flex-wrap: wrap;
    padding: 10px 14px;
    gap: 8px;
  }

  .brand-sub {
    display: none;
  }

  .user-pill {
    padding: 4px 9px;
    font-size: 0.78rem;
  }

  .user-name-text {
    max-width: 105px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .nav {
    order: 3;
    width: 100%;
    overflow-x: auto;
    padding-bottom: 4px;
    border-top: 1px solid var(--border);
    padding-top: 6px;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }

  .nav::-webkit-scrollbar {
    display: none;
  }

  .nav-link {
    padding: 8px 12px;
    font-size: 0.85rem;
  }

  .container {
    padding: 18px 14px 56px;
  }

  .page-title {
    font-size: 1.32rem;
  }

  .head-controls {
    width: 100%;
  }

  .search-wrap {
    width: 100%;
    flex: 1;
  }

  .kbd-hint {
    display: none;
  }

  .grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }

  .btn {
    min-height: 44px;
  }

  .btn-sm {
    min-height: 38px;
  }

  /* Zapobiega automatycznemu zoomowaniu iOS/Android przy focusie */
  .input, select, textarea, .search-input {
    font-size: 16px;
  }

  .panel {
    padding: 16px 14px;
  }

  /* Responsywne tabele zamieniające się w przejrzyste karty na mobile */
  table.mobile-cards thead {
    display: none;
  }

  table.mobile-cards,
  table.mobile-cards tbody,
  table.mobile-cards tr,
  table.mobile-cards td {
    display: block;
    width: 100%;
  }

  table.mobile-cards tr {
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 10px 12px;
    margin-bottom: 12px;
    background: var(--surface-alt);
  }

  table.mobile-cards td {
    border-bottom: 1px solid var(--border);
    padding: 8px 0;
    text-align: left !important;
  }

  table.mobile-cards td:last-child {
    border-bottom: none;
    padding-bottom: 2px;
  }

  table.mobile-cards td::before {
    content: attr(data-label);
    display: block;
    font-size: 0.72rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--text-soft);
    margin-bottom: 4px;
  }

  /* Bottom sheet modal na komórkach */
  .modal-backdrop {
    align-items: flex-end;
    padding: 0;
  }

  .modal {
    max-width: 100%;
    border-radius: 18px 18px 0 0;
    padding: 20px 18px calc(20px + env(safe-area-inset-bottom, 0px));
    animation: sheetUp 0.25s cubic-bezier(0.22, 1, 0.36, 1) both;
  }

  .toast-container {
    left: 16px;
    right: 16px;
    bottom: calc(16px + env(safe-area-inset-bottom, 0px));
    align-items: center;
  }
}
