:root {
  --primary: #2563eb;
  --border: #e2e8f0;
  --sidebar-w: 300px;
}

body {
  margin: 0;
  font-family: "Segoe UI", sans-serif;
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: #f8fafc;
}

/* --- DESKTOP DEFAULT STYLES --- */
.mobile-only {
  display: none !important;
}

.app-container {
  display: flex;
  flex-direction: row;
  flex: 1;
  height: 100%;
  overflow: hidden;
}

.sidebar.desktop-only {
  width: var(--sidebar-w);
  background: white;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 1rem;
  gap: 1rem;
  overflow-y: auto;
  z-index: 20;
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: bold;
  border-bottom: 1px solid var(--border);
  padding-bottom: 1rem;
}
.brand img {
  height: 24px;
}

.pad-wrapper {
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  background: white;
  height: 140px;
  margin-bottom: 0.5rem;
  touch-action: none;
}
canvas {
  width: 100%;
  height: 100%;
}
.btn-block {
  width: 100%;
  padding: 10px;
  border: none;
  border-radius: 6px;
  font-weight: 600;
  cursor: pointer;
  transition: 0.2s;
}
.btn-primary {
  background: var(--primary);
  color: white;
  text-decoration: none;
}
.btn-primary:hover {
  opacity: 0.9;
}
.btn-primary:disabled {
  background: #94a3b8;
  cursor: not-allowed;
  opacity: 0.6;
}
.btn-sm {
  padding: 5px 10px;
  background: #f1f5f9;
  border: 1px solid #cbd5e1;
  border-radius: 4px;
  cursor: pointer;
}
.controls {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}
.color-picker {
  display: flex;
  gap: 5px;
}
.color-btn,
.color-btn-m {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: 2px solid transparent;
  cursor: pointer;
}
.active {
  border-color: #64748b;
  transform: scale(1.1);
}

.main-area {
  flex: 1;
  padding: 20px;
  overflow: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  background: #f1f5f9;
  position: relative;
}
.pdf-container {
  display: flex;
  flex-direction: column;
  gap: 20px;
  padding-bottom: 100px;
  width: 100%;
  align-items: center;
}
.page-wrapper {
  position: relative;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
  background: white;
}
.page-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 10;
  overflow: hidden;
}
.page-overlay.active-drop-zone {
  box-shadow: inset 0 0 0 4px rgba(37, 99, 235, 0.6);
  background: rgba(37, 99, 235, 0.05);
}

.draggable-sig {
  position: absolute;
  cursor: move;
  border: 1px dashed transparent;
  pointer-events: auto;
  min-width: 50px;
  min-height: 20px;
}
.draggable-sig:hover,
.draggable-sig.active-edit {
  border-color: var(--primary);
}
.draggable-sig img {
  width: 100%;
  height: 100%;
  display: block;
  pointer-events: none;
}

.remove-sig {
  position: absolute;
  top: -10px;
  right: -10px;
  width: 20px;
  height: 20px;
  background: red;
  color: white;
  border-radius: 50%;
  font-size: 12px;
  display: none;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 102;
}
.resize-handle {
  position: absolute;
  bottom: -6px;
  right: -6px;
  width: 16px;
  height: 16px;
  background-color: var(--primary);
  border: 2px solid white;
  border-radius: 50%;
  cursor: nwse-resize;
  z-index: 101;
  display: none;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.draggable-sig:hover .remove-sig,
.draggable-sig.active-edit .remove-sig {
  display: flex;
}
.draggable-sig:hover .resize-handle,
.draggable-sig.active-edit .resize-handle {
  display: block;
}

.saved-sig {
  position: relative;
  border: 1px solid var(--border);
  padding: 5px;
  border-radius: 6px;
  background: white;
  cursor: grab;
  margin-bottom: 8px;
}
.saved-sig img {
  height: 40px;
  display: block;
  margin: 0 auto;
  pointer-events: none;
}
.dragging-helper {
  position: fixed;
  z-index: 9999;
  pointer-events: none;
  opacity: 0.8;
}

.down-btn {
  position: absolute;
  top: -5px;
  left: -5px;
  background: #2563eb;
  color: white;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  display: none;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  z-index: 2;
  cursor: pointer;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}
.saved-sig:hover .down-btn {
  display: flex;
}
.saved-sig .del-btn {
  display: none;
}
.saved-sig:hover .del-btn {
  display: flex;
}

/* --- FIX MODAL & TRAY VISIBILITY --- */
#modal-pad,
#tray-sigs {
  display: none;
  z-index: 9999;
}

/* --- BUTTONS ON SAVED SIGNATURE --- */

/* Style Dasar untuk Tombol Kecil (Hapus & Download) */
.sig-action-btn {
  position: absolute;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  display: none; /* Default: Sembunyi (untuk Desktop sebelum hover) */
  align-items: center;
  justify-content: center;
  font-size: 12px;
  cursor: pointer;
  z-index: 10;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
  font-weight: bold;
  transition: transform 0.1s;
}

.sig-action-btn:active {
  transform: scale(0.9);
}

/* Posisi & Warna Spesifik */
.del-btn {
  top: -10px;
  right: -10px;
  background: #ef4444; /* Merah */
  color: white;
}

