/* ============================================================
   World Editor — Dark Theme Stylesheet
   ============================================================ */

/* --- CSS Custom Properties --- */
:root {
  /* Colors — dark family */
  --bg-primary: #1a1a2e;
  --bg-secondary: #16213e;
  --bg-tertiary: #0f3460;
  --bg-panel: #1e1e3a;
  --bg-panel-header: #252547;
  --bg-input: #12122a;
  --bg-hover: #2a2a4e;
  --bg-active: #3a3a6e;
  --bg-selected: #e94560;
  --bg-canvas: #0a0a1a;

  /* Text */
  --text-primary: #e0e0e0;
  --text-secondary: #a0a0b8;
  --text-muted: #6a6a8e;
  --text-accent: #e94560;
  --text-bright: #ffffff;

  /* Borders */
  --border-color: #2a2a4e;
  --border-focus: #e94560;
  --border-subtle: #1e1e3a;

  /* Accent */
  --accent: #e94560;
  --accent-hover: #ff6b81;
  --accent-muted: #a83050;
  --success: #2ecc71;
  --warning: #f39c12;
  --error: #e74c3c;
  --info: #3498db;

  /* Panel dimensions */
  --toolbar-height: 40px;
  --tool-options-height: 36px;
  --status-bar-height: 24px;
  --property-panel-height: 120px;
  --asset-palette-width: 240px;
  --layer-panel-width: 200px;
  --minimap-size: 200px;

  /* Spacing */
  --gap-xs: 2px;
  --gap-sm: 4px;
  --gap-md: 8px;
  --gap-lg: 12px;
  --gap-xl: 16px;

  /* Typography */
  --font-family: 'Segoe UI', -apple-system, BlinkMacSystemFont, 'Roboto', sans-serif;
  --font-mono: 'Cascadia Code', 'Fira Code', 'Consolas', monospace;
  --font-size-xs: 10px;
  --font-size-sm: 11px;
  --font-size-md: 13px;
  --font-size-lg: 15px;

  /* Borders & Radius */
  --radius-sm: 3px;
  --radius-md: 5px;
  --radius-lg: 8px;

  /* Transitions */
  --transition-fast: 120ms ease;
  --transition-normal: 200ms ease;

  /* Touch target (iPad compliance) */
  --touch-target-min: 44px;

  /* Z-index layers */
  --z-panel: 10;
  --z-overlay: 50;
  --z-modal: 100;
  --z-tooltip: 150;
}

/* --- Reset & Base --- */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  font-family: var(--font-family);
  font-size: var(--font-size-md);
  color: var(--text-primary);
  background: var(--bg-primary);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

/* --- Scrollbar Styling --- */
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

::-webkit-scrollbar-track {
  background: var(--bg-primary);
}

::-webkit-scrollbar-thumb {
  background: var(--bg-tertiary);
  border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--bg-active);
}

::-webkit-scrollbar-corner {
  background: var(--bg-primary);
}

/* Firefox scrollbar */
* {
  scrollbar-width: thin;
  scrollbar-color: var(--bg-tertiary) var(--bg-primary);
}

/* --- Utility Classes --- */
.hidden {
  display: none !important;
}

.separator {
  display: inline-block;
  width: 1px;
  height: 20px;
  background: var(--border-color);
  margin: 0 var(--gap-md);
  vertical-align: middle;
}

/* --- Button Styles --- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: var(--touch-target-min);
  min-height: 28px;
  padding: var(--gap-sm) var(--gap-md);
  background: var(--bg-secondary);
  color: var(--text-primary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  font-family: var(--font-family);
  font-size: var(--font-size-sm);
  cursor: pointer;
  transition: background var(--transition-fast), border-color var(--transition-fast);
  white-space: nowrap;
  line-height: 1;
}

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

.btn:active {
  background: var(--bg-active);
}

.btn.active {
  background: var(--accent-muted);
  border-color: var(--accent);
  color: var(--text-bright);
}

.btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--text-bright);
}

.btn-primary:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

.btn-sm {
  min-width: 28px;
  min-height: 24px;
  padding: var(--gap-xs) var(--gap-sm);
  font-size: var(--font-size-xs);
}

.btn-close {
  min-width: 24px;
  min-height: 24px;
  padding: 0;
  font-size: 16px;
  line-height: 1;
  background: transparent;
  border-color: transparent;
}

.btn-close:hover {
  background: var(--error);
  border-color: var(--error);
  color: var(--text-bright);
}

/* Touch: ensure 44x44 hit target on touch devices */
@media (pointer: coarse) {
  .btn {
    min-width: var(--touch-target-min);
    min-height: var(--touch-target-min);
    padding: var(--gap-md);
  }

  .btn-sm {
    min-width: 36px;
    min-height: 36px;
  }
}

