:root {
  --bg: #0b0b0d;
  --panel: #151518;
  --panel-2: #1c1c20;
  --line: rgba(255, 255, 255, 0.08);
  --line-strong: rgba(255, 255, 255, 0.16);
  --text: #f2efe8;
  --muted: #8f8c94;
  --accent: #e0a458;
  --accent-ink: #1a1208;
  --danger: #e4645c;
  --ok: #7fc98a;
  --radius: 12px;
  --font-body: "Instrument Sans", system-ui, sans-serif;
  --font-display: "Fraunces", Georgia, serif;
  --font-mono: ui-monospace, "SF Mono", Menlo, monospace;
}

* { box-sizing: border-box; margin: 0; }
[hidden] { display: none !important; }

body {
  font: 15px/1.5 var(--font-body);
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

h1 {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 28px;
  letter-spacing: 0.01em;
}
h1 em { font-style: italic; color: var(--accent); }

input, textarea, select, button {
  font: inherit;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 9px 13px;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}
input::placeholder { color: var(--muted); }
input:focus, textarea:focus, select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(224, 164, 88, 0.18);
}
button:focus-visible {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(224, 164, 88, 0.22);
}
select { appearance: none; padding-right: 30px; background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%); background-position: calc(100% - 16px) 55%, calc(100% - 11px) 55%; background-size: 5px 5px; background-repeat: no-repeat; }
button { cursor: pointer; font-weight: 500; }
button:hover { border-color: var(--line-strong); }

/* ---------- login ---------- */

#login {
  min-height: 100vh;
  display: grid;
  place-items: center;
  background:
    radial-gradient(60% 50% at 50% 0%, rgba(224, 164, 88, 0.07), transparent 70%),
    var(--bg);
}
#login-form {
  display: grid;
  gap: 14px;
  width: min(320px, 90vw);
  text-align: center;
}
#login-form h1 { font-size: 40px; }
#login-form .tagline { color: var(--muted); margin: -8px 0 10px; }
#login-form button {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
}
#login-form button:hover { filter: brightness(1.08); }
#login-error { color: var(--danger); font-size: 13px; }

/* ---------- header ---------- */

#app header {
  position: sticky;
  top: 0;
  z-index: 10;
  background: rgba(11, 11, 13, 0.82);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--line);
  padding: 16px 24px 14px;
  display: grid;
  gap: 12px;
}
.bar { display: flex; align-items: center; justify-content: space-between; gap: 16px; }
.bar h1 span {
  font-family: var(--font-body);
  font-size: 14px;
  color: var(--muted);
  font-weight: 400;
  margin-left: 6px;
}
#logout {
  background: transparent;
  border-color: var(--line);
  color: var(--muted);
  font-size: 13px;
  padding: 6px 12px;
  white-space: nowrap;
}
#logout:hover { color: var(--text); border-color: var(--line-strong); }
.header-actions { display: flex; align-items: center; gap: 8px; }
#view-toggle {
  display: flex;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--panel);
}
#view-toggle button {
  padding: 3px 10px;
  border: 0;
  border-radius: 6px;
  color: var(--muted);
  background: transparent;
  font-size: 12px;
}
#view-toggle button[aria-pressed="true"] {
  color: var(--accent-ink);
  background: var(--accent);
}

#filters { display: flex; gap: 10px; flex-wrap: wrap; }
#q { flex: 1; min-width: 180px; }
#filters input[type="date"] { color-scheme: dark; }

/* ---------- grid ---------- */

#grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 14px;
  padding: 20px 24px 24px;
}
.card {
  position: relative;
  display: block;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--panel);
  aspect-ratio: 1;
  animation: rise 0.45s cubic-bezier(0.2, 0.7, 0.3, 1) backwards;
}
.card-media {
  display: block;
  width: 100%;
  height: 100%;
  background-color: #111114;
  background-image:
    linear-gradient(45deg, #1b1b1f 25%, transparent 25%),
    linear-gradient(-45deg, #1b1b1f 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #1b1b1f 75%),
    linear-gradient(-45deg, transparent 75%, #1b1b1f 75%);
  background-size: 18px 18px;
  background-position: 0 0, 0 9px, 9px -9px, -9px 0;
}
.card img, .card video {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.35s cubic-bezier(0.2, 0.7, 0.3, 1), opacity 0.2s;
}
.card:hover { border-color: var(--line-strong); }
.card:hover img, .card:hover video { transform: scale(1.045); }
.card-info {
  position: absolute;
  inset: auto 0 0 0;
  padding: 26px 10px 9px;
  font-size: 12px;
  color: rgba(255, 255, 255, 0.92);
  text-align: left;
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.75));
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  opacity: 0;
  transition: opacity 0.2s;
}
.card:hover .card-info, .card:focus-visible .card-info { opacity: 1; }
.card-name { display: block; overflow: hidden; text-overflow: ellipsis; }
.card-details, .kind-badge { display: none; }

#grid.list-view {
  display: grid;
  grid-template-columns: 1fr;
  gap: 7px;
  max-width: 1100px;
  margin: 0 auto;
}
.list-view .card {
  display: grid;
  grid-template-columns: 72px minmax(0, 1fr);
  align-items: center;
  aspect-ratio: auto;
  min-height: 70px;
  text-align: left;
  overflow: visible;
}
.list-view .card-media {
  width: 72px;
  height: 68px;
  border-radius: 11px 0 0 11px;
  overflow: hidden;
}
.list-view .card-info {
  position: static;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 190px 74px;
  align-items: center;
  gap: 20px;
  padding: 10px 16px;
  color: var(--text);
  background: none;
  opacity: 1;
}
.list-view .card-name { font-size: 14px; font-weight: 500; }
.list-view .card-details { display: block; color: var(--muted); font-size: 12px; }
.list-view .kind-badge {
  display: inline-block;
  justify-self: end;
  padding: 3px 7px;
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  color: var(--muted);
  font-size: 10px;
  letter-spacing: 0.08em;
  text-align: center;
  text-transform: uppercase;
}
.list-view .kind-badge.video { color: var(--accent); border-color: rgba(224, 164, 88, 0.35); }
.list-view .card:hover img, .list-view .card:hover video { transform: none; }

@keyframes rise {
  from { opacity: 0; transform: translateY(10px); }
}

#empty, #load-error, #sentinel { text-align: center; color: var(--muted); padding: 28px; }
#empty { padding: 72px 24px; }
#empty strong { display: block; color: var(--text); font-family: var(--font-display); font-size: 24px; }
#empty p { margin-top: 5px; }
#load-error { color: var(--danger); }
#loading {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 150px;
  color: var(--muted);
}
#loading span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
  animation: loading-pulse 0.9s ease-in-out infinite alternate;
}
#loading span:nth-child(2) { animation-delay: 0.15s; }
#loading span:nth-child(3) { animation-delay: 0.3s; }
#loading p { margin-left: 7px; font-size: 13px; }
@keyframes loading-pulse { to { opacity: 0.25; transform: translateY(-3px); } }

/* ---------- modal ---------- */

#modal {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(5, 5, 7, 0.75);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  display: grid;
  place-items: center;
  padding: 24px;
  animation: fade 0.15s;
}
@keyframes fade { from { opacity: 0; } }
#modal-box {
  position: relative;
  width: min(960px, 100%);
  max-height: 88vh;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(340px, 390px);
  background: var(--panel);
  border: 1px solid var(--line-strong);
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 30px 80px rgba(0, 0, 0, 0.5);
}
#modal-stage { position: relative; display: grid; min-width: 0; background: #050506; }
#modal-media { display: grid; min-height: 420px; overflow: hidden; }
#modal-media.checkerboard {
  background-color: #111114;
  background-image:
    linear-gradient(45deg, #25252a 25%, transparent 25%),
    linear-gradient(-45deg, #25252a 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #25252a 75%),
    linear-gradient(-45deg, transparent 75%, #25252a 75%);
  background-size: 24px 24px;
  background-position: 0 0, 0 12px, 12px -12px, -12px 0;
}
#modal-media img, #modal-media video {
  width: 100%;
  height: 100%;
  max-height: 88vh;
  object-fit: contain;
}
#modal-info {
  min-width: 0;
  padding: 24px;
  display: grid;
  gap: 14px;
  align-content: start;
  overflow-y: auto;
  border-left: 1px solid var(--line);
}
.modal-heading { display: flex; align-items: center; justify-content: space-between; gap: 16px; }
#modal-stage, #modal-info, #modal-usage { min-width: 0; } /* grid/flex blowout guard: long titles must not stretch the modal */
#modal-usage { display: grid; gap: 8px; }
#usage-body { color: var(--muted); font-size: 13px; display: grid; gap: 6px; min-width: 0; }
.usage-row {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  padding: 8px 10px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
  text-decoration: none;
  transition: border-color 0.15s;
}
.usage-row:hover { border-color: var(--accent); }
.usage-type {
  flex-shrink: 0;
  padding: 2px 8px;
  border-radius: 999px;
  background: rgba(224, 164, 88, 0.14);
  color: var(--accent);
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.usage-title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.usage-status { flex-shrink: 0; padding: 2px 8px; border-radius: 999px; background: rgba(255,255,255,0.07); color: var(--muted); font-size: 10px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.08em; }
.usage-none { color: var(--ok); padding: 8px 10px; border: 1px dashed var(--line); border-radius: 8px; }
.eyebrow {
  color: var(--accent);
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}
#modal-key {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--muted);
  overflow-wrap: anywhere;
  color: var(--text);
  line-height: 1.65;
  white-space: normal;
}
#modal-meta { font-size: 13px; color: var(--muted); }
#modal-info label { color: var(--muted); font-size: 11px; font-weight: 600; letter-spacing: 0.08em; text-transform: uppercase; }
#modal-url {
  width: 100%;
  min-width: 0;
  resize: none;
  font-family: var(--font-mono);
  font-size: 12px;
  line-height: 1.55;
  background: var(--panel-2);
  overflow-wrap: anywhere;
}
.copy-actions { display: grid; grid-template-columns: 1fr auto; gap: 8px; }
.copy-hint { font-size: 12px; color: var(--muted); }
#copy-ghost {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
  white-space: nowrap;
}
#copy-ghost:hover { filter: brightness(1.08); }
#copy-url { background: transparent; }
#copied { color: var(--ok); font-size: 13px; }
#close {
  flex: 0 0 auto;
  width: 34px;
  height: 34px;
  padding: 0;
  border-radius: 50%;
  font-size: 18px;
  line-height: 1;
  background: rgba(20, 20, 24, 0.85);
  border-color: var(--line-strong);
}
.modal-nav {
  position: absolute;
  z-index: 2;
  top: 50%;
  width: 42px;
  height: 42px;
  padding: 0;
  border-radius: 50%;
  background: rgba(20, 20, 24, 0.88);
  border-color: var(--line-strong);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  transform: translateY(-50%);
}
.modal-nav:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); }
.modal-nav:disabled { cursor: default; opacity: 0.28; }
#previous { left: 14px; }
#next { right: 14px; }

