:root {
  --bg: #0e111a;
  --chrome: #1a1f2e;
  --chrome-2: #22293b;
  --line: #2d354b;
  --fg: #e1e6f0;
  --dim: #828ca0;
  --accent: #ffc850;
  --link: #aac8ff;
  --btn: #2d364e;
  --btn-hover: #3a4562;
  --danger: #ff6b6b;
  --shadow: 0 10px 30px rgba(0, 0, 0, .45);
  --font: Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --bar-h: 52px;
  color-scheme: dark;
}
:root[data-theme="light"] {
  --bg: #f6f7fa;
  --chrome: #eaecf2;
  --chrome-2: #ffffff;
  --line: #d3d7e1;
  --fg: #1c202a;
  --dim: #697182;
  --accent: #c87800;
  --link: #285abe;
  --btn: #dadee7;
  --btn-hover: #cdd2de;
  --danger: #d72d2d;
  --shadow: 0 10px 30px rgba(20, 30, 50, .18);
  color-scheme: light;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  display: grid;
  grid-template: var(--bar-h) 1fr / 1fr 340px;
  grid-template-areas: "bar bar" "stage panel";
  background: var(--bg);
  color: var(--fg);
  font: 14px/1.4 var(--font);
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }

/* ---- top bar */
.bar {
  grid-area: bar;
  display: flex; align-items: center; gap: 12px;
  padding: 0 10px 0 14px;
  padding-top: env(safe-area-inset-top);
  background: var(--chrome);
  border-bottom: 1px solid var(--line);
  min-width: 0;
}
.brand { display: flex; align-items: center; gap: 8px; color: var(--fg); text-decoration: none; font-weight: 700; font-size: 16px; }
.brand img { border-radius: 6px; }
.channel {
  font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em;
  padding: 2px 7px; border-radius: 999px; text-decoration: none;
  color: #1c202a; background: var(--accent);
}
.doc-name { color: var(--dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; flex: 0 1 auto; max-width: 28vw; }
.menus { display: flex; gap: 2px; }
.menus button {
  background: none; border: 0; padding: 6px 10px; border-radius: 6px;
}
.menus button:hover, .menus button[aria-expanded="true"] { background: var(--btn); }
.bar-actions { margin-left: auto; display: flex; gap: 4px; }
.icon-btn {
  display: inline-grid; place-items: center;
  width: 38px; height: 38px; padding: 0;
  border: 0; border-radius: 8px; background: none;
}
.icon-btn:hover { background: var(--btn); }
.icon-btn:disabled { opacity: .35; cursor: default; background: none; }
.icon-btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
#menuBtn { display: none; }

/* ---- canvas */
#stage { grid-area: stage; position: relative; min-width: 0; min-height: 0; overflow: hidden; }
#tree { display: block; width: 100%; height: 100%; touch-action: none; user-select: none; -webkit-user-select: none; outline: none; }
.hint {
  position: absolute; left: 12px; top: 10px; right: 12px;
  color: var(--dim); font-size: 13px; pointer-events: none;
}
.hud {
  position: absolute; left: 10px; bottom: 8px; right: 70px;
  color: var(--dim); font-size: 12px; pointer-events: none;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.zoom-btns { position: absolute; right: 10px; bottom: 10px; display: flex; flex-direction: column; gap: 6px; }
.zoom-btns .icon-btn { background: var(--chrome); border: 1px solid var(--line); }
.zoom-btns .icon-btn:hover { background: var(--btn); }
.drop-zone {
  position: absolute; inset: 12px; display: grid; place-items: center;
  border: 2px dashed var(--accent); border-radius: 14px;
  background: color-mix(in srgb, var(--bg) 80%, transparent);
  color: var(--fg); font-size: 18px; pointer-events: none;
}

/* ---- side panel */
.panel {
  grid-area: panel;
  display: flex; flex-direction: column; gap: 10px;
  padding: 12px;
  background: var(--chrome);
  border-left: 1px solid var(--line);
  overflow-y: auto; min-height: 0;
}
.sheet-handle { display: none; }
.search { position: relative; }
.search input {
  width: 100%; padding: 8px 10px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--chrome-2); outline: none;
}
.search input:focus { border-color: var(--link); }
.results {
  list-style: none; margin: 4px 0 0; padding: 4px;
  max-height: 240px; overflow-y: auto;
  background: var(--chrome-2); border: 1px solid var(--line); border-radius: 8px;
}
.results li { padding: 6px 8px; border-radius: 6px; cursor: pointer; }
.results li .in { color: var(--dim); font-size: 12px; margin-left: 6px; }
.results li.cur, .results li:hover { background: var(--btn); }
.results li.inert { cursor: default; color: var(--dim); background: none; }
.empty { color: var(--dim); }
.crumbs { display: flex; flex-wrap: wrap; align-items: baseline; gap: 2px 4px; font-size: 12px; color: var(--dim); margin-top: 2px; }
.crumbs:empty { display: none; }
.crumbs button { background: none; border: 0; padding: 1px 2px; color: var(--link); border-radius: 4px; max-width: 100%; overflow-wrap: anywhere; text-align: left; }
.crumbs button:hover { background: var(--btn); }
.crumbs .sep { opacity: .7; }
#details h2 { margin: 2px 0 8px; color: var(--accent); font-size: 20px; line-height: 1.25; overflow-wrap: anywhere; }
.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.actions button {
  padding: 8px 6px; border: 0; border-radius: 8px; background: var(--btn); min-height: 36px;
}
.actions button:hover:not(:disabled) { background: var(--btn-hover); }
.actions button:disabled { opacity: .4; cursor: default; }
.actions button.danger { color: var(--danger); }
.actions kbd { color: var(--dim); font: 12px var(--font); margin-left: 4px; }
.meta { color: var(--dim); white-space: pre-line; margin: 12px 0 6px; font-size: 13px; }
.link-btn { background: none; border: 0; padding: 2px 0; color: var(--link); text-align: left; }
.panel h3 { font-size: 13px; color: var(--dim); font-weight: 600; margin: 12px 0 4px; }
.kids { list-style: none; margin: 0; padding: 0; max-height: 180px; overflow-y: auto; }
.kids li { padding: 4px 6px; border-radius: 6px; cursor: pointer; color: var(--link); }
.kids li:hover { background: var(--btn); }
.info { white-space: pre-wrap; overflow-wrap: anywhere; }
.info.none { color: var(--dim); font-style: italic; }
.info a { color: var(--link); }
.pics { display: flex; flex-wrap: wrap; gap: 6px; }
.pics:empty { display: none; }
.pics .pic { position: relative; }
.pics img { display: block; height: 72px; max-width: 140px; object-fit: cover; border-radius: 6px; border: 1px solid var(--line); cursor: zoom-in; background: var(--chrome-2); }
/* remove: a little white x in a little black circle in the corner */
.pics .pic button { position: absolute; top: 3px; right: 3px; width: 18px; height: 18px; padding: 0; border: 0; border-radius: 50%; background: #000; color: #fff; font-size: 13px; line-height: 18px; }
.panel.drop-target { outline: 2px dashed var(--accent); outline-offset: -6px; }
.edit-pics-row { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 12px; }
.edit-pics-row .pics { width: 100%; }
/* the edit box shows its pictures as one row that scrolls sideways */
.edit-pics-row .carousel { position: relative; width: 100%; }
#editPics { position: relative; flex-wrap: nowrap; overflow-x: auto; overflow-y: hidden; scroll-snap-type: x proximity; padding-bottom: 4px; }
/* arrows on the middle of each edge, shown while the row can scroll that way */
.car-btn { position: absolute; top: calc(50% - 14px); width: 24px; height: 24px; padding: 0; border: 0; border-radius: 50%;
           background: rgba(128, 128, 128, .6); color: #000; font-size: 18px; line-height: 24px; }
.car-btn:hover { background: rgba(160, 160, 160, .85); }
.car-btn.back { left: 4px; }
.car-btn.next { right: 4px; }
#editPics .pic { flex: none; scroll-snap-align: start; }
#editDlg.drop-target { outline: 2px dashed var(--accent); }
/* the picture viewer is the size of the edit box, whose place it takes when opened from it */
/* 1.5 times the edit box's area (1.2247 = the square root of 1.5), within the window */
.pic-dlg { width: min(686px, calc(100vw - 24px)); height: min(637px, calc(100vh - 32px)); max-width: calc(100vw - 24px); max-height: calc(100vh - 32px); padding: 12px; }
.pic-dlg[open] { display: flex; flex-direction: column; gap: 6px; }
.pic-frame { position: relative; flex: 1; min-height: 0; }
#picBig { display: block; width: 100%; height: 100%; object-fit: contain; }
/* close and arrows: a black mark in a translucent grey circle, placed on the picture itself */
.pic-btn { position: absolute; width: 30px; height: 30px; padding: 0; border: 0; border-radius: 50%;
           background: rgba(128, 128, 128, .55); color: #000; font-size: 22px; line-height: 30px; }
.pic-btn:hover { background: rgba(160, 160, 160, .8); }
.pic-close { top: 6px; right: 6px; z-index: 1; }  /* on the box's corner */
.pic-dlg .dlg-hint { text-align: center; }
.pic-dlg .dlg-hint:empty { display: none; }

/* ---- popup menus */
.menu-pop {
  position: fixed; z-index: 20; min-width: 220px; max-width: calc(100vw - 16px);
  max-height: calc(100vh - 16px); overflow-y: auto;
  padding: 6px; border-radius: 10px;
  background: var(--chrome-2); border: 1px solid var(--line); box-shadow: var(--shadow);
}
.menu-pop .sec { padding: 8px 10px 4px; color: var(--dim); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.menu-pop hr { border: 0; border-top: 1px solid var(--line); margin: 4px 2px; }
.menu-pop button {
  display: flex; align-items: center; gap: 10px; width: 100%;
  padding: 8px 10px; border: 0; border-radius: 6px; background: none; text-align: left;
}
.menu-pop button:hover:not(:disabled), .menu-pop button:focus-visible { background: var(--btn); outline: none; }
.menu-pop button:disabled { opacity: .4; cursor: default; }
.menu-pop .check { width: 14px; color: var(--accent); }
/* rendered (not display:none) so browsers will open its picker, anchored where the colour menu was */
.color-input { position: fixed; width: 1px; height: 1px; padding: 0; border: 0; opacity: 0; pointer-events: none; }
.menu-pop .swatch { width: 14px; height: 14px; border-radius: 50%; border: 1px solid var(--line); flex: none; }
.menu-pop .key { margin-left: auto; color: var(--dim); font-size: 12px; padding-left: 16px; }
.menu-pop .danger { color: var(--danger); }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 30;
  padding: 8px 14px; border-radius: 8px; background: var(--chrome-2); border: 1px solid var(--line);
  box-shadow: var(--shadow); max-width: calc(100vw - 32px);
}

/* hover note */
.tip {
  position: fixed; z-index: 25; max-width: min(360px, calc(100vw - 24px)); max-height: 40vh; overflow: hidden;
  padding: 8px 10px; border-radius: 8px; pointer-events: none;
  background: var(--chrome-2); border: 1px solid var(--line); box-shadow: var(--shadow);
  font-size: 13px; white-space: pre-wrap; overflow-wrap: anywhere;
}
.tip b { display: block; color: var(--accent); margin-bottom: 2px; }

/* ---- dialogs */
dialog {
  border: 1px solid var(--line); border-radius: 12px; padding: 0;
  background: var(--chrome-2); color: var(--fg); box-shadow: var(--shadow);
  width: min(560px, calc(100vw - 24px));
}
dialog::backdrop { background: rgba(0, 0, 0, .45); }
dialog form { padding: 18px; display: flex; flex-direction: column; gap: 10px; }
dialog h2 { margin: 0 0 4px; font-size: 18px; }
dialog label { display: flex; flex-direction: column; gap: 4px; color: var(--dim); font-size: 13px; }
dialog input, dialog textarea {
  padding: 8px 10px; border-radius: 8px; border: 1px solid var(--line);
  background: var(--bg); font-size: 15px; outline: none; resize: vertical;
}
dialog input:focus, dialog textarea:focus { border-color: var(--link); }
.dlg-hint { color: var(--dim); font-size: 12px; margin: 0; }
.dlg-hint a { color: var(--link); }
.dlg-buttons { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }
.dlg-buttons button { padding: 8px 16px; border-radius: 8px; border: 0; background: var(--btn); }
.dlg-buttons button.primary { background: #3a5ca0; color: #fff; }
.dlg-buttons button.danger-btn { background: #b8403f; }
.dlg-buttons button:disabled { opacity: .45; cursor: default; }
.confirm-text { margin: 0; }
.choice { border: 0; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 4px 16px; align-items: center; }
.choice legend { color: var(--dim); font-size: 13px; padding: 0; margin-bottom: 4px; width: 100%; }
dialog .choice label, dialog .check-row { flex-direction: row; align-items: center; gap: 6px; color: var(--fg); font-size: 14px; }
dialog input[type="radio"], dialog input[type="checkbox"] { padding: 0; margin: 0; accent-color: #3a5ca0; }
.palette { width: min(600px, calc(100vw - 24px)); margin-top: 12vh; }
.palette form { padding: 10px; gap: 6px; }
.palette-list { max-height: min(420px, 60vh); margin: 0; border: 0; padding: 0; background: none; }
.palette-list .key { float: right; color: var(--dim); font-size: 12px; margin-left: 12px; }
.palette-list .grp { color: var(--dim); font-size: 12px; margin-left: 8px; }
.palette-list li.off { opacity: .45; }
.help { width: min(640px, calc(100vw - 24px)); max-height: calc(100vh - 32px); }
.help h3 { margin: 10px 0 2px; font-size: 14px; }
.help p { margin: 0; }
.help table { border-collapse: collapse; font-size: 13px; }
.help td { padding: 3px 14px 3px 0; vertical-align: top; }
.help td:first-child { white-space: nowrap; color: var(--accent); }

@media (pointer: coarse) { #editForm .dlg-hint { display: none; } }

/* ---- phones and narrow windows: menus fold into one button, the panel becomes a bottom sheet */
@media (max-width: 760px) {
  body { grid-template: var(--bar-h) 1fr / 1fr; grid-template-areas: "bar" "stage"; }
  .menus { display: none; }
  #menuBtn { display: inline-grid; }
  .brand span { display: none; }
  .doc-name { max-width: none; flex: 1 1 auto; }
  .bar { gap: 8px; }
  .hint { display: none; }
  .panel {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
    max-height: 30vh; padding: 0 12px calc(12px + env(safe-area-inset-bottom));
    border-left: 0; border-top: 1px solid var(--line);
    border-radius: 16px 16px 0 0; box-shadow: var(--shadow);
    transition: max-height .2s ease;
  }
  .panel.expanded { max-height: 78vh; }
  .panel:not(.expanded) .more { display: none; }
  .sheet-handle {
    display: grid; place-items: center; flex: none;
    height: 22px; margin: 0 -12px; border: 0; background: none;
  }
  .sheet-handle span { width: 40px; height: 4px; border-radius: 2px; background: var(--dim); opacity: .6; }
  .actions { grid-template-columns: repeat(4, 1fr); }
  .actions button { padding: 8px 2px; font-size: 13px; min-height: 40px; }
  .actions kbd { display: none; }
  #details h2 { font-size: 17px; margin-bottom: 6px; }
  .search input { font-size: 16px; }  /* no zoom-on-focus on iOS */
  .hud { bottom: auto; top: 8px; right: 10px; }
  .zoom-btns { bottom: calc(30vh + 10px); }
  .toast { bottom: calc(30vh + 16px); }
  dialog input, dialog textarea { font-size: 16px; }
}
