/* Dropdown styles */
.dropdown {
  position: relative;
  display: inline-block;
  vertical-align: middle;
  margin-right: 8px;
}
.dropdown-btn {
  background: var(--accent);
  color: #06131a;
  padding: 8px 14px;
  border-radius: 8px;
  font-weight: 600;
  font-size: 14px;
  border: none;
  cursor: pointer;
}
.dropdown-menu {
  display: none;
  position: absolute;
  right: 0;
  top: 100%;
  min-width: 180px;
  background: var(--panel);
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.12);
  z-index: 100;
  padding: 8px 0;
}
.dropdown.open .dropdown-menu {
  display: block;
}
.dropdown-menu button {
  width: 100%;
  background: none;
  color: var(--text);
  border: none;
  padding: 10px 18px;
  text-align: left;
  font-size: 15px;
  cursor: pointer;
  border-radius: 0;
}
.dropdown-menu button:hover {
  background: #232a36;
}
.dropdown-menu a {
  width: 100%;
  background: none;
  color: var(--text);
  border: none;
  padding: 10px 18px;
  text-align: left;
  font-size: 15px;
  cursor: pointer;
  border-radius: 0;
  display: block;
  text-decoration: none;
}
.dropdown-menu a:hover {
  background: #232a36;
  color: var(--text);
}
:root {
  color-scheme: light dark;
  --bg: #0b0f14;
  --panel: #121923;
  --text: #e6edf3;
  --accent: #4cc2ff;
  --muted: #95a2b3;
}

* {
  box-sizing: border-box;
}

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

.app {
  width: 100%;
  height: 100vh;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 0;
}

header h1 {
  margin: 0 0 8px;
  font-size: 28px;
}

header p {
  margin: 0 0 20px;
  color: var(--muted);
}

.layout {
  display: grid;
  grid-template-columns: 1.2fr 0.8fr;
  grid-template-rows: minmax(0, 1fr);
  gap: 20px;
  flex: 1;
  height: 100%;
  min-height: 0;
}

.editor {
  background: var(--panel);
  border-radius: 12px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
}

.toolbar {
  display: flex;
  gap: 10px;
  margin-bottom: 12px;
}

.toolbar-spacer {
  flex: 1;
}

.toolbar-link {
  background: var(--accent);
  color: #06131a;
  padding: 8px 14px;
  border-radius: 8px;
  font-weight: 600;
  font-size: 14px;
  line-height: 1;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
}

.toolbar-link:hover {
  filter: brightness(1.05);
}


button {
  background: var(--accent);
  border: none;
  color: #06131a;
  padding: 8px 14px;
  border-radius: 8px;
  font-weight: 600;
  cursor: pointer;
}

button#stopBtn {
  background: #ff8c4c;
}


.code-editor {
  position: relative;
  flex: 1;
  width: 100%;
  min-height: 0;
  overflow: hidden;
}

#lineNumbers,
#highlight,
#programInput {
  width: 100%;
  height: 100%;
  min-height: 420px;
  padding: 12px;
  font-family: "Cascadia Mono", "Consolas", monospace;
  font-size: 14px;
  line-height: 20px;
  border-radius: 10px;
  tab-size: 2;
  font-variant-ligatures: none;
  letter-spacing: 0;
}

#lineNumbers {
  position: absolute;
  inset: 0 auto 0 0;
  width: 48px;
  margin: 0;
  padding-right: 10px;
  text-align: right;
  color: #60708a;
  background: #0b1119;
  border: 1px solid #1f2a38;
  border-right: 0;
  border-radius: 10px 0 0 10px;
  white-space: pre;
  pointer-events: none;
  overflow: hidden;
}

#highlight {
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
  overflow-wrap: anywhere;
  background: #0d131b;
  border: 1px solid #1f2a38;
  color: #9fb4d1;
  padding-left: 60px;
  overflow: hidden;
  scrollbar-gutter: stable;
}

#programInput {
  position: absolute;
  inset: 0;
  background: transparent;
  color: transparent;
  caret-color: var(--text);
  border: 1px solid transparent;
  resize: none;
  padding-left: 60px;
  overflow-y: auto;
  overflow-x: hidden;
  scrollbar-gutter: stable;
}

