:root {
  --bg: #0f1115;
  --bg-2: #161a22;
  --bg-3: #1f2531;
  --fg: #e7ecf3;
  --fg-dim: #98a2b3;
  --accent: #6ea8fe;
  --accent-2: #4a86f0;
  --border: #2a3140;
  --danger: #ef6b6b;
  --radius: 10px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  min-height: 100vh;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 18px;
  background: var(--bg-2);
  border-bottom: 1px solid var(--border);
}
.topbar h1 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  flex: 1;
}
.icon-btn {
  background: transparent;
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 10px;
  cursor: pointer;
  font-size: 16px;
}
.icon-btn:hover { background: var(--bg-3); }

.layout {
  display: grid;
  grid-template-columns: 280px 1fr;
  min-height: calc(100vh - 53px);
}

.sidebar {
  background: var(--bg-2);
  border-right: 1px solid var(--border);
  padding: 14px;
  overflow-y: auto;
  max-height: calc(100vh - 53px);
  position: sticky;
  top: 53px;
}
.sidebar-header {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--fg-dim);
  margin-bottom: 8px;
}

.tree ul { list-style: none; padding-left: 14px; margin: 0; }
.tree > ul { padding-left: 0; }
.tree li { margin: 2px 0; }
.tree-row {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 8px;
  border-radius: 6px;
  cursor: pointer;
  user-select: none;
  font-size: 14px;
}
.tree-row:hover { background: var(--bg-3); }
.tree-row.active { background: var(--accent-2); color: white; }
.tree-row .caret { width: 12px; display: inline-block; color: var(--fg-dim); }
.tree-row .count { color: var(--fg-dim); font-size: 12px; margin-left: auto; }
.tree-row.active .count { color: rgba(255,255,255,0.85); }

.main {
  padding: 22px 28px 60px;
  max-width: 100%;
  overflow-x: hidden;
}

.breadcrumbs {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
  margin-bottom: 14px;
  font-size: 14px;
  color: var(--fg-dim);
}
.breadcrumbs a { color: var(--fg-dim); }
.breadcrumbs a:hover { color: var(--accent); }
.breadcrumbs .sep { opacity: 0.5; }
.breadcrumbs .current { color: var(--fg); font-weight: 600; }

.actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 18px;
}
.btn {
  background: var(--bg-3);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 14px;
  cursor: pointer;
  font-size: 14px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.btn:hover { background: #2a3140; }
.btn.primary { background: var(--accent-2); border-color: var(--accent-2); color: white; }
.btn.primary:hover { background: var(--accent); }
.btn:disabled { opacity: 0.6; cursor: not-allowed; }

.subcats {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 12px;
  margin-bottom: 22px;
}
.subcat-card {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  cursor: pointer;
  transition: transform 0.1s, border-color 0.1s;
  display: flex;
  flex-direction: column;
  gap: 6px;
  aspect-ratio: 4 / 3;
  overflow: hidden;
  position: relative;
}
.subcat-card:hover { border-color: var(--accent); transform: translateY(-2px); }
.subcat-card .cover {
  position: absolute; inset: 0;
  background-size: cover; background-position: center;
  opacity: 0.35;
  z-index: 0;
}
.subcat-card .info { position: relative; z-index: 1; margin-top: auto; }
.subcat-card .title { font-weight: 600; font-size: 15px; }
.subcat-card .meta { font-size: 12px; color: var(--fg-dim); }

.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 10px;
}
.tile {
  position: relative;
  aspect-ratio: 1;
  border-radius: 8px;
  overflow: hidden;
  background: var(--bg-2);
  cursor: zoom-in;
}
.tile img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  transition: transform 0.2s;
}
.tile:hover img { transform: scale(1.04); }
.tile .dl {
  position: absolute;
  top: 6px; right: 6px;
  background: rgba(0,0,0,0.55);
  color: white;
  border-radius: 6px;
  padding: 4px 8px;
  font-size: 12px;
  opacity: 0;
  transition: opacity 0.15s;
  text-decoration: none;
}
.tile:hover .dl { opacity: 1; }
.tile .dl:hover { background: var(--accent-2); }

.empty-state {
  margin-top: 40px;
  text-align: center;
  color: var(--fg-dim);
}
.empty-state code {
  background: var(--bg-3);
  padding: 2px 6px;
  border-radius: 4px;
}
.hidden { display: none !important; }

