*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --color-bg: #f5f5f5;
  --color-surface: #ffffff;
  --color-border: #d9d9d9;
  --color-text: #1a1a1a;
  --color-muted: #666;
  --color-accent: #1a6fb5;
  --color-accent-hover: #145a96;
  --color-error: #c0392b;
  --color-success: #1e7e34;
  --color-warning: #b45309;
  --radius: 4px;
  --font: 'Inter', system-ui, sans-serif;
  --font-mono: 'JetBrains Mono', monospace;
}

[data-theme='dark'] {
  --color-bg: #1a1a1a;
  --color-surface: #242424;
  --color-border: #383838;
  --color-text: #e8e8e8;
  --color-muted: #aaa;
  --color-accent: #4a9ade;
  --color-accent-hover: #5badeb;
}

body {
  font-family: var(--font);
  background: var(--color-bg);
  color: var(--color-text);
  font-size: 15px;
  line-height: 1.5;
}

/* Header */
.site-header {
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  height: 52px;
}
.logo { font-weight: 600; text-decoration: none; color: var(--color-text); }
.logo span { color: var(--color-accent); font-weight: 400; }
nav { display: flex; align-items: center; gap: 16px; }
nav a { color: var(--color-muted); text-decoration: none; font-size: 14px; }
nav a:hover { color: var(--color-text); }

main { padding: 32px 24px; max-width: 960px; margin: 0 auto; }

/* Page header */
.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 24px;
  gap: 8px;
  flex-wrap: wrap;
}
.page-header__sub {
  color: var(--color-muted);
  font-size: 14px;
}
h1 { font-size: 20px; font-weight: 600; }

