* { box-sizing: border-box; margin: 0; padding: 0; }

/* ✅ Responsive Images & Canvas: Ensure they never overflow their container */
img, canvas, video { max-width: 100%; height: auto; }

/* ================= Header ================= */
header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
  color: #fff;
  padding: 15px 30px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.15);
  position: sticky;
  top: 0;
  z-index: 1000;
}
.logo {
  display: flex;
  align-items: center;
  gap: 15px;
  font-family: 'Khmer OS Muol Light', sans-serif;
  font-size: 1.8rem;
  font-weight: bold;
  color: #fff;
  flex-shrink: 0;
}
.logo span {
  animation: logoAnimate 3s infinite alternate;
}
@keyframes logoAnimate {
  0% { color: #fff; text-shadow: none; }
  50% { color: #f1c40f; text-shadow: 0 0 5px rgba(241, 196, 15, 0.5); }
  100% { color: #fff; text-shadow: none; }
}
.logo img {
  height: 75px;
  width: auto;
  filter: drop-shadow(0 2px 4px rgba(0,0,0,0.2));
}

/* ================= Nav ================= */
nav {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 5px;
}
nav button {
  font-family: 'Battambang', 'Khmer OS Battambang', sans-serif;
  margin-left: 8px;
  padding: 8px 20px;
  border: none;
  border-radius: 50px;
  background: rgba(255, 255, 255, 0.2);
  color: #fff;
  cursor: pointer;
  font-weight: bold;
  transition: background .2s, transform .2s;
}
nav button.active { background: #fff; color: #1e3c72; font-weight: bold; box-shadow: 0 2px 5px rgba(0,0,0,0.2); }
nav button:hover  { background: rgba(255, 255, 255, 0.3); transform: translateY(-2px); }
nav button:active { transform: translateY(0) scale(0.95); }

/* ================= Body & Footer ================= */
body {
  font-family: Arial, sans-serif;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  transition: background-color 0.3s, color 0.3s;
  overflow-x: hidden; /* Prevent horizontal scroll */
}
main { flex: 1; padding: 20px; }
footer {
  background: #34495e;
  color: #fff;
  padding: 10px;
  text-align: center;
  font-weight: bold;
}

/* ================= Controls ================= */
.controls {
  margin-bottom: 20px;
  width: 100%;
  max-width: 500px; /* Limit width on large screens */
  max-width: 100%;  /* ការពារកុំឱ្យលើសអេក្រង់ទូរស័ព្ទ */
}
.controls textarea {
  width: 100%;
  height: 100px;
  padding: 12px 15px;
  font-family: 'Khmer OS Battambang', sans-serif;
  font-size: 16px;
  border: 2px solid #e0e0e0;
  border-radius: 12px;
  /* បន្ថែមរូបភាព Background (ដាក់រូបឈ្មោះ textarea_bg.jpg) */
  background: url('textarea_bg.jpg') center/cover no-repeat, linear-gradient(180deg, #ffffff 0%, #f0f4f8 100%);
  color: #000;
  resize: vertical;
  font-weight: bold;
  transition: all 0.3s ease;
  box-shadow: inset 0 2px 4px rgba(0,0,0,0.03);
}
.controls textarea:focus {
  border-color: #3498db;
  background-color: #fff;
  outline: none;
  box-shadow: 0 0 20px rgba(52, 152, 219, 0.6), 0 0 10px rgba(52, 152, 219, 0.4);
  transform: scale(1.02);
}
.controls textarea::placeholder {
  color: #aaa;
  font-style: italic;
}
.controls .buttons {
  display: flex;
  gap: 10px;
  margin-top: 1%;
  flex-wrap: wrap; /* ⭐ បន្ថែមឱ្យប៊ូតុងចុះបន្ទាត់ពេលអេក្រង់តូច */
  justify-content: center;
}
.controls .buttons button {
  font-family: 'Khmer OS Battambang', sans-serif;
  flex: 1;
  padding: 10px 18px;
  background: #2980b9;
  color: #000;
  border: none;
  border-radius: 999px;
  cursor: pointer;
  transition: background .2s, transform .2s;
  white-space: nowrap; /* ការពារអក្សរធ្លាក់បន្ទាត់ក្នុងប៊ូតុង */
}
.controls .buttons button:hover {
  background: #1abc9c;
  transform: translateY(-2px);
  filter: brightness(1.2);
}
.controls .buttons button:active { transform: translateY(0) scale(0.95); }
.total-count {
  font-family: 'Khmer OS Battambang', sans-serif;
  font-weight: bold;
  color: #000;
  background-color: #ecf0f1;
  padding: 8px 16px;
  border-radius: 20px;
  border: 1px solid #bdc3c7;
  box-shadow: 0 2px 4px rgba(0,0,0,0.05);
  align-self: center;
  white-space: nowrap;
  font-size: 16px;
}

/* ================= Saved Lists Dropdown (ប្រអប់ជ្រើសរើសថ្នាក់) ================= */
.saved-lists-manager select {
  width: 100%;
  padding: 12px 20px;
  font-family: 'Khmer OS Battambang', sans-serif;
  font-size: 16px;
  font-weight: bold;
  color: #000;
  background-color: #fff;
  border: 2px solid #3498db;
  border-radius: 50px; /* មូលស្អាត */
  cursor: pointer;
  outline: none;
  box-shadow: 0 4px 10px rgba(0,0,0,0.1);
  transition: all 0.3s ease;
  appearance: none;
  -webkit-appearance: none;
  -moz-appearance: none;
}
.saved-lists-manager select:hover { border-color: #2980b9; box-shadow: 0 6px 15px rgba(0,0,0,0.15); transform: translateY(-2px); }
.saved-lists-manager select:focus { 
  border-color: #2ecc71; 
  box-shadow: 0 0 0 4px rgba(46, 204, 113, 0.2), 0 0 25px rgba(46, 204, 113, 0.6); 
  animation: bounceInput 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}
@keyframes bounceInput {
  0% { transform: scale(1); }
  50% { transform: scale(1.05); }
  70% { transform: scale(0.98); }
  100% { transform: scale(1); }
}

/* Wrapper for Animated Arrow */
.custom-select-wrapper {
  position: relative;
  width: 100%;
  margin-bottom: 15px;
}
.custom-select-wrapper::after {
  content: '';
  position: absolute;
  top: 50%;
  right: 20px;
  transform: translateY(-50%);
  width: 20px;
  height: 20px;
  background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%233498db' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3e%3cpolyline points='6 9 12 15 18 9'%3e%3c/polyline%3e%3c/svg%3e");
  background-size: contain;
  background-repeat: no-repeat;
  pointer-events: none;
  transition: transform 0.4s cubic-bezier(0.68, -0.55, 0.265, 1.55);
}
.custom-select-wrapper:focus-within::after {
  transform: translateY(-50%) rotate(180deg);
}

/* ================= Group Config ================= */
.group-config {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 15px;
  margin-top: 20px;
  padding: 15px;
  background-color: #f8f9fa;
  border-radius: 12px;
  border: 1px solid #e0e0e0;
}
.group-config label,
.group-config button {
  font-family: 'Khmer OS Battambang', sans-serif;
  font-weight: bold;
  color: #000;
}
.group-config input {
  width: 130px; /* បង្កើនប្រវែងប្រហែល 1.5cm */
  padding: 10px 15px;
  border: 2px solid #3498db;
  border-radius: 30px; /* រចនាមូលស្អាត */
  background: #fff;
  box-shadow: 0 4px 6px rgba(0,0,0,0.1);
  font-family: 'Khmer OS Battambang', sans-serif;
  font-size: 18px;
  font-weight: bold;
  color: #000;
  text-align: center;
  transition: all 0.3s ease;
}
.group-config input:focus {
  border-color: #2ecc71;
  outline: none;
  box-shadow: 0 0 15px rgba(46, 204, 113, 0.5);
  transform: scale(1.05);
}
.group-config button {
  padding: 6px 15px;
  background: #27ae60;
  color: #000;
  border: none;
  border-radius: 999px;
  font-size: 16px;
  cursor: pointer;
  transition: background .2s, transform .2s;
}
.group-config button:hover {
  background: #1e8449;
  transform: translateY(-2px);
  filter: brightness(1.2);
}
.group-config button:active { transform: translateY(0) scale(0.95); }

/* ================= Spin Layout ================= */
.spin-content {
  display: flex;
  gap: 30px;
  align-items: flex-start;
}

/* ================= Table Scroll Wrapper (ប្រអប់រមូរតារាង) ================= */
.table-scroll-wrapper {
  max-height: 200px; /* កម្ពស់សម្រាប់បង្ហាញប្រហែល ៣ ឈ្មោះ */
  overflow-y: auto;
  border-radius: 12px;
  box-shadow: 0 10px 20px rgba(0,0,0,0.08);
  background-color: #fff;
  margin-bottom: 15px;
  border: 1px solid #f0f0f0;
}
/* Custom Scrollbar */
.table-scroll-wrapper::-webkit-scrollbar { width: 8px; }
.table-scroll-wrapper::-webkit-scrollbar-track { background: #f1f1f1; border-radius: 0 12px 12px 0; }
.table-scroll-wrapper::-webkit-scrollbar-thumb { background: #ccc; border-radius: 4px; }
.table-scroll-wrapper::-webkit-scrollbar-thumb:hover { background: #aaa; }

/* ================= Tables ================= */
#namesTable,
#groupNamesTable,
#groupNamesTable2,
#fishingNamesTable {
  border-collapse: separate;
  border-spacing: 0;
  table-layout: fixed;
  width: auto;
  background-color: #fff;
  width: 100%; /* Make table full width of container */
  border: 1px solid #000;
}
#namesTable th, #namesTable td,
#groupNamesTable th, #groupNamesTable td,
#groupNamesTable2 th, #groupNamesTable2 td,
#fishingNamesTable th, #fishingNamesTable td {
  padding: 10px 12px;
  border-bottom: 1px solid #ccc;
  border-right: 1px solid #ccc;
  font-family: 'Khmer OS Battambang', sans-serif;
  font-size: 16px;
  color: #333;
  font-weight: bold;
}
#namesTable thead th,
#groupNamesTable thead th,
#groupNamesTable2 thead th,
#fishingNamesTable thead th {
  background: #fff;
  color: #333;
  font-weight: bold;
  border-bottom: none;
  border-right: 1px solid #ccc;
  position: sticky; /* ធ្វើឱ្យក្បាលតារាងនៅជាប់ខាងលើ */
  top: 0;
  z-index: 10; /* បង្កើន z-index ដើម្បីឱ្យនៅពីលើជួរដេកជានិច្ច */
  text-shadow: none;
  box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
/* Remove right border from last column */
#namesTable th:last-child, #namesTable td:last-child,
#groupNamesTable th:last-child, #groupNamesTable td:last-child,
#groupNamesTable2 th:last-child, #groupNamesTable2 td:last-child,
#fishingNamesTable th:last-child, #fishingNamesTable td:last-child {
  border-right: none;
}
#namesTable tr:nth-child(even),
#groupNamesTable tr:nth-child(even),
#groupNamesTable2 tr:nth-child(even),
#fishingNamesTable tr:nth-child(even) {
  background: #eef6fa;
}
#namesTable tr:nth-child(odd),
#groupNamesTable tr:nth-child(odd),
#groupNamesTable2 tr:nth-child(odd),
#fishingNamesTable tr:nth-child(odd) {
  background: #fff;
}
#namesTable tr:hover,
#groupNamesTable tr:hover,
#groupNamesTable2 tr:hover,
#fishingNamesTable tr:hover {
  background: #ffe082;
  transform: scale(1.01);
  box-shadow: 0 5px 15px rgba(0,0,0,0.1);
  position: relative;
  z-index: 2;
  font-weight: bold;
  color: #bf360c;
  transition: all 0.2s ease;
}

/* Column widths */
#namesTable th:nth-child(1), #namesTable td:nth-child(1),
#groupNamesTable th:nth-child(1), #groupNamesTable td:nth-child(1),
#groupNamesTable2 th:nth-child(1), #groupNamesTable2 td:nth-child(1),
#fishingNamesTable th:nth-child(1), #fishingNamesTable td:nth-child(1) {
  width: 15%;
  min-width: 50px;
  text-align: center;
}
#namesTable th:nth-child(2), #namesTable td:nth-child(2),
#groupNamesTable th:nth-child(2), #groupNamesTable td:nth-child(2),
#groupNamesTable2 th:nth-child(2), #groupNamesTable2 td:nth-child(2),
#fishingNamesTable th:nth-child(2), #fishingNamesTable td:nth-child(2) { 
  width: auto;
  font-weight: bold;
  font-size: 16px;
  color: #000;
}
#namesTable th:nth-child(3), #namesTable td:nth-child(3),
#groupNamesTable th:nth-child(3), #groupNamesTable td:nth-child(3),
#groupNamesTable2 th:nth-child(3), #groupNamesTable2 td:nth-child(3),
#fishingNamesTable th:nth-child(3), #fishingNamesTable td:nth-child(3) { width: 30%; min-width: 110px; }

/* ================= Wheel ================= */
.wheel-wrapper {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 20px;
  min-height: 400px;
}
.wheel-container {
  position: relative;
  /* Removed hardcoded transforms for responsiveness */
}
#wheelCanvas {
  border: 2px solid #34495e;
  border-radius: 50%;
}
#spinButton {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 50px;
  height: 50px;
  transform: translate(-50%, -50%);
  border: none;
  border-radius: 50%;
  background: #e67e22;
  color: #fff;
  font-size: .8rem;
  font-weight: bold;
  font-family: 'Khmer OS Battambang', sans-serif;
  box-shadow: 0 4px 12px rgba(0,0,0,0.25);
  transition: transform .2s;
}
#spinButton:hover {
  transform: translate(-50%, -50%) scale(1.1);
}
#spinButton:active { transform: translate(-50%, -50%) scale(0.95); }

