:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-2: #f8faff;
  --border: #dbe2ef;
  --text: #111827;
  --muted: #6b7280;
  --primary: #2b6fff;
  --primary-soft: #e9f0ff;
  --danger: #ef4444;
  --shadow: 0 8px 24px rgba(16, 24, 40, 0.08);
  --radius: 12px;
  --panel-w: 280px;
  --panel-right-w: 280px;
}

body.theme-dark {
  --bg: #0f1118;
  --surface: #151925;
  --surface-2: #1a2130;
  --border: #293247;
  --text: #e8ecf8;
  --muted: #9ca9c3;
  --primary: #69a5ff;
  --primary-soft: #223558;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  width: 100%;
  height: 100%;
  font-family: Inter, "Segoe UI", Roboto, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.app-shell {
  height: 100vh;
  display: grid;
  gap: 8px;
  grid-template-areas:
    "topbar topbar topbar"
    "left canvas right"
    "bottombar bottombar bottombar";
  grid-template-columns: var(--panel-w) minmax(0, 1fr) var(--panel-right-w);
  grid-template-rows: 56px minmax(0, 1fr) 64px;
  padding: 8px;
}

@media (min-width: 961px) {
  .app-shell.left-collapsed {
    grid-template-columns: 0 minmax(0, 1fr) var(--panel-right-w);
  }
  .app-shell.right-collapsed {
    grid-template-columns: var(--panel-w) minmax(0, 1fr) 0;
  }
  .app-shell.left-collapsed.right-collapsed {
    grid-template-areas:
      "topbar"
      "canvas"
      "bottombar";
    grid-template-columns: minmax(0, 1fr);
  }
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.topbar {
  grid-area: topbar;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
}

.left-panel { grid-area: left; }
.canvas-panel { grid-area: canvas; }
.right-panel { grid-area: right; }
.bottombar { grid-area: bottombar; }

.brand {
  font-weight: 700;
  letter-spacing: 0.2px;
}

.toolbar,
.toolbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

.icon-toolbar {
  gap: 6px;
}

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

.icon-btn {
  width: 38px;
  height: 38px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  cursor: pointer;
  transition: all 160ms ease;
}

.lang-btn {
  width: 52px;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.4px;
}

.rtl-ui {
  direction: rtl;
}

.icon-btn svg {
  width: 20px;
  height: 20px;
  display: block;
}

.icon-btn:hover {
  transform: translateY(-1px);
  border-color: var(--primary);
  background: rgba(255, 255, 255, 0.12);
}

body.theme-dark .icon-btn:hover {
  background: rgba(255, 255, 255, 0.08);
}

.icon-btn:active {
  transform: translateY(0);
}

.icon-btn:focus-visible {
  outline: 3px solid var(--primary-soft);
  outline-offset: 2px;
}

.dropdown {
  position: relative;
  display: inline-flex;
}

.dropdown-menu {
  position: absolute;
  top: calc(100% + 8px);
  left: 0;
  min-width: 200px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 6px;
  display: none;
  z-index: 50;
}

.dropdown[data-open="true"] .dropdown-menu {
  display: block;
}

.menu-item {
  width: 100%;
  text-align: left;
  border: 0;
  background: transparent;
  color: var(--text);
  padding: 9px 10px;
  border-radius: 10px;
  cursor: pointer;
  font-size: 13px;
  transition: background 140ms ease, color 140ms ease;
}

.menu-item:hover {
  background: var(--primary-soft);
}

.menu-danger {
  color: var(--danger);
}

.menu-danger:hover {
  background: rgba(239, 68, 68, 0.12);
}

.menu-sep {
  height: 1px;
  background: var(--border);
  margin: 6px 6px;
  border-radius: 999px;
}

.left-panel,
.right-panel {
  padding: 12px;
  overflow: auto;
}

.left-panel h3,
.right-panel h3 {
  margin: 4px 0 12px;
  font-size: 14px;
}

.left-section {
  margin-bottom: 16px;
}

.left-section-title {
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.btn-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.btn-grid.components {
  grid-template-columns: repeat(auto-fill, 52px);
  justify-content: start;
  align-items: start;
}

.component-btn {
  width: 52px;
  height: 52px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.2s ease;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  padding: 0;
}

.component-btn:hover {
  background: rgba(255, 255, 255, 0.10);
  transform: scale(1.05);
  border-color: var(--primary);
}

body.theme-dark .component-btn:hover {
  background: rgba(255, 255, 255, 0.08);
}

.component-btn:active {
  transform: scale(1.0);
}

.component-btn.is-selected {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px var(--primary-soft);
  background: rgba(96, 165, 250, 0.14);
}

.component-btn:focus-visible {
  outline: 3px solid var(--primary-soft);
  outline-offset: 2px;
}

.component-icon {
  width: 28px;
  height: 28px;
}

.component-btn .component-icon {
  display: block;
}

.component-img {
  width: 36px;
  height: 36px;
  display: block;
  margin: 0 auto;
  object-fit: contain;
  object-position: center;
  image-rendering: auto;
}

/* Legacy-generated component icons are raster images (black strokes).
   In dark mode we invert them so they remain visible. */
body.theme-dark .component-btn .component-img {
  filter: invert(1) hue-rotate(180deg) brightness(1.05) contrast(1.05);
}

.component-icon-skel {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  background: linear-gradient(90deg, rgba(255,255,255,0.08), rgba(255,255,255,0.18), rgba(255,255,255,0.08));
  background-size: 200% 100%;
  animation: skel 1.2s ease-in-out infinite;
}

@keyframes skel {
  0% { background-position: 0% 0; }
  100% { background-position: 200% 0; }
}

.canvas-panel {
  position: relative;
  overflow: hidden;
  background: #000 !important;
}

#sim-frame {
  width: 100%;
  height: 100%;
  border: 0;
  background: #000 !important;
}

.floating-tools {
  position: absolute;
  top: 12px;
  left: 12px;
  display: flex;
  gap: 8px;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(6px);
}

body.theme-dark .floating-tools {
  background: rgba(21, 25, 37, 0.85);
}

.bottombar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px;
}

.status {
  color: var(--muted);
  font-size: 13px;
}

.input {
  width: 100%;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--text);
  margin-bottom: 8px;
}

