:root {
  color-scheme: light dark;
  --bg: #f4f5f7;
  --panel: #ffffff;
  --stage: #e3e5e8;
  --fg: #1c1e21;
  --muted: #6b7078;
  --border: #d5d8dd;
  --field: #ffffff;
  --accent: #0a66d8;
  --accent-fg: #ffffff;
  --ok: #1f9d55;
  --warn: #c98200;
  --bad: #d64541;
  --shadow: 0 8px 30px rgb(0 0 0 / .18);
  font: 14px/1.4 "Segoe UI", system-ui, -apple-system, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1d21; --panel: #23262b; --stage: #111316; --fg: #eceef1; --muted: #9aa0a8;
    --border: #353941; --field: #16181b; --accent: #3d8bfd; --shadow: 0 8px 30px rgb(0 0 0 / .5);
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--fg); }
button, select, input { font: inherit; color: inherit; }
button, select {
  height: 32px; padding: 0 12px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--panel); cursor: pointer; white-space: nowrap;
}
button:hover:not(:disabled), select:hover { border-color: color-mix(in srgb, var(--fg) 35%, var(--border)); }
button:disabled { opacity: .45; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); }
button.icon { width: 32px; padding: 0; font-size: 16px; }
button:focus-visible, select:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.muted { color: var(--muted); }

/* ------------------------------------------------ layout */
body.app { display: flex; flex-direction: column; overflow: hidden; }
.bar {
  display: flex; align-items: center; gap: 6px; padding: 8px 10px; background: var(--panel);
  border-bottom: 1px solid var(--border); flex-wrap: nowrap;
}
.url { flex: 1; min-width: 120px; }
.url input {
  width: 100%; height: 32px; padding: 0 14px; border-radius: 16px; border: 1px solid var(--border);
  background: var(--field); outline: none;
}
.url input:focus { border-color: var(--accent); }
.badge {
  display: inline-block; min-width: 20px; padding: 0 6px; margin-left: 4px; border-radius: 10px;
  background: rgb(255 255 255 / .25); font-size: 12px; line-height: 18px; text-align: center;
}
#toggleGallery.active { box-shadow: inset 0 0 0 2px rgb(255 255 255 / .5); }
.pill {
  display: inline-flex; align-items: center; gap: 6px; height: 28px; padding: 0 10px; border-radius: 14px;
  border: 1px solid var(--border); font-size: 12px; color: var(--muted); white-space: nowrap;
}
.pill .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--warn); }
.pill[data-state="online"] .dot { background: var(--ok); }
.pill[data-state="offline"] .dot { background: var(--bad); }