/* ================= Pointer ================= */
.pointer {
  position: absolute;
  top: 50%;
  right: -15px;
  transform: translateY(-50%);
  width: 0;
  height: 0;
  border-top: 12px solid transparent;
  border-bottom: 12px solid transparent;
  border-right: 30px solid #c0392b;
  filter: drop-shadow(-2px 2px 2px rgba(0,0,0,0.2));
  z-index: 20;
}

/* ================= Table Buttons ================= */
.table-buttons {
  display: flex;
  justify-content: center;
  gap: 8px;
}
.table-buttons button {
  font-family: 'Khmer OS Battambang', sans-serif;
  padding: 4px 10px;
  border: none;
  border-radius: 20px;
  color: #fff;
  font-weight: 500;
  font-size: 14px;
  cursor: pointer;
  box-shadow: 0 2px 6px rgba(0,0,0,0.1);
  transition: transform .2s, box-shadow .2s;
}
.table-buttons .btn-add {
  background: linear-gradient(135deg, #2ecc71, #27ae60);
  color: #000;
}
.table-buttons .btn-remove {
  background: linear-gradient(135deg, #e74c3c, #c0392b);
  color: #000;
}
.table-buttons button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  filter: brightness(1.2);
}
.table-buttons button:active {
  transform: translateY(0) scale(0.95);
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

/* ================= Result Panel ================= */
.result-panel {
  position: absolute;
  inset: 0;
  background: rgba(255,255,255,0.95);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}
.panel-content {
  background: #fffbe6;
  border: 2px solid #e67e22;
  border-radius: 12px;
  padding: 20px 30px;
  text-align: center;
  box-shadow: 0 4px 12px rgba(0,0,0,0.2);
}
.panel-text {
  font-family: 'Khmer OS Muol Light', sans-serif;
  font-size: 32px;
  font-weight: bold;
  letter-spacing: 1px;
  background: linear-gradient(90deg,#f1c40f,#e67e22,#f1c40f);
  background-clip: text;
  color: transparent;
  text-shadow: 2px 2px 4px rgba(255,0,0,0.2);
  animation: shimmer 2.5s infinite;
}
@keyframes shimmer {
  0% { background-position:-200%; }
  100% { background-position:200%; }
}
.panel-buttons {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin-top: 12px;
}
.panel-buttons button {
  padding: 8px 16px;
  border: none;
  border-radius: 6px;
  font-family: 'Khmer OS Muol Light', sans-serif;
  font-size: 10px;
  cursor: pointer;
}
#btnAdd    { background: #27ae60; color: #000; }
#btnRemove { background: #c0392b; color: #000; }
#btnClose  { background: #7f8c8d; color: #000; }
.panel-buttons button:hover { opacity: 0.9; }
.panel-buttons button:active { transform: scale(0.95); }
.hidden { display: none !important; }

/* ================= Group Work ================= */
.groups-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
  margin: 20px;
}
.group {
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 15px rgba(0,0,0,0.08);
  overflow: visible;
  transition: transform 0.2s;
}
.group:hover {
  transform: translateY(-5px);
}
.group-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 5px 10px;
  background: transparent;
  color: #333;
  text-shadow: none;
  border: none;
  border-bottom: 2px solid #f0f0f0;
  border-radius: 8px;
  margin: 5px;
}

/* Styles for Group Drag & Drop */
.group[draggable="true"] {
  cursor: grab;
}
.group[draggable="true"]:active {
  cursor: grabbing;
}
.group.dragging-source {
  opacity: 0.4;
  border-style: dashed;
}
.group.drop-target-highlight {
  outline: 3px dashed #3498db;
  outline-offset: -3px;
  transform: translateY(-5px) scale(1.02);
  background: rgba(52, 152, 219, 0.1);
}

/* ================= Magic Book Styles (សៀវភៅវេទមន្ត) ================= */
.magic-book-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  perspective: 1500px;
  perspective: 2000px; /* Enhanced perspective for 3D flip */
}
.magic-book {
  width: 100%;
  max-width: 1000px;
  min-height: 600px;
  background-color: #fdf5e6;
  /* Optional texture for paper feel */
  background-image: linear-gradient(to right, rgba(0,0,0,0.02) 1px, transparent 1px), linear-gradient(to bottom, rgba(0,0,0,0.02) 1px, transparent 1px);
  background-size: 20px 20px;
  border-radius: 5px;
  box-shadow: 0 20px 50px rgba(0,0,0,0.5), inset 0 0 60px rgba(0,0,0,0.1);
  display: flex;
  position: relative;
  border: 12px solid #4e342e; /* Leather cover */
  overflow: hidden;
}
.book-spine {
  position: absolute;
  left: 50%;
  top: 0;
  bottom: 0;
  width: 40px;
  background: linear-gradient(to right, rgba(0,0,0,0.2), rgba(0,0,0,0.05) 40%, rgba(0,0,0,0.05) 60%, rgba(0,0,0,0.2));
  transform: translateX(-50%);
  z-index: 10;
  border-left: 1px solid rgba(0,0,0,0.1);
  border-right: 1px solid rgba(0,0,0,0.1);
}
.book-page {
  flex: 1;
  padding: 30px 50px;
  position: relative;
  overflow-y: auto;
}
.book-page.left {
  background: linear-gradient(to right, #e3dac9 0%, #fdf5e6 15%);
  border-right: 1px solid #d7ccc8;
}
.book-page.right {
  background: linear-gradient(to left, #e3dac9 0%, #fdf5e6 15%);
  border-left: 1px solid #d7ccc8;
}
.book-controls {
  margin-top: 20px;
  display: flex;
  gap: 20px;
  align-items: center;
  background: rgba(0,0,0,0.6);
  padding: 10px 25px;
  border-radius: 50px;
  backdrop-filter: blur(5px);
}
.book-controls button {
  background: linear-gradient(135deg, #ffb74d, #ffa726);
  border: none;
  padding: 8px 20px;
  border-radius: 20px;
  cursor: pointer;
  font-weight: bold;
  color: #4e342e;
  transition: transform 0.2s;
  font-family: 'Khmer OS Battambang';
  font-size: 20px;
}
.book-controls button:hover:not(:disabled) {
  transform: scale(1.1);
  filter: brightness(1.1);
}
.book-controls button:disabled {
  background: #ccc;
  cursor: not-allowed;
  opacity: 0.7;
  transform: none;
}

/* Override Group Styles inside Magic Book */
.magic-book .group {
  background: transparent !important;
  box-shadow: none !important;
  transform: none !important;
  padding: 0 !important;
  border-radius: 0 !important;
}
.magic-book .group-header {
  background: transparent !important;
  color: #5d4037 !important;
  border: none !important;
  border-radius: 0 !important;
  border-bottom: 3px double #8d6e63 !important;
  padding: 5px 0 !important;
  margin: 0 0 15px 0 !important;
  text-shadow: none !important;
}
.magic-book .group h4 {
  font-family: 'Khmer OS Muol Light';
  font-size: 1.6rem;
  text-align: center;
  color: #3e2723;
}
.magic-book .group ul {
  padding: 0 !important;
}
.magic-book .group ul li {
  border-bottom: 1px dashed #a1887f !important;
  color: #4e342e !important;
  font-size: 18px !important;
  font-weight: bold !important;
  padding: 12px 5px !important;
  background: transparent !important;
}
.magic-book .copy-btn {
  background: #8d6e63 !important;
  color: white !important;
  box-shadow: none !important;
}

@media (max-width: 768px) {
  .magic-book {
    flex-direction: column;
    min-height: auto;
  }
  .book-spine {
    width: 100%;
    height: 15px;
    left: 0;
    top: auto;
    bottom: auto;
    position: relative;
    transform: none;
    background: linear-gradient(to bottom, rgba(0,0,0,0.2), rgba(0,0,0,0.05));
  }
  .book-page {
    padding: 20px;
    min-height: 300px;
  }
  .book-page.left { border-right: none; border-bottom: 1px solid #d7ccc8; }
  .book-page.right { border-left: none; }
}

/* ================= 3D Page Flip Animation ================= */
.flipping-page {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 50%;
  height: 100%;
  z-index: 50;
  transform-style: preserve-3d;
  pointer-events: none; /* Allow clicks to pass through during animation */
}

.flipping-page .page-content {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  padding: 30px 50px; /* Match .book-page padding */
  backface-visibility: hidden;
  background-color: #fdf5e6;
  overflow: hidden;
}

.flipping-page .page-content.front {
  z-index: 2;
}

.flipping-page .page-content.back {
  transform: rotateY(180deg);
  z-index: 1;
}

/* Styles to match Left/Right pages */
.page-content.style-left {
  background: linear-gradient(to right, #e3dac9 0%, #fdf5e6 15%);
  border-right: 1px solid #d7ccc8;
}
.page-content.style-right {
  background: linear-gradient(to left, #e3dac9 0%, #fdf5e6 15%);
  border-left: 1px solid #d7ccc8;
}

/* Animation Keyframes */
@keyframes flipRightToLeft {
  from { transform: rotateY(0deg); }
  to { transform: rotateY(-180deg); }
}
@keyframes flipLeftToRight {
  from { transform: rotateY(0deg); }
  to { transform: rotateY(180deg); }
}

.animate-flip-next {
  left: 50%;
  transform-origin: left center;
  animation: flipRightToLeft 0.8s cubic-bezier(0.645, 0.045, 0.355, 1) forwards;
}
.animate-flip-prev {
  left: 0;
  transform-origin: right center;
  animation: flipLeftToRight 0.8s cubic-bezier(0.645, 0.045, 0.355, 1) forwards;
}

@media (max-width: 768px) {
  .flipping-page { display: none; } /* Disable 3D flip on mobile */
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}
.color-picker-btn {
  width: 28px;
  height: 28px;
  padding: 0;
  border: 2px solid rgba(255,255,255,0.8);
  border-radius: 50%;
  background: none;
  cursor: pointer;
  overflow: hidden;
  box-shadow: 0 2px 5px rgba(0,0,0,0.2);
}
.color-picker-btn::-webkit-color-swatch-wrapper { padding: 0; }
.color-picker-btn::-webkit-color-swatch { border: none; border-radius: 50%; }

.group h4 {
  margin: 0;
  padding: 0;
  background: none;
  font-family: 'Khmer OS Muol Light', sans-serif;
  text-align: left;
  font-size: 20px;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.copy-btn {
  background: rgba(255, 255, 255, 0.2);
  border: none;
  border-radius: 6px;
  color: #000;
  cursor: pointer;
  padding: 5px 10px;
  font-size: 14px;
  transition: background 0.2s;
}
.copy-btn:hover {
  background: rgba(255, 255, 255, 0.4);
}
.copy-btn:active {
  transform: scale(0.95);
}
.group ul {
  list-style: none;
  padding: 15px;
}
.group ul li {
  padding: 6px 8px;
  margin-bottom: 0;
  font-family: 'Khmer OS Battambang', sans-serif;
  font-size: 18px;
  font-weight: bold;
  color: #2c3e50;
  border-bottom: 1px solid #f0f0f0;
}
.group ul li:last-child { border-bottom: none; }

/* ================= Save Buttons ================= */
.save-actions {
  display: flex;
  gap: 14px;
  margin: 20px;
  flex-wrap: wrap;
}

#saveImage,
#savePdf,
#saveImage2,
#savePdf2,
#fullscreenBtn,
#zoomInBtn, #zoomOutBtn,
#zoomInBtn2, #zoomOutBtn2 {
  font-family: 'Khmer OS Battambang', sans-serif;
  padding: 10px 22px;
  font-size: 16px;
  border: none;
  border-radius: 999px;
  cursor: pointer;
  box-shadow: 0 4px 10px rgba(0,0,0,0.15);
  transition: all 0.25s ease;
}

#saveImage, #saveImage2 {
  background: linear-gradient(135deg, #27ae60, #2ecc71);
  color: #000;
}
#saveImage:hover, #saveImage2:hover {
  background: linear-gradient(135deg, #1e8449, #27ae60);
  transform: translateY(-2px);
}
#saveImage:active, #saveImage2:active { transform: translateY(0) scale(0.95); }

#savePdf, #savePdf2 {
  background: linear-gradient(135deg, #c0392b, #e74c3c);
  color: #000;
}
#savePdf:hover, #savePdf2:hover {
  background: linear-gradient(135deg, #922b21, #c0392b);
  transform: translateY(-2px);
}
#savePdf:active, #savePdf2:active { transform: translateY(0) scale(0.95); }

#fullscreenBtn {
  background: linear-gradient(135deg, #8e44ad, #9b59b6);
  color: #000;
}
#fullscreenBtn:hover {
  background: linear-gradient(135deg, #71368a, #8e44ad);
  transform: translateY(-2px);
}
#fullscreenBtn:active { transform: translateY(0) scale(0.95); }

/* Zoom Buttons Style */
.fullscreen-zoom-controls {
  display: none; /* Hidden by default */
  position: fixed;
  bottom: 30px;
  right: 30px;
  gap: 15px;
  z-index: 2147483647; /* Top of everything */
}

#groupWrapper:fullscreen .fullscreen-zoom-controls,
#groupWrapper2:fullscreen .fullscreen-zoom-controls,
#timerSection:fullscreen .fullscreen-zoom-controls {
  display: flex;
}

.fullscreen-zoom-controls button {
  background: linear-gradient(135deg, #17a2b8, #00f260);
  color: #000;
  width: 60px;
  height: 60px;
  border-radius: 50%;
  font-size: 24px;
  border: 2px solid white;
  box-shadow: 0 5px 15px rgba(0,0,0,0.3);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

/* Reset Zoom Button Color */
#zoomResetBtn, #zoomResetBtn2 {
  background: linear-gradient(135deg, #f2994a, #f2c94c);
}

/* ================= FULLSCREEN GROUP MODE ================= */
#groupWrapper:fullscreen {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 40px;
  overflow-y: auto;
  display: block;
}

#groupWrapper:fullscreen #groupsContainer {
  width: 100%;
  min-height: 100vh;
  grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
  gap: 40px;
  padding: 20px;
  box-sizing: border-box;
  align-content: center;
}

/* ================= FULLSCREEN TIMER MODE ================= */
#timerSection:fullscreen {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
}

/* Timer Inputs Responsive Fix */
#timerHourInput, #timerMinuteInput, #timerSecondInput {
  width: auto !important; /* Override inline styles */
  min-width: 70px;
  flex: 1;
}

/* ================= Export Button ================= */
#exportNames,
#exportGroupNames,
#exportGroupNames2,
#exportFishingNames {
  margin-top: 15px;
  font-family: 'Khmer OS Battambang', sans-serif;
  padding: 10px 22px;
  background: linear-gradient(135deg, #27ae60, #2ecc71);
  color: #000;
  border: none;
  border-radius: 999px;
  cursor: pointer;
  box-shadow: 0 4px 10px rgba(0,0,0,0.15);
  transition: all 0.25s ease;
}
#exportNames:hover,
#exportGroupNames:hover,
#exportGroupNames2:hover,
#exportFishingNames:hover {
  background: linear-gradient(135deg, #1e8449, #27ae60);
  transform: translateY(-2px);
}
#exportNames:active,
#exportGroupNames:active,
#exportGroupNames2:active,
#exportFishingNames:active { transform: translateY(0) scale(0.95); }

/* ================= Timer Presets ================= */
.preset-btn {
  font-family: 'Khmer OS Battambang', sans-serif;
  padding: 10px 25px;
  font-size: 16px;
  border-radius: 50px;
  border: none;
  background: #3498db;
  color: #000;
  cursor: pointer;
  box-shadow: 0 4px 6px rgba(0,0,0,0.1);
  transition: transform 0.2s, background 0.2s;
}
.preset-btn:hover { transform: translateY(-2px); background: #2980b9; }
.preset-btn:active { transform: translateY(0) scale(0.95); }

/* ================= Timer Section Alignment ================= */
#timerSection {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 70vh; /* ដាក់ឱ្យនៅកណ្តាលអេក្រង់ (បញ្ឈរ) */
}
.timer-controls {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center; /* ដាក់ឱ្យនៅកណ្តាល (ផ្តេក) */
  text-align: center;
}

/* ================= Responsive ================= */
@media (max-width: 1024px) {
  /* ✅ Stack content vertically on tablets and smaller screens */
  .spin-content { 
    flex-direction: column; 
    align-items: center;
  }
  .controls {
    max-width: 100%;
  }
  .wheel-wrapper {
    width: 100%;
    margin-bottom: 50px;
  }
  /* Group Section Layout Fix */
  /* ✅ Override inline width styles (35%) to force full width on mobile */
  .responsive-col,
  #groupSection .spin-content > div,
  .spin-content > div {
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
    flex: none !important; /* Disable flex sizing to allow stacking */
  }
}

@media (max-width: 768px) {
  /* ✅ Header adjustments for mobile */
  header {
    flex-direction: column;
    gap: 15px;
    text-align: center;
    padding: 15px 10px;
  }
  nav {
    width: 100%;
    /* ✅ Switch to wrapping layout for mobile (No horizontal scroll) */
    overflow-x: visible;
    white-space: normal;
    flex-wrap: wrap;
    justify-content: center;
      padding: 0 20px;
      min-width: 200px; /* ⭐ កំណត់ទំហំឱ្យស្មើគ្នា */
      height: auto;     /* ⭐ Allow height to grow with content */
      min-height: 50px;
      padding-bottom: 10px;
    gap: 10px;
  }

  nav button { 
    font-size: 14px; 
    padding: 10px 15px; 
    /* ✅ Allow buttons to grow and fill space evenly */
    flex: 1 1 auto;
    min-width: 110px; /* Ensure readable width */
    margin: 2px;
    box-shadow: 0 3px 6px rgba(0,0,0,0.15); /* Better shadow for mobile */
  }

  .logo {
    font-size: 1.4rem;
    justify-content: center;
  }
  .logo img {
    height: 50px;
  }
  .groups-grid { 
    grid-template-columns: 1fr; 
  }
  .timer-controls {
    padding: 20px;
    width: 95%;
  }
  #timerDisplay {
    font-size: 15vw !important; /* ប្រើ VW ដើម្បីឱ្យអក្សរម៉ោងធំតាមទទឹងអេក្រង់ */
    padding: 15px 20px !important;
    width: 90%;
    word-break: break-all;
  }
  
  /* ✅ Ensure control buttons wrap and have touch-friendly size */
  .controls .buttons button {
    min-width: 100px;
    margin-bottom: 5px;
  }
  
  /* ✅ Center save actions on mobile */
  .save-actions {
    justify-content: center;
  }

  /* ✅ Responsive Tables: Enable horizontal scrolling on small screens */
  #namesTable, #groupNamesTable, #groupNamesTable2, #fishingNamesTable {
    display: block;
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch; /* Smooth scrolling on iOS */
  }

  /* ✅ Fix Result Panel on Mobile (Override HTML inline styles) */
  .panel-content {
    min-width: auto !important;
    width: 90% !important;
    padding: 30px !important;
  }
  #panelResult {
    font-size: 12vw !important; /* Responsive font size for winner name */
  }
  .panel-buttons {
    flex-wrap: wrap;
  }
  .panel-buttons button {
    width: 100%;
    margin: 5px 0;
  }

  /* ✅ Fix Fishing Container Width on Mobile */
  #fishingSection .fishing-container {
    width: 95% !important; /* Override calc(100% - 4cm) */
    height: 50vh !important;
  }
}

