/* PixMe — พื้นหลัง 1920×1080 + หน้าต่างกลางแนวตั้ง */

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  min-height: 100%;
  min-height: 100dvh;
  overflow: hidden;
  /* ล็อกหน้าจอทั้งหมดไม่ให้เลื่อนขยับ */
  overscroll-behavior: none;
  touch-action: none;
}

body {
  -webkit-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

body {
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  color: #111;
  overflow-x: hidden;
}

/*
  พื้นหลัง: ใส่ไฟล์ที่ assets/bg.jpg (1920×1080)
  - ใช้ background-size: cover เพื่อให้เต็มหน้าจอโดยไม่ต้องกด F11
  - อาจมีการตัดขอบเล็กน้อยเมื่อสัดส่วนหน้าต่างไม่พอดีกับภาพ
*/
.scene-bg {
  position: fixed;
  inset: 0;
  z-index: 0;
  background-color: #2d1f3d;
  background-image: url("assets/bg.jpg");
  background-repeat: no-repeat;
  background-position: center center;
  background-size: cover;
}

.scene-front {
  position: relative;
  z-index: 1;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: max(12px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}

/* หน้าต่างแนวตั้ง สัดส่วนประมาณมือถือ + สไตล์คล้ายหน้าต่าง macOS */
.phone-window {
  width: min(100%, 550px);
  aspect-ratio: 9 / 16;
  max-height: min(88dvh, 900px);
  /* ลดขนาดลงเผื่อที่ให้ลายน้ำ ไม่ให้จอทะลุ */
  display: flex;
  flex-direction: column;
  background: rgba(255, 250, 252, 0.94);
  border-radius: 12px;
  box-shadow:
    0 0 0 1px rgba(0, 0, 0, 0.12),
    0 24px 48px rgba(30, 20, 60, 0.35),
    0 4px 12px rgba(0, 0, 0, 0.12);
  overflow: hidden;
  backdrop-filter: blur(8px);
}

.window-chrome {
  flex-shrink: 0;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
  min-height: 40px;
  padding: 8px 12px;
  background: linear-gradient(180deg, #ececec 0%, #e2e2e2 100%);
  border-bottom: 1px solid rgba(0, 0, 0, 0.12);
  user-select: none;
}

.window-traffic {
  display: flex;
  align-items: center;
  gap: 7px;
  justify-self: start;
}

.dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.15);
}

.dot-close {
  background: #ff5f57;
  border: 1px solid #e0443e;
}

.dot-min {
  background: #febc2e;
  border: 1px solid #dba524;
}

.dot-max {
  background: #28c840;
  border: 1px solid #1fa834;
}

.window-title {
  font-size: 13px;
  font-weight: 600;
  color: #333;
  letter-spacing: 0.02em;
  text-align: center;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.8);
}

.window-chrome-spacer {
  justify-self: end;
  width: 52px;
}

