:root {
  --bg: #0f1115;
  --bg-panel: #161a22;
  --bg-elevated: #1c2230;
  --bg-hover: #242b3a;
  --border: #2a3344;
  --border-strong: #3a465c;
  --text: #e6ebf2;
  --text-muted: #8b97ab;
  --accent: #4da3ff;
  --accent-dim: #2d6fb8;
  --danger: #e85d5d;
  --success: #4ecf8a;
  --gold: #d4a017;
  --handle: 6px;
  --radius: 6px;
  --font: "Segoe UI", "Helvetica Neue", sans-serif;
  --mono: "Cascadia Code", "Consolas", monospace;
  --split: 55%;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 13px;
}

#app {
  display: grid;
  grid-template-rows: 44px 1fr;
  height: 100vh;
  min-height: 0;
}

.app-header {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 12px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
  z-index: 20;
}

.brand {
  font-weight: 650;
  letter-spacing: 0.02em;
  white-space: nowrap;
}

.mode-bar {
  display: flex;
  gap: 4px;
  margin-left: 8px;
}

.mode-btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text-muted);
  border-radius: var(--radius);
  padding: 5px 12px;
  cursor: pointer;
  font: inherit;
  font-weight: 600;
}

.mode-btn:hover {
  border-color: var(--border-strong);
  color: var(--text);
}

.mode-btn.active {
  background: var(--accent-dim);
  border-color: var(--accent);
  color: #fff;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-left: auto;
  flex-wrap: wrap;
}

.header-sep {
  width: 1px;
  height: 20px;
  background: var(--border);
  margin: 0 4px;
}

.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text);
  border-radius: var(--radius);
  padding: 5px 10px;
  cursor: pointer;
  font: inherit;
  line-height: 1.2;
}

.btn:hover {
  background: var(--bg-hover);
  border-color: var(--border-strong);
}

.btn:active {
  transform: translateY(1px);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-sm {
  padding: 3px 8px;
  font-size: 12px;
}

.btn-block {
  width: 100%;
  margin-top: 6px;
}

.btn-ghost {
  background: transparent;
}

.btn-accent,
.export-menu button.accent {
  background: var(--accent-dim);
  border-color: var(--accent);
}

.btn-accent:hover,
.export-menu button.accent:hover {
  background: var(--accent);
  color: #081018;
}

.btn-danger {
  border-color: #7a3030;
  color: #ffb4b4;
}

.btn-danger:hover {
  background: #4a1f1f;
}

.workspace {
  display: grid;
  grid-template-columns: var(--split, 55%) 1fr;
  min-height: 0;
  overflow: hidden;
}

.side {
  position: relative;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background: var(--bg-panel);
}

.image-side {
  border-right: 1px solid var(--border);
  background: #0b0d11;
}

.font-side {
  background: var(--bg-panel);
}

.side-toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-panel);
  flex-wrap: wrap;
}

.side-title {
  font-weight: 650;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  margin-right: 4px;
}

.side-tabs {
  display: flex;
  gap: 4px;
  margin-right: 4px;
}

.side-tab {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text-muted);
  border-radius: var(--radius);
  padding: 4px 10px;
  cursor: pointer;
  font: inherit;
  font-weight: 650;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.side-tab:hover {
  border-color: var(--border-strong);
  color: var(--text);
}

.side-tab.active {
  background: var(--accent-dim);
  border-color: var(--accent);
  color: #fff;
}

.zoom-label {
  min-width: 44px;
  text-align: center;
  font-variant-numeric: tabular-nums;
  color: var(--text-muted);
}

.check {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--text-muted);
  cursor: pointer;
}

.check.tight {
  font-size: 11px;
  gap: 4px;
}

.badge {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 1px 7px;
  font-size: 10px;
  color: var(--text);
}

.atlas-viewport,
.preview-viewport {
  flex: 1;
  min-height: 0;
  overflow: hidden;
  position: relative;
  cursor: default;
}

.atlas-viewport.mode-slice {
  cursor: crosshair;
}

.atlas-viewport.mode-assign {
  cursor: grab;
}

.atlas-viewport.panning {
  cursor: grabbing;
}

.atlas-viewport.assigning {
  cursor: grabbing;
}

#atlas-canvas,
#preview-canvas {
  display: block;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

/* Bottom-left collapsible float menus */
.float-menus {
  position: absolute;
  left: 10px;
  bottom: 10px;
  z-index: 15;
  display: flex;
  flex-direction: row;
  align-items: flex-end;
  gap: 8px;
  pointer-events: none;
  max-height: calc(100% - 20px);
  max-width: calc(100% - 20px);
}

.float-menu {
  display: flex;
  flex-direction: column-reverse;
  width: auto;
  max-height: 100%;
  pointer-events: none;
}

.float-menu > * {
  pointer-events: auto;
}

.float-menu-toggle {
  align-self: flex-start;
  appearance: none;
  border: 1px solid var(--border-strong);
  background: var(--bg-elevated);
  color: var(--text);
  border-radius: var(--radius);
  padding: 8px 12px;
  cursor: pointer;
  font: inherit;
  font-weight: 650;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
}

.float-menu-toggle:hover {
  border-color: var(--accent);
}

.float-menu-body {
  margin-bottom: 8px;
  width: min(300px, calc(100vw - 40px));
  background: rgba(22, 26, 34, 0.96);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  padding: 10px;
  overflow: auto;
  max-height: min(420px, 60vh);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
}

