* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

:root {
  /* Apple-inspired color palette */
  --bg: #f5f5f7;
  --panel: rgba(255, 255, 255, 0.72);
  --panel-solid: #ffffff;
  --accent: #007aff;
  --accent-hover: #0051d5;
  --tile: rgba(120, 120, 128, 0.12);
  --text: #1d1d1f;
  --text-secondary: #6e6e73;
  --grid-line: rgba(60, 60, 67, 0.15);
  --border: rgba(120, 120, 128, 0.24);
  --button-bg: rgba(255, 255, 255, 0.64);
  --button-hover: rgba(120, 120, 128, 0.16);
  --zoom-controls-bg: rgba(255, 255, 255, 0.72);
  --header-bg: rgba(255, 255, 255, 0.72);
  --status-bg: rgba(255, 255, 255, 0.72);
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.08);
  --card-shadow: 0 2px 12px rgba(0, 0, 0, 0.06);
  --canvas-bg: #ffffff;
  --danger: #ff3b30;
  --success: #34c759;
  --warning: #ff9500;
}

body.theme-dark {
  --bg: #000000;
  --panel: rgba(28, 28, 30, 0.72);
  --panel-solid: #1c1c1e;
  --accent: #0a84ff;
  --accent-hover: #409cff;
  --tile: rgba(120, 120, 128, 0.24);
  --text: #f5f5f7;
  --text-secondary: #8e8e93;
  --grid-line: rgba(255, 255, 255, 0.38);
  --border: rgba(120, 120, 128, 0.32);
  --button-bg: rgba(44, 44, 46, 0.72);
  --button-hover: rgba(72, 72, 74, 0.8);
  --zoom-controls-bg: rgba(28, 28, 30, 0.8);
  --header-bg: rgba(28, 28, 30, 0.72);
  --status-bg: rgba(28, 28, 30, 0.72);
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.36);
  --card-shadow: 0 2px 12px rgba(0, 0, 0, 0.24);
  --canvas-bg: #1c1c1e;
  --danger: #ff453a;
  --success: #30d158;
  --warning: #ff9f0a;
}

:root {
  /* Real rendered toolbar height, measured at runtime by updateMobileToolbarHeight()
     (0 on desktop where the toolbar is hidden). Prevents device-specific overlap. */
  --mobile-toolbar-real-h: 0px;
  /* Gap between the canvas and the bottom toolbar, matching the side padding */
  --mobile-bottom-gap: 12px;
}

html {
  scroll-behavior: smooth;
}

body {
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  /* Dynamic viewport height on mobile: keeps layout stable when the browser chrome collapses */
  height: 100dvh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  transition: background 0.3s ease, color 0.3s ease;
  image-rendering: pixelated;
}

/* Header */
.header {
  background: var(--header-bg);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  padding: 10px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid var(--border);
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.04);
  z-index: 50;
}

.logo {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo h1 {
  font-size: 1.25rem;
  color: var(--text);
  font-weight: 700;
  letter-spacing: -0.02em;
}

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

.btn {
  background: var(--button-bg);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 8px 14px;
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.2s cubic-bezier(0.25, 0.1, 0.25, 1);
  font-weight: 500;
  font-size: 0.9rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  backdrop-filter: saturate(180%) blur(10px);
  -webkit-backdrop-filter: saturate(180%) blur(10px);
}

.btn:hover {
  background: var(--button-hover);
  transform: translateY(-1px);
}

.btn:active {
  transform: scale(0.96);
}

.btn i {
  font-size: 0.95rem;
  width: 16px;
  text-align: center;
}

/* Main layout */
.main-container {
  display: flex;
  flex: 1;
  overflow: hidden;
  padding: 16px;
  gap: 16px;
}

.bottom-container {
  display: flex;
  flex-direction: column;
}

/* Components panel */
#components-panel {
  width: 320px;
  background: var(--panel);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  padding: 16px;
  overflow-y: auto;
  border-radius: 20px;
  border: 1px solid var(--border);
  box-shadow: var(--card-shadow);
  display: flex;
  flex-direction: column;
}

/* Hide the panel's scrollbar while keeping scrolling (WebKit / Firefox / legacy Edge) */
#components-panel,
#components-list {
  scrollbar-width: none;
  -ms-overflow-style: none;
}

#components-panel::-webkit-scrollbar,
#components-list::-webkit-scrollbar {
  display: none;
  width: 0;
  height: 0;
}

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border);
}

.panel-header h2 {
  font-size: 1.15rem;
  color: var(--text);
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 8px;
}

