:root {
  color-scheme: light;
  --ink: #1c2430;
  --muted: #5c6b7a;
  --line: #d5dde6;
  --bg: #eef3f7;
  --card: #fff;
  --accent: #0f6e56;
  --accent-ink: #fff;
  --accent-bg: #e5f5f0;
  --danger: #9e1239;
  --danger-bg: #fde8e8;
  --warning: #9a5b00;
  --warning-ink: #7a4e00;
  --warning-bg: #fff6e0;
  --locked: #e2e2e2;
  --locked-ink: #8e9194;
  --hover: #e7eef4;
  --map-select: #007ac2;
  --map-select-bg: #d6ebf8;
  --head-bg: #f7fafc;
  --check-border: #94a3b8;
  --backdrop: rgba(22, 34, 51, 0.35);
  --shadow: 0 12px 40px rgba(22, 34, 51, 0.16);
  --shadow-toast: 0 4px 12px rgba(22, 34, 51, 0.18);
  --toast-text: #fff;
  --size-toast: 24rem;
  --radius: 8px;
  --radius-lg: 12px;
  --row: 30px;
  --head: 34px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  display: flex;
  flex-direction: column;
  color: var(--ink);
  background: var(--bg);
  font: 14px/1.4 "Segoe UI", system-ui, sans-serif;
}
body.admin-mode { height: auto; min-height: 100%; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { opacity: 0.45; cursor: default; }
h1, h2, p { margin: 0; }

.top {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: center;
  padding: 10px 16px;
  background: var(--card);
  border-bottom: 1px solid var(--line);
}
.brand p, .hint, .status { color: var(--muted); }
h1 { font-size: 18px; }
.top-actions { display: flex; align-items: center; flex-wrap: wrap; justify-content: flex-end; gap: 12px; }
.file-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  padding-right: 12px;
  border-right: 1px solid var(--line);
}

.tabs { display: flex; gap: 6px; }
.tabs button, .toolbar > button, .file-actions > button, .file span, .popover-actions button, .filter-values-bar button, .login-card button {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--line);
  background: var(--card);
  border-radius: var(--radius);
  padding: 6px 10px;
}
.tabs button.on, .toolbar > button.on {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}
.tabs button.on .app-icon--main,
.toolbar > button.on .app-icon--main { filter: brightness(0) invert(1); opacity: 1; }

.app-icon { display: block; flex: none; object-fit: contain; }
.app-icon--main { opacity: 0.88; }
.app-icon--inverse { filter: brightness(0) invert(1); opacity: 0.92; }

#flash {
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 12px;
  width: min(var(--size-toast), calc(100vw - 32px));
  margin: 0;
  padding: 10px 8px 10px 14px;
  border-radius: var(--radius);
  background: var(--accent);
  color: var(--toast-text);
  box-shadow: var(--shadow-toast);
}
#flash.warning { background: var(--warning); }
#flash.error { background: var(--danger); }
#flash.info { background: var(--ink); }
#flash p { flex: 1; }
.toast-close {
  border: 0;
  background: transparent;
  padding: 2px;
  border-radius: var(--radius);
  line-height: 0;
}

