/* Malebog – legende, touch-venligt, dansk. Ingen eksterne fonts (privatliv + offline). */

:root {
  --bg: #FFF8F0;
  --surface: #FFFFFF;
  --ink: #2B2140;
  --muted: #7A7290;
  --line: #EFE4DA;
  --accent: #FF7A59;
  --accent-ink: #FFFFFF;
  --accent-2: #5AC8FA;
  --danger: #E53935;
  --shadow: 0 4px 16px rgba(43, 33, 64, 0.10);
  --radius: 18px;
  --tap: 48px;
  --font: "Segoe UI Rounded", "Nunito", "Quicksand", "Arial Rounded MT Bold", ui-rounded, system-ui, -apple-system, "Segoe UI", sans-serif;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  font-family: var(--font);
  color: var(--ink);
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
  min-height: 100dvh;
}
button, input { font: inherit; }
svg { width: 24px; height: 24px; fill: currentColor; flex: none; }
a { color: inherit; text-decoration: none; }
.hidden { display: none !important; }
.spacer { flex: 1; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.center { text-align: center; }

/* ---------- Views ---------- */
.view { display: none; flex-direction: column; min-height: 100dvh; }
.view.active { display: flex; }
#view-editor.active { height: 100dvh; overflow: hidden; }

.topbar {
  display: flex; align-items: center; gap: 8px;
  padding: calc(8px + var(--safe-t)) calc(12px + var(--safe-r)) 8px calc(12px + var(--safe-l));
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  min-height: calc(60px + var(--safe-t));
  flex: none;
}
.logo { font-size: 1.4rem; margin: 0; display: flex; align-items: center; gap: 10px; font-weight: 800; letter-spacing: 0.01em; }
.logo-dot { width: 18px; height: 18px; border-radius: 50%; background: conic-gradient(#E53935, #FDD835, #8BC34A, #4FC3F7, #8E44AD, #E53935); box-shadow: 0 0 0 3px #fff, 0 0 0 5px var(--accent); }
.title { font-size: 1.2rem; margin: 0; font-weight: 700; }

.page { padding: 20px max(16px, var(--safe-r)) calc(24px + var(--safe-b)) max(16px, var(--safe-l)); max-width: 1100px; width: 100%; margin: 0 auto; }
.hero { text-align: center; padding: 8px 0 20px; }
.tagline { font-size: 1.15rem; color: var(--muted); margin: 0 0 18px; }
.privacy { font-size: 0.9rem; color: var(--muted); margin: 16px 0 0; }
.section-title { font-size: 1.1rem; margin: 8px 0 12px; color: var(--muted); font-weight: 700; text-transform: uppercase; letter-spacing: 0.06em; }

/* ---------- Knapper ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: var(--tap); padding: 10px 18px; border-radius: 999px;
  border: 2px solid var(--line); background: var(--surface); color: var(--ink);
  font-weight: 700; cursor: pointer; user-select: none; -webkit-user-select: none;
  transition: transform .08s ease, background .15s ease;
}
.btn:active { transform: scale(0.97); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); box-shadow: 0 6px 18px rgba(255, 122, 89, 0.35); }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-ghost { border-color: transparent; background: transparent; color: var(--accent); }
.btn-big { font-size: 1.15rem; padding: 14px 28px; min-height: 56px; }
.btn-big svg { width: 28px; height: 28px; }

.icon-btn {
  width: var(--tap); height: var(--tap); border-radius: 14px; border: none; background: transparent;
  color: var(--ink); display: inline-flex; align-items: center; justify-content: center; cursor: pointer; flex: none;
}
.icon-btn:hover { background: rgba(43, 33, 64, 0.06); }
.icon-btn:active { transform: scale(0.94); }
.icon-btn:disabled { opacity: 0.3; cursor: default; }
.icon-btn.danger { color: var(--danger); }
button:focus-visible, a:focus-visible, [tabindex]:focus-visible { outline: 3px solid var(--accent-2); outline-offset: 2px; }

/* ---------- Galleri ---------- */
.empty { text-align: center; padding: 40px 16px; color: var(--muted); display: flex; flex-direction: column; align-items: center; gap: 10px; }
.empty-art svg { width: 72px; height: 72px; opacity: 0.4; }
.empty h2 { margin: 0; color: var(--ink); }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 14px; }
.card { display: flex; flex-direction: column; background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; transition: transform .12s ease; }
.card:hover { transform: translateY(-2px); }
.thumb { aspect-ratio: 1; background: #fff; display: flex; align-items: center; justify-content: center; border-bottom: 1px solid var(--line); }
.thumb img { width: 100%; height: 100%; object-fit: contain; display: block; }
.thumb-placeholder svg { width: 48px; height: 48px; opacity: 0.3; }
.card-body { display: flex; align-items: center; gap: 4px; padding: 8px 6px 8px 12px; }
.card-text { flex: 1; min-width: 0; }
.card-name { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card-date { font-size: 0.8rem; color: var(--muted); }
.card-actions { display: flex; }
.card-actions .icon-btn { width: 40px; height: 40px; }
.card-cta { background: var(--accent); color: #fff; text-align: center; font-weight: 700; padding: 10px; }

.section-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.section-row .section-title { margin: 0; }
.card-btn { border: none; padding: 0; text-align: left; cursor: pointer; font: inherit; color: inherit; }

/* ---------- Min malebog ---------- */
.book-options { display: flex; flex-direction: column; gap: 10px; background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin-bottom: 20px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field .input { margin: 0; }
.check { display: flex; align-items: center; gap: 10px; min-height: 40px; cursor: pointer; }
.check input { width: 24px; height: 24px; accent-color: var(--accent); }
.book-list { list-style: none; margin: 0 0 16px; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.book-page { display: flex; align-items: center; gap: 10px; background: var(--surface); border-radius: 14px; box-shadow: var(--shadow); padding: 6px 8px 6px 12px; cursor: grab; }
.book-page.dragging { opacity: 0.4; }
.book-page.drop-target { outline: 3px dashed var(--accent); }
.book-empty { background: transparent; box-shadow: none; padding: 12px; }
.page-no { font-weight: 800; color: var(--accent); min-width: 1.6em; text-align: center; }
.thumb.small { width: 64px; height: 64px; flex: none; border: 1px solid var(--line); border-radius: 8px; aspect-ratio: auto; }
.book-actions { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; margin: 8px 0 24px; }

/* ---------- Nyt billede ---------- */
.dropzone {
  border: 3px dashed #F3B9A8; border-radius: 24px; background: var(--surface);
  padding: 48px 20px; text-align: center; display: flex; flex-direction: column; align-items: center; gap: 10px;
  cursor: pointer; transition: background .15s ease, border-color .15s ease; font-size: 1.1rem;
}
.dropzone:hover, .dropzone.drag { background: #FFF1EA; border-color: var(--accent); }
.dz-icon { width: 64px; height: 64px; color: var(--accent); }
.preview { display: flex; flex-direction: column; gap: 16px; }
.preview-canvas-wrap { background: #fff; border-radius: var(--radius); box-shadow: var(--shadow); padding: 8px; display: flex; justify-content: center; }
#preview-canvas { max-width: 100%; max-height: 60dvh; width: auto; height: auto; display: block; }
.preview-controls { display: flex; flex-direction: column; gap: 14px; }
.detail-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; justify-content: center; }
.label { font-weight: 700; color: var(--muted); }
.segmented { display: inline-flex; background: var(--surface); border: 2px solid var(--line); border-radius: 999px; padding: 3px; }
.seg-btn { min-height: 44px; min-width: 84px; padding: 8px 16px; border: none; border-radius: 999px; background: transparent; font-weight: 700; color: var(--muted); cursor: pointer; }
.seg-btn.active { background: var(--accent); color: #fff; }
.preview-actions { display: flex; gap: 12px; justify-content: center; flex-wrap: wrap; }

/* ---------- Editor ---------- */
#editor-header .name-btn {
  border: none; background: transparent; font-weight: 700; font-size: 1.05rem; color: var(--ink);
  min-height: var(--tap); padding: 0 8px; border-radius: 12px; max-width: 40vw; cursor: pointer;
}
#editor-header .name-text { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#editor-header .name-btn:hover { background: rgba(43, 33, 64, 0.06); }
#editor-header .status { font-size: 0.8rem; color: var(--muted); min-width: 3.5em; }

.stage-wrap {
  flex: 1; min-height: 0; position: relative; overflow: hidden;
  display: flex; align-items: center; justify-content: center;
  background: #E9E1D8;
  background-image: radial-gradient(rgba(43,33,64,0.08) 1px, transparent 1px);
  background-size: 18px 18px;
}
.canvas-stage {
  position: relative; background: #fff; box-shadow: 0 10px 30px rgba(43,33,64,0.18);
  touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
  transform-origin: center center; will-change: transform;
  cursor: crosshair;
}
.canvas-stage[data-tool="bucket"] { cursor: cell; }
.canvas-stage canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
#stroke-canvas, #line-canvas { pointer-events: none; }
#stroke-canvas { visibility: hidden; }
.cursor {
  position: absolute; left: 0; top: 0; pointer-events: none; border-radius: 50%;
  border: 2px solid rgba(43,33,64,0.7); box-shadow: 0 0 0 1px rgba(255,255,255,0.8);
  mix-blend-mode: difference; opacity: 0.9;
}
.cursor.off { opacity: 0; }
.zoom-reset { color: var(--accent); }

.panel {
  flex: none; background: var(--surface); border-top: 1px solid var(--line);
  padding: 8px max(8px, var(--safe-r)) calc(8px + var(--safe-b)) max(8px, var(--safe-l));
  display: flex; flex-direction: column; gap: 8px;
}
.tools { display: flex; gap: 6px; justify-content: center; }
.tool-btn {
  flex: 1; max-width: 96px; min-height: 56px; border-radius: 14px; border: 2px solid transparent; background: #F6F0EA;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-size: 0.72rem; font-weight: 700; color: var(--muted); cursor: pointer;
}
.tool-btn svg { width: 26px; height: 26px; }
.tool-btn.active { background: #FFE8DF; border-color: var(--accent); color: var(--accent); }
.tool-btn:active { transform: scale(0.96); }

.size { display: flex; align-items: center; gap: 10px; }
.size-preview { width: 44px; height: 44px; display: flex; align-items: center; justify-content: center; flex: none; background: #F6F0EA; border-radius: 12px; }
.size-preview i { display: block; border-radius: 50%; background: var(--accent); box-shadow: 0 0 0 1px rgba(0,0,0,0.15); }
.size input[type="range"] { flex: 1; min-width: 0; height: 44px; accent-color: var(--accent); margin: 0; }
.size-presets { display: flex; gap: 4px; }
.preset-btn { width: 44px; height: 44px; border-radius: 12px; border: 2px solid var(--line); background: var(--surface); font-weight: 800; cursor: pointer; }
.preset-btn:active { background: #FFE8DF; }

.palette { display: grid; grid-template-columns: repeat(15, 1fr); gap: 6px; }
.swatch {
  aspect-ratio: 1; min-height: 40px; width: 100%; border-radius: 12px; border: 2px solid rgba(0,0,0,0.12);
  cursor: pointer; display: flex; align-items: center; justify-content: center; padding: 0;
}
.swatch svg { width: 22px; height: 22px; opacity: 0; color: #fff; filter: drop-shadow(0 0 2px rgba(0,0,0,0.9)); }
.swatch.active { outline: 4px solid var(--ink); outline-offset: 2px; }
.swatch.active svg { opacity: 1; }
.swatch:active { transform: scale(0.92); }

/* Smalle skærme: to rækker farver */
@media (max-width: 560px) {
  .palette { grid-template-columns: repeat(8, 1fr); }
  .swatch { min-height: 44px; }
  .tool-btn span { font-size: 0.68rem; }
}

/* Bred skærm / landskab: værktøjer til venstre, farver til højre */
@media (min-width: 900px) and (min-aspect-ratio: 4/3) {
  #view-editor.active { display: grid; grid-template-rows: auto 1fr auto; grid-template-columns: 136px 1fr 124px; grid-template-areas: "hdr hdr hdr" "tools stage pal" "size stage pal"; }
  #editor-header { grid-area: hdr; }
  .stage-wrap { grid-area: stage; }
  .panel { display: contents; }
  .tools { grid-area: tools; flex-direction: column; justify-content: flex-start; padding: 12px 10px 0; background: var(--surface); border-right: 1px solid var(--line); gap: 8px; overflow-y: auto; }
  .tool-btn { flex: 0 0 auto; max-width: none; width: 100%; height: 64px; }
  .size { grid-area: size; flex-direction: column; padding: 12px 10px calc(16px + var(--safe-b)); gap: 8px; background: var(--surface); border-right: 1px solid var(--line); }
  .size input[type="range"] { writing-mode: vertical-lr; direction: rtl; width: 44px; height: 120px; }
  .size-presets { flex-direction: row; gap: 3px; }
  .size-presets .preset-btn { width: 36px; height: 40px; }
  .palette { grid-area: pal; grid-template-columns: repeat(2, 1fr); align-content: start; padding: 12px 10px; background: var(--surface); border-left: 1px solid var(--line); gap: 8px; overflow-y: auto; }
}

/* ---------- Dialoger / toast / spinner ---------- */
.dlg { border: none; border-radius: 22px; padding: 22px 22px 18px; max-width: min(92vw, 440px); width: 100%; box-shadow: 0 20px 60px rgba(43,33,64,0.3); background: var(--surface); color: var(--ink); }
.dlg::backdrop { background: rgba(43,33,64,0.45); }
.dlg h2 { margin: 0 0 10px; font-size: 1.25rem; }
.dlg p { margin: 0 0 16px; color: var(--muted); }
.dlg-body { max-height: 60dvh; overflow: auto; margin-bottom: 8px; }
.dlg-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 12px; }
.input { width: 100%; font-size: 16px; padding: 12px 14px; border-radius: 12px; border: 2px solid var(--line); margin: 4px 0 6px; }
.news-entry h3 { margin: 12px 0 6px; font-size: 1.05rem; }
.news-entry h3 small { color: var(--muted); font-weight: 400; font-size: 0.8rem; margin-left: 6px; }
.news-entry ul { margin: 0 0 8px; padding-left: 20px; }
.news-entry li { margin: 4px 0; }

#toast {
  position: fixed; left: 50%; bottom: calc(24px + var(--safe-b)); transform: translate(-50%, 20px);
  background: var(--ink); color: #fff; padding: 12px 18px; border-radius: 999px; font-weight: 600;
  opacity: 0; pointer-events: none; transition: opacity .2s ease, transform .2s ease; z-index: 50; max-width: 92vw; text-align: center;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast[data-kind="error"] { background: var(--danger); }
#toast[data-kind="success"] { background: #2E7D32; }

#busy { position: fixed; inset: 0; background: rgba(255,248,240,0.75); display: none; align-items: center; justify-content: center; flex-direction: column; gap: 14px; z-index: 40; font-weight: 700; color: var(--ink); }
#busy.show { display: flex; }
.spinner { width: 52px; height: 52px; border-radius: 50%; border: 6px solid rgba(255,122,89,0.25); border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation-duration: 1.5s !important; }
}