.panel-header h2 i {
  color: var(--accent);
  font-size: 1rem;
}

.search-container {
  position: relative;
  margin-bottom: 16px;
}

#component-search {
  width: 100%;
  padding: 10px 14px 10px 38px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--tile);
  color: var(--text);
  font-size: 0.95rem;
  transition: all 0.2s ease;
}

#component-search:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(0, 122, 255, 0.18);
  background: var(--panel-solid);
}

.search-icon {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-secondary);
  font-size: 0.85rem;
}

.category {
  margin-bottom: 20px;
}

.category h3 {
  color: var(--text-secondary);
  margin-bottom: 12px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--border);
  font-size: 0.9rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  display: flex;
  align-items: center;
  gap: 6px;
}

.category h3 i {
  font-size: 0.85rem;
}

.components-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 6px;
}

.component {
  aspect-ratio: 1;
  min-height: 40px;
  border-radius: 10px;
  background: var(--panel-solid);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.15s ease;
  position: relative;
  border: 1px solid var(--border);
  overflow: hidden;
  box-shadow: var(--card-shadow);
}

.component:hover {
  transform: scale(1.05);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  border-color: var(--accent);
}

.component:active {
  transform: scale(0.92);
}

.component.selected {
  border: 2px solid var(--accent);
  box-shadow: 0 0 0 3px rgba(0, 122, 255, 0.25);
  background: rgba(0, 122, 255, 0.08);
}

.component img {
  width: 28px;
  height: 28px;
  object-fit: contain;
  image-rendering: pixelated;
}

/* Canvas area */
#canvas-container {
  flex: 1;
  position: relative;
  overflow: hidden;
  background: var(--canvas-bg);
  border-radius: 20px;
  border: 1px solid var(--border);
  box-shadow: var(--card-shadow);
}

#canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  cursor: default;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  touch-action: none;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

/* Status bar */
.status-bar {
  background: var(--status-bg);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  padding: 8px 20px;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  border-top: 1px solid var(--border);
  font-size: 0.8rem;
  color: var(--text-secondary);
}

.status-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: 8px;
  background: var(--tile);
  border: 1px solid var(--border);
  white-space: nowrap;
}

.status-item i {
  color: var(--accent);
  font-size: 0.75rem;
}

/* Zoom controls */
.zoom-controls {
  display: flex;
  gap: 8px;
  background: var(--zoom-controls-bg);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  padding: 8px 12px;
  border-radius: 16px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  position: absolute;
  bottom: 20px;
  right: 20px;
  z-index: 10;
}

.zoom-btn {
  background: var(--button-bg);
  color: var(--text);
  border: 1px solid var(--border);
  width: 32px;
  height: 32px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s ease;
  font-size: 0.85rem;
}

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

.zoom-btn:active {
  transform: scale(0.92);
}

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

::-webkit-scrollbar-track {
  background: transparent;
}

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

::-webkit-scrollbar-thumb:hover {
  background: var(--text-secondary);
}

/* Modals */
.modal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  z-index: 1000;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.modal.show {
  opacity: 1;
}

.modal-content {
  background: var(--panel-solid);
  padding: 24px;
  border-radius: 20px;
  width: 460px;
  max-width: 90%;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.25);
  position: relative;
  transform: scale(0.92) translateY(20px);
  opacity: 0;
  transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  display: flex;
  flex-direction: column;
  max-height: 90vh;
  border: 1px solid var(--border);
}

.modal.show .modal-content {
  transform: scale(1) translateY(0);
  opacity: 1;
}

.modal-header {
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--border);
}

.modal-header h2 {
  color: var(--text);
  font-size: 1.35rem;
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
}

.modal-header h2 i {
  font-size: 1.15rem;
  color: var(--accent);
}

.modal-body {
  margin-bottom: 20px;
  flex: 1;
  overflow-y: auto;
}

.modal-footer {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}

.modal-footer .modal-btn {
  flex: 1;
  min-height: 44px;
  /* Override interference from the .close-modal helper class used on cancel buttons */
  width: auto;
  height: auto;
  border-radius: 12px;
  font-size: 0.95rem;
}

.close-modal {
  background: var(--tile);
  border: none;
  color: var(--text-secondary);
  font-size: 1.4rem;
  cursor: pointer;
  transition: all 0.2s ease;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
}

.close-modal:hover {
  color: var(--text);
  background: var(--button-hover);
}

.input-group {
  margin-bottom: 18px;
}

.input-group label {
  display: block;
  margin-bottom: 8px;
  font-weight: 500;
  color: var(--text);
  font-size: 0.95rem;
}

