:root {
  --maroon: #801900;
  --bg: #1e2025;
  --panel: #26292f;
  --line: #3a3e46;
  --text: #e6e6e8;
  --dim: #9aa0aa;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font: 14px/1.4 "Segoe UI", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
}

body { display: flex; flex-direction: column; }
#appShell { flex: 1; min-height: 0; display: flex; flex-direction: column; }
#appShell[hidden], #authShell[hidden] { display: none; }
#authShell [hidden] { display: none; }

#authShell {
  flex: 1;
  display: grid;
  place-items: center;
  padding: 24px;
}
.authCard {
  width: min(100%, 420px);
  padding: 24px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
}
.authCard h1 { margin: 0 0 8px; color: #fff; }
.authCard form, #addUserForm { display: grid; gap: 12px; }
.authCard label, #addUserForm label { display: grid; gap: 5px; color: var(--dim); }
.authCard input, #addUserForm input, #addUserForm select { width: 100%; }
.formError { color: #f08b7b; }
#currentUser { color: var(--dim); font-size: 12px; }

#userDialog {
  width: min(92vw, 480px);
  max-height: 85vh;
  overflow: auto;
  padding: 20px;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 9px;
}
#userDialog::backdrop { background: #000a; }
.dialogHead { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.dialogHead h2 { margin: 0; }
#usersList { padding-left: 20px; color: var(--dim); }
#addUserForm { border-top: 1px solid var(--line); padding-top: 12px; }
#addUserForm h3 { margin: 0; color: var(--text); }

#topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
#topbar .brand {
  font-weight: 700;
  color: #fff;
  letter-spacing: 0.5px;
  margin-right: 6px;
}
#topbar .spacer { flex: 1; }
#topbar .brand::before {
  content: "";
  display: inline-block;
  width: 12px;
  height: 12px;
  background: var(--maroon);
  margin-right: 7px;
  border-radius: 2px;
}

select, input[type="text"], input:not([type]), button {
  background: #31353d;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 5px;
  padding: 5px 9px;
  font: inherit;
}
select:focus, input:focus, textarea:focus { outline: 1px solid var(--maroon); }
button { cursor: pointer; }
button:hover { background: #3a3f48; }
button.primary {
  background: var(--maroon);
  border-color: #a02a10;
  color: #fff;
}
button.primary:hover { background: #99220a; }
#newIssueId { width: 170px; }
#status { color: var(--dim); font-size: 13px; }
#status.ok { color: #7dc47d; }
#status.err { color: #e07a6a; }

main { flex: 1; display: flex; min-height: 0; }

#left {
  width: 46%;
  min-width: 380px;
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--line);
}

body.viz-mode #left {
  width: 100%;
  border-right: 0;
}
body.viz-mode #right { display: none; }

#pageList {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  padding: 8px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.pageChip {
  border: 1px solid var(--line);
  background: #31353d;
  color: var(--text);
  border-radius: 5px;
  padding: 4px 8px;
  font-size: 12px;
  cursor: pointer;
  max-width: 190px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.pageChip b { color: var(--maroon); filter: brightness(1.9); margin-right: 4px; }
.pageChip.active { background: var(--maroon); border-color: #a02a10; color: #fff; }
.pageChip.active b { color: #ffd9cf; filter: none; }
.pageChip.dirty::after { content: " •"; color: #ffbf47; }
.pageChip.addPages {
  color: #fff;
  border-color: #6c7566;
  background: #354137;
}
.pageChip.addPages:hover { background: #435447; }

#editorPane { flex: 1; display: flex; flex-direction: column; min-height: 0; }
#editorHead {
  padding: 7px 10px;
  font-size: 13px;
  color: var(--dim);
  display: flex;
  justify-content: space-between;
}
#editorTitle { color: var(--text); font-weight: 600; }
.dirty { color: #ffbf47; }
#headRight { display: flex; align-items: center; gap: 10px; }
#modeTabs { display: flex; gap: 4px; }
.modeTab {
  padding: 3px 10px;
  font-size: 12px;
  border: 1px solid var(--line);
  background: #31353d;
  color: var(--dim);
  border-radius: 5px;
  cursor: pointer;
}
.modeTab:hover { background: #3a3f48; color: var(--text); }
.modeTab.active {
  background: var(--maroon);
  border-color: #a02a10;
  color: #fff;
}

#vizPane {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
}
#vizPane[hidden] { display: none; }
#vizToolbar {
  display: flex;
  gap: 5px;
  padding: 6px 10px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
#vizToolbar button {
  padding: 3px 9px;
  font-size: 12px;
  line-height: 1.3;
}
#viz { flex: 1; border: none; width: 100%; background: #44474d; }

#editor {
  flex: 1;
  resize: none;
  border: none;
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  background: #17191d;
  color: #d8dbe0;
  padding: 10px 12px;
  font: 13px/1.5 Consolas, "Cascadia Mono", monospace;
  white-space: pre;
  tab-size: 2;
}

#imageBar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  background: var(--panel);
  min-height: 34px;
  flex-wrap: wrap;
}
.filebtn {
  background: #31353d;
  border: 1px solid var(--line);
  border-radius: 5px;
  padding: 4px 9px;
  font-size: 12px;
  cursor: pointer;
}
.filebtn:hover { background: #3a3f48; }
#imageList { display: flex; gap: 6px; flex-wrap: wrap; }
.imgchip {
  font-size: 12px;
  color: var(--dim);
  background: #31353d;
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 2px 7px;
  cursor: pointer;
}
.imgchip:hover { color: #fff; border-color: var(--maroon); }

#right {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  background: #101114;
}
#preview {
  flex: 1;
  border: none;
  width: 100%;
  background: #44474d;
}

#toast {
  position: fixed;
  bottom: 18px;
  left: 50%;
  transform: translateX(-50%);
  background: #000c;
  color: #fff;
  padding: 9px 16px;
  border-radius: 6px;
  font-size: 13px;
  z-index: 10;
  max-width: 70%;
}
#toast.err { background: #7a1f12; }