.down-btn {
  top: -10px;
  left: -10px;
  background: #2563eb; /* Biru */
  color: white;
}

/* DESKTOP: Munculkan saat mouse di atas (Hover) */
.saved-sig:hover .sig-action-btn {
  display: flex;
}

/* --- EMPTY STATE STYLES --- */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  margin-top: 100px; /* Turunkan sedikit dari atas */
  color: #64748b;
  animation: fadeIn 0.5s ease-in;
}

.empty-icon {
  font-size: 4rem;
  margin-bottom: 1rem;
  opacity: 0.5;
  filter: grayscale(100%);
}

.empty-state h3 {
  margin: 0 0 10px 0;
  color: #334155;
}

.empty-state p {
  margin: 0;
  font-size: 0.95rem;
  max-width: 300px;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* Adjust posisi vertical di mobile agar pas di tengah layar */
@media (max-width: 768px) {
  .empty-state {
    margin-top: 0;
    height: 60vh; /* Agar benar-benar di tengah layar HP */
  }
}

/* MOBILE: Selalu Muncul (Override di dalam Media Query) */
@media (max-width: 768px) {
  /* Paksa tombol selalu muncul di HP */
  .sig-action-btn {
    display: flex !important;
  }
}
@media (min-width: 769px) {
  #modal-pad.open,
  #tray-sigs.open {
    display: none !important;
  }
}

/* --- MOBILE STYLES (MAX-WIDTH: 768PX) --- */
@media (max-width: 768px) {
  .desktop-only {
    display: none !important;
  }
  .mobile-only {
    display: flex !important;
  }

  body {
    position: fixed;
    width: 100%;
    height: 100%;
    overflow: hidden;
  }
  .app-container {
    flex-direction: column;
    height: 100%;
  }

  .main-area {
    padding: 10px;
    height: calc(100dvh - 70px);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-y;
    box-sizing: border-box;
    display: block;
  }

  .pdf-container {
    padding-bottom: 100px;
    min-height: 101%;
  }

  /* FIX 1: Tombol Hapus di Tray SELALU MUNCUL */
  .saved-sig .del-btn {
    display: flex !important;
  }
  .down-btn {
    display: flex !important;
  }

  .draggable-sig .remove-sig {
    display: flex !important;
  }
  .draggable-sig .resize-handle {
    display: block !important;
  }
  .draggable-sig {
    border-color: rgba(37, 99, 235, 0.3);
  }

  .mobile-nav {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 65px;
    background: white;
    border-top: 1px solid var(--border);
    display: flex;
    justify-content: space-around;
    align-items: center;
    z-index: 100;
    box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);
    padding-bottom: 5px;
  }
  .nav-item {
    background: none;
    border: none;
    font-size: 0.7rem;
    color: #64748b;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 22%;
  }
  .nav-item:disabled {
    opacity: 0.4;
  }
  .center-btn {
    transform: translateY(-25px);
    background: var(--primary);
    color: white;
    width: 65px;
    height: 65px;
    border-radius: 50%;
    box-shadow: 0 4px 10px rgba(37, 99, 235, 0.4);
    justify-content: center;
  }
  .center-btn span {
    font-size: 0.65rem;
    margin-top: 2px;
    color: white;
  }

  #modal-pad.open {
    display: flex;
    background: rgba(0, 0, 0, 0.5);
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    align-items: center;
    justify-content: center;
  }
  .modal-content {
    background: white;
    width: 90%;
    max-width: 400px;
    padding: 20px;
    border-radius: 12px;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
  }
  .modal-header {
    display: flex;
    justify-content: space-between;
    margin-bottom: 15px;
  }
  .close-modal {
    background: none;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
  }

  #tray-sigs {
    display: flex;
    transform: translateY(110%);
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    max-height: 50vh;
    background: white;
    border-top-left-radius: 20px;
    border-top-right-radius: 20px;
    z-index: 150;
    transition: transform 0.3s ease;
    box-shadow: 0 -5px 20px rgba(0, 0, 0, 0.1);
    flex-direction: column;
  }
  #tray-sigs.open {
    transform: translateY(0);
    padding-bottom: 80px;
  }
  .tray-header {
    padding: 15px;
    border-bottom: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .tray-body {
    padding: 15px;
    overflow-y: auto;
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    justify-content: center;
  }
  .tray-body .saved-sig {
    width: 45%;
    margin: 0;
  }

  /* SweetAlert Mobile */
  .swal2-popup {
    font-size: 0.75rem !important;
    width: 85% !important;
    padding: 1em !important;
  }
  .swal2-title {
    font-size: 1.1rem !important;
  }
  .swal2-html-container {
    font-size: 0.9rem !important;
  }
  .swal2-actions {
    gap: 5px;
  }
  .swal2-styled {
    padding: 8px 16px !important;
    font-size: 0.85rem !important;
  }

  /* Mencegah PDF Container melebar melebihi layar */
  .pdf-container {
    width: 100% !important;
    overflow-x: hidden; /* Potong jika ada kelebihan dikit */
  }

  /* Pastikan wrapper PDF tidak pernah lebih lebar dari container */
  .page-wrapper {
    max-width: 100%;
  }
}
