:root {
  --sand: #FFF5E1; --sand2: #F3DFB6; --navy: #1D3557; --navy2: #2a4670; --teal: #1B98A6;
  --coral: #EF6A4C; --yellow: #FFD65C; --card: #fff; --muted: #5b6b80; --bad: #d64545; --ok: #2e9d62;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body { margin: 0; background: var(--sand); color: var(--navy);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; }
button { font: inherit; font-weight: 700; border: 0; border-radius: 999px; padding: 8px 16px;
  background: var(--navy); color: #fff; cursor: pointer; }
button:hover { background: var(--navy2); }
button:disabled { opacity: .45; cursor: default; }
button.primary { background: var(--coral); }
button.primary:hover { background: #d95a3d; }
button.ghost { background: #fff; color: var(--navy); border: 1.5px solid var(--sand2); }
button.ghost:hover { border-color: var(--navy); }
button.danger { color: var(--bad); }
button.small { padding: 5px 12px; font-size: 13px; }
input, textarea, select { font: inherit; color: inherit; border: 1.5px solid var(--sand2); border-radius: 8px;
  padding: 7px 9px; background: #fff; }
textarea { width: 100%; resize: vertical; }
code { background: var(--sand); padding: 1px 4px; border-radius: 4px; }
h1, h2, h3 { margin: 0; }
h3 { font-size: 16px; margin-bottom: 10px; }
.muted { color: var(--muted); font-weight: 500; }
.hint { color: var(--muted); font-size: 12.5px; margin: 6px 0 10px; }
.spacer { flex: 1; }
.row { display: flex; gap: 8px; align-items: center; margin: 8px 0; }
.row.wrap { flex-wrap: wrap; }
.row.end { justify-content: flex-end; }

header.top { height: 54px; display: flex; align-items: center; gap: 14px; padding: 0 16px;
  background: var(--navy); color: #fff; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 900; font-size: 17px; white-space: nowrap; }
.booksel { display: flex; align-items: center; gap: 6px; font-weight: 600; }
.booksel select { min-width: 200px; color: var(--navy); }
.status { font-size: 13px; opacity: .9; }

.layout { display: grid; grid-template-columns: 230px 1fr; height: calc(100% - 54px); }
aside.scenes { background: #fff; border-right: 1.5px solid var(--sand2); overflow: auto; padding: 12px; }
.scenes-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.scenes-head h2 { font-size: 15px; }
#sceneList { list-style: none; margin: 0; padding: 0; }
#sceneList li { padding: 8px 10px; border-radius: 10px; cursor: pointer; display: flex; gap: 8px; align-items: center; }
#sceneList li:hover { background: var(--sand); }
#sceneList li.on { background: var(--navy); color: #fff; }
#sceneList .n { font-weight: 900; color: var(--coral); min-width: 20px; }
#sceneList li.on .n { color: var(--yellow); }
#sceneList .t { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dots { display: flex; gap: 3px; }
.dot { width: 7px; height: 7px; border-radius: 50%; background: var(--sand2); }
.dot.ok { background: var(--ok); }

main { overflow: auto; padding: 16px 20px; }
.empty { max-width: 560px; margin: 80px auto; text-align: center; color: var(--muted); }
.empty h1 { color: var(--navy); margin-bottom: 8px; }
.scene-head { display: flex; align-items: center; gap: 12px; margin-bottom: 14px; }
.scene-head .num { font-weight: 900; color: var(--coral); font-size: 22px; }
.title-input { font-size: 22px; font-weight: 900; border-color: transparent; background: transparent; min-width: 240px; }
.title-input:hover, .title-input:focus { border-color: var(--sand2); background: #fff; }
.tabs { margin-left: auto; display: flex; gap: 4px; background: #fff; padding: 4px; border-radius: 999px; border: 1.5px solid var(--sand2); }
.tab { background: transparent; color: var(--navy); }
.tab:hover { background: var(--sand); }
.tab.on { background: var(--navy); color: #fff; }

.card { background: var(--card); border-radius: 14px; padding: 16px; box-shadow: 0 1px 4px rgba(29,53,87,.08); }
.setup-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.field { display: block; font-weight: 600; margin-bottom: 4px; }
.field textarea { margin-top: 6px; font-weight: 400; }
.field.inline { display: flex; align-items: center; gap: 8px; font-weight: 600; margin: 8px 0; }
.field.inline input { flex: 1; padding: 0; border: 0; }
.field.inline output { width: 42px; text-align: right; color: var(--muted); }
.jobmsg { font-size: 13px; color: var(--muted); }
.jobmsg.err { color: var(--bad); }
.preview img { width: 100%; border-radius: 10px; display: block; margin-top: 6px; }
.warn { background: #fff1e6; color: #a2461f; border-radius: 8px; padding: 8px 10px; margin-top: 8px; font-size: 13px; }
.stgrid { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; margin-top: 8px; }
.stcell { background: var(--sand); border-radius: 8px; padding: 4px; text-align: center; font-size: 11px; position: relative; }
.stcell img { width: 100%; aspect-ratio: 1; object-fit: contain; display: block; }
.stcell.missing { color: var(--bad); }
.stcell label { display: flex; align-items: center; justify-content: center; gap: 3px; font-size: 10.5px; color: var(--muted); }
.stcell .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.place { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 18px; align-items: start; }
.stage-wrap { position: sticky; top: 0; }
.stage { position: relative; width: 100%; aspect-ratio: 1; max-height: calc(100vh - 150px); max-width: calc(100vh - 150px);
  background: #e9dcc0; border-radius: 12px; overflow: hidden; outline: none; user-select: none; touch-action: none;
  box-shadow: 0 2px 12px rgba(29,53,87,.18); }
.stage:focus-visible { box-shadow: 0 0 0 3px var(--teal); }
#bg { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
#layer { position: absolute; inset: 0; }
.stage-empty { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); font-weight: 600; }
.pl { position: absolute; cursor: grab; filter: drop-shadow(1px 2px 1.5px rgba(90,60,20,.35)); }
.pl img { width: 100%; height: 100%; display: block; pointer-events: none; }
.pl.sel { outline: 2px dashed var(--teal); outline-offset: 3px; }
.pl.dragging { cursor: grabbing; }
.circle { position: absolute; border: 2px solid #E6007E; border-radius: 50%; pointer-events: none; box-shadow: 0 0 0 2px #fff; }
.circle.bad { border-color: var(--bad); border-style: dashed; }
.ghost-drag { position: fixed; pointer-events: none; z-index: 50; transform: translate(-50%, -50%); opacity: .85; }
.ghost-drag img { width: 64px; height: 64px; object-fit: contain; }

.side .totals { font-weight: 700; margin-bottom: 8px; }
.side .totals .ok { color: var(--ok); }
.side .totals .bad { color: var(--bad); }
.tray { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-bottom: 12px; }
.ti { background: #fff; border: 1.5px solid var(--sand2); border-radius: 10px; padding: 4px 4px 2px; cursor: grab;
  touch-action: none; position: relative; }
.ti:hover { border-color: var(--navy); }
.ti img { width: 100%; aspect-ratio: 1; object-fit: contain; display: block; pointer-events: none; }
.ti .cnt { display: flex; justify-content: center; align-items: center; gap: 2px; font-size: 11.5px; font-weight: 800; }
.ti .cnt button { padding: 0 5px; line-height: 16px; font-size: 12px; background: var(--sand); color: var(--navy); border-radius: 6px; }
.ti.done { border-color: var(--ok); }
.ti.over { border-color: var(--bad); }
.ti.done .cnt .v { color: var(--ok); }
.ti.over .cnt .v { color: var(--bad); }
.sel { padding: 12px; margin-bottom: 8px; }
.sel-head { display: flex; align-items: center; gap: 8px; }
.sel-head img { width: 34px; height: 34px; object-fit: contain; }
.sel.none .field, .sel.none .row { opacity: .4; pointer-events: none; }
.check { display: flex; align-items: center; gap: 5px; font-weight: 600; font-size: 13px; }
.keys { line-height: 1.6; }
.saved-note { font-size: 13px; color: var(--ok); font-weight: 600; min-height: 1.4em; }
.saved-note.dirty { color: #a2461f; }

@media (max-width: 1100px) {
  .place { grid-template-columns: minmax(0, 1fr); }
  .setup-grid { grid-template-columns: 1fr; }
}

.buildbox { border-top: 1.5px solid var(--sand2); margin-top: 14px; padding-top: 12px; }
.buildbox h2 { font-size: 15px; }
.buildbox a { color: var(--teal); font-weight: 700; }
.signout { margin-top: 14px; }
body.login { display: grid; place-items: center; min-height: 100%; padding: 16px; }
body.login form { width: 100%; max-width: 340px; display: grid; gap: 12px; }
body.login input { width: 100%; margin-top: 6px; font-size: 16px; }
.touch-keys { display: none; }
@media (pointer: coarse) {
  .touch-keys { display: block; }
  .keys { display: none; }
  input, textarea, select { font-size: 16px; } /* stops iOS zooming into fields */
}

/* Phones: one column, scene list becomes a horizontal strip above the editor. */
@media (max-width: 760px) {
  header.top { height: auto; flex-wrap: wrap; gap: 8px; padding: 8px 12px; }
  .brand { flex: 1; }
  .booksel { order: 5; width: 100%; }
  .booksel select { min-width: 0; flex: 1; }
  .status { width: 100%; }
  .layout { display: block; height: auto; }
  aside.scenes { border-right: 0; border-bottom: 1.5px solid var(--sand2); padding: 10px 12px; }
  #sceneList { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 4px; }
  #sceneList li { flex: 0 0 auto; max-width: 200px; background: var(--sand); }
  .buildbox { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 10px; margin-top: 8px; padding-top: 8px; }
  .buildbox h2 { font-size: 14px; }
  .buildbox .hint { display: none; }
  .buildbox .jobmsg { margin: 0; width: 100%; }
  .signout { display: none; }
  main { padding: 12px; overflow: visible; }
  .scene-head { flex-wrap: wrap; gap: 8px; }
  .title-input { min-width: 0; flex: 1; font-size: 19px; }
  .tabs { margin-left: 0; width: 100%; }
  .tab { flex: 1; padding: 8px 6px; }
  .stage { max-width: 100%; max-height: none; }
  .stage-wrap { z-index: 5; background: var(--sand); padding-bottom: 6px; }
  .tray { grid-template-columns: repeat(5, minmax(0, 1fr)); }
  .ti .cnt { font-size: 10.5px; }
  .ti .cnt button { padding: 0 3px; }
  .stgrid { grid-template-columns: repeat(4, 1fr); }
}