.input-group input {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--tile);
  color: var(--text);
  font-size: 1rem;
  transition: all 0.2s ease;
}

.input-group input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(0, 122, 255, 0.18);
  background: var(--panel-solid);
}

.modal-btn {
  padding: 12px 24px;
  border-radius: 12px;
  cursor: pointer;
  /* Transparent border on the primary button so both buttons share identical box sizing */
  border: 1px solid transparent;
  font-weight: 600;
  transition: all 0.2s ease;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 0.95rem;
  white-space: nowrap;
}

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

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

.secondary-btn {
  background: var(--button-bg);
  color: var(--text);
  border: 1px solid var(--border);
}

.secondary-btn:hover {
  background: var(--button-hover);
}

.modal-btn:active {
  transform: scale(0.96);
}

/* Loader */
.loader {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  z-index: 2000;
  transition: opacity 0.4s ease;
}

.loader-spinner {
  border: 5px solid var(--tile);
  border-top: 5px solid var(--accent);
  border-radius: 50%;
  width: 52px;
  height: 52px;
  animation: spin 1s linear infinite;
  margin-bottom: 20px;
}

.loader-text {
  font-size: 1.05rem;
  color: var(--text);
  text-align: center;
  max-width: 80%;
  font-weight: 500;
}

.loader-progress {
  width: 260px;
  height: 6px;
  background: var(--tile);
  border-radius: 3px;
  margin-top: 20px;
  overflow: hidden;
}

.loader-progress-bar {
  height: 100%;
  background: var(--accent);
  width: 0%;
  transition: width 0.25s ease;
  border-radius: 3px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* Resource error */
.resource-error {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: var(--bg);
  z-index: 2000;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  text-align: center;
  padding: 24px;
}

.resource-error h2 {
  color: var(--danger);
  margin-bottom: 20px;
  font-weight: 700;
}

.resource-error p {
  color: var(--text-secondary);
  margin-bottom: 14px;
  max-width: 560px;
  line-height: 1.6;
}

.resource-error pre {
  background: var(--tile);
  padding: 16px;
  border-radius: 14px;
  margin: 16px 0;
  text-align: left;
  max-width: 80%;
  color: var(--text);
  border: 1px solid var(--border);
}

.resource-error #reload-btn {
  margin-top: 20px;
}

/* Help content */
.help-content {
  padding: 16px;
  background: var(--tile);
  border-radius: 16px;
  max-height: 360px;
  overflow-y: auto;
  line-height: 1.6;
  color: var(--text);
}

/* Keyboard key cap in the help shortcuts list */
.help-content kbd {
  display: inline-block;
  min-width: 22px;
  padding: 2px 7px;
  margin-right: 4px;
  font-family: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  text-align: center;
  color: var(--text);
  background: var(--button-bg);
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 6px;
}

.help-content h3 {
  color: var(--text);
  margin: 18px 0 8px;
  font-size: 1.05rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 8px;
}

.help-content h3:first-child {
  margin-top: 0;
}

.help-content h3 i {
  font-size: 0.95rem;
  color: var(--accent);
}

.help-content ul {
  padding-left: 20px;
  margin-bottom: 16px;
}

.help-content li {
  margin-bottom: 8px;
  color: var(--text-secondary);
}

.help-content a {
  color: var(--accent);
  text-decoration: none;
}

.help-content a:hover {
  text-decoration: underline;
}

/* Confirmation message */
.confirmation-message {
  text-align: center;
  padding: 20px 0;
  font-size: 1.05rem;
  line-height: 1.7;
}

.confirmation-message i {
  font-size: 3rem;
  color: var(--warning);
  margin-bottom: 18px;
  display: block;
}

.confirmation-message p {
  margin-bottom: 12px;
  color: var(--text);
}

/* File upload */
.file-upload {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.file-drop-area {
  border: 2px dashed var(--border);
  border-radius: 16px;
  padding: 28px;
  text-align: center;
  transition: all 0.2s ease;
  background: var(--tile);
  cursor: pointer;
}

.file-drop-area:hover,
.file-drop-area.drag-over {
  border-color: var(--accent);
  background: rgba(0, 122, 255, 0.06);
}

.file-drop-area i {
  font-size: 2.2rem;
  color: var(--accent);
  margin-bottom: 12px;
  display: block;
}

.file-drop-text {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 6px;
}

.file-drop-hint {
  color: var(--text-secondary);
  font-size: 0.9rem;
}

.load-status {
  margin-top: 16px;
  padding: 14px;
  border-radius: 12px;
  background: var(--tile);
  text-align: center;
  font-size: 0.95rem;
  color: var(--text-secondary);
  border: 1px solid var(--border);
}

/* Screenshot preview: always fit inside the modal, no scrolling needed */
.screenshot-modal-body {
  text-align: center;
}

#screenshot-preview {
  display: block;
  margin: 0 auto;
  max-width: 100%;
  height: auto;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--canvas-bg);
}

