/* Basic Reset & Global Styles */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: "JetBrains Mono", "Courier New", monospace;
  line-height: 1.6;
  background: #0a0a0a;
  background-image: radial-gradient(circle at 25% 25%, #1a1a2e 0%, transparent 50%),
    radial-gradient(circle at 75% 75%, #16213e 0%, transparent 50%), linear-gradient(135deg, #0f0f0f 0%, #1a1a1a 100%);
  color: #00ff88;
  min-height: 100vh;
  overflow-x: hidden;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding: 20px;
}

/* Animated background effect */
body::before {
  content: "";
  position: fixed;
  inset: 0;
  background: repeating-linear-gradient(
    0deg,
    transparent,
    transparent 2px,
    rgba(0, 255, 136, 0.03) 2px,
    rgba(0, 255, 136, 0.03) 4px
  );
  pointer-events: none;
  z-index: 1;
}

/* Background Desktop - chỉ hiện trên desktop */
.background-desktop {
  display: block;
}

/* Background Mobile - chỉ hiện trên mobile */
.background-mobile {
  display: none;
}

.container {
  background-color: #0a0a0a;
  border-radius: 8px;
  box-shadow: 0 0 30px rgba(0, 255, 136, 0.1);
  max-width: 1400px;
  width: 100%;
  position: relative;
  z-index: 2;
  overflow: hidden;
}

.header {
  background: linear-gradient(135deg, #0a0a0a 0%, #1a1a2e 50%, #0a0a0a 100%);
  border: 2px solid #00ff88;
  border-radius: 8px;
  color: #00ff88;
  box-shadow: 0 0 20px rgba(0, 255, 136, 0.3), inset 0 0 20px rgba(0, 0, 0, 0.1);
  position: relative;
  overflow: hidden;
  text-align: center;
  margin-bottom: 30px;
  padding: 25px;
}
.header::before {
  content: "";
  position: absolute;
  top: -2px;
  left: -2px;
  right: -2px;
  bottom: -2px;
  background: linear-gradient(45deg, #00ff88, #ff0080, #00ff88);
  border-radius: 8px;
  z-index: -1;
  animation: borderGlow 3s ease-in-out infinite alternate;
}
@keyframes borderGlow {
  0% {
    opacity: 0.5;
  }
  100% {
    opacity: 1;
  }
}

.header h1 {
  font-family: "Orbitron", monospace;
  font-size: 2.8rem;
  font-weight: 900;
  margin-bottom: 10px;
  text-shadow: 0 0 10px #00ff88, 0 0 20px #00ff88, 0 0 30px #00ff88;
  letter-spacing: 3px;
  animation: textGlow 2s ease-in-out infinite alternate;
}
@keyframes textGlow {
  0% {
    text-shadow: 0 0 10px #00ff88, 0 0 20px #00ff88, 0 0 30px #00ff88;
  }
  100% {
    text-shadow: 0 0 15px #00ff88, 0 0 25px #00ff88, 0 0 35px #00ff88;
  }
}
.subtitle {
  font-size: 1.1rem;
  color: #ff0080;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 2px;
}

/* Main Content Layout */
.main-content {
  display: grid;
  grid-template-columns: 1fr 400px;
  gap: 30px;
  align-items: start;
  padding: 0 20px;
}

/* Mobile-first adjustments */
@media (max-width: 1024px) {
  .main-content {
    grid-template-columns: 1fr;
    gap: 20px;
    padding: 0 15px;
  }
  .header h1 {
    font-size: 2rem;
  }
  .subtitle {
    font-size: .9rem;
  }
  .background-desktop {
    display: none;
  }

  .background-mobile {
    display: block;
  }
}

/* Canvas Section */
.canvas-section {
  background: linear-gradient(135deg, #0f0f0f 0%, #1a1a2e 100%);
  border: 2px solid #00ff88;
  border-radius: 8px;
  padding: 25px;
  box-shadow: 0 0 30px rgba(0, 255, 136, 0.2), inset 0 0 30px rgba(0, 0, 0, 0.5);
  position: relative;
  order: 1;
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.canvas-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 15px;
  border-bottom: 2px solid #00ff88;
}
@media (max-width: 768px) {
  .canvas-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
  }
  .canvas-header h2 {
    font-size: 1.5rem;
  }
}
.canvas-header h2 {
  font-family: "Orbitron", monospace;
  font-size: 1.8rem;
  color: #00ff88;
  font-weight: 700;
  text-shadow: 0 0 10px #00ff88;
}
.canvas-controls {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: center;
}

/* Mobile default: square, full width */
.canvas-container {
  position: relative;
  width: 100%;
  padding-bottom: 100%;
  margin-bottom: 20px;
  background: linear-gradient(45deg, #0a0a0a 25%, transparent 25%),
    linear-gradient(-45deg, #0a0a0a 25%, transparent 25%), linear-gradient(45deg, transparent 75%, #0a0a0a 75%),
    linear-gradient(-45deg, transparent 75%, #0a0a0a 75%);
  background-size: 20px 20px;
  background-position: 0 0, 0 10px, 10px -10px, -10px 0px;
  border: 3px solid #00ff88;
  border-radius: 8px;
  box-shadow: 0 0 20px rgba(0, 255, 136, 0.3), inset 0 0 20px rgba(0, 0, 0, 0.3);
  overflow: hidden;
}
#character-canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100% !important;
  height: 100% !important;
  display: block;
  cursor: crosshair;
}

.canvas-info {
  text-align: center;
  margin-bottom: 25px;
}
.canvas-info p:first-child {
  font-size: 1.1rem;
  font-weight: 600;
  color: #00ff88;
  margin-bottom: 8px;
  text-shadow: 0 0 5px #00ff88;
}
.help-text {
  font-size: .9rem;
  color: #888;
  line-height: 1.5;
}

/* Customization Sections */
.customization-panel {
  background: linear-gradient(135deg, #0f0f0f 0%, #1a1a2e 100%);
  border: 2px solid #00ff88;
  border-radius: 8px;
  padding: 25px;
  box-shadow: 0 0 30px rgba(0, 255, 136, 0.2), inset 0 0 30px rgba(0, 0, 0, 0.5);
  max-height: 80vh;
  overflow-y: auto;
  order: 2;
}
.customization-section {
  margin-bottom: 25px;
  border: 1px solid #333;
  border-radius: 8px;
  overflow: hidden;
  background: #111;
}
.section-header {
  display: flex;
  align-items: center;
  padding: 15px 20px;
  background: linear-gradient(135deg, #00ff88 0%, #00cc6a 100%);
  color: #000;
  cursor: pointer;
  transition: all .3s ease;
  user-select: none;
  font-weight: 600;
}
.section-header:hover {
  background: linear-gradient(135deg, #00cc6a 0%, #00ff88 100%);
  box-shadow: 0 0 15px rgba(0, 255, 136, 0.5);
}
.section-header.collapsible {
  justify-content: space-between;
}
.section-icon {
  font-size: 1.5rem;
  margin-right: 12px;
}
.section-header h3 {
  font-family: "Orbitron", monospace;
  font-size: 1.2rem;
  font-weight: 700;
  flex-grow: 1;
  text-transform: uppercase;
  letter-spacing: 1px;
}
.collapse-icon {
  font-size: 1rem;
  transition: transform .3s ease;
  font-weight: bold;
}
.section-header.collapsed .collapse-icon {
  transform: rotate(-90deg);
}

.options-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(70px, 1fr));
  gap: 12px;
  padding: 20px;
  background: #0a0a0a;
  transition: all .3s ease;
}
.options-grid.collapsed {
  display: none;
}
.option-item {
  position: relative;
  cursor: pointer;
  border-radius: 8px;
  overflow: hidden;
  transition: all .3s ease;
  background: #111;
  border: 2px solid #333;
  aspect-ratio: 1;
}
.option-item:hover {
  transform: translateY(-3px);
  box-shadow: 0 0 20px rgba(0, 255, 136, 0.4);
  border-color: #00ff88;
}
.option-item.active {
  border-color: #ff0080;
  box-shadow: 0 0 20px rgba(255, 0, 128, 0.6), inset 0 0 10px rgba(255, 0, 128, 0.2);
  transform: scale(1.05);
}
.option-preview {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}
.option-preview img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 6px;
}
.remove-option {
  background: linear-gradient(135deg, #ff0040, #cc0033);
  color: #fff;
}
.remove-option:hover {
  background: linear-gradient(135deg, #ff0080, #ff0040);
  box-shadow: 0 0 20px rgba(255, 0, 64, 0.6);
}
.remove-icon {
  font-size: 1.5rem;
  font-weight: bold;
  color: #fff;
  text-shadow: 0 0 5px rgba(255, 255, 255, 0.8);
}
.upload-section {
  padding: 15px 20px;
  background: #0a0a0a;
  border-top: 1px solid #333;
}

/* Buttons */
.btn {
  padding: 12px 24px;
  border: 2px solid;
  border-radius: 6px;
  font-size: .9rem;
  font-weight: 600;
  cursor: pointer;
  transition: all .3s ease;
  text-transform: uppercase;
  letter-spacing: 1px;
  font-family: "JetBrains Mono", monospace;
  position: relative;
  overflow: hidden;
}
.btn::before {
  content: "";
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left .5s;
}
.btn:hover::before {
  left: 100%;
}
.btn-primary {
  background: linear-gradient(135deg, #ff0080, #cc0066);
  color: #fff;
  border-color: #ff0080;
}
.btn-primary:hover {
  background: linear-gradient(135deg, #ff0099, #ff0080);
  box-shadow: 0 0 20px rgba(255, 0, 128, 0.5);
  transform: translateY(-2px);
}
.btn-secondary {
  background: linear-gradient(135deg, #333, #555);
  color: #00ff88;
  border-color: #00ff88;
}
.btn-secondary:hover {
  background: linear-gradient(135deg, #555, #333);
  box-shadow: 0 0 20px rgba(0, 255, 136, 0.3);
  transform: translateY(-2px);
}
.btn-upload {
  background: linear-gradient(135deg, #1a1a2e, #16213e);
  color: #00ff88;
  border-color: #00ff88;
  width: 100%;
}
.btn-upload:hover {
  background: linear-gradient(135deg, #16213e, #1a1a2e);
  box-shadow: 0 0 20px rgba(0, 255, 136, 0.3);
  transform: translateY(-2px);
}

/* Footer */
.footer {
  text-align: center;
  margin-top: 40px;
  padding: 20px;
  color: #666;
  font-size: .9rem;
  border-top: 1px solid #333;
}

/* Draggable elements */
.draggable-element {
  position: absolute;
  cursor: move;
  border: 2px dashed transparent;
  border-radius: 5px;
  transition: border-color .2s ease;
}
.draggable-element:hover {
  border-color: #4facfe;
}
.draggable-element.dragging {
  border-color: #00f2fe;
  opacity: 0.8;
}

/* Scrollbar styling */
.customization-panel::-webkit-scrollbar {
  width: 8px;
}
.customization-panel::-webkit-scrollbar-track {
  background: #111;
  border-radius: 4px;
}
.customization-panel::-webkit-scrollbar-thumb {
  background: linear-gradient(135deg, #00ff88, #00cc6a);
  border-radius: 4px;
}
.customization-panel::-webkit-scrollbar-thumb:hover {
  background: linear-gradient(135deg, #00cc6a, #00ff88);
}

/* Loading animation */
@keyframes pulse {
  0%,
  100% {
    opacity: 1;
  }
  50% {
    opacity: 0.5;
  }
}
.loading {
  animation: pulse 1.5s ease-in-out infinite;
}

/* Matrix & Glitch */
@keyframes matrix {
  0% {
    opacity: 0;
    transform: translateY(-10px);
  }
  50% {
    opacity: 1;
    transform: translateY(0);
  }
  100% {
    opacity: 0;
    transform: translateY(10px);
  }
}
@keyframes glitch {
  0% {
    transform: translate(0);
  }
  20% {
    transform: translate(-2px, 2px);
  }
  40% {
    transform: translate(-2px, -2px);
  }
  60% {
    transform: translate(2px, 2px);
  }
  80% {
    transform: translate(2px, -2px);
  }
  100% {
    transform: translate(0);
  }
}
.header h1:hover {
  animation: glitch .3s ease-in-out;
}

canvas {
  image-rendering: pixelated;
  image-rendering: crisp_edges;
}

@media (min-width: 1024px) {
  .canvas-section {
    align-items: center;
  }
  .canvas-container {
    width: 512px !important;
    height: auto !important;
    aspect-ratio: 1 / 1;
    padding-bottom: 0 !important;
    max-width: 512px !important;
    align-self: center;
  }
  #character-canvas {
    width: 100% !important;
    height: 100% !important;
  }
  .canvas-header {
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    width: 100%;
  }
  .canvas-header h2 {
    font-size: 1.8rem;
  }
  .canvas-controls {
    flex-wrap: nowrap;
  }
  .customization-panel {
    max-height: 80vh;
    overflow-y: auto;
  }
  .background-desktop {
    display: block;
  }

  .background-mobile {
    display: none;
  }
}

/* Hide upload section on mobile when the Background header is collapsed */
@media (max-width: 1024px) {
  .background-mobile .section-header.collapsible.collapsed ~ .upload-section {
    display: none;
  }
}

/* Trait Controls */
.trait-controls {
  background: #111;
  border: 1px solid #333;
  border-radius: 8px;
  padding: 10px; /* Giảm padding từ 15px xuống 10px */
  margin-bottom: 15px; /* Giảm margin-bottom từ 20px xuống 15px */
  display: block; /* Ẩn mặc định */
}
.trait-controls.disabled {
  opacity: 0.6;
}

.trait-controls input:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.trait-controls h3 {
  font-family: "Orbitron", monospace;
  font-size: 1.2rem; /* Giảm font-size từ 1.2rem xuống 1.1rem */
  color: #00ff88;
  margin-bottom: 10px; /* Giảm margin-bottom từ 15px xuống 10px */
  text-shadow: 0 0 5px #00ff88;
}

.trait-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.buttons-group {
  display: flex;
  gap: 10px; /* Khoảng cách giữa hai nút */
}

.reset-btn, .remove-btn {
  background: linear-gradient(135deg, #333, #555);
  color: #00ff88;
  border: 1px solid #00ff88;
  border-radius: 4px;
  padding: 5px 10px;
  font-size: 1rem;
  cursor: pointer;
  transition: all 0.3s;
  margin-left: 10px; /* Khoảng cách giữa hai nút */
}

.reset-btn:hover, .remove-btn:hover {
  background: linear-gradient(135deg, #555, #333);
  box-shadow: 0 0 10px rgba(0, 255, 136, 0.3);
}


.reset-btn:disabled, .remove-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.control-group {
  display: flex;
  align-items: center;
  margin-bottom: 5px; /* Giảm margin-bottom từ 10px xuống 5px để compact hơn */
}

.control-group label {
  width: 80px; /* Giảm width từ 100px xuống 80px */
  color: #00ff88;
  font-size: 0.8rem; /* Giảm font-size từ 0.9rem xuống 0.8rem */
  margin-left: 1rem;
}

.control-group input[type="range"] {
  flex: 1;
  margin-right: 10px; /* Giảm margin-right từ 10px xuống 5px */
  background: #333;
  -webkit-appearance: none;
  height: 4px; /* Giảm height từ 6px xuống 4px để mỏng hơn */
  border-radius: 2px; /* Điều chỉnh border-radius tương ứng */
}

.control-group input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 12px; /* Giảm width/height từ 16px xuống 12px */
  height: 12px;
  background: #00ff88;
  border-radius: 50%;
  cursor: pointer;
}

.control-group input[type="number"] {
  width: 50px; /* Giảm width từ 60px xuống 50px */
  background: #0a0a0a;
  border: 1px solid #00ff88;
  color: #00ff88;
  padding: 3px; /* Giảm padding từ 5px xuống 3px */
  border-radius: 4px;
  font-size: 0.8rem; /* Thêm font-size nhỏ hơn */
}

/* Media query cho desktop (min-width 1025px) để compact hơn nữa nếu cần */
@media (min-width: 1025px) {
  .trait-controls {
    padding: 8px;
  }
  .control-group {
    margin-bottom: 3px;
  }
  .trait-controls h3 {
    margin-bottom: 8px;
    font-size: 1rem;
  }
  .control-group label {
    width: 70px;
    font-size: 0.75rem;
  }
  .control-group input[type="number"] {
    width: 45px;
    padding: 2px;
  }
}

/* Media query cho mobile (giữ nguyên hoặc điều chỉnh nếu cần, nhưng focus là laptop) */
@media (max-width: 1024px) {
  .trait-controls {
    padding: 10px;
    display: none;
  }
  .control-group {
    flex-direction: column;
    align-items: flex-start;
    margin-bottom: 8px; /* Tăng nhẹ cho mobile để dễ chạm */
  }
  .control-group label {
    width: auto;
    margin-bottom: 3px;
    font-size: 0.9rem;
  }
  .control-group input[type="range"] {
    width: 100%;
    margin-bottom: 3px;
    height: 6px; /* Giữ height lớn hơn cho mobile */
  }
  .control-group input[type="number"] {
    width: 100%;
    padding: 5px;
  }
}