:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-2: #eef2f6;
  --text: #17202b;
  --muted: #5b6776;
  --line: #d6dde5;
  --accent: #1f6fd1;
  --accent-text: #ffffff;
  --brand: #1f3a5c;
  --stage-bg: #e8eef4;
  --outline: #1d2229;
  --select: #ffd400;
  --radius: 10px;
  --panel-w: 360px;
  --shadow: 0 6px 24px rgba(15, 30, 50, 0.16);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #11161d;
    --surface: #18202a;
    --surface-2: #212b37;
    --text: #e8edf3;
    --muted: #9aa8b8;
    --line: #2e3a48;
    --accent: #5aa2ff;
    --accent-text: #0b1420;
    --brand: #0d1826;
    --stage-bg: #1b2735;
    --outline: #0a0d11;
    --shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
  }
}
:root[data-theme="dark"] {
  --bg: #11161d; --surface: #18202a; --surface-2: #212b37; --text: #e8edf3; --muted: #9aa8b8;
  --line: #2e3a48; --accent: #5aa2ff; --accent-text: #0b1420; --brand: #0d1826; --stage-bg: #1b2735; --outline: #0a0d11;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}
.visually-hidden { position: absolute !important; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* Toppen */
.topbar {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 12px; padding-top: max(8px, env(safe-area-inset-top));
  background: var(--brand); color: #fff;
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 17px; margin-right: auto; }
.topbar select, .topbar .btn {
  min-height: 40px; border-radius: 8px; border: 1px solid rgba(255,255,255,0.35);
  background: rgba(255,255,255,0.12); color: #fff; padding: 0 12px; font: inherit;
}
.topbar select option { color: #111; }
.topbar .btn[aria-pressed="true"] { background: #fff; color: var(--brand); }

/* Huvudyta */
.layout { flex: 1; display: flex; min-height: 0; position: relative; }
.stage { flex: 1; position: relative; min-width: 0; background: var(--stage-bg); }
#view { width: 100%; height: 100%; display: block; touch-action: none; outline: none; }
.loading {
  position: absolute; inset: 0; display: grid; place-items: center;
  font-weight: 600; color: var(--muted); pointer-events: none;
}
.loading[hidden] { display: none; }
.hint {
  position: absolute; left: 12px; bottom: 10px; right: 12px; margin: 0;
  font-size: 13px; color: var(--muted); pointer-events: none; text-shadow: 0 1px 0 var(--stage-bg);
}
.hint .short { display: none; }
.labels { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.labels .lbl {
  position: absolute; transform: translate(-50%, -100%);
  background: var(--surface); color: var(--text); border: 1px solid var(--line);
  border-radius: 8px; padding: 4px 8px; font-size: 13px; white-space: nowrap; box-shadow: var(--shadow);
}
.labels .lbl small { display: block; color: var(--muted); font-size: 11px; }

/* Delkort */
.part-card {
  position: absolute; top: 12px; left: 12px; width: min(380px, calc(100% - 24px));
  background: var(--surface); border: 1px solid var(--line); border-left: 5px solid var(--select);
  border-radius: var(--radius); padding: 12px 14px; box-shadow: var(--shadow);
  max-height: calc(100% - 24px); overflow: auto;
}
.part-card h2 { margin: 2px 0 0; font-size: 18px; padding-right: 28px; }
.part-card .en { margin: 0 0 6px; color: var(--muted); font-style: italic; }
.part-card .sys { margin: 0; font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.part-card .desc { margin: 6px 0; }
.part-card .size { margin: 4px 0 10px; font-size: 13px; color: var(--muted); }
.part-card .close {
  position: absolute; top: 6px; right: 6px; width: 36px; height: 36px; border-radius: 8px;
  border: none; background: transparent; color: var(--text); font-size: 24px; cursor: pointer;
}

/* Panel */
.panel {
  width: var(--panel-w); background: var(--surface); border-left: 1px solid var(--line);
  display: flex; flex-direction: column; min-height: 0;
}
.panel-handle { display: none; }
.tabs { display: flex; border-bottom: 1px solid var(--line); }
.tabs button {
  flex: 1; min-height: 46px; border: none; background: transparent; color: var(--muted);
  font: inherit; font-weight: 600; cursor: pointer; border-bottom: 3px solid transparent;
}
.tabs button[aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); }
.panel-body { overflow: auto; padding: 4px 14px 16px; padding-bottom: max(16px, env(safe-area-inset-bottom)); flex: 1; }
.panel h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin: 16px 0 8px; display: flex; justify-content: space-between; }
.panel h3 output { color: var(--text); font-weight: 700; }
.muted { color: var(--muted); font-size: 14px; }
input[type="range"] { width: 100%; accent-color: var(--accent); height: 32px; }

.btn {
  min-height: 40px; padding: 0 14px; border-radius: 8px; border: 1px solid var(--line);
  background: var(--surface-2); color: var(--text); font: inherit; font-weight: 600; cursor: pointer;
}
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn:focus-visible, .tabs button:focus-visible, select:focus-visible, .chip:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }

.segmented { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.segmented button {
  min-height: 44px; border-radius: 8px; border: 1px solid var(--line); background: var(--surface-2);
  color: var(--text); font: inherit; font-size: 13px; font-weight: 600; cursor: pointer; padding: 4px;
}
.segmented button[aria-pressed="true"] { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; min-height: 36px; padding: 0 12px;
  border-radius: 999px; border: 1px solid var(--line); background: var(--surface-2); color: var(--text);
  font: inherit; font-size: 13px; cursor: pointer;
}
.chip .dot { width: 10px; height: 10px; border-radius: 50%; }
.chip[aria-pressed="false"] { opacity: .5; text-decoration: line-through; }

.switch { display: flex; align-items: center; gap: 10px; min-height: 40px; cursor: pointer; }
.switch input { width: 20px; height: 20px; accent-color: var(--accent); }
.counter { color: var(--muted); font-size: 13px; }

/* Dellista */
.tree details { border-bottom: 1px solid var(--line); }
.tree summary { min-height: 44px; display: flex; align-items: center; gap: 8px; cursor: pointer; font-weight: 600; }
.tree summary .count { margin-left: auto; color: var(--muted); font-weight: 400; font-size: 13px; }
.tree ul { list-style: none; margin: 0 0 8px; padding: 0; }
.tree li { display: flex; align-items: center; gap: 8px; min-height: 38px; }
.tree li input { width: 20px; height: 20px; flex: none; accent-color: var(--accent); }
.tree li button {
  flex: 1; text-align: left; border: none; background: transparent; color: var(--text); font: inherit;
  padding: 6px 4px; border-radius: 6px; cursor: pointer;
}
.tree li button small { color: var(--muted); }
.tree li.selected button { background: color-mix(in srgb, var(--select) 35%, transparent); }

/* Fakta */
.facts h2 { margin: 12px 0 2px; font-size: 20px; }
.facts .role { color: var(--muted); margin: 0 0 8px; }
.facts table { width: 100%; border-collapse: collapse; font-size: 14px; margin: 8px 0; }
.facts th, .facts td { text-align: left; padding: 6px 4px; border-bottom: 1px solid var(--line); vertical-align: top; }
.facts th { color: var(--muted); font-weight: 600; }
.facts .note { font-size: 13px; color: var(--muted); background: var(--surface-2); padding: 8px 10px; border-radius: 8px; }
.facts a { color: var(--accent); word-break: break-word; }
.facts li { margin-bottom: 8px; font-size: 14px; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; box-shadow: var(--shadow); z-index: 20;
}

/* Mobil: menyn blir ett ark längst ner */
@media (max-width: 900px) {
  .layout { flex-direction: column; }
  .panel {
    width: 100%; border-left: none; border-top: 1px solid var(--line);
    max-height: 48vh; flex: none; transition: max-height .2s ease;
  }
  .panel.collapsed { max-height: 50px; }
  .panel.collapsed .tabs, .panel.collapsed .panel-body { display: none; }
  .panel-handle {
    display: flex; align-items: center; justify-content: center; gap: 10px; min-height: 48px;
    border: none; background: transparent; color: var(--muted); font: inherit; font-weight: 600; cursor: pointer;
  }
  .grip { width: 36px; height: 4px; border-radius: 4px; background: var(--line); }
  .hint { font-size: 12px; }
  .hint .long { display: none; }
  .hint .short { display: inline; }
  .part-card { top: 8px; left: 8px; width: calc(100% - 16px); }
  .brand span { display: none; }
  .topbar select { flex: 1; min-width: 0; }
}