#workspace {
  flex: 1;
  min-width: 0;
  min-height: 0;
  display: flex;
}
#view-editor, #view-map {
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
}
#view-editor { flex: 2; }
#view-map {
  flex: 1;
  background: var(--bg);
  border-left: 1px solid var(--line);
}
#view-map .status {
  margin-left: 0;
  flex: 1 1 100%;
}
.map-view {
  position: relative;
  flex: 1;
  min-height: 0;
  margin: 0 12px 12px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  overflow: hidden;
}
.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  padding: 10px 16px;
}
.kind, .login-card label { display: flex; align-items: center; gap: 6px; font-weight: 600; }
.kind select, .login-card input, .admin-table input:not([type="checkbox"]), .admin-table select, .admin-table textarea {
  font-weight: 400;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 6px 8px;
  background: var(--card);
}
.file input { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.file span { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.file input:focus-visible + span, button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
#download { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.status { margin-left: auto; }

.banner {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  margin: 0 16px 8px;
  padding: 8px 12px;
  border-radius: var(--radius);
  background: var(--accent-bg);
  border: 1px solid var(--line);
}
.empty {
  margin: 24px auto;
  max-width: 640px;
  padding: 24px;
  background: var(--card);
  border-radius: var(--radius-lg);
  border: 1px solid var(--line);
}
.empty .app-icon--file { width: 40px; height: 40px; margin-bottom: 8px; }
.empty h2 { margin-bottom: 8px; }
.empty p { color: var(--muted); }

.table-wrap {
  flex: 1;
  min-height: 0;
  overflow: auto;
  margin: 0 16px 16px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
}
.admin-wrap { max-height: none; overflow: auto; flex: none; }

#grid { border-collapse: separate; border-spacing: 0; width: max-content; min-width: 100%; }
#grid th, #grid td {
  height: var(--row);
  padding: 0 8px;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  background: var(--card);
  text-align: left;
  vertical-align: middle;
  white-space: nowrap;
}
#grid thead th {
  position: sticky;
  z-index: 3;
  height: var(--head);
  background: var(--head-bg);
  font-weight: 600;
}
#grid thead tr:first-child th { top: 0; }
#grid thead tr:nth-child(2) th { top: var(--head); z-index: 3; }
#grid .status-cell, #grid .locate, #grid .check {
  position: sticky;
  z-index: 2;
  min-width: 36px;
  width: 36px;
  max-width: 36px;
  text-align: center;
  padding: 0;
}
#grid .status-cell { left: 0; }
#grid .locate { left: 36px; }
#grid .check { left: 72px; }
#grid thead .status-cell, #grid thead .locate, #grid thead .check { z-index: 5; background: var(--head-bg); }
#grid td.status-cell, #grid td.locate, #grid td.check { background: var(--card); }
.status-icon {
  display: inline-block;
  width: 18px;
  height: 18px;
  vertical-align: middle;
  background: var(--accent);
  -webkit-mask: url("icons/check.svg") center / 18px 18px no-repeat;
  mask: url("icons/check.svg") center / 18px 18px no-repeat;
}
.status-icon--bad {
  background: var(--danger);
  -webkit-mask-image: url("icons/status_missing.svg");
  mask-image: url("icons/status_missing.svg");
}
#grid .icon-btn:hover:not(:disabled) { background: var(--hover); }
#grid .sort {
  display: flex;
  width: 100%;
  gap: 6px;
  align-items: center;
  border: 0;
  background: transparent;
  padding: 0;
  font-weight: 600;
  text-align: left;
}
#grid .sort .col-title {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}
#grid .col-lock { margin-left: 6px; }
#grid .icon-btn {
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  margin: 0 auto;
  border: 0;
  background: transparent;
  padding: 0;
  border-radius: var(--radius);
}
#grid td.locked { background: var(--locked); color: var(--locked-ink); }
#grid .cell-editor:focus-visible { outline: none; }
.col-resize {
  position: absolute;
  top: 0;
  right: 0;
  width: 8px;
  height: 100%;
  cursor: col-resize;
  z-index: 6;
  touch-action: none;
}
.col-resize:hover { background: var(--accent); }
#grid td.bool { text-align: center; padding: 0; }
dialog {
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 16px 18px;
  max-width: min(560px, calc(100vw - 32px));
  box-shadow: var(--shadow);
}
dialog h2 { margin-bottom: 8px; }
#load-dialog-text { white-space: pre-wrap; color: var(--ink); }
dialog form { display: flex; justify-content: flex-end; margin-top: 14px; }
dialog .primary {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--accent);
  background: var(--accent);
  color: var(--accent-ink);
  border-radius: var(--radius);
  padding: 6px 12px;
}
dialog::backdrop { background: var(--backdrop); }
#grid .mark { color: var(--accent); flex: none; }
#grid .filter {
  width: 100%;
  height: 24px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0 6px;
  font-weight: 400;
}
#grid .filter-cell {
  display: flex;
  align-items: center;
  gap: 2px;
}
#grid .filter-cell .filter {
  flex: 1;
  min-width: 0;
  width: auto;
}
#grid .filter-pick {
  flex: none;
  width: 22px;
  height: 22px;
  margin: 0;
  border: 0;
  padding: 0;
  display: grid;
  place-items: center;
  border-radius: var(--radius);
  background: transparent;
  cursor: pointer;
}
#grid .filter-pick.on { background: var(--accent-bg); }
#grid .filter-pick:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
#grid .filter-pick .app-icon { width: 16px; height: 16px; }
.filter-values {
  width: min(360px, calc(100vw - 24px));
  max-height: min(360px, calc(100vh - 24px));
  display: flex;
  flex-direction: column;
  overflow: hidden;
  padding: 8px;
}
.filter-values-bar {
  display: flex;
  align-items: center;
  gap: 6px;
}
.filter-values-bar input {
  flex: 3 1 0;
  min-width: 0;
  height: 32px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0 8px;
}
.filter-values-bar button {
  flex: 1 1 0;
  min-width: 0;
  justify-content: center;
  text-align: center;
  white-space: normal;
  line-height: 1.15;
  padding: 4px 6px;
}
#value-filter-list {
  overflow: auto;
  margin-top: 8px;
  min-height: 0;
}
#value-filter-list label {
  display: flex;
  gap: 8px;
  align-items: flex-start;
  padding: 3px 2px;
}
#value-filter-list label span { overflow-wrap: anywhere; }
#grid td { position: relative; }
#grid .val {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
}
#grid tr.picked td { background: var(--map-select-bg); color: var(--ink); }
#grid tr.picked td.missing { background: var(--danger-bg); box-shadow: inset 3px 0 0 var(--danger); }
#grid td.missing { background: var(--danger-bg); box-shadow: inset 3px 0 0 var(--danger); }
#grid td.purpose { cursor: pointer; }
#grid td.active { box-shadow: inset 0 0 0 2px var(--accent); }
#grid td.in-fill { background: var(--accent-bg); }
#grid tr.spacer td {
  height: auto;
  min-width: 0;
  padding: 0;
  border: 0;
  background: transparent;
}
.cell-editor {
  width: 100%;
  height: 24px;
  border: 0;
  padding: 0;
  background: transparent;
}
.fill-handle {
  position: absolute;
  right: 0;
  bottom: 0;
  width: 8px;
  height: 8px;
  background: var(--accent);
  cursor: crosshair;
  z-index: 2;
}

