:root {
  color-scheme: dark;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  background: #090909;
  color: #f4f4f4;
  --line: #353535;
  --muted: #888;
  --panel: rgba(15, 15, 15, 0.94);
}

* { box-sizing: border-box; }

html, body { width: 100%; height: 100%; margin: 0; overflow: hidden; }
body { background: #090909; }
button, input, select { font: inherit; }
button { color: inherit; }

.mobile-notice { display: none; }

.model-loader {
  position: fixed;
  z-index: 100;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  background: #090909;
}
.model-loader-card { width: min(520px, 100%); }
.model-loader-heading { display: flex; justify-content: space-between; margin-bottom: 30px; color: #888; font-size: 10px; letter-spacing: .12em; }
.model-loader-heading span:first-child { color: #fff; font-weight: 700; }
.model-loader-card strong { display: block; max-width: 12ch; font-family: Arial, sans-serif; font-size: clamp(32px, 7vw, 58px); line-height: .95; letter-spacing: -.05em; }
.model-loader-card p { min-height: 1.4em; margin: 24px 0 10px; color: #aaa; font-size: 10px; }
.model-load-track { height: 8px; overflow: hidden; border: 1px solid #444; background: #111; }
.model-load-track div { width: 100%; height: 100%; background: #fff; transform: scaleX(0); transform-origin: left; transition: transform .12s linear; }
.model-loader-card small { display: block; margin-top: 10px; color: #666; font-size: 8px; letter-spacing: .05em; }
.model-loader.error .model-load-track div { background: #ff6b6b; }

header {
  position: fixed;
  z-index: 20;
  top: 0;
  left: 0;
  right: 0;
  height: 62px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 22px;
  pointer-events: none;
}

header > * { pointer-events: auto; }
.brand { color: #fff; text-decoration: none; font-size: 12px; font-weight: 700; letter-spacing: .14em; }
.brand span { color: var(--muted); }
.header-actions { display: flex; gap: 7px; }
.zoom-controls { display: flex; align-items: center; border: 1px solid #292929; }
.zoom-controls .text-button { min-width: 30px; padding-inline: 7px; }
.zoom-controls .zoom-value { min-width: 48px; border-inline: 1px solid #292929; color: #777; font-size: 9px; }
.text-button, .icon-button { border: 0; background: none; cursor: pointer; }
.text-button { padding: 9px; color: #aaa; font-size: 11px; }
.text-button:hover { color: white; }
.text-button:disabled { color: #444; cursor: default; }

#stage { position: fixed; inset: 0; cursor: default; touch-action: none; }
#stage.dragging { cursor: grabbing; }
#canvas { display: block; width: 100%; height: 100%; image-rendering: pixelated; }
#mobileGallery { display: none; }

#emptyState {
  position: absolute;
  left: 50%;
  top: 43%;
  display: grid;
  gap: 8px;
  text-align: center;
  transform: translate(-50%, -50%);
  pointer-events: none;
}
#emptyState strong { font-family: Arial, sans-serif; font-size: clamp(28px, 4vw, 52px); letter-spacing: -.05em; }
#emptyState span { color: var(--muted); font-size: 11px; }
.mobile-help { display: none; }

.panel {
  position: fixed;
  z-index: 15;
  background: var(--panel);
  border: 1px solid var(--line);
  backdrop-filter: blur(16px);
}
.panel-heading { min-height: 41px; padding: 0 11px; display: flex; align-items: center; justify-content: space-between; border-bottom: 1px solid var(--line); font-size: 11px; text-transform: uppercase; letter-spacing: .08em; }
.panel-heading small { color: #666; font-size: 8px; }
.icon-button { padding: 8px; color: #888; font-size: 19px; line-height: 1; }

#inspector { left: 50%; bottom: 105px; width: min(410px, calc(100vw - 40px)); transform: translateX(-50%); }
.palette { display: grid; grid-template-columns: repeat(4, 1fr); height: 44px; }
.color-control { position: relative; border-right: 1px solid #111; overflow: hidden; }
.color-control:last-child { border: 0; }
.color-control input { position: absolute; inset: -10px; width: calc(100% + 20px); height: calc(100% + 20px); border: 0; padding: 0; cursor: pointer; }
.color-control span { position: absolute; z-index: 1; left: 7px; bottom: 5px; color: white; font: 8px/1 monospace; mix-blend-mode: difference; pointer-events: none; }
.palette-actions { display: grid; grid-template-columns: repeat(4, 1fr); border-top: 1px solid var(--line); }
.palette-actions button { height: 34px; border: 0; border-right: 1px solid var(--line); background: transparent; color: #bbb; font-size: 9px; cursor: pointer; }
.palette-actions button:last-child { border: 0; }
.palette-actions button:hover { background: #f0f0f0; color: #090909; }
.palette-actions button:disabled { color: #555; cursor: wait; }
.transform-row { display: grid; grid-template-columns: repeat(3, 1fr); border-top: 1px solid var(--line); }
.transform-row button { height: 34px; border: 0; border-right: 1px solid var(--line); background: transparent; color: #bbb; font-size: 9px; cursor: pointer; }
.transform-row button:last-child { border: 0; }
.transform-row button:hover { background: #f0f0f0; color: #090909; }
.group-row { display: grid; grid-template-columns: 1fr 1fr; border-top: 1px solid var(--line); }
.group-row button { height: 34px; border: 0; border-right: 1px solid var(--line); background: transparent; color: #bbb; font-size: 9px; cursor: pointer; }
.group-row button:last-child { border: 0; }
.group-row button:hover:not(:disabled) { background: #f0f0f0; color: #090909; }
.group-row button:disabled { color: #444; cursor: default; }
.tool-row { display: grid; grid-template-columns: 40px 40px 1fr 1fr 1fr; border-top: 1px solid var(--line); }
.tool-row button { height: 38px; border: 0; border-right: 1px solid var(--line); background: transparent; font-size: 10px; cursor: pointer; }
.tool-row button:last-child { border: 0; }
.tool-row button:hover { background: #f0f0f0; color: #090909; }
.tool-row .delete { color: #aaa; }

#pixelEditor { width: min(680px, calc(100vw - 32px)); max-width: none; max-height: calc(100dvh - 32px); padding: 0; overflow: auto; border: 1px solid #555; background: #0c0c0c; color: #fff; }
#pixelEditor::backdrop { background: rgba(0,0,0,.78); backdrop-filter: blur(5px); }
.pixel-editor-window { display: grid; grid-template-rows: auto 1fr auto; }
.pixel-editor-head { min-height: 58px; display: flex; align-items: center; justify-content: space-between; padding: 0 12px 0 18px; border-bottom: 1px solid var(--line); }
.pixel-editor-head div { display: grid; gap: 5px; }
.pixel-editor-head span { color: #777; font-size: 8px; letter-spacing: .1em; text-transform: uppercase; }
.pixel-editor-head strong { font-size: 12px; font-weight: 600; }
.pixel-editor-body { display: grid; place-items: center; padding: 24px; background: #080808; }
#pixelEditorCanvas { display: block; width: min(100%, 52vh, 520px); aspect-ratio: 1; border: 1px solid #444; background: #111; image-rendering: pixelated; cursor: crosshair; touch-action: none; }
.pixel-editor-controls { min-height: 64px; display: grid; grid-template-columns: auto 1fr auto; align-items: stretch; border-top: 1px solid var(--line); }
.editor-tools { display: grid; grid-template-columns: 1fr 1fr; }
.editor-tools button, .editor-done { min-width: 86px; border: 0; border-right: 1px solid var(--line); background: transparent; color: #aaa; font-size: 10px; cursor: pointer; }
.editor-tools button.active { background: #eee; color: #111; }
.editor-colors { display: grid; grid-template-columns: repeat(4, minmax(36px, 1fr)); padding: 10px; gap: 6px; }
.editor-color { min-width: 0; border: 1px solid #555; cursor: pointer; }
.editor-color.active { outline: 2px solid #fff; outline-offset: 2px; }
.editor-done { border-right: 0; border-left: 1px solid var(--line); padding: 0 22px; background: #eee; color: #111; }

.export-dialog { width: min(480px, calc(100vw - 32px)); max-width: none; padding: 0; border: 1px solid #555; background: #0c0c0c; color: #fff; }
.export-dialog::backdrop { background: rgba(0,0,0,.78); backdrop-filter: blur(5px); }
.export-dialog-head { min-height: 60px; display: flex; align-items: center; justify-content: space-between; padding: 0 12px 0 18px; border-bottom: 1px solid var(--line); }
.export-dialog-head div { display: grid; gap: 5px; }
.export-dialog-head span { color: #777; font-size: 8px; letter-spacing: .1em; text-transform: uppercase; }
.export-dialog-head strong { font-size: 12px; }
.export-options { display: grid; }
.export-options button { min-height: 72px; display: flex; align-items: center; justify-content: space-between; gap: 24px; padding: 0 18px; border: 0; border-bottom: 1px solid var(--line); background: transparent; color: #fff; text-align: left; cursor: pointer; }
.export-options button:last-child { border-bottom: 0; }
.export-options button:hover { background: #eee; color: #111; }
.export-options button strong { font-size: 11px; }
.export-options button span { color: #777; font-size: 8px; }

.layers-panel { top: 66px; right: 20px; width: 210px; max-height: calc(100vh - 180px); overflow: hidden; }
.panel-tabs { min-height: 41px; display: grid; grid-template-columns: 1fr 1fr; border-bottom: 1px solid var(--line); }
.panel-tabs button { border: 0; border-right: 1px solid var(--line); background: transparent; color: #666; cursor: pointer; font-size: 10px; text-transform: uppercase; letter-spacing: .08em; }
.panel-tabs button:last-child { border-right: 0; }
.panel-tabs button[aria-selected="true"] { background: #eee; color: #111; }
#layers, #history { max-height: calc(100vh - 222px); overflow: auto; }
.layer { width: 100%; display: grid; grid-template-columns: 14px minmax(0, 1fr) 30px; align-items: stretch; border-bottom: 1px solid #252525; background: transparent; font-size: 10px; }
.layer::before { content: "⋮⋮"; align-self: center; color: #555; font-size: 9px; letter-spacing: -4px; cursor: grab; }
.layer:hover, .layer.selected { background: #eee; color: #111; }
.layer.dragging { opacity: .35; }
.layer.drop-above { box-shadow: inset 0 2px #fff; }
.layer.drop-below { box-shadow: inset 0 -2px #fff; }
.layer-main, .layer-delete { border: 0; background: transparent; cursor: pointer; }
.layer-main { min-width: 0; display: flex; justify-content: space-between; gap: 8px; padding: 11px 6px; text-align: left; }
.layer-main span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.layer-main small { flex: 0 0 auto; opacity: .5; }
.layer-delete { color: #777; font-size: 15px; }
.layer-delete:hover { background: #d54242; color: #fff; }
.panel-empty { padding: 18px 11px; color: #666; font-size: 9px; text-align: center; }
.history-entry { display: flex; align-items: center; gap: 8px; width: 100%; padding: 9px 11px; border-bottom: 1px solid #252525; color: #aaa; font-size: 9px; }
.history-entry::before { content: ""; width: 5px; height: 5px; flex: 0 0 auto; background: #555; }
.history-entry.latest { color: #fff; }
.history-entry.latest::before { background: #fff; }
.history-entry.undone { color: #555; text-decoration: line-through; }
.history-entry.undone::before { background: transparent; border: 1px solid #555; }

#promptForm {
  position: fixed;
  z-index: 30;
  left: 50%;
  bottom: 20px;
  width: min(620px, calc(100vw - 32px));
  height: 64px;
  display: grid;
  grid-template-columns: 1fr 150px auto;
  transform: translateX(-50%);
  background: #f2f2f2;
  border: 1px solid white;
  box-shadow: 0 12px 50px rgba(0,0,0,.5);
}
#prompt { min-width: 0; padding: 0 18px; border: 0; outline: 0; background: transparent; color: #0a0a0a; font-size: 15px; }
#prompt::placeholder { color: #777; }
.temperature-control { display: grid; grid-template-columns: 1fr; align-items: center; padding: 0 8px; color: #555; font-size: 8px; text-transform: uppercase; letter-spacing: .06em; }
.temperature-control select { width: 100%; min-width: 0; border: 1px solid #aaa; border-radius: 0; padding: 7px 24px 7px 7px; background: #fff; color: #111; font-size: 9px; }
#generateButton { margin: 7px; padding: 0 15px; border: 0; background: #0b0b0b; cursor: pointer; font-size: 10px; }
#generateButton i { display: none; font-style: normal; }
#generateButton:hover { background: #292929; }
#generateButton:disabled { color: #777; cursor: wait; }
#status { position: absolute; left: 0; bottom: -17px; color: #777; font-size: 8px; letter-spacing: .04em; }

.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0; }
[hidden] { display: none !important; }

@media (max-width: 640px) {
  body { --mobile-top: 0px; }
  body.mobile-notice-dismissed { --mobile-top: 0px; }
  .mobile-notice { position: fixed; z-index: 45; inset: 0 0 auto; height: 34px; align-items: center; justify-content: center; gap: 8px; padding-left: 32px; border-bottom: 1px solid #2d2d2d; background: #151515; color: #aaa; font-size: 9px; letter-spacing: .02em; }
  .mobile-notice button { position: absolute; right: 6px; width: 30px; height: 30px; border: 0; background: transparent; color: #aaa; font-size: 18px; cursor: pointer; }
  body.mobile-notice-dismissed .mobile-notice { display: none; }
  header { top: var(--mobile-top); height: 48px; padding: 0 10px; background: linear-gradient(#090909 70%, transparent); }
  .brand { font-size: 10px; }
  .header-actions { gap: 0; }
  .text-button { min-height: 38px; padding: 6px 8px; font-size: 9px; }
  #undoButton, #redoButton, .zoom-controls { display: none; }
  #toggleLayersPanel { font-size: 0; }
  #toggleLayersPanel::after { content: "Layers"; font-size: 9px; }
  #stage { top: var(--mobile-top); overflow-y: auto; touch-action: pan-y; }
  #canvas { display: none; }
  #mobileGallery { min-height: 100%; display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); align-content: start; gap: 8px; padding: 58px 10px 128px; }
  .mobile-sprite { min-width: 0; border: 1px solid #292929; background: #101010; color: #aaa; }
  .mobile-sprite.selected { border-color: #fff; color: #fff; }
  .mobile-sprite-preview { display: block; width: 100%; padding: 8px; border: 0; background: transparent; color: inherit; text-align: left; cursor: pointer; }
  .mobile-sprite svg { display: block; width: 100%; aspect-ratio: 1; background: #090909; image-rendering: pixelated; }
  .mobile-sprite span { display: block; overflow: hidden; padding-top: 7px; font-size: 9px; text-overflow: ellipsis; white-space: nowrap; }
  .mobile-sprite-actions { display: grid; grid-template-columns: repeat(3, 1fr); border-top: 1px solid #292929; }
  .mobile-sprite-actions button { min-width: 0; height: 32px; border: 0; border-right: 1px solid #292929; background: transparent; color: #bbb; font-size: 8px; text-transform: uppercase; cursor: pointer; }
  .mobile-sprite-actions button:last-child { border-right: 0; }
  .mobile-sprite-actions button:disabled { color: #555; cursor: wait; }
  #emptyState { position: fixed; top: calc(var(--mobile-top) + 42%); width: calc(100% - 36px); }
  #emptyState strong { font-size: 28px; }
  #emptyState span { max-width: 28ch; margin: auto; line-height: 1.5; }
  #emptyState .desktop-help { display: none; }
  #emptyState .mobile-help { display: block; }
  #inspector { display: none !important; }
  .panel-heading { min-height: 36px; }
  .palette { height: 36px; }
  .palette-actions button, .transform-row button, .group-row button { height: 31px; }
  .tool-row button { height: 34px; }
  .layers-panel { top: calc(var(--mobile-top) + 50px); right: 8px; width: min(230px, calc(100vw - 16px)); max-height: 42vh; }
  #layers, #history { max-height: calc(35vh - 42px); }
  #promptForm { bottom: var(--safe-bottom, 0px); top: auto; width: calc(100vw - 16px); height: 64px; grid-template-columns: minmax(0, 1fr) 92px 62px; grid-template-rows: 1fr; }
  #prompt { padding: 0 11px; font-size: 16px; }
  .temperature-control { grid-column: 2; grid-row: 1; grid-template-columns: minmax(0, 1fr); padding: 10px 5px 10px 0; }
  .temperature-control select { height: 44px; padding: 0 22px 0 8px; font-size: 16px; }
  #generateButton { grid-column: 3; grid-row: 1; margin: 6px; padding: 0; font-size: 16px; }
  #generateButton span { display: none; }
  #generateButton i { display: inline; }
  #status { display: none; }
}

@media (max-width: 640px) and (hover: none) and (pointer: coarse) {
  body { --mobile-top: 34px; }
  body.mobile-notice-dismissed { --mobile-top: 0px; }
  .mobile-notice { display: flex; }
}