main { flex: 1; display: flex; min-height: 0; }
#stage {
  flex: 1; position: relative; min-width: 0; display: flex; align-items: center; justify-content: center;
  background: var(--stage); overflow: hidden;
}
#screen { display: block; background: #fff; box-shadow: var(--shadow); outline: none; image-rendering: auto; }
#stage.focused #screen { box-shadow: var(--shadow), 0 0 0 2px var(--accent); }
#keysink { position: absolute; left: 0; top: 0; width: 1px; height: 1px; opacity: 0; resize: none; border: 0; padding: 0; pointer-events: none; }
#overlay {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 12px; background: color-mix(in srgb, var(--stage) 85%, transparent); color: var(--muted); pointer-events: none;
}
.spinner {
  width: 28px; height: 28px; border-radius: 50%; border: 3px solid var(--border); border-top-color: var(--accent);
  animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ------------------------------------------------ gallery */
#gallery {
  width: min(440px, 45vw); display: flex; flex-direction: column; background: var(--panel);
  border-left: 1px solid var(--border); min-height: 0;
}
.g-head { display: flex; align-items: center; gap: 8px; padding: 10px 12px 6px; }
.g-head h2 { flex: 1; margin: 0; font-size: 15px; }
.g-filters { padding: 4px 12px; display: grid; gap: 6px; }
.g-filters input {
  height: 32px; padding: 0 10px; border-radius: 8px; border: 1px solid var(--border); background: var(--field);
}
.g-filters .row { display: flex; gap: 6px; }
.g-filters .row select { flex: 1; min-width: 0; padding: 0 6px; }
.g-select { display: flex; justify-content: space-between; align-items: center; padding: 6px 12px; font-size: 13px; }
.g-select label { display: flex; align-items: center; gap: 6px; cursor: pointer; }
#grid {
  flex: 1; overflow-y: auto; padding: 6px 12px 12px; display: grid; gap: 8px;
  grid-template-columns: repeat(auto-fill, minmax(118px, 1fr)); grid-auto-rows: max-content; align-content: start;
}
#grid .empty { grid-column: 1 / -1; text-align: center; padding: 30px 10px; }
.tile {
  position: relative; border-radius: 10px; border: 2px solid transparent; background: var(--bg);
  cursor: pointer; overflow: hidden; user-select: none;
}
.tile:hover { border-color: color-mix(in srgb, var(--accent) 40%, transparent); }
.tile.selected { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 12%, var(--bg)); }
.tile .thumb {
  aspect-ratio: 1; display: flex; align-items: center; justify-content: center;
  background: repeating-conic-gradient(color-mix(in srgb, var(--fg) 6%, transparent) 0% 25%, transparent 0% 50%) 0 0 / 16px 16px;
}
.tile img { max-width: 100%; max-height: 100%; object-fit: contain; display: block; }
.tile img:not([src]) { display: none; }
.tile.broken .thumb::after { content: "unavailable"; font-size: 12px; color: var(--muted); }
.tile .meta { display: flex; justify-content: space-between; gap: 4px; padding: 4px 6px; font-size: 11px; color: var(--muted); }
.tile .check {
  position: absolute; top: 6px; left: 6px; width: 20px; height: 20px; border-radius: 50%;
  border: 2px solid #fff; background: rgb(0 0 0 / .25); box-shadow: 0 1px 3px rgb(0 0 0 / .4);
}
.tile.selected .check { background: var(--accent); }
.tile.selected .check::after {
  content: ""; position: absolute; left: 5px; top: 1px; width: 5px; height: 10px;
  border: solid #fff; border-width: 0 2px 2px 0; transform: rotate(45deg);
}
.tile .zoom {
  position: absolute; top: 4px; right: 4px; width: 26px; height: 26px; padding: 0; border-radius: 6px;
  background: rgb(0 0 0 / .45); border: 0; color: #fff; opacity: 0; transition: opacity .15s;
}
.tile:hover .zoom { opacity: 1; }
.g-foot { display: flex; gap: 8px; align-items: center; padding: 10px 12px; border-top: 1px solid var(--border); }
.g-foot button { flex: 1; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg label { cursor: pointer; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span { display: block; padding: 6px 10px; font-size: 13px; }
.seg label + label span { border-left: 1px solid var(--border); }
.seg input:checked + span { background: var(--accent); color: var(--accent-fg); }
.seg input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: -2px; }

/* ------------------------------------------------ lightbox */
#lightbox {
  position: fixed; inset: 0; z-index: 20; display: flex; align-items: center; justify-content: center;
  background: rgb(0 0 0 / .75); padding: 24px;
}
#lightbox figure { margin: 0; display: flex; flex-direction: column; gap: 10px; max-width: 100%; max-height: 100%; }
#lightbox img { max-width: 92vw; max-height: calc(100vh - 120px); object-fit: contain; align-self: center; background: #fff; }
#lightbox figcaption {
  display: flex; gap: 12px; align-items: center; justify-content: space-between; color: #eee; font-size: 13px;
}
#lbCap { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 70vw; }
.lb-actions { display: flex; gap: 8px; }

/* ------------------------------------------------ toasts */
#toasts { position: fixed; left: 16px; bottom: 16px; z-index: 30; display: flex; flex-direction: column; gap: 8px; max-width: min(480px, calc(100vw - 32px)); }
.toast {
  padding: 10px 14px; border-radius: 10px; background: var(--panel); border: 1px solid var(--border);
  border-left: 4px solid var(--accent); box-shadow: var(--shadow); font-size: 13px; overflow-wrap: anywhere;
}
.toast.ok { border-left-color: var(--ok); }
.toast.warn { border-left-color: var(--warn); }
.toast.error { border-left-color: var(--bad); }

/* ------------------------------------------------ login */
body.login { display: flex; align-items: center; justify-content: center; padding: 16px; }
.card {
  width: min(340px, 100%); display: flex; flex-direction: column; align-items: center; gap: 10px; padding: 28px;
  border-radius: 14px; background: var(--panel); border: 1px solid var(--border); box-shadow: var(--shadow);
}
.card h1 { margin: 4px 0 0; font-size: 20px; }
.card p { margin: 0 0 8px; }
.card input {
  width: 100%; height: 38px; padding: 0 12px; border-radius: 8px; border: 1px solid var(--border); background: var(--field);
}
.card button { width: 100%; height: 38px; }
.error { color: var(--bad); }

/* ------------------------------------------------ narrow screens */
@media (max-width: 900px) {
  #fit, #quality, .pill #connText { display: none; }
  #gallery { position: absolute; right: 0; top: 49px; bottom: 0; width: min(420px, 100vw); z-index: 10; box-shadow: var(--shadow); }
}
