:root {
  --bg: #f6f1e9;
  --surface: #fffdf9;
  --surface-2: #efe8dc;
  --text: #2a2520;
  --muted: #7a6f63;
  --line: #e3dacb;
  --accent: #8a3b2b;
  --accent-text: #ffffff;
  --accent-soft: #f3e1da;
  --ok: #2f6b45;
  --ok-soft: #dcefe2;
  --warn: #9a5b00;
  --warn-soft: #fbe9cc;
  --danger: #b3261e;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(40, 30, 20, .06), 0 4px 14px rgba(40, 30, 20, .06);
  --tabbar: 62px;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, system-ui, sans-serif;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1916;
    --surface: #25221e;
    --surface-2: #302c27;
    --text: #f1ebe2;
    --muted: #a99d8f;
    --line: #3a352f;
    --accent: #e08a6f;
    --accent-text: #1b1916;
    --accent-soft: #3d2a24;
    --ok: #7fcf9a;
    --ok-soft: #233a2b;
    --warn: #f0b860;
    --warn-soft: #3d3020;
    --danger: #ff8a80;
    --shadow: none;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--text); font: 16px/1.45 var(--font); }
body { min-height: 100dvh; padding-bottom: calc(var(--tabbar) + env(safe-area-inset-bottom) + 16px); }
body.no-tabs { padding-bottom: calc(env(safe-area-inset-bottom) + 16px); }
a { color: var(--accent); }
h1, h2, h3 { font-family: var(--serif); font-weight: 600; letter-spacing: -.01em; margin: 0; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.2rem; margin: 22px 0 10px; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
[hidden] { display: none !important; }

/* ---------- Header */
#top {
  position: sticky; top: 0; z-index: 20;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(12px); -webkit-backdrop-filter: saturate(1.4) blur(12px);
  display: flex; align-items: center; gap: 10px; min-height: 52px;
}
#top:empty { display: none; }
#top .title { flex: 1; min-width: 0; font-family: var(--serif); font-size: 1.35rem; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.iconbtn {
  border: 0; background: transparent; color: var(--accent); font: inherit; font-size: 1rem;
  padding: 8px 6px; min-height: 44px; display: inline-flex; align-items: center; gap: 4px; cursor: pointer;
}
.iconbtn svg { width: 22px; height: 22px; }

main { padding: 0 16px; max-width: 720px; margin: 0 auto; }

/* ---------- Tabbar */
#tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: grid; grid-template-columns: repeat(4, 1fr);
  height: calc(var(--tabbar) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: saturate(1.4) blur(14px); -webkit-backdrop-filter: saturate(1.4) blur(14px);
  border-top: 1px solid var(--line);
}
#tabs a {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  text-decoration: none; color: var(--muted); font-size: .72rem; font-weight: 500;
}
#tabs a svg { width: 25px; height: 25px; }
#tabs a.active { color: var(--accent); }
#tabs a.scan span.pill {
  background: var(--accent); color: var(--accent-text); border-radius: 999px; width: 52px; height: 34px;
  display: grid; place-items: center;
}
#tabs a.scan.active span.pill { box-shadow: 0 0 0 3px var(--accent-soft); }

/* ---------- Form */
label { display: block; font-size: .85rem; font-weight: 600; color: var(--muted); margin: 14px 0 6px; }
input, textarea, select {
  width: 100%; font: inherit; font-size: 16px; color: var(--text);
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
  padding: 12px 14px; min-height: 48px; outline: none; appearance: none; -webkit-appearance: none;
}
textarea { min-height: 90px; resize: vertical; }
input:focus, textarea:focus, select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.row { display: flex; gap: 10px; }
.row > * { flex: 1; min-width: 0; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  width: 100%; min-height: 50px; padding: 12px 18px; margin-top: 16px;
  font: inherit; font-weight: 600; border-radius: 12px; border: 1px solid transparent; cursor: pointer;
  background: var(--accent); color: var(--accent-text); text-decoration: none;
}
.btn.secondary { background: var(--surface); color: var(--text); border-color: var(--line); }
.btn.ghost { background: transparent; color: var(--accent); }
.btn.danger { background: transparent; color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, transparent); }
.btn.ok { background: var(--ok); color: #fff; }
.btn:disabled { opacity: .55; }
.btn svg { width: 20px; height: 20px; }
.btn-row { display: flex; gap: 10px; }
.btn-row .btn { flex: 1; }

.error { color: var(--danger); font-size: .9rem; margin-top: 12px; min-height: 1em; }

/* ---------- Kort / listor */
.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); border: 1px solid var(--line); padding: 16px; }
.auth { padding-top: calc(env(safe-area-inset-top) + 48px); }
.brand { text-align: center; margin-bottom: 26px; }
.brand .logo { width: 64px; height: 64px; margin: 0 auto 10px; }
.brand h1 { font-size: 2rem; }

