:root {
  --fg: #f5f5f7;
  --muted: rgba(245, 245, 247, 0.58);
  --glass: rgba(28, 28, 30, 0.58);
  --glass-strong: rgba(38, 38, 42, 0.86);
  --line: rgba(255, 255, 255, 0.12);
  --danger: #ff5c55;
  --ease: cubic-bezier(0.2, 0.8, 0.2, 1);
  color-scheme: dark;
}

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

html, body {
  height: 100%;
  background: #000;
  color: var(--fg);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Inter", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
[hidden] { display: none !important; }

svg {
  width: 20px; height: 20px; flex: none;
  fill: none; stroke: currentColor; stroke-width: 1.8;
  stroke-linecap: round; stroke-linejoin: round;
}

/* ------------------------------------------------------------ slideshow */
#stage { position: fixed; inset: 0; background: #000; cursor: pointer; }
#stage img {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  object-fit: cover;
  opacity: 0;
  transition: opacity 1.6s ease;
  will-change: opacity;
}
#stage img.on { opacity: 1; }
body.fit-contain #stage img { object-fit: contain; }

/* ---------------------------------------------------------- control bar */
.bar {
  position: fixed; left: 50%; z-index: 10;
  bottom: calc(22px + env(safe-area-inset-bottom));
  display: flex; gap: 2px; padding: 6px;
  border-radius: 999px;
  background: var(--glass);
  border: 1px solid var(--line);
  -webkit-backdrop-filter: blur(24px) saturate(1.6);
  backdrop-filter: blur(24px) saturate(1.6);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.35);
  opacity: 0; pointer-events: none;
  transform: translate(-50%, 18px);
  transition: opacity 0.3s var(--ease), transform 0.35s var(--ease);
}
body.ui .bar { opacity: 1; pointer-events: auto; transform: translate(-50%, 0); }
.bar button {
  display: flex; align-items: center; gap: 8px;
  padding: 12px 18px; border-radius: 999px;
  font-weight: 500; white-space: nowrap;
  transition: background 0.15s;
}
.bar button:active { background: rgba(255, 255, 255, 0.14); }
@media (hover: hover) { .bar button:hover { background: rgba(255, 255, 255, 0.1); } }
@media (max-width: 380px) {
  .bar button { padding: 13px 16px; }
  .bar button span { display: none; }
}

/* ---------------------------------------------------------- empty state */
#empty {
  position: fixed; inset: 0; z-index: 5;
  display: grid; place-items: center; text-align: center;
  padding: 24px;
  background: radial-gradient(circle at 50% 38%, #202027 0%, #000 65%);
}
.empty-card h1 { font-size: 46px; font-weight: 700; letter-spacing: -0.035em; }
.empty-card p { color: var(--muted); margin: 6px 0 28px; font-size: 17px; }
.empty-actions { display: flex; flex-direction: column; gap: 10px; align-items: center; }

.pill {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 14px 26px; border-radius: 999px;
  font-weight: 600; font-size: 16px;
  transition: transform 0.15s var(--ease), opacity 0.15s;
}
.pill:active { transform: scale(0.96); }
.pill.primary { background: #fff; color: #000; }
.pill.ghost { background: rgba(255, 255, 255, 0.1); }
.pill.small { padding: 9px 16px; font-size: 15px; }
.pill.small svg { width: 18px; height: 18px; stroke-width: 2.2; }

/* ---------------------------------------------------------- library sheet */
.sheet {
  position: fixed; inset: 0; z-index: 20;
  display: flex; flex-direction: column;
  background: rgba(12, 12, 14, 0.88);
  -webkit-backdrop-filter: blur(30px) saturate(1.4);
  backdrop-filter: blur(30px) saturate(1.4);
  transform: translateY(100%);
  visibility: hidden;
  transition: transform 0.45s var(--ease), visibility 0s linear 0.45s;
}
body.lib .sheet { transform: none; visibility: visible; transition: transform 0.45s var(--ease); }

.sheet-head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: calc(18px + env(safe-area-inset-top)) calc(20px + env(safe-area-inset-right)) 10px calc(20px + env(safe-area-inset-left));
}
.sheet-head h2 { font-size: 30px; font-weight: 700; letter-spacing: -0.03em; line-height: 1.1; }
.sub { color: var(--muted); font-size: 14px; margin-top: 2px; }
.head-actions { display: flex; align-items: center; gap: 8px; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 50%;
  display: grid; place-items: center;
  background: rgba(255, 255, 255, 0.1);
}
.icon-btn:active { background: rgba(255, 255, 255, 0.2); }

.scroll {
  flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain;
  padding: 0 calc(16px + env(safe-area-inset-right)) calc(28px + env(safe-area-inset-bottom)) calc(16px + env(safe-area-inset-left));
}

.settings {
  display: grid; gap: 12px;
  margin: 8px 0 18px; padding: 14px;
  border-radius: 16px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--line);
}
.row { display: flex; align-items: center; justify-content: space-between; gap: 10px 14px; flex-wrap: wrap; }
.label { color: var(--muted); font-size: 14px; }
.seg {
  display: flex; padding: 2px; border-radius: 10px;
  background: rgba(255, 255, 255, 0.08);
  max-width: 100%; overflow-x: auto; scrollbar-width: none;
}
.seg::-webkit-scrollbar { display: none; }
.seg button {
  padding: 7px 12px; border-radius: 8px;
  font-size: 14px; font-weight: 500; color: var(--muted);
  white-space: nowrap; transition: background 0.2s, color 0.2s;
}
.seg button.active { background: rgba(255, 255, 255, 0.2); color: var(--fg); }
.pin-row .label { color: var(--fg); }

