body {
  margin: 0;
  padding: 0;
  font-family: 'Roboto', sans-serif;
  overflow: hidden;
  transition: background-color 0.3s ease, color 0.3s ease;
  /* iOS улучшения */
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* Плавная анимация смены темы холста */
#canvas-container.theme-transition {
  animation: smoothThemeChange 0.5s ease-out;
}

@keyframes smoothThemeChange {
  0% {
    background-image: var(--from-bg-image);
    background-color: var(--from-bg-color);
  }
  100% {
    background-image: var(--to-bg-image);
    background-color: var(--to-bg-color);
  }
}

/* Темная тема */
body.dark-theme {
  background-color: transparent;
  color: #e0e0e0;
}

body.dark-theme #canvas-container {
  background-image: radial-gradient(#1c1c1c 1px, transparent 1px);
  background-color: #000000;
}

body.dark-theme #toolbar {
  background-color:transparent;
  border: 1px solid transparent;
}

body.dark-theme button {
  background-color: transparent;
  color: #404040;
  border: 1px solid transparent;
}

body.dark-theme button:hover {
  background-color: transparent;
  color: #b5b5b5;
  transform: scale(1.6);
}

body.dark-theme button.active {
  background-color: transparent;
  color: #ffffff;
}

body.dark-theme .brush-menu {
  background-color: rgb(0, 0, 0, 0.95);
  border: 1px solid #000000;
  color: #e0e0e0;
}

body.dark-theme .brush-settings-section {
  background: rgb(0, 0, 0, 0.3);
}

body.dark-theme .brush-parameter label {
  color: #595959;
}

body.dark-theme .asset-selector {
  background-color: #000000;
  color: #000000;
  border: 1px solid #000000;
}

body.dark-theme .asset-item {
  border: 1px solid #000000;
  background-color: #000000;
}

body.dark-theme .asset-item:hover {
  background-color: #000000;
}

body.dark-theme .layers-panel {
  background-color: rgb(0, 0, 0, 0.95);
  border: 1px solid #000000;
  color: #000000;
}

body.dark-theme .layers-panel-header {
  border-bottom: 1px solid #000000;
}

body.dark-theme .layers-panel-title {
  color: #e0e0e0;
}

body.dark-theme .layer-item {
  border-color: #000000;
}

body.dark-theme .layer-item:hover {
  background-color: rgb(0, 0, 0, 0.1);
}

body.dark-theme .layer-item.selected {
  background-color: rgb(0, 47, 117, 0.3);
  border-color: #000000;
}

body.dark-theme .layer-name {
  color: #808080;
}

body.dark-theme .layer-name.hidden {
  color: #404040;
}

body.dark-theme .layers-panel-footer {
  border-top: 1px solid #000000;
  color: #999;
}

body.dark-theme #context-menu {
  background-color: rgb(0, 0, 0, 0.95);
  border: 1px solid #000000;
  color: #ffffff;
}

body.dark-theme .animation-controls {
  background: rgb(0, 0, 0, 0.95);
  color: #e0e0e0;
}

body.dark-theme #ios-save-modal > div {
  background-color: #000000;
  color: #e0e0e0;
  border: 1px solid #000000;
}

body.dark-theme .save-format-menu {
  background-color: rgb(0, 0, 0, 0.95);
  border: 1px solid #000000;
  color: #e0e0e0;
}

body.dark-theme .save-format-menu button {
  color: #e0e0e0;
  border-color: #000000;
}

body.dark-theme .save-format-menu button:hover {
  background-color: transparent;
  color: #ffffff;
}



/* Стили для выпадающего меню формата сохранения */
body.dark-theme .format-dropdown {
  background-color: rgb(0, 0, 0, 0.95) !important;
  border: 1px solid #000000 !important;
  color: #e0e0e0 !important;
}

body.dark-theme .format-dropdown button {
  background-color: transparent !important;
  color: #e0e0e0 !important;
  border: none !important;
}

body.dark-theme .format-dropdown button:hover {
  background-color:transparent;
  color: #ffffff !important;
}

#editor {
  width: 100vw;
  height: 100vh;
}

#canvas-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background-image: radial-gradient(#d0d0d0 1px, transparent 1px);
  background-size: 20px 20px;
  /* iOS улучшения */
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  touch-action: none;
  overscroll-behavior: none;
}