.window-body {
  flex: 1;
  min-height: 0;
  padding: 0 16px 16px;
  overflow-x: hidden;
  /* ล็อกห้ามเลื่อนซ้าย-ขวาเด็ดขาด */
  overflow-y: auto;
  /* อนุญาตให้เลื่อนขึ้น-ลงได้อย่างเดียว */
  touch-action: pan-y;
  background: linear-gradient(180deg, #faf8fc 0%, #f3f0f8 100%);
}

.app-logo-wrap {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  gap: 0;
  padding: 0;
}

.app-logo-swap {
  width: min(92%, 320px);
  /* 500×160 => aspect-ratio = 500 / 160 */
  aspect-ratio: 500 / 160;
  position: relative;
}

.app-logo {
  width: 100%;
  height: 100%;
  position: absolute;
  inset: 0;
  object-fit: contain;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  user-select: none;
  pointer-events: none;
}

/* ++ เพิ่มโค้ดส่วนนี้: ปิดการกระพริบของโลโก้เฉพาะในหน้าจอ Result (หน้าเซฟ) ให้เป็นภาพนิ่ง ++ */
.result-modal .app-logo-a {
  opacity: 1 !important;
  animation: none !important;
}

.result-modal .app-logo-b {
  display: none !important;
  animation: none !important;
}

.app-logo-a {
  opacity: 1;
  animation: pixme-logo-a-toggle 1s infinite;
}

.app-logo-b {
  opacity: 0;
  animation: pixme-logo-b-toggle 1s infinite;
}

@keyframes pixme-logo-a-toggle {

  0%,
  49.999% {
    opacity: 1;
  }

  50%,
  100% {
    opacity: 0;
  }
}

@keyframes pixme-logo-b-toggle {

  0%,
  49.999% {
    opacity: 0;
  }

  50%,
  100% {
    opacity: 1;
  }
}

@media (prefers-reduced-motion: reduce) {

  .app-logo-a,
  .app-logo-b {
    animation: none;
  }

  .app-logo-a {
    opacity: 1;
  }

  .app-logo-b {
    opacity: 0;
  }
}

.window-placeholder {
  margin: 0;
  font-size: 14px;
  line-height: 1.5;
  color: #555;
  text-align: center;
  padding: 0;
}

/* --- Pixel Art Font & Global Styles for New Sections --- */
.card-preview-section,
.character-editor-section {
  font-family: 'VT323', 'Chakra Petch', monospace;
  width: 100%;
}

.character-editor-section {
  margin-top: 2px;
}

.card-preview-section {
  margin-top: 0;
  position: sticky;
  top: 0;
  z-index: 40;
  background: rgba(250, 248, 252, 0.95);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  margin-left: -16px;
  margin-right: -16px;
  padding-left: 16px;
  padding-right: 16px;
  width: calc(100% + 32px);
  padding-top: 4px;
  padding-bottom: 2px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  margin-bottom: 6px;
  position: relative;
  z-index: 30;
}

.section-header h2 {
  font-size: 22px;
  margin: 0;
  color: #111;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.ratio-note {
  font-size: 14px;
  color: #666;
}

/* --- Viral Premium ID Card (Card Preview) --- */
.id-card-frame {
  background: #3f395b;
  /* สีม่วงเข้มให้ความพรีเมียม */
  border: 4px solid #111;
  border-radius: 12px;
  padding: 10px;
  box-shadow:
    inset 4px 4px 0 rgba(255, 255, 255, 0.15),
    inset -4px -4px 0 rgba(0, 0, 0, 0.3),
    0 8px 0 #111,
    0 16px 20px rgba(0, 0, 0, 0.25);
  margin: 0 auto 12px auto;
  width: 440px;
  /* บังคับขนาดการ์ดเท่าในคอมพิวเตอร์เสมอ */
  transform: rotate(-1deg);
  transition: transform 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.id-card-frame:hover {
  transform: rotate(1deg) scale(1.03);
}

.id-card-inner.holo-effect {
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.9), rgba(255, 255, 255, 0.98)),
    repeating-linear-gradient(45deg, transparent, transparent 10px, rgba(255, 200, 255, 0.05) 10px, rgba(255, 200, 255, 0.05) 20px);
  background-color: #fafafa;
  border: 3px solid #111;
  border-radius: 6px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  box-shadow: inset 0 0 16px rgba(150, 100, 255, 0.1);
}

.card-stripe {
  display: flex;
  height: 14px;
  width: 100%;
}

.card-stripe.top {
  border-bottom: 3px solid #111;
}

.card-stripe.bottom {
  border-top: 3px solid #111;
}

.stripe-color {
  flex: 1;
}

.stripe-blue {
  background: #5ce1e6;
}

.stripe-purple {
  background: #b892fc;
}

.stripe-pink {
  background: #ff8eaf;
}

.card-body {
  display: flex;
  padding: 16px;
  gap: 12px;
  align-items: center;
}

.character-container {
  position: relative;
  display: inline-block;
  width: 128px;
  height: 128px;
}

