/* ============ Variables ============ */
:root {
  --bg: #f0f2f5;
  --bg-card: #ffffff;
  --bg-header: linear-gradient(135deg, #0f0c29 0%, #302b63 50%, #24243e 100%);
  --bg-section: #ffffff;
  --text: #1a1a2e;
  --text-secondary: #6b7280;
  --text-muted: #9ca3af;
  --border: #e5e7eb;
  --accent: #6366f1;
  --accent-light: #818cf8;
  --accent-bg: rgba(99,102,241,.08);
  --accent-hover: #4f46e5;
  --success: #10b981;
  --shadow: 0 1px 3px rgba(0,0,0,.06), 0 1px 2px rgba(0,0,0,.04);
  --shadow-md: 0 4px 6px rgba(0,0,0,.05), 0 2px 4px rgba(0,0,0,.04);
  --shadow-lg: 0 10px 25px rgba(0,0,0,.08), 0 4px 10px rgba(0,0,0,.04);
  --shadow-hover: 0 10px 30px rgba(99,102,241,.15), 0 4px 10px rgba(0,0,0,.06);
  --radius: 12px;
  --radius-sm: 8px;
  --radius-lg: 16px;
  --transition: .25s cubic-bezier(.4,0,.2,1);
}

html.dark {
  --bg: #0f172a;
  --bg-card: #1e293b;
  --bg-header: linear-gradient(135deg, #0f172a 0%, #1e1b4b 50%, #0f172a 100%);
  --bg-section: #1e293b;
  --text: #f1f5f9;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;
  --border: #334155;
  --accent-bg: rgba(99,102,241,.15);
  --shadow: 0 1px 3px rgba(0,0,0,.2);
  --shadow-md: 0 4px 6px rgba(0,0,0,.15);
  --shadow-lg: 0 10px 25px rgba(0,0,0,.25);
  --shadow-hover: 0 10px 30px rgba(99,102,241,.2), 0 4px 10px rgba(0,0,0,.15);
}

/* ============ Reset ============ */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display", "Segoe UI", Roboto, "Noto Sans SC", sans-serif; background: var(--bg); color: var(--text); line-height: 1.6; min-height: 100vh; -webkit-font-smoothing: antialiased; }
a { color: var(--accent); text-decoration: none; transition: color var(--transition); }
a:hover { color: var(--accent-hover); }
button { cursor: pointer; border: none; font-family: inherit; }
input, select, textarea { font-family: inherit; }
::selection { background: var(--accent); color: #fff; }

/* ============ Container ============ */
.container { max-width: 1500px; margin: 0 auto; padding: 0 24px; }

/* ============ Search Header ============ */
.search-header {
  background: var(--bg-header);
  padding: 48px 0 36px;
  color: #fff;
  position: relative;
  overflow: hidden;
}
.search-header::before {
  content: '';
  position: absolute;
  inset: 0;
  background: radial-gradient(ellipse at 30% 0%, rgba(99,102,241,.3) 0%, transparent 60%),
              radial-gradient(ellipse at 70% 100%, rgba(139,92,246,.2) 0%, transparent 50%);
  pointer-events: none;
}
.site-title {
  text-align: center;
  font-size: 32px;
  font-weight: 800;
  margin-bottom: 28px;
  letter-spacing: 3px;
  position: relative;
  text-shadow: 0 2px 10px rgba(0,0,0,.3);
}
.search-box { max-width: 700px; margin: 0 auto; position: relative; }
.search-input-wrap {
  display: flex;
  background: rgba(255,255,255,.95);
  border-radius: 50px;
  overflow: hidden;
  box-shadow: 0 8px 32px rgba(0,0,0,.2);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(255,255,255,.2);
}
html.dark .search-input-wrap { background: rgba(30,41,59,.9); }
.search-input {
  flex: 1;
  padding: 16px 28px;
  border: none;
  outline: none;
  font-size: 16px;
  color: #333;
  background: transparent;
}
html.dark .search-input { color: #f1f5f9; }
.search-btn {
  padding: 16px 36px;
  background: linear-gradient(135deg, var(--accent) 0%, #8b5cf6 100%);
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  border: none;
  transition: all var(--transition);
  letter-spacing: 1px;
}
.search-btn:hover { filter: brightness(1.1); transform: scale(1.02); }

/* Search engine buttons */
.search-engines {
  display: flex;
  gap: 8px;
  margin-top: 16px;
  flex-wrap: wrap;
  justify-content: center;
  position: relative;
}
.engine-btn {
  padding: 6px 16px;
  border-radius: 20px;
  background: rgba(255,255,255,.1);
  color: rgba(255,255,255,.75);
  font-size: 13px;
  cursor: pointer;
  transition: all var(--transition);
  border: 1px solid rgba(255,255,255,.1);
  backdrop-filter: blur(10px);
}
.engine-btn:hover { background: rgba(255,255,255,.2); color: #fff; border-color: rgba(255,255,255,.25); }
.engine-btn.active {
  background: rgba(255,255,255,.25);
  color: #fff;
  border-color: rgba(255,255,255,.4);
  box-shadow: 0 2px 8px rgba(99,102,241,.3);
}

/* Theme toggle */
.header-actions { position: absolute; top: 16px; right: 24px; z-index: 10; }
.theme-toggle {
  background: rgba(255,255,255,.1);
  border-radius: 50%;
  width: 42px; height: 42px;
  font-size: 18px;
  display: flex; align-items: center; justify-content: center;
  transition: all var(--transition);
  color: #fff;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255,255,255,.1);
}
.theme-toggle:hover { background: rgba(255,255,255,.2); transform: rotate(20deg); }

/* ============ Main Layout ============ */
main.container {
  display: flex;
  gap: 28px;
  padding-top: 28px;
  padding-bottom: 48px;
  align-items: flex-start;
}

/* ============ Sidebar ============ */
.sidebar {
  width: 210px;
  flex-shrink: 0;
  position: sticky;
  top: 20px;
  background: var(--bg-card);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  padding: 16px 0;
  max-height: calc(100vh - 40px);
  overflow-y: auto;
  border: 1px solid var(--border);
}
.sidebar::-webkit-scrollbar { width: 4px; }
.sidebar::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
.sidebar-item {
  display: block;
  padding: 10px 24px;
  color: var(--text-secondary);
  font-size: 14px;
  cursor: pointer;
  transition: all var(--transition);
  border-left: 3px solid transparent;
  font-weight: 500;
}
.sidebar-item:hover { background: var(--accent-bg); color: var(--accent); text-decoration: none; }
.sidebar-item.active {
  border-left-color: var(--accent);
  color: var(--accent);
  background: var(--accent-bg);
  font-weight: 600;
}

/* ============ Content ============ */
.content { flex: 1; min-width: 0; }
.loading { text-align: center; padding: 60px; color: var(--text-muted); font-size: 15px; }

/* ============ Category Section ============ */
.category-section {
  margin-bottom: 32px;
  background: var(--bg-section);
  border-radius: var(--radius-lg);
  padding: 24px;
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  transition: box-shadow var(--transition);
}
.category-section:hover { box-shadow: var(--shadow-md); }
.category-title {
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 16px;
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--text);
}
.category-title::before {
  content: '';
  width: 4px;
  height: 24px;
  background: linear-gradient(180deg, var(--accent) 0%, #8b5cf6 100%);
  border-radius: 2px;
}

/* Subcategory Tabs */
.sub-tabs {
  display: flex;
  gap: 6px;
  margin-bottom: 16px;
  flex-wrap: wrap;
  padding: 4px;
  background: var(--bg);
  border-radius: var(--radius);
  border: 1px solid var(--border);
}
.sub-tab {
  padding: 6px 18px;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-secondary);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all var(--transition);
  border: none;
}
.sub-tab:hover { color: var(--accent); background: var(--accent-bg); }
.sub-tab.active {
  background: var(--accent);
  color: #fff;
  box-shadow: 0 2px 8px rgba(99,102,241,.3);
}

/* ============ Link Cards Grid ============ */
.links-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 14px;
}
.link-card {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 16px 18px;
  background: var(--bg-card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  transition: all var(--transition);
  cursor: pointer;
  text-decoration: none;
  color: var(--text);
  border: 1px solid var(--border);
  position: relative;
  overflow: hidden;
}
.link-card::after {
  content: '';
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 100%;
  background: linear-gradient(135deg, rgba(99,102,241,.02) 0%, transparent 100%);
  opacity: 0;
  transition: opacity var(--transition);
}
.link-card:hover {
  box-shadow: var(--shadow-hover);
  transform: translateY(-3px);
  border-color: var(--accent-light);
  text-decoration: none;
}
.link-card:hover::after { opacity: 1; }

.link-card .card-icon {
  width: 32px; height: 32px;
  border-radius: 6px;
  flex-shrink: 0;
  object-fit: contain;
  background: none;
  padding: 0;
  position: relative;
  z-index: 1;
}
.link-card .card-icon-placeholder {
  width: 40px; height: 40px;
  border-radius: 10px;
  flex-shrink: 0;
  background: linear-gradient(135deg, var(--accent) 0%, #8b5cf6 100%);
  color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-size: 16px; font-weight: 700;
  position: relative; z-index: 1;
  box-shadow: 0 2px 6px rgba(99,102,241,.3);
}
.link-card .card-info { min-width: 0; flex: 1; position: relative; z-index: 1; }
.link-card .card-title {
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-bottom: 4px;
}
.link-card .card-desc {
  font-size: 12px;
  color: var(--text-muted);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  line-height: 1.5;
}

/* ============ Footer ============ */
.footer {
  text-align: center;
  padding: 24px;
  color: var(--text-muted);
  font-size: 13px;
  border-top: 1px solid var(--border);
  margin-top: 24px;
}
.footer a { color: var(--text-muted); }
.footer a:hover { color: var(--accent); }

/* ============ Responsive ============ */
@media (max-width: 768px) {
  .sidebar { display: none; }
  main.container { flex-direction: column; }
  .links-grid { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 10px; }
  .search-header { padding: 28px 0 24px; }
  .site-title { font-size: 24px; margin-bottom: 18px; }
  .search-input { padding: 12px 18px; font-size: 15px; }
  .search-btn { padding: 12px 22px; font-size: 14px; }
  .category-section { padding: 16px; margin-bottom: 20px; }
  .link-card { padding: 12px 14px; gap: 10px; }
  .link-card .card-icon, .link-card .card-icon-placeholder { width: 32px; height: 32px; border-radius: 8px; }
}

/* ============ Scrollbar ============ */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* ============ Animations ============ */
@keyframes fadeInUp {
  from { opacity: 0; transform: translateY(16px); }
  to { opacity: 1; transform: translateY(0); }
}
.category-section { animation: fadeInUp .4s ease both; }
.category-section:nth-child(2) { animation-delay: .05s; }
.category-section:nth-child(3) { animation-delay: .1s; }
.category-section:nth-child(4) { animation-delay: .15s; }
.category-section:nth-child(5) { animation-delay: .2s; }

/* ============ Admin Styles ============ */
.admin-wrap { max-width: 1000px; margin: 0 auto; padding: 20px; }
.admin-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 24px; padding-bottom: 16px; border-bottom: 2px solid var(--border); }
.admin-header h1 { font-size: 22px; }
.admin-nav { display: flex; gap: 8px; margin-bottom: 20px; flex-wrap: wrap; }
.admin-nav button { padding: 8px 20px; border-radius: var(--radius-sm); background: var(--bg-card); border: 1px solid var(--border); color: var(--text); font-size: 14px; transition: all .2s; }
.admin-nav button.active, .admin-nav button:hover { background: var(--accent); color: #fff; border-color: var(--accent); }
.admin-table { width: 100%; border-collapse: collapse; background: var(--bg-card); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
.admin-table th, .admin-table td { padding: 10px 14px; text-align: left; border-bottom: 1px solid var(--border); font-size: 14px; }
.admin-table th { background: var(--bg); font-weight: 600; }
.admin-table tr:hover { background: var(--accent-bg); }
.btn { padding: 6px 14px; border-radius: var(--radius-sm); font-size: 13px; transition: all .2s; font-weight: 500; }
.btn-primary { background: linear-gradient(135deg, var(--accent), #8b5cf6); color: #fff; }
.btn-primary:hover { filter: brightness(1.1); }
.btn-danger { background: #e53e3e; color: #fff; }
.btn-danger:hover { background: #c53030; }
.btn-sm { padding: 4px 10px; font-size: 12px; }
.form-group { margin-bottom: 14px; }
.form-group label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 4px; color: var(--text-secondary); }
.form-group input, .form-group select, .form-group textarea { width: 100%; padding: 10px 14px; border: 1px solid var(--border); border-radius: var(--radius-sm); font-size: 14px; background: var(--bg-card); color: var(--text); transition: border-color var(--transition); }
.form-group input:focus, .form-group select:focus, .form-group textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-bg); }
.form-group textarea { min-height: 80px; resize: vertical; }
.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,.5); display: flex; align-items: center; justify-content: center; z-index: 1000; backdrop-filter: blur(4px); }
.modal { background: var(--bg-card); border-radius: var(--radius-lg); padding: 28px; width: 90%; max-width: 500px; max-height: 80vh; overflow-y: auto; box-shadow: 0 20px 60px rgba(0,0,0,.2); border: 1px solid var(--border); }
.modal h3 { margin-bottom: 16px; font-size: 18px; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 20px; }
.login-box { max-width: 380px; margin: 80px auto; background: var(--bg-card); padding: 36px; border-radius: var(--radius-lg); box-shadow: var(--shadow-lg); border: 1px solid var(--border); }
.login-box h2 { text-align: center; margin-bottom: 28px; font-size: 24px; }
.toolbar { display: flex; gap: 8px; margin-bottom: 16px; align-items: center; flex-wrap: wrap; }
.toolbar input { flex: 1; min-width: 200px; }
.badge { display: inline-block; padding: 2px 10px; border-radius: 12px; font-size: 11px; background: var(--accent-bg); color: var(--accent); font-weight: 500; }

/* ============ Admin FAB ============ */
.admin-fab {
  position: fixed;
  bottom: 24px; right: 24px;
  width: 52px; height: 52px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), #8b5cf6);
  color: #fff;
  font-size: 22px;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 4px 16px rgba(99,102,241,.4);
  cursor: pointer; border: none;
  transition: all var(--transition);
  z-index: 999;
}
.admin-fab:hover { transform: scale(1.1); box-shadow: 0 6px 24px rgba(99,102,241,.5); }
body.edit-mode .admin-fab {
  width: auto; border-radius: 26px; padding: 0 24px;
  font-size: 15px; font-weight: 600; letter-spacing: 1px;
}

/* ============ iOS Jiggle Edit Mode ============ */
@keyframes jiggle {
  0%   { transform: rotate(-0.7deg); }
  25%  { transform: rotate(0.7deg); }
  50%  { transform: rotate(-0.5deg); }
  75%  { transform: rotate(0.5deg); }
  100% { transform: rotate(-0.7deg); }
}
body.edit-mode .link-card:not(.card-add):not(.drag-active):not(.drag-ghost) {
  animation: jiggle .3s ease-in-out infinite;
  cursor: grab;
  user-select: none;
  -webkit-user-select: none;
}
body.edit-mode .link-card {
  transition: transform .3s cubic-bezier(.25,1,.5,1), box-shadow .3s ease, opacity .2s ease;
}
body.edit-mode .link-card:nth-child(odd) { animation-delay: .05s; }
body.edit-mode .link-card:nth-child(3n) { animation-delay: .1s; }
body.edit-mode .link-card:active:not(.card-add) { cursor: grabbing; }

/* Delete button on cards */
.card-delete {
  position: absolute;
  top: 6px; right: 6px;
  width: 24px; height: 24px;
  border-radius: 50%;
  background: #e53e3e;
  color: #fff;
  font-size: 16px; font-weight: 700;
  display: flex; align-items: center; justify-content: center;
  border: 2px solid var(--bg-card);
  cursor: pointer;
  z-index: 10;
  line-height: 1;
  transition: transform .15s;
  box-shadow: 0 2px 6px rgba(0,0,0,.2);
}
.card-delete:hover { transform: scale(1.2); }

/* Add card button */
.card-add {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 2px dashed var(--border);
  background: transparent;
  cursor: pointer;
  animation: none !important;
  min-height: 80px;
  transition: all var(--transition);
}
.card-add:hover {
  border-color: var(--accent);
  background: var(--accent-bg);
  transform: none;
  box-shadow: none;
}
.card-add .add-icon {
  font-size: 28px;
  color: var(--text-muted);
  font-weight: 300;
  line-height: 1;
}
.card-add .add-text {
  font-size: 12px;
  color: var(--text-muted);
}
.card-add:hover .add-icon,
.card-add:hover .add-text { color: var(--accent); }

/* Drag feedback */
.drag-ghost {
  opacity: 0.15;
  border: 2px dashed var(--accent) !important;
  background: var(--accent-bg) !important;
  box-shadow: none !important;
  animation: none !important;
  border-radius: var(--radius);
}
.drag-active {
  box-shadow: 0 16px 40px rgba(0,0,0,.2) !important;
  transform: scale(1.06) !important;
  animation: none !important;
  opacity: 0.92;
  z-index: 9999;
  border-radius: var(--radius);
}
.drag-chosen {
  border-color: var(--accent-light) !important;
  animation: none !important;
}

/* Toast notifications */
.sort-toast {
  position: fixed;
  bottom: 88px; right: 24px;
  padding: 12px 22px;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 500;
  z-index: 10000;
  opacity: 0;
  transform: translateY(10px) scale(.95);
  transition: all .3s cubic-bezier(.4,0,.2,1);
  pointer-events: none;
  box-shadow: var(--shadow-lg);
}
.sort-toast.show { opacity: 1; transform: translateY(0) scale(1); }
.sort-toast-success { background: var(--success); color: #fff; }
.sort-toast-error { background: #e53e3e; color: #fff; }

/* Hide link navigation in edit mode */
body.edit-mode .link-card:not(.card-add) { pointer-events: auto; }
body.edit-mode .link-card:not(.card-add)::after { display: none; }

/* ============ Category Edit Controls ============ */
/* Sidebar edit mode */
body.edit-mode .sidebar-item:not(.sidebar-add-cat) { cursor: grab; }
body.edit-mode .sidebar-item:active:not(.sidebar-add-cat) { cursor: grabbing; }
.sidebar-add-cat {
  color: var(--text-muted) !important;
  border-left-color: transparent !important;
  cursor: pointer !important;
  font-style: italic;
  border-top: 1px dashed var(--border);
  margin-top: 8px;
}
.sidebar-add-cat:hover { color: var(--accent) !important; }
.sidebar-drag-ghost {
  opacity: 0.3;
  background: var(--accent-bg);
  border-left: 3px solid var(--accent);
}

/* Subcategory tab edit button */
.sub-tab-edit {
  display: none;
  margin-left: 4px;
  font-size: 10px;
  opacity: 0.5;
  cursor: pointer;
}
body.edit-mode .sub-tab-edit { display: inline; }
body.edit-mode .sub-tab-edit:hover { opacity: 1; }
body.edit-mode .sub-tab { position: relative; padding-right: 24px; }

.cat-drag-handle {
  cursor: grab;
  font-size: 20px;
  color: var(--text-muted);
  opacity: 0.5;
  transition: opacity var(--transition);
  user-select: none;
  line-height: 1;
  margin-right: 4px;
}
.cat-drag-handle:hover { opacity: 1; }
.cat-drag-handle:active { cursor: grabbing; }

.cat-actions {
  display: flex;
  gap: 6px;
  margin-left: auto;
}
.cat-action-btn {
  width: 30px; height: 30px;
  border-radius: 50%;
  background: var(--bg);
  border: 1px solid var(--border);
  font-size: 14px;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  transition: all var(--transition);
}
.cat-action-btn:hover { background: var(--accent-bg); border-color: var(--accent); }
.cat-action-btn.cat-action-danger:hover { background: rgba(229,62,62,.1); border-color: #e53e3e; color: #e53e3e; }
.cat-action-btn.is-hidden { opacity: 0.5; }

/* Add category section */
.add-cat-section {
  border: 2px dashed var(--border) !important;
  background: transparent !important;
  cursor: pointer;
  text-align: center;
  box-shadow: none !important;
}
.add-cat-section:hover { border-color: var(--accent) !important; background: var(--accent-bg) !important; }
.add-cat-btn {
  font-size: 16px;
  color: var(--text-muted);
  padding: 16px;
  font-weight: 500;
}
.add-cat-section:hover .add-cat-btn { color: var(--accent); }

/* Sub-tab add button */
.sub-tab-add {
  background: transparent !important;
  border: 1px dashed var(--border) !important;
  font-size: 16px;
  font-weight: 300;
  color: var(--text-muted);
  min-width: 36px;
}
.sub-tab-add:hover { border-color: var(--accent) !important; color: var(--accent); }

/* Category title inline edit */
.cat-title-input {
  font-size: 20px;
  font-weight: 700;
  border: none;
  border-bottom: 2px solid var(--accent);
  background: transparent;
  color: var(--text);
  outline: none;
  padding: 2px 4px;
  flex: 1;
  min-width: 100px;
}

/* Hidden category */
.cat-hidden {
  opacity: 0.4;
  position: relative;
}
.cat-hidden::after {
  content: '已隐藏';
  position: absolute;
  top: 12px; right: 16px;
  background: var(--text-muted);
  color: #fff;
  padding: 2px 10px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 500;
}

/* Category drag feedback */
.cat-drag-ghost {
  opacity: 0.15;
  border: 2px dashed var(--accent) !important;
}
.cat-drag-active {
  box-shadow: 0 16px 48px rgba(0,0,0,.15) !important;
  z-index: 9999;
}
