:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #eef0f4;
  --text: #15181d;
  --muted: #5d6573;
  --border: #dde1e8;
  --accent: #4f46e5;
  --accent-text: #ffffff;
  --accent-soft: #e7e6fd;
  --star: #f5b301;
  --danger: #c2410c;
  --tile-bg: #e4e7ec;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 4px 14px rgba(16, 24, 40, .08);
  --radius: 10px;
  --row-h: 150px;
  --gap: 6px;
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #171a21;
    --surface-2: #20242d;
    --text: #e8eaef;
    --muted: #9aa3b2;
    --border: #2b303b;
    --accent: #8b85ff;
    --accent-text: #0f1115;
    --accent-soft: #272552;
    --tile-bg: #232833;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 6px 18px rgba(0, 0, 0, .35);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
/* keep focused / scrolled-to tiles clear of the sticky top bar */
html { scroll-padding-top: 84px; scroll-padding-bottom: 70px; }
body {
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.4;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}
.sprite { position: absolute; width: 0; height: 0; overflow: hidden; }
.offscreen { position: fixed; top: -1000px; left: -1000px; opacity: 0; }
svg { width: 18px; height: 18px; fill: currentColor; flex: none; }
button, input, select { font: inherit; color: inherit; }
a { color: var(--accent); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 16px;
  padding: 10px 20px;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; letter-spacing: -.01em; }
.brand-mark {
  display: inline-grid; place-items: center;
  width: 34px; height: 26px; border-radius: 7px;
  background: var(--accent); color: var(--accent-text);
  font-size: 11px; font-weight: 800; letter-spacing: .04em;
}
.brand-name { font-size: 17px; }
.provider-switch {
  display: inline-flex; padding: 3px; gap: 2px;
  background: var(--surface-2); border-radius: 999px; border: 1px solid var(--border);
}
.provider-switch button {
  border: 0; background: transparent; cursor: pointer;
  padding: 5px 14px; border-radius: 999px; font-size: 13px; font-weight: 700; letter-spacing: .03em; color: var(--muted);
}
.provider-switch button[aria-checked="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.sync {
  font-size: 12px; color: var(--muted);
  display: inline-flex; align-items: center; gap: 6px; white-space: nowrap;
}
.sync::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.sync[data-status="synced"]::before { background: #16a34a; }
.sync[data-status="pending"]::before, .sync[data-status="syncing"]::before { background: #eab308; }
.sync[data-status="auth"]::before, .sync[data-status="error"]::before { background: var(--danger); }
.sync[data-status="auth"] { color: var(--danger); cursor: pointer; text-decoration: underline; }
.icon-btn {
  display: inline-grid; place-items: center; width: 36px; height: 36px;
  border-radius: 9px; border: 1px solid transparent; background: transparent; cursor: pointer; color: var(--muted);
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }

/* ---------- search ---------- */
.search-area { padding: 18px 20px 6px; max-width: 1200px; width: 100%; margin: 0 auto; }
.search {
  display: flex; align-items: center; gap: 10px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
  padding: 6px 6px 6px 14px; box-shadow: var(--shadow);
}
.search:focus-within { border-color: var(--accent); }
.search-icon { color: var(--muted); }
.search input {
  flex: 1; min-width: 0; border: 0; outline: 0; background: transparent;
  font-size: 17px; padding: 8px 0;
}
.kbd {
  font: 12px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  border: 1px solid var(--border); border-bottom-width: 2px; border-radius: 5px;
  padding: 1px 6px; color: var(--muted); background: var(--surface-2);
}
.search .kbd { margin-right: 2px; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--border); background: var(--surface); cursor: pointer;
  padding: 8px 14px; border-radius: 10px; font-size: 14px; font-weight: 600;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.07); }
.btn.ghost { background: transparent; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; min-height: 0; }
.chip {
  display: inline-flex; align-items: center; gap: 2px;
  border: 1px solid var(--border); background: var(--surface); border-radius: 999px;
  font-size: 13px; color: var(--muted);
}
.chip button { border: 0; background: transparent; cursor: pointer; padding: 4px 4px 4px 11px; color: inherit; }
.chip button.chip-x { padding: 4px 8px 4px 2px; opacity: .6; }
.chip button.chip-x svg { width: 12px; height: 12px; }
.chip:hover { color: var(--text); border-color: var(--accent); }

/* ---------- tabs ---------- */
.tabs {
  display: flex; gap: 4px; padding: 8px 20px 0; max-width: 1200px; width: 100%; margin: 0 auto;
  border-bottom: 1px solid var(--border);
}
.tabs button {
  border: 0; background: transparent; cursor: pointer;
  padding: 10px 14px; font-weight: 600; color: var(--muted);
  border-bottom: 2px solid transparent; margin-bottom: -1px;
}
.tabs button[aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); }
.count {
  display: inline-block; min-width: 20px; padding: 0 6px; margin-left: 4px;
  border-radius: 999px; background: var(--surface-2); font-size: 12px; text-align: center;
}
.tabs button[aria-selected="true"] .count { background: var(--accent-soft); }