#toolbar {
  position: fixed;
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
  width: auto;
  background-color: transparent;
  border-radius: 10px;
  box-shadow:transparent;
  padding: 10px;
  z-index: 1000;
}

.tool-group {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
}

button {
  width: 40px;
  height: 40px;
  padding: 8px;
  border: transparent;
  border-radius: 1px;
  background-color:transparent;
  color: #8f8f8f;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex;
  justify-content: center;
  align-items: center;
  transform: scale(1);
}

button:hover {
  background-color:transparent;
  color: #616161;
  transform: scale(1.6);
}

button.active {
  background-color:transparent;
  color: #000000;
}

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

.brush-menu {
  position: fixed;
  top: 80px;
  left: 50%;
  transform: translateX(-50%);
  background-color: rgba(255, 255, 255, 0.9);
  border-radius: 10px;
  box-shadow: 0 0 10px rgba(0,0,0,0.1);
  padding: 15px;
  margin-top: 10px;
  width: 600px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 15px;
  z-index: 1500;
}

.brush-settings-section {
  padding: 10px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 5px;
}

.brush-settings-section h4 {
  margin: 0 0 10px 0;
  font-size: 14px;
  color: #333;
}

.brush-parameter {
  margin-bottom: 8px;
}

.brush-parameter label {
  display: block;
  font-size: 12px;
  margin-bottom: 4px;
  color: #666;
}

.brush-menu input[type="color"],
.brush-menu input[type="range"] {
  width: 100%;
  margin-bottom: 5px;
}

.brush-menu input[type="range"] {
  -webkit-appearance: none;
  height: 5px;
  border-radius: 5px;
  background: #ddd;
  outline: none;
}

.brush-menu input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background: #666;
  cursor: pointer;
}

#brush-preview {
  width: 100%;
  height: 100px;
  margin-top: 10px;
}

/* Стили для контекстного меню */
#context-menu {
  position: absolute;
  background-color: rgba(255, 255, 255, 0.9);
  border-radius: 10px;
  box-shadow: 0 0 10px rgba(0,0,0,0.1);
  padding: 10px;
  z-index: 1000;
  min-width: 180px;
}

/* Стили для меню выбора формата сохранения */
.save-format-menu {
  position: absolute;
  background-color: rgba(255, 255, 255, 0.9);
  border-radius: 10px transparent;
  padding: 10px;
  z-index: 1000;
  min-width: 180px;
}

.save-format-menu button {
  width: 100%;
  text-align: left;
  padding: 8px;
  margin-bottom: 5px;
  height: auto;
  min-height: 32px;
  font-size: 12px;
  white-space: nowrap;
}

.save-format-menu button:last-child {
  margin-bottom: 0;
}

/* Базовые стили для выпадающего меню формата */
.format-dropdown {
  position: absolute;
  background-color: rgba(255, 255, 255, 0.9);
  border-radius: transparent;
  box-shadow: transparent;
  padding: 10px;
  z-index: 1000;
  min-width: 120px;
  /* По умолчанию открывается вниз */
  top: 100%;
  margin-top: 5px;
}

.format-dropdown button {
  width: 100%;
  text-align: center;
  padding: 8px;
  margin-bottom: 5px;
  height: auto;
  min-height: 32px;
  font-size: 12px;
  white-space: nowrap;
  background-color: transparent;
  border: none;
  border-radius: 5px;
  cursor: pointer;
}

.format-dropdown button:last-child {
  margin-bottom: 0;
}

.format-dropdown button:hover {
  background-color:transparent;
}

#context-menu button {
  width: 100%;
  text-align: left;
  padding: 8px;
  margin-bottom: 5px;
  height: auto;
  min-height: 32px;
  font-size: 12px;
  white-space: nowrap;
}

#context-menu button:last-child {
  margin-bottom: 0;
}

.animation-controls {
  padding: 10px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 5px;
  margin-top: 5px;
}

.animation-controls label {
  display: block;
  margin-bottom: 10px;
  font-size: 12px;
}

.animation-controls input[type="range"] {
  width: 100%;
  margin-top: 5px;
}

/* Стили для панели слоев */
.layers-panel {
  position: fixed;
  right: 20px;
  top: 80px;
  width: 280px;
  height: 400px;
  background-color: rgba(255, 255, 255, 0.95);
  border-radius: 10px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.15);
  z-index: 1500;
  display: flex;
  flex-direction: column;
  padding: 15px;
}

