:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --side: #eceef2;
  --ink: #1d2230;
  --muted: #667085;
  --line: #d6d9e0;
  --accent: #3b5bdb;
  --accent-ink: #ffffff;
  --accent-soft: #e5eaff;
  --danger: #c92a2a;
  --cell: #ffffff;
  --cell-alt: #fafbfc;
  --head: #f0f2f6;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 14px;
  color: var(--ink);
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); height: 100vh; display: flex; flex-direction: column; overflow: hidden; }
button {
  font: inherit; border: 1px solid var(--line); background: var(--panel); color: var(--ink);
  padding: 6px 12px; border-radius: 6px; cursor: pointer; white-space: nowrap;
}
button:hover { border-color: var(--accent); }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
button.primary:hover { filter: brightness(1.08); }
button.small { padding: 3px 8px; font-size: 12px; }
button.danger { color: var(--danger); }
input, select, textarea { font: inherit; color: var(--ink); }

.topbar {
  display: flex; align-items: center; gap: 16px; padding: 8px 16px;
  background: var(--panel); border-bottom: 1px solid var(--line); flex-wrap: wrap;
}
.brand { font-weight: 700; font-size: 17px; display: flex; align-items: center; gap: 8px; }
.logo {
  width: 22px; height: 22px; display: inline-block;
  background: conic-gradient(#e03131, #f08c00, #fab005, #37b24d, #1c7ed6, #7048e8, #e03131);
  clip-path: polygon(50% 0, 100% 100%, 0 100%);
}
.toolbar { display: flex; gap: 14px; flex-wrap: wrap; }
.toolbar .group { display: flex; gap: 6px; }
.save-state { margin-left: auto; color: var(--muted); font-size: 12px; }

.layout { flex: 1; display: flex; min-height: 0; }
.sidebar {
  width: 250px; flex-shrink: 0; background: var(--side); border-right: 1px solid var(--line);
  overflow-y: auto; padding: 10px 8px;
}
.sidebar h3 {
  font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted);
  margin: 14px 8px 6px; display: flex; justify-content: space-between; align-items: center;
}
.sidebar h3 button { padding: 0 6px; font-size: 13px; border: none; background: none; color: var(--accent); }
.nav-item {
  display: flex; align-items: center; gap: 6px; padding: 6px 8px; border-radius: 6px;
  cursor: pointer; user-select: none;
}
.nav-item:hover { background: rgba(0,0,0,.05); }
.nav-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav-item .ico { width: 16px; text-align: center; opacity: .8; }
.nav-item .label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-item .x { visibility: hidden; border: none; background: none; padding: 0 4px; color: var(--muted); }
.nav-item:hover .x { visibility: visible; }
.nav-item .x:hover { color: var(--danger); }
.nav-empty { color: var(--muted); font-size: 12px; padding: 2px 8px 4px; }