/* ---------- panels ---------- */
main { flex: 1; width: 100%; max-width: 1200px; margin: 0 auto; padding: 14px 20px 30px; }
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 4px 0 12px; flex-wrap: wrap; }
.panel-head h2 { font-size: 16px; margin: 0; font-weight: 700; }
.attribution {
  font-size: 11px; font-weight: 800; letter-spacing: .08em; text-transform: uppercase;
  color: var(--muted); text-decoration: none; padding: 4px 8px; border-radius: 6px; background: var(--surface-2);
}
.attribution:hover { color: var(--text); }
.filter {
  border: 1px solid var(--border); background: var(--surface); border-radius: 10px; padding: 7px 12px; min-width: 240px;
}
.provider-section { margin-bottom: 26px; }
.provider-section h3 { display: flex; align-items: center; gap: 8px; font-size: 13px; letter-spacing: .06em; margin: 0 0 10px; color: var(--muted); }
.provider-section .panel-head { margin-bottom: 8px; }
.empty {
  text-align: center; color: var(--muted); padding: 50px 20px;
  border: 1px dashed var(--border); border-radius: var(--radius);
}
.empty strong { display: block; color: var(--text); margin-bottom: 4px; font-size: 15px; }
.status { text-align: center; color: var(--muted); padding: 16px; font-size: 14px; min-height: 20px; }
.status .btn { margin-left: 8px; }
#sentinel { height: 1px; }
.banner {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 10px 14px; margin-bottom: 14px; border-radius: var(--radius);
  background: color-mix(in srgb, var(--danger) 12%, var(--surface)); border: 1px solid color-mix(in srgb, var(--danger) 35%, var(--border));
}
.banner .btn { padding: 5px 12px; }

/* ---------- justified grid (row-major order, no JS layout) ---------- */
.grid { display: flex; flex-wrap: wrap; gap: var(--gap); }
.grid::after { content: ""; flex-grow: 100000; }
.tile {
  --ar: 1;
  position: relative;
  flex: var(--ar) 1 calc(var(--ar) * var(--row-h));
  aspect-ratio: var(--ar);
  min-width: 0;
  max-width: 100%;
  border-radius: 8px;
  overflow: hidden;
  background: var(--tile-bg);
  container-type: inline-size;
}
.tile.skeleton { animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .55; } }
.tile-copy {
  display: block; width: 100%; height: 100%; padding: 0; border: 0; background: transparent; cursor: copy;
}
.tile-copy img { display: block; width: 100%; height: 100%; object-fit: cover; }
.tile-broken { display: none; }
.tile.broken img { display: none; }
.tile.broken .tile-broken { display: grid; place-items: center; height: 100%; color: var(--muted); font-size: 12px; }
.tile:hover, .tile:has(:focus-visible) { box-shadow: 0 0 0 2px var(--accent); }
/* Buttons fill from the top-right corner (star first) and wrap inside narrow tiles. */
.tile-actions {
  position: absolute; top: 6px; right: 6px; left: 6px;
  display: flex; flex-direction: row-reverse; flex-wrap: wrap; justify-content: flex-start; gap: 4px;
  pointer-events: none;
  opacity: 0; transform: translateY(-3px); transition: opacity .12s, transform .12s;
}
.tile-actions > * { pointer-events: auto; }
.tile:hover .tile-actions, .tile:has(:focus-visible) .tile-actions { opacity: 1; transform: none; }
.act {
  display: inline-grid; place-items: center; width: 30px; height: 30px; border-radius: 8px;
  border: 0; cursor: pointer; color: #fff; background: rgba(15, 17, 21, .62); backdrop-filter: blur(4px);
}
.act:hover { background: rgba(15, 17, 21, .85); }
.act svg { width: 16px; height: 16px; }
.act.fav svg { fill: none; stroke: currentColor; stroke-width: 2; stroke-linejoin: round; }
.act.fav[aria-pressed="true"] { color: var(--star); }
.act.fav[aria-pressed="true"] svg { fill: currentColor; }
/* keep the star visible when favorited */
.tile .tile-actions:has(.fav[aria-pressed="true"]) { opacity: 1; transform: none; }
/* faded (not visibility:hidden) so keyboard Tab can still reach them */
.tile:not(:hover):not(:has(:focus-visible)) .tile-actions .act:not(.fav[aria-pressed="true"]) { opacity: 0; }
.tile:not(:hover):not(:has(:focus-visible)) .tile-actions .act.fav[aria-pressed="true"] { background: rgba(15, 17, 21, .45); }
.tile.copied::after {
  content: "Copied"; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
  background: var(--accent); color: var(--accent-text); font-size: 12px; font-weight: 700;
  padding: 4px 10px; border-radius: 7px; pointer-events: none; box-shadow: 0 2px 10px rgba(0, 0, 0, .3);
}

