:root {
  color-scheme: light dark;
  --bg: #0e1117;
  --panel: #171b22;
  --border: #2a3340;
  --text: #e6ebf5;
  --muted: #9ca9bf;
  --accent: #5ba2ff;
}

* { box-sizing: border-box; }
.hidden { display: none !important; }
body {
  margin: 0;
  font-family: Inter, system-ui, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

header {
  display: flex;
  justify-content: space-between;
  gap: .75rem;
  align-items: center;
  padding: .65rem .8rem;
  border-bottom: 1px solid var(--border);
}

h1, h2 { margin: 0 0 .7rem; }
.actions { display: flex; gap: .6rem; }
.file-btn, button {
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: .55rem .9rem;
  cursor: pointer;
}
.file-btn input { display: none; }

main {
  display: grid;
  grid-template-columns: 280px 1fr 300px;
  gap: .7rem;
  padding: .7rem;
}

@media (min-width: 1001px) {
  body.tree-lr-mode .detail {
    margin-top: 220px;
  }
}

/* När personpanelen flyttas ned i LR-läge låter vi trädytan ta samma extra höjd. */
body.tree-lr-mode .graph-wrap {
  height: calc(100vh + 150px);
}

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: .65rem;
  margin-bottom: .7rem;
}

#searchInput, select, input[type="number"], #surnameFilter {
  width: 100%;
  padding: .42rem;
  border-radius: 7px;
  border: 1px solid var(--border);
  background: #11151d;
  color: var(--text);
  font-size: .88rem;
}

.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: .45rem;
  margin-bottom: .45rem;
}

label {
  font-size: .82rem;
  color: var(--muted);
}

.collapsible > summary {
  cursor: pointer;
  list-style: none;
}
.collapsible > summary::-webkit-details-marker { display: none; }
.collapsible > summary h2 {
  display: inline;
  margin: 0;
  font-size: 1rem;
}
.collapsible[open] > summary { margin-bottom: .5rem; }

.checkbox-row {
  display: flex;
  align-items: center;
  gap: .4rem;
  margin-top: .4rem;
}
.checkbox-row input {
  width: auto;
}

#searchResults, #validationList {
  list-style: none;
  margin: .7rem 0 0;
  padding: 0;
  max-height: 55vh;
  overflow: auto;
}

#searchResults li, #validationList li {
  padding: .32rem .4rem;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  font-size: .92rem;
}
#searchResults li:hover { background: #232a36; }
#validationList li { cursor: default; }

.tabs { display: flex; gap: .5rem; margin-bottom: .7rem; }
.tab.active { border-color: var(--accent); color: var(--accent); }
.view { display: none; }
.view.active { display: block; }

.tree-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(220px, 1fr));
  gap: .7rem;
}

.column {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: .7rem;
  min-height: 240px;
}
.column h3 { margin: 0 0 .6rem; font-size: 1rem; }
.person-chip {
  display: block;
  width: 100%;
  text-align: left;
  margin: .35rem 0;
  background: #11151d;
}
.person-chip.active { border-color: var(--accent); color: var(--accent); }

.content table {
  width: 100%;
  border-collapse: collapse;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
}
th, td {
  padding: .35rem .4rem;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
  text-align: left;
  font-size: .88rem;
  line-height: 1.2;
}
th {
  position: sticky;
  top: 0;
  background: #1a202b;
  z-index: 2;
}

#tableView thead th[data-sort] {
  cursor: pointer;
  user-select: none;
}
#tableView thead th.active-sort {
  color: var(--accent);
}
#tableView thead th.active-sort[data-dir="asc"]::after {
  content: ' ▲';
}
#tableView thead th.active-sort[data-dir="desc"]::after {
  content: ' ▼';
}
.link-list { display: flex; flex-wrap: wrap; gap: .3rem; }
.link-list button {
  padding: .12rem .3rem;
  font-size: .76rem;
}

#tableView td > .name-chip,
#tableView td > .cell-chip {
  display: inline-block;
  margin: 0;
  vertical-align: middle;
  padding: .22rem .5rem;
  line-height: 1.2;
}

#tableView td > .cell-chip {
  border: 1px solid transparent;
}

#tableView {
  max-height: calc(100vh - 170px);
  overflow: auto;
}

.graph-controls {
  display: grid;
  grid-template-columns: repeat(3, minmax(120px, 1fr));
  gap: .45rem;
  margin-bottom: .5rem;
}

.graph-wrap {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  height: calc(100vh - 230px);
  overflow: auto;
}

#graphTreeSvg {
  width: 1600px;
  min-height: 560px;
}

#graphTreeSvg .edge {
  stroke: #5f6f86;
  stroke-width: 1.3;
}

#graphTreeSvg .node { cursor: pointer; }
#graphTreeSvg .node rect {
  fill: #111827;
  stroke: #3a4a60;
}
#graphTreeSvg .node.selected rect {
  stroke: var(--accent);
  stroke-width: 1.8;
}
#graphTreeSvg .node text {
  fill: var(--text);
  font-size: 11px;
  font-family: Inter, system-ui, sans-serif;
}
#graphTreeSvg .node text.sub {
  fill: var(--muted);
  font-size: 10px;
}

.edit-form {
  display: grid;
  gap: .5rem;
  margin-bottom: 1rem;
}
.edit-form input, .edit-form textarea {
  width: 100%;
  margin-top: .25rem;
  padding: .45rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #11151d;
  color: var(--text);
}
.person-detail small { color: var(--muted); }

.card.compact {
  margin-top: .5rem;
  margin-bottom: .6rem;
  padding: .5rem;
}

.card.compact h3 {
  margin: 0 0 .4rem;
  font-size: .95rem;
}

.relation-actions {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: .35rem;
  align-items: end;
}

.relation-actions button {
  padding: .35rem .5rem;
  font-size: .8rem;
}

.quick-nav {
  display: flex;
  gap: .35rem;
  margin-bottom: .5rem;
}
.quick-nav button {
  flex: 1;
  padding: .3rem .35rem;
  font-size: .78rem;
}

@media (max-width: 1000px) {
  main { grid-template-columns: 1fr; }
  .tree-grid { grid-template-columns: 1fr; }
  #searchResults { max-height: 200px; }
  .graph-controls { grid-template-columns: 1fr; }

  body.tree-lr-mode .detail {
    margin-top: 0;
  }

  body.tree-lr-mode .graph-wrap {
    height: calc(100vh - 170px);
  }
}

@media (max-width: 1000px) and (orientation: landscape) {
  body.tab-tree-active .graph-wrap {
    height: calc(100vh - 30px);
    min-height: 380px;
  }

  body.tab-tree-active .detail {
    margin-top: 220px;
  }
}
