/* ==========================================================================
   Städschema Studio – appens gränssnitt (skärm)
   ========================================================================== */
:root {
  --ui-bg: #EEF1F5;
  --ui-panel: #FFFFFF;
  --ui-border: #DFE4EB;
  --ui-border-strong: #C9D1DC;
  --ui-text: #1C2733;
  --ui-muted: #5F6E7D;
  --ui-accent: #005AA7;
  --ui-accent-hover: #08809D;
  --ui-accent-soft: #E6F0FA;
  --ui-danger: #B42318;
  --ui-warn-bg: #FFF4E5;
  --ui-warn-fg: #8A4B00;
  --ui-ok-bg: #E7F6EC;
  --ui-ok-fg: #1B5E20;
  --ui-shadow: 0 8px 24px rgba(20, 40, 60, .14);
  --ui-radius: 10px;
  --ui-font: "Inter", "Segoe UI", system-ui, -apple-system, sans-serif;
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--ui-font);
  font-size: 13px;
  line-height: 1.45;
  color: var(--ui-text);
  background: var(--ui-bg);
  display: grid;
  grid-template-rows: 54px 1fr;
  grid-template-columns: 250px minmax(0, 1fr) 340px;
  grid-template-areas: "top top top" "side canvas insp";
  height: 100vh;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--ui-accent); outline-offset: 2px; }
svg { display: block; }