.layers-panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
  border-bottom: 1px solid #eee;
  padding-bottom: 10px;
}

.layers-panel-title {
  margin: 0;
  font-size: 16px;
  color: #333;
}

.layers-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.layer-item {
  display: flex;
  align-items: center;
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
  height: 40px;
  border: 1px solid transparent;
  transition: all 0.15s ease;
  font-size: 13px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

.layer-item:hover {
  background-color: rgba(0, 0, 0, 0.05);
}

.layer-item.selected {
  background-color: #f0f8ff;
  border-color: #4a90e2;
}

.layer-type-icon {
  width: 20px;
  height: 20px;
  border-radius: 3px;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  margin-right: 8px;
}

.layer-type-group {
  background-color: #ff9800;
}

.layer-type-default {
  background-color: #4caf50;
}

.layer-name {
  flex: 1;
  font-size: 12px;
  color: #2c3e50;
  font-weight: 400;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.layer-item.selected .layer-name {
  font-weight: 500;
}

.layer-name.hidden {
  color: #95a5a6;
  text-decoration: line-through;
}

.layer-controls {
  display: flex;
  gap: 2px;
  opacity: 0.6;
  transition: opacity 0.15s ease;
}

.layer-item.selected .layer-controls {
  opacity: 1;
}

.layer-control-btn {
  padding: 2px 4px;
  font-size: 10px;
  border: none;
  background: none;
  cursor: pointer;
  min-width: auto;
  width: auto;
  height: auto;
}

.layer-control-btn:hover {
  background-color: rgba(0, 0, 0, 0.1);
}

.layer-control-btn.delete {
  color: #f44336;
}

.layers-panel-footer {
  font-size: 11px;
  color: #666;
  margin-top: 10px;
  text-align: center;
  border-top: 1px solid #eee;
  padding-top: 8px;
}



/* Стилизация скроллбара */
.asset-list::-webkit-scrollbar,
.layers-list::-webkit-scrollbar {
  width: 8px;
}

.asset-list::-webkit-scrollbar-track,
.layers-list::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 4px;
}

.asset-list::-webkit-scrollbar-thumb,
.layers-list::-webkit-scrollbar-thumb {
  background: #888;
  border-radius: 4px;
}

.asset-list::-webkit-scrollbar-thumb:hover,
.layers-list::-webkit-scrollbar-thumb:hover {
  background: #555;
}

/* Стили для мобильных устройств */
@media (max-width: 768px) {
  /* Меню формата сохранения открывается вверх на мобильных */
  .format-dropdown {
    top: auto !important;
    bottom: 100% !important;
    margin-top: 0 !important;
    margin-bottom: 5px !important;
  }

  /* Перемещаем индикатор зума вверх влево на мобильных */
  .zoom-indicator {
    top: max(20px, env(safe-area-inset-top)) !important;
    bottom: auto !important;
    left: max(20px, env(safe-area-inset-left)) !important;
  }

  /* Адаптация селектора области рендера для мобилки */
  .render-area-selector-mobile {
    padding: 4px 8px !important;
    border-radius: 12px !important;
    max-width: 98vw !important;
  }

  /* Маркеры ресайза больше для touch */
  .resize-handle-mobile {
    width: 20px !important;
    height: 20px !important;
    border: 3px solid rgba(255, 255, 255, 0.9) !important;
    border-radius: 6px !important;
  }

  /* Увеличиваем боковые маркеры для мобилки */
  .resize-side-handle-mobile {
    width: 30px !important;
    height: 15px !important;
  }

  /* Панель с информацией выше области на мобилке */
  .area-info-panel-mobile {
    top: -60px !important;
    font-size: 14px !important;
    padding: 6px 12px !important;
    border-radius: 8px !important;
  }
  
  #toolbar {
    top: auto;
    bottom: 20px;
    left: 20px;
    right: 20px;
    transform: none;
    width: auto;
    max-width: none;
    padding: 5px;
    overflow-x: auto;
    overflow-y: hidden;
    white-space: nowrap;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    -ms-overflow-style: none;
    position: fixed;
    /* iOS-специфичные стили */
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    touch-action: manipulation;
  }

  #toolbar::-webkit-scrollbar {
    display: none;
  }

  .tool-group {
    flex-wrap: nowrap;
    gap: 2px;
    min-width: max-content;
  }

  button {
    width: 35px;
    height: 35px;
    padding: 6px;
    font-size: 12px;
    /* iOS touch улучшения */
    -webkit-touch-callout: none;
    -webkit-tap-highlight-color: rgba(0,0,0,0.1);
    touch-action: manipulation;
  }

  .brush-menu {
    position: fixed !important;
    left: max(10px, env(safe-area-inset-left)) !important;
    right: max(10px, env(safe-area-inset-right)) !important;
    bottom: calc(80px + env(safe-area-inset-bottom, 0px)) !important;
    top: auto !important;
    transform: translate3d(0, 0, 0) !important;
    z-index: 2000 !important;
    max-height: calc(100vh - 120px - env(safe-area-inset-bottom, 0px)) !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch !important;
    margin: 0 !important;
    width: auto !important;
    max-width: none !important;
    /* iOS-специфичные улучшения */
    -webkit-touch-callout: none;
    box-sizing: border-box;
    /* Изоляция позиционирования от родительского контейнера */
    contain: layout;
  }

  

  /* Панель слоев на мобиле */
  .layers-panel {
    right: 10px;
    top: max(60px, env(safe-area-inset-top));
    width: 250px;
    height: 350px;
    padding: 10px;
  }
}