/* ================= Mobile Small (Phone) ================= */
@media (max-width: 480px) {
  /* ✅ Adjust padding for small mobile screens */
  main { padding: 10px; }
  
  .logo span { font-size: 1.1rem; }
  .logo img { height: 40px; }

  #landingPage h1 {
    font-size: 1.8rem; /* បន្ថយទំហំចំណងជើងលើទូរស័ព្ទតូច */
  }
  
  /* Force 1 column for groups on small phones */
  .groups-grid { grid-template-columns: 1fr; }
  
  /* Adjust Timer Font */
  #timerDisplay { font-size: 3rem !important; }
  
  /* Adjust Fishing Container Height */
  #fishingSection .fishing-container { height: 40vh; }
  
  /* Smaller buttons in controls */
  .controls .buttons button { padding: 8px 12px; font-size: 13px; flex: 1 1 auto; }
  
  /* Adjust Nav Icons spacing */
  .nav-icons { gap: 10px !important; }
  .icon-btn { width: 35px !important; height: 35px !important; font-size: 1rem !important; }
  
  /* Wheel wrapper height adjustment */
  .wheel-wrapper { min-height: 300px; }

  /* Magic Book Mobile Fix */
  .magic-book {
    border-width: 5px; /* បន្ថយគែមសៀវភៅ */
    min-height: 400px;
  }

  /* ✅ Timer Inputs on Small Screens */
  #timerSection .buttons input {
    width: 70px !important;
    font-size: 1.2rem !important;
  }
}

