/* Sortable Tree - Minimalist Design */
/* A clean, modern, and intuitive page management interface */

/* ============================================
   Base Container
   ============================================ */
.sortable_tree {
  margin: 0;
  padding: 0;
  padding-top: 8px;              /* Issue #82: 增加頂部空間，讓第一個項目可以向下拖動 */
  list-style: none;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  min-height: 80px;              /* Issue #82: 確保最小高度，提供拖放空間 */
  position: relative;            /* Issue #82: 確保正確的定位上下文 */
}

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

/* ============================================
   Nested Lists - Hierarchy Indicator
   ============================================ */
.sortable_tree ol {
  margin: 8px 0 0 24px;
  padding: 0 0 0 20px;
  border-left: 2px solid #e5e7eb;
  list-style: none;
  transition: border-color 0.2s ease;
}

.sortable_tree ol:hover {
  border-color: #d1d5db;
}

/* ============================================
   List Items
   ============================================ */
.sortable_tree li {
  margin-bottom: 8px;
  position: relative;
  list-style: none;
}

/* Issue #82: 第一個項目增加上方間距，便於拖動到最上方 */
.sortable_tree > li:first-child {
  margin-top: 4px;
}

/* ============================================
   Item Container - Card Style
   ============================================ */
.sortable_tree .item {
  padding: 14px 16px;
  background: #ffffff;
  border-radius: 8px;
  margin-bottom: 8px;
  border: 1px solid #e5e7eb;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.04);
  display: flex;
  align-items: center;
  transition: all 0.2s ease;
  position: relative;
}

.sortable_tree .item:hover {
  background: #fafbfc;
  border-color: #d1d5db;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  transform: translateY(-1px);
}

/* Active/Dragging state */
.sortable_tree .item.ui-sortable-helper {
  background: #ffffff;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
  border-color: #3b82f6;
  transform: scale(1.02);
}

/* Issue #82: 拖動時的視覺提示 */
.sortable_tree.ui-sortable-dragging {
  background: rgba(59, 130, 246, 0.02);
  border-radius: 8px;
}

/* ============================================
   Drag Handle
   ============================================ */
.sortable_tree .handle {
  width: 32px;
  height: 32px;
  margin-right: 12px;
  flex-shrink: 0;
  cursor: grab;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #9ca3af;
  font-size: 14px;
  border-radius: 6px;
  transition: all 0.2s ease;
  background: transparent;
}

.sortable_tree .handle:hover {
  color: #3b82f6;
  background: #eff6ff;
}

.sortable_tree .handle:active {
  cursor: grabbing;
  color: #2563eb;
  background: #dbeafe;
}

.sortable_tree .handle::before {
  font-family: "Font Awesome 6 Free";
  font-weight: 900;
  content: "\f58e"; /* fa-grip-vertical */
}

/* ============================================
   Page Title Link
   ============================================ */
.sortable_tree a {
  color: #374151;
  font-size: 14px;
  font-weight: 500;
  text-decoration: none;
  line-height: 1.5;
  margin-left: 0;
  margin-right: 140px;
  display: block;
  flex: 1;
  transition: color 0.15s ease;
}

.sortable_tree a:hover {
  color: #3b82f6;
  text-decoration: none;
}

.sortable_tree h4 {
  margin-left: 0;
  margin-bottom: 0;
  flex: 1;
}

.sortable_tree h4 a {
  color: #1f2937;
  font-size: 14px;
  font-weight: 500;
  margin-left: 0;
  padding: 4px 8px;
  border-radius: 4px;
  transition: all 0.15s ease;
}

.sortable_tree h4 a:hover {
  background: #f3f4f6;
  color: #3b82f6;
}

/* ============================================
   Description Text
   ============================================ */
.sortable_tree p {
  margin: 4px 0 0 44px;
  padding: 0;
  color: #6b7280;
  font-size: 13px;
  line-height: 1.4;
}

/* ============================================
   Control Buttons
   ============================================ */
.sortable_tree .controls {
  position: absolute;
  top: 50%;
  right: 12px;
  transform: translateY(-50%);
  display: flex;
  gap: 6px;
  opacity: 0;
  transition: opacity 0.2s ease;
}

.sortable_tree .item:hover .controls {
  opacity: 1;
}

.sortable_tree .controls a {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  margin: 0;
  cursor: pointer;
  border-radius: 6px;
  background: #ffffff;
  border: 1px solid #e5e7eb;
  color: #6b7280;
  text-decoration: none;
  font-size: 12px;
  transition: all 0.15s ease;
}

.sortable_tree .controls a:hover {
  transform: scale(1.08);
}

