:root {
  --sidebar-w: 220px;
  --topbar-h: 64px;
  --card-min: 100px;
  --icon-size: 60px;
  --transition: 0.2s ease;
  --icon-grid-max-width: 1920px;

  --bg: #0a0a0a;
  --sidebar-bg: #121212;
  --topbar-bg: #121212;
  --card-bg: #1a1a1a;
  --card-border: #2a2a2a;
  --card-border-hover: #3a3a3a;
  --text: #e5e5e5;
  --text-muted: #888;
  --accent: #a3e635;
  --accent-line: #a3e635;
  --icon-bg: #222;
  --icon-ring: transparent;

  --hover-bg: #252525;
  --nav-hover-bg: rgba(255, 255, 255, 0.04);
  --nav-active-bg: rgba(163, 230, 53, 0.08);
  --card-hover-bg: #4ade8022;

  --shadow-dropdown: 0 8px 24px rgba(0, 0, 0, 0.5);
  --shadow-card: 0 4px 12px rgba(0, 0, 0, 0.4);
  --shadow-card-featured: 0 4px 12px rgba(163, 230, 53, 0.15);
  --shadow-icon: 0 0 16px rgba(0, 0, 0, 0.3);

  --type-lan: #facc15;
  --type-frp: #4ade80;
  --type-tor: #7c3aed;
  --type-nsfw: #ef4444;
}

@media (max-width: 768px) {
  :root {
    --sidebar-w: 64px;
    --card-min: 64px;
    --icon-size: 44px;
  }
}

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

.page-loader {
  position: fixed;
  inset: 0;
  z-index: 9999;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity 0.4s ease;
}

.page-loader.hidden {
  opacity: 0;
  pointer-events: none;
}

.page-loader-content {
  width: 320px;
  animation: page-loader-fadein 0.5s ease;
}

@keyframes page-loader-fadein {
  from { opacity: 0; }
  to { opacity: 1; }
}

.page-loader-title {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
}

.page-loader-text {
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 0.2rem;
  color: var(--text);
}

.page-loader-spin {
  font-size: 18px;
  color: var(--accent);
  animation: page-loader-rotate 1s linear infinite;
}

@keyframes page-loader-rotate {
  to { transform: rotate(360deg); }
}