/* --- Toolbar --- */
#toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: var(--toolbar-height);
  padding: 0 var(--gap-md);
  background: var(--bg-panel-header);
  border-bottom: 1px solid var(--border-color);
  z-index: var(--z-panel);
  flex-shrink: 0;
}

.toolbar-group {
  display: flex;
  align-items: center;
  gap: var(--gap-sm);
}

.toolbar-right {
  gap: var(--gap-lg);
}

.toolbar-info {
  font-family: var(--font-mono);
  font-size: var(--font-size-xs);
  color: var(--text-secondary);
  padding: var(--gap-xs) var(--gap-sm);
  background: var(--bg-input);
  border-radius: var(--radius-sm);
  min-width: 60px;
  text-align: center;
}

/* --- Tool Options Bar --- */
#tool-options {
  display: flex;
  align-items: center;
  height: var(--tool-options-height);
  padding: 0 var(--gap-md);
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border-color);
  gap: var(--gap-lg);
  flex-shrink: 0;
  overflow-x: auto;
}

.tool-option {
  display: flex;
  align-items: center;
  gap: var(--gap-sm);
  font-size: var(--font-size-sm);
  color: var(--text-secondary);
  cursor: pointer;
  white-space: nowrap;
}

.tool-option select,
.tool-option input[type="text"],
.tool-option input[type="number"] {
  background: var(--bg-input);
  color: var(--text-primary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  padding: var(--gap-xs) var(--gap-sm);
  font-size: var(--font-size-sm);
  font-family: var(--font-family);
  min-height: 24px;
}

.tool-option select:focus,
.tool-option input:focus {
  border-color: var(--border-focus);
  outline: none;
}

.tool-option input[type="checkbox"] {
  width: 16px;
  height: 16px;
  accent-color: var(--accent);
  cursor: pointer;
}

@media (pointer: coarse) {
  .tool-option select,
  .tool-option input {
    min-height: var(--touch-target-min);
    padding: var(--gap-md);
  }

  .tool-option input[type="checkbox"] {
    width: 22px;
    height: 22px;
  }
}

/* --- Editor Layout --- */
#editor-layout {
  display: flex;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

/* Full page layout */
body {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

/* --- Asset Palette (Left Panel) --- */
#asset-palette {
  width: var(--asset-palette-width);
  min-width: 0;
  background: var(--bg-panel);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  z-index: var(--z-panel);
  overflow: hidden;
  transition: width var(--transition-normal);
}

#asset-palette.collapsed {
  width: 32px;
}

#asset-palette.collapsed .panel-content {
  display: none;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--gap-sm) var(--gap-md);
  background: var(--bg-panel-header);
  border-bottom: 1px solid var(--border-color);
  min-height: 32px;
  flex-shrink: 0;
}

.panel-title {
  font-size: var(--font-size-sm);
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.panel-content {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: var(--gap-sm);
}

.panel-content-row {
  display: flex;
  flex-direction: row;
  gap: var(--gap-md);
  overflow-x: auto;
}

.panel-search {
  width: 100%;
  padding: var(--gap-sm) var(--gap-md);
  background: var(--bg-input);
  color: var(--text-primary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  font-size: var(--font-size-sm);
  font-family: var(--font-family);
  margin-bottom: var(--gap-sm);
}

.panel-search:focus {
  border-color: var(--border-focus);
  outline: none;
}

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

#asset-categories {
  display: flex;
  flex-wrap: wrap;
  gap: var(--gap-xs);
  margin-bottom: var(--gap-sm);
}

#asset-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(32px, 1fr));
  gap: var(--gap-xs);
}

#asset-grid .tile-thumb {
  width: 100%;
  aspect-ratio: 1;
  background: var(--bg-input);
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  cursor: pointer;
  image-rendering: pixelated;
  transition: border-color var(--transition-fast);
}

#asset-grid .tile-thumb:hover {
  border-color: var(--accent-hover);
}

#asset-grid .tile-thumb.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent);
}