/* ---------- context menu ---------- */

#menu {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 30;
  display: grid;
  min-width: 170px;
  padding: 5px;
  background: var(--panel-2);
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.55);
  animation: fade 0.1s;
}
#menu button {
  background: transparent;
  border: none;
  text-align: left;
  padding: 8px 12px;
  border-radius: 6px;
  font-size: 13.5px;
}
#menu button:hover { background: rgba(255, 255, 255, 0.07); }
#menu-ghost { color: var(--accent); font-weight: 600; }
#toast {
  position: fixed;
  z-index: 40;
  right: 18px;
  bottom: 18px;
  max-width: min(360px, calc(100vw - 36px));
  padding: 11px 15px;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--line-strong);
  border-left: 3px solid var(--accent);
  border-radius: 9px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.55);
  font-size: 13px;
  animation: fade 0.15s;
}

/* ---------- mobile ---------- */

@media (max-width: 900px) {
  #modal-box { grid-template-columns: 1fr; }
  #modal-media img, #modal-media video { max-height: 52vh; }
}

@media (max-width: 640px) {
  h1 { font-size: 23px; }
  #app header { padding: 12px 14px 10px; }
  .bar { align-items: flex-start; }
  .header-actions { gap: 6px; }
  #view-toggle button { padding-inline: 8px; }
  #logout { padding-inline: 9px; }
  #filters { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
  #q { grid-column: 1 / -1; width: 100%; }
  #ext { grid-column: 1 / -1; }
  #filters input[type="date"] { min-width: 0; width: 100%; }
  #grid {
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 10px;
    padding: 14px;
  }
  #grid.list-view { gap: 7px; }
  .list-view .card { grid-template-columns: 58px minmax(0, 1fr); min-height: 58px; }
  .list-view .card-media { width: 58px; height: 56px; }
  .list-view .card-info { grid-template-columns: minmax(0, 1fr) auto; gap: 6px 10px; padding: 8px 10px; }
  .list-view .card-name { grid-column: 1 / -1; }
  .list-view .card-details { white-space: nowrap; }
  #modal { padding: 0; }
  #modal-box {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(220px, 48vh) minmax(0, 1fr);
    max-height: 100vh;
    height: 100%;
    border-radius: 0;
  }
  #modal-media { min-height: 0; }
  #modal-media img, #modal-media video { max-height: 48vh; }
  #modal-info { padding: 18px; overflow-y: auto; border-left: none; border-top: 1px solid var(--line); }
  .copy-actions { grid-template-columns: 1fr; }
  #previous { left: 10px; }
  #next { right: 10px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