#view-admin { padding-bottom: 32px; }
.card {
  margin: 16px;
  padding: 16px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
}
.card h2 { display: flex; align-items: center; gap: 8px; }
.login-card { max-width: 420px; }
.login-card form { display: flex; gap: 8px; align-items: end; margin-top: 12px; }
.login-card button { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.admin-table { width: max-content; max-width: 100%; border-collapse: collapse; }
.admin-table th, .admin-table td {
  border-bottom: 1px solid var(--line);
  padding: 4px 8px;
  vertical-align: middle;
  text-align: left;
}
.admin-table th { position: sticky; top: 0; background: var(--head-bg); }
.admin-table select { width: 112px; }
.admin-table textarea { width: 148px; min-height: 36px; }
.admin-table .col-name { font-weight: 600; max-width: 260px; }
.admin-table .col-width { width: 76px; }
.admin-table .col-req-for {
  max-width: 168px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font: inherit;
  font-size: 13px;
  padding: 4px 8px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--card);
  color: var(--ink);
  cursor: pointer;
}
.admin-table .col-req-for.has {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-bg);
}
.admin-table .col-req-for:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.req-for-cell { color: var(--muted); }
#req-for-dialog { width: min(520px, calc(100vw - 32px)); }
#req-for-search {
  width: 100%;
  margin-top: 8px;
  padding: 7px 8px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--card);
  color: var(--ink);
}
.req-for-tools { display: flex; gap: 8px; margin-top: 8px; }
.req-for-tools button {
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--ink);
  border-radius: var(--radius);
  padding: 6px 10px;
  cursor: pointer;
}
.req-for-tools button:focus-visible,
#req-for-search:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.req-for-list { max-height: min(420px, 50vh); overflow: auto; margin-top: 8px; }
.req-for-group { margin: 10px 0 4px; font-weight: 700; }
.req-for-item { display: flex; align-items: center; gap: 8px; padding: 3px 0 3px 4px; cursor: pointer; }
.order-cell { white-space: nowrap; }
.order-cell button {
  width: 24px;
  height: 24px;
  border: 0;
  background: transparent;
  padding: 0;
  display: inline-grid;
  place-items: center;
  border-radius: var(--radius);
  cursor: pointer;
}
.order-cell button:disabled { opacity: 0.35; cursor: default; }
.order-cell button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.order-up .app-icon { transform: rotate(180deg); }
.admin-table .flag { width: 64px; text-align: center; padding-inline: 4px; white-space: nowrap; }
input[type="checkbox"] {
  appearance: none;
  width: 18px;
  height: 18px;
  margin: 0;
  border: 1.5px solid var(--check-border);
  border-radius: 4px;
  background: var(--card);
  display: inline-grid;
  place-content: center;
  cursor: pointer;
  vertical-align: middle;
}
input[type="checkbox"]::after {
  content: "";
  width: 5px;
  height: 9px;
  border: solid var(--accent-ink);
  border-width: 0 2px 2px 0;
  transform: rotate(45deg) translate(-1px, -1px);
  opacity: 0;
}
input[type="checkbox"]:checked {
  background: var(--accent);
  border-color: var(--accent);
}
input[type="checkbox"]:checked::after { opacity: 1; }
input[type="checkbox"]:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
input[type="checkbox"]:disabled { opacity: 0.45; cursor: default; }
#grid input.bool-check { width: 18px; height: 18px; }
.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin-top: 4px;
  padding: 1px 6px;
  border-radius: 999px;
  background: var(--accent-bg);
  color: var(--accent);
  font-size: 12px;
  font-weight: 600;
}

