:root {
  --brand: #991b1e;
  --brand-hover: #7d1518;
  --bg: #f6f4f1;
  --surface: #ffffff;
  --surface-2: #efebe6;
  --text: #1a1a1a;
  --muted: #6b6560;
  --border: #e2ddd6;
  --shadow: 0 1px 2px rgba(0, 0, 0, .06), 0 4px 16px rgba(0, 0, 0, .06);
  --radius: 10px;
  --sidebar-w: 260px;
  --topbar-h: 68px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --brand: #c0272b;
    --brand-hover: #d63a3e;
    --bg: #121110;
    --surface: #1c1a19;
    --surface-2: #262321;
    --text: #f1eeea;
    --muted: #a39d96;
    --border: #2f2b28;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 4px 16px rgba(0, 0, 0, .3);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: Inter, system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}
a { color: inherit; }
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
[hidden] { display: none !important; }

/* ---------------------------------------------------------------- top bar */
.topbar {
  position: sticky; top: 0; z-index: 20;
  height: var(--topbar-h);
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 0 max(24px, env(safe-area-inset-right)) 0 max(24px, env(safe-area-inset-left));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 12px; text-decoration: none; min-width: 0; }
.brand-mark { height: 40px; width: auto; }
.brand-text { display: flex; flex-direction: column; line-height: 1.05; font-family: Montserrat, sans-serif; text-transform: uppercase; }
.brand-name { font-weight: 800; letter-spacing: .14em; font-size: 18px; }
.brand-sub { font-weight: 600; letter-spacing: .28em; font-size: 11px; color: var(--brand); margin-top: 3px; }

.btn {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 10px 18px; border-radius: 999px;
  font: 600 14px/1 Inter, sans-serif; text-decoration: none; white-space: nowrap;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  cursor: pointer; transition: background .15s, border-color .15s, color .15s;
}
.btn:hover { border-color: var(--text); }
.btn-primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn-primary:hover { background: var(--brand-hover); border-color: var(--brand-hover); }
.btn[aria-disabled="true"] { opacity: .6; pointer-events: none; }
.btn .sub { font-weight: 500; opacity: .8; }

/* ---------------------------------------------------------------- layout */
.layout { flex: 1; display: flex; width: 100%; max-width: 2400px; margin: 0 auto; }
.sidebar {
  width: var(--sidebar-w); flex: none;
  position: sticky; top: var(--topbar-h); align-self: flex-start;
  max-height: calc(100vh - var(--topbar-h)); max-height: calc(100dvh - var(--topbar-h)); overflow-y: auto;
  padding: 24px 12px 24px 20px;
  border-right: 1px solid var(--border);
}
.sidebar-title { font: 700 11px Montserrat, sans-serif; text-transform: uppercase; letter-spacing: .16em; color: var(--muted); margin: 0 8px 10px; }
.tree, .tree ul { list-style: none; margin: 0; padding: 0; }
.tree ul { padding-left: 14px; }
.tree-row { display: flex; align-items: center; gap: 2px; }
.tree-toggle {
  width: 22px; height: 22px; display: grid; place-items: center; flex: none;
  border: 0; background: none; color: var(--muted); cursor: pointer; border-radius: 4px; padding: 0;
}
.tree-toggle svg { width: 14px; height: 14px; transition: transform .15s; }
.tree-toggle[aria-expanded="true"] svg { transform: rotate(90deg); }
.tree-toggle.empty { visibility: hidden; }
.tree a {
  flex: 1; min-width: 0; display: block;
  padding: 5px 8px; border-radius: 6px; text-decoration: none;
  font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.tree a:hover { background: var(--surface-2); }
.tree a.active { background: var(--brand); color: #fff; font-weight: 600; }

.main { flex: 1; min-width: 0; padding: 28px 32px 48px; outline: none; }
.loading-state, .empty-state { color: var(--muted); padding: 48px 0; text-align: center; }

.footer { padding: 20px 24px; color: var(--muted); font-size: 13px; text-align: center; border-top: 1px solid var(--border); }

/* ---------------------------------------------------------------- page header */
.crumbs { display: flex; flex-wrap: wrap; align-items: center; gap: 4px; font-size: 13px; color: var(--muted); margin-bottom: 10px; }
.crumbs a { text-decoration: none; padding: 2px 4px; border-radius: 4px; }
.crumbs a:hover { color: var(--text); background: var(--surface-2); }
.crumbs .sep { opacity: .5; }
.crumbs .current { color: var(--text); font-weight: 500; padding: 2px 4px; }

.page-head { display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 24px; }
.page-title { font: 800 clamp(26px, 3.4vw, 38px)/1.1 Montserrat, sans-serif; text-transform: uppercase; letter-spacing: .02em; margin: 0; overflow-wrap: anywhere; }
.page-stats { color: var(--muted); margin-top: 6px; font-size: 14px; }
.page-actions { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }

.toggle { display: inline-flex; align-items: center; gap: 8px; min-height: 40px; font-size: 14px; color: var(--muted); cursor: pointer; user-select: none; }
.toggle input { accent-color: var(--brand); width: 16px; height: 16px; }

.section-label { font: 700 12px Montserrat, sans-serif; text-transform: uppercase; letter-spacing: .14em; color: var(--muted); margin: 28px 0 12px; }
.section-label:first-child { margin-top: 0; }

/* ---------------------------------------------------------------- folder cards */
.folders { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 18px; }
.folder-card {
  display: flex; flex-direction: column; text-decoration: none;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  overflow: hidden; box-shadow: var(--shadow); transition: transform .15s, box-shadow .15s;
}
@media (hover: hover) { .folder-card:hover { transform: translateY(-2px); box-shadow: 0 8px 28px rgba(0, 0, 0, .12); } }
.folder-cover { aspect-ratio: 4 / 3; background: var(--surface-2); position: relative; overflow: hidden; }
.folder-cover img { width: 100%; height: 100%; object-fit: cover; display: block; transition: transform .4s; }
@media (hover: hover) { .folder-card:hover .folder-cover img { transform: scale(1.04); } }
.folder-cover .placeholder { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); }
.folder-cover .placeholder svg { width: 44px; height: 44px; stroke-width: 1.4; opacity: .6; }
.folder-body { padding: 12px 14px 14px; }
.folder-name { font-weight: 600; font-size: 15px; overflow-wrap: anywhere; }
.folder-meta { color: var(--muted); font-size: 13px; margin-top: 2px; }

/* ---------------------------------------------------------------- file grid */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 12px; }
.tile {
  position: relative; display: block; border: 0; padding: 0; margin: 0; cursor: zoom-in;
  aspect-ratio: 1; border-radius: 8px; overflow: hidden; background: var(--surface-2);
  text-align: left; color: inherit; font: inherit;
}
.tile img { width: 100%; height: 100%; object-fit: cover; display: block; opacity: 0; transition: opacity .3s, transform .4s; }
.tile img.loaded { opacity: 1; }
@media (hover: hover) { .tile:hover img { transform: scale(1.04); } }
.tile:focus-visible { outline: 3px solid var(--brand); outline-offset: 2px; }
.tile-caption {
  position: absolute; left: 0; right: 0; bottom: 0;
  padding: 26px 10px 8px; font-size: 12px; color: #fff;
  background: linear-gradient(transparent, rgba(0, 0, 0, .7));
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  opacity: 0; transition: opacity .2s;
}
.tile:hover .tile-caption, .tile:focus-visible .tile-caption { opacity: 1; }
.tile-doc { cursor: pointer; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px; padding: 12px; text-decoration: none; border: 1px solid var(--border); background: var(--surface); }
.tile-doc .ext { font: 800 13px Montserrat, sans-serif; letter-spacing: .1em; color: #fff; background: var(--brand); padding: 6px 10px; border-radius: 6px; text-transform: uppercase; }
.tile-doc .doc-name { font-size: 13px; text-align: center; overflow-wrap: anywhere; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.tile-doc .doc-size { font-size: 12px; color: var(--muted); }
.tile .badge { position: absolute; top: 8px; left: 8px; background: rgba(0, 0, 0, .65); color: #fff; font-size: 11px; font-weight: 600; padding: 3px 7px; border-radius: 4px; }

/* ---------------------------------------------------------------- lightbox */
.lightbox {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(10, 10, 10, .97); color: #f2f2f2;
  display: grid; grid-template-columns: minmax(0, 1fr); grid-template-rows: auto minmax(0, 1fr) auto;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  animation: fade .18s ease-out;
}
@keyframes fade { from { opacity: 0; } }
.lb-top { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 12px 16px 12px 24px; }
.lb-meta { flex: 1; min-width: 0; }
.lb-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lb-info { font-size: 13px; color: #a8a8a8; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lb-actions { display: flex; gap: 8px; flex: none; }
.lb-btn {
  display: inline-flex; align-items: center; gap: 8px; height: 40px; padding: 0 14px;
  border-radius: 999px; border: 1px solid rgba(255, 255, 255, .18); background: rgba(255, 255, 255, .06);
  color: #fff; text-decoration: none; font: 600 14px Inter, sans-serif; cursor: pointer;
}
.lb-btn:hover { background: rgba(255, 255, 255, .16); }
.lb-btn.lb-icon { width: 40px; padding: 0; justify-content: center; }
.lb-stage { position: relative; min-height: 0; overflow: hidden; display: flex; align-items: center; justify-content: center; touch-action: none; user-select: none; -webkit-user-select: none; }
.lb-media { position: absolute; inset: 0 72px; display: flex; align-items: center; justify-content: center; }
.lb-media img, .lb-media video { max-width: 100%; max-height: 100%; object-fit: contain; display: block; user-select: none; -webkit-user-drag: none; transform-origin: center center; will-change: transform; }
.lb-media img { cursor: zoom-in; }
.lightbox.zoomed .lb-media img { cursor: grab; }
.lightbox.zoomed .lb-media img:active { cursor: grabbing; }
.lightbox.zoomed .lb-nav, .lightbox.zoomed .lb-strip { opacity: .25; }
.lb-media img.lb-placeholder { filter: blur(6px); }
.lb-media .spinner { position: absolute; width: 36px; height: 36px; border: 3px solid rgba(255, 255, 255, .2); border-top-color: #fff; border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.lb-nav {
  position: absolute; top: 50%; transform: translateY(-50%); z-index: 2;
  width: 52px; height: 52px; border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, .18); background: rgba(255, 255, 255, .06);
  color: #fff; cursor: pointer; display: grid; place-items: center;
}
.lb-nav:hover { background: rgba(255, 255, 255, .18); }
.lb-nav svg { width: 24px; height: 24px; }
.lb-nav:disabled { opacity: .2; cursor: default; }
.lb-prev { left: 12px; }
.lb-next { right: 12px; }
.lb-strip { display: flex; gap: 6px; overflow-x: auto; padding: 12px 16px 16px; scrollbar-width: thin; scroll-behavior: smooth; }
.lb-strip button { flex: none; width: 72px; height: 54px; padding: 0; border: 2px solid transparent; border-radius: 5px; overflow: hidden; background: #222; cursor: pointer; opacity: .5; transition: opacity .15s; }
.lb-strip button:hover { opacity: .85; }
.lb-strip button.active { border-color: #fff; opacity: 1; }
.lb-strip img { width: 100%; height: 100%; object-fit: cover; display: block; }

/* ---------------------------------------------------------------- admin */
.admin .topbar form { margin: 0; }
.admin-main { width: 100%; max-width: 1400px; margin: 0 auto; padding: 28px 32px 48px; }
.admin-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.admin h1 { font: 800 clamp(24px, 3vw, 32px)/1.1 Montserrat, sans-serif; text-transform: uppercase; margin: 0; }
.admin h2 { font: 700 13px Montserrat, sans-serif; text-transform: uppercase; letter-spacing: .1em; margin: 0 0 12px; }
.admin .muted { color: var(--muted); font-weight: 500; text-transform: none; letter-spacing: 0; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 20px; }
.chip { padding: 7px 14px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface); font-size: 13px; font-weight: 500; text-decoration: none; }
.chip.active { background: var(--brand); border-color: var(--brand); color: #fff; }
.chip-sep { width: 1px; height: 22px; background: var(--border); margin: 0 4px; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat, .admin-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.stat { padding: 16px 18px; }
.stat-num { font: 800 28px/1.1 Montserrat, sans-serif; }
.stat-label { color: var(--muted); font-size: 13px; margin-top: 4px; }
.admin-card { padding: 18px 20px; min-width: 0; }
.admin-card.wide { margin-bottom: 16px; }
.admin-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 420px), 1fr)); gap: 16px; margin-bottom: 16px; }
.bars { display: flex; align-items: flex-end; gap: 2px; height: 140px; }
.bar { flex: 1; min-width: 2px; height: max(var(--h), 1px); background: var(--brand); border-radius: 2px 2px 0 0; }
.bars-axis { display: flex; justify-content: space-between; color: var(--muted); font-size: 12px; margin-top: 6px; }
.table-wrap { overflow-x: auto; }
.admin table { width: 100%; border-collapse: collapse; font-size: 14px; }
.admin th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; padding: 6px 10px 8px 0; border-bottom: 1px solid var(--border); }
.admin td { padding: 8px 10px 8px 0; border-bottom: 1px solid var(--border); vertical-align: top; overflow-wrap: anywhere; }
.admin tr:last-child td { border-bottom: 0; }
.admin .num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.admin .nowrap { white-space: nowrap; }
.admin-foot { font-size: 13px; }
.admin-login { flex: 1; display: grid; place-items: center; padding: 32px 16px; }
.admin-login form { width: 100%; max-width: 360px; display: grid; gap: 14px; padding: 28px; }
.admin-login label { display: grid; gap: 6px; font-size: 14px; font-weight: 500; }
.admin-login input { font: inherit; padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); color: var(--text); }
.admin-login input:focus { outline: 2px solid var(--brand); outline-offset: 1px; }
.admin-login .btn { justify-content: center; padding: 12px; }
.admin-error { margin: 0; padding: 10px 12px; border-radius: 8px; background: rgba(192, 39, 43, .1); color: var(--brand); font-size: 14px; }
.admin-nav { display: flex; align-items: center; gap: 4px; min-width: 0; }
.admin-nav a { padding: 8px 12px; border-radius: 999px; text-decoration: none; font-size: 14px; font-weight: 500; color: var(--muted); white-space: nowrap; }
.admin-nav a:hover { color: var(--text); background: var(--surface-2); }
.admin-nav a.active { color: var(--text); background: var(--surface-2); font-weight: 600; }
.admin-nav form { margin: 0 0 0 8px; }
.btn-sm { padding: 7px 12px; font-size: 13px; }
.btn-danger { color: var(--brand); border-color: currentColor; }
.btn-danger:hover { background: var(--brand); color: #fff; border-color: var(--brand); }
.admin-main.narrow { max-width: 1000px; }
.admin .small { font-size: 12px; }
.admin-ok { margin: 0 0 16px; padding: 10px 12px; border-radius: 8px; background: rgba(34, 139, 34, .12); color: #1e7a1e; font-size: 14px; }
.admin-main .admin-error { margin-bottom: 16px; }
.admin input, .admin select { font: inherit; padding: 9px 11px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); color: var(--text); min-width: 0; }
.admin input:focus, .admin select:focus { outline: 2px solid var(--brand); outline-offset: 1px; }
.admin label { display: grid; gap: 6px; font-size: 14px; font-weight: 500; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; align-items: end; }
.form-grid .btn { justify-content: center; padding: 11px 16px; }
.form-stack { display: grid; gap: 14px; max-width: 360px; }
.form-stack .btn { justify-content: center; padding: 11px 16px; }
.users-table td { vertical-align: middle; }
.row-actions summary { list-style: none; }
.row-actions summary::-webkit-details-marker { display: none; }
.row-forms { display: grid; gap: 8px; margin-top: 10px; min-width: 260px; }
.row-forms form { display: flex; flex-wrap: wrap; gap: 6px; }
.row-forms input, .row-forms select { flex: 1; padding: 7px 10px; font-size: 13px; }
@media (max-width: 640px) {
  .admin-main { padding: 20px 16px 40px; } .stat-num { font-size: 24px; }
  .admin .brand-text { display: none; }
  .admin-nav a { padding: 8px 8px; font-size: 13px; }
  .admin-nav form { margin-left: 4px; }
}

/* ---------------------------------------------------------------- responsive */
/* Touch screens: finger-sized targets. */
@media (pointer: coarse) {
  .crumbs a, .crumbs .current { padding: 8px 6px; }
  .toggle input { width: 20px; height: 20px; }
  .tree a { padding: 9px 8px; }
  .tree-toggle { width: 32px; height: 32px; }
  .tree-toggle svg { width: 16px; height: 16px; }
  .lb-btn { height: 44px; }
  .lb-btn.lb-icon { width: 44px; }
}
/* Large screens: roomier grid. */
@media (min-width: 1800px) {
  .grid { grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 14px; }
  .folders { grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }
}
@media (max-width: 960px) {
  .sidebar { display: none; }
  .main { padding: 20px 16px 40px; }
}
@media (max-width: 640px) {
  :root { --topbar-h: 60px; }
  .topbar { padding: 0 16px; }
  .brand-mark { height: 32px; }
  .brand-name { font-size: 15px; }
  #zip-btn .sub { display: none; }
  #zip-btn { padding: 10px 14px; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; }
  .folders { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 12px; }
  .lb-media { inset: 0; }
  .lb-nav { display: none; }
  .lb-top { padding: 10px 12px; gap: 10px; }
  .lb-name { font-size: 14px; }
  .lb-info { font-size: 12px; }
  .lb-btn span { display: none; }
  .lb-btn { width: 40px; padding: 0; justify-content: center; }
  .lb-strip button { width: 56px; height: 42px; }
}
/* Short screens (phones in landscape): give the photo the height. */
@media (max-height: 500px) {
  .lb-strip { display: none; }
  .lb-top { padding: 6px 12px; }
  .lb-btn { height: 36px; }
  .lb-btn.lb-icon { width: 36px; }
  .lb-nav { width: 44px; height: 44px; }
  .lb-media { inset: 0 60px; }
  .topbar { position: static; }
  .sidebar { top: 0; max-height: 100dvh; }
}
@media (max-width: 420px) {
  .brand-text { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; }
}
