:root {
  --u: max(1px, 0.1vmin);
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --ink: #2c2416;
  --ink-soft: #5d5140;
  --paper: rgba(251, 246, 233, 0.8);
  --paper-solid: #fbf6e9;
  --edge: rgba(84, 66, 38, 0.16);
  --edge-strong: rgba(84, 66, 38, 0.55);
  --accent: #8a6a2a;
  --tradition: #7b5a8c;
  --delvis: #9a6b2d;
  --historia: #4f6b3f;
  --focus: #b07a1c;
  --shadow: 0 10px 30px rgba(60, 44, 20, 0.18);
  --bg: #e9dcc0;
  color-scheme: light;
}

:root[data-theme="dark"] {
  --ink: #e9ecf4;
  --ink-soft: #b4bccc;
  --paper: rgba(20, 26, 44, 0.82);
  --paper-solid: #161c2f;
  --edge: rgba(190, 205, 235, 0.22);
  --edge-strong: rgba(200, 214, 240, 0.55);
  --accent: #c9b27a;
  --tradition: #c3a6d6;
  --delvis: #dcb276;
  --historia: #a9c79a;
  --focus: #e8c56c;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
  --bg: #18233f;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--serif);
  -webkit-font-smoothing: antialiased;
}

.app {
  position: fixed;
  inset: 0;
  overflow: hidden;
}

.stage {
  position: absolute;
  inset: 0;
  touch-action: none;
  cursor: grab;
  user-select: none;
  -webkit-user-select: none;
}

.stage:active { cursor: grabbing; }

.stage canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

#fx { pointer-events: none; }

