* {
  box-sizing: border-box;
}

:root {
  --bg: #0a1f2e;
  --card: #112b3c;
  --text: #e6f0f6;
  --muted: #9bb2c9;
  --primary: #38bdf8;
  --primary-2: #22d3ee;
  --success: #34d399;
  --ring: 0 0 0 8px rgba(34, 211, 238, 0.15);
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.25);
  --danger: #f87171;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: radial-gradient(1200px 600px at 20% -10%, #123d57 0%, #0a1f2e 50%, #06121c 100%);
  color: var(--text);
  font-family: Inter, system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
  line-height: 1.5;
  overflow-x: hidden;
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  position: sticky;
  top: 0;
  z-index: 10;
  backdrop-filter: blur(10px);
  background: linear-gradient(180deg, rgba(10, 20, 30, 0.9), rgba(10, 20, 30, 0.6));
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand h1 {
  font-size: 20px;
  margin: 0;
  font-weight: 800;
}

.brand h1 span {
  background: linear-gradient(90deg, var(--primary), var(--primary-2));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.logo-spin {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, 0.15);
  border-top-color: var(--primary);
  animation: spin 3.5s linear infinite;
  box-shadow: inset 0 0 10px rgba(56, 189, 248, 0.4);
}

.github {
  color: var(--muted);
  text-decoration: none;
  font-weight: 600;
  transition: color 0.2s ease;
}

.github:hover {
  color: var(--primary);
}

.container {
  max-width: 1100px;
  margin: 20px auto;
  padding: 0 16px;
}

.card {
  background: linear-gradient(180deg, rgba(18, 42, 58, 0.9), rgba(12, 30, 46, 0.9));
  border: 1px solid rgba(255, 255, 255, 0.06);
  border-radius: 18px;
  padding: 16px;
  box-shadow: var(--shadow);
  margin-bottom: 18px;
  animation: elevate 0.8s ease both;
}

/* Frame grid */
.frame-grid {
  display: grid;
  gap: 14px;
  grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
}