.page-loader-counter {
  font-size: 14px;
  font-weight: 700;
  color: var(--accent);
  min-width: 24px;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.page-loader-percent {
  font-size: 12px;
  color: var(--text-muted);
}

.page-loader-bar-border {
  padding: 3px;
  border-radius: 3px;
  border-top: 1px solid var(--card-border);
  border-bottom: 1px solid var(--card-border);
}

.page-loader-bar {
  height: 6px;
  border-radius: 2px;
  background: var(--accent);
  width: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  height: 100vh;
  overflow: hidden;
}

/* ===== Sidebar ===== */

.sidebar {
  background: var(--sidebar-bg);
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  overflow-x: hidden;
  scrollbar-width: none;
}

.sidebar::-webkit-scrollbar {
  display: none;
}

.sidebar-header {
  padding: 24px 20px 16px;
  border-bottom: 1px solid var(--card-border);
  flex-shrink: 0;
}

.site-name {
  font-size: 24px;
  font-weight: 700;
  letter-spacing: -0.5px;
}

.site-name a {
  color: inherit;
  text-decoration: none;
  cursor: pointer;
}

.site-name a:link,
.site-name a:visited,
.site-name a:hover,
.site-name a:active {
  color: inherit;
  text-decoration: none;
}

.site-by {
  display: block;
  font-size: 11px;
  color: var(--text-muted);
  letter-spacing: 1px;
  margin-top: 2px;
}

.sidebar-nav {
  display: flex;
  flex-direction: column;
  padding: 8px 0;
  flex: 1;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 20px;
  cursor: pointer;
  border-left: 3px solid transparent;
  transition: background var(--transition), border-color var(--transition);
  white-space: nowrap;
  user-select: none;
}

.nav-item:hover {
  background: var(--nav-hover-bg);
}

.nav-item.active {
  background: var(--nav-active-bg);
  border-left-color: var(--accent-line);
}

.nav-star {
  display: inline-block;
  width: 10px;
  height: 10px;
  margin-left: 8px;
  vertical-align: middle;
  background: var(--text-muted);
  opacity: 0.5;
  clip-path: polygon(50% 0%, 61% 35%, 98% 35%, 68% 57%, 79% 91%, 50% 70%, 21% 91%, 32% 57%, 2% 35%, 39% 35%);
}

.nav-icon {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
  border-radius: 3px;
  overflow: hidden;
}

.nav-icon img {
  width: 100%;
  height: 100%;
}

.nav-label {
  font-size: 14px;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ===== Main area ===== */

.main {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

/* ===== Top bar ===== */

.topbar {
  height: var(--topbar-h);
  background: var(--topbar-bg);
  border-bottom: 1px solid var(--card-border);
  display: flex;
  align-items: center;
  padding: 0 24px;
  flex-shrink: 0;
  z-index: 100;
}

.search-area {
  display: flex;
  align-items: center;
  flex: 1;
  min-width: 0;
  max-width: 600px;
  position: relative;
}

.engine-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 14px;
  height: 40px;
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-right: none;
  border-radius: 8px 0 0 8px;
  color: var(--text);
  font-size: 14px;
  cursor: pointer;
  white-space: nowrap;
  transition: background var(--transition);
  flex-shrink: 0;
}

.engine-btn:hover {
  background: var(--hover-bg);
}

.engine-icon {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  object-fit: contain;
  filter: brightness(0) invert(0.9);
}

.engine-option-icon {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  object-fit: contain;
  filter: brightness(0) invert(0.9);
}

.engine-chevron {
  color: var(--text-muted);
  flex-shrink: 0;
}

.search-input-wrap {
  flex: 1;
  position: relative;
  min-width: 0;
}

.search-input {
  width: 100%;
  height: 40px;
  padding: 0 36px 0 16px;
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: 0 8px 8px 0;
  color: var(--text);
  font-size: 14px;
  outline: none;
  transition: border-color var(--transition);
  min-width: 0;
}

.search-clear {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  width: 24px;
  height: 24px;
  display: none;
  align-items: center;
  justify-content: center;
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  border-radius: 4px;
  padding: 0;
  transition: color var(--transition), background var(--transition);
}

.search-clear:hover {
  color: var(--text);
  background: var(--hover-bg);
}

.search-clear.visible {
  display: flex;
}

.search-input::placeholder {
  color: var(--text-muted);
}

.search-input:focus {
  border-color: var(--accent-line);
}

/* ===== Engine dropdown ===== */

.engine-dropdown {
  position: absolute;
  top: 100%;
  left: 0;
  margin-top: 4px;
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: 8px;
  min-width: 240px;
  max-height: calc(100vh - 80px);
  overflow-y: auto;
  box-shadow: var(--shadow-dropdown);
  z-index: 200;
  display: none;
  scrollbar-width: none;
}

.engine-dropdown.open {
  display: block;
}

.engine-dropdown::-webkit-scrollbar {
  display: none;
}

.engine-option {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  cursor: pointer;
  font-size: 14px;
  transition: background var(--transition);
}

.engine-option:hover {
  background: var(--hover-bg);
}

.engine-option.selected {
  color: var(--accent);
}

.engine-option .check {
  color: var(--accent);
  flex-shrink: 0;
}

.engine-option-tags {
  display: flex;
  gap: 4px;
  margin-left: auto;
}

.engine-option-tag {
  font-size: 10px;
  font-weight: 600;
  padding: 2px 6px;
  border-radius: 3px;
  background: var(--nav-active-bg);
  color: var(--text-muted);
  text-transform: uppercase;
}

/* ===== Theme switcher ===== */

.theme-area {
  position: relative;
  margin-left: auto;
  padding-left: 16px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 8px;
}

.topbar-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: 8px;
  color: var(--text-muted);
  cursor: pointer;
  transition: background var(--transition), color var(--transition);
}

.topbar-btn:hover {
  background: var(--hover-bg);
  color: var(--text);
}

.theme-dropdown {
  left: auto;
  right: 0;
  min-width: 180px;
}

.prefs-dropdown {
  left: auto;
  right: 0;
  min-width: 180px;
}

.prefs-dropdown a {
  display: block;
  padding: 10px 16px;
  font-size: 14px;
  color: var(--text);
  text-decoration: none;
  transition: background var(--transition);
}

.prefs-dropdown a:hover {
  background: var(--hover-bg);
}

.prefs-dropdown .dropdown-divider {
  margin: 4px 0;
  border: none;
  border-top: 1px solid var(--card-border);
}

/* ===== Content ===== */

.content {
  flex: 1;
  overflow-y: auto;
  scrollbar-width: none;
  padding: 32px 24px 48px;
}

.content::-webkit-scrollbar {
  display: none;
}

.category-section {
  margin-bottom: 48px;
  scroll-margin-top: 24px;
}

.category-section.hidden {
  display: none;
}

.category-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 24px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--card-border);
}