.btn {
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 13px;
  cursor: pointer;
  transition: all 160ms ease;
}

.btn:hover {
  transform: translateY(-1px);
  border-color: var(--primary);
}

.btn:active {
  transform: translateY(0);
}

.btn-primary {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

.btn-danger {
  background: var(--danger);
  border-color: var(--danger);
  color: #fff;
}

.actions {
  display: grid;
  gap: 8px;
}

.tree {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  padding: 8px;
  max-height: 260px;
  overflow: auto;
}

.tree details {
  margin: 3px 0;
}

.tree summary {
  cursor: pointer;
  color: var(--muted);
  font-size: 12px;
}

.tree .tree-item {
  margin: 4px 0 4px 14px;
}

.tree .tree-item .btn {
  width: 100%;
  text-align: left;
  font-size: 12px;
  padding: 6px 8px;
}

.setting {
  margin-bottom: 12px;
}

.setting-title {
  color: var(--muted);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin: 4px 0 8px;
}

.setting-value {
  font-size: 12px;
  color: var(--muted);
  margin-top: 6px;
}

.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  margin-bottom: 8px;
}

.toggle-row label {
  font-size: 13px;
}

.toggle-row input[type="checkbox"] {
  width: 18px;
  height: 18px;
}

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

.row {
  display: flex;
  gap: 8px;
}

.collapse-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 4px 0 10px;
}

.hidden {
  display: none !important;
}

@media (max-width: 1200px) {
  .app-shell {
    grid-template-columns: 240px minmax(0, 1fr) 240px;
  }
}

@media (max-width: 960px) {
  .app-shell {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: 56px minmax(0, 1fr) auto auto 64px;
    grid-template-areas:
      "topbar"
      "canvas"
      "left"
      "right"
      "bottombar";
  }
  .left-panel,
  .right-panel {
    max-height: 220px;
  }
  .canvas-panel {
    min-height: 50vh;
  }
}