/* Fieldset reset - semantik utan native browser-border */
fieldset {
  border: none;
  padding: 0;
  margin: 0;
  min-width: 0;
}
legend {
  font-size: 15px;
  font-weight: 600;
  padding: 0;
  margin-bottom: 4px;
  float: left;
  width: 100%;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  background: var(--color-accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  padding: 8px 16px;
  font-size: 14px;
  font-family: var(--font);
  cursor: pointer;
  text-decoration: none;
}
.btn:hover { background: var(--color-accent-hover); }
.btn-small {
  display: inline-block;
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  color: var(--color-text);
  border-radius: var(--radius);
  padding: 4px 10px;
  font-size: 13px;
  font-family: var(--font);
  cursor: pointer;
}
.btn-small:hover { background: var(--color-bg); }
.btn-return { color: var(--color-success); border-color: var(--color-success); }
.btn-danger { color: var(--color-error); border-color: var(--color-error); }
.btn-link {
  background: none;
  border: none;
  color: var(--color-muted);
  font-size: 14px;
  font-family: var(--font);
  cursor: pointer;
  padding: 0;
}
.btn-link:hover { color: var(--color-text); }

/* Table */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.table, .loans-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  overflow: hidden;
  font-size: 14px;
}
.table th, .loans-table th {
  text-align: left;
  padding: 10px 14px;
  background: var(--color-bg);
  border-bottom: 1px solid var(--color-border);
  font-weight: 600;
  font-size: 13px;
  white-space: nowrap;
}
.table td, .loans-table td {
  padding: 10px 14px;
  border-bottom: 1px solid var(--color-border);
  vertical-align: middle;
}
.table tr:last-child td, .loans-table tr:last-child td { border-bottom: none; }
.table small, .loans-table small { color: var(--color-muted); }
.actions { display: flex; gap: 6px; white-space: nowrap; }
.overdue td { background: #fff8f0; }

/* Badges */
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 600;
}
.badge.confirmed { background: #dcf5e4; color: var(--color-success); }
.badge.pending { background: #fff3cd; color: var(--color-warning); }
.badge.available { background: #dcf5e4; color: var(--color-success); }
.badge.loaned { background: #fde8e8; color: var(--color-error); }

/* Signature thumbnail */
.sig-thumb { height: 40px; border: 1px solid var(--color-border); border-radius: var(--radius); }

/* Forms */
.form-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 24px;
  width: 100%;
  max-width: 480px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.form-card--centered { margin: 0 auto; }
.form-page {
  max-width: 480px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.inline-form {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 20px;
}
.inline-form input { font-family: var(--font); }
.role-form { display: inline; margin: 0; }
.role-form select { font-size: 13px; padding: 2px 4px; }

label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 14px;
  font-weight: 500;
}
input, select, textarea {
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 8px 10px;
  font-size: 14px;
  font-family: var(--font);
  background: var(--color-surface);
  color: var(--color-text);
  width: 100%;
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--color-accent);
  outline-offset: -1px;
}

/* Auth */
.auth-box {
  max-width: 360px;
  margin: 80px auto;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 32px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}
.auth-box h1 { font-size: 18px; }
.auth-box form { display: flex; flex-direction: column; gap: 14px; }

/* Misc */
.error { color: var(--color-error); font-size: 14px; }
.warning {
  background: #fff8e6;
  border: 1px solid #f0c040;
  border-radius: var(--radius);
  padding: 10px 14px;
  font-size: 14px;
  color: #7a5200;
  margin-bottom: 16px;
}
.empty { color: var(--color-muted); padding: 24px 0; }

/* Confirm page */
body.confirm-page {
  display: flex;
  align-items: flex-start;
  justify-content: center;
  min-height: 100vh;
  padding: 40px 16px;
}
.confirm-box {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 32px;
  width: 100%;
  max-width: 480px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}
.confirm-logo {
  font-size: 13px;
  font-weight: 600;
  color: var(--color-muted);
  letter-spacing: 0.02em;
}
.confirm-logo span { color: var(--color-accent); font-weight: 400; }
.confirm-box h1 { font-size: 20px; }
.confirm-box form { display: flex; flex-direction: column; gap: 16px; }

.loan-info {
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 14px;
}

/* Field (ersätter label för canvas-sektionen) */
.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.field-label {
  font-size: 14px;
  font-weight: 500;
}

/* Signature canvas */
.canvas-wrap {
  position: relative;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  background: #fff;
  touch-action: none;
}
#sig-canvas {
  display: block;
  width: 100%;
  height: 160px;
  border-radius: var(--radius);
  cursor: crosshair;
}
#clear-sig {
  position: absolute;
  top: 6px;
  right: 6px;
}

.confirm-done, .confirm-info { text-align: center; padding: 16px 0; }
.confirm-done p, .confirm-info p { color: var(--color-muted); margin-top: 8px; }

.terms {
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 16px;
  font-size: 13px;
  line-height: 1.65;
  color: var(--color-text);
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.terms h2 { font-size: 14px; font-weight: 600; }
.terms ul { padding-left: 18px; display: flex; flex-direction: column; gap: 4px; }

.checkbox-label {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  font-size: 14px;
  cursor: pointer;
  line-height: 1.4;
}
.checkbox-label input[type='checkbox'] {
  margin-top: 2px;
  flex-shrink: 0;
  accent-color: var(--color-accent);
}

/* Loans filter */
.loans-filter select {
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 6px 10px;
  font-size: 14px;
  font-family: var(--font);
  background: var(--color-surface);
  color: var(--color-text);
}
.loans-filter input[type='date'] {
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 6px 10px;
  font-size: 14px;
  font-family: var(--font);
}
.badge-overdue { background: #fde8e8; color: var(--color-error); }
.pill-unconfirmed { background: #fef3c7; color: #92400e; font-size: 11px; }
.pill-overdue { background: #fee2e2; color: #991b1b; font-size: 11px; }
.pill-old { background: #f3f4f6; color: #6b7280; font-size: 11px; }
.pagination { display: flex; gap: 8px; margin-top: 16px; }

/* Side drawer (BL-020) */
#drawer-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  z-index: 100;
}
#drawer-overlay[data-open] { display: block; }

#loan-drawer {
  position: fixed;
  top: 0;
  right: -100%;
  width: 480px;
  max-width: 100vw;
  height: 100%;
  background: var(--color-surface);
  border-left: 1px solid var(--color-border);
  box-shadow: -4px 0 16px rgba(0, 0, 0, 0.1);
  z-index: 101;
  overflow-y: auto;
  transition: right 0.2s ease;
  padding: 20px 24px;
}
#loan-drawer[data-open] { right: 0; }

@media (max-width: 600px) {
  #loan-drawer { width: 100vw; }
}

.drawer-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  border-bottom: 1px solid var(--color-border);
  padding-bottom: 12px;
}
.drawer-header h2 { font-size: 16px; margin: 0; }
#drawer-close { font-size: 20px; padding: 0 4px; }

.drawer-dl { display: grid; grid-template-columns: auto 1fr; gap: 6px 16px; margin-bottom: 16px; }
.drawer-dl dt { color: var(--color-muted); font-size: 13px; white-space: nowrap; }
.drawer-dl dd { font-size: 14px; margin: 0; word-break: break-word; }

.drawer-sig { max-width: 100%; border: 1px solid var(--color-border); border-radius: var(--radius); margin-bottom: 16px; }
.drawer-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.row-clickable { cursor: pointer; }
.row-clickable:hover td { background: var(--color-bg); }

/* Dev page */
.dev-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(400px, 1fr)); gap: 16px; margin-top: 16px; }
.dev-card { background: var(--color-surface); border: 1px solid var(--color-border); border-radius: var(--radius); padding: 16px 20px; }
.dev-card h2 { font-size: 15px; margin-bottom: 12px; color: var(--color-muted); font-weight: 600; }

/* Dev footer (only visible in non-production) */
.dev-footer {
  text-align: center;
  padding: 12px;
  font-size: 12px;
  color: var(--color-muted);
  font-family: var(--font-mono);
}
.dev-footer a { color: var(--color-muted); }

/* Blob loading states */
#blob-splash {
  position: fixed;
  inset: 0;
  z-index: 9999;
  background: var(--color-bg);
  align-items: center;
  justify-content: center;
}
/* display sätts bara när [hidden] tas bort av JS — ID-selector slår annars [hidden] */
#blob-splash:not([hidden]) {
  display: flex;
}
#blob-splash .blob-svg {
  width: min(60vw, 60vh);
  height: min(60vw, 60vh);
}
.btn-blob {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 2.4rem;
  padding: 0 0.5rem;
}