.cat-icon {
  width: 24px;
  height: 24px;
  flex-shrink: 0;
  border-radius: 3px;
  overflow: hidden;
}

.cat-icon img {
  width: 100%;
  height: 100%;
}

.cat-title {
  font-size: 20px;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 12px;
  white-space: nowrap;
  user-select: none;
}

.cat-title span {
  padding: 4px 6px;
  border-radius: 6px;
  background: var(--nav-active-bg);
  font-weight: normal;
  font-size: 12px;
}

/* ===== Bookmark grid ===== */

.bookmark-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(var(--card-min), 1fr));
  gap: 16px;
  max-width: var(--icon-grid-max-width);
}

.bookmark-card {
  text-decoration: none;
  color: var(--text);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.bookmark-card.hidden {
  display: none;
}

.bookmark-container {
  width: 100%;
  aspect-ratio: 1;
  border: 1px solid var(--card-border);
  border-radius: 20px;
  background: var(--card-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
  transition: border-color var(--transition), transform var(--transition), box-shadow var(--transition);
}

.bookmark-card:hover .bookmark-container {
  border-color: var(--card-border-hover);
  transform: translateY(-2px);
  box-shadow: var(--shadow-card);
  background: var(--card-hover-bg);
}

.bookmark-card.featured .bookmark-container {
  border-color: var(--accent-line);
}

.bookmark-card.featured:hover .bookmark-container {
  border-color: var(--accent-line);
  box-shadow: var(--shadow-card-featured);
}

.bookmark-icon {
  width: var(--icon-size);
  height: var(--icon-size);
  border-radius: 50%;
  background: var(--icon-bg);
  border: 1px solid var(--icon-ring);
  overflow: hidden;
  box-shadow: var(--shadow-icon);
  display: flex;
  align-items: center;
  justify-content: center;
}

.bookmark-icon img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.type-bar {
  position: absolute;
  bottom: 0;
  left: 20%;
  right: 20%;
  height: 4px;
  border-radius: 2px 2px 0 0;
  display: flex;
  overflow: hidden;
}

.type-seg {
  flex: 1;
}

.type-seg.type-lan { background: var(--type-lan); }
.type-seg.type-frp { background: var(--type-frp); }
.type-seg.type-tor { background: var(--type-tor); }
.type-seg.type-nsfw { background: var(--type-nsfw); }

.bookmark-title {
  font-size: 14px;
  font-weight: 500;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

/* ===== Empty state ===== */

.empty-state {
  text-align: center;
  color: var(--text-muted);
  padding: 64px 24px;
  font-size: 15px;
  display: none;
}

.empty-state.show {
  display: block;
}

.total-count {
  width: 100%;
  text-align: center;
  font-size: 12px;
}

/* ===== Multi-URL selector ===== */

.type-bar-top {
  top: 0;
  bottom: auto;
  border-radius: 0 0 2px 2px;
}

.type-seg.type-multi {
  background: var(--accent);
}

.url-popup {
  position: absllute;
  z-index: 1000;
  transform: translateX(-50%);
  background: var(--card-bg);
  border: 1px solid var(--card-border-hover);
  border-radius: 8px;
  box-shadow: var(--shadow-dropdown);
  width: 240px;
  max-width: calc(100vw - 16px);
  display: flex;
  flex-direction: column;
}

.url-popup-body {
  max-height: 70vh;
  overflow-y: auto;
  scrollbar-width: none;
  /*padding: 4px 0;*/
}

.url-popup-body::-webkit-scrollbar {
  display: none;
}

.url-popup-title {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px 8px 8px;
  border-bottom: 1px solid var(--card-border);
  /*margin-bottom: 4px;*/
}

.url-popup-icon {
  width: 20px;
  height: 20px;
  border-radius: 4px;
  object-fit: cover;
  flex-shrink: 0;
}

.url-popup-title-text {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  flex: 1;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.url-popup-description {
  font-size: 12px;
  color: var(--text-muted);
  padding: 8px;
  border-bottom: 1px solid var(--card-border);
  line-height: 1.4;
  text-align: center;
}

.url-option {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px;
  text-decoration: none;
  color: var(--text);
  font-size: 13px;
  transition: background var(--transition);
  width: calc(100% - 12px);
  /*border-bottom: 1px solid var(--card-border);*/
  border-radius: 6px;
  background: var(--bg);
  margin: 6px;
}

.url-option:last-child {
  border-bottom: none;
}

.url-option:hover {
  background: var(--hover-bg);
}

.url-option-number {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  min-width: 18px;
  text-align: center;
  flex-shrink: 0;
}

.url-option-text {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-width: 0;
}

.url-option-label {
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.url-option-url {
  font-size: 11px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.url-option-types {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
}

.url-type-badge {
  font-size: 10px;
  font-weight: 600;
  padding: 1px 5px;
  border-radius: 3px;
  text-transform: uppercase;
}

.url-type-badge.type-lan { background: var(--type-lan); color: #000; }
.url-type-badge.type-frp { background: var(--type-frp); color: #000; }
.url-type-badge.type-tor { background: #7c3aed; color: #fff; }
.url-type-badge.type-nsfw { background: var(--type-nsfw); color: #fff; }
.url-type-badge.type-file { background: #6b7280; color: #fff; }

/* ===== Mobile tweaks ===== */

@media (max-width: 768px) {
  .sidebar-header {
    padding: 16px 8px;
    text-align: center;
    height: var(--topbar-h);
  }
  .site-name { font-size: 14px; }
  .site-by { display: none; }
  .nav-item {
    justify-content: center;
    padding: 12px 0;
    border-left: none;
    border-right: 3px solid transparent;
  }
  .nav-item.active { border-right-color: var(--accent-line); }
  .nav-star { display: none; }
  .nav-label { display: none; }
  .content { padding: 20px 12px 32px; }
  .topbar { padding: 0 12px; }
  .bookmark-grid { gap: 12px; }
  .category-section { margin-bottom: 32px; }
  .category-header { margin-bottom: 16px; }
  .cat-title { font-size: 17px; }
  .engine-name { display: none; }
  .engine-btn { padding: 0 10px; }
  .search-area { max-width: 100%; }
  .theme-area { padding-left: 8px; }
  .bookmark-title { font-size: 10px; }
}
