:root {
  --bg: #16151c;
  --panel: #1f1e27;
  --panel2: #282633;
  --line: #34323f;
  --text: #e6e3ee;
  --dim: #9a96a8;
  --accent: #f0a53c;
  --accent2: #ffc56e;
  --sel: #5fb3ff;
  --danger: #e5645a;
  --radius: 6px;
  color-scheme: dark;
}
* { box-sizing: border-box; }
/* Author display rules (e.g. .menu { display: grid }) would otherwise beat the
   browser's [hidden] style and leave "hidden" elements on screen. */
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 13px/1.35 -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, sans-serif;
  display: flex; flex-direction: column; overflow: hidden;
}
button, input, select { font: inherit; color: inherit; }
button {
  background: var(--panel2); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 5px 10px; cursor: pointer; white-space: nowrap;
}
button:hover { border-color: #4a4758; background: #302e3c; }
button:active { transform: translateY(1px); }
button.primary { background: var(--accent); border-color: var(--accent); color: #221a0e; font-weight: 600; }
button.primary:hover { background: var(--accent2); }
button.on, .seg button.on { background: #3b3550; border-color: var(--sel); color: #fff; }
button:disabled { opacity: .45; cursor: default; }
input[type=text], input:not([type]), input[type=number], select {
  background: #14131a; border: 1px solid var(--line); border-radius: var(--radius); padding: 4px 7px; min-width: 0;
}
input:focus, select:focus, button:focus-visible { outline: 2px solid var(--sel); outline-offset: -1px; }
input[type=range] { accent-color: var(--accent); width: 100%; }
input[type=checkbox] { accent-color: var(--accent); }
input[type=color] { width: 30px; height: 28px; padding: 0 2px; border: 1px solid var(--line); border-radius: var(--radius); background: #14131a; cursor: pointer; }
.hint { color: var(--dim); font-size: 11.5px; }
kbd { background: #2c2a36; border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 4px; padding: 0 5px; font: 11px ui-monospace, monospace; }
code { background: #2c2a36; padding: 0 4px; border-radius: 3px; font: 12px ui-monospace, monospace; }

/* ---------- top bar */
#topbar {
  display: flex; align-items: center; gap: 10px; padding: 8px 12px;
  background: var(--panel); border-bottom: 1px solid var(--line); flex: none;
}
.brand { font-weight: 700; letter-spacing: .3px; display: flex; align-items: center; gap: 8px; }
.logo {
  width: 18px; height: 18px; display: inline-block; image-rendering: pixelated;
  background: conic-gradient(var(--accent) 0 25%, #5fb3ff 0 50%, var(--accent) 0 75%, #5fb3ff 0); border-radius: 3px;
}
.namefield input { width: 180px; font-weight: 600; }
.group { display: flex; gap: 4px; align-items: center; }
.spacer { flex: 1; }
.menu-wrap { position: relative; }
.menu {
  position: absolute; top: calc(100% + 4px); left: 0; z-index: 30; background: var(--panel2);
  border: 1px solid var(--line); border-radius: 8px; padding: 6px; display: grid; grid-template-columns: repeat(3, 92px); gap: 6px;
  box-shadow: 0 10px 30px #0008;
}
.menu { max-height: 75vh; overflow-y: auto; }
.menu-group { grid-column: 1 / -1; font-size: 11px; text-transform: uppercase; letter-spacing: .7px; color: var(--dim); padding: 4px 2px 0; }
.menu button { display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 4px; font-size: 11.5px; }
.menu canvas { width: 72px; height: 84px; image-rendering: pixelated; }

/* ---------- layout */
main { flex: 1; display: grid; grid-template-columns: 300px 1fr 330px; min-height: 0; }
aside, #center { min-height: 0; }
#library { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; }
#inspector { background: var(--panel); border-left: 1px solid var(--line); overflow-y: auto; }
#center { display: flex; flex-direction: column; min-width: 0; }

/* ---------- library */
.lib-search { padding: 10px 10px 6px; }
.lib-search input { width: 100%; }
#cats { display: flex; flex-wrap: wrap; gap: 4px; padding: 4px 10px 8px; border-bottom: 1px solid var(--line); }
#cats button { padding: 3px 8px; font-size: 12px; border-radius: 12px; }
#cats button .n { color: var(--dim); font-size: 10.5px; margin-left: 3px; }
#libTitle { padding: 8px 12px 2px; font-weight: 600; display: flex; justify-content: space-between; align-items: center; gap: 6px; }
#tiles {
  flex: 1; overflow-y: auto; padding: 8px 10px 16px; display: grid;
  grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); gap: 8px; align-content: start;
}
.tile {
  background: var(--panel2); border: 1px solid var(--line); border-radius: 8px; padding: 4px 4px 5px;
  display: flex; flex-direction: column; align-items: center; gap: 3px; cursor: pointer; user-select: none;
}
.tile:hover { border-color: #5a566c; background: #2e2c3a; }
.tile.active { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.tile canvas { width: 72px; height: 72px; image-rendering: pixelated; background: #18171e; border-radius: 5px; }
.tile span { font-size: 11px; text-align: center; line-height: 1.15; color: #d6d2e0; }
.tile.none canvas { opacity: .35; }
.tile-group { grid-column: 1 / -1; font-size: 11px; text-transform: uppercase; letter-spacing: .7px; color: var(--dim); margin: 6px 2px -2px; }
.tile-action { grid-column: 1 / -1; padding: 10px; text-align: left; }
.tile-action b { display: block; margin-bottom: 2px; }

/* ---------- toolbar */
#toolbar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px; padding: 8px 12px;
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.seg { display: flex; }
.seg button { border-radius: 0; margin-left: -1px; }
.seg button:first-child { border-radius: var(--radius) 0 0 var(--radius); margin-left: 0; }
.seg button:last-child { border-radius: 0 var(--radius) var(--radius) 0; }
label.check { display: inline-flex; align-items: center; gap: 4px; color: var(--dim); cursor: pointer; white-space: nowrap; }
label.check:hover { color: var(--text); }
label.check.warnable:has(input:checked) { color: var(--accent2); }

/* ---------- stage */
#stageWrap {
  flex: 1; min-height: 0; position: relative; display: flex; align-items: center; justify-content: center; overflow: auto;
  background: radial-gradient(circle at 50% 45%, #22212b, #15141a 75%);
}
#stage { image-rendering: pixelated; cursor: default; box-shadow: 0 8px 30px #0007; border-radius: 4px; }
#stage.paint { cursor: crosshair; }
#stage.grab { cursor: grab; }
#stage.grabbing { cursor: grabbing; }
#hoverInfo { position: absolute; left: 10px; bottom: 8px; color: var(--dim); font: 11.5px ui-monospace, monospace; pointer-events: none; }
#dropHint {
  position: absolute; inset: 12px; border: 2px dashed var(--accent); border-radius: 12px; display: none;
  align-items: center; justify-content: center; font-size: 18px; color: var(--accent2); background: #f0a53c14; pointer-events: none;
}
#stageWrap.dropping #dropHint { display: flex; }

/* ---------- timeline */
#timeline {
  display: flex; align-items: center; gap: 10px; padding: 8px 12px; background: var(--panel);
  border-top: 1px solid var(--line); flex-wrap: wrap;
}
#frames { display: flex; gap: 6px; }
.frame { background: #18171e; border: 1px solid var(--line); border-radius: 5px; padding: 2px; cursor: pointer; position: relative; }
.frame canvas { display: block; width: 48px; height: 56px; image-rendering: pixelated; }
.frame.on { border-color: var(--sel); box-shadow: inset 0 0 0 1px var(--sel); }
.frame.tweaked::after { content: ''; position: absolute; top: 3px; right: 3px; width: 6px; height: 6px; border-radius: 50%; background: var(--accent); }
#previews { margin-left: auto; display: flex; gap: 6px; }
.pv { display: flex; flex-direction: column; align-items: center; gap: 2px; cursor: pointer; }
.pv canvas { width: 72px; height: 84px; image-rendering: pixelated; background: #18171e; border: 1px solid var(--line); border-radius: 5px; }
.pv.on canvas { border-color: var(--sel); }
.pv span { font-size: 10.5px; color: var(--dim); }

/* ---------- inspector */
.panel { padding: 10px 12px 12px; border-bottom: 1px solid var(--line); }
.panel h3 { margin: 0 0 8px; font-size: 12px; text-transform: uppercase; letter-spacing: .8px; color: var(--dim); }
.panel-head { display: flex; justify-content: space-between; align-items: baseline; }
#layers { display: flex; flex-direction: column; gap: 2px; max-height: 300px; overflow-y: auto; }
.layer {
  display: grid; grid-template-columns: 22px 12px 1fr auto 22px 22px; align-items: center; gap: 6px;
  padding: 3px 6px; border-radius: 5px; cursor: pointer; border: 1px solid transparent;
}
.layer:hover { background: var(--panel2); }
.layer.on { background: #2f2b40; border-color: #4b4466; }
.layer .dot { width: 12px; height: 12px; border-radius: 3px; border: 1px solid #0006; }
.layer .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.layer .cat { color: var(--dim); font-size: 11px; }
.layer button { padding: 0; width: 22px; height: 22px; border: none; background: none; color: var(--dim); font-size: 13px; }
.layer button:hover { color: var(--text); background: #3a3748; }
.layer.hidden .nm { opacity: .45; text-decoration: line-through; }
.layer .lock.on { color: var(--accent); }
.layer .nm .offc { display: inline; width: auto; height: auto; padding: 0 4px; margin-left: 6px; color: #221a0e; background: var(--accent); border-radius: 4px; font-size: 11px; line-height: 16px; }
.layer .nm .offc:hover { background: var(--accent2); color: #221a0e; }
.warn-text { color: var(--accent2) !important; }
.prop-title { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.prop-title select { flex: 1; }
.section-label { font-size: 11px; text-transform: uppercase; letter-spacing: .7px; color: var(--dim); margin: 12px 0 6px; }
.field { display: grid; grid-template-columns: 110px 1fr; align-items: center; gap: 8px; margin: 5px 0; }
.field > span { color: #cfcbd9; font-size: 12.5px; }
.field .rng { display: grid; grid-template-columns: 1fr 26px; gap: 6px; align-items: center; }
.field .rng output { text-align: right; color: var(--dim); font: 11.5px ui-monospace, monospace; }
.swatches { display: flex; flex-wrap: wrap; gap: 8px; }
.swatch-btn { display: flex; align-items: center; gap: 6px; padding: 3px 8px 3px 3px; }
.swatch-btn i { width: 20px; height: 20px; border-radius: 4px; border: 1px solid #0008; display: block; }
.swatch-btn.auto i { background-image: linear-gradient(135deg, transparent 45%, #fff8 45% 55%, transparent 55%) !important; }
.nudge { display: grid; grid-template-columns: repeat(4, auto) 1fr; gap: 4px; align-items: center; }
.nudge button { padding: 3px 8px; }
.btnrow { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 10px; }
.btnrow .danger { color: var(--danger); }
.dirchecks { display: flex; gap: 10px; }
.imgslots { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.imgslot { background: #18171e; border: 1px dashed var(--line); border-radius: 6px; padding: 4px; text-align: center; font-size: 11px; color: var(--dim); cursor: pointer; }
.imgslot canvas { width: 100%; height: 56px; image-rendering: pixelated; object-fit: contain; display: block; }
.empty-props { color: var(--dim); }
.empty-props p { margin: 4px 0; }

/* ---------- popover */
#popover {
  position: fixed; z-index: 50; background: var(--panel2); border: 1px solid var(--line); border-radius: 10px;
  padding: 10px; width: 250px; box-shadow: 0 12px 40px #000a;
}
#popover .pal { display: grid; grid-template-columns: repeat(8, 1fr); gap: 4px; margin-bottom: 8px; }
#popover .pal button { height: 22px; padding: 0; border-radius: 4px; border: 1px solid #0008; }
#popover .pal button.cur { outline: 2px solid #fff; outline-offset: 1px; }
#popover .pal-label { font-size: 10.5px; text-transform: uppercase; letter-spacing: .6px; color: var(--dim); margin: 2px 0 4px; }
#popover .pop-row { display: flex; gap: 6px; align-items: center; }
#popover .pop-row input:not([type]) { flex: 1; font-family: ui-monospace, monospace; }

/* ---------- modal */
#modalBack { position: fixed; inset: 0; background: #0009; z-index: 40; display: flex; align-items: center; justify-content: center; }
#modalBack[hidden] { display: none; }
.modal { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; width: min(1000px, 94vw); max-height: 90vh; display: flex; flex-direction: column; box-shadow: 0 20px 60px #000c; }
.modal[hidden] { display: none; }
#helpModal { width: min(640px, 94vw); }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 12px 16px; border-bottom: 1px solid var(--line); }
.modal-head h2 { margin: 0; font-size: 16px; }
.modal-head .x { border: none; background: none; font-size: 16px; }
.modal-body { padding: 14px 16px; overflow: auto; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--line); }
.export-grid { display: grid; grid-template-columns: 290px 1fr; gap: 18px; min-height: 0; }
.export-opts { display: flex; flex-direction: column; gap: 10px; }
.export-opts > label { display: flex; flex-direction: column; gap: 4px; color: #cfcbd9; }
.export-opts label.check { flex-direction: row; }
.export-opts fieldset { border: 1px solid var(--line); border-radius: 8px; padding: 6px 8px 8px; margin: 0; }
.export-opts legend { color: #cfcbd9; padding: 0 4px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.row2 label { display: flex; flex-direction: column; gap: 4px; color: #cfcbd9; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips label { display: inline-flex; gap: 4px; align-items: center; background: var(--panel2); border: 1px solid var(--line); border-radius: 14px; padding: 2px 9px 2px 6px; cursor: pointer; }
.export-preview { display: flex; flex-direction: column; gap: 8px; min-width: 0; min-height: 0; }
#exInfo { color: var(--dim); font-size: 12px; }
#exInfo b { color: var(--text); }
#exInfo .warn { color: var(--danger); }
.sheet-scroll { overflow: auto; max-height: 58vh; background: repeating-conic-gradient(#1c1b23 0 25%, #23222c 0 50%) 0 0 / 16px 16px; border-radius: 8px; border: 1px solid var(--line); padding: 8px; }
#exCanvas { image-rendering: pixelated; display: block; }
.help ol { padding-left: 20px; margin: 0; }
.help li { margin: 0 0 8px; }
.help h4 { margin: 14px 0 6px; }

/* ---------- toast */
#toast {
  position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%) translateY(20px); opacity: 0; pointer-events: none;
  background: #2f2c3c; border: 1px solid #4b4466; padding: 8px 14px; border-radius: 8px; transition: .2s; z-index: 60;
}
#toast.show { opacity: 1; transform: translateX(-50%); }

@media (max-width: 1150px) {
  main { grid-template-columns: 250px 1fr 290px; }
  #previews { display: none; }
}
@media (max-width: 900px) {
  body { overflow: auto; height: auto; }
  #topbar { flex-wrap: wrap; }
  main { display: flex; flex-direction: column; }
  #center { order: -1; min-height: 70vh; }
  #stageWrap { min-height: 50vh; }
  #library { max-height: 60vh; border-right: none; border-top: 1px solid var(--line); }
  #inspector { overflow: visible; border-left: none; }
  #previews { display: flex; margin-left: 0; }
  .export-grid { grid-template-columns: 1fr; }
}
