:root {
  --bg: #16161d; --panel: #1f1f29; --panel2: #2a2a37; --line: #3a3a4b;
  --ink: #e8e6df; --dim: #9896a3; --acc: #a7f070; --acc-ink: #10210a; --warn: #ef7d57;
  --keep: repeating-linear-gradient(45deg, #ff00ff 0 3px, #2a2a37 3px 6px);
  font: 14px/1.3 ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--ink); overscroll-behavior: none; }
body { position: fixed; inset: 0; -webkit-user-select: none; user-select: none; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
button, select, input { font: inherit; color: var(--ink); }
button, select {
  background: var(--panel2); border: 1px solid var(--line); border-radius: 4px;
  min-height: 34px; min-width: 34px; padding: 0 9px; cursor: pointer; white-space: nowrap;
}
button:hover { border-color: var(--dim); }
button:focus-visible, select:focus-visible, input:focus-visible { outline: 2px solid var(--acc); outline-offset: 1px; }
button.primary { background: var(--acc); color: var(--acc-ink); border-color: var(--acc); font-weight: 700; }
button.on, button.tog.on { background: var(--acc); color: var(--acc-ink); border-color: var(--acc); }
button.danger { background: none; color: var(--warn); border-color: var(--warn); }
input[type=text], input[type=password], input[type=number], input:not([type]) {
  background: var(--bg); border: 1px solid var(--line); border-radius: 4px; min-height: 34px; padding: 0 8px;
  -webkit-user-select: text; user-select: text;
}
input[type=number] { width: 72px; }
.grow { flex: 1; }
.sep { width: 1px; align-self: stretch; background: var(--line); margin: 0 2px; flex: none; }
.err { color: var(--warn); min-height: 1.3em; margin: 0; }
.logo { font-size: 22px; margin: 0; letter-spacing: 1px; }
.logo b { color: var(--acc); }

.screen { position: absolute; inset: 0; }
.bar { display: flex; align-items: center; gap: 6px; padding: 6px 8px; background: var(--panel); border-bottom: 1px solid var(--line); }
.strip { display: flex; align-items: center; gap: 6px; padding: 5px 8px; background: var(--panel); overflow-x: auto; scrollbar-width: thin; }
.strip > * { flex: none; }
.strip label { color: var(--dim); display: flex; align-items: center; gap: 4px; white-space: nowrap; }

/* auth + home */
#auth { display: grid; place-items: center; padding: 16px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 24px; width: min(340px, 100%); display: grid; gap: 12px; }
.card p { margin: 0; color: var(--dim); }
#home { display: flex; flex-direction: column; }
#plist { flex: 1; overflow-y: auto; padding: 14px; display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); align-content: start; }
.proj { background: var(--panel); border: 1px solid var(--line); border-radius: 6px; padding: 10px; display: grid; gap: 6px; cursor: pointer; text-align: left; min-height: 0; }
.proj:hover { border-color: var(--acc); }
.proj .th { aspect-ratio: 1; background: #0e0e13; border-radius: 4px; display: grid; place-items: center; overflow: hidden; }
.proj img { max-width: 100%; max-height: 100%; width: 100%; height: 100%; object-fit: contain; image-rendering: pixelated; }
.proj .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 700; }
.proj .meta { color: var(--dim); font-size: 12px; display: flex; justify-content: space-between; align-items: center; }
.proj .meta button { min-height: 26px; min-width: 26px; padding: 0 6px; }
.empty { color: var(--dim); grid-column: 1/-1; text-align: center; padding: 40px 0; }

/* editor */
#editor { display: grid; grid-template-columns: 52px minmax(0, 1fr) 176px; grid-template-rows: auto auto 1fr auto;
  grid-template-areas: "top top top" "opts opts opts" "tools stage side" "tl tl tl"; }
#top { grid-area: top; }
#pname { flex: 1; min-width: 60px; font-weight: 700; }
#status { color: var(--dim); font-size: 12px; white-space: nowrap; }
#opts { grid-area: opts; border-bottom: 1px solid var(--line); }
#tools { grid-area: tools; flex-direction: column; overflow-y: auto; border-right: 1px solid var(--line); padding: 6px; }
#tools button { width: 38px; height: 38px; padding: 0; font-size: 17px; }
#stage { grid-area: stage; position: relative; overflow: hidden; min-height: 0; min-width: 0; background: #0e0e13; }
#view { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; cursor: crosshair; }
#pv { position: absolute; top: 8px; right: 8px; background: #0e0e13; border: 1px solid var(--line); border-radius: 4px; padding: 4px; cursor: pointer; line-height: 0;
  max-width: 45%; max-height: 45%; overflow: hidden; }