.character-avatar {
  width: 256px;
  height: 256px;
  transform: scale(0.5);
  transform-origin: top left;
  position: absolute;
  top: 0;
  left: 0;
  background: linear-gradient(180deg, #e0f2fe 0%, #bae6fd 100%);
  border: 6px solid #111;
  border-radius: 16px;
  box-shadow: inset 4px 4px 0 rgba(255, 255, 255, 0.6), 8px 8px 0 rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.watermark-logo {
  width: 66px;
  height: auto;
  image-rendering: pixelated;
  z-index: 2;
  transform: rotate(-4deg);
  margin-bottom: -6px;
  /* ดึงโลโก้ให้ชิด QR code นิดนึงเหมือนสติกเกอร์แปะทับขอบ */
  filter: drop-shadow(2px 2px 0 rgba(0, 0, 0, 0.15)) drop-shadow(-1px -1px 0 rgba(255, 255, 255, 0.8));
}

/* Layering system: this ensures your images stack neatly */
.layer-background,
.layer-face,
.layer-clothes,
.layer-hair {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-size: 100% 100%;
  background-position: center bottom;
  background-repeat: no-repeat;
  image-rendering: pixelated;
  /* นำกลับมาเพื่อบังคับรักษาขอบพิกเซลให้คมกริบ */
}

/* เพิ่มเทคนิคขยายขนาด 2px เพื่อถมรอยต่อโปร่งแสง (Anti-aliased gap) จากการวาดแยกชิ้นให้สนิท */
.layer-clothes,
.layer-hair {
  top: -1px;
  left: -1px;
  width: calc(100% + 2px);
  height: calc(100% + 2px);
}

/* ตัวอย่างใส่สีหลอกๆ ให้เห็นโครง ถ้ายังไม่มีภาพฉากหลัง */
.layer-background {
  background-color: transparent;
}

.card-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 6px;
  justify-content: center;
}

.info-group {
  display: flex;
  flex-direction: column;
  line-height: 1.1;
  gap: 4px;
}

.info-bottom-row {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
}

.info-label {
  font-size: 15px;
  color: #666;
  font-weight: normal;
  text-transform: uppercase;
  letter-spacing: 1px;
}

.info-value {
  font-size: 24px;
  color: #111;
  text-transform: uppercase;
  margin-top: 2px;
  text-shadow: 1px 1px 0 rgba(0, 0, 0, 0.1);
  word-break: break-word;
  /* รองรับชื่อยาวทะลุกรอบ */
  line-height: 1.1;
}

.pixel-star {
  color: #f59e0b;
  font-size: 20px;
  vertical-align: middle;
  text-shadow: 1px 1px 0 #111;
}

.card-qr {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 6px;
}

.qr-label {
  font-size: 14px;
  color: #f43f5e;
  font-weight: bold;
  letter-spacing: 0.5px;
  animation: blink 2s steps(2, start) infinite;
}

@keyframes blink {

  0%,
  100% {
    opacity: 1;
  }

  50% {
    opacity: 0;
  }
}

.qr-placeholder {
  width: 54px;
  height: 54px;
  border: 3px solid #111;
  border-radius: 4px;
  padding: 4px;
  box-shadow: 2px 2px 0 rgba(0, 0, 0, 0.1);
}

.qr-inner {
  width: 100%;
  height: 100%;
  /* Removed gradient placeholder for new QR image */
  background: none;
  image-rendering: pixelated;
}

.card-qr-box {
  width: 44px;
  height: 44px;
  border: 2px solid #111;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 2px 2px 0 #111;
  padding: 2px;
  overflow: hidden;
  /* ป้องกันรูปภาพล้นขอบ */
}

/* --- Character Editor --- */
.editor-tabs {
  display: flex;
  justify-content: space-between;
  border-bottom: 3px solid #111;
  position: relative;
  z-index: 10;
  background: #faf8fc;
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.1);
  gap: 0;
  margin-bottom: -3px;
  /* overlap top border of grid */
}

/* สร้างพื้นหลังทึบแอบซ่อนอยู่หลังแท็บ เพื่อบังขอบตารางเวลาเลื่อนขึ้น (mask) */
.editor-tabs::before {
  content: '';
  position: absolute;
  top: -10px;
  bottom: 0px;
  left: -24px;
  right: -24px;
  background: #faf8fc;
  z-index: -1;
}

.tab-btn {
  flex: 1;
  background: #cabee6;
  /* light purple from image */
  border: 3px solid #111;
  border-right: none;
  border-radius: 8px 8px 0 0;
  padding: 12px 4px;
  font-family: inherit;
  font-size: 15px;
  font-weight: bold;
  cursor: pointer;
  color: #333;
  text-align: center;
  transition: background-color 0.2s;
  position: relative;
  /* สำคัญ: เอาไว้อ้างอิง ::after */
}

.tab-btn:last-child {
  border-right: 3px solid #111;
}

.tab-btn.active {
  background: #cde0f5;
  border-bottom: 3px solid #111;
  /* กลับมาใช้เส้นล่างสีดำ เพื่อแก้รอยบากทะแยงมุมที่ขอบ */
}

/* ใช้ ::after ในการคลุมเส้นดำทับ เพื่อให้ได้ขอบที่ตรงเป๊ะ */
.tab-btn.active::after {
  content: "";
  position: absolute;
  bottom: -3px;
  /* เลื่อนลงมาทับเส้นกรอบล่างของตัวมันเอง */
  left: 0;
  right: 0;
  height: 3px;
  background: #cde0f5;
  /* สีเดียวกับ tab-panels */
}

.pixel-icon {
  display: inline-block;
  vertical-align: bottom;
  margin-right: 6px;
  margin-bottom: -4px;
}