/* Дополнительные стили для iPhone с notch */
@media (max-width: 768px) and (orientation: portrait) {
  #toolbar {
    bottom: max(20px, env(safe-area-inset-bottom));
    left: max(20px, env(safe-area-inset-left));
    right: max(20px, env(safe-area-inset-right));
  }
  
  .brush-menu {
    bottom: calc(80px + env(safe-area-inset-bottom)) !important;
    left: max(10px, env(safe-area-inset-left)) !important;
    right: max(10px, env(safe-area-inset-right)) !important;
  }
}

/* Специальные стили для iPhone Safari */
@supports (-webkit-touch-callout: none) {
  @media (max-width: 768px) {
    .brush-menu {
      position: fixed !important;
      left: 10px !important;
      right: 10px !important;
      bottom: calc(80px + env(safe-area-inset-bottom, 0px)) !important;
      transform: none !important;
      width: auto !important;
      max-width: none !important;
      margin: 0 !important;
      box-sizing: border-box !important;
    }
  }
}

/* Landscape iPhone стили */
@media (max-width: 768px) and (orientation: landscape) {
  #toolbar {
    bottom: max(10px, env(safe-area-inset-bottom));
    left: max(60px, env(safe-area-inset-left));
    right: max(10px, env(safe-area-inset-right));
  }
  
  .brush-menu {
    bottom: calc(60px + env(safe-area-inset-bottom));
    left: max(60px, env(safe-area-inset-left));
    right: max(10px, env(safe-area-inset-right));
    max-height: calc(100vh - 80px - env(safe-area-inset-bottom));
  }

  .layers-panel h3 {
    font-size: 14px;
  }

  .layer-item {
    padding: 6px;
    min-height: 32px;
  }

  .layer-name {
    font-size: 12px;
  }

  .layer-control-btn {
    font-size: 9px;
  }
}

/* Стили для круглого color picker */
input[type="color"] {
  width: 22px;
  height: 22px;
  padding: 0;
  border: none;
  background: none;
  cursor: pointer;
  border-radius: 50%;
  overflow: hidden;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  display: block;
  margin: auto;
}

input[type="color"]::-webkit-color-swatch-wrapper {
  padding: 0;
  border: none;
  border-radius: 50%;
}

input[type="color"]::-webkit-color-swatch {
  border: none;
  border-radius: 50%;
}

input[type="color"]::-moz-color-swatch {
  border: none;
  border-radius: 50%;
}

body.dark-theme input[type="color"] {
  border: 0px solid #404040;
}

body.dark-theme input[type="color"]::-webkit-color-swatch-wrapper {
  border-radius: 50%;
}

body.dark-theme input[type="color"]::-webkit-color-swatch {
  border-radius: 50%;
}

body.dark-theme input[type="color"]::-moz-color-swatch {
  border-radius: 50%;
}

/* Запрет выделения текста во время перетаскивания */
.no-select {
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

*:not(input):not(textarea) {
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}