:root {
  --blue: #146eb4;
  --blue-dark: #0f4c81;
  --orange: #ff9900;
  --bg: #f3f4f6;
  --panel: #ffffff;
  --line: #d8dee4;
  --text: #1f2933;
  --muted: #6b7280;
  --ok: #2f855a;
  --err: #b42318;
  --appbar: #146eb4;
  --danger-press: #e54f3f;
  --press: #eef1f4;
  --accent-soft: #eef6fd;
  --accent-tint: #e7f1fb;
  --input-bg: #ffffff;
  --panel-2: #fbfcfd;
  --ok-bg: #edfdf4;
  --err-bg: #fef3f2;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: "Segoe UI", Roboto, -apple-system, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  padding-bottom: env(safe-area-inset-bottom, 0px);
}

/* ---------- App bar ---------- */

.topbar {
  position: sticky; top: 0; z-index: 500;
  background: var(--appbar); color: #fff;
  padding-top: env(safe-area-inset-top, 0px);
  box-shadow: 0 1px 0 rgba(0,0,0,.12);
}
.topbar-inner {
  display: flex; align-items: center; gap: 10px;
  max-width: 1100px; margin: 0 auto; padding: 8px 12px;
  min-height: 54px;
}
.topbar h1 { margin: 0; font-size: 19px; font-weight: 700; color: #fff; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.muted-inline { font-weight: 400; opacity: .75; font-size: 14px; }
.menu-btn {
  width: 44px; height: 44px; flex: 0 0 44px;
  border: none; border-radius: 10px; background: rgba(255,255,255,.14);
  cursor: pointer; display: grid; place-items: center; padding: 0;
}
.menu-btn svg { width: 24px; height: 24px; stroke: #fff; fill: none; stroke-width: 2; stroke-linecap: round; }
.menu-btn:active { background: rgba(255,255,255,.28); }
.btn-auth {
  border: 1px solid rgba(255,255,255,.55); background: transparent; color: #fff;
  border-radius: 10px; min-height: 40px; padding: 8px 12px; font-size: 13px; font-weight: 600;
}
.btn-auth.btn-authenticated { border-color: #7ee2b8; color: #d9fbe9; }

/* Zakładki — ukryte na mobile (obsługuje je drawer), widoczne na desktopie */
.tabs { display: none; }

/* ---------- Drawer (menu rozwijane) ---------- */

.drawer {
  position: fixed; top: calc(env(safe-area-inset-top, 0px) + 54px); left: 0; right: 0;
  background: var(--panel); color: var(--text);
  box-shadow: 0 18px 44px rgba(15,23,42,.30);
  border-radius: 0 0 18px 18px; padding: 6px 10px calc(10px + env(safe-area-inset-bottom, 0px));
  z-index: 490; max-height: calc(100vh - 70px); overflow-y: auto;
}
.drawer[hidden] { display: none; }
.drawer-section {
  margin: 12px 10px 4px; font-size: 11px; font-weight: 700;
  text-transform: uppercase; letter-spacing: .08em; color: var(--muted);
}
.drawer-item {
  display: flex; align-items: center; gap: 14px;
  width: 100%; min-height: 50px; padding: 10px 12px;
  border: none; background: none; border-radius: 12px;
  font-size: 16px; font-weight: 600; color: var(--text);
  cursor: pointer; text-decoration: none; text-align: left;
}
.drawer-item:active { background: var(--accent-soft); }
.drawer-item.active { background: var(--accent-tint); color: var(--blue); }
.di-ico { font-size: 20px; width: 26px; text-align: center; flex: 0 0 26px; }
.drawer-backdrop {
  position: fixed; inset: 0; z-index: 480; background: rgba(15,23,42,.35);
}
.drawer-backdrop[hidden] { display: none; }

/* ---------- Layout — mobile: pełny ekran, bez ramek ---------- */

main { max-width: none; margin: 0; padding: 0; }
.tab-panel { display: none; }
.tab-panel.active { display: block; }

.card {
  background: var(--panel); border: none; border-radius: 0;
  padding: 16px 14px 18px; margin: 0 0 10px;
  box-shadow: 0 1px 0 var(--line);
}
.card h2 { margin: 0 0 8px; font-size: 17px; }
.card-head { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 8px; }
.card-head .btn { margin: 2px; }
.muted { color: var(--muted); font-size: 13px; }
.error { color: var(--err); font-size: 13px; }

/* ---------- Przyciski ---------- */

.btn {
  display: inline-flex; align-items: center; justify-content: center;
  border: 1px solid var(--line); border-radius: 12px;
  background: var(--panel); color: var(--text);
  min-height: 44px; padding: 10px 16px;
  font-size: 15px; font-weight: 600; cursor: pointer; text-decoration: none;
}
.btn:active { background: var(--press); }
.btn:disabled { opacity: .55; cursor: default; }
.btn-primary { background: var(--blue); border-color: var(--blue); color: #fff; }
.btn-primary:active { background: var(--blue-dark); }
.btn-secondary { border-color: var(--blue); color: var(--blue); }
.btn-link { border-color: transparent; color: var(--blue); }
.btn-danger { border-color: var(--err); color: var(--err); }
.btn-small { padding: 6px 12px; min-height: 36px; font-size: 13px; border-radius: 9px; }

/* ---------- Formularz ---------- */

.grid { display: grid; grid-template-columns: 1fr; gap: 12px; margin-bottom: 12px; }
.field { display: flex; flex-direction: column; gap: 5px; }
.field span { font-size: 12.5px; font-weight: 700; color: var(--muted); }
.field input {
  width: 100%; min-height: 46px; padding: 11px 12px;
  border: 1px solid var(--line); border-radius: 12px;
  font-size: 16px; font-family: inherit; background: var(--input-bg);
}
.field input:focus { outline: 2px solid var(--blue); outline-offset: -1px; }

.input-action { display: flex; gap: 8px; align-items: stretch; }
.input-action input { flex: 1; }
.icon-btn {
  flex: 0 0 48px; width: 48px; min-height: 46px;
  border: 1px solid var(--line); border-radius: 12px;
  background: var(--panel); cursor: pointer; display: grid; place-items: center; padding: 0;
}
.icon-btn svg { width: 22px; height: 22px; stroke: var(--blue); fill: none; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }
.mic.recording {
  background: var(--danger-press); border-color: var(--danger-press);
  animation: mic-pulse 1.2s ease-in-out infinite;
}
.mic.recording svg { stroke: #fff; }
@keyframes mic-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(229,79,63,.45); }
  50% { box-shadow: 0 0 0 10px rgba(229,79,63,0); }
}

.actions-row { display: flex; align-items: center; gap: 10px; margin: 14px 0; flex-wrap: wrap; }
#qaSuggestBtn { flex: 1; }
#qaSubmit { width: 100%; min-height: 52px; font-size: 17px; }
#qaStatus, #qaSuggestStatus { width: 100%; }

/* ---------- Zdjęcia: duże przyciski Aparat / Galeria ---------- */

.photo-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin: 12px 0 8px; }
.photo-btn {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
  min-height: 104px; padding: 14px 10px;
  border: 1.5px dashed var(--line); border-radius: 16px;
  background: var(--panel); cursor: pointer;
}
.photo-btn:active { background: var(--accent-soft); border-color: var(--blue); }
.photo-btn-primary { border-style: solid; border-color: var(--blue); background: var(--blue); }
.photo-btn-primary .pi-label, .photo-btn-primary .pi-sub { color: #fff; }
.photo-btn-primary:active { background: var(--blue-dark); }
.photo-btn-primary svg { stroke: #fff; }
.pi-ico svg { width: 34px; height: 34px; stroke: var(--blue); fill: none; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.pi-label { font-size: 16px; font-weight: 700; color: var(--text); }
.pi-sub { font-size: 12px; color: var(--muted); }

.dropzone {
  border: 1.5px dashed var(--line); border-radius: 12px; padding: 10px;
  text-align: center; color: var(--muted); cursor: pointer; margin: 4px 0 8px;
  font-size: 12.5px;
}
.dropzone.dragover { border-color: var(--blue); background: var(--accent-soft); }

.checkbox-row {
  display: flex; align-items: center; gap: 12px;
  margin: 10px 0 2px; cursor: pointer; font-size: 15px; color: var(--text);
  min-height: 44px;
}
.checkbox-row input[type="checkbox"] { width: 22px; height: 22px; accent-color: var(--blue); cursor: pointer; flex: 0 0 22px; }

.preview { display: flex; flex-wrap: wrap; gap: 8px; margin: 10px 0; }
.preview .thumb { position: relative; width: 92px; height: 92px; }
.preview .thumb img { width: 100%; height: 100%; object-fit: cover; border-radius: 10px; border: 1px solid var(--line); }
.preview .thumb .remove {
  position: absolute; top: -7px; right: -7px; width: 26px; height: 26px;
  border-radius: 50%; border: none; background: var(--err); color: #fff;
  cursor: pointer; font-size: 14px; line-height: 1;
}
.preview .thumb .order {
  position: absolute; bottom: 4px; left: 4px; background: rgba(0,0,0,.65);
  color: #fff; font-size: 10px; padding: 1px 6px; border-radius: 6px;
}

/* ---------- Tabele ---------- */

.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; margin-top: 8px; }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { text-align: left; padding: 8px 9px; border-bottom: 1px solid var(--line); vertical-align: top; }
.table th { font-size: 11px; text-transform: uppercase; color: var(--muted); white-space: nowrap; }
.table td { font-size: 13.5px; }
.status-ok { color: var(--ok); font-weight: 600; }
.status-error { color: var(--err); font-weight: 600; }
.status-started { color: var(--orange); font-weight: 600; }

.status-cards { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin-top: 12px; }
.status-card { border: 1px solid var(--line); border-radius: 14px; padding: 14px 8px; text-align: center; background: var(--panel-2); }
.status-card .value { font-size: 26px; font-weight: 700; color: var(--blue); }
.status-card .label { font-size: 11.5px; color: var(--muted); }

.result {
  margin-top: 12px; padding: 12px; border-radius: 10px;
  background: var(--ok-bg); border: 1px solid var(--ok); font-size: 14px;
}
.result.error { background: var(--err-bg); border-color: var(--err); }
.result code { word-break: break-all; }

/* ---------- Modal ---------- */

.modal {
  position: fixed; inset: 0; z-index: 10000; display: grid; place-items: center;
  background: rgba(15,23,42,.62); padding: 20px;
}
.modal[hidden] { display: none; }
.modal-card {
  position: relative; width: min(390px, calc(100vw - 32px));
  border: 1px solid var(--line); border-radius: 18px; padding: 24px;
  background: var(--panel); box-shadow: 0 24px 70px rgba(15,23,42,.32);
}
.modal-card h2 { margin: 0 36px 8px 0; font-size: 20px; }
.modal-card label { display: block; margin-bottom: 6px; font-size: 12px; font-weight: 700; }
.modal-card input { width: 100%; padding: 12px; font-size: 16px; border: 1px solid var(--line); border-radius: 12px; background: var(--input-bg); color: var(--text); }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.modal-close {
  position: absolute; top: 12px; right: 12px; width: 36px; height: 36px;
  border-radius: 50%; border: 1px solid var(--line); background: var(--panel);
  cursor: pointer; font-size: 20px; line-height: 1;
}

/* ---------- Desktop (≥760px): klasyczny układ z zakładkami ---------- */

@media (min-width: 760px) {
  body { font-size: 14px; }
  .menu-btn { display: none; }
  .topbar { background: var(--appbar); }
  .topbar-inner { padding: 10px 20px; min-height: 56px; }
  .muted-inline { display: inline; }
  .tabs {
    display: flex; gap: 4px; max-width: 1100px; margin: 0 auto; padding: 0 20px;
  }
  .tab {
    border: none; background: none; padding: 10px 16px; cursor: pointer;
    font-size: 14px; font-weight: 600; color: rgba(255,255,255,.78);
    border-bottom: 3px solid transparent; border-radius: 0;
  }
  .tab.active { color: #fff; border-bottom-color: var(--orange); }
  main { max-width: 1100px; margin: 20px auto; padding: 0 16px; }
  .card {
    border: 1px solid var(--line); border-radius: 12px;
    padding: 18px 20px; margin-bottom: 16px; box-shadow: none;
  }
  .grid { grid-template-columns: 2fr 1fr; }
  .grid.grid-title { grid-template-columns: 5fr 2fr; }
  .grid:has(.field:nth-child(2):last-child):not(.grid-title) { grid-template-columns: 1fr 1fr; }
  .field input { min-height: 40px; font-size: 14px; padding: 9px 11px; border-radius: 8px; }
  .icon-btn { min-height: 40px; flex-basis: 44px; width: 44px; border-radius: 8px; }
  .btn { min-height: 38px; font-size: 13.5px; padding: 8px 14px; border-radius: 9px; }
  .btn-small { min-height: 30px; }
  #qaSuggestBtn { flex: 0 1 auto; }
  #qaSubmit { width: auto; min-height: 42px; font-size: 15px; padding: 10px 26px; }
  .photo-btn { min-height: 92px; }
  #qaStatus, #qaSuggestStatus { width: auto; }
  .status-cards { grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; }
  .field span { font-size: 12px; }
}

/* ---------- Własny aparat (pełny ekran, 1:1) ---------- */

.camera-view {
  position: fixed; inset: 0; z-index: 9000;
  background: #000; color: #fff;
  display: flex; flex-direction: column;
  padding: calc(env(safe-area-inset-top, 0px) + 10px) 14px calc(env(safe-area-inset-bottom, 0px) + 14px);
}
.camera-view[hidden] { display: none; }
.cv-top { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.cv-btn {
  width: 46px; height: 46px; border: none; border-radius: 50%;
  background: rgba(255,255,255,.16); cursor: pointer; display: grid; place-items: center; padding: 0;
}
.cv-btn svg { width: 22px; height: 22px; stroke: #fff; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.cv-btn:active { background: rgba(255,255,255,.32); }
.cv-btn.torch-on { background: #ffcc00; }
.cv-btn.torch-on svg { stroke: #1f2933; }
.cv-counter {
  font-size: 14px; font-weight: 700; padding: 7px 14px; border-radius: 999px;
  background: rgba(255,255,255,.14);
}
.cv-stage {
  position: relative; flex: 1; display: grid; place-items: center; min-height: 0;
  margin: 10px 0;
}
.cv-square {
  position: relative; aspect-ratio: 1 / 1;
  width: min(100%, calc(100dvh - 300px));
  max-width: 100%; overflow: hidden; border-radius: 4px;
  background: #111;
}
#cvVideo {
  position: absolute; inset: 0; width: 100%; height: 100%;
  object-fit: cover; transform-origin: center;
  transition: transform .12s ease-out;
}
.cv-guide {
  position: absolute; inset: 0; pointer-events: none;
  border: 1px solid rgba(255,255,255,.55); border-radius: 4px;
  background:
    linear-gradient(to right, transparent calc(33.33% - 1px), rgba(255,255,255,.28) 33.33%, transparent calc(33.33% + 1px)),
    linear-gradient(to right, transparent calc(66.66% - 1px), rgba(255,255,255,.28) 66.66%, transparent calc(66.66% + 1px)),
    linear-gradient(to bottom, transparent calc(33.33% - 1px), rgba(255,255,255,.28) 33.33%, transparent calc(33.33% + 1px)),
    linear-gradient(to bottom, transparent calc(66.66% - 1px), rgba(255,255,255,.28) 66.66%, transparent calc(66.66% + 1px));
}
.cv-hint {
  position: absolute; bottom: 10px; left: 50%; transform: translateX(-50%);
  background: rgba(0,0,0,.72); border-radius: 999px; padding: 8px 16px;
  font-size: 13px; white-space: nowrap; opacity: 0; transition: opacity .25s; pointer-events: none;
}
.cv-hint.show { opacity: 1; }
.cv-zoom { display: flex; align-items: center; gap: 12px; padding: 6px 4px 12px; }
.cv-zoom-label { font-size: 13px; font-weight: 700; opacity: .85; flex: 0 0 auto; }
#cvZoom { flex: 1; accent-color: #ffcc00; height: 34px; }
.cv-bottom {
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 10px;
}
.cv-last { display: flex; justify-content: flex-start; }
.cv-last img {
  width: 54px; height: 54px; object-fit: cover; border-radius: 10px;
  border: 2px solid rgba(255,255,255,.85);
}
.cv-shutter {
  justify-self: center; width: 76px; height: 76px; border-radius: 50%;
  border: 5px solid #fff; background: rgba(255,255,255,.18); cursor: pointer; padding: 0;
  display: grid; place-items: center;
}
.cv-shutter span {
  width: 58px; height: 58px; border-radius: 50%; background: #fff;
  transition: transform .1s;
}
.cv-shutter:active span { transform: scale(.82); }
.cv-done { justify-self: end; min-width: 104px; }
.camera-view.flash::after {
  content: ""; position: absolute; inset: 0; background: #fff; opacity: .75;
  animation: cv-flash .16s ease-out forwards; pointer-events: none;
}
@keyframes cv-flash { to { opacity: 0; } }

/* ---------- Toasty + licznik zadań w tle ---------- */

#toasts {
  position: fixed; left: 12px; right: 12px;
  bottom: calc(env(safe-area-inset-bottom, 0px) + 14px);
  z-index: 9500; display: flex; flex-direction: column; gap: 8px;
  pointer-events: none;
}
.toast {
  pointer-events: auto; cursor: pointer;
  background: #111827; color: #fff; border-radius: 12px;
  padding: 12px 14px; font-size: 14px; font-weight: 600;
  box-shadow: 0 10px 30px rgba(15,23,42,.35);
  animation: toast-in .18s ease-out;
  word-break: break-word;
}
.toast.ok { background: #166534; }
.toast.err { background: #b42318; }
@keyframes toast-in { from { transform: translateY(8px); opacity: 0; } }

.pending-badge {
  display: inline-block; margin-left: 8px; padding: 3px 10px;
  background: rgba(255,255,255,.2); border-radius: 999px;
  font-size: 12.5px; font-weight: 700; color: #fff; vertical-align: middle;
}
.pending-badge[hidden] { display: none; }

.inapp-camera { width: 100%; margin-bottom: 4px; font-weight: 600; }
.inapp-camera .muted { font-weight: 400; font-size: 12.5px; }

/* ---------- Motyw ciemny (ręczny: przycisk ☾/☀ | auto: ustawienia systemu) ---------- */

:root[data-theme="dark"] {
  --blue: #4d9de0;
  --blue-dark: #2f7fc4;
  --orange: #ffa726;
  --bg: #0f1216;
  --panel: #171b22;
  --line: #2a3140;
  --text: #e8eaee;
  --muted: #98a2b0;
  --ok: #4cc38a;
  --err: #f66d6d;
  --appbar: #0e2a45;
  --danger-press: #e5614f;
  --press: #222834;
  --accent-soft: rgba(77,157,224,.12);
  --accent-tint: rgba(77,157,224,.22);
  --input-bg: #12161d;
  --panel-2: #1b202a;
  --ok-bg: rgba(46,120,80,.20);
  --err-bg: rgba(150,60,55,.22);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --blue: #4d9de0;
    --blue-dark: #2f7fc4;
    --orange: #ffa726;
    --bg: #0f1216;
    --panel: #171b22;
    --line: #2a3140;
    --text: #e8eaee;
    --muted: #98a2b0;
    --ok: #4cc38a;
    --err: #f66d6d;
    --appbar: #0e2a45;
    --danger-press: #e5614f;
    --press: #222834;
    --accent-soft: rgba(77,157,224,.12);
    --accent-tint: rgba(77,157,224,.22);
    --input-bg: #12161d;
    --panel-2: #1b202a;
    --ok-bg: rgba(46,120,80,.20);
    --err-bg: rgba(150,60,55,.22);
  }
}

/* ---------- Przełącznik motywu ---------- */

.theme-btn {
  flex: 0 0 44px; width: 44px; height: 40px;
  border: 1px solid rgba(255,255,255,.45); border-radius: 10px;
  background: transparent; color: #fff; font-size: 17px; cursor: pointer;
  display: grid; place-items: center; padding: 0; line-height: 1;
}
.theme-btn:active { background: rgba(255,255,255,.18); }

/* ---------- Pole "uzupełnij z fragmentu" ---------- */

.grid-draft { margin-bottom: 10px; }
.field .field-hint { font-weight: 400; font-size: 11.5px; color: var(--muted); }
.icon-btn.draft {
  background: var(--accent-soft); border-color: var(--accent-soft, var(--line));
}
.icon-btn.draft svg { stroke: var(--orange); }
.icon-btn.draft:active { background: var(--accent-tint); }