.tab-panels {
  background: #cde0f5;
  /* light blue from image */
  border: 3px solid #111;
  border-radius: 0 0 12px 12px;
  position: relative;
  z-index: 1;
  box-shadow: 0 6px 0 rgba(30, 20, 60, 0.15);
  margin-bottom: 16px;
}

.editor-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  padding: 16px;
}

.color-palette {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  /* บังคับ 8 สีต่อแถว ให้เรียงสวยๆ ไม่ล้น */
  gap: 6px;
  padding-bottom: 12px;
  border-bottom: 3px dashed #111;
  margin-bottom: 8px;
  justify-items: center;
}

.color-btn {
  width: 100%;
  aspect-ratio: 1;
  border: 3px solid #111;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  box-shadow: inset 2px 2px 0 rgba(255, 255, 255, 0.3), inset -2px -2px 0 rgba(0, 0, 0, 0.2);
  transition: transform 0.1s;
}

.color-btn:hover {
  transform: translateY(-2px);
}

.color-btn.active {
  border-color: #fff;
  box-shadow: 0 0 0 3px #111, inset 2px 2px 0 rgba(255, 255, 255, 0.4), inset -2px -2px 0 rgba(0, 0, 0, 0.3);
  transform: scale(1.15);
  z-index: 2;
}

.grid-item {
  aspect-ratio: 1;
  background: #a99ec9;
  /* item purple from image */
  border: 3px solid #111;
  border-radius: 8px;
  cursor: pointer;
  box-shadow: inset -2px -2px 0 rgba(0, 0, 0, 0.15), inset 2px 2px 0 rgba(255, 255, 255, 0.2);
  transition: transform 0.1s;
}

.grid-item:hover {
  background: #9f94bd;
}

.grid-item.active {
  border: 3px solid #fb923c;
  background: #fcd5c5;
  transform: scale(1.05);
  box-shadow: none;
}

.editor-inputs {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 12px;
}

.pixel-input {
  width: 100%;
  padding: 12px;
  font-family: inherit;
  font-size: 20px;
  border: 3px solid #222;
  border-radius: 8px;
  outline: none;
  box-shadow: inset 2px 2px 0 rgba(0, 0, 0, 0.05);
}

.pixel-input:focus {
  border-color: #6a5acd;
  background: #fdf5e6;
}

.btn-randomize {
  font-family: inherit;
  font-size: 16px;
  padding: 4px 10px;
  background: #fcd5c5;
  border: 2px solid #111;
  border-radius: 6px;
  box-shadow: 0 2px 0 #111;
  cursor: pointer;
  transform: translateY(0);
  color: #111;
  font-weight: bold;
}

.btn-randomize:active {
  transform: translateY(2px);
  box-shadow: 0 0 0 #111;
}

.btn-save {
  width: 100%;
  font-family: inherit;
  font-size: 28px;
  padding: 16px;
  background: #4ade80;
  /* Vibrant pixel game green */
  color: #fff;
  text-shadow: 2px 2px 0 #111;
  /* Pixel drop shadow for text */
  border: 4px solid #111;
  border-radius: 8px;
  /* Blockier corners */
  box-shadow:
    inset -4px -4px 0 rgba(0, 0, 0, 0.2),
    inset 4px 4px 0 rgba(255, 255, 255, 0.4),
    0 6px 0 #111;
  cursor: pointer;
  text-transform: uppercase;
  margin-bottom: 24px;
  transform: translateY(0);
  font-weight: bold;
  letter-spacing: 2px;
}

.btn-save:active {
  transform: translateY(6px);
  box-shadow:
    inset -4px -4px 0 rgba(0, 0, 0, 0.2),
    inset 4px 4px 0 rgba(255, 255, 255, 0.4),
    0 0 0 #111;
}

/* ------------------------------------- */
/* RESULT MODAL STYLES (SAVE / SHARE) */
/* ------------------------------------- */
.result-modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100%;
  height: 100dvh;
  z-index: 99999;
  /* ยกระดับความสูงมหาศาลเพื่อแก้บัคปุ่มเสื้อผ้าทะลุบน Safari */
  display: flex;
  justify-content: center;
  align-items: center;
}

.result-backdrop {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(17, 17, 17, 0.85);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  z-index: 1;
}

.result-content {
  position: relative;
  z-index: 2;
  background: #faf8fc;
  padding: 32px;
  border: 6px solid #111;
  border-radius: 16px;
  box-shadow: 12px 12px 0 #111;
  display: flex;
  flex-direction: column;
  gap: 20px;
  width: max-content;
  max-width: 95vw;
  overflow: visible;
  align-items: center;
  font-family: 'VT323', 'Chakra Petch', monospace;
}