.frame-item {
  background: #0e2230;
  border: 1px solid rgba(255, 255, 255, 0.07);
  border-radius: 14px;
  padding: 8px;
  transition: transform .2s ease, box-shadow .2s ease, border-color .2s ease;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.frame-item::after {
  content: "Chọn";
  position: absolute;
  inset: auto 8px 8px auto;
  font-size: 12px;
  padding: 4px 8px;
  border-radius: 999px;
  background: rgba(56, 189, 248, 0.12);
  color: var(--primary);
  transition: transform .2s ease, opacity .2s ease;
  transform: translateY(8px);
  opacity: 0;
}

.frame-item:hover {
  transform: translateY(-3px) scale(1.01);
  box-shadow: 0 10px 25px rgba(15, 40, 60, 0.5);
  border-color: rgba(56, 189, 248, 0.5);
}

.frame-item:hover::after {
  transform: translateY(0);
  opacity: 1;
}

.frame-thumb {
  width: 100%;
  aspect-ratio: 1/1;
  display: block;
  border-radius: 8px;
  object-fit: cover;
}

.uploader {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.tips {
  color: var(--muted);
  font-size: 13px;
  margin-top: 8px;
}

/* Editor layout */
.editor {
  display: grid;
  grid-template-columns: 1fr 300px;
  gap: 16px;
  align-items: start;
}

.canvas-wrap {
  background: conic-gradient(from 45deg, #10384d, #0a2536, #12364a, #10384d);
  border-radius: 16px;
  padding: 14px;
  border: 1px solid rgba(255, 255, 255, 0.06);
  box-shadow: var(--shadow);
  position: relative;
}

canvas {
  width: 100%;
  height: auto;
  border-radius: 10px;
  background:
    linear-gradient(45deg, #223344 25%, transparent 25%) -16px 0/32px 32px,
    linear-gradient(-45deg, #223344 25%, transparent 25%) -16px 0/32px 32px,
    linear-gradient(45deg, transparent 75%, #223344 75%)/32px 32px,
    linear-gradient(-45deg, transparent 75%, #223344 75%)/32px 32px,
    #0f1e2a;
  border: 1px solid rgba(255, 255, 255, 0.06);
  display: block;
}

/* Controls */
/* ===== SỬA PHẦN CONTROLS ĐỂ NÚT XẾP THÀNH NHIỀU HÀNG ===== */

.controls {
  display: flex;
  flex-direction: column;
  gap: 16px;
  order: 2;
}

.control-panel {
  background: linear-gradient(180deg, rgba(18, 42, 58, 0.9), rgba(12, 30, 46, 0.9));
  border: 1px solid rgba(255, 255, 255, 0.06);
  border-radius: 16px;
  padding: 16px;
  box-shadow: var(--shadow);
}

.control-panel h3 {
  margin-top: 0;
  margin-bottom: 15px;
  font-size: 16px;
  color: var(--primary);
}

.control {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 15px;
}

.control.grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  /* 3 cột thay vì 2 */
  gap: 8px;
  /* Giảm khoảng cách giữa các nút */
}

label {
  font-weight: 600;
  font-size: 14px;
  color: #cbe7f5;
}

input[type="range"] {
  width: 100%;
  accent-color: var(--primary);
  background: transparent;
  cursor: pointer;
  height: 24px;
  /* Tăng chiều cao cho dễ chạm trên mobile */
  -webkit-tap-highlight-color: transparent;
  /* Loại bỏ highlight khi chạm */
}

input[type="file"] {
  width: 100%;
  padding: 10px;
  border-radius: 12px;
  background: #0d2533;
  border: 1px solid rgba(255, 255, 255, 0.08);
  color: var(--text);
  font-size: 14px;
  cursor: pointer;
}

.btn {
  border: 1px solid rgba(255, 255, 255, 0.08);
  background: #0d2533;
  padding: 12px 16px;
  /* Tăng padding cho dễ chạm */
  border-radius: 12px;
  color: var(--text);
  font-weight: 600;
  cursor: pointer;
  transition: transform .15s ease, box-shadow .2s ease, border-color .2s ease;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  /* Đảm bảo đủ lớn cho touch */
  -webkit-tap-highlight-color: transparent;
}

.btn:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow);
  border-color: rgba(56, 189, 248, 0.3);
}

.btn:active {
  transform: translateY(0) scale(0.97);
}

.btn.primary {
  background: linear-gradient(180deg, #136f8a, #0d5066);
  border-color: rgba(34, 211, 238, 0.4);
  box-shadow: var(--ring);
}

.btn.success {
  background: linear-gradient(180deg, #0f5f42, #0c4431);
  border-color: rgba(52, 211, 153, 0.5);
}

.btn.subtle {
  background: #0f2233;
  border-style: dashed;
  color: var(--muted);
}

.btn.danger {
  background: linear-gradient(180deg, #7e2a2a, #5c1e1e);
  border-color: rgba(248, 113, 113, 0.4);
}

.bounce {
  animation: bounce 2s ease-in-out infinite;
}

.footer {
  text-align: center;
  color: var(--muted);
  padding: 20px 0 40px;
  font-size: 14px;
}

.fade-in {
  animation: fade-in .6s ease both;
}

/* Frame option và delete button */
.frame-option {
  position: relative;
  height: 100%;
}

.frame-option img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 12px;
}

.delete-btn {
  position: absolute;
  top: 5px;
  right: 5px;
  background: rgba(255, 0, 0, 0.8);
  border: none;
  color: white;
  border-radius: 50%;
  width: 28px;
  height: 28px;
  cursor: pointer;
  transition: background 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  padding: 0;
}

.delete-btn:hover {
  background: red;
  transform: scale(1.1);
}

/* Loading state */
.loading {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(10, 31, 46, 0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
  z-index: 5;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.3s ease;
}

.loading.active {
  opacity: 1;
  visibility: visible;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid rgba(255, 255, 255, 0.1);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

/* Nút xóa trên mỗi khung ảnh */
.frame-item .delete-btn {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 28px;
  height: 28px;
  background: rgba(217, 48, 48, 0.8);
  color: white;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  line-height: 1;
  z-index: 2;
  opacity: 0;
  transform: scale(0.8);
  transition: all 0.2s ease-in-out;
}

.frame-item:hover .delete-btn {
  opacity: 1;
  transform: scale(1);
}

.delete-btn:hover {
  background: rgb(239, 68, 68);
  transform: scale(1.15) rotate(90deg) !important;
}

/* Range slider styles - CẢI THIỆN cho mobile */
.range-container {
  position: relative;
  width: 100%;
  margin: 15px 0;
}

.range-value {
  position: absolute;
  top: -25px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--primary);
  color: white;
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 12px;
  font-weight: bold;
}

/* Animations */
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

@keyframes fade-in {
  from {
    opacity: 0;
    transform: translateY(8px);
  }

  to {
    opacity: 1;
    transform: none;
  }
}

@keyframes bounce {

  0%,
  100% {
    transform: translateY(0);
  }

  50% {
    transform: translateY(-3px);
  }
}

@keyframes elevate {
  from {
    transform: translateY(10px);
    opacity: 0;
  }

  to {
    transform: none;
    opacity: 1;
  }
}

/* ===== RESPONSIVE STYLES - CẢI THIỆN CHO MOBILE ===== */

/* Màn hình lớn */
@media (min-width: 1400px) {
  .container {
    max-width: 1200px;
  }

  .editor {
    grid-template-columns: 1fr 340px;
  }
}

/* Tablet */
@media (max-width: 980px) {
  .editor {
    grid-template-columns: 1fr;
  }

  /* 
  .controls {
    order: -1;
  } */
}

/* Tablet nhỏ */
@media (max-width: 768px) {
  .container {
    padding: 0 12px;
  }

  .editor {
    gap: 12px;
  }

  .canvas-wrap {
    padding: 10px;
  }

  .frame-grid {
    grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
    gap: 12px;
  }

  .app-header {
    padding: 14px 16px;
  }

  .brand h1 {
    font-size: 18px;
  }

  .card {
    padding: 14px;
  }

  .control.grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    /* 3 cột thay vì 2 */
    gap: 8px;
    /* Giảm khoảng cách giữa các nút */
  }

  /* Điều chỉnh thanh trượt cho tablet */
  input[type="range"] {
    height: 28px;
  }
}

/* Điện thoại */
@media (max-width: 600px) {
  .container {
    margin: 16px auto;
    padding: 0 10px;
  }

  .app-header {
    padding: 12px 14px;
    flex-direction: column;
    text-align: center;
    gap: 10px;
  }

  .brand {
    margin-bottom: 8px;
    justify-content: center;
  }

  .github {
    margin-top: 8px;
  }

  .card {
    padding: 12px;
    border-radius: 16px;
    margin-bottom: 16px;
  }

  .frame-grid {
    grid-template-columns: repeat(auto-fill, minmax(90px, 1fr));
    gap: 10px;
  }

  .uploader {
    flex-direction: column;
  }

  .controls {
    gap: 10px;
  }

  .btn {
    width: 100%;
    text-align: center;
    padding: 14px 16px;
    min-height: 48px;
  }

  .footer {
    padding: 15px 0 30px;
    font-size: 13px;
  }

  /* Điều chỉnh thanh trượt cho mobile */
  .range-container {
    margin: 20px 0;
  }

  input[type="range"] {
    height: 32px;
    /* Tăng kích thước cho dễ chạm */
    padding: 0 5px;
  }

  .range-value {
    top: -30px;
    font-size: 13px;
    padding: 4px 10px;
  }

  /* Đảm bảo các input range dễ sử dụng trên mobile */
  input[type="range"]::-webkit-slider-thumb {
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: var(--primary);
    cursor: pointer;
    -webkit-appearance: none;
  }

  input[type="range"]::-moz-range-thumb {
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: var(--primary);
    cursor: pointer;
    border: none;
  }
}

/* Điện thoại nhỏ */
@media (max-width: 380px) {
  .frame-grid {
    grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
    gap: 8px;
  }

  .brand h1 {
    font-size: 16px;
  }

  .logo-spin {
    width: 22px;
    height: 22px;
  }

  .btn {
    font-size: 14px;
    padding: 12px 14px;
  }

  .card h2 {
    font-size: 16px;
  }

  /* Thu nhỏ thanh trượt trên màn hình rất nhỏ */
  input[type="range"] {
    height: 28px;
  }

  input[type="range"]::-webkit-slider-thumb {
    width: 20px;
    height: 20px;
  }

  input[type="range"]::-moz-range-thumb {
    width: 20px;
    height: 20px;
  }
}

/* Điều chỉnh cho màn hình rất nhỏ (iPhone 5/SE) */
@media (max-width: 320px) {
  .container {
    padding: 0 8px;
  }

  .frame-grid {
    grid-template-columns: repeat(auto-fill, minmax(70px, 1fr));
  }

  .btn {
    padding: 10px 12px;
    min-height: 44px;
    font-size: 13px;
  }
}

/* Cải thiện khả năng truy cập và trải nghiệm người dùng trên mobile */
@media (hover: none) {

  /* Ẩn hiệu ứng hover trên thiết bị cảm ứng */
  .frame-item:hover {
    transform: none;
  }

  .frame-item .delete-btn {
    opacity: 1;
    /* Luôn hiển thị nút xóa trên mobile */
    transform: scale(0.9);
  }

  .frame-item .delete-btn:active {
    transform: scale(1.1) rotate(90deg);
  }

  .btn:hover {
    transform: none;
  }

  .btn:active {
    transform: scale(0.97);
  }
}

/* Đảm bảo nội dung không bị che trên mobile */
@media (max-height: 700px) and (orientation: landscape) {
  .app-header {
    position: relative;
  }

  .container {
    margin: 10px auto;
  }

  .card {
    margin-bottom: 12px;
    padding: 10px;
  }

  .frame-grid {
    grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
    gap: 8px;
  }
}

/* Cải thiện trải nghiệm cảm ứng */
.canvas-wrap {
  position: relative;
  touch-action: none;
  /* Ngăn chặn scroll mặc định trên canvas */
  user-select: none;
  /* Ngăn chọn văn bản khi zoom */
}

/* Hiệu ứng khi đang zoom */
.canvas-container {
  cursor: grab;
}

.canvas-container:active {
  cursor: grabbing;
}

/* Ẩn thanh cuộn cho phần tử canvas */
.canvas-container::-webkit-scrollbar {
  display: none;
}

#uploadsGallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 10px;
  margin-top: 10px;
}

#uploadsGallery img.thumb {
  width: 100%;
  height: auto;
  border-radius: 8px;
  object-fit: cover;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
  cursor: pointer;
  transition: transform 0.2s ease;
}

#uploadsGallery img.thumb:hover {
  transform: scale(1.05);
}

.thumb {
  max-width: 200px;
  max-height: 150px;
  object-fit: cover;
  transition: filter 0.5s ease;
}

.thumb.loading {
  filter: blur(15px);
}