/* Confirm dialog */
#confirm-dialog {
  position: fixed;
  inset: 0;
  margin: auto;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 20px 24px;
  width: calc(100vw - 32px);
  max-width: 360px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
}
#confirm-dialog::backdrop { background: rgba(0, 0, 0, 0.35); }

/* Mobile */
@media (max-width: 768px) {
  .site-header {
    height: auto;
    flex-direction: column;
    align-items: flex-start;
    padding: 10px 14px 0;
    gap: 0;
  }
  nav {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    width: 100%;
    gap: 10px;
    padding: 8px 0 10px;
    scrollbar-width: none;
  }
  nav::-webkit-scrollbar { display: none; }
  nav a, nav button {
    font-size: 13px;
    white-space: nowrap;
    min-height: 44px;
    display: inline-flex;
    align-items: center;
  }
  main { padding: 20px 14px; }
  .page-header { flex-wrap: wrap; gap: 8px; }
  .table, .loans-table { display: block; overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .table th, .loans-table th { white-space: nowrap; }
  .search-bar { flex-direction: column; align-items: flex-start; }
  .btn-small {
    min-height: 44px;
    padding: 8px 12px;
    display: inline-flex;
    align-items: center;
  }
}
#confirm-dialog-message { margin-bottom: 16px; }
.dialog-actions { display: flex; gap: 8px; justify-content: flex-end; }

/* Utility-klasser (BL-073: ersätter inline style-attribut) */
.d-none { display: none; }
.d-inline { display: inline; }
.mt-12 { margin-top: 12px; }
.mt-16 { margin-top: 1rem; }
.mt-2rem { margin-top: 2rem; }
.mt-32 { margin-top: 32px; }
.mb-8 { margin-bottom: 8px; }
.mb-16 { margin-bottom: 1rem; }
.mw-520 { max-width: 520px; }
.text-muted { color: var(--color-muted); }
.text-center { text-align: center; }
.qr-wrap { text-align: center; margin: 16px 0; }
.qr-wrap canvas { max-width: 100%; height: auto; display: block; margin: 0 auto; }
.totp-code { font-family: var(--font-mono); font-size: 13px; word-break: break-all; }

/* Admin-handbok (BL-085) */
.help-toc {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 32px;
  padding: 16px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
}
.help-toc a {
  color: var(--color-accent);
  text-decoration: none;
  font-size: 13px;
  padding: 4px 10px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
}
.help-toc a:hover { background: var(--color-bg); }
.help-section {
  margin-bottom: 40px;
  padding-bottom: 32px;
  border-bottom: 1px solid var(--color-border);
}
.help-section:last-child { border-bottom: none; }
.help-section h2 {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 8px;
}
.help-section > .badge { margin-right: 4px; margin-bottom: 12px; display: inline-block; }
.help-steps {
  margin: 12px 0 0 20px;
  line-height: 1.7;
}
.help-steps li { margin-bottom: 4px; font-size: 14px; }
.passkey-input { margin-bottom: 8px; width: 100%; max-width: 300px; }