/* New/Add Button */
.sortable_tree .controls a.new {
  color: #10b981;
}

.sortable_tree .controls a.new::before {
  font-family: "Font Awesome 6 Free";
  font-weight: 900;
  content: "\f067"; /* fa-plus */
}

.sortable_tree .controls a.new:hover {
  background: #ecfdf5;
  border-color: #10b981;
}

/* Edit Button */
.sortable_tree .controls a.edit {
  color: #3b82f6;
}

.sortable_tree .controls a.edit::before {
  font-family: "Font Awesome 6 Free";
  font-weight: 900;
  content: "\f303"; /* fa-pencil */
}

.sortable_tree .controls a.edit:hover {
  background: #eff6ff;
  border-color: #3b82f6;
}

/* Delete Button */
.sortable_tree .controls a.delete {
  color: #ef4444;
}

.sortable_tree .controls a.delete::before {
  font-family: "Font Awesome 6 Free";
  font-weight: 900;
  content: "\f1f8"; /* fa-trash */
}

.sortable_tree .controls a.delete:hover {
  background: #fef2f2;
  border-color: #ef4444;
}

/* ============================================
   Drag Placeholder
   ============================================ */
.sortable_tree .placeholder {
  background: linear-gradient(135deg, #eff6ff 0%, #dbeafe 100%);
  border: 2px dashed #3b82f6;
  border-radius: 8px;
  min-height: 56px;          /* 加大高度使拖拉目標更明顯 */
  margin-bottom: 8px;
  margin-top: 4px;           /* 新增：上方間距 */
  opacity: 0.9;              /* 提高可見度 */
  animation: placeholderPulse 1.5s ease-in-out infinite;
  transition: all 0.15s ease; /* 新增：平滑過渡 */
}

/* Issue #82: 第一個位置的 placeholder 也需要顯示正確 */
.sortable_tree > .placeholder:first-child {
  margin-top: 4px;           /* 改為有間距，確保視覺一致性 */
}

@keyframes placeholderPulse {
  0%, 100% { opacity: 0.6; }
  50% { opacity: 1; }
}

/* ============================================
   Error State
   ============================================ */
.sortable_tree .ui-nestedSortable-error,
.sortable_tree .mjs-nestedSortable-error {
  background: #fef2f2 !important;
  border-color: #fca5a5 !important;
  color: #991b1b;
  animation: errorShake 0.4s ease;
}

@keyframes errorShake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-4px); }
  75% { transform: translateX(4px); }
}

/* ============================================
   Nested Children Indicator
   ============================================ */
.sortable_tree ol.nested_set {
  margin-top: 8px;
}

/* Visual hierarchy depth indicators */
.sortable_tree > li > .item {
  border-left: 3px solid #3b82f6;
}

.sortable_tree ol > li > .item {
  border-left: 3px solid #60a5fa;
}

.sortable_tree ol ol > li > .item {
  border-left: 3px solid #93c5fd;
}

.sortable_tree ol ol ol > li > .item {
  border-left: 3px solid #bfdbfe;
}

/* ============================================
   Status Indicator (for save feedback)
   ============================================ */
.sortable-status {
  font-size: 13px;
  font-weight: 500;
  padding: 4px 12px;
  border-radius: 20px;
  animation: statusFadeIn 0.3s ease;
}

.sortable-status.text-warning {
  background: #fef3c7;
  color: #92400e;
}

.sortable-status.text-success {
  background: #d1fae5;
  color: #065f46;
}

.sortable-status.text-danger {
  background: #fee2e2;
  color: #991b1b;
}

@keyframes statusFadeIn {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ============================================
   Empty State
   ============================================ */
.sortable_tree:empty::after {
  content: "尚無頁面，點擊右上角「新增」按鈕建立第一個頁面";
  display: block;
  padding: 40px;
  text-align: center;
  color: #9ca3af;
  font-size: 14px;
  background: #f9fafb;
  border: 2px dashed #e5e7eb;
  border-radius: 12px;
}

/* ============================================
   Responsive Design
   ============================================ */
@media (max-width: 768px) {
  .sortable_tree .item {
    padding: 12px;
  }

  .sortable_tree .controls {
    opacity: 1;
    position: static;
    transform: none;
    margin-left: auto;
  }

  .sortable_tree a {
    margin-right: 8px;
    font-size: 13px;
  }

  .sortable_tree ol {
    margin-left: 16px;
    padding-left: 12px;
  }
}

/* ============================================
   Print Styles
   ============================================ */
@media print {
  .sortable_tree .handle,
  .sortable_tree .controls {
    display: none !important;
  }

  .sortable_tree .item {
    box-shadow: none;
    border: 1px solid #ccc;
  }
}