.labels,
.buds {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

button { font: inherit; color: inherit; }

button:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

/* Etiketter som hänger kvar vid grenarna */

.label,
.tag {
  position: absolute;
  left: 0;
  top: 0;
  pointer-events: auto;
  cursor: pointer;
  border: 1px solid var(--edge);
  background: var(--paper);
  -webkit-backdrop-filter: blur(3px);
  backdrop-filter: blur(3px);
  color: var(--ink);
  white-space: nowrap;
  opacity: 0;
  transition: border-color 0.2s, box-shadow 0.2s;
  will-change: transform;
}

.label {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0;
  padding: 2px 7px 3px 8px;
  border-radius: 3px;
  line-height: 1.12;
  text-align: start;
}

.label .name {
  font-size: max(13px, calc(var(--u) * 15.5));
  font-weight: 600;
  letter-spacing: 0.005em;
}

.label .period {
  font-size: max(10.5px, calc(var(--u) * 12));
  font-style: italic;
  color: var(--ink-soft);
}

.label::before {
  content: "";
  position: absolute;
  inset-inline-start: 0;
  top: 3px;
  bottom: 3px;
  width: 4px;
  border-radius: 2px;
  background: var(--historia);
  opacity: 0.85;
}

/* Märkningen syns också i kanten, så att den går att läsa på projektor. */
.label.kind-tradition::before { background: var(--tradition); }
.label.kind-delvis::before { background: var(--delvis); }
.label.kind-tradition { border-color: color-mix(in srgb, var(--tradition) 45%, var(--edge)); }
.label.kind-delvis { border-color: color-mix(in srgb, var(--delvis) 45%, var(--edge)); }
.label.kind-historia { border-color: color-mix(in srgb, var(--historia) 35%, var(--edge)); }

.label.tip .name { font-size: max(12.5px, calc(var(--u) * 15)); }

.label.hidden,
.tag.hidden { pointer-events: none; }

.compact .label { padding: 1px 6px 2px 7px; }
.compact .label .period { display: none; }
.compact .label .name { font-size: max(12px, calc(var(--u) * 14.5)); }

.label.marker {
  border-style: dashed;
  background: transparent;
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
}

.label.marker .name { font-style: italic; font-weight: 600; }

.label:hover,
.tag:hover { border-color: var(--edge-strong); }

.label.selected {
  border-color: var(--edge-strong);
  box-shadow: 0 0 0 3px rgba(255, 214, 120, 0.35);
}

:root[data-theme="dark"] .label.selected { box-shadow: 0 0 0 3px rgba(180, 205, 255, 0.28); }

.tag {
  padding: 0 6px 1px;
  border-radius: 10px;
  font-size: max(10.5px, calc(var(--u) * 12.5));
  font-style: italic;
  color: var(--ink-soft);
  background: var(--paper);
}

/* Skott */

.bud {
  position: absolute;
  left: 0;
  top: 0;
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  pointer-events: auto;
  cursor: pointer;
}

/* Rubrik och kontroller */

.title {
  position: absolute;
  z-index: 2;
  top: 14px;
  left: 18px;
  pointer-events: none;
}

.title-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.title h1 {
  margin: 0;
  font-size: max(30px, calc(var(--u) * 36));
  font-weight: 600;
  letter-spacing: 0.01em;
  line-height: 1.05;
  text-shadow: 0 1px 12px rgba(255, 246, 225, 0.7);
}

:root[data-theme="dark"] .title h1 { text-shadow: 0 1px 14px rgba(10, 14, 30, 0.8); }

.progress {
  margin: 4px 0 0;
  font-size: max(13px, calc(var(--u) * 15));
  font-style: italic;
  color: var(--ink-soft);
}

.info-button {
  pointer-events: auto;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 1px solid var(--edge-strong);
  background: var(--paper);
  font-size: 15px;
  font-style: italic;
  font-weight: 600;
  line-height: 1;
  cursor: pointer;
}

.controls {
  --b: max(34px, calc(var(--u) * 40));
  position: absolute;
  z-index: 2;
  left: 16px;
  bottom: 16px;
  opacity: 0.5;
  transition: opacity 0.25s;
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 4px;
  border-radius: 999px;
  background: var(--paper);
  border: 1px solid var(--edge);
  box-shadow: 0 4px 16px rgba(60, 44, 20, 0.12);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
}

.controls:hover,
.controls:focus-within { opacity: 1; }

.controls .icon {
  position: relative;
  display: grid;
  place-items: center;
  width: var(--b);
  height: var(--b);
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--ink-soft);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.controls .icon svg {
  width: 58%;
  height: 58%;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.controls .icon:hover { color: var(--ink); background: var(--edge); }
.controls .icon:disabled { opacity: 0.4; cursor: default; background: transparent; }
.controls .icon[hidden] { display: none; }

.controls .icon.primary {
  color: var(--paper-solid);
  background: var(--ink);
}

.controls .icon.primary:hover { background: var(--ink-soft); }
.controls .icon.primary:disabled { background: var(--ink); }

#fullscreen .leave,
#fullscreen.on .enter { display: none; }
#fullscreen.on .leave { display: inline; }

/* Nivåvalet: tre träd med allt fler förgreningar. */
.levels {
  display: inline-flex;
  gap: 1px;
  padding: 2px;
  border-radius: 999px;
  background: var(--edge);
}

.levels .icon {
  width: calc(var(--b) - 4px);
  height: calc(var(--b) - 4px);
}

.levels .icon[aria-pressed="true"] {
  color: var(--ink);
  background: var(--paper-solid);
  box-shadow: 0 1px 3px rgba(60, 44, 20, 0.2);
}

/* Språkval: runda flaggknappar längst ned till höger. */
.languages {
  --b: max(34px, calc(var(--u) * 40));
  overflow: visible;
  position: absolute;
  z-index: 2;
  right: 16px;
  bottom: 16px;
  display: flex;
  padding: 4px;
  border-radius: 999px;
  background: var(--paper);
  border: 1px solid var(--edge);
  box-shadow: 0 4px 16px rgba(60, 44, 20, 0.12);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
  opacity: 0.5;
  transition: opacity 0.25s;
}

.languages:hover,
.languages:focus-within,
.languages.open { opacity: 1; }

.languages .flag {
  position: relative;
  order: 0;
  flex: none;
  width: calc(var(--b) - 6px);
  height: calc(var(--b) - 6px);
  margin: 3px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: none;
  cursor: pointer;
  box-shadow: 0 0 0 1px var(--edge-strong);
  opacity: 0.6;
  filter: saturate(0.6);
  transition: width 0.25s ease, margin 0.25s ease, opacity 0.2s, filter 0.15s, box-shadow 0.15s;
}

.languages .flag svg {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: 50%;
}

.languages .flag:hover { opacity: 0.95; filter: none; }

/* Den aktiva flaggan ligger alltid längst till höger, i hörnet. */
.languages .flag[aria-pressed="true"] {
  order: 1;
  opacity: 1;
  filter: none;
  box-shadow: 0 0 0 2px var(--paper-solid), 0 0 0 3.5px var(--ink);
}

/* Ihopfällt: de andra flaggorna har ingen bredd och kan inte klickas. */
.languages:not(.open) .flag:not([aria-pressed="true"]) {
  width: 0;
  margin-left: 0;
  margin-right: 0;
  opacity: 0;
  box-shadow: none;
  pointer-events: none;
}

.languages.open .flag:not([aria-pressed="true"]) { margin-right: 4px; }

/* Om språkversionen: en diskret knapp till vänster i språkvalet. */
.languages .notes-button {
  order: -2;
  position: relative;
  display: grid;
  place-items: center;
  flex: none;
  width: calc(var(--b) - 6px);
  height: calc(var(--b) - 6px);
  margin: 3px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--ink-soft);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.languages .notes-button svg {
  width: 60%;
  height: 60%;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.6;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.languages .notes-button:hover { color: var(--ink); background: var(--edge); }

.info h3 {
  margin: 16px 0 4px;
  font-size: max(16px, calc(var(--u) * 19));
  font-weight: 600;
}

#notes-body p { margin: 0; }

/* Ikonraden och språkvalet behåller sin ordning även när texten skrivs från höger. */
.controls,
.languages { direction: ltr; }

/* Förklaring som visas direkt ovanför knappen. */
.controls .icon::after,
.languages .flag::after,
.languages .notes-button::after {
  content: attr(data-tip);
  unicode-bidi: plaintext;
  position: absolute;
  left: 50%;
  bottom: calc(100% + 10px);
  transform: translateX(-50%) translateY(4px);
  padding: 4px 9px;
  border-radius: 5px;
  background: var(--ink);
  color: var(--paper-solid);
  font-size: max(13px, calc(var(--u) * 15));
  font-style: normal;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.12s, transform 0.12s;
}

.controls .icon:first-child::after { left: 0; transform: translateY(4px); }
.languages .flag:last-child::after { left: auto; right: 0; transform: translateY(4px); }

.controls .icon:hover::after,
.controls .icon:focus-visible::after,
.languages .flag:hover::after,
.languages .flag:focus-visible::after,
.languages .notes-button:hover::after,
.languages .notes-button:focus-visible::after {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.controls .icon:first-child:hover::after,
.controls .icon:first-child:focus-visible::after,
.languages .flag:last-child:hover::after,
.languages .flag:last-child:focus-visible::after { transform: translateY(0); }

.divider {
  width: 1px;
  height: calc(var(--b) * 0.55);
  background: var(--edge-strong);
  opacity: 0.5;
  margin: 0 5px;
}

/* Informationspanel */

.panel {
  position: absolute;
  z-index: 3;
  top: 16px;
  right: 16px;
  bottom: 16px;
  width: max(340px, 27vw);
  overflow-y: auto;
  padding: 22px 22px 18px;
  border-radius: 8px;
  background: var(--paper);
  border: 1px solid var(--edge);
  box-shadow: var(--shadow);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  /* Slutar ovanför språkvalet i nedre högra hörnet. */
  max-height: calc(100% - 32px - max(34px, calc(var(--u) * 40)) - 24px);
  height: fit-content;
}

.panel[hidden] { display: none; }

.panel-close {
  position: absolute;
  top: 8px;
  inset-inline-end: 8px;
  width: 30px;
  height: 30px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  color: var(--ink-soft);
}

.panel-close:hover { color: var(--ink); }

.kind {
  margin: 0 0 6px;
  font-size: max(11px, calc(var(--u) * 13));
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--historia);
}

.kind.kind-tradition { color: var(--tradition); }
.kind.kind-delvis { color: var(--delvis); }

.panel h2,
.info h2 {
  margin: 0;
  font-size: max(24px, calc(var(--u) * 32));
  font-weight: 600;
  line-height: 1.15;
  padding-inline-end: 24px;
}

.panel-period {
  margin: 4px 0 12px;
  font-size: max(15px, calc(var(--u) * 18));
  font-style: italic;
  color: var(--ink-soft);
}

.panel-text,
.relations p,
.info p {
  margin: 0 0 12px;
  font-size: max(15.5px, calc(var(--u) * 20));
  line-height: 1.5;
}

.events {
  margin: 0 0 14px;
  padding: 10px 0 0;
  list-style: none;
  border-top: 1px solid var(--edge);
  font-size: max(14px, calc(var(--u) * 17));
  font-style: italic;
  color: var(--ink-soft);
}

.events li { margin: 0 0 4px; }

.relations {
  padding-top: 10px;
  border-top: 1px solid var(--edge);
}

.relations p { font-size: max(14.5px, calc(var(--u) * 17)); color: var(--ink-soft); }

.link {
  padding: 0;
  border: 0;
  background: none;
  color: var(--accent);
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 2px;
  cursor: pointer;
}

/* Om trädet */

.info {
  width: min(max(560px, 46vw), calc(100vw - 32px));
  max-height: calc(100vh - 48px);
  padding: 24px 26px 10px;
  border-radius: 10px;
  border: 1px solid var(--edge);
  background: var(--paper-solid);
  color: var(--ink);
  box-shadow: var(--shadow);
  font-family: var(--serif);
}

.info h2 { margin-bottom: 14px; }

.info::backdrop { background: rgba(30, 24, 14, 0.35); }

.info .sources { font-size: 13.5px; color: var(--ink-soft); }

/* Arabiska: naskh-typsnitt och ingen konstgjord kursiv. */
html[lang="ar"] {
  --serif: "Noto Naskh Arabic", "Geeza Pro", "Amiri", "Traditional Arabic", "Arabic Typesetting", "Iowan Old Style", Georgia, serif;
}

/* Kinesiska: song-typsnitt (antikva) från systemet. */
html[lang="zh-Hans"] {
  --serif: "Songti SC", "STSong", "Noto Serif SC", "Source Han Serif SC", "Noto Serif CJK SC", "SimSun", "PingFang SC", "Microsoft YaHei", serif;
}

html[lang="ar"] .label .period,
html[lang="ar"] .tag,
html[lang="ar"] .label.marker .name,
html[lang="ar"] .progress,
html[lang="ar"] .panel-period,
html[lang="ar"] .events,
html[lang="zh-Hans"] .label .period,
html[lang="zh-Hans"] .tag,
html[lang="zh-Hans"] .label.marker .name,
html[lang="zh-Hans"] .progress,
html[lang="zh-Hans"] .panel-period,
html[lang="zh-Hans"] .events { font-style: normal; }

html[lang="ar"] .kind,
html[lang="zh-Hans"] .kind { letter-spacing: 0; text-transform: none; }

@media (prefers-reduced-motion: reduce) {
  .languages .flag { transition: none; }
  .label, .tag { transition: none; }
}