/* ================= Dark Mode ================= */
body.dark-mode {
  background-color: #121212;
  color: #ffffff;
}
body.dark-mode .modal-content,
body.dark-mode .modal-header {
  background-color: #1e1e1e;
  color: #e0e0e0;
  border-color: #333;
}

/* Dark Mode Button Text Override */
body.dark-mode .controls .buttons button,
body.dark-mode .group-config button,
body.dark-mode .table-buttons button,
body.dark-mode .panel-buttons button,
body.dark-mode .save-actions button,
body.dark-mode .preset-btn,
body.dark-mode .fullscreen-zoom-controls button,
body.dark-mode #exportNames,
body.dark-mode #exportGroupNames,
body.dark-mode #exportGroupNames2,
body.dark-mode #exportFishingNames {
  color: #ffffff !important;
}
body.dark-mode .modal-header h2 { color: #fff; }
body.dark-mode .close-btn { color: #aaa; }
body.dark-mode footer { background-color: #000; }

/* Header Dark Mode */
body.dark-mode header { background-color: #1e1e1e; color: #fff; }
body.dark-mode nav button { background-color: #2d2d2d; color: #e0e0e0; }
body.dark-mode nav button:hover { background-color: #3d3d3d; }
body.dark-mode nav button.active { background-color: #0062E6; color: #fff; }

/* Controls & Inputs Dark Mode */
body.dark-mode .group-config input,
body.dark-mode #timerHourInput,
body.dark-mode #timerMinuteInput,
body.dark-mode #timerSecondInput {
  background-color: #2d2d2d !important;
  color: #ffffff !important;
  border-color: #444 !important;
}
/* Saved Lists Dropdown Dark Mode */
body.dark-mode .saved-lists-manager select {
  background-color: #2d2d2d;
  color: #fff;
  border-color: #555;
}
body.dark-mode .custom-select-wrapper::after {
  background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23ffffff' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3e%3cpolyline points='6 9 12 15 18 9'%3e%3c/polyline%3e%3c/svg%3e");
}

body.dark-mode .controls textarea {
  background-color: #2d2d2d;
  color: #ffffff;
  border-color: #444;
}
body.dark-mode .group-config {
  background-color: #1e1e1e;
  border-color: #333;
}
body.dark-mode .group-config label { color: #ccc; }
body.dark-mode .total-count {
  background-color: #2d2d2d;
  color: #fff;
  border-color: #444;
}

/* Tables Dark Mode */
body.dark-mode #namesTable,
body.dark-mode #groupNamesTable,
body.dark-mode #groupNamesTable2,
body.dark-mode #fishingNamesTable {
  background-color: #1e1e1e;
  color: #e0e0e0;
}
body.dark-mode #namesTable td,
body.dark-mode #groupNamesTable td,
body.dark-mode #groupNamesTable2 td,
body.dark-mode #fishingNamesTable td {
  border-bottom-color: #333;
  color: #ccc;
}
body.dark-mode #namesTable tr:nth-child(even),
body.dark-mode #groupNamesTable tr:nth-child(even),
body.dark-mode #groupNamesTable2 tr:nth-child(even),
body.dark-mode #fishingNamesTable tr:nth-child(even) { background-color: #252525; }
body.dark-mode #namesTable tr:nth-child(odd),
body.dark-mode #groupNamesTable tr:nth-child(odd),
body.dark-mode #groupNamesTable2 tr:nth-child(odd),
body.dark-mode #fishingNamesTable tr:nth-child(odd) { background-color: #1e1e1e; }

/* Cards & Timer Dark Mode */
body.dark-mode .group { background-color: #1e1e1e; }
body.dark-mode .group ul li { color: #ffffff !important; border-bottom-color: #333; }
body.dark-mode .group h4 {
  background: linear-gradient(to right, #ffffff 0%, #ffeb3b 50%, #ffffff 100%);
  background-size: 200% auto;
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  animation: shimmer 3s linear infinite;
  text-shadow: 0 0 15px rgba(255, 235, 59, 0.3);
}
body.dark-mode #timerDisplay { color: #f1c40f; }

/* Magic Book Group Title Red Glow in Dark Mode */
body.dark-mode .magic-book .group h4 {
  background: linear-gradient(to right, #ff9999 0%, #ff0000 50%, #ff9999 100%);
  background-size: 200% auto;
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  text-shadow: 0 0 20px rgba(255, 0, 0, 0.6);
}

/* Magic Book Page Glow in Dark Mode */
body.dark-mode .book-page {
  animation: bookPageGlow 3s ease-in-out infinite alternate;
}
@keyframes bookPageGlow {
  from { box-shadow: inset 0 0 20px rgba(255, 223, 0, 0.2); }
  to { box-shadow: inset 0 0 40px rgba(255, 223, 0, 0.5), 0 0 15px rgba(255, 255, 255, 0.2); }
}

@media (max-width: 650px) {
  nav button { font-size: 12px; padding: 7px 9px; }
  .groups-grid { grid-template-columns: 1fr; }
}
