:root {
  --bg: #f4f2ee;
  --surface: #ffffff;
  --text: #1d1b18;
  --muted: #6b665e;
  --line: #e2ded6;
  --accent: #d2491f;
  --accent-ink: #ffffff;
  --danger: #b42318;
  --ok: #1f7a4d;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.06);
  color-scheme: light;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}
a { color: inherit; }
button, input, select { font: inherit; color: inherit; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 38px; padding: 0 14px; border-radius: 9px;
  border: 1px solid var(--line); background: var(--surface);
  cursor: pointer; text-decoration: none; font-weight: 500; white-space: nowrap;
}
.btn:hover { border-color: #cfc9be; background: #faf9f6; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.06); }
.btn.danger { color: var(--danger); }
.btn.small { min-height: 32px; padding: 0 10px; font-size: 14px; }
.btn:disabled { opacity: .5; cursor: default; }
input[type=text], input[type=password], input[type=url], input[type=number], select {
  width: 100%; min-height: 40px; padding: 8px 12px; border-radius: 9px;
  border: 1px solid var(--line); background: var(--surface);
}
input:focus, select:focus, .btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
label { display: block; font-size: 13px; font-weight: 600; color: var(--muted); margin: 0 0 4px; }

/* Topp */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px; background: rgba(244,242,238,.92); backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; letter-spacing: -.01em; display: flex; align-items: center; gap: 8px; text-decoration: none; }
.brand-mark { width: 26px; height: 26px; border-radius: 7px; background: var(--accent); display: grid; place-items: center; color: #fff; font-size: 14px; }
.spacer { flex: 1; }
.wrap { max-width: 960px; margin: 0 auto; padding: 20px 16px 60px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
h1 { font-size: 24px; margin: 8px 0 16px; letter-spacing: -.02em; }
h2 { font-size: 17px; margin: 0 0 12px; }
.muted { color: var(--muted); }

/* Inloggning */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login .card { width: 100%; max-width: 360px; padding: 28px 24px; }
.login .brand { justify-content: center; margin-bottom: 20px; font-size: 18px; }
.err { color: var(--danger); font-size: 14px; margin: 8px 0 0; }

/* Uppladdning */
.upload { padding: 18px; margin-bottom: 24px; }
.drop {
  border: 2px dashed #d5cfc4; border-radius: 10px; padding: 26px 16px; text-align: center;
  cursor: pointer; transition: background .15s, border-color .15s; margin-top: 12px;
}
.drop.over { background: #fdf1ec; border-color: var(--accent); }
.drop strong { color: var(--accent); }
.progress { height: 6px; background: var(--line); border-radius: 3px; overflow: hidden; margin-top: 12px; display: none; }
.progress > div { height: 100%; width: 0; background: var(--accent); transition: width .2s; }

/* Lista */
.books { display: grid; gap: 12px; }
.book { padding: 14px; display: flex; gap: 14px; align-items: center; }
.thumb { width: 64px; height: 84px; flex: none; border-radius: 6px; background: #eee; overflow: hidden; border: 1px solid var(--line); }
.thumb canvas { width: 100%; height: 100%; object-fit: cover; display: block; }
.book-info { flex: 1; min-width: 0; }
.book-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.book-meta { font-size: 13px; color: var(--muted); }
.book-actions { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; }
.empty { padding: 40px 16px; text-align: center; color: var(--muted); }
@media (max-width: 560px) {
  .book { align-items: flex-start; }
  .thumb { width: 52px; height: 68px; }
}

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(20px);
  background: #1d1b18; color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 14px;
  opacity: 0; transition: .2s; pointer-events: none; z-index: 100; max-width: calc(100% - 32px);
}
.toast.show { opacity: 1; transform: translateX(-50%); }

/* Redigerare */
.editor { display: grid; grid-template-columns: 1fr 300px; gap: 20px; max-width: 1200px; margin: 0 auto; padding: 16px; align-items: start; }
.pages { display: grid; gap: 24px; justify-items: center; }
.epage { position: relative; background: #fff; box-shadow: var(--shadow); width: 100%; max-width: 760px; user-select: none; -webkit-user-select: none; }
.epage canvas { display: block; width: 100%; height: 100%; }
.epage .pno { position: absolute; left: -2px; top: -22px; font-size: 12px; color: var(--muted); }
.layer { position: absolute; inset: 0; cursor: crosshair; }
.drawmode .layer { touch-action: none; }
.lbox {
  position: absolute; border: 2px solid rgba(37, 99, 235, .9); background: rgba(37, 99, 235, .14);
  border-radius: 3px; cursor: move; touch-action: none;
}
.lbox.manual { border-color: rgba(210, 73, 31, .95); background: rgba(210, 73, 31, .14); }
.lbox.sel { box-shadow: 0 0 0 3px rgba(255,255,255,.9), 0 0 0 5px #1d1b18; }
.lbox .h { position: absolute; right: -7px; bottom: -7px; width: 14px; height: 14px; background: #1d1b18; border: 2px solid #fff; border-radius: 50%; cursor: nwse-resize; display: none; }
.lbox.sel .h { display: block; }
.side { position: sticky; top: 72px; display: grid; gap: 14px; }
.side .card { padding: 16px; }
.legend { display: flex; gap: 14px; font-size: 13px; color: var(--muted); flex-wrap: wrap; }
.legend i { display: inline-block; width: 12px; height: 12px; border-radius: 3px; vertical-align: -1px; margin-right: 5px; }
.field { margin-bottom: 12px; }
.seg { display: flex; border: 1px solid var(--line); border-radius: 9px; overflow: hidden; }
.seg button { flex: 1; border: 0; background: var(--surface); min-height: 36px; cursor: pointer; }
.seg button.on { background: #1d1b18; color: #fff; }
.row { display: flex; gap: 8px; flex-wrap: wrap; }
.dirty { color: var(--accent); font-size: 13px; font-weight: 600; }
@media (max-width: 860px) {
  .editor { grid-template-columns: 1fr; padding: 12px; }
  .side { position: fixed; left: 0; right: 0; bottom: 0; top: auto; z-index: 20; gap: 0; }
  .side .card { border-radius: 14px 14px 0 0; box-shadow: 0 -6px 24px rgba(0,0,0,.12); }
  .side .help { display: none; }
  .side .panel-empty { display: none; }
  .pages { padding-bottom: 260px; }
  #drawToggle { margin: 0 12px max(12px, env(safe-area-inset-bottom)); min-height: 48px; box-shadow: 0 4px 20px rgba(0,0,0,.18); }
}