.main { flex: 1; overflow: auto; padding: 18px 22px; min-width: 0; }
.view-head { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.view-head h2 { margin: 0; font-size: 19px; }
.view-head .sub { color: var(--muted); font-size: 13px; }
.view-head .actions { margin-left: auto; display: flex; gap: 6px; flex-wrap: wrap; }

/* Data table */
.grid-wrap { overflow: auto; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; max-height: calc(100vh - 170px); }
table.grid { border-collapse: separate; border-spacing: 0; }
table.grid th, table.grid td { border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); padding: 0; }
table.grid thead th { position: sticky; top: 0; background: var(--head); z-index: 2; }
table.grid th.rownum, table.grid td.rownum {
  position: sticky; left: 0; background: var(--head); color: var(--muted); font-size: 12px;
  width: 44px; min-width: 44px; text-align: center; z-index: 1;
}
table.grid thead th.rownum { z-index: 3; }
table.grid th .colhead { display: flex; align-items: center; }
table.grid th input {
  width: 120px; border: none; background: transparent; padding: 7px 8px; font-weight: 600; text-align: center;
}
table.grid th .del { border: none; background: none; color: var(--muted); padding: 0 6px; visibility: hidden; }
table.grid th:hover .del { visibility: visible; }
table.grid th .del:hover { color: var(--danger); }
table.grid th.xcol, table.grid td.xcol { background: #f7f3e8; }
table.grid.grouped thead tr:first-child th { height: 33px; }
table.grid.grouped thead tr.subhead th { top: 33px; font-size: 11px; color: var(--muted); font-weight: 500; padding: 3px 0; }
table.grid.grouped th.dshead input { width: 100%; }
table.grid td.dsstart, table.grid.grouped thead tr.subhead th:nth-child(1) { border-left: 2px solid #b8bdc8; }
table.grid.grouped td input { width: 84px; }
table.grid.grouped td.xcol input { width: 110px; }
table.grid td input {
  width: 128px; border: none; padding: 6px 8px; text-align: right; background: var(--cell);
}
table.grid td.textcell input { text-align: left; }
table.grid tr:nth-child(even) td input { background: var(--cell-alt); }
table.grid td.xcol input { background: #fbf8ef; }
table.grid td input:focus, table.grid th input:focus { outline: 2px solid var(--accent); outline-offset: -2px; background: #fff; }
table.grid td input.bad { color: var(--danger); }
.grid-tools { display: flex; gap: 6px; margin-top: 10px; flex-wrap: wrap; align-items: center; }
.hint { color: var(--muted); font-size: 12px; }

/* Results */
.result-card { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; margin-bottom: 14px; overflow: hidden; }
.result-card h4 { margin: 0; padding: 9px 14px; background: var(--head); border-bottom: 1px solid var(--line); font-size: 13px; }
table.res { border-collapse: collapse; width: 100%; }
table.res td, table.res th { padding: 6px 14px; border-bottom: 1px solid #eef0f4; text-align: left; font-variant-numeric: tabular-nums; }
table.res th { font-size: 12px; color: var(--muted); font-weight: 600; }
table.res tr:last-child td { border-bottom: none; }
table.res td:first-child { color: #344054; }
table.res td.num, table.res th.num { text-align: right; }
.res-note { padding: 8px 14px; color: var(--muted); font-size: 12px; }
.badge { display: inline-block; padding: 1px 7px; border-radius: 10px; font-size: 12px; font-weight: 600; }
.badge.yes { background: #d3f9d8; color: #2b8a3e; }
.badge.no { background: #f1f3f5; color: #495057; }
.error-box { background: #fff5f5; border: 1px solid #ffc9c9; color: var(--danger); padding: 12px 14px; border-radius: 8px; }

/* Graph */
.graph-layout { display: flex; gap: 16px; align-items: flex-start; flex-wrap: wrap; }
.graph-opts {
  width: 270px; flex-shrink: 0; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 12px 14px;
}
.graph-opts label { display: block; font-size: 12px; color: var(--muted); margin: 10px 0 3px; }
.graph-opts label.check { display: flex; gap: 6px; align-items: center; color: var(--ink); font-size: 13px; }
.graph-opts input[type=text], .graph-opts input[type=number], .graph-opts select { width: 100%; padding: 5px 7px; border: 1px solid var(--line); border-radius: 5px; background: #fff; }
.graph-opts .row2 { display: flex; gap: 8px; }
.graph-opts .row2 > div { flex: 1; }
.graph-opts h5 { margin: 14px 0 0; font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); border-top: 1px solid var(--line); padding-top: 10px; }
.graph-opts h5:first-child { border-top: none; margin-top: 0; padding-top: 0; }
.graph-canvas { flex: 1; min-width: 0; max-width: 100%; overflow: auto; background: #fff; border: 1px solid var(--line); border-radius: 8px; padding: 8px; }

/* Start page */
.welcome { max-width: 720px; margin: 30px auto; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 28px 32px; }
.welcome h1 { margin-top: 0; }
.welcome .cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 12px; margin: 18px 0; }
.welcome .card { border: 1px solid var(--line); border-radius: 10px; padding: 14px; cursor: pointer; }
.welcome .card:hover { border-color: var(--accent); background: var(--accent-soft); }
.welcome .card b { display: block; margin-bottom: 4px; }
.welcome .card span { color: var(--muted); font-size: 13px; }

/* Dialog */
dialog { border: none; border-radius: 12px; padding: 0; box-shadow: 0 20px 60px rgba(0,0,0,.25); width: min(560px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(20,24,35,.35); }
dialog form { padding: 20px 22px; }
dialog h3 { margin: 0 0 14px; }
dialog .field { margin-bottom: 12px; }
dialog .field > label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
dialog select, dialog input[type=text], dialog input[type=number] { width: 100%; padding: 7px 9px; border: 1px solid var(--line); border-radius: 6px; }
dialog .choices { display: grid; gap: 8px; }
dialog .choice { display: flex; gap: 10px; border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; cursor: pointer; align-items: flex-start; }
dialog .choice:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
dialog .choice small { display: block; color: var(--muted); }
dialog .choice-group { font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin: 8px 0 0; }
dialog .row2-dlg { display: flex; gap: 10px; }
dialog .row2-dlg > .field { flex: 1; }
dialog form { max-height: calc(100vh - 60px); overflow-y: auto; }
dialog .buttons { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
dialog .checks label { display: flex; gap: 6px; align-items: center; margin: 4px 0; }

@media (max-width: 760px) {
  body { height: auto; overflow: auto; }
  .layout { flex-direction: column; }
  .sidebar { width: 100%; border-right: none; border-bottom: 1px solid var(--line); max-height: 220px; }
  .main { padding: 14px 16px; }
  .graph-opts { width: 100%; }
}

button:disabled { opacity: .45; cursor: default; border-color: var(--line); }

/* Navigator: search and family */
#navSearch { width: 100%; padding: 6px 9px; border: 1px solid var(--line); border-radius: 6px; background: #fff; margin-bottom: 2px; }
.nav-empty a { color: var(--muted); font-style: italic; text-decoration: none; }
.nav-empty a:hover { color: var(--accent); }
.family { margin-top: 14px; border-top: 1px solid var(--line); padding-top: 4px; }
.nav-item[draggable=true] { cursor: grab; }

/* Info sheet */
.info-sheet { max-width: 760px; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 14px; }
.info-table td { padding: 4px 6px; }
.info-table input { width: 100%; padding: 6px 8px; border: 1px solid var(--line); border-radius: 5px; }
.info-table td:first-child { width: 36%; }
.info-table td:last-child { width: 40px; }
.notes-label { display: block; font-size: 12px; color: var(--muted); margin: 16px 0 4px; }
#infoNotes { width: 100%; padding: 8px 10px; border: 1px solid var(--line); border-radius: 6px; font: inherit; resize: vertical; }

/* Layouts */
.layout-tools { display: flex; gap: 14px; flex-wrap: wrap; align-items: center; margin-bottom: 12px; font-size: 13px; color: var(--muted); }
.layout-tools select, .layout-tools input { margin-left: 6px; padding: 5px 7px; border: 1px solid var(--line); border-radius: 5px; background: #fff; color: var(--ink); }
.layout-page-wrap { background: #d9dce2; border-radius: 8px; padding: 22px; overflow: auto; }
.layout-page { position: relative; background: #fff; width: 100%; max-width: 1100px; margin: 0 auto; box-shadow: 0 4px 18px rgba(0,0,0,.18); }
.layout-title { position: absolute; display: flex; align-items: center; font: bold 18px Arial, Helvetica, sans-serif; }
.lcell { position: absolute; display: flex; align-items: center; justify-content: center; }
.lcell.empty { background: #c9ccd2; color: #4a5060; font-size: 12px; padding: 10px; }
.lcell.empty select { display: block; margin-top: 8px; max-width: 100%; padding: 4px; border-radius: 4px; border: 1px solid #aaa; background: #fff; }
.lcell.filled img { max-width: 100%; max-height: 100%; object-fit: contain; }
.lcell.filled:hover { outline: 1px dashed #9aa3b5; }
.lcell-bar { position: absolute; top: 2px; right: 2px; display: none; gap: 4px; align-items: center; background: rgba(255,255,255,.92); border: 1px solid var(--line); border-radius: 6px; padding: 2px 4px; font-size: 11px; }
.lcell-bar span { max-width: 140px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); }
.lcell:hover .lcell-bar { display: flex; }
.lcell.drop { outline: 3px solid var(--accent); outline-offset: -3px; background: var(--accent-soft); }
