:root {
  --bg: #f3f5f8;
  --card: #ffffff;
  --text: #13202e;
  --muted: #5b6878;
  --line: #d6dde6;
  --primary: #1f4e79;
  --primary-text: #ffffff;
  --accent: #f2a900;
  --danger: #b3261e;
  --ok: #1e7b34;
  --warn: #c76b00;
  --radius: 14px;
  --tap: 56px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #10161d; --card: #1a232d; --text: #eef2f6; --muted: #9fb0c2; --line: #2c3a48;
    --primary: #3d8bd4; --primary-text: #08121c; --accent: #ffc233; --danger: #ff6b5f; --ok: #5ccf78; --warn: #ffab40;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 18px/1.35 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg); color: var(--text);
  min-height: 100vh;
  padding-bottom: calc(76px + var(--safe-bottom));
  -webkit-tap-highlight-color: transparent;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--primary); }

/* ---------- topp ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; gap: 8px; align-items: stretch;
  padding: calc(6px + env(safe-area-inset-top, 0px)) 10px 6px;
  background: var(--primary); color: var(--primary-text);
}
.project-button {
  flex: 1; min-width: 0; text-align: left; background: transparent; border: 0; color: inherit;
  display: flex; flex-direction: column; padding: 4px 6px; border-radius: 10px; cursor: pointer;
}
.project-button .label { font-size: 12px; opacity: .8; text-transform: uppercase; letter-spacing: .05em; }
.project-button .name { font-size: 20px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.project-button .name::after { content: " ▾"; font-size: 14px; opacity: .8; }
.sync {
  display: flex; align-items: center; gap: 6px; background: rgba(255,255,255,.14); color: inherit;
  border: 0; border-radius: 10px; padding: 0 10px; font-size: 14px; cursor: pointer; max-width: 45%;
}
.sync .dot { width: 12px; height: 12px; border-radius: 50%; background: #9aa; flex: none; }
.sync[data-state="ok"] .dot { background: #4cd964; }
.sync[data-state="syncing"] .dot { background: #ffd60a; }
.sync[data-state="pending"] .dot { background: #ffab40; }
.sync[data-state="error"] .dot { background: #ff5b4f; }
.sync[data-state="auth"] .dot { background: #ff5b4f; }
.sync[data-state="offline"] .dot { background: #8e9aa6; }

/* ---------- flikar ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(4, 1fr);
  background: var(--card); border-top: 1px solid var(--line);
  padding-bottom: var(--safe-bottom);
}
.tabbar a {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  min-height: 64px; text-decoration: none; color: var(--muted); font-size: 14px; font-weight: 600;
}
.tabbar a .ico { font-size: 24px; line-height: 1.1; }
.tabbar a.active { color: var(--primary); box-shadow: inset 0 4px 0 var(--primary); }

main { padding: 12px; max-width: 900px; margin: 0 auto; outline: none; }
h1 { font-size: 24px; margin: 6px 0 12px; }
h2 { font-size: 20px; margin: 18px 0 8px; }
.muted { color: var(--muted); }
.small { font-size: 15px; }

/* ---------- knappar ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: var(--tap); padding: 0 18px; border-radius: var(--radius);
  border: 2px solid var(--line); background: var(--card); color: var(--text);
  font-weight: 700; font-size: 18px; cursor: pointer; text-decoration: none; user-select: none;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn.danger { color: var(--danger); border-color: var(--danger); }
.btn.big { min-height: 68px; font-size: 21px; width: 100%; }
.btn.block { width: 100%; }
.btn[disabled] { opacity: .5; pointer-events: none; }
.row-buttons { display: flex; flex-wrap: wrap; gap: 8px; margin: 10px 0; }
.row-buttons > .btn { flex: 1 1 140px; }
.icon-btn {
  width: var(--tap); height: var(--tap); flex: none; border-radius: var(--radius);
  border: 2px solid var(--line); background: var(--card); font-size: 26px; font-weight: 800;
  display: inline-flex; align-items: center; justify-content: center; cursor: pointer; color: var(--text);
}
.icon-btn.star { font-size: 28px; color: var(--muted); border-color: transparent; background: transparent; }
.icon-btn.star.on { color: var(--accent); }

/* ---------- formulär ---------- */
label.field { display: block; margin: 10px 0; font-weight: 600; font-size: 15px; color: var(--muted); }
label.field > input, label.field > select, label.field > textarea { margin-top: 4px; }
input[type="text"], input[type="search"], input[type="number"], input[type="url"], select, textarea {
  width: 100%; min-height: var(--tap); padding: 10px 14px; border-radius: var(--radius);
  border: 2px solid var(--line); background: var(--card); color: var(--text); font-size: 19px;
}
textarea { min-height: 160px; font-family: ui-monospace, Consolas, monospace; font-size: 17px; }
input:focus, select:focus, textarea:focus { outline: 3px solid var(--accent); outline-offset: 0; border-color: var(--primary); }
.check { display: flex; align-items: center; gap: 10px; min-height: 48px; font-weight: 600; }
.check input { width: 28px; height: 28px; }

/* ---------- sök ---------- */
.searchbar { position: sticky; top: 68px; z-index: 5; background: var(--bg); padding: 4px 0 8px; }
.searchbar input { font-size: 21px; min-height: 60px; }
.chips { display: flex; gap: 8px; margin-top: 8px; overflow-x: auto; }
.chip {
  min-height: 46px; padding: 0 16px; border-radius: 23px; border: 2px solid var(--line);
  background: var(--card); font-weight: 700; cursor: pointer; white-space: nowrap; color: var(--text);
}
.chip.on { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }

.list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); }
.prod {
  display: flex; align-items: center; gap: 10px; padding: 8px 8px 8px 10px; min-height: 76px;
}
.prod .main { flex: 1; min-width: 0; cursor: pointer; padding: 4px 0; }
.prod .title { font-weight: 700; overflow-wrap: anywhere; }
.prod .meta { color: var(--muted); font-size: 15px; }
.thumb {
  width: 56px; height: 56px; flex: none; border-radius: 10px; object-fit: contain; background: #fff;
  border: 1px solid var(--line);
}
.thumb.empty { display: flex; align-items: center; justify-content: center; font-size: 26px; color: var(--muted); background: var(--bg); }
.enr { font-family: ui-monospace, Consolas, monospace; font-weight: 700; letter-spacing: .02em; }
.badge { display: inline-block; font-size: 13px; padding: 2px 8px; border-radius: 10px; background: var(--bg); border: 1px solid var(--line); }
.badge.in { background: var(--ok); color: #fff; border-color: var(--ok); }

/* ---------- listrader ---------- */
.line { display: flex; align-items: center; gap: 8px; padding: 8px; }
.line .qty {
  min-width: 82px; text-align: center; font-size: 24px; font-weight: 800; padding: 6px 4px;
  border-radius: 10px; background: var(--bg); cursor: pointer; border: 2px solid var(--line);
}
.line .qty small { display: block; font-size: 13px; font-weight: 600; color: var(--muted); }
.line .main { flex: 1; min-width: 0; cursor: pointer; }
.line .title { font-weight: 700; overflow-wrap: anywhere; }
.line .meta { color: var(--muted); font-size: 15px; }
.line .steppers { display: flex; flex-direction: column; gap: 6px; }
.line .steppers .icon-btn { width: 52px; height: 46px; font-size: 24px; }
.unknown .title { color: var(--warn); }

.empty-state { text-align: center; padding: 30px 10px; color: var(--muted); }
.empty-state .big { font-size: 48px; }

/* ---------- dialog ---------- */
dialog {
  border: 0; padding: 0; border-radius: 18px; width: min(560px, calc(100vw - 16px));
  max-height: calc(100vh - 24px); background: var(--card); color: var(--text);
  box-shadow: 0 10px 40px rgba(0,0,0,.35);
}
dialog::backdrop { background: rgba(10,20,30,.55); }
.dlg { padding: 16px; display: flex; flex-direction: column; gap: 6px; max-height: calc(100vh - 24px); overflow-y: auto; }
.dlg h2 { margin: 0 0 4px; }
.dlg .close { position: absolute; right: 8px; top: 8px; }
.qtybox { display: flex; align-items: center; gap: 8px; margin: 10px 0; }
.qtybox input { text-align: center; font-size: 34px; font-weight: 800; min-height: 72px; flex: 1; min-width: 0; }
.qtybox .icon-btn { width: 72px; height: 72px; font-size: 34px; }
.quick { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.quick .btn { min-height: 56px; }
.dlg-actions { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.dlg-actions .btn { flex: 1 1 150px; }
.preview { max-height: 40vh; overflow: auto; border: 1px solid var(--line); border-radius: 10px; }
.preview table { width: 100%; border-collapse: collapse; font-size: 15px; }
.preview td, .preview th { padding: 6px 8px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
.preview tr.bad td { color: var(--danger); }
.pickrow { display: flex; gap: 10px; align-items: center; padding: 8px; }
.pickrow input[type="checkbox"] { width: 30px; height: 30px; flex: none; }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(86px + var(--safe-bottom)); transform: translateX(-50%) translateY(20px);
  background: #13202e; color: #fff; padding: 12px 18px; border-radius: 12px; font-weight: 600;
  opacity: 0; pointer-events: none; transition: .2s; max-width: calc(100vw - 24px); z-index: 50;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; font-size: 16px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; }
.code { font-family: ui-monospace, Consolas, monospace; font-size: 14px; background: var(--bg); padding: 8px; border-radius: 8px; overflow-wrap: anywhere; }
.bookmarklet {
  display: inline-block; padding: 14px 20px; border-radius: 12px; background: var(--accent); color: #111;
  font-weight: 800; text-decoration: none; border: 2px dashed #333;
}

/* ---------- utskrift ---------- */
.print-page { background: #fff; color: #000; padding: 8px; border-radius: 8px; }
.print-page h1 { margin: 0 0 4px; }
.print-page table { width: 100%; border-collapse: collapse; font-size: 15px; }
.print-page th, .print-page td { border: 1px solid #999; padding: 6px 8px; text-align: left; vertical-align: top; }
.print-page th { background: #eee; }
.print-page td.num { text-align: right; white-space: nowrap; font-weight: 700; }
.print-page td.enr { white-space: nowrap; font-family: ui-monospace, Consolas, monospace; }
.print-page .info { font-size: 14px; color: #333; margin-bottom: 10px; }

@media print {
  @page { margin: 12mm; }
  body { background: #fff; color: #000; padding: 0; font-size: 12pt; }
  .no-print, .topbar, .tabbar, .toast, dialog { display: none !important; }
  main { padding: 0; max-width: none; }
  .print-page { padding: 0; }
  .print-page table { font-size: 11pt; }
  .print-page tr { page-break-inside: avoid; }
  .print-page thead { display: table-header-group; }
}

@media (min-width: 760px) {
  .tabbar { left: 50%; transform: translateX(-50%); width: 760px; border: 1px solid var(--line); border-bottom: 0; border-radius: 16px 16px 0 0; }
}