.tok-keyword { color: #ffb454; }
.tok-func { color: #8be9fd; }
.tok-number { color: #f1fa8c; }
.tok-string { color: #50fa7b; }
.tok-comment { color: #7b8aa5; }
.tok-operator { color: #ff79c6; }

.panel {
  display: flex;
  flex-direction: column;
  gap: 20px;
  height: 100%;
  min-height: 0;
}

.panel-block {
  background: var(--panel);
  border-radius: 12px;
  padding: 16px;
}

.panel-block h2 {
  margin: 0 0 10px;
  font-size: 18px;
}

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

.screen-controls {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: var(--muted);
}

.screen-controls select {
  background: #0b1119;
  color: var(--text);
  border: 1px solid #1f2a38;
  border-radius: 6px;
  padding: 4px 8px;
}

.screen-controls button {
  padding: 6px 10px;
}

#screenHost {
  display: flex;
  justify-content: center;
  align-items: center;
}

#screen {
  width: 640px;
  height: 400px;
  border-radius: 10px;
  border: 1px solid #1f2a38;
  background: #0a1018;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

.modal {
  position: fixed;
  inset: 0;
  background: rgba(5, 10, 15, 0.7);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 1000;
}

.modal.open {
  display: flex;
}

.modal-content {
  background: var(--panel);
  border-radius: 12px;
  width: min(1200px, 96vw);
  max-height: 90vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  border: 1px solid #1f2a38;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0px 16px;
  border-bottom: 1px solid #1f2a38;
}

.modal-body {
  padding: 12px;
  overflow: auto;
  max-height: 100vh;
}

.modal-body .base64-toolbar {
  display: flex;
  gap: 10px;
  margin-bottom: 12px;
  margin: 12px 16px 12px 16px;
}
.modal-body .base64-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.modal-body .base64-entry {
  background: #181f2a;
  border-radius: 8px;
  padding: 18px 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.12);
}
.modal-body .base64-edit-row {
  display: flex;
  gap: 12px;
  align-items: center;
  margin-bottom: 8px;
}
.modal-body .base64-edit-name {
  flex: 1;
  font-size: 16px;
  padding: 10px 14px;
  border-radius: 6px;
  border: 1px solid #2a3444;
  background: #232a36;
  color: var(--text);
  font-weight: 500;
}
.modal-body .base64-edit-data {
  width: 100%;
  min-height: 120px;
  font-size: 15px;
  padding: 12px 14px;
  border-radius: 6px;
  border: 1px solid #2a3444;
  background: #232a36;
  color: var(--text);
  resize: vertical;
  font-family: "Cascadia Mono", "Consolas", monospace;
  margin-bottom: 8px;
  white-space: pre-wrap;
  word-break: break-all;
  overflow-wrap: anywhere;
}
.modal-body .base64-list textarea[readonly] {
  white-space: pre-wrap;
  word-break: break-all;
  overflow-wrap: anywhere;
}
.modal-body .base64-entry-actions {
  display: flex;
  gap: 8px;
}

.sprite-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  margin-bottom: 12px;
  padding: 0px 0px;
}

.sprite-toolbar label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--muted);
}

.sprite-toolbar .tool-btn {
  padding: 6px 10px;
  border-radius: 6px;
  border: 1px solid #1f2a38;
  background: #111827;
  color: var(--text);
  font-size: 12px;
}

.sprite-toolbar .tool-btn.active {
  background: #2563eb;
  border-color: #3b82f6;
  color: #fff;
}

.sprite-toolbar input[type="number"] {
  width: 70px;
  padding: 4px 6px;
  border-radius: 6px;
  border: 1px solid #1f2a38;
  background: #0b1119;
  color: var(--text);
}

.sprite-toolbar input[type="color"] {
  width: 36px;
  height: 28px;
  padding: 0;
  border: none;
  background: transparent;
}

.sprite-editor-area {
  display: grid;
  grid-template-columns: minmax(300px, 1fr) minmax(260px, 0.6fr);
  gap: 16px;
}

.sprite-canvas-wrap {
  position: relative;
  width: 100%;
  background: #0a1018;
  border: 1px solid #1f2a38;
  border-radius: 10px;
  overflow: hidden;
  display: grid;
  place-items: center;
  padding: 12px;
}

.sprite-canvas-stage {
  position: relative;
  display: inline-block;
  background-color: #1f2933;
  background-image:
    linear-gradient(45deg, rgba(255,255,255,0.08) 25%, transparent 25%),
    linear-gradient(-45deg, rgba(255,255,255,0.08) 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, rgba(255,255,255,0.08) 75%),
    linear-gradient(-45deg, transparent 75%, rgba(255,255,255,0.08) 75%);
  background-size: 16px 16px;
  background-position: 0 0, 0 8px, 8px -8px, -8px 0px;
}

#spriteEditorCanvas,
#spriteGridCanvas {
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  width: 100%;
  height: 100%;
  display: block;
}