/* เพิ่มลวดลายตารางแบบ 8-bit ลงไปบนพื้นหลังของ Modal */
.result-content::before {
  content: '';
  position: absolute;
  inset: 0;
  z-index: 0;
  pointer-events: none;

  /* สีลายตารางตามแบบการ์ด: ฟ้า -> ม่วง -> ชมพู */
  background: linear-gradient(135deg, #5ce1e6 10%, #b892fc 50%, #ff8eaf 90%);
  opacity: 0.65;
  /* ปรับระดับความเข้มตาราง สีอ่อนเลยต้องเพิ่มแสงให้เห็นชัด */

  /* เจาะ Mask ให้เหลือเฉพาะเส้นตาราง */
  -webkit-mask-image:
    linear-gradient(rgba(0, 0, 0, 1) 2px, rgba(0, 0, 0, 0) 2px),
    linear-gradient(90deg, rgba(0, 0, 0, 1) 2px, rgba(0, 0, 0, 0) 2px);
  mask-image:
    linear-gradient(rgba(0, 0, 0, 1) 2px, rgba(0, 0, 0, 0) 2px),
    linear-gradient(90deg, rgba(0, 0, 0, 1) 2px, rgba(0, 0, 0, 0) 2px);
  -webkit-mask-size: 24px 24px;
  mask-size: 24px 24px;
}

.result-header,
#final-card-container,
.result-actions {
  position: relative;
  z-index: 1;
  /* ให้อยู่เหนือลวดลายตาราง */
  width: 100%;
}

.result-header {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 16px;
}

.result-title {
  font-size: 26px;
  margin: 0;
  color: #fbbf24;
  text-shadow: 2px 2px 0 #111;
  text-align: center;
  letter-spacing: 1px;
  width: 100%;
}

#final-card-container {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  overflow: hidden;
}

.btn-secondary {
  background: #e2e8f0;
  color: #111;
  font-size: 16px;
  padding: 8px 16px;
  border: 3px solid #111;
  font-family: inherit;
  box-shadow: inset 2px 2px 0 rgba(255, 255, 255, 0.7), inset -2px -2px 0 rgba(0, 0, 0, 0.1), 0 4px 0 #111;
  cursor: pointer;
}

.btn-secondary:active {
  transform: translateY(4px);
  box-shadow: inset 2px 2px 0 rgba(255, 255, 255, 0.7), inset -2px -2px 0 rgba(0, 0, 0, 0.1), 0 0 0 #111;
}

.result-actions {
  display: flex;
  flex-direction: column;
  width: 100%;
  gap: 12px;
}

.btn-primary {
  background: #22c55e;
  color: white;
  font-size: 20px;
  width: 100%;
  padding: 12px 24px;
  border: 3px solid #111;
  font-family: inherit;
  box-shadow: inset 2px 2px 0 rgba(255, 255, 255, 0.4), inset -2px -2px 0 rgba(0, 0, 0, 0.2), 0 6px 0 #111;
  border-radius: 8px;
  cursor: pointer;
}

.btn-primary:active {
  transform: translateY(6px);
  box-shadow: inset 2px 2px 0 rgba(255, 255, 255, 0.4), inset -2px -2px 0 rgba(0, 0, 0, 0.2), 0 0 0 #111;
}

.social-share {
  display: flex;
  gap: 12px;
  justify-content: center;
}

.btn-social {
  width: 60px;
  height: 60px;
  font-size: 28px;
  border-radius: 8px;
  border: 3px solid #111;
  font-family: inherit;
  box-shadow: inset 2px 2px 0 rgba(255, 255, 255, 0.3), inset -2px -2px 0 rgba(0, 0, 0, 0.2), 0 4px 0 #111;
  cursor: pointer;
}

.btn-social:active {
  transform: translateY(4px);
  box-shadow: inset 2px 2px 0 rgba(255, 255, 255, 0.3), inset -2px -2px 0 rgba(0, 0, 0, 0.2), 0 0 0 #111;
}

.fb {
  background: #3b5998;
  color: white;
}

.tw {
  background: #111;
  color: white;
}

.line {
  background: #00c300;
  color: white;
}

/* Native Responsive Modal Card Scaling */
@media (max-width: 550px) {
  .id-card-frame {
    zoom: 0.88;
  }
}

@media (max-width: 410px) {
  .id-card-frame {
    zoom: 0.78;
  }
}

@media (max-width: 350px) {
  .id-card-frame {
    zoom: 0.70;
  }
}