.float-menu.collapsed .float-menu-body {
  display: none;
}

.font-side-body {
  flex: 1;
  min-height: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.font-scroll {
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.font-controls {
  border-bottom: 1px solid var(--border);
  padding-bottom: 10px;
  flex-shrink: 0;
}

.char-slots {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(64px, 1fr));
  gap: 8px;
  align-content: start;
  min-height: 120px;
  padding: 4px;
  border-radius: var(--radius);
  border: 1px dashed transparent;
  transition: border-color 0.15s, background 0.15s;
}

.char-slots.drop-target {
  border-color: var(--accent);
  background: rgba(77, 163, 255, 0.06);
}

.char-slot {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  min-height: 72px;
  padding: 4px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  cursor: pointer;
  user-select: none;
}

.char-slot:hover {
  border-color: var(--border-strong);
  background: var(--bg-hover);
}

.char-slot.filled {
  border-color: rgba(78, 207, 138, 0.5);
}

.char-slot.drag-over {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent);
  background: rgba(77, 163, 255, 0.12);
}

.char-slot.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent);
}

.char-slot .slot-char {
  font-family: var(--mono);
  font-size: 14px;
  font-weight: 650;
  line-height: 1.2;
}

.char-slot .slot-thumb {
  width: 100%;
  height: 36px;
  image-rendering: pixelated;
  background:
    linear-gradient(45deg, #222 25%, transparent 25%) 0 0 / 8px 8px,
    linear-gradient(-45deg, #222 25%, transparent 25%) 0 4px / 8px 8px,
    #111;
}

.char-slot .slot-empty {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  font-size: 10px;
  min-height: 36px;
}

.char-slot .slot-meta {
  font-size: 9px;
  color: var(--text-muted);
}

.drag-ghost {
  position: fixed;
  z-index: 200;
  pointer-events: none;
  border: 1px solid var(--accent);
  background: rgba(28, 34, 48, 0.9);
  padding: 4px;
  border-radius: 4px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
}

.drag-ghost canvas {
  display: block;
  image-rendering: pixelated;
}

.drag-ghost.hidden,
.hidden {
  display: none !important;
}

.section {
  margin-bottom: 12px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--border);
}

.section:last-child {
  border-bottom: none;
  margin-bottom: 0;
}

.section h2 {
  margin: 0 0 8px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  font-weight: 650;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 3px;
  margin-bottom: 8px;
  flex: 1;
  min-width: 0;
}

.field label {
  color: var(--text-muted);
  font-size: 11px;
}

.bg-color-field {
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: 8px;
}

.bg-color-field input[type="color"] {
  width: 42px;
  height: 28px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: transparent;
  cursor: pointer;
}

.field-row {
  display: flex;
  gap: 8px;
  align-items: center;
}

.btn-row {
  display: flex;
  gap: 6px;
  margin-top: 4px;
  flex-wrap: wrap;
}

input[type="number"],
input[type="text"],
textarea,
select {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius);
  padding: 5px 8px;
  font: inherit;
  outline: none;
}

input:focus,
textarea:focus,
select:focus {
  border-color: var(--accent);
}

textarea {
  resize: vertical;
  font-family: var(--mono);
}

.muted {
  color: var(--text-muted);
}

.small {
  font-size: 11px;
}

.drop-zone {
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius);
  padding: 12px 8px;
  text-align: center;
  background: var(--bg);
}

.drop-zone.dragover {
  border-color: var(--accent);
  background: rgba(77, 163, 255, 0.08);
}

.drop-zone p {
  margin: 0 0 8px;
  color: var(--text-muted);
}

.glyph-preview-wrap {
  display: flex;
  justify-content: center;
  margin-bottom: 8px;
  padding: 8px;
  background: var(--bg);
  border-radius: var(--radius);
  border: 1px solid var(--border);
}

#glyph-thumb {
  image-rendering: pixelated;
  max-width: 100%;
}

.glyph-inspector {
  flex-shrink: 0;
  border-top: 1px solid var(--border);
  background: var(--bg-panel);
  padding: 10px;
  max-height: 50%;
  overflow: auto;
}

.preview-tab-panel {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.preview-tab-panel.hidden {
  display: none;
}

.preview-toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.preview-toolbar input[type="text"] {
  flex: 1;
  min-width: 160px;
}

.preview-fallback-label {
  color: var(--text-muted);
  font-size: 11px;
  white-space: nowrap;
}

#preview-fallback {
  max-width: 200px;
}

.resize-handle {
  position: absolute;
  z-index: 5;
  background: transparent;
}

.image-side > .resize-e {
  top: 0;
  right: -3px;
  width: var(--handle);
  height: 100%;
  cursor: ew-resize;
}

.export-menu {
  position: fixed;
  top: 44px;
  right: 12px;
  z-index: 100;
  background: var(--bg-elevated);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  padding: 4px;
  min-width: 220px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
}

.export-menu button {
  display: block;
  width: 100%;
  text-align: left;
  background: transparent;
  border: none;
  color: var(--text);
  padding: 8px 10px;
  border-radius: 4px;
  cursor: pointer;
  font: inherit;
}

.export-menu button:hover {
  background: var(--bg-hover);
}

.export-menu .ext {
  color: var(--gold);
  font-weight: 600;
}

.export-menu hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 4px 0;
}

body.tool-slice #mode-hint::after {
  content: none;
}

@media (max-width: 900px) {
  .workspace {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr 1fr;
  }

  .image-side > .resize-e {
    display: none;
  }
}
