/* === Design tokens === */
:root {
  --bg-base: #1a1a2e;
  --bg-card: #16213e;
  --bg-deep: #0f3460;
  --fg: #eee;
  --fg-muted: #dce4ee;
  --fg-dim: #cdd9e5;
  --fg-sub: #c0c0c0;
  --fg-link: #ccc;
  --accent: #f06070;
  --accent-bg: rgba(240,96,112,0.15);
  --ok: #53d769;
  --ok-bg: rgba(83,215,105,0.12);
  --danger: #f25545;
  --highlight: #f1c40f;
  --border: #0f3460;
  --radius: 0.625rem;
  --radius-lg: 0.75rem;
  --touch-min: max(2.75rem, 44px);
  --space-xs: clamp(0.25rem, 0.5vw, 0.5rem);
  --space-sm: clamp(0.5rem, 1vw, 0.75rem);
  --space-md: clamp(0.75rem, 1.5vw, 1rem);
  --space-lg: clamp(1rem, 2vw, 1.5rem);
  --font-xs: clamp(0.78rem, 0.7rem + 0.4vw, 0.88rem);
  --font-sm: clamp(0.84rem, 0.76rem + 0.4vw, 0.95rem);
  --font-base: clamp(0.9rem, 0.82rem + 0.45vw, 1.05rem);
  --font-lg: clamp(1rem, 0.9rem + 0.5vw, 1.2rem);
  --font-xl: clamp(1.2rem, 1rem + 0.8vw, 1.6rem);
}

* { margin: 0; padding: 0; box-sizing: border-box; }
html, body {
  height: 100%; width: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: var(--font-base);
  background: var(--bg-base);
  color: var(--fg);
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  line-height: 1.5;
}

/* === Header === */
.header {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between;
  padding: var(--space-sm) var(--space-md);
  background: var(--bg-card);
  border-bottom: 1px solid var(--bg-deep);
}
.header h1 { font-size: var(--font-lg); font-weight: 600; letter-spacing: 0.03125rem; }
.status-badge {
  padding: var(--space-xs) var(--space-sm);
  border-radius: 1.25rem;
  font-size: var(--font-xs); font-weight: 500;
  background: var(--bg-deep);
  transition: all 0.3s;
}
.status-badge.active {
  background: var(--accent);
  animation: pulse 2s infinite;
}
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.7; } }