#pv.light { background: #d8d8d8; }
#pvc { image-rendering: pixelated; display: block; max-width: 100%; }
#pvl { position: absolute; left: 3px; bottom: 2px; font-size: 10px; line-height: 1; color: var(--dim); background: #0e0e13cc; padding: 1px 3px; border-radius: 2px; }
#zoomctl { position: absolute; left: 8px; bottom: 8px; display: flex; align-items: center; gap: 4px; background: #1f1f29dd; border-radius: 4px; padding: 3px; }
#zlabel { min-width: 38px; text-align: center; color: var(--dim); }
#pos { position: absolute; right: 8px; bottom: 8px; color: var(--dim); font-size: 12px; pointer-events: none; }
#side { grid-area: side; flex-direction: column; align-items: stretch; flex-wrap: nowrap; border-left: 1px solid var(--line); overflow-y: auto; }
#curwrap { height: 40px; border: 2px solid var(--ink); border-radius: 4px; overflow: hidden; padding: 0; }
#curwrap.off { border-color: var(--line); opacity: .6; }
#cur { width: 100%; height: 100%; border: 0; padding: 0; background: none; cursor: pointer; }
#keepsw { background: var(--keep); height: 34px; border-width: 2px; }
#keepsw.on { border-color: var(--ink); background: var(--keep); }
#keepsw::after { content: "keep blank"; background: #16161dcc; color: var(--ink); padding: 1px 5px; border-radius: 2px; font-size: 12px; }
#pal { display: grid; grid-template-columns: repeat(auto-fill, minmax(26px, 1fr)); gap: 3px; }
#pal button { min-width: 0; min-height: 0; aspect-ratio: 1; padding: 0; border: 1px solid #0006; border-radius: 2px; }
#pal button.on { outline: 2px solid var(--ink); outline-offset: 1px; z-index: 1; }

#tl { grid-area: tl; border-top: 1px solid var(--line); background: var(--panel); min-width: 0; display: flex; flex-direction: column; max-height: 38vh; }
#tl.fold #tlscroll { display: none; }
#tlscroll { overflow: auto; min-height: 0; }
#tlgrid { border-collapse: separate; border-spacing: 0; }
#tlgrid th, #tlgrid td { border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); padding: 0; font-weight: 400; }
#tlgrid .lh { position: sticky; left: 0; z-index: 2; background: var(--panel); min-width: 118px; max-width: 118px; text-align: left; padding: 0 4px; }
#tlgrid .lh.act { background: var(--panel2); box-shadow: inset 3px 0 var(--acc); }
#tlgrid .lh div { display: flex; align-items: center; gap: 4px; }
#tlgrid .lh button { min-width: 26px; min-height: 26px; padding: 0; border: 0; background: none; }
#tlgrid .lh .nm { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; padding: 6px 0; }
#tlgrid .fh { min-width: 40px; cursor: pointer; text-align: center; font-size: 11px; color: var(--dim); padding: 3px 2px; }
#tlgrid .fh canvas { display: block; margin: 0 auto 2px; width: 32px; height: 32px; image-rendering: pixelated; background: #0e0e13; object-fit: contain; }
#tlgrid .fh.cur { background: var(--panel2); color: var(--ink); box-shadow: inset 0 3px var(--acc); }
#tlgrid td.cel { height: 30px; text-align: center; cursor: pointer; color: var(--dim); }
#tlgrid td.cel.cur { background: #ffffff0d; }
#tlgrid td.cel.act { background: var(--acc); color: var(--acc-ink); }
#tlgrid td.tg { height: 18px; position: relative; font-size: 11px; cursor: pointer; border-right-color: transparent; }
#tlgrid td.tg span { position: absolute; left: 4px; top: 2px; white-space: nowrap; z-index: 1; color: #000; font-weight: 700; pointer-events: none; }
#tlgrid td.tg.last { border-right-color: var(--line); }

dialog { background: var(--panel); color: var(--ink); border: 1px solid var(--line); border-radius: 8px; padding: 18px; width: min(360px, 92vw); }
dialog::backdrop { background: #000a; }
dialog h2 { margin: 0 0 12px; font-size: 16px; }
dialog form { display: grid; gap: 10px; }
dialog label { display: grid; gap: 4px; color: var(--dim); }
dialog label.row { display: flex; align-items: center; gap: 8px; }
dialog label > input:not([type=checkbox]):not([type=color]), dialog select { width: 100%; }
dialog .btns { display: flex; gap: 8px; justify-content: flex-end; margin-top: 6px; }
dialog p { margin: 0; color: var(--dim); }
#toast { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); background: var(--ink); color: var(--bg); padding: 8px 14px; border-radius: 4px;
  opacity: 0; transition: opacity .2s; pointer-events: none; z-index: 99; max-width: 90vw; text-align: center; }
#toast.show { opacity: 1; }

@media (max-width: 780px) {
  #editor { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto auto minmax(0, 1fr) auto auto auto;
    grid-template-areas: "top" "opts" "stage" "tools" "side" "tl"; }
  #tools { flex-direction: row; border-right: 0; border-top: 1px solid var(--line); overflow-y: hidden; padding: 5px 8px; }
  #tools button { width: 42px; height: 40px; }
  #side { flex-direction: row; align-items: center; border-left: 0; border-top: 1px solid var(--line); overflow-y: hidden; }
  #curwrap { width: 44px; height: 36px; }
  #keepsw { width: 44px; }
  #keepsw::after { content: "∅"; }
  #pal { display: flex; }
  #pal button { width: 30px; height: 30px; flex: none; }
  #tl { max-height: 30vh; }
  #savebtn { display: none; }
  #tlgrid .lh { min-width: 92px; max-width: 92px; }
  #pos { display: none; }
  body { padding-bottom: env(safe-area-inset-bottom); }
}