/* --- Game Container (Center) --- */
#game-container {
  flex: 1;
  min-width: 0;
  min-height: 0;
  background: var(--bg-canvas);
  position: relative;
  overflow: hidden;
}

#game-container canvas {
  display: block;
  width: 100% !important;
  height: 100% !important;
}

/* --- Layer Panel (Right Panel) --- */
#layer-panel {
  width: var(--layer-panel-width);
  min-width: 0;
  background: var(--bg-panel);
  border-left: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  z-index: var(--z-panel);
  overflow: hidden;
  transition: width var(--transition-normal);
}

#layer-panel.collapsed {
  width: 32px;
}

#layer-panel.collapsed .panel-content {
  display: none;
}

#layer-list {
  display: flex;
  flex-direction: column;
  gap: var(--gap-xs);
}

.layer-item {
  display: flex;
  align-items: center;
  gap: var(--gap-sm);
  padding: var(--gap-sm) var(--gap-md);
  background: var(--bg-secondary);
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: var(--font-size-sm);
  transition: background var(--transition-fast), border-color var(--transition-fast);
  min-height: var(--touch-target-min);
}

.layer-item:hover {
  background: var(--bg-hover);
}

.layer-item.active {
  border-color: var(--accent);
  background: var(--bg-hover);
}

.layer-item .layer-visibility {
  width: 16px;
  height: 16px;
  cursor: pointer;
  opacity: 0.7;
}

.layer-item .layer-visibility:hover {
  opacity: 1;
}

.layer-item .layer-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.layer-item .layer-lock {
  width: 16px;
  height: 16px;
  opacity: 0.4;
  cursor: pointer;
}

.layer-item .layer-lock.locked {
  opacity: 0.9;
  color: var(--warning);
}

/* --- Property Panel (Bottom) --- */
#property-panel {
  height: var(--property-panel-height);
  background: var(--bg-panel);
  border-top: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow: hidden;
}

#property-fields {
  flex: 1;
  display: flex;
  flex-wrap: wrap;
  gap: var(--gap-md);
  align-items: flex-start;
  align-content: flex-start;
  padding: var(--gap-sm);
}

.property-placeholder {
  color: var(--text-muted);
  font-size: var(--font-size-sm);
  font-style: italic;
}

.property-row {
  display: flex;
  align-items: center;
  gap: var(--gap-sm);
  font-size: var(--font-size-sm);
}

.property-row label {
  color: var(--text-secondary);
  min-width: 60px;
}

.property-row input,
.property-row select {
  background: var(--bg-input);
  color: var(--text-primary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  padding: var(--gap-xs) var(--gap-sm);
  font-size: var(--font-size-sm);
  min-height: 24px;
}

.property-row input:focus,
.property-row select:focus {
  border-color: var(--border-focus);
  outline: none;
}

/* --- Status Bar --- */
#status-bar {
  display: flex;
  align-items: center;
  height: var(--status-bar-height);
  padding: 0 var(--gap-md);
  background: var(--bg-panel-header);
  border-top: 1px solid var(--border-color);
  font-size: var(--font-size-xs);
  color: var(--text-muted);
  gap: var(--gap-lg);
  flex-shrink: 0;
  z-index: var(--z-panel);
}

#status-bar span {
  white-space: nowrap;
}

#status-message {
  flex: 1;
}

/* --- Minimap Panel --- */
#minimap-panel {
  position: fixed;
  bottom: calc(var(--status-bar-height) + var(--property-panel-height) + var(--gap-md));
  right: calc(var(--layer-panel-width) + var(--gap-md));
  width: var(--minimap-size);
  background: var(--bg-panel);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  z-index: var(--z-overlay);
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
}

#minimap-canvas {
  display: block;
  width: 100%;
  height: auto;
  image-rendering: pixelated;
  background: var(--bg-canvas);
}

/* --- Generator Panel (Floating) --- */
#generator-panel {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 360px;
  max-width: 90vw;
  background: var(--bg-panel);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  z-index: var(--z-modal);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.6);
  overflow: hidden;
}

#generator-panel .panel-content {
  padding: var(--gap-lg);
}

.form-field {
  display: flex;
  align-items: center;
  gap: var(--gap-md);
  margin-bottom: var(--gap-md);
  font-size: var(--font-size-sm);
  color: var(--text-secondary);
}

.form-field span:first-child {
  min-width: 80px;
}

