:root {
  --bg: #171716;
  --surface: #20201e;
  --panel: #282825;
  --panel-2: #323330;
  --line: #474943;
  --line-soft: rgba(255, 255, 255, 0.09);
  --text: #f3f2ec;
  --muted: #b5b0a4;
  --accent: #4fb6c8;
  --accent-strong: #69d3e0;
  --accent-warm: #f0b15c;
  --accent-green: #6fc77a;
  --danger: #ff7468;
  --shadow: rgba(0, 0, 0, 0.32);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  height: 100vh;
  overflow: hidden;
  color: var(--text);
  background: var(--bg);
  font-family: "Segoe UI", system-ui, sans-serif;
}

button,
input,
select,
textarea {
  font: inherit;
}

button,
select,
textarea,
input[type="number"] {
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 6px;
}

button {
  min-height: 34px;
  padding: 0 12px;
  cursor: pointer;
}

button:hover,
button:focus-visible,
select:focus-visible,
textarea:focus-visible,
input:focus-visible {
  border-color: var(--accent-strong);
  outline: none;
}

button:disabled {
  color: #7e7b72;
  cursor: not-allowed;
  border-color: #3a3a37;
  background: #252522;
}

h1,
h2,
p {
  margin: 0;
}

.app {
  height: 100vh;
  display: grid;
  grid-template-rows: auto 1fr;
  overflow: hidden;
}

.topbar {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 16px;
  align-items: center;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

.brand {
  display: flex;
  gap: 12px;
  align-items: center;
  min-width: 0;
}

.brand-mark {
  width: 34px;
  height: 34px;
  border: 1px solid #6f756b;
  background:
    linear-gradient(90deg, transparent 7px, rgba(255,255,255,0.17) 8px),
    linear-gradient(transparent 7px, rgba(255,255,255,0.17) 8px),
    linear-gradient(135deg, var(--accent) 0 48%, var(--accent-warm) 48% 72%, var(--accent-green) 72% 100%);
  background-size: 8px 8px, 8px 8px, 100% 100%;
  border-radius: 6px;
  box-shadow: 0 10px 24px var(--shadow);
  flex: 0 0 auto;
}

h1 {
  font-size: 16px;
  line-height: 1.1;
}

.brand p {
  color: var(--muted);
  font-size: 12px;
  margin-top: 3px;
}

.new-canvas,
.top-actions,
.convert-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

.brand-links {
  display: flex;
  gap: 6px;
  align-items: center;
  margin-left: 6px;
  flex: 0 0 auto;
}

.ghost-button {
  min-height: 30px;
  padding: 0 10px;
  font-size: 12px;
  color: var(--muted);
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 6px;
}

.ghost-button:hover,
.ghost-button:focus-visible {
  color: var(--text);
  border-color: var(--accent-strong);
}

.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(0, 0, 0, 0.55);
}

.modal-overlay[hidden] {
  display: none;
}

.modal {
  display: flex;
  flex-direction: column;
  width: min(560px, 100%);
  max-height: 82vh;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.55);
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}

.modal-head h2 {
  font-size: 15px;
}

.modal-close {
  width: 32px;
  height: 32px;
  min-height: 32px;
  padding: 0;
  font-size: 18px;
  line-height: 1;
}

.modal-body {
  overflow: auto;
  padding: 16px;
  font-size: 13px;
  line-height: 1.7;
  color: var(--text);
}

.modal-body h3 {
  margin: 16px 0 6px;
  font-size: 13px;
  color: var(--accent-strong);
}

.modal-body h3:first-child {
  margin-top: 0;
}

.modal-body ul {
  margin: 0 0 6px;
  padding-left: 1.2em;
}

.modal-body li {
  margin: 3px 0;
}

.modal-body .modal-note {
  margin-bottom: 4px;
  color: var(--muted);
  font-size: 12px;
}

.modal-body kbd {
  display: inline-block;
  padding: 1px 6px;
  font-family: inherit;
  font-size: 11px;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 4px;
}

.new-canvas label {
  display: flex;
  gap: 5px;
  align-items: center;
  color: var(--muted);
  font-size: 13px;
}

.new-canvas input {
  width: 70px;
  padding: 6px 8px;
}

.button-icon {
  display: inline-block;
  margin-right: 5px;
  color: var(--accent-strong);
}

.export-scale {
  min-height: 34px;
  padding: 0 6px;
}

.workspace {
  min-height: 0;
  overflow: hidden;
  display: grid;
  grid-template-columns: 58px minmax(360px, 1fr) minmax(320px, 370px);
}

.toolbar,
.inspector {
  background: var(--surface);
  border-color: var(--line);
}

.toolbar {
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: center;
  padding: 12px 8px;
  border-right: 1px solid var(--line);
}

