:root {
  --bg: #15171c;
  --bg2: #1d2027;
  --card: #23262e;
  --line: #333844;
  --text: #e8eaef;
  --muted: #9aa1ad;
  --accent: #2f6df6;
  --accent-ink: #ffffff;
  --danger: #e5484d;
  --warn: #f5b301;
  --ok: #2fb36d;
  --field: #111318;
  --shadow: 0 10px 30px rgb(0 0 0 / .35);
  color-scheme: dark;
}
:root[data-theme="light"] {
  --bg: #f3f4f7;
  --bg2: #ffffff;
  --card: #ffffff;
  --line: #dcdfe6;
  --text: #1b1e24;
  --muted: #5f6673;
  --field: #ffffff;
  --shadow: 0 10px 30px rgb(20 30 60 / .12);
  color-scheme: light;
}
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #f3f4f7; --bg2: #ffffff; --card: #ffffff; --line: #dcdfe6;
    --text: #1b1e24; --muted: #5f6673; --field: #ffffff;
    --shadow: 0 10px 30px rgb(20 30 60 / .12);
    color-scheme: light;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
}
[hidden] { display: none !important; }
a { color: var(--accent); }
h1, h2, h3 { line-height: 1.2; }
.muted { color: var(--muted); }
.small, small { font-size: 12.5px; }
.center { text-align: center; }
.error { color: var(--danger); min-height: 1.2em; margin: 8px 0 0; }

/* przyciski */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--line); background: var(--card); color: var(--text);
  padding: 8px 14px; border-radius: 8px; font: inherit; cursor: pointer; text-decoration: none;
}
.btn:hover { filter: brightness(1.08); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.small { padding: 5px 10px; font-size: 13.5px; }
.btn.big { padding: 12px 22px; font-size: 16px; width: 100%; }
.btn:disabled { opacity: .6; cursor: progress; }
.icon-btn {
  position: relative; border: 0; background: transparent; color: var(--text);
  width: 36px; height: 36px; border-radius: 8px; font-size: 20px; cursor: pointer;
}
.icon-btn:hover { background: var(--line); }
.chip {
  border: 1px solid var(--line); background: var(--bg2); color: var(--text);
  border-radius: 999px; padding: 3px 10px; font: inherit; font-size: 13px; cursor: pointer;
  max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.chip.warn { background: var(--warn); border-color: var(--warn); color: #1b1e24; font-weight: 600; padding: 1px 8px; }
.badge {
  display: inline-block; min-width: 18px; padding: 0 5px; border-radius: 9px;
  background: var(--danger); color: #fff; font-size: 11.5px; line-height: 18px; text-align: center;
}
.icon-btn .badge { position: absolute; top: -2px; right: -4px; }
.tag { font-size: 11px; padding: 0 6px; border-radius: 6px; background: var(--accent); color: #fff; }

/* pola */
.field { display: flex; flex-direction: column; gap: 5px; font-size: 13.5px; color: var(--muted); }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--field);
  border: 1px solid var(--line); border-radius: 8px; padding: 9px 11px; width: 100%;
}
input:focus, select:focus, textarea:focus, .btn:focus-visible, .icon-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.inline { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.inline input { flex: 1; min-width: 160px; }
.check { display: flex; gap: 10px; align-items: flex-start; font-size: 13.5px; color: var(--muted); margin: 14px 0; }
.check input { width: auto; margin-top: 3px; }

/* przełączniki */
.switch-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 22px; }
.switch { display: flex; gap: 10px; align-items: flex-start; cursor: pointer; }
.switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switch .track {
  flex: none; width: 38px; height: 22px; border-radius: 11px; background: var(--line);
  position: relative; transition: background .15s; margin-top: 1px;
}
.switch .track::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%;
  background: #fff; transition: transform .15s;
}
.switch input:checked + .track { background: var(--accent); }
.switch input:checked + .track::after { transform: translateX(16px); }
.switch input:focus-visible + .track { outline: 2px solid var(--accent); outline-offset: 2px; }
.switch .lbl { display: flex; flex-direction: column; }
.switch .lbl small { color: var(--muted); }

/* górny pasek */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px; background: var(--bg2); border-bottom: 1px solid var(--line);
}
.logo { display: inline-flex; align-items: center; gap: 8px; color: var(--text); text-decoration: none; font-weight: 700; font-size: 18px; }