.form-field input[type="number"],
.form-field input[type="text"] {
  flex: 1;
  background: var(--bg-input);
  color: var(--text-primary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  padding: var(--gap-sm) var(--gap-md);
  font-size: var(--font-size-sm);
  font-family: var(--font-mono);
  min-height: 28px;
}

.form-field input[type="range"] {
  flex: 1;
  accent-color: var(--accent);
  cursor: pointer;
}

.form-field input:focus {
  border-color: var(--border-focus);
  outline: none;
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: var(--gap-md);
  margin-top: var(--gap-lg);
  padding-top: var(--gap-lg);
  border-top: 1px solid var(--border-color);
}

/* --- Prefab Panel (Floating) --- */
#prefab-panel {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 500px;
  max-width: 90vw;
  max-height: 80vh;
  background: var(--bg-panel);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  z-index: var(--z-modal);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.6);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

#prefab-panel .panel-content {
  flex: 1;
  overflow-y: auto;
  padding: var(--gap-lg);
}

#prefab-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
  gap: var(--gap-md);
}

.prefab-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: var(--gap-md);
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: border-color var(--transition-fast), background var(--transition-fast);
}

.prefab-card:hover {
  border-color: var(--accent-hover);
  background: var(--bg-hover);
}

.prefab-card img {
  width: 48px;
  height: 48px;
  image-rendering: pixelated;
  margin-bottom: var(--gap-sm);
}

.prefab-card span {
  font-size: var(--font-size-xs);
  color: var(--text-secondary);
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  width: 100%;
}

/* --- Modal Overlay (for generator/prefab background) --- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: calc(var(--z-modal) - 1);
}

/* --- Responsive: Tablets & Small Screens --- */
@media (max-width: 1024px) {
  :root {
    --asset-palette-width: 0px;
    --layer-panel-width: 0px;
  }

  #asset-palette {
    position: fixed;
    left: 0;
    top: var(--toolbar-height);
    bottom: var(--status-bar-height);
    width: 260px;
    transform: translateX(-100%);
    transition: transform var(--transition-normal);
    z-index: var(--z-overlay);
    box-shadow: 4px 0 16px rgba(0, 0, 0, 0.4);
  }

  #asset-palette.open {
    transform: translateX(0);
  }

  #layer-panel {
    position: fixed;
    right: 0;
    top: var(--toolbar-height);
    bottom: var(--status-bar-height);
    width: 220px;
    transform: translateX(100%);
    transition: transform var(--transition-normal);
    z-index: var(--z-overlay);
    box-shadow: -4px 0 16px rgba(0, 0, 0, 0.4);
  }

  #layer-panel.open {
    transform: translateX(0);
  }

  #property-panel {
    height: auto;
    max-height: 100px;
  }

  #minimap-panel {
    right: var(--gap-md);
    bottom: calc(var(--status-bar-height) + var(--gap-md));
    width: 140px;
  }

  #minimap-canvas {
    width: 140px;
    height: 140px;
  }
}

/* --- Responsive: Small phones --- */
@media (max-width: 600px) {
  #tool-options {
    gap: var(--gap-sm);
    padding: 0 var(--gap-sm);
  }

  .toolbar-info {
    display: none;
  }

  #minimap-panel {
    display: none;
  }

  #property-panel {
    display: none;
  }

  #generator-panel,
  #prefab-panel {
    width: 95vw;
  }
}

/* --- Animations --- */
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes slideUp {
  from { transform: translate(-50%, -45%); opacity: 0; }
  to { transform: translate(-50%, -50%); opacity: 1; }
}

#generator-panel:not(.hidden),
#prefab-panel:not(.hidden) {
  animation: slideUp 200ms ease;
}

/* --- Selection highlight --- */
::selection {
  background: var(--accent);
  color: var(--text-bright);
}

/* --- Focus visible for keyboard navigation --- */
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* --- Tooltip (generic) --- */
[title] {
  position: relative;
}

/* --- Drag handle cursor --- */
.draggable {
  cursor: grab;
}

.draggable:active {
  cursor: grabbing;
}

/* --- Canvas cursor overrides --- */
#game-container.tool-paint { cursor: crosshair; }
#game-container.tool-erase { cursor: crosshair; }
#game-container.tool-fill { cursor: crosshair; }
#game-container.tool-rect { cursor: crosshair; }
#game-container.tool-select { cursor: default; }
#game-container.tool-pick { cursor: copy; }
#game-container.tool-pan { cursor: grab; }
#game-container.tool-pan:active { cursor: grabbing; }