#screenshot-info {
  margin-top: 10px;
  color: var(--text-secondary);
  font-size: 0.9rem;
}

/* File input is triggered via the drop area; keep it out of sight */
#load-file {
  display: none;
}

/* Error display */
#error-display {
  display: none;
  position: fixed;
  top: 12px;
  right: 12px;
  background: var(--danger);
  color: white;
  padding: 12px 16px;
  border-radius: 12px;
  z-index: 1001;
  max-width: 320px;
  font-size: 0.8rem;
  box-shadow: var(--shadow);
  backdrop-filter: blur(10px);
}

#error-display h3 {
  margin-bottom: 6px;
  font-weight: 700;
}

/* Mobile panel toggle: anchored to the canvas container's corner (like the zoom controls) */
.mobile-panel-toggle {
  display: none;
  position: absolute;
  bottom: 16px;
  right: 16px;
  z-index: 10;
  background: var(--accent);
  color: white;
  border: none;
  padding: 14px 18px;
  border-radius: 30px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 6px 20px rgba(0, 122, 255, 0.35);
  transition: all 0.2s ease;
  align-items: center;
  gap: 8px;
}

.mobile-panel-toggle:active {
  transform: scale(0.94);
}

.mobile-only {
  display: none !important;
}

/* Panel overlay (drawer backdrop with iOS-style frosted fade) */
.panel-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.35);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  z-index: 199;
  visibility: hidden;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.35s ease, visibility 0s linear 0.35s;
}

.panel-overlay.show {
  visibility: visible;
  opacity: 1;
  pointer-events: auto;
  transition: opacity 0.35s ease;
}

/* Panel close button */
.panel-close-btn {
  display: none;
  background: var(--tile);
  border: none;
  color: var(--text-secondary);
  font-size: 1.1rem;
  cursor: pointer;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  transition: all 0.2s ease;
  align-items: center;
  justify-content: center;
}

.panel-close-btn:hover {
  background: var(--button-hover);
  color: var(--text);
}

/* Mobile toolbar */
.mobile-toolbar {
  display: none;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--header-bg);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-top: 1px solid var(--border);
  padding: 8px 12px calc(8px + env(safe-area-inset-bottom, 0px));
  z-index: 90;
  justify-content: space-around;
  align-items: center;
  box-shadow: 0 -2px 12px rgba(0, 0, 0, 0.08);
}

.mobile-tool-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  background: none;
  border: none;
  color: var(--text-secondary);
  padding: 8px 10px;
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.15s ease;
  min-width: 52px;
}

.mobile-tool-btn i {
  font-size: 1.25rem;
  color: var(--text);
}

.mobile-tool-btn span {
  font-size: 0.6rem;
  font-weight: 500;
}

.mobile-tool-btn:active,
.mobile-tool-btn.active {
  background: var(--tile);
  color: var(--accent);
}

.mobile-tool-btn:active i,
.mobile-tool-btn.active i {
  color: var(--accent);
}

/* Responsive - Tablets and small desktops */
@media (max-width: 1024px) {
  .components-grid {
    grid-template-columns: repeat(5, 1fr);
  }
}

/* Narrow desktop windows (mouse devices keep the desktop layout; just tighten the panel) */
@media (hover: hover) and (max-width: 1100px) {
  #components-panel {
    width: 260px;
  }
}

/* Responsive - Mobile (touch devices only; desktop windows with small width keep the desktop layout) */
@media (max-width: 900px) and (hover: none) {
  .header {
    padding: 10px 14px;
  }

  .logo h1 {
    font-size: 1.1rem;
  }

  .btn {
    padding: 7px 10px;
    font-size: 0.85rem;
  }

  .btn span {
    display: none;
  }

  .main-container {
    padding: 12px;
    gap: 12px;
    /* Reserve the real toolbar height plus a gap matching the side padding */
    padding-bottom: calc(var(--mobile-toolbar-real-h) + var(--mobile-bottom-gap, 12px));
  }

  .mobile-panel-toggle,
  .mobile-toolbar {
    display: flex;
  }

  .mobile-only {
    display: inline-flex !important;
  }

  .zoom-controls {
    display: none !important;
  }

  .bottom-container {
    display: none;
  }

  #components-panel {
    position: fixed;
    top: 0;
    left: 0;
    width: 88%;
    max-width: 380px;
    bottom: 0;
    height: 100vh;
    z-index: 200;
    transform: translateX(-100%);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    border-radius: 0;
    box-shadow: 4px 0 30px rgba(0, 0, 0, 0.18);
    padding: 16px;
    max-height: none;
    background: var(--panel-solid);
  }

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

  #components-list {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
  }

  .panel-close-btn {
    display: flex;
  }

  .header .controls .btn:not(#language-toggle):not(#help-btn):not(#save-screen):not(#zoom-reset-top) {
    display: none;
  }

  .components-grid {
    grid-template-columns: repeat(7, 1fr);
    gap: 5px;
  }

  .component img {
    width: 24px;
    height: 24px;
  }
}

