* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  min-height: 100vh;
  height: 100%;
}

body {
  font-family: Inter, system-ui, sans-serif;
  background: #0b1220;
  color: #e2e8f0;
  overflow: hidden;
}

button,
input,
select {
  font: inherit;
}

#app-shell {
  display: flex;
  flex-direction: column;
  height: 100vh;
  width: 100vw;
  overflow: hidden;
}

#top-bar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 18px;
  background: rgba(15, 23, 42, 0.96);
  border-bottom: 1px solid rgba(148, 163, 184, 0.08);
  backdrop-filter: blur(10px);
}

.top-left,
.top-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.dropdown {
  background: #111827;
  color: #f8fafc;
  border: 1px solid rgba(148, 163, 184, 0.15);
  border-radius: 12px;
  padding: 10px 12px;
}

.tool-button,
.action-button,
.wide-button,
.secondary-button,
.close-button,
.export-pill {
  border: none;
  border-radius: 12px;
  cursor: pointer;
  transition: transform 0.12s ease, background 0.12s ease;
}

.tool-button,
.secondary-button,
.export-pill {
  background: #1f2937;
  color: #f8fafc;
}

button:focus-visible,
input:focus-visible,
select:focus-visible {
  outline: 2px solid #38bdf8;
  outline-offset: 2px;
}

.tool-button.selected,
.tool-button:hover,
.secondary-button:hover,
.export-pill:hover,
.action-button:hover,
.wide-button:hover {
  background: #2563eb;
}

.action-button,
.wide-button {
  padding: 12px 16px;
  background: #2563eb;
  color: #fff;
}

.export-pill {
  padding: 10px 16px;
}

.close-button {
  background: transparent;
  color: #e2e8f0;
  font-size: 1.4rem;
  line-height: 1;
  padding: 0 8px;
}

#workspace {
  display: flex;
  flex: 1;
  height: calc(100vh - 60px);
  overflow: hidden;
}

#left-sidebar {
  width: 280px;
  min-width: 280px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 18px;
  background: #111827;
  border-right: 1px solid rgba(148, 163, 184, 0.08);
  overflow: hidden;
}

#right-sidebar {
  width: 320px;
  min-width: 320px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 18px;
  background: #111827;
  border-left: 1px solid rgba(148, 163, 184, 0.08);
  overflow: hidden;
}

.sidebar-panel.collapsed {
  min-height: 0;
}

.sprite-sidebar .panel-header {
  font-size: 1rem;
}

.sprite-preview {
  min-height: 160px;
  background: #0b1220;
  border: 1px solid rgba(148, 163, 184, 0.12);
  border-radius: 18px;
  padding: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.sprite-preview canvas {
  width: 100%;
  height: auto;
  display: block;
}

.sprite-library-thumbnails {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
  max-height: calc(100vh - 360px);
  overflow-y: auto;
  padding-right: 4px;
}

.sidebar-panel {
  display: flex;
  flex-direction: column;
  gap: 12px;
  background: #0f1720;
  padding: 16px;
  border-radius: 20px;
  border: 1px solid rgba(148, 163, 184, 0.08);
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  font-weight: 600;
  color: #f8fafc;
}

.panel-title {
  font-size: 1rem;
}

.panel-subtitle {
  font-size: 0.83rem;
  color: #94a3b8;
  margin-top: 2px;
}

.panel-body {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.panel-section {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.panel-settings-section {
  border-top: 1px solid rgba(148, 163, 184, 0.08);
  padding-top: 12px;
}

.section-toggle,
.panel-toggle {
  appearance: none;
  border: 1px solid rgba(148, 163, 184, 0.18);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.04);
  color: #f8fafc;
  padding: 8px 12px;
  cursor: pointer;
  font: inherit;
}

.section-toggle:hover,
.panel-toggle:hover {
  background: rgba(37, 99, 235, 0.16);
}

.section-content.hidden,
.panel-body.hidden {
  display: none;
}

.panel-header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.sidebar-panel.collapsed {
  padding-bottom: 12px;
}

.sidebar-panel.collapsed .panel-body {
  display: none;
}

.sidebar-panel.collapsed .panel-toggle {
  transform: rotate(0deg);
}

.sidebar-panel .panel-toggle[aria-expanded="true"] {
  transform: rotate(90deg);
}

.sprite-library-count {
  font-size: 0.85rem;
  color: #94a3b8;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(148, 163, 184, 0.15);
  border-radius: 999px;
  padding: 4px 10px;
}

.logo {
  font-size: 1rem;
  letter-spacing: 0.04em;
}

.tileset-preview-wrapper {
  position: relative;
  width: 100%;
  aspect-ratio: 4 / 3;
  min-height: 380px;
  background: #0b1220;
  border-radius: 18px;
  border: 1px solid rgba(148, 163, 184, 0.08);
  overflow: hidden;
  transition: height 0.2s ease, aspect-ratio 0.2s ease;
}

.tileset-preview-wrapper.expanded {
  aspect-ratio: auto;
  min-height: 520px;
}

.tileset-preview-wrapper.locked {
  border-color: #38bdf8;
}

.preview-overlay-actions {
  position: absolute;
  top: 12px;
  right: 12px;
  display: flex;
  gap: 8px;
  z-index: 10;
}

.tileset-preview-wrapper .tileset-overlay {
  z-index: 1;
}

.icon-button {
  min-width: 40px;
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  border-radius: 12px;
  border: 1px solid rgba(148, 163, 184, 0.18);
  background: rgba(15, 23, 42, 0.96);
  color: #f8fafc;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  transition: transform 0.12s ease, background 0.12s ease;
}

.icon-button:hover,
.icon-button:focus-visible {
  background: rgba(37, 99, 235, 0.22);
}

.icon-button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

#tileset-preview {
  width: 100%;
  height: 100%;
  display: block;
}

#calibration-panel {
  display: flex;
  flex-direction: column;
  gap: 14px;
  width: 100%;
  margin-bottom: 18px;
}