.tool {
  width: 40px;
  height: 40px;
  min-height: 40px;
  padding: 0;
  position: relative;
  display: grid;
  place-items: center;
}

.tool::after {
  content: attr(data-shortcut);
  position: absolute;
  right: 4px;
  bottom: 2px;
  color: var(--muted);
  font-size: 10px;
  font-weight: 700;
  line-height: 1;
}

.tool svg {
  width: 21px;
  height: 21px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.tool.is-active {
  background: var(--accent);
  border-color: var(--accent-strong);
  color: #071316;
}

.tool.is-active::after {
  color: #071316;
}

.toolbar-sep {
  width: 26px;
  height: 1px;
  margin: 4px 0;
  background: var(--line);
}

.side-toggle {
  width: 40px;
  height: 40px;
  min-height: 40px;
  padding: 0;
  position: relative;
  display: grid;
  place-items: center;
}

.side-toggle::after {
  content: attr(data-shortcut);
  position: absolute;
  right: 4px;
  bottom: 2px;
  color: var(--muted);
  font-size: 10px;
  font-weight: 700;
  line-height: 1;
}

.side-toggle svg {
  width: 21px;
  height: 21px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.side-toggle.is-active {
  background: var(--accent-green);
  border-color: #a7e8b0;
  color: #08130b;
}

.side-toggle.is-active::after {
  color: #08130b;
}

.stage {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: auto 1fr;
}

.stage-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
  background: #1d1d1b;
}

.stage-head strong {
  font-size: 14px;
}

#statusText {
  color: var(--muted);
  font-size: 13px;
  margin-left: 10px;
}

.zoom-control {
  display: flex;
  gap: 8px;
  align-items: center;
  color: var(--muted);
  font-size: 13px;
}

.zoom-control input,
.layer-opacity input,
#paletteSize {
  accent-color: var(--accent);
}

.canvas-shell {
  min-height: 0;
  position: relative;
  overflow: hidden;
  display: block;
  padding: 0;
  background:
    linear-gradient(90deg, rgba(255,255,255,0.04) 1px, transparent 1px),
    linear-gradient(rgba(255,255,255,0.04) 1px, transparent 1px),
    #111110;
  background-size: 24px 24px;
}

#pixelCanvas {
  display: block;
  position: absolute;
  left: 0;
  top: 0;
  image-rendering: pixelated;
  box-shadow: 0 16px 42px var(--shadow);
  border: 1px solid #6a7068;
  background: white;
  cursor: crosshair;
  user-select: none;
  touch-action: none;
  transform-origin: 0 0;
}

#pixelCanvas.is-panning {
  cursor: grabbing !important;
}

.inspector {
  min-width: 0;
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 12px;
  border-left: 1px solid var(--line);
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
}

.panel-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
}

.panel-title h2 {
  font-size: 14px;
}

.model-badge {
  max-width: 155px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  color: #10130f;
  background: var(--accent-warm);
  border-radius: 999px;
  padding: 3px 8px;
  font-size: 11px;
  font-weight: 700;
}

.dropzone {
  min-height: 74px;
  display: grid;
  grid-template-columns: 32px minmax(0, 1fr);
  gap: 10px;
  align-items: center;
  padding: 12px;
  border: 1px dashed #76786d;
  border-radius: 8px;
  background:
    linear-gradient(135deg, rgba(79, 182, 200, 0.1), rgba(240, 177, 92, 0.09)),
    #20211f;
  cursor: pointer;
}

.dropzone:hover,
.dropzone:focus-visible,
.dropzone.is-dragging {
  border-color: var(--accent-strong);
  outline: none;
}

.drop-icon {
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  color: #0c1515;
  background: var(--accent-strong);
  border-radius: 6px;
  font-weight: 700;
}

#sourceFileName {
  min-width: 0;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  color: var(--text);
  font-size: 13px;
}

.source-preview {
  position: relative;
  display: none;
  margin-top: 10px;
  min-height: 118px;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 7px;
  background:
    linear-gradient(90deg, rgba(255,255,255,0.06) 1px, transparent 1px),
    linear-gradient(rgba(255,255,255,0.06) 1px, transparent 1px),
    #191a18;
  background-size: 12px 12px;
  overflow: hidden;
}

.source-clear {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 26px;
  height: 26px;
  min-height: 26px;
  padding: 0;
  display: grid;
  place-items: center;
  font-size: 16px;
  line-height: 1;
  color: var(--text);
  background: rgba(20, 22, 24, 0.78);
  border: 1px solid var(--line);
  border-radius: 6px;
}

.source-clear:hover,
.source-clear:focus-visible {
  color: #fff;
  border-color: var(--danger);
  background: rgba(40, 22, 22, 0.9);
}

.source-preview.has-image {
  display: grid;
}

.source-preview img {
  display: block;
  max-width: 100%;
  max-height: 170px;
  object-fit: contain;
}