@media (max-width: 600px) and (hover: none) {
  :root {
    /* Smaller side padding on phones -> smaller gap */
    --mobile-bottom-gap: 10px;
  }

  .header {
    padding: 8px 12px;
  }

  .controls {
    gap: 6px;
  }

  .btn {
    padding: 6px 8px;
    border-radius: 8px;
  }

  .btn i {
    font-size: 0.9rem;
  }

  .main-container {
    padding: 10px;
    /* Reserve the real toolbar height plus a gap matching the side padding */
    padding-bottom: calc(var(--mobile-toolbar-real-h) + var(--mobile-bottom-gap, 10px));
  }

  .mobile-tool-btn {
    padding: 6px 6px;
    min-width: 46px;
  }

  .mobile-tool-btn i {
    font-size: 1.1rem;
  }

  .mobile-tool-btn span {
    font-size: 0.55rem;
  }

  .mobile-panel-toggle {
    padding: 12px 16px;
    font-size: 0.85rem;
  }

  .components-grid {
    grid-template-columns: repeat(6, 1fr);
    gap: 4px;
  }

  .component {
    min-height: 44px;
    border-radius: 8px;
  }

  .modal-content {
    width: 95%;
    padding: 18px;
    border-radius: 18px;
    max-height: 88vh;
  }

  .modal-header h2 {
    font-size: 1.15rem;
  }

  .modal-btn {
    padding: 10px 18px;
    font-size: 0.9rem;
  }

  .help-content {
    max-height: 52vh;
    padding: 12px;
  }
}

/* Landscape mobile */
@media (max-width: 900px) and (orientation: landscape) and (hover: none) {
  .mobile-toolbar {
    left: 0;
    top: 50%;
    transform: translateY(-50%);
    bottom: auto;
    right: auto;
    width: 56px;
    flex-direction: column;
    height: 100vh;
    border-top: none;
    border-right: 1px solid var(--border);
    box-shadow: 2px 0 12px rgba(0, 0, 0, 0.1);
    padding: 10px 4px;
  }

  .mobile-tool-btn {
    padding: 10px 4px;
    min-width: auto;
  }

  .mobile-tool-btn span {
    display: none;
  }

  .main-container {
    padding-left: 64px;
    padding-bottom: 10px;
  }

  #components-panel {
    width: 55%;
    max-width: 320px;
  }
}

/* Animations */
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

.category {
  animation: fadeIn 0.35s ease forwards;
}

.category:nth-child(1) { animation-delay: 0.05s; }
.category:nth-child(2) { animation-delay: 0.1s; }
.category:nth-child(3) { animation-delay: 0.15s; }
.category:nth-child(4) { animation-delay: 0.2s; }

/* Selected component pulse */
.component.selected {
  animation: pulse 2s infinite ease-in-out;
}

@keyframes pulse {
  0%, 100% {
    box-shadow: 0 0 0 3px rgba(0, 122, 255, 0.25);
  }
  50% {
    box-shadow: 0 0 0 5px rgba(0, 122, 255, 0.12);
  }
}

body.theme-dark .component.selected {
  box-shadow: 0 0 0 3px rgba(10, 132, 255, 0.3);
  animation: pulse-dark 2s infinite ease-in-out;
}

@keyframes pulse-dark {
  0%, 100% {
    box-shadow: 0 0 0 3px rgba(10, 132, 255, 0.3);
  }
  50% {
    box-shadow: 0 0 0 5px rgba(10, 132, 255, 0.15);
  }
}

/* Safe area support */
@supports (padding: max(0px)) {
  .mobile-toolbar {
    padding-bottom: max(8px, env(safe-area-inset-bottom));
  }

  .header {
    padding-top: max(10px, env(safe-area-inset-top));
  }
}