.popover {
  position: fixed;
  z-index: 20;
  width: min(440px, calc(100vw - 24px));
  max-height: min(560px, calc(100vh - 24px));
  overflow: auto;
  padding: 14px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
}
.popover-title { font-weight: 700; margin-bottom: 6px; }
.try-field { margin-top: 10px; }
.try-field label { display: block; font-weight: 600; margin-bottom: 4px; }
.combo { position: relative; }
.combo input {
  width: 100%;
  padding: 7px 32px 7px 8px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.combo-open {
  position: absolute;
  top: 1px;
  right: 1px;
  display: grid;
  place-items: center;
  height: calc(100% - 2px);
  width: 30px;
  border: 0;
  border-radius: 0 var(--radius) var(--radius) 0;
  background: transparent;
  padding: 0;
}
.combo-list {
  position: relative;
  z-index: 1;
  margin-top: 4px;
  max-height: 140px;
  overflow: auto;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.combo-opt, .ambiguous button {
  display: block;
  width: 100%;
  text-align: left;
  border: 0;
  background: transparent;
  padding: 6px 8px;
}
.combo-opt.on, .combo-opt:hover, .ambiguous button:hover { background: var(--hover); }
.combo-empty { margin: 6px 8px; color: var(--muted); }
.ambiguous { display: flex; flex-direction: column; gap: 4px; margin-top: 8px; }
.popover-actions {
  display: flex;
  gap: 8px;
  position: sticky;
  bottom: 0;
  z-index: 3;
  margin-top: 14px;
  padding-top: 8px;
  background: var(--card);
}
#purpose {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  width: min(420px, calc(100vw - 24px));
}
#purpose .purpose-leaf {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}
#purpose .combo[data-level="3"] {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}
#purpose .combo[data-level="3"] .combo-open { height: 34px; }
#purpose .combo[data-level="1"] .combo-list,
#purpose .combo[data-level="2"] .combo-list { max-height: 112px; }
#purpose .combo[data-level="3"] .combo-list {
  flex: 1;
  min-height: 0;
  max-height: none;
}