.hint { color: var(--muted); font-size: 13px; margin: 0 4px 10px; }

.grid {
  display: grid; gap: 3px;
  grid-template-columns: repeat(auto-fill, minmax(104px, 1fr));
  border-radius: 14px; overflow: hidden;
}
@media (min-width: 700px) { .grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); } }

.tile { position: relative; aspect-ratio: 1; overflow: hidden; background: #1c1c1e; }
.tile img {
  display: block; width: 100%; height: 100%; object-fit: cover;
  transition: opacity 0.25s, transform 0.2s var(--ease), filter 0.25s;
}
.tile:active img { transform: scale(0.95); }
.tile.off img { opacity: 0.3; filter: grayscale(1); }
.tile.current::before {
  content: ""; position: absolute; inset: 0; z-index: 1; pointer-events: none;
  box-shadow: inset 0 0 0 3px #fff;
}
.badge {
  display: none; position: absolute; left: 6px; bottom: 6px; z-index: 2;
  padding: 3px 8px; border-radius: 999px;
  font-size: 11px; font-weight: 600; letter-spacing: 0.01em;
  background: rgba(0, 0, 0, 0.62);
  -webkit-backdrop-filter: blur(8px); backdrop-filter: blur(8px);
}
.tile.off .badge, .tile.current .badge, .tile.pinned .badge { display: block; }
.tile.off .badge::after { content: "Hidden"; }
.tile.current .badge::after { content: "On screen"; }
.tile.pinned .badge::after { content: "Pinned"; }

.lib-empty { color: var(--muted); text-align: center; padding: 48px 16px; }
.lib-empty b { color: var(--fg); }
.sheet-foot { display: flex; justify-content: center; padding: 28px 0 4px; }
.link { color: var(--muted); font-size: 14px; padding: 8px 12px; }
.link:active { color: var(--fg); }

/* --------------------------------------------------------- action sheet */
.modal {
  position: fixed; inset: 0; z-index: 30;
  display: flex; align-items: flex-end; justify-content: center;
  background: rgba(0, 0, 0, 0.5);
  opacity: 0; pointer-events: none;
  transition: opacity 0.25s;
}
.modal.open { opacity: 1; pointer-events: auto; }
.card {
  width: min(480px, 100%);
  padding: 0 10px calc(10px + env(safe-area-inset-bottom));
  transform: translateY(40px);
  transition: transform 0.4s var(--ease);
}
.modal.open .card { transform: none; }
@media (min-width: 700px) { .modal { align-items: center; } .card { padding-bottom: 0; } }

.preview { border-radius: 16px; overflow: hidden; background: #000; margin-bottom: 8px; }
.preview img { display: block; width: 100%; height: min(40vh, 340px); object-fit: contain; }
.menu, .cancel {
  background: var(--glass-strong);
  -webkit-backdrop-filter: blur(30px); backdrop-filter: blur(30px);
  border-radius: 16px; overflow: hidden;
}
.menu button {
  display: flex; align-items: center; gap: 14px; width: 100%;
  padding: 16px 18px; font-size: 17px; text-align: left;
  border-top: 1px solid var(--line);
}
.menu button:first-child { border-top: 0; }
.menu button:active, .cancel:active { background: rgba(255, 255, 255, 0.08); }
.menu .danger { color: var(--danger); }
.cancel { width: 100%; margin-top: 8px; padding: 16px; font-size: 17px; font-weight: 600; }

/* ------------------------------------------------------------- toast */
#toast {
  position: fixed; left: 0; right: 0; z-index: 40;
  top: calc(16px + env(safe-area-inset-top));
  margin: 0 auto; width: max-content; max-width: calc(100% - 32px);
  padding: 10px 18px; border-radius: 999px;
  background: var(--glass-strong); border: 1px solid var(--line);
  -webkit-backdrop-filter: blur(20px); backdrop-filter: blur(20px);
  font-size: 14px; font-weight: 500; text-align: center;
  opacity: 0; transform: translateY(-16px);
  transition: opacity 0.3s var(--ease), transform 0.3s var(--ease);
  pointer-events: none;
}
#toast.show { opacity: 1; transform: none; }

#drop {
  position: fixed; inset: 16px; z-index: 50;
  display: grid; place-items: center;
  border: 2px dashed rgba(255, 255, 255, 0.5); border-radius: 24px;
  background: rgba(0, 0, 0, 0.7);
  font-size: 22px; font-weight: 600;
  opacity: 0; pointer-events: none; transition: opacity 0.2s;
}
body.dragging #drop { opacity: 1; }

/* ------------------------------------------------------------- login */
.login {
  position: fixed; inset: 0;
  display: grid; place-items: center; padding: 24px;
  background: radial-gradient(circle at 50% 32%, #25252e 0%, #000 65%);
}
.login form { width: min(360px, 100%); display: grid; gap: 12px; text-align: center; }
.login h1 { font-size: 46px; font-weight: 700; letter-spacing: -0.035em; }
.login p { color: var(--muted); margin-bottom: 18px; }
.login input {
  width: 100%; padding: 16px 18px;
  border-radius: 14px; border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.06); color: var(--fg);
  font: inherit; font-size: 17px; outline: none;
  -webkit-user-select: text; user-select: text;
  transition: border-color 0.2s, background 0.2s;
}
.login input:focus { border-color: rgba(255, 255, 255, 0.45); background: rgba(255, 255, 255, 0.09); }
.login .pill { width: 100%; padding: 16px; font-size: 17px; }
.login .pill:disabled { opacity: 0.5; }
.err { color: var(--danger); font-size: 14px; min-height: 20px; }