.control-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 12px;
}

.control-grid label,
.prompt-notes {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 12px;
}

.control-grid select,
.prompt-notes textarea {
  width: 100%;
  min-width: 0;
  padding: 8px;
}

.control-grid input[type="range"] {
  width: 100%;
}

.control-grid output {
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.toggle-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 12px;
  color: var(--text);
  font-size: 13px;
}

.toggle-row input {
  width: 16px;
  height: 16px;
  accent-color: var(--accent-green);
}

.prompt-notes {
  margin-top: 12px;
}

.prompt-notes textarea {
  min-height: 72px;
  resize: vertical;
  line-height: 1.45;
}

.convert-actions {
  margin-top: 12px;
}

.convert-actions button {
  flex: 1 1 0;
}

.primary-action {
  color: #081313;
  background: var(--accent-strong);
  border-color: #a7eef4;
  font-weight: 700;
}

.convert-status {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.5;
  margin-top: 9px;
}

.preview-monitor {
  width: 100%;
  min-height: 150px;
  display: grid;
  place-items: center;
  background:
    linear-gradient(90deg, rgba(255,255,255,0.06) 1px, transparent 1px),
    linear-gradient(rgba(255,255,255,0.06) 1px, transparent 1px),
    #191a18;
  background-size: 12px 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  overflow: hidden;
}

#previewCanvas {
  display: block;
  max-width: 100%;
  max-height: 180px;
  image-rendering: pixelated;
}

#colorPicker {
  width: 42px;
  height: 34px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: transparent;
}

.color-readout {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}

#currentSwatch {
  width: 30px;
  height: 30px;
  border-radius: 6px;
  border: 1px solid rgba(255,255,255,0.32);
}

#currentColorText {
  color: var(--muted);
}

.palette-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-bottom: 10px;
}

.palette-tab {
  flex: 1 1 auto;
  min-height: 28px;
  padding: 0 6px;
  font-size: 11px;
  color: var(--muted);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 6px;
}

.palette-tab:hover,
.palette-tab:focus-visible {
  color: var(--text);
  border-color: var(--accent-strong);
}

.palette-tab.is-active {
  color: #071316;
  background: var(--accent);
  border-color: var(--accent-strong);
  font-weight: 700;
}

.palette-hint {
  margin-top: 8px;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.45;
}

.palette-grid {
  display: grid;
  grid-template-columns: repeat(8, minmax(0, 1fr));
  gap: 6px;
  margin-bottom: 10px;
}

.swatch {
  aspect-ratio: 1;
  min-height: 26px;
  border: 2px solid rgba(255,255,255,0.18);
  border-radius: 5px;
  padding: 0;
}

.swatch.is-active {
  border-color: #ffffff;
  box-shadow: 0 0 0 2px var(--accent);
}

.layer-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.layer-row {
  display: grid;
  grid-template-columns: 32px 32px minmax(0, 1fr) 32px;
  gap: 6px;
  align-items: center;
  padding: 7px;
  background: #20211f;
  border: 1px solid var(--line);
  border-radius: 7px;
}

.layer-opacity {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: auto 1fr 38px;
  gap: 8px;
  align-items: center;
  color: var(--muted);
  font-size: 12px;
}

.layer-opacity input {
  width: 100%;
}

.opacity-value {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.layer-row.is-active {
  border-color: var(--accent);
  background: #243433;
}

.layer-row button {
  min-height: 28px;
  width: 28px;
  padding: 0;
}

.icon-button {
  display: inline-grid;
  place-items: center;
}

.icon-button svg {
  width: 16px;
  height: 16px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.layer-name {
  min-width: 0;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  color: var(--text);
  font-size: 13px;
}

.layer-row.is-locked .layer-name {
  color: var(--muted);
}

.layer-name-input {
  min-width: 0;
  width: 100%;
  padding: 3px 6px;
  font-size: 13px;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--accent-strong);
  border-radius: 5px;
}

@media (max-width: 1080px) {
  .topbar {
    grid-template-columns: 1fr;
  }

  .workspace {
    grid-template-columns: 54px minmax(0, 1fr);
    grid-template-rows: minmax(430px, 1fr) auto;
  }

  .inspector {
    grid-column: 1 / -1;
    border-left: 0;
    border-top: 1px solid var(--line);
    display: grid;
    grid-template-columns: repeat(2, minmax(260px, 1fr));
    align-items: start;
  }
}

@media (max-width: 720px) {
  .workspace {
    grid-template-columns: 48px minmax(0, 1fr);
  }

  .inspector {
    display: flex;
  }

  .control-grid {
    grid-template-columns: 1fr;
  }

  .new-canvas,
  .top-actions {
    flex-wrap: wrap;
  }

  #statusText {
    display: block;
    margin-left: 0;
    margin-top: 2px;
  }
}