#spriteGridCanvas {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.sprite-output {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.sprite-output textarea {
  min-height: 240px;
  resize: vertical;
  padding: 10px;
  border-radius: 8px;
  border: 1px solid #1f2a38;
  background: #0b1119;
  color: var(--text);
  font-family: "Cascadia Mono", "Consolas", monospace;
  font-size: 12px;
}

.sprite-output-actions {
  display: flex;
  gap: 8px;
}

@media (max-width: 980px) {
  .sprite-editor-area {
    grid-template-columns: 1fr;
  }
}

footer {
  margin-top: 20px;
  color: var(--muted);
  font-size: 12px;
}

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

@media (max-width: 768px) {
  body {
    height: 100dvh;
  }

  .app {
    padding: 8px;
    gap: 8px;
  }

  header h1 {
    font-size: 18px;
    margin: 0 0 2px;
  }

  header p {
    margin: 0;
    font-size: 11px;
  }

  .layout {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto;
    grid-auto-rows: auto;
    gap: 8px;
    height: auto;
    align-content: flex-start;
  }

  .editor {
    padding: 8px;
    flex: 0 0 auto;
    max-height: 45vh;
    height: auto;
  }

  .code-editor {
    min-height: 220px;
  }

  #lineNumbers,
  #highlight,
  #programInput {
    font-size: 11px;
    line-height: 16px;
    min-height: 220px;
    padding: 6px;
  }

  #lineNumbers {
    width: 32px;
    padding-right: 4px;
  }

  #highlight {
    padding-left: 42px;
  }

  #programInput {
    padding-left: 42px;
  }

  .toolbar {
    gap: 4px;
    margin-bottom: 6px;
    flex-wrap: wrap;
  }

  button,
  .toolbar-link {
    padding: 5px 8px;
    font-size: 11px;
  }

  .panel {
    gap: 4px;
    flex: 1 0 auto;
    min-height: 0;
    height: auto;
  }

  .panel-block {
    padding: 6px;
    flex: 1 0 auto;
    display: flex;
    flex-direction: column;
    min-height: 0;
  }

  .panel-block h2 {
    font-size: 14px;
    margin: 0 0 4px;
  }

  .panel-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 6px;
  }

  .screen-controls {
    width: 100%;
    display: flex;
    gap: 6px;
    align-items: center;
    flex-wrap: wrap;
  }

  .screen-controls label,
  .screen-controls select,
  .screen-controls button {
    font-size: 11px;
    padding: 4px 6px;
  }

  #screenHost {
    margin: 0 auto;
    flex: 1;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    min-height: 0;
    overflow: auto;
  }

  canvas {
    max-width: 100%;
    height: auto;
    display: block;
  }

  .modal-content {
    width: 95vw;
    max-height: 95vh;
  }

  .sprite-editor-area {
    grid-template-columns: 1fr;
  }

  .sprite-canvas-wrap {
    max-width: 100%;
  }

  .sprite-toolbar {
    flex-wrap: wrap;
  }

  .engine-attribution {
    display:none;
  }

}