#calibration-container {
  position: relative;
  width: 100%;
  aspect-ratio: 4 / 3;
  background: #111827;
  border-radius: 18px;
  overflow: hidden;
}

#calibration-container img,
#calibration-container canvas,
#calibration-container svg {
  position: absolute;
  display: block;
  left: 0;
  top: 0;
}

#calibration-image {
  object-fit: contain;
  transform-origin: 0 0;
}

#calibration-grid {
  z-index: 1;
}

#calibration-overlay {
  z-index: 2;
  pointer-events: none;
}

.calibration-pin {
  fill: #38bdf8;
  stroke: #fff;
  stroke-width: 2px;
  cursor: grab;
}

.calibration-pin:active {
  cursor: grabbing;
}

.calibration-coordinates {
  padding: 10px 12px;
  border-radius: 14px;
  background: rgba(15, 23, 42, 0.96);
  border: 1px solid rgba(148, 163, 184, 0.12);
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.95rem;
  line-height: 1.5;
  color: #e2e8f0;
}

.tileset-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
  display: flex;
  align-items: flex-end;
  justify-content: flex-start;
  padding: 12px;
  color: rgba(229, 231, 235, 0.9);
  font-size: 0.85rem;
  background: linear-gradient(180deg, transparent 50%, rgba(15, 23, 42, 0.7));
}

.tileset-action-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.sprite-library-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 12px;
}

.tileset-settings {
  display: grid;
  gap: 10px;
  padding: 8px 0;
}

.tileset-settings-row {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.tileset-settings-label {
  display: block;
  color: #cbd5e1;
  font-size: 0.88rem;
  font-weight: 600;
}

.tileset-settings-controls {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.tileset-settings input {
  width: 100%;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid rgba(148, 163, 184, 0.18);
  background: #111827;
  color: #f8fafc;
}

.tileset-settings input:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.tileset-thumbnail-header {
  color: #e2e8f0;
  font-size: 0.95rem;
  font-weight: 600;
  padding: 6px 0 0 2px;
}

.tileset-thumbnails {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
  margin-top: 12px;
  max-height: 160px;
  overflow-y: auto;
  padding-right: 4px;
}

.thumbnail-item {
  width: 100%;
  padding: 4px;
  border-radius: 12px;
  border: 1px solid rgba(148, 163, 184, 0.18);
  background: #0b1220;
  cursor: pointer;
  transition: border-color 0.12s ease, transform 0.12s ease;
}

.thumbnail-item.selected {
  border-color: #38bdf8;
  transform: translateY(-1px);
}

.thumbnail-item canvas {
  width: 100%;
  height: auto;
  display: block;
  border-radius: 8px;
}

.wide-button,
.secondary-button {
  width: 100%;
}

.status-text,
.editor-status,
.palette-status-text {
  color: #cbd5e1;
  font-size: 0.92rem;
}

.layers-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow-y: auto;
  max-height: 420px;
  padding-right: 4px;
}

.layer-row {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) repeat(4, 40px);
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-radius: 14px;
  background: #111827;
  border: 1px solid rgba(148, 163, 184, 0.08);
}

.layer-row.active {
  border-color: #38bdf8;
  box-shadow: 0 0 0 2px rgba(56, 189, 248, 0.12);
}

.layer-row input {
  width: 100%;
  min-width: 0;
  background: transparent;
  border: none;
  color: #e2e8f0;
}

.layer-badge {
  padding: 4px 8px;
  border-radius: 999px;
  font-size: 0.75rem;
  background: rgba(56, 189, 248, 0.14);
  color: #dbeafe;
  white-space: nowrap;
}

.layer-button {
  min-width: 40px;
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  background: #1f2937;
  border-radius: 12px;
  color: #f8fafc;
}

.layer-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

#main-view {
  flex: 1;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  overflow: hidden;
}

.viewport-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  background: #111827;
  border-radius: 18px;
  border: 1px solid rgba(148, 163, 184, 0.08);
}

#editor-container {
  position: relative;
  flex: 1;
  min-height: 0;
  background: #0b1220;
  border-radius: 24px;
  overflow: hidden;
  border: 1px solid rgba(148, 163, 184, 0.08);
}

#tileset-expanded-slot {
  position: absolute;
  inset: 0;
  z-index: 15;
  pointer-events: none;
}

#tileset-expanded-slot.hidden {
  display: none;
}

.hidden {
  display: none !important;
}

#tileset-expanded-slot .tileset-preview-wrapper {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  border-radius: 0;
  pointer-events: auto;
}

.tileset-preview-wrapper.expanded {
  width: 100%;
  height: 100%;
  min-height: 0;
}

#editor-canvas,
#editor-overlay {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

#editor-overlay {
  cursor: crosshair;
}

.export-modal {
  position: fixed;
  inset: 0;
  background: rgba(11, 18, 32, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.export-modal.hidden {
  display: none;
}

.export-card {
  width: min(640px, 100%);
  background: #111827;
  border: 1px solid rgba(148, 163, 184, 0.12);
  border-radius: 24px;
  padding: 24px;
  box-shadow: 0 40px 120px rgba(0, 0, 0, 0.24);
}

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

.export-section {
  margin-bottom: 18px;
}

.export-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 12px;
}

@media (max-width: 1024px) {
  #workspace {
    flex-direction: column;
  }

  #left-sidebar {
    width: 100%;
    min-width: auto;
    max-height: 520px;
    overflow: auto;
  }
}