/* === Tabs === */
.tabs {
  display: flex;
  background: var(--bg-card);
  border-bottom: 1px solid var(--bg-deep);
}
.tab {
  flex: 1;
  padding: var(--space-sm) var(--space-xs);
  text-align: center;
  font-size: var(--font-sm); font-weight: 500;
  min-height: var(--touch-min);
  color: var(--fg-muted);
  cursor: pointer;
  border-bottom: 3px solid transparent;
  transition: all 0.2s;
}
.tab.active { color: var(--fg); border-bottom-color: var(--accent); }
.tab-share { margin-left: auto; font-size: var(--font-lg); }
.tab .tab-ip {
  display: block;
  font-size: var(--font-xs);
  opacity: 0.7;
  margin-top: 0.125rem;
}
.nvr-web-link {
  text-decoration: none;
  color: var(--fg-link);
  border: 1px dashed #7a8a9a !important;
  background: transparent !important;
}
.nvr-web-link:hover { border-color: var(--accent) !important; color: #fff; }

/* === Tab content === */
.tab-content { display: none; }
.tab-content.active { display: block; }

/* === Grid buttons === */
.grid-btn-wrap {
  padding: var(--space-sm);
  display: flex; gap: var(--space-xs);
  overflow-x: auto;
}
.grid-btn {
  flex: 1 0 3.75rem;
  padding: var(--space-sm) 0.125rem;
  border: 2px solid var(--bg-deep);
  border-radius: var(--radius);
  background: var(--bg-card);
  color: var(--fg);
  font-size: var(--font-xs); font-weight: 600;
  min-height: var(--touch-min);
  cursor: pointer;
  transition: all 0.2s;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 0.1875rem;
}
.grid-btn:active { transform: scale(0.97); background: var(--bg-deep); }
.grid-btn.current { border-color: var(--ok); color: var(--ok); }
.grid-btn svg { width: 1.125rem; height: 1.125rem; }
.grid-btn .grid-label { font-size: var(--font-xs); opacity: 0.7; }

/* === Sekcje === */
.section-title {
  padding: var(--space-sm) var(--space-md) var(--space-xs);
  font-size: var(--font-sm); font-weight: 600;
  text-transform: uppercase; letter-spacing: 0.0625rem;
  color: var(--fg-dim);
  display: flex; align-items: center; gap: var(--space-xs);
}
.help-icon {
  display: inline-flex; align-items: center; justify-content: center;
  width: 2rem; height: 2rem;
  border-radius: 50%;
  background: var(--bg-deep);
  color: var(--fg-muted);
  font-size: var(--font-sm); font-weight: 700;
  cursor: pointer; flex-shrink: 0;
  transition: background 0.2s;
  min-width: var(--touch-min); min-height: var(--touch-min);
}
.help-icon:hover { background: var(--accent); color: #fff; }
.help-text {
  display: none;
  padding: var(--space-sm) var(--space-md);
  margin: 0 var(--space-md) var(--space-sm);
  font-size: var(--font-sm);
  line-height: 1.5;
  color: var(--fg-sub);
  background: var(--bg-deep);
  border-radius: var(--radius);
}
.help-text.show { display: block; }

/* === Keypad === */
.kp-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: var(--space-sm);
  max-width: clamp(16rem, 40vw, 20rem);
  margin: 0 auto;
}
.kp-btn {
  padding: var(--space-lg) 0;
  border: 2px solid var(--bg-deep);
  border-radius: var(--radius-lg);
  background: var(--bg-card);
  color: var(--fg);
  font-size: var(--font-xl);
  font-weight: 700;
  cursor: pointer;
  transition: all 0.15s;
  -webkit-tap-highlight-color: transparent;
  min-height: var(--touch-min);
}
.kp-btn:active { transform: scale(0.95); background: var(--bg-deep); }
.kp-fn { font-size: var(--font-base); font-weight: 600; color: #0ff; }
.help-text .hl { color: var(--accent); font-weight: 600; }
.help-text .dot-red {
  display: inline-block; width: 0.375rem; height: 0.375rem;
  border-radius: 50%; background: var(--accent);
  vertical-align: middle; margin: 0 0.125rem;
}

/* === Legenda badge === */
.legend-bar {
  padding: var(--space-xs) var(--space-md);
  display: flex; gap: var(--space-sm); flex-wrap: wrap;
  font-size: var(--font-xs); color: var(--fg-muted);
}
.legend-item { display: flex; align-items: center; gap: var(--space-xs); }
.legend-dot { width: 0.375rem; height: 0.375rem; border-radius: 50%; }
.legend-dot.red { background: var(--accent); }
.legend-dot.green { background: var(--ok); }

/* === Tabs dolne (Pilot / Ustawienia) === */
.bottom-tabs {
  display: flex;
  background: var(--bg-card);
  border-top: 1px solid var(--bg-deep);
  position: fixed; bottom: 0; left: 0; right: 0;
  z-index: 10;
}
.bottom-tab {
  flex: 1;
  padding: var(--space-sm) var(--space-xs);
  text-align: center;
  font-size: var(--font-xs); font-weight: 500;
  min-height: var(--touch-min);
  color: var(--fg-muted);
  cursor: pointer;
  border-top: 3px solid transparent;
  transition: all 0.2s;
}
.bottom-tab.active { color: var(--fg); border-top-color: var(--accent); }
.bottom-tab svg { display: block; margin: 0 auto 0.1875rem; width: 1.25rem; height: 1.25rem; }
.main-content { padding-bottom: 3.75rem; }

/* === Ustawienia === */
.settings-page { display: none; padding: var(--space-sm) var(--space-md) 5rem; }
.settings-page.active { display: block; }
.settings-section {
  margin-bottom: var(--space-md);
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: var(--space-sm) var(--space-md);
}
.settings-group-header {
  cursor: pointer; user-select: none;
  background: #0f2847; border: 1px solid #1a3a5c;
  margin-bottom: 0; border-radius: var(--radius);
  transition: background 0.15s;
}
.settings-group-header:hover { background: #142f52; }
.settings-group-header h3 {
  font-size: var(--font-base); margin-bottom: 0;
  justify-content: space-between;
}
.grp-arrow { font-size: var(--font-sm); transition: transform 0.2s; }
.grp-arrow.collapsed { transform: rotate(-90deg); }
.settings-group { overflow: hidden; transition: max-height 0.3s ease; }
.settings-group.collapsed { max-height: 0 !important; overflow: hidden; }
.settings-section h3 {
  font-size: var(--font-xs); font-weight: 600;
  color: var(--fg-muted); text-transform: uppercase;
  letter-spacing: 0.03125rem;
  margin-bottom: var(--space-sm);
  display: flex; align-items: center; gap: var(--space-xs);
}
.settings-section h3 .help-icon { font-size: var(--font-xs); }
.setting-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: var(--space-sm) 0;
  border-bottom: 1px solid rgba(15,52,96,0.25);
}
.setting-row:last-child { border-bottom: none; }
.setting-label { font-size: var(--font-sm); }
.setting-label small { display: block; font-size: var(--font-xs); color: var(--fg-dim); margin-top: 0.125rem; }

/* === Toggle switch === */
.toggle { position: relative; width: 2.75rem; height: 1.5rem; flex-shrink: 0; }
.toggle input { opacity: 0; width: 0; height: 0; }
.toggle .slider {
  position: absolute; top: 0; left: 0; right: 0; bottom: 0;
  background: #3a4a5a; border-radius: 0.75rem;
  cursor: pointer; transition: background 0.3s;
}
.toggle .slider::before {
  content: ""; position: absolute;
  width: 1.125rem; height: 1.125rem;
  left: 0.1875rem; bottom: 0.1875rem;
  background: #fff; border-radius: 50%;
  transition: transform 0.3s;
}
.toggle input:checked + .slider { background: var(--ok); }
.toggle input:checked + .slider::before { transform: translateX(1.25rem); }

/* === Input numeryczny === */
.setting-input {
  width: 4.375rem;
  padding: var(--space-xs) var(--space-sm);
  border: 1px solid var(--bg-deep);
  border-radius: var(--radius);
  background: var(--bg-base);
  color: var(--fg);
  font-size: var(--font-sm);
  text-align: right;
  min-height: var(--touch-min);
}
.setting-input:focus { outline: none; border-color: var(--accent); }
.settings-save {
  width: 100%;
  padding: var(--space-sm);
  min-height: var(--touch-min);
  border: none; border-radius: var(--radius);
  background: var(--accent);
  color: #fff;
  font-size: var(--font-base); font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}
.settings-save:active { background: var(--danger); }

/* === Sekwencje drag & drop === */
.sekw-list { display: flex; flex-direction: column; gap: var(--space-xs); }
.sekw-item {
  display: flex; align-items: center; gap: var(--space-xs);
  background: #1e293b; padding: var(--space-xs) var(--space-sm); border-radius: var(--radius);
}
.sekw-drag { cursor: grab; font-size: var(--font-sm); color: #b0bec5; }
.sekw-name { flex: 1; font-size: var(--font-xs); min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sekw-item .btn-del {
  background: none; border: none; color: #ef4444; font-size: var(--font-lg);
  cursor: pointer; padding: 0 var(--space-xs); min-height: var(--touch-min);
  min-width: var(--touch-min); display: flex; align-items: center; justify-content: center;
}

/* === Siatka kamer === */
.cam-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: var(--space-sm);
  padding: var(--space-sm) var(--space-md) var(--space-md);
}
.cam-btn {
  position: relative;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  padding: var(--space-md) var(--space-xs);
  border: 1px solid var(--bg-deep);
  border-radius: var(--radius);
  background: var(--bg-card);
  color: #ddd;
  cursor: pointer;
  transition: all 0.2s;
  min-height: 4.5rem;
}
.cam-btn:active { transform: scale(0.95); background: var(--bg-deep); }
.cam-btn.active { border-color: var(--accent); background: var(--accent); color: #fff; }
.cam-btn .ch-num { font-size: var(--font-xs); font-weight: 700; opacity: 0.7; margin-bottom: 0.125rem; }
.cam-btn .ch-name { font-size: var(--font-xs); font-weight: 500; text-align: center; line-height: 1.2; }
.cam-btn .ch-blocked {
  position: absolute;
  top: var(--space-xs); right: var(--space-xs);
  width: 0.375rem; height: 0.375rem;
  border-radius: 50%;
  background: var(--accent);
}

/* === Loading overlay === */
.loading {
  display: none; position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.6);
  z-index: 100;
  align-items: center; justify-content: center;
}
.loading.show { display: flex; }
.spinner {
  width: 3rem; height: 3rem;
  border: 0.25rem solid #6a7a8a;
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* === Toast === */
.toast {
  position: fixed;
  bottom: 1.875rem; left: 50%;
  transform: translateX(-50%) translateY(6.25rem);
  padding: var(--space-sm) var(--space-lg);
  border-radius: var(--radius);
  font-size: var(--font-base); font-weight: 500;
  background: #3a4a5a;
  color: #fff;
  transition: transform 0.3s;
  z-index: 200;
  white-space: nowrap;
}
.toast.show { transform: translateX(-50%) translateY(0); }
.toast.error { background: var(--danger); }
.toast.success { background: #27ae60; }

/* === Focus === */
:focus-visible { outline: 2px solid #2ecc71; outline-offset: 2px; }

/* === WCAG: reduced motion ===
   Wylacz animacje dla uzytkownikow z preferencja reduced-motion */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* === WCAG: skip-link === */
.skip-link {
  position: absolute; top: -100%; left: 50%; transform: translateX(-50%);
  background: #fff; color: #000; padding: 0.5rem 1rem; z-index: 9999;
  border-radius: 0 0 0.25rem 0.25rem; font-weight: bold;
  text-decoration: none;
}
.skip-link:focus { top: 0; }