/* ===== Auth screen ===== */
.auth-screen {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background:
    radial-gradient(1200px 600px at 10% -10%, rgba(110,168,254,0.25), transparent 60%),
    radial-gradient(1000px 600px at 110% 110%, rgba(186,110,254,0.22), transparent 60%),
    linear-gradient(135deg, #0b1020 0%, #11162a 60%, #0d1424 100%);
  animation: authFadeIn 0.4s ease;
}
@keyframes authFadeIn { from { opacity: 0; } to { opacity: 1; } }
.auth-card {
  width: 100%;
  max-width: 380px;
  background: rgba(22, 26, 34, 0.85);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 20px;
  padding: 36px 28px;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0,0,0,0.5);
  animation: authPop 0.45s cubic-bezier(0.2, 0.9, 0.3, 1.4);
}
@keyframes authPop {
  from { transform: translateY(20px) scale(0.96); opacity: 0; }
  to { transform: translateY(0) scale(1); opacity: 1; }
}
.auth-logo {
  font-size: 52px;
  line-height: 1;
  margin-bottom: 8px;
  filter: drop-shadow(0 4px 16px rgba(110,168,254,0.5));
}
.auth-card h2 {
  margin: 8px 0 6px;
  font-size: 22px;
  font-weight: 700;
  background: linear-gradient(90deg, #fff, #b8d0ff);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.auth-sub {
  margin: 0 0 22px;
  color: var(--fg-dim);
  font-size: 14px;
}
.auth-input-wrap {
  position: relative;
  margin-bottom: 14px;
}
#auth-input {
  width: 100%;
  background: rgba(255,255,255,0.06);
  border: 1px solid rgba(255,255,255,0.12);
  color: var(--fg);
  border-radius: 12px;
  padding: 14px 46px 14px 16px;
  font-size: 16px;
  outline: none;
  transition: border-color 0.15s, background 0.15s, box-shadow 0.15s;
}
#auth-input::placeholder { color: rgba(255,255,255,0.35); }
#auth-input:focus {
  border-color: var(--accent);
  background: rgba(255,255,255,0.09);
  box-shadow: 0 0 0 4px rgba(110,168,254,0.15);
}
.auth-eye {
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  background: transparent;
  border: none;
  color: var(--fg-dim);
  font-size: 18px;
  width: 36px;
  height: 36px;
  border-radius: 8px;
  cursor: pointer;
}
.auth-eye:hover { background: rgba(255,255,255,0.08); color: var(--fg); }
.auth-submit {
  width: 100%;
  padding: 13px;
  font-size: 15px;
  font-weight: 600;
  border-radius: 12px;
  background: linear-gradient(135deg, #4a86f0, #6ea8fe);
  border: none;
  box-shadow: 0 6px 20px rgba(74,134,240,0.35);
  transition: transform 0.1s, box-shadow 0.15s;
}
.auth-submit:hover { box-shadow: 0 8px 24px rgba(74,134,240,0.5); }
.auth-submit:active { transform: scale(0.98); }
.auth-error {
  margin-top: 12px;
  min-height: 18px;
  color: var(--danger);
  font-size: 13px;
  opacity: 0;
  transition: opacity 0.15s;
}
.auth-error.show { opacity: 1; }
.auth-card.shake { animation: shake 0.35s; }
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-8px); }
  40% { transform: translateX(8px); }
  60% { transform: translateX(-6px); }
  80% { transform: translateX(6px); }
}

.lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.92);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.lightbox img {
  max-width: 92vw;
  max-height: 82vh;
  object-fit: contain;
  border-radius: 6px;
}
.lb-close, .lb-nav {
  position: absolute;
  background: rgba(255,255,255,0.08);
  border: none;
  color: white;
  font-size: 32px;
  width: 48px; height: 48px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.lb-close:hover, .lb-nav:hover { background: rgba(255,255,255,0.18); }
.lb-close { top: 18px; right: 18px; }
.lb-prev { left: 18px; top: 50%; transform: translateY(-50%); }
.lb-next { right: 18px; top: 50%; transform: translateY(-50%); }
.lb-bar {
  position: absolute;
  bottom: 18px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0,0,0,0.6);
  padding: 8px 14px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  gap: 14px;
  color: white;
  max-width: 90vw;
}
.lb-bar #lb-name {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 50vw;
  font-size: 14px;
}

.toast {
  position: fixed;
  bottom: 30px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-3);
  color: var(--fg);
  border: 1px solid var(--border);
  padding: 12px 20px;
  border-radius: 10px;
  z-index: 200;
  box-shadow: 0 6px 24px rgba(0,0,0,0.4);
  font-size: 14px;
}

.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 53px 0 0 0;
  background: rgba(0,0,0,0.5);
  z-index: 49;
}
.sidebar-backdrop.show { display: block; }

@media (max-width: 800px) {
  .topbar { padding: 10px 14px; }
  .topbar h1 { font-size: 16px; }
  .layout { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed;
    top: 53px; left: 0; bottom: 0;
    width: min(85vw, 320px);
    transform: translateX(-100%);
    transition: transform 0.22s ease;
    z-index: 50;
    max-height: none;
    box-shadow: 4px 0 24px rgba(0,0,0,0.4);
  }
  .sidebar.open { transform: translateX(0); }
  .main { padding: 14px; }
  .gallery { grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 6px; }
  .subcats { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; }
  .subcat-card { padding: 10px; aspect-ratio: 1; }
  .tile .dl { opacity: 1; padding: 6px 10px; font-size: 14px; }
  .actions { gap: 8px; }
  .btn { padding: 10px 14px; font-size: 14px; }
  .lb-close, .lb-nav { width: 44px; height: 44px; font-size: 26px; }
  .lb-close { top: 12px; right: 12px; }
  .lb-prev { left: 8px; }
  .lb-next { right: 8px; }
  .lb-bar #lb-name { max-width: 40vw; font-size: 12px; }
  .auth-card { padding: 28px 22px; border-radius: 18px; }
  .auth-logo { font-size: 44px; }
  .auth-card h2 { font-size: 20px; }
}
@media (min-width: 801px) {
  #menu-toggle { display: none; }
}

/* iOS safe area */
@supports (padding: max(0px)) {
  .topbar { padding-top: max(12px, env(safe-area-inset-top)); }
  .main { padding-bottom: max(60px, env(safe-area-inset-bottom)); }
}