/* Link / Image copy buttons along the bottom of each GIF */
.tile-copybar {
  position: absolute; left: 6px; right: 6px; bottom: 6px;
  display: flex; justify-content: center; gap: 4px;
  pointer-events: none;
  opacity: 0; transform: translateY(3px); transition: opacity .12s, transform .12s;
}
.tile-copybar > * { pointer-events: auto; }
.tile:hover .tile-copybar, .tile:has(:focus-visible) .tile-copybar { opacity: 1; transform: none; }
.cp {
  display: inline-flex; align-items: center; justify-content: center; gap: 5px;
  height: 28px; padding: 0 10px; min-width: 0;
  border: 0; border-radius: 8px; cursor: pointer;
  background: rgba(15, 17, 21, .72); color: #fff; backdrop-filter: blur(4px);
  font-size: 12px; font-weight: 700; letter-spacing: .01em; white-space: nowrap;
}
.cp:hover { background: rgba(15, 17, 21, .92); }
.cp svg { width: 14px; height: 14px; }
/* the button that matches a plain click gets a ring */
body[data-copy-default="link"] .cp-link,
body[data-copy-default="image"] .cp-image { box-shadow: inset 0 0 0 1.5px rgba(255, 255, 255, .7); }
/* narrow GIFs: icons only */
@container (max-width: 150px) {
  .cp { padding: 0 8px; }
  .cp span { display: none; }
}
@media (hover: none) {
  .tile-actions { opacity: 1; transform: none; top: 4px; right: 4px; left: 4px; gap: 3px; }
  .tile:not(:hover):not(:has(:focus-visible)) .tile-actions .act:not(.fav[aria-pressed="true"]) { opacity: 1; }
  .act { width: 26px; height: 26px; border-radius: 7px; }
  .act.open { display: none; } /* less clutter on touch screens */
  .tile-copybar { opacity: 1; transform: none; left: 4px; right: 4px; bottom: 4px; gap: 3px; justify-content: flex-end; }
  .cp { height: 26px; padding: 0 7px; border-radius: 7px; }
  .cp span { display: none; }
  .act svg { width: 14px; height: 14px; }
}

/* ---------- footer / toast ---------- */
.hint { text-align: center; color: var(--muted); font-size: 12px; padding: 12px 20px 18px; display: flex; gap: 4px; justify-content: center; align-items: center; flex-wrap: wrap; }
.hint-star { width: 13px; height: 13px; color: var(--star); }
.toast {
  position: fixed; left: 50%; bottom: 22px; transform: translateX(-50%); z-index: 50;
  display: flex; align-items: center; gap: 14px;
  background: #15181d; color: #fff; padding: 10px 14px 10px 16px; border-radius: 12px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, .3); font-size: 14px; max-width: calc(100vw - 32px);
}
.toast.error { background: #7c2d12; }
.toast-action { border: 0; background: transparent; color: #a5b4fc; font-weight: 700; cursor: pointer; padding: 2px 4px; }

/* ---------- settings dialog ---------- */
dialog {
  border: 1px solid var(--border); border-radius: 16px; padding: 0; width: min(560px, calc(100vw - 24px));
  background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgba(0, 0, 0, .35);
}
dialog::backdrop { background: rgba(10, 12, 16, .45); }
.settings-form { display: flex; flex-direction: column; max-height: min(86vh, 820px); }
.dialog-head, .dialog-foot { display: flex; align-items: center; justify-content: space-between; padding: 14px 18px; }
.dialog-head { border-bottom: 1px solid var(--border); }
.dialog-head h2 { margin: 0; font-size: 17px; }
.dialog-foot { border-top: 1px solid var(--border); justify-content: flex-end; }
.settings-scroll { overflow: auto; flex: 1 1 auto; min-height: 0; }
.settings-form fieldset { border: 0; margin: 0; padding: 12px 18px; border-bottom: 1px solid var(--border); min-width: 0; }
.settings-scroll fieldset:last-child { border-bottom: 0; }
.settings-form legend { font-weight: 700; font-size: 13px; padding: 0; margin-bottom: 6px; float: left; width: 100%; }
.settings-form legend + * { clear: both; }
.radio, .check { display: flex; align-items: flex-start; gap: 8px; padding: 3px 0; cursor: pointer; }
.radio input, .check input { margin-top: 3px; flex: none; }
.stack { display: flex; flex-direction: column; gap: 4px; margin: 6px 0; font-size: 13px; }
.stack input, select, .num {
  border: 1px solid var(--border); background: var(--surface-2); border-radius: 8px; padding: 7px 10px;
}
.num { width: 72px; margin: 0 4px; }
.muted { color: var(--muted); }
.small { font-size: 12px; margin: 4px 0; }
.row-btns { display: flex; gap: 8px; flex-wrap: wrap; }
.file-btn { cursor: pointer; }

@media (max-width: 640px) {
  :root { --row-h: 104px; }
  .topbar { padding: 8px 12px; gap: 10px; flex-wrap: wrap; }
  .brand-name { display: none; }
  .search-area, main { padding-left: 12px; padding-right: 12px; }
  .tabs { padding-left: 12px; padding-right: 12px; }
  .search .kbd { display: none; }
  .filter { min-width: 0; width: 100%; }
  .sync { font-size: 11px; }
}
@media (prefers-reduced-motion: reduce) {
  .tile.skeleton { animation: none; }
  .tile-actions, .tile-copybar { transition: none; }
}