.search { position: relative; margin: 4px 0 10px; }
.search input { padding-left: 42px; border-radius: 999px; background: var(--surface); }
.search svg { position: absolute; left: 14px; top: 50%; transform: translateY(-50%); width: 18px; height: 18px; color: var(--muted); }
.chips { display: flex; gap: 8px; overflow-x: auto; padding: 2px 0 10px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; border: 1px solid var(--line); background: var(--surface); color: var(--text);
  border-radius: 999px; padding: 7px 14px; font: inherit; font-size: .875rem; min-height: 36px; cursor: pointer;
}
.chip.on { background: var(--text); color: var(--bg); border-color: var(--text); }
select.chip { width: auto; min-height: 36px; padding-right: 28px; background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%); background-position: calc(100% - 14px) 50%, calc(100% - 9px) 50%; background-size: 5px 5px; background-repeat: no-repeat; }

.list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.book {
  display: flex; gap: 12px; align-items: center; padding: 10px; text-decoration: none; color: inherit;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
}
.book:active { background: var(--surface-2); }
.cover {
  flex: none; width: 46px; height: 68px; border-radius: 4px; object-fit: cover; background: var(--surface-2);
  display: grid; place-items: center; color: var(--muted); font-family: var(--serif); font-size: 1.2rem; overflow: hidden;
  box-shadow: inset 0 0 0 1px rgba(0,0,0,.06);
}
.book .meta { min-width: 0; flex: 1; }
.book .t { font-weight: 600; line-height: 1.25; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.book .a { color: var(--muted); font-size: .875rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tags { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 4px; }
.tag { font-size: .75rem; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); white-space: nowrap; max-width: 100%; overflow: hidden; text-overflow: ellipsis; }
.tag.lent { background: var(--warn-soft); color: var(--warn); font-weight: 600; }
.tag.late { background: var(--accent-soft); color: var(--danger); font-weight: 600; }
.tag.home { background: var(--ok-soft); color: var(--ok); }

.empty { text-align: center; padding: 48px 12px; color: var(--muted); }
.empty svg { width: 54px; height: 54px; opacity: .6; }
.stats { color: var(--muted); font-size: .85rem; margin: 0 2px 10px; }

/* ---------- Bokdetalj */
.hero { display: flex; gap: 16px; align-items: flex-start; margin: 6px 0 16px; }
.hero .cover { width: 96px; height: 144px; font-size: 2rem; border-radius: 6px; box-shadow: var(--shadow), inset 0 0 0 1px rgba(0,0,0,.06); }
.hero h1 { font-size: 1.35rem; line-height: 1.2; }
.hero .by { color: var(--muted); margin-top: 6px; }
.facts { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; font-size: .9rem; margin: 0; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; overflow-wrap: anywhere; }
.status { border-radius: var(--radius); padding: 14px 16px; margin: 14px 0; }
.status.lent { background: var(--warn-soft); color: var(--warn); }
.status.home { background: var(--ok-soft); color: var(--ok); }
.status strong { display: block; font-size: 1.05rem; }
.history { list-style: none; padding: 2px 16px; margin: 0; font-size: .9rem; }
.history li { padding: 10px 0; border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; gap: 10px; }
.history li:last-child { border-bottom: 0; }

/* ---------- Scanner */
.scanner-wrap { position: relative; border-radius: var(--radius); overflow: hidden; background: #000; aspect-ratio: 4 / 3; margin-top: 4px; }
#video { width: 100%; height: 100%; object-fit: cover; display: block; }
.scan-overlay { position: absolute; inset: 0; pointer-events: none; display: grid; place-items: center; }
.scan-overlay .frame { width: 78%; height: 38%; border: 2px solid rgba(255,255,255,.9); border-radius: 12px; box-shadow: 0 0 0 999px rgba(0,0,0,.35); }
.scan-overlay .laser { position: absolute; width: 70%; height: 2px; background: #ff5a3c; box-shadow: 0 0 10px #ff5a3c; animation: laser 1.8s ease-in-out infinite; }
@keyframes laser { 0%,100% { transform: translateY(-40px); } 50% { transform: translateY(40px); } }
.scan-msg { position: absolute; left: 0; right: 0; bottom: 10px; text-align: center; color: #fff; font-size: .9rem; text-shadow: 0 1px 3px #000; }
.scan-start { position: absolute; inset: 0; display: grid; place-items: center; color: #fff; text-align: center; padding: 20px; }
.scan-start .btn { width: auto; }
.manual { display: flex; gap: 8px; margin-top: 12px; }
.manual input { flex: 1; }
.manual .btn { width: auto; margin: 0; }
.batch { display: flex; align-items: center; gap: 10px; margin: 12px 2px 0; font-size: .9rem; color: var(--muted); }
.batch input { width: auto; min-height: 0; }
.switch { position: relative; width: 46px; height: 28px; flex: none; }
.switch input { opacity: 0; width: 100%; height: 100%; position: absolute; margin: 0; z-index: 1; cursor: pointer; }
.switch span { position: absolute; inset: 0; background: var(--line); border-radius: 999px; transition: .2s; }
.switch span::after { content: ""; position: absolute; width: 22px; height: 22px; left: 3px; top: 3px; border-radius: 50%; background: #fff; transition: .2s; box-shadow: 0 1px 3px rgba(0,0,0,.25); }
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::after { transform: translateX(18px); }

.found { margin-top: 14px; }
.notice { border-radius: 12px; padding: 12px 14px; font-size: .9rem; margin-top: 12px; }
.notice.warn { background: var(--warn-soft); color: var(--warn); }
.notice.info { background: var(--surface-2); color: var(--muted); }

/* ---------- Inställningar */
.menu { list-style: none; padding: 0; margin: 0; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.menu li + li { border-top: 1px solid var(--line); }
.menu .item { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 14px 16px; min-height: 52px; color: inherit; text-decoration: none; background: none; border: 0; width: 100%; font: inherit; text-align: left; cursor: pointer; }
.menu .item .sub { color: var(--muted); font-size: .85rem; }
.menu .item.danger { color: var(--danger); }
.user-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 12px 16px; }
.user-row .who { min-width: 0; }
.user-row .who div { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.linkbox { display: flex; gap: 8px; margin-top: 10px; }
.linkbox input { font-size: 14px; }
.linkbox .btn { width: auto; margin: 0; }

/* ---------- Sheet (modal) */
.sheet-bg { position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 50; display: flex; align-items: flex-end; animation: fade .15s; }
.sheet {
  background: var(--bg); width: 100%; max-width: 720px; margin: 0 auto; border-radius: 20px 20px 0 0;
  padding: 10px 16px calc(env(safe-area-inset-bottom) + 18px); max-height: 92dvh; overflow-y: auto; animation: up .2s ease-out;
}
.sheet .grab { width: 40px; height: 5px; border-radius: 3px; background: var(--line); margin: 0 auto 12px; }
@keyframes up { from { transform: translateY(40px); opacity: .5; } }
@keyframes fade { from { opacity: 0; } }

/* ---------- Toast */
#toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar) + env(safe-area-inset-bottom) + 14px); z-index: 60;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none; transition: .2s;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 999px; font-size: .9rem; max-width: calc(100% - 32px);
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

.spinner { width: 22px; height: 22px; border: 3px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; margin: 30px auto; }
@keyframes spin { to { transform: rotate(360deg); } }

@media (min-width: 720px) {
  .sheet-bg { align-items: center; }
  .sheet { border-radius: 20px; }
}
.tag.gone { background: var(--surface-2); color: var(--muted); font-weight: 600; }
.status.gone { background: var(--surface-2); color: var(--muted); }
.status.gone strong { color: var(--text); }

.photo-btn { margin-top: 12px; }
.torch { position: absolute; top: 10px; right: 10px; width: 44px; height: 44px; border-radius: 50%; border: 0; font-size: 20px; background: rgba(0,0,0,.45); color: #fff; }
.torch.on { background: #ffd54a; }
