* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #1b2a22; color: #eee; font: 14px system-ui, sans-serif; }
[hidden] { display: none !important; }
button, select, input { font: inherit; }

/* ---- table ---- */
#viewport {
  position: fixed; inset: 0; overflow: hidden; touch-action: none; user-select: none; -webkit-user-select: none;
  cursor: grab; background-color: #1f3a2c;
  background-image: radial-gradient(circle, #34603f 1.6px, transparent 2.2px);
}
body.tool-draw #viewport { cursor: crosshair; }
body.tool-select #viewport { cursor: default; }
#world { position: absolute; left: 0; top: 0; width: 0; height: 0; transform-origin: 0 0; }
#items { position: absolute; left: 0; top: 0; z-index: 1; }
#items img { position: absolute; display: block; user-select: none; -webkit-user-drag: none; }
#items img.sel { outline: calc(2px / var(--k, 1)) dashed #ffd54a; }
#svg { position: absolute; left: 0; top: 0; overflow: visible; z-index: 2; pointer-events: none; }
#svg polyline { fill: none; stroke-linecap: round; stroke-linejoin: round; }
#svg .selhl { stroke: #ffd54a; stroke-opacity: .75; stroke-width: calc(var(--sw) * 1px + 8px / var(--k, 1)); }
#svg .hit { stroke: transparent; stroke-width: max(calc(var(--sw) * 1px), calc(18px / var(--k, 1))); }
body.tool-select #svg .hit { pointer-events: stroke; cursor: pointer; }
#handle {
  position: absolute; z-index: 3; width: calc(16px / var(--k, 1)); height: calc(16px / var(--k, 1));
  background: #ffd54a; border: calc(1px / var(--k, 1)) solid #000; border-radius: 2px;
  transform: translate(-50%, -50%); cursor: nwse-resize;
}
.marquee { position: fixed; z-index: 6; pointer-events: none; border: 1px solid #8fd3a0; background: #8fd3a02e; }
button:disabled { opacity: .4; cursor: default; }
#empty { position: fixed; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center; font-size: 22px; color: #ffffff66; pointer-events: none; z-index: 5; }
#empty small { font-size: 14px; margin-top: 6px; }

/* ---- toolbar ---- */
.ui { position: fixed; z-index: 10; background: #12181ce8; border: 1px solid #ffffff22; border-radius: 8px; }
#bar { top: 8px; left: 8px; right: 8px; display: flex; flex-wrap: wrap; gap: 6px 14px; padding: 6px 8px; align-items: center; }
.grp { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; }
.grp.right { margin-left: auto; }
#who { opacity: .75; margin-right: 6px; }
button, .btn {
  background: #2a343a; color: #eee; border: 1px solid #ffffff2a; border-radius: 6px; padding: 5px 10px; cursor: pointer;
}
button:hover, .btn:hover { background: #38454c; }
button.on { background: #3d6b4f; border-color: #8fd3a0; }
button.danger { border-color: #a04444; }
button.danger:hover { background: #6b2d2d; }
button.sw { width: 24px; height: 24px; padding: 0; border-radius: 50%; border: 2px solid #ffffff55; }
button.sw.on { border-color: #fff; box-shadow: 0 0 0 2px #8fd3a0; }
button.sz { width: 30px; height: 28px; padding: 0; display: inline-flex; align-items: center; justify-content: center; }
button.sz i { display: block; background: #eee; border-radius: 50%; }
input[type=color] { width: 30px; height: 26px; padding: 0; border: 1px solid #ffffff44; background: none; border-radius: 6px; cursor: pointer; }

#ctx { left: 50%; bottom: 14px; transform: translateX(-50%); padding: 8px 12px; display: flex; gap: 8px; align-items: center; }
.item-only { display: inline-flex; gap: 4px; }

/* ---- library ---- */
#lib { top: 64px; right: 8px; bottom: 8px; display: flex; flex-direction: column; gap: 8px; padding: 10px; }
.libhead { display: flex; justify-content: space-between; align-items: center; }
.libup { display: flex; flex-direction: column; gap: 6px; }
.libup input, #libFilter { width: 100%; background: #1c2429; color: #eee; border: 1px solid #ffffff2a; border-radius: 6px; padding: 5px 8px; }
.libup .btn { text-align: center; }
#upStatus { color: #ffd54a; min-height: 1em; font-size: 12px; }
#libGrid { flex: 1; overflow-y: auto; display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; align-content: start; }
.card { background: #1c2429; border: 1px solid #ffffff1f; border-radius: 6px; padding: 6px; display: flex; flex-direction: column; gap: 4px; }
.card img { width: 100%; height: 90px; object-fit: contain; background: #0006; border-radius: 4px; cursor: pointer; }
.card .nm { font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card .row { display: flex; gap: 3px; }
.card .row button { flex: 1; padding: 2px 4px; font-size: 11px; }

/* ---- scenes ---- */
#scenes { top: 64px; right: 8px; bottom: 8px; display: flex; flex-direction: column; gap: 8px; padding: 10px; }
#scenes .libup input[type=text], #scenes .libup input:not([type]), #scFilter { width: 100%; background: #1c2429; color: #eee; border: 1px solid #ffffff2a; border-radius: 6px; padding: 5px 8px; }
.chk { display: flex; gap: 6px; align-items: flex-start; font-size: 12px; opacity: .9; }
.chk input { width: auto !important; margin: 2px 0 0; flex: none; }
#scList { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 8px; }
.scene { background: #1c2429; border: 1px solid #ffffff1f; border-radius: 6px; padding: 8px; display: flex; flex-direction: column; gap: 4px; }
.scene .nm { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.scene .meta { font-size: 11px; opacity: .65; }
.scene .row { display: flex; flex-wrap: wrap; gap: 3px; }
.scene .row button { padding: 2px 8px; font-size: 12px; }
#dlgBack { position: fixed; inset: 0; z-index: 90; background: #0008; display: flex; align-items: center; justify-content: center; }
#dlg { background: #12181c; border: 1px solid #ffffff33; border-radius: 10px; padding: 18px; width: min(420px, 92vw); }
#dlg p { margin: 0 0 14px; line-height: 1.4; }
#dlgBtns { display: flex; flex-wrap: wrap; gap: 8px; justify-content: flex-end; }
@media (max-width: 700px) { #scenes { top: auto; height: 60vh; } }

/* ---- login ---- */
#login { position: fixed; inset: 0; z-index: 100; background: #1b2a22; display: flex; align-items: center; justify-content: center; }
#loginForm { background: #12181c; border: 1px solid #ffffff22; border-radius: 10px; padding: 24px; width: min(340px, 92vw); display: flex; flex-direction: column; gap: 12px; }
#loginForm h1 { margin: 0 0 4px; font-size: 22px; text-align: center; }
#loginForm label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; }
#loginForm input { background: #1c2429; color: #eee; border: 1px solid #ffffff2a; border-radius: 6px; padding: 8px; }
#loginForm button { padding: 9px; background: #3d6b4f; }
#lErr { color: #ff8a80; margin: 0; min-height: 1em; font-size: 13px; }

@media (max-width: 700px) {
  #bar { gap: 4px 8px; }
  #lib { top: auto; height: 55vh; }
}

/* ---- panel resizing, library selection ---- */
#lib, #scenes { width: min(var(--panelw, 340px), calc(100vw - 16px)); }
.resizer { position: absolute; left: -4px; top: 0; bottom: 0; width: 9px; cursor: ew-resize; z-index: 2; touch-action: none; }
.resizer:hover, .resizer.drag { background: #8fd3a055; }
#libGrid { grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); }
.card { position: relative; }
.card .pick { position: absolute; top: 8px; left: 8px; width: 16px; height: 16px; margin: 0; cursor: pointer; }
#libSel { display: flex; gap: 6px; align-items: center; font-size: 12px; }
#libSel button { padding: 2px 8px; font-size: 12px; }

/* ---- rotate handle, movable images, folder dialog ---- */
#rhandle {
  position: absolute; z-index: 3; width: calc(14px / var(--k, 1)); height: calc(14px / var(--k, 1));
  background: #8fd3a0; border: calc(1px / var(--k, 1)) solid #000; border-radius: 50%;
  transform: translate(-50%, -50%); cursor: grab;
}
body.tool-select #items img.mv { outline: calc(2px / var(--k, 1)) dotted #8fd3a0cc; outline-offset: calc(-2px / var(--k, 1)); }
#items img.sel.mv { outline-style: dashed; outline-color: #ffd54a; }
.dlgback { position: fixed; inset: 0; z-index: 95; background: #0008; display: flex; align-items: center; justify-content: center; }
.dlgback[hidden] { display: none; }
.dlgbox { background: #12181c; border: 1px solid #ffffff33; border-radius: 10px; padding: 18px; width: min(420px, 92vw); display: flex; flex-direction: column; gap: 10px; }
.dlgbox p { margin: 0; }
.dlgbox input { background: #1c2429; color: #eee; border: 1px solid #ffffff2a; border-radius: 6px; padding: 7px 8px; }
.dlgbtns { display: flex; gap: 8px; justify-content: flex-end; }
.dlgbtns .primary { background: #3d6b4f; }
#fdList { display: flex; flex-wrap: wrap; gap: 6px; max-height: 160px; overflow-y: auto; font-size: 13px; opacity: .95; }
#fdList button.on { background: #3d6b4f; border-color: #8fd3a0; }

/* library cards: buttons wrap instead of overflowing the card */
.card { min-width: 0; }
.card .row { flex-wrap: wrap; }
.card .row button { flex: 1 1 auto; min-width: 0; }
#libGrid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