/* strona główna */
.home-main { max-width: 860px; margin: 0 auto; padding: 28px 16px 60px; }
.hero { text-align: center; margin: 10px 0 26px; }
.hero h1 { font-size: clamp(26px, 4vw, 36px); margin: 0 0 10px; }
.hero p { color: var(--muted); max-width: 620px; margin: 0 auto; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow); }
.create-card { padding: 22px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px 18px; }
.switches { border: 1px solid var(--line); border-radius: 10px; padding: 14px 16px 16px; margin: 18px 0; }
.switches legend { padding: 0 6px; color: var(--muted); font-size: 13.5px; }
.drop {
  border: 2px dashed var(--line); border-radius: 12px; padding: 26px 16px; text-align: center;
  color: var(--muted); cursor: pointer; display: flex; flex-direction: column; gap: 4px;
}
.drop strong { color: var(--text); }
.drop.over, .drop:hover { border-color: var(--accent); }
.picked { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.pick { position: relative; width: 84px; height: 84px; border-radius: 8px; overflow: hidden; border: 1px solid var(--line); }
.pick img { width: 100%; height: 100%; object-fit: cover; }
.pick .x {
  position: absolute; top: 3px; right: 3px; width: 22px; height: 22px; border-radius: 50%; border: 0;
  background: rgb(0 0 0 / .65); color: #fff; cursor: pointer; line-height: 1;
}
.actions { margin-top: 6px; }

/* ekran wejścia */
.gate { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.gate-card { width: min(420px, 100%); padding: 24px; display: flex; flex-direction: column; gap: 10px; }
.gate-card h1 { font-size: 22px; margin: 10px 0 0; overflow-wrap: anywhere; }
.gate-card form { display: flex; flex-direction: column; gap: 12px; }

/* pokój */
body.room { overflow: hidden; }
.app { height: 100dvh; display: grid; grid-template-rows: auto 1fr; }
.room-head {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 8px 14px; background: var(--bg2); border-bottom: 1px solid var(--line); min-width: 0;
}
.room-title { display: flex; flex-direction: column; min-width: 0; }
.room-title strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.timer { display: flex; align-items: center; gap: 8px; font-variant-numeric: tabular-nums; font-size: 13.5px; color: var(--muted); }
.timer .soon { color: var(--danger); font-weight: 600; }
.extend { display: inline-flex; gap: 4px; }
.head-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.conn { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); }
.conn.on { background: var(--ok); }
.conn.off { background: var(--danger); }
.conn.wait { background: var(--warn); }

.room-body { display: grid; grid-template-columns: 1fr 360px; min-height: 0; }
.gallery { overflow: auto; padding: 12px; min-width: 0; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 10px; }
.empty { text-align: center; padding: 60px 16px; }

.ph {
  position: relative; display: block; padding: 0; border: 1px solid var(--line); border-radius: 10px;
  overflow: hidden; background: var(--bg2); aspect-ratio: 1 / 1; cursor: zoom-in; color: #fff;
}
.ph img { width: 100%; height: 100%; object-fit: cover; display: block; }
.ph-top, .ph-bottom {
  position: absolute; left: 0; right: 0; display: flex; justify-content: space-between; gap: 6px;
  padding: 5px 8px; font-size: 12.5px; text-shadow: 0 1px 2px #000;
}
.ph-top { top: 0; background: linear-gradient(rgb(0 0 0 / .55), transparent); }
.ph-bottom { bottom: 0; background: linear-gradient(transparent, rgb(0 0 0 / .7)); }
.ph-author { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ph:hover { border-color: var(--accent); }

.side { border-left: 1px solid var(--line); background: var(--bg2); display: flex; flex-direction: column; min-height: 0; }
.tabs { display: flex; gap: 6px; padding: 8px; border-bottom: 1px solid var(--line); }
.tab {
  flex: 1; border: 0; background: transparent; color: var(--muted); font: inherit; padding: 8px;
  border-radius: 8px; cursor: pointer;
}
.tab.active { background: var(--card); color: var(--text); }
.pane { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.msgs { flex: 1; overflow: auto; padding: 8px; display: flex; flex-direction: column; gap: 6px; min-height: 0; }
.msg { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 7px 10px; }
.msg.mine { border-color: color-mix(in srgb, var(--accent) 45%, var(--line)); }
.msg-head { display: flex; align-items: center; gap: 7px; font-size: 12.5px; }
.msg-author { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg-head time { color: var(--muted); margin-left: auto; }
.msg-del { border: 0; background: transparent; color: var(--muted); cursor: pointer; font-size: 16px; padding: 0 2px; }
.msg-del:hover { color: var(--danger); }
.msg-text { white-space: pre-wrap; overflow-wrap: anywhere; margin-top: 3px; }
.msg-ref {
  display: flex; align-items: center; gap: 8px; margin-top: 6px; padding: 0; border: 0; background: none;
  color: var(--muted); font: inherit; font-size: 12px; cursor: pointer;
}
.msg-ref img { width: 38px; height: 38px; object-fit: cover; border-radius: 6px; }
.avatar {
  flex: none; width: 24px; height: 24px; border-radius: 50%; color: #fff; display: inline-grid;
  place-items: center; font-size: 10.5px; font-weight: 700;
}
.composer { display: flex; gap: 6px; padding: 8px; border-top: 1px solid var(--line); align-items: flex-end; }
.composer textarea { resize: none; min-height: 40px; max-height: 140px; overflow-y: hidden; }
.send {
  flex: none; width: 40px; height: 40px; border-radius: 8px; border: 0; background: var(--accent);
  color: #fff; font-size: 17px; cursor: pointer;
}
.off-note { margin: 0; padding: 10px; text-align: center; color: var(--muted); font-size: 13px; border-top: 1px solid var(--line); }
.users { list-style: none; margin: 0; padding: 8px; overflow: auto; }
.users li { display: flex; align-items: center; gap: 8px; padding: 7px 6px; border-bottom: 1px solid var(--line); }

.fab {
  position: fixed; left: 20px; bottom: 20px; width: 58px; height: 58px; border-radius: 50%;
  border: 0; background: var(--accent); color: #fff; font-size: 24px; cursor: pointer; box-shadow: var(--shadow);
}
.drop-cover {
  position: fixed; inset: 0; background: rgb(20 60 160 / .45); display: grid; place-items: center;
  z-index: 40; pointer-events: none;
}
.drop-cover div { background: var(--card); padding: 20px 28px; border-radius: 12px; font-size: 18px; border: 2px dashed var(--accent); }

/* podgląd */
/* Okno podglądu: dopasowane do zdjęcia, najwyżej 80% szerokości i wysokości ekranu */
.lightbox {
  position: fixed; inset: 0; z-index: 30; display: grid; place-items: center;
  background: rgb(0 0 0 / .78); cursor: zoom-out;
}
.lb-box {
  --lb-side: 340px;
  display: flex; max-width: 80vw; max-height: 80vh; cursor: auto;
  background: var(--bg2); border: 1px solid var(--line); border-radius: 14px;
  overflow: hidden; box-shadow: 0 20px 60px rgb(0 0 0 / .5);
}
.lb-stage {
  position: relative; display: grid; place-items: center; background: #0d0e11;
  min-width: min(360px, 40vw); min-height: min(380px, 80vh);
}
.lb-stage img {
  display: block; object-fit: contain;
  max-width: calc(80vw - var(--lb-side) - 2px); max-height: calc(80vh - 2px);
}
.shield { display: none; position: absolute; inset: 0; }
.protect .shield { display: block; }
.protect .lb-stage .lb-nav, .protect .lb-pos { z-index: 2; }
.protect img { -webkit-user-drag: none; user-select: none; -webkit-touch-callout: none; }
.lb-nav {
  position: absolute; top: 50%; transform: translateY(-50%); width: 46px; height: 64px; border: 0; z-index: 2;
  background: rgb(255 255 255 / .12); color: #fff; font-size: 34px; cursor: pointer; border-radius: 8px;
}
.lb-nav:hover { background: rgb(255 255 255 / .25); }
.lb-nav.prev { left: max(8px, calc(10vw - 60px)); }
.lb-nav.next { right: max(8px, calc(10vw - 60px)); }
.lb-pos {
  position: absolute; bottom: 8px; left: 50%; transform: translateX(-50%); color: #ddd; font-size: 12.5px;
  background: rgb(0 0 0 / .45); padding: 1px 8px; border-radius: 9px; z-index: 2;
}
/* contain: size — panel komentarzy nie rozpycha okna; wysokość wyznacza zdjęcie */
.lb-side {
  width: var(--lb-side); flex: none; contain: size; background: var(--bg2);
  display: flex; flex-direction: column; border-left: 1px solid var(--line);
}
.lb-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 10px; border-bottom: 1px solid var(--line); }
.who { display: flex; align-items: center; gap: 8px; min-width: 0; }
.who div { display: flex; flex-direction: column; min-width: 0; }
.who strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-tools { display: flex; align-items: center; gap: 6px; }

/* okna */
.modal {
  width: min(640px, calc(100% - 32px)); border: 1px solid var(--line); border-radius: 14px;
  background: var(--card); color: var(--text); padding: 18px 20px 22px; box-shadow: var(--shadow);
}
.modal::backdrop { background: rgb(0 0 0 / .6); }
.modal-head { display: flex; align-items: center; justify-content: space-between; margin: 0; }
.modal h2 { margin: 0; font-size: 20px; }
.modal h3 { font-size: 15px; margin: 20px 0 8px; }
.modal .field { margin-top: 12px; }

#toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 30px); opacity: 0; z-index: 60;
  background: var(--card); color: var(--text); border: 1px solid var(--line); border-radius: 10px;
  padding: 10px 16px; box-shadow: var(--shadow); max-width: min(560px, calc(100% - 32px));
  transition: opacity .2s, transform .2s; pointer-events: none;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.bad { border-color: var(--danger); }

.only-mobile { display: none; }

/* okno pytania (zamiast systemowego confirm/prompt) */
.modal.ask { width: min(420px, calc(100% - 32px)); padding: 20px; }
.ask-title { font-size: 18px; margin: 0 0 8px; }
.ask-text { margin: 0 0 16px; color: var(--muted); white-space: pre-line; }
.ask input { margin-bottom: 16px; }
.ask-actions { display: flex; justify-content: flex-end; gap: 8px; }
@media (max-width: 600px) { .ask-actions .btn { flex: 1; padding: 11px; } }

/* panel kluczy */
.admin .card + .card, .admin #panel .card { margin-bottom: 16px; }
.card-title { font-size: 17px; margin: 0 0 12px; }
.list-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.small-check { margin: 0; }
.fresh { margin-top: 14px; padding: 12px; border: 1px dashed var(--accent); border-radius: 10px; }
.key-box { font: 600 18px/1.2 ui-monospace, Consolas, monospace; letter-spacing: 1px; text-align: center; }
.fresh .inline .key-box { flex: 1 1 100%; min-width: 0; }
.fresh .inline .btn { flex: 1 1 100%; }
@media (max-width: 600px) {
  .admin #newKey .inline input { flex: 1 1 100%; }
  .admin #newKey .inline .btn { flex: 1 1 100%; }
}
.keys { display: flex; flex-direction: column; gap: 8px; }
.key-row { border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; display: grid; gap: 6px; }
.key-row.revoked { opacity: .6; }
.key-main { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; overflow-wrap: anywhere; }
.key-meta { display: flex; flex-wrap: wrap; gap: 4px 14px; }
.key-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.tag.ok { background: var(--ok); }
.tag.bad { background: var(--danger); }

@media (max-width: 860px) {
  .room-body { grid-template-columns: 1fr; }
  .side {
    position: fixed; inset: 0; z-index: 20; border-left: 0; transform: translateX(100%);
    transition: transform .2s;
  }
  .side.open { transform: none; }
  .only-mobile { display: inline-block; }
  .tabs .only-mobile { flex: none; }
  .lb-box { flex-direction: column; max-width: 96vw; max-height: 92vh; }
  .lb-stage { min-width: min(300px, 96vw); min-height: 180px; }
  .lb-stage img { max-width: calc(96vw - 2px); max-height: 55vh; }
  .lb-side { width: auto; height: 36vh; border-left: 0; border-top: 1px solid var(--line); }
  .lb-nav { top: auto; bottom: 12px; transform: none; height: 48px; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); }
  #ctrlBtn { font-size: 0; padding: 6px 9px; }
  #ctrlBtn::before { content: "⚙"; font-size: 16px; }
}
@media (max-width: 600px) {
  .grid2, .switch-grid { grid-template-columns: 1fr; }
  .home-main { padding: 16px 12px 40px; }
  .create-card { padding: 16px; }
  .hero { margin: 4px 0 16px; }

  /* nagłówek pokoju w jednym rzędzie: tytuł + czas po lewej, ikony po prawej */
  .room-head { padding: 6px 8px 6px 12px; gap: 6px; }
  .room-title { flex: 1; }
  .timer { font-size: 12.5px; gap: 6px; }
  .extend, .room-head #theme, #meBtn { display: none !important; }
  .head-actions { flex-wrap: nowrap; gap: 4px; }
  #shareBtn, #ctrlBtn { font-size: 0; width: 40px; height: 40px; padding: 0; }
  #shareBtn::before { content: "🔗"; font-size: 17px; }
  #ctrlBtn::before { font-size: 18px; }
  .icon-btn { width: 40px; height: 40px; }

  .gallery { padding: 8px 8px 90px; }
  .grid { gap: 6px; }
  .fab { left: 16px; bottom: calc(16px + env(safe-area-inset-bottom)); }
  #toast { bottom: calc(88px + env(safe-area-inset-bottom)); }

  .modal { width: calc(100% - 16px); max-height: 90dvh; padding: 14px 14px 18px; }
  .inline .btn { flex: 1; }
}

/* ekrany dotykowe: większe cele, brak opóźnienia dotknięcia, bez powiększania przy pisaniu (iPhone) */
@media (hover: none), (pointer: coarse) {
  input, select, textarea { font-size: 16px; }
  button, .btn, .ph, .tab, .chip { touch-action: manipulation; }
  .msg-del { min-width: 32px; min-height: 28px; font-size: 20px; }
  .lb-tools .icon-btn, #sideClose { width: 44px; height: 44px; font-size: 26px; }
  .tab { padding: 11px 8px; }
  .send { width: 46px; height: 46px; }
  .only-mouse { display: none !important; }
  .lb-nav { display: none !important; }
}
@media (hover: hover) and (pointer: fine) {
  .only-touch { display: none !important; }
}
.composer { padding-bottom: calc(8px + env(safe-area-inset-bottom)); }
.users .me-name { margin: 8px; }
.tag-a { font-weight: 700; padding: 0 5px; min-width: 16px; text-align: center; cursor: help; }
.icon-btn.rot { font-size: 21px; }
.icon-btn.rot:disabled { opacity: .4; cursor: progress; }
.key-meta .flag { border: 1px solid var(--line); border-radius: 6px; padding: 0 6px; color: var(--muted); }

/* wideo */
.ph.is-video img { filter: brightness(.92); }
.ph-play { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); width: 46px; height: 46px; border-radius: 50%;
  background: rgb(0 0 0 / .55); color: #fff; display: grid; place-items: center; font-size: 20px; padding-left: 3px; pointer-events: none; }
.ph-dur { position: absolute; right: 8px; bottom: 26px; background: rgb(0 0 0 / .65); color: #fff; font-size: 12px; padding: 0 6px; border-radius: 6px; }
.ph-wait { position: absolute; inset: 0; display: grid; place-items: center; padding: 12px; text-align: center; color: var(--muted); font-size: 13px; }
.ph-wait.bad { color: var(--danger); }
#lbVideo { display: block; max-width: calc(80vw - var(--lb-side) - 2px); max-height: calc(80vh - 2px); background: #000; }
.lb-wait { color: #ddd; padding: 30px; text-align: center; }
.lb-stage { overflow: hidden; }
.lb-wm { position: absolute; inset: -60%; pointer-events: none; transform: rotate(-28deg); background-repeat: repeat; z-index: 1; }
.lb-stage.is-video .shield { display: none !important; }
.pick-video { display: grid; place-items: center; width: 100%; height: 100%; font-size: 28px; background: var(--bg2); }
.pick-video small { font-size: 11px; color: var(--muted); }
@media (max-width: 860px) { #lbVideo { max-width: calc(96vw - 2px); max-height: 55vh; } }.lb-stage.is-video .lb-pos { top: 8px; bottom: auto; }
.time-rows { display: grid; grid-template-columns: auto 1fr; gap: 8px 12px; align-items: center; }
.btn.minus { border-color: color-mix(in srgb, var(--danger) 45%, var(--line)); }
@media (max-width: 600px) { .time-rows { grid-template-columns: 1fr; gap: 4px; } .time-rows .inline { margin-bottom: 6px; } }