/* ---------- Verktygsfält ---------- */
#topbar {
  grid-area: top;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 14px 0 16px;
  background: var(--ui-panel);
  border-bottom: 1px solid var(--ui-border);
  z-index: 20;
}
.brand { display: flex; align-items: center; gap: 10px; margin-right: 6px; }
.brand-mark {
  width: 30px; height: 30px; border-radius: 9px;
  background: linear-gradient(145deg, #005AA7, #1BB998);
  display: flex; align-items: center; justify-content: center; color: #fff;
}
.brand-mark svg { width: 17px; height: 17px; }
.brand-name { font-weight: 700; font-size: 14px; letter-spacing: -.01em; }
.brand-name small { display: block; font-weight: 500; font-size: 11px; color: var(--ui-muted); letter-spacing: .02em; margin-top: -2px; }
#doc-title { font-weight: 600; color: var(--ui-muted); padding-left: 12px; border-left: 1px solid var(--ui-border); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 260px; }
#save-state { font-size: 11.5px; color: var(--ui-muted); white-space: nowrap; display: inline-flex; align-items: center; gap: 6px; }
#save-state.pending { color: var(--ui-accent); }
#save-state .cloud-dot { width: 8px; height: 8px; border-radius: 50%; background: #9AA5B1; flex: none; }
#save-state.ok .cloud-dot { background: #2E9E5B; }
#save-state.pending .cloud-dot { background: var(--ui-accent); animation: pulse 1s infinite alternate; }
#save-state.warn .cloud-dot { background: #E0A100; }
#save-state.err .cloud-dot { background: var(--ui-danger); }
#save-state.err { color: var(--ui-danger); }
@keyframes pulse { from { opacity: .35; } to { opacity: 1; } }
.spacer { flex: 1; }
.tb-group { display: flex; align-items: center; gap: 4px; padding: 0 8px; border-left: 1px solid var(--ui-border); }
.tb-group:first-of-type { border-left: 0; }
.tb-group[hidden] { display: none; }
.btn-icon {
  all: unset; cursor: pointer; width: 32px; height: 32px; border-radius: 8px;
  display: inline-flex; align-items: center; justify-content: center; color: #3b4754;
}
.btn-icon:hover { background: #EEF3F8; color: var(--ui-accent); }
.btn-icon:disabled { opacity: .35; cursor: default; background: none; }
.btn-icon:focus-visible { outline: 2px solid var(--ui-accent); }
.btn-icon svg { width: 17px; height: 17px; }
.btn-icon.danger:hover { background: #FDECEC; color: var(--ui-danger); }
#zoom-val { min-width: 46px; text-align: center; font-variant-numeric: tabular-nums; font-weight: 600; color: var(--ui-muted); font-size: 12px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 34px; padding: 0 14px; border-radius: 8px;
  border: 1px solid var(--ui-border-strong); background: #fff; color: var(--ui-text);
  font-weight: 600; font-size: 13px; white-space: nowrap; cursor: pointer;
  transition: background .12s, border-color .12s, box-shadow .12s;
}
.btn:hover { background: #F5F8FB; border-color: #B8C4D2; }
.btn svg { width: 16px; height: 16px; }
.btn-primary { background: var(--ui-accent); border-color: var(--ui-accent); color: #fff; }
.btn-primary:hover { background: var(--ui-accent-hover); border-color: var(--ui-accent-hover); }
.btn-sm { height: 30px; padding: 0 11px; font-size: 12.5px; }
.btn-danger { background: var(--ui-danger); border-color: var(--ui-danger); color: #fff; }
.btn-danger:hover { background: #8f1c14; }
.btn-danger-ghost { color: var(--ui-danger); }
.btn-danger-ghost:hover { background: #FDECEC; border-color: #F3B1AC; }
.seg { display: inline-flex; background: #EEF1F5; border-radius: 8px; padding: 3px; gap: 2px; }
.seg button {
  all: unset; cursor: pointer; padding: 5px 11px; border-radius: 6px; font-weight: 600; font-size: 12.5px; color: var(--ui-muted); white-space: nowrap;
  display: inline-flex; align-items: center; gap: 6px;
}
.seg button:hover { color: var(--ui-text); }
.seg button.active { background: #fff; color: var(--ui-text); box-shadow: 0 1px 2px rgba(0, 0, 0, .12); }
.seg button:focus-visible { outline: 2px solid var(--ui-accent); }
.seg button svg { width: 14px; height: 14px; }

/* ---------- Sidlista ---------- */
#sidebar {
  grid-area: side;
  background: var(--ui-panel);
  border-right: 1px solid var(--ui-border);
  display: flex; flex-direction: column; min-height: 0;
}
.panel-head { padding: 14px 16px 8px; font-size: 11px; font-weight: 700; letter-spacing: .1em; text-transform: uppercase; color: var(--ui-muted); display: flex; align-items: center; justify-content: space-between; }
#page-list { list-style: none; margin: 0; padding: 4px 8px 8px; overflow: auto; flex: 1; }
#page-list li {
  display: flex; align-items: center; gap: 9px; padding: 7px 8px; border-radius: 8px; cursor: pointer; margin-bottom: 2px;
  border: 1px solid transparent;
}
#page-list li:hover { background: #F3F6FA; }
#page-list li.active { background: var(--ui-accent-soft); border-color: #C9DDF1; }
#page-list li.hidden-page { opacity: .6; }
.pl-num { width: 18px; font-size: 11px; color: var(--ui-muted); font-variant-numeric: tabular-nums; text-align: right; flex: none; }
.pl-icon { width: 26px; height: 26px; border-radius: 7px; background: #EEF1F5; color: #4d5b69; display: inline-flex; align-items: center; justify-content: center; flex: none; }
#page-list li.active .pl-icon { background: #fff; color: var(--ui-accent); }
.pl-icon svg { width: 14px; height: 14px; }
.pl-text { min-width: 0; flex: 1; display: flex; flex-direction: column; }
.pl-title { font-weight: 600; font-size: 12.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pl-sub { font-size: 11px; color: var(--ui-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pl-dot, .dot { width: 10px; height: 10px; border-radius: 50%; flex: none; border: 1px solid rgba(0, 0, 0, .12); }
.dot.type-sur, .pl-dot.type-sur { background: #F6C1BC; }
.dot.type-basisk, .pl-dot.type-basisk { background: #C3E3B3; }
.dot.type-desinfektion, .pl-dot.type-desinfektion { background: #BFD9F5; }
.dot.type-ovrigt, .pl-dot.type-ovrigt { background: #E6E7E3; }
.pl-warn { color: #B26A00; flex: none; }
.pl-warn svg { width: 14px; height: 14px; }
.sidebar-foot { padding: 10px 12px; border-top: 1px solid var(--ui-border); }
.sidebar-foot .btn { width: 100%; }

/* ---------- Arbetsyta ---------- */
#canvas-root {
  grid-area: canvas;
  overflow: auto;
  position: relative;
  background:
    radial-gradient(circle at 1px 1px, rgba(20, 40, 60, .10) 1px, transparent 1px) 0 0 / 22px 22px,
    var(--ui-bg);
}
#canvas { padding: 32px 32px 80px; display: flex; flex-direction: column; align-items: center; width: max-content; min-width: 100%; }
#hintbar {
  position: sticky; top: 0; z-index: 15;
  display: flex; align-items: center; gap: 10px;
  padding: 8px 14px;
  background: rgba(255, 255, 255, .94);
  backdrop-filter: blur(6px);
  border-bottom: 1px solid var(--ui-border);
  font-size: 12.5px; color: var(--ui-muted);
}
#hintbar[hidden] { display: none; }
#hintbar .hint-text { flex: 1; }
#hintbar b { color: var(--ui-text); }
#hintbar .btn-icon { width: 26px; height: 26px; font-size: 12px; }
body.mode-preview #hintbar { display: none; }
.reg-card { display: flex; flex-direction: column; gap: 6px; padding: 8px; border-radius: 10px; background: #F5F8FB; }
.reg-line { display: flex; align-items: center; gap: 6px; }
.reg-line .input { flex: 1; min-width: 0; height: 30px; font-size: 12.5px; }
.reg-line .btn-icon { width: 28px; height: 28px; flex: none; }
.insp-details { border-bottom: 1px solid var(--ui-border); padding: 12px 0; }
.insp-details summary { cursor: pointer; font-size: 11px; font-weight: 700; letter-spacing: .1em; text-transform: uppercase; color: var(--ui-muted); list-style: none; display: flex; align-items: center; gap: 6px; }
.insp-details summary::-webkit-details-marker { display: none; }
.insp-details summary::before { content: '▸'; font-size: 10px; }
.insp-details[open] summary::before { content: '▾'; }
.insp-details > div { margin-top: 10px; }
#loading { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; color: var(--ui-muted); font-weight: 600; }

/* ---------- Inspektör ---------- */
#inspector {
  grid-area: insp;
  background: var(--ui-panel);
  border-left: 1px solid var(--ui-border);
  display: flex; flex-direction: column; min-height: 0;
}
#insp-tabs { display: flex; gap: 2px; padding: 10px 12px 0; border-bottom: 1px solid var(--ui-border); }
#insp-tabs button {
  all: unset; cursor: pointer; padding: 8px 10px 10px; font-weight: 600; font-size: 12.5px; color: var(--ui-muted);
  border-bottom: 2px solid transparent; margin-bottom: -1px;
}
#insp-tabs button:hover { color: var(--ui-text); }
#insp-tabs button.active { color: var(--ui-accent); border-color: var(--ui-accent); }
#insp-tabs button:focus-visible { outline: 2px solid var(--ui-accent); border-radius: 4px; }
#inspector-body { overflow: auto; flex: 1; padding: 6px 16px 30px; }
.insp-section { padding: 14px 0 12px; border-bottom: 1px solid var(--ui-border); }
.insp-section:last-child { border-bottom: 0; }
.insp-section h4 { margin: 0 0 10px; font-size: 11px; font-weight: 700; letter-spacing: .1em; text-transform: uppercase; color: var(--ui-muted); }
.insp-section h4:empty { display: none; }
.insp-muted, .insp-text { color: var(--ui-muted); margin: 0 0 10px; font-size: 12.5px; }
.insp-hint { display: block; color: var(--ui-muted); font-size: 11.5px; margin-top: 8px; line-height: 1.4; }
.field { display: flex; flex-direction: column; gap: 5px; margin-bottom: 10px; }
.field > span { font-size: 12px; font-weight: 600; color: #3b4754; }
.field small { color: var(--ui-muted); font-size: 11px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.input {
  width: 100%; height: 34px; padding: 0 10px; border-radius: 8px; border: 1px solid var(--ui-border-strong); background: #fff;
  font-size: 13px; transition: border-color .12s, box-shadow .12s;
}
textarea.input { height: auto; min-height: 58px; padding: 7px 10px; resize: vertical; line-height: 1.4; }
.input:focus { outline: none; border-color: var(--ui-accent); box-shadow: 0 0 0 3px rgba(0, 90, 167, .15); }
.input.narrow { width: auto; min-width: 0; }
select.input { appearance: auto; }
.check { display: flex; align-items: flex-start; gap: 8px; margin: 6px 0 10px; font-size: 12.5px; cursor: pointer; }
.check input { margin: 2px 0 0; accent-color: var(--ui-accent); }
.chipbox { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; padding: 5px 10px; border-radius: 999px; border: 1px solid var(--ui-border-strong);
  background: #fff; font-size: 12px; font-weight: 600; cursor: pointer; user-select: none; transition: all .12s;
}
.chip:hover { border-color: var(--ui-accent); }
.chip.on { background: var(--ui-accent); border-color: var(--ui-accent); color: #fff; }
.chip input { position: absolute; opacity: 0; width: 0; height: 0; }
.chip:focus-within { box-shadow: 0 0 0 3px rgba(0, 90, 167, .25); }
.machine-list { display: flex; flex-direction: column; gap: 6px; }
.machine-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.stepper { display: inline-flex; align-items: center; gap: 2px; border: 1px solid var(--ui-border-strong); border-radius: 999px; padding: 2px; background: #fff; }
.stepper button { all: unset; cursor: pointer; width: 22px; height: 22px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center; font-weight: 700; color: var(--ui-accent); }
.stepper button:hover { background: var(--ui-accent-soft); }
.stepper button:disabled { color: #B8C2CC; cursor: default; background: none; }
.stepper button:focus-visible { outline: 2px solid var(--ui-accent); }
.stepper-val { min-width: 34px; text-align: center; font-size: 12px; font-weight: 600; font-variant-numeric: tabular-nums; }
.btn-row { display: flex; flex-wrap: wrap; gap: 6px; }
.btn-col { display: flex; flex-direction: column; gap: 6px; }
.btn-col .btn { width: 100%; }
.kem-preview { display: flex; align-items: center; gap: 8px; padding: 8px 10px; border-radius: 8px; background: #F5F8FB; font-size: 12.5px; }
.kem-preview span:last-child { color: var(--ui-muted); }
.fit-info { padding: 9px 11px; border-radius: 8px; font-size: 12.5px; line-height: 1.4; }
.fit-info.ok { background: var(--ui-ok-bg); color: var(--ui-ok-fg); }
.fit-info.warn { background: var(--ui-warn-bg); color: var(--ui-warn-fg); }
.fit-info.bad { background: #FDECEC; color: var(--ui-danger); }
.img-preview { border-radius: 10px; overflow: hidden; border: 1px solid var(--ui-border); background: #f3f5f8; aspect-ratio: 3/2; display: flex; align-items: center; justify-content: center; margin-bottom: 10px; color: var(--ui-muted); }
.img-preview img { width: 100%; height: 100%; object-fit: cover; }
.img-preview.small { aspect-ratio: 210/297; width: 90px; }
.color-row { display: flex; align-items: center; gap: 8px; }
.color-row input[type="color"] { width: 40px; height: 32px; padding: 2px; border: 1px solid var(--ui-border-strong); border-radius: 8px; background: #fff; cursor: pointer; }
.color-row code { font-size: 11.5px; color: var(--ui-muted); }
.range-row { display: flex; align-items: center; gap: 10px; }
.range-row input { flex: 1; accent-color: var(--ui-accent); }
.range-val { width: 40px; text-align: right; font-size: 12px; color: var(--ui-muted); font-variant-numeric: tabular-nums; }
.theme-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.theme-card {
  all: unset; cursor: pointer; display: flex; flex-direction: column; gap: 2px; padding: 6px; border-radius: 10px; border: 1px solid var(--ui-border);
  background: #fff; transition: border-color .12s, box-shadow .12s;
}
.theme-card:hover { border-color: var(--ui-accent); }
.theme-card.active { border-color: var(--ui-accent); box-shadow: 0 0 0 2px rgba(0, 90, 167, .25); }
.theme-card:focus-visible { outline: 2px solid var(--ui-accent); }
.tc-prev { position: relative; height: 74px; border-radius: 6px; overflow: hidden; border: 1px solid rgba(0, 0, 0, .08); display: block; }
.tc-bg { position: absolute; inset: 0; background-size: cover; background-position: center; mix-blend-mode: multiply; }
.tc-title { position: absolute; left: 10px; top: 12px; width: 52%; height: 9px; border-radius: 3px; }
.tc-acc { position: absolute; left: 10px; top: 28px; width: 28%; height: 5px; border-radius: 2px; }
.tc-line { position: absolute; left: 10px; top: 42px; width: 70%; height: 4px; border-radius: 2px; }
.tc-line.short { top: 52px; width: 55%; }
.tc-name { font-weight: 700; font-size: 12.5px; margin-top: 4px; }
.tc-season { font-size: 11px; color: var(--ui-muted); }
.reg-list { display: flex; flex-direction: column; gap: 6px; margin-bottom: 8px; }
.reg-row { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; padding: 6px; border-radius: 8px; background: #F5F8FB; }
.reg-row .input { flex: 1; min-width: 90px; height: 30px; font-size: 12.5px; }
.reg-row select.input { flex: 0 1 auto; max-width: 130px; }
.reg-row .btn-icon { width: 28px; height: 28px; }
.reg-icon { width: 26px; height: 26px; object-fit: contain; }
.tips { margin: 0; padding-left: 18px; color: var(--ui-muted); font-size: 12px; }
.tips li { margin-bottom: 5px; }

/* ---------- Popover ---------- */
.popover {
  position: fixed; z-index: 100; min-width: 240px; max-width: 340px; padding: 12px; border-radius: 12px;
  background: #fff; border: 1px solid var(--ui-border); box-shadow: var(--ui-shadow); font-family: var(--ui-font); font-size: 13px; color: var(--ui-text);
  animation: pop .12s ease-out;
}
@keyframes pop { from { opacity: 0; transform: translateY(-4px); } to { opacity: 1; transform: none; } }
.pop-title { font-weight: 700; font-size: 12px; letter-spacing: .06em; text-transform: uppercase; color: var(--ui-muted); margin-bottom: 8px; }
.pop-text { margin: 0 0 8px; color: var(--ui-muted); font-size: 12.5px; }
.pop-list { margin: 0 0 10px; padding-left: 18px; font-size: 12.5px; color: var(--ui-muted); max-height: 140px; overflow: auto; }
.pop-scroll { max-height: 320px; overflow: auto; margin: 0 -4px; padding: 0 4px; }
.pop-item {
  all: unset; cursor: pointer; display: flex; align-items: center; gap: 8px; width: 100%; padding: 7px 8px; border-radius: 7px; box-sizing: border-box;
}
.pop-item:hover { background: #F3F6FA; }
.pop-item.active { background: var(--ui-accent-soft); }
.pop-item:focus-visible { outline: 2px solid var(--ui-accent); }
.pop-muted { color: var(--ui-muted); font-size: 12px; margin-left: auto; }
.pop-item.lib-item { flex-direction: column; align-items: flex-start; gap: 1px; }
.lib-title { font-weight: 600; }
.lib-prev { font-size: 11.5px; color: var(--ui-muted); line-height: 1.3; }
.popover .chipbox { max-width: 320px; }
.popover .input { margin-bottom: 8px; }

/* ---------- Modal ---------- */
.modal-back { position: fixed; inset: 0; background: rgba(15, 25, 35, .45); display: flex; align-items: center; justify-content: center; z-index: 200; }
.modal { background: #fff; border-radius: 14px; padding: 22px 24px; width: min(440px, 92vw); box-shadow: 0 20px 60px rgba(0, 0, 0, .3); }
.modal h3 { margin: 0 0 8px; font-size: 16px; }
.modal p { margin: 0 0 18px; color: var(--ui-muted); }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; }

/* ---------- Toasts ---------- */
#toasts { position: fixed; left: 50%; bottom: 22px; transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; z-index: 300; pointer-events: none; }
.toast {
  display: flex; align-items: center; gap: 12px; padding: 10px 14px; border-radius: 10px; background: #1C2733; color: #fff; font-size: 13px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .3); opacity: 0; transform: translateY(8px); transition: all .2s; pointer-events: auto; max-width: 520px;
}
.toast.show { opacity: 1; transform: none; }
.toast.error { background: var(--ui-danger); }
.toast-btn { all: unset; cursor: pointer; font-weight: 700; color: #8FD3FF; padding: 2px 4px; border-radius: 4px; }
.toast-btn:hover { text-decoration: underline; }

/* ---------- Låsskärm ---------- */
#gate {
  position: fixed; inset: 0; z-index: 1000;
  display: grid; place-items: center; padding: 24px;
  background: radial-gradient(120% 90% at 10% 0%, #1BB998 0%, transparent 55%), linear-gradient(160deg, #0B3D63 0%, #005AA7 55%, #08809D 100%);
  transition: opacity .35s ease;
}
#gate[hidden] { display: none; }
#gate.gate-out { opacity: 0; pointer-events: none; }
.gate-card {
  width: min(380px, 100%); background: #fff; border-radius: 18px; padding: 34px 32px 30px;
  box-shadow: 0 30px 80px -20px rgba(0, 20, 40, .55); text-align: center;
  display: flex; flex-direction: column; align-items: center; gap: 8px;
}
.gate-card.shake { animation: gate-shake .45s; }
@keyframes gate-shake { 20%, 60% { transform: translateX(-8px); } 40%, 80% { transform: translateX(8px); } }
.gate-mark { width: 54px; height: 54px; border-radius: 16px; background: linear-gradient(145deg, #005AA7, #1BB998); color: #fff; display: flex; align-items: center; justify-content: center; margin-bottom: 6px; }
.gate-mark svg { width: 28px; height: 28px; }
.gate-card h1 { margin: 0; font-size: 22px; letter-spacing: -.01em; }
.gate-sub { margin: 0 0 14px; color: var(--ui-muted); font-size: 13px; }
.gate-label { align-self: flex-start; font-size: 12px; font-weight: 600; color: #3b4754; }
.gate-input {
  width: 100%; height: 56px; border-radius: 12px; border: 1.5px solid var(--ui-border-strong);
  font-size: 26px; letter-spacing: .45em; text-align: center; padding: 0 0 0 .45em; font-variant-numeric: tabular-nums;
}
.gate-input:focus { outline: none; border-color: var(--ui-accent); box-shadow: 0 0 0 4px rgba(0, 90, 167, .18); }
.gate-error { margin: 2px 0 0; color: var(--ui-danger); font-size: 12.5px; font-weight: 600; align-self: flex-start; }
.gate-btn { width: 100%; height: 44px; font-size: 14px; margin-top: 10px; }

/* ---------- Läge: förhandsgranskning / utskrift ---------- */
body.mode-preview #canvas-root { background: #DCE2E9; }
body.printing #topbar, body.printing #sidebar, body.printing #inspector { display: none; }
body.printing { grid-template-columns: 1fr; grid-template-areas: "canvas"; grid-template-rows: 1fr; }

@media (max-width: 1100px) {
  body { grid-template-columns: 210px minmax(0, 1fr) 300px; }
}
