:root {
  --bg: #f3f4f6;
  --card: #ffffff;
  --ink: #16202c;
  --muted: #5d6877;
  --line: #dfe3e8;
  --accent: #f5a300;
  --accent-ink: #1d1500;
  --dark: #16202c;
  --skada: #d92d20;
  --smuts: #9a6200;
  --saknas: #7a3fd0;
  --annat: #3a6ea5;
  --ok: #1a7f4b;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior-y: none;
}
h1, h2, h3, p { margin: 0; }
h2 { font-size: 1.05rem; }
h3 { font-size: 0.95rem; }
img { display: block; max-width: 100%; }
button, input, select, textarea { font: inherit; color: inherit; }
.loading { padding: 40px 20px; text-align: center; color: var(--muted); }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.center { text-align: center; }
[hidden] { display: none !important; }

/* ---------- Sidram ---------- */
.bar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 8px;
  padding: calc(var(--safe-top) + 10px) calc(var(--safe-right) + 12px) 10px calc(var(--safe-left) + 12px);
  background: var(--dark); color: #fff;
}
.bar h1 { font-size: 1.1rem; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar .icon-btn { color: #fff; }
.page {
  max-width: 760px; margin: 0 auto;
  padding: 14px calc(var(--safe-right) + 14px) calc(var(--safe-bottom) + 96px) calc(var(--safe-left) + 14px);
  display: grid; gap: 14px;
}
.card { background: var(--card); border-radius: var(--radius); padding: 14px; box-shadow: 0 1px 2px rgba(22, 32, 44, 0.08); display: grid; gap: 10px; }
.row { display: flex; align-items: center; gap: 10px; }
.row.wrap { flex-wrap: wrap; }
.grow { flex: 1; min-width: 0; }

/* ---------- Knappar och formulär ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 48px; padding: 0 18px; border: 0; border-radius: 12px;
  background: #e7eaee; color: var(--ink); font-weight: 600; cursor: pointer; text-decoration: none;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.dark { background: var(--dark); color: #fff; }
.btn.danger { background: #fde8e6; color: var(--skada); }
.btn.ghost { background: transparent; border: 1.5px solid var(--line); }
.btn.block { display: flex; width: 100%; }
.btn.small { min-height: 38px; padding: 0 12px; font-size: 0.9rem; border-radius: 10px; }
.btn:disabled { opacity: 0.5; }
.icon-btn { width: 44px; height: 44px; border: 0; text-decoration: none; line-height: 1; background: transparent; border-radius: 50%; font-size: 1.3rem; display: inline-grid; place-items: center; cursor: pointer; }
label.field { display: grid; gap: 4px; font-size: 0.85rem; font-weight: 600; color: var(--muted); }
input[type="text"], input[type="password"], input[type="number"], select, textarea {
  width: 100%; min-height: 46px; padding: 10px 12px; border: 1.5px solid var(--line); border-radius: 10px; background: #fff; font-size: 16px; color: var(--ink);
}
textarea { min-height: 72px; resize: vertical; }
input:focus, select:focus, textarea:focus, .btn:focus-visible { outline: 3px solid rgba(245, 163, 0, 0.55); outline-offset: 1px; }
input[type="range"] { width: 100%; accent-color: var(--accent); height: 32px; }
.fab {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(var(--safe-bottom) + 18px); z-index: 9;
  min-height: 56px; padding: 0 26px; border-radius: 28px; box-shadow: 0 6px 18px rgba(22, 32, 44, 0.3);
}

/* ---------- Brickor ---------- */
.chip { display: inline-flex; align-items: center; gap: 5px; padding: 2px 9px; border-radius: 999px; font-size: 0.78rem; font-weight: 700; background: #e7eaee; color: var(--ink); white-space: nowrap; }
.chip.utkast { background: #e7eaee; }
.chip.ute { background: #fff1cc; color: #6b4a00; }
.chip.klar { background: #dff3e7; color: var(--ok); }
.chip.skada, .chip.allvarlig { background: #fde8e6; color: var(--skada); }
.chip.smuts { background: #f6ead2; color: var(--smuts); }
.chip.saknas { background: #ece2fb; color: var(--saknas); }
.chip.annat { background: #e1ebf5; color: var(--annat); }
.chip.ok { background: #dff3e7; color: var(--ok); }

/* ---------- Startsidan ---------- */
.tabs { display: grid; grid-template-columns: 1fr 1fr; background: #e2e5ea; border-radius: 12px; padding: 4px; gap: 4px; }
.tabs button { min-height: 40px; border: 0; border-radius: 9px; background: transparent; font-weight: 600; color: var(--muted); cursor: pointer; }
.tabs button.on { background: #fff; color: var(--ink); box-shadow: 0 1px 2px rgba(22, 32, 44, 0.15); }
.insp { display: grid; grid-template-columns: 84px 1fr; gap: 12px; padding: 10px; text-decoration: none; color: inherit; align-items: center; }
.insp .thumb { width: 84px; height: 64px; border-radius: 10px; background: #dfe3e8; object-fit: cover; display: grid; place-items: center; color: var(--muted); font-size: 1.4rem; }
.insp h3 { font-size: 1rem; }
.empty { padding: 34px 16px; text-align: center; color: var(--muted); }

/* ---------- Besiktningssidan ---------- */
.facts { display: grid; grid-template-columns: auto 1fr; gap: 3px 12px; font-size: 0.92rem; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; }
.angle-card { padding: 12px; gap: 8px; text-decoration: none; color: inherit; }
.pair { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.pair figure { margin: 0; position: relative; background: #e2e5ea; border-radius: 10px; overflow: hidden; aspect-ratio: 4 / 3; display: grid; place-items: center; color: var(--muted); font-size: 0.8rem; }
.pair img { width: 100%; height: 100%; object-fit: cover; }
.pair figcaption { position: absolute; left: 6px; top: 6px; background: rgba(22, 32, 44, 0.75); color: #fff; font-size: 0.7rem; font-weight: 700; padding: 1px 7px; border-radius: 999px; }
.finding { display: grid; gap: 4px; padding: 10px 0; border-top: 1px solid var(--line); }
.finding:first-child { border-top: 0; padding-top: 0; }
.finding.avfardad { opacity: 0.5; }
.finding.avfardad .f-title { text-decoration: line-through; }
.f-title { font-weight: 700; }
.spinner { width: 18px; height: 18px; border: 3px solid #d7dbe1; border-top-color: var(--accent); border-radius: 50%; animation: spin 0.9s linear infinite; display: inline-block; vertical-align: -3px; }
@keyframes spin { to { transform: rotate(360deg); } }
.notice { background: #fff6dd; border: 1px solid #f1d98a; border-radius: 12px; padding: 10px 12px; font-size: 0.9rem; }
.notice.error { background: #fde8e6; border-color: #f4b6b0; }

/* ---------- Jämförelsevyn ---------- */
.viewer { position: relative; background: #111; border-radius: var(--radius); overflow: hidden; touch-action: pan-y; user-select: none; -webkit-user-select: none; }
.viewer img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; pointer-events: none; }
.viewer .divider { position: absolute; top: 0; bottom: 0; width: 3px; margin-left: -1.5px; background: var(--accent); pointer-events: none; }
.viewer .divider::after { content: "◂ ▸"; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); background: var(--accent); color: var(--accent-ink); font-size: 0.7rem; font-weight: 800; padding: 6px 7px; border-radius: 999px; white-space: nowrap; }
.viewer .tag { position: absolute; top: 8px; background: rgba(22, 32, 44, 0.75); color: #fff; font-size: 0.72rem; font-weight: 700; padding: 2px 8px; border-radius: 999px; pointer-events: none; }
.viewer .tag.l { left: 8px; }
.viewer .tag.r { right: 8px; }
.viewer.adding { cursor: crosshair; outline: 3px solid var(--accent); }
.box { position: absolute; border: 2.5px solid var(--annat); border-radius: 6px; box-shadow: 0 0 0 1.5px rgba(255, 255, 255, 0.85); cursor: pointer; padding: 0; background: transparent; }
.box.skada { border-color: var(--skada); }
.box.smuts { border-color: #e39a12; }
.box.saknas { border-color: var(--saknas); }
.box.avfardad { display: none; }
.box b { position: absolute; left: -2px; top: -22px; min-width: 20px; height: 20px; border-radius: 6px; background: currentColor; display: grid; place-items: center; }
.box b span { color: #fff; font-size: 0.72rem; }
.box.skada { color: var(--skada); }
.box.smuts { color: #b97a00; }
.box.saknas { color: var(--saknas); }
.box { color: var(--annat); }
.box.sel { box-shadow: 0 0 0 3px #fff, 0 0 0 6px currentColor; }
.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; background: #e2e5ea; border-radius: 12px; padding: 4px; gap: 4px; }
.seg button { min-height: 38px; border: 0; border-radius: 9px; background: transparent; font-weight: 600; font-size: 0.9rem; color: var(--muted); cursor: pointer; }
.seg button.on { background: #fff; color: var(--ink); }
.num { display: inline-grid; place-items: center; min-width: 22px; height: 22px; border-radius: 6px; color: #fff; font-size: 0.75rem; font-weight: 800; background: var(--annat); }
.num.skada { background: var(--skada); }
.num.smuts { background: #b97a00; }
.num.saknas { background: var(--saknas); }

/* ---------- Bottenark ---------- */
.sheet-back { position: fixed; inset: 0; background: rgba(22, 32, 44, 0.55); z-index: 40; display: flex; align-items: flex-end; justify-content: center; }
.sheet { width: 100%; max-width: 560px; max-height: 92vh; overflow: auto; background: #fff; border-radius: 18px 18px 0 0; padding: 16px 16px calc(var(--safe-bottom) + 16px); display: grid; gap: 12px; }

/* ---------- Kameran ---------- */
.cam {
  position: fixed; inset: 0; z-index: 30; background: #000; color: #fff;
  display: grid; grid-template-rows: auto minmax(0, 1fr) auto; grid-template-columns: minmax(0, 1fr);
  padding: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left);
}
.cam-top { display: flex; align-items: center; gap: 8px; padding: 6px 8px; }
.cam-top .icon-btn { color: #fff; background: rgba(255, 255, 255, 0.12); flex: none; }
.cam-title { flex: 1; min-width: 0; }
.cam-title b { display: block; font-size: 1rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cam-title span { display: block; font-size: 0.8rem; color: #c9d0d9; line-height: 1.25; }
.minimap { flex: none; color: #dfe5ec; }
.cam-stage { position: relative; display: flex; align-items: center; justify-content: center; overflow: hidden; min-height: 0; min-width: 0; }
.cam-frame { position: relative; background: #151a21; overflow: hidden; }
.cam-frame video, .cam-frame img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; }
.cam-frame .ghost { pointer-events: none; }
.cam-msg { position: absolute; inset: 0; display: grid; place-items: center; text-align: center; padding: 20px; color: #c9d0d9; font-size: 0.95rem; }
.cam-hint { position: absolute; left: 50%; top: 10px; transform: translateX(-50%); max-width: 92%; background: rgba(245, 163, 0, 0.95); color: var(--accent-ink); font-weight: 700; font-size: 0.82rem; padding: 5px 12px; border-radius: 999px; text-align: center; z-index: 3; }
.cam-badge { position: absolute; left: 8px; bottom: 8px; background: rgba(0, 0, 0, 0.6); font-size: 0.72rem; font-weight: 700; padding: 2px 8px; border-radius: 999px; z-index: 3; }
.cam-controls { display: grid; gap: 8px; padding: 8px 12px 10px; }
.cam-opacity { display: flex; align-items: center; gap: 10px; font-size: 0.8rem; color: #c9d0d9; }
.cam-opacity input { flex: 1; }
.cam-buttons { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 10px; }
.cam-buttons > :first-child { justify-self: start; }
.cam-buttons > :last-child { justify-self: end; }
.shutter { width: 74px; height: 74px; border-radius: 50%; border: 5px solid #fff; background: transparent; padding: 0; display: grid; place-items: center; cursor: pointer; }
.shutter::after { content: ""; width: 56px; height: 56px; border-radius: 50%; background: #fff; transition: transform 0.1s; }
.shutter:active::after { transform: scale(0.88); }
.shutter:disabled { opacity: 0.4; }
.cam .btn { background: rgba(255, 255, 255, 0.14); color: #fff; }
.cam .btn.primary { background: var(--accent); color: var(--accent-ink); }
.cam-strip { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 2px; scrollbar-width: none; }
.cam-strip::-webkit-scrollbar { display: none; }
.cam-strip button { flex: none; min-width: 40px; height: 34px; padding: 0 10px; border-radius: 9px; border: 1.5px solid rgba(255, 255, 255, 0.3); background: transparent; color: #fff; font-size: 0.8rem; font-weight: 700; cursor: pointer; }
.cam-strip button.done { background: rgba(26, 127, 75, 0.9); border-color: transparent; }
.cam-strip button.on { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); }
.cam-done { position: absolute; inset: 0; z-index: 5; background: rgba(0, 0, 0, 0.82); display: grid; place-items: center; padding: 20px; }
.cam-done > div { display: grid; gap: 12px; text-align: center; max-width: 340px; }

@media (orientation: landscape) and (max-height: 560px) {
  .cam { grid-template-rows: auto minmax(0, 1fr); grid-template-columns: minmax(0, 1fr) 190px; }
  .cam-top { grid-column: 1 / 3; padding: 2px 8px; }
  .cam-title span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .cam-controls { align-content: center; padding: 4px 10px; }
  .cam-buttons { grid-template-columns: 1fr; justify-items: center; }
  .cam-buttons > :first-child, .cam-buttons > :last-child { justify-self: center; }
  .shutter { width: 64px; height: 64px; }
  .shutter::after { width: 48px; height: 48px; }
  .cam-strip { flex-wrap: wrap; overflow: visible; }
  .cam .btn { min-height: 40px; }
}

/* ---------- Inställningar ---------- */
.angle-edit { border: 1.5px solid var(--line); border-radius: 12px; padding: 10px; display: grid; gap: 8px; }
.angle-edit .head { display: flex; align-items: center; gap: 6px; }
.angle-edit .head b { flex: 1; }

/* ---------- Meddelande ---------- */
#toast { position: fixed; left: 50%; bottom: calc(var(--safe-bottom) + 90px); transform: translateX(-50%); z-index: 60; background: var(--dark); color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 0.92rem; max-width: 90vw; opacity: 0; pointer-events: none; transition: opacity 0.2s; text-align: center; }
#toast.show { opacity: 1; }

/* ---------- Utskrift ---------- */
.print-only { display: none; }
@media print {
  body { background: #fff; }
  .bar, .fab, .no-print, #toast { display: none !important; }
  .page { max-width: none; padding: 0; }
  .card { box-shadow: none; border: 1px solid #ccc; break-inside: avoid; }
  .print-only { display: block; }
}
