:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --text: #1c1f24;
  --muted: #6b7280;
  --line: #dfe2e7;
  --accent: #0b5fff;
  --accent-text: #ffffff;
  --danger: #c62828;
  --hover: #eef2ff;
  --badge-bd: #1565c0;
  --badge-4k: #111111;
  --badge-3d: #6a1b9a;
  --badge-dvd: #7a5c00;
  --warn-bg: #fff6e0;
  --error-bg: #fdecea;
  --info-bg: #eaf2ff;
  --radius: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --panel: #1d2025;
    --text: #e8eaed;
    --muted: #9aa0a8;
    --line: #2e333a;
    --accent: #5b8cff;
    --accent-text: #0b0d10;
    --danger: #ef5350;
    --hover: #262b36;
    --badge-4k: #e8eaed;
    --warn-bg: #3a2f12;
    --error-bg: #3d1a1a;
    --info-bg: #17243d;
    color-scheme: dark;
  }
  .badge.t-4kultrahd { color: #111; }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

button, input { font: inherit; color: inherit; }

button {
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 8px;
  padding: 7px 14px;
  cursor: pointer;
}
button:hover:not(:disabled) { background: var(--hover); }
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
button.primary:hover:not(:disabled) { filter: brightness(1.08); background: var(--accent); }
button.danger { color: var(--danger); border-color: var(--danger); }
button.link { border: 0; background: none; padding: 4px 0; color: var(--accent); }
button.link:hover { background: none; text-decoration: underline; }
button.ghost { background: none; }

input {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
}
input:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.muted { color: var(--muted); }
.small { font-size: 13px; }

/* ---------- top bar ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  gap: 16px;
  align-items: center;
  padding: 12px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: baseline; gap: 10px; white-space: nowrap; }
.brand h1 { font-size: 20px; margin: 0; }
#search { flex: 1; max-width: 520px; }
.actions { display: flex; gap: 8px; margin-left: auto; }
.only-narrow { display: none; }

/* ---------- layout ---------- */

.layout { display: flex; align-items: flex-start; }

.sidebar {
  position: sticky;
  top: 61px;
  width: 230px;
  flex-shrink: 0;
  max-height: calc(100vh - 61px);
  overflow-y: auto;
  padding: 16px 12px 24px 20px;
}
.sidebar h2 {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
  margin: 8px 0 6px;
}
.facets { list-style: none; margin: 0 0 14px; padding: 0; }
.facet {
  display: flex;
  justify-content: space-between;
  width: 100%;
  border: 0;
  background: none;
  padding: 5px 8px;
  border-radius: 6px;
  text-align: left;
}
.facet .n { color: var(--muted); font-variant-numeric: tabular-nums; }
.facet.on { background: var(--accent); color: var(--accent-text); }
.facet.on .n { color: inherit; }
.facet.on:hover { background: var(--accent); }

main { flex: 1; min-width: 0; padding: 16px 20px 40px 8px; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 12px; }
.tab {
  border: 0;
  border-bottom: 2px solid transparent;
  border-radius: 0;
  background: none;
  padding: 8px 14px;
  margin-bottom: -1px;
  font-weight: 600;
  color: var(--muted);
}
.tab:hover:not(:disabled) { background: none; color: var(--text); }
.tab.on { color: var(--text); border-bottom-color: var(--accent); }
.tab .n { font-weight: 400; color: var(--muted); margin-left: 4px; font-variant-numeric: tabular-nums; }

.toolbar { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
#summary { flex: 1; margin: 0; min-height: 1.4em; }
.sort { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 13px; }
.sort select {
  font: inherit;
  font-size: 14px;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 5px 8px;
}
.layout-toggle { display: inline-flex; }
.layout-toggle button { border-radius: 0; padding: 5px 12px; font-size: 14px; }
.layout-toggle button:first-child { border-radius: 8px 0 0 8px; }
.layout-toggle button:last-child { border-radius: 0 8px 8px 0; margin-left: -1px; }
.layout-toggle button.on { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 16px;
}

.card {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 0 0 8px;
  border: 0;
  background: none;
  text-align: left;
  border-radius: var(--radius);
}
.card:hover { background: none; }
.card:hover .cover { transform: translateY(-2px); box-shadow: 0 6px 16px rgba(0, 0, 0, .25); }
.cover {
  width: 100%;
  aspect-ratio: 115 / 140;
  object-fit: cover;
  border-radius: 6px;
  background: var(--line);
  box-shadow: 0 1px 3px rgba(0, 0, 0, .2);
  transition: transform .12s, box-shadow .12s;
}
.nocover {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px;
  text-align: center;
  color: var(--muted);
  font-weight: 600;
}
.card-title { font-weight: 600; line-height: 1.25; }

.list { display: flex; flex-direction: column; border-top: 1px solid var(--line); }
.line {
  display: grid;
  grid-template-columns: 40px minmax(0, 1fr) auto 130px;
  gap: 14px;
  align-items: center;
  width: 100%;
  padding: 6px 4px;
  border: 0;
  border-bottom: 1px solid var(--line);
  border-radius: 0;
  background: none;
  text-align: left;
}
.line .thumb { width: 40px; aspect-ratio: 115 / 140; object-fit: cover; border-radius: 3px; font-size: 7px; padding: 2px; }
.line-main { display: flex; flex-direction: column; min-width: 0; }
.line-main > span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.line-ean { color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; text-align: right; }
.card-meta { color: var(--muted); font-size: 13px; }

.badges { display: inline-flex; flex-wrap: wrap; gap: 4px; }
.badge {
  font-size: 11px;
  font-weight: 700;
  padding: 1px 6px;
  border-radius: 4px;
  color: #fff;
  background: var(--badge-bd);
  white-space: nowrap;
}
.badge.t-4kultrahd { background: var(--badge-4k); }
.badge.t-3dbluray { background: var(--badge-3d); }
.badge.t-dvd { background: var(--badge-dvd); }

.empty {
  padding: 60px 20px;
  text-align: center;
  color: var(--muted);
  border: 2px dashed var(--line);
  border-radius: var(--radius);
}

/* ---------- dialogs ---------- */

.dlg {
  width: min(680px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 20px;
  background: var(--panel);
  color: var(--text);
}
.dlg.narrow { width: min(460px, calc(100vw - 32px)); }
.dlg::backdrop { background: rgba(0, 0, 0, .5); }
.dlg h2 { margin: 0 0 12px; font-size: 20px; }
.dlg-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 18px; }
/* First in the markup = button Enter uses; row-reverse shows it on the right. */
.save-btns { display: inline-flex; flex-direction: row-reverse; gap: 8px; }
.spacer { flex: 1; }
.dlg.busy { cursor: progress; }

.step label { display: block; font-weight: 600; font-size: 13px; margin-bottom: 10px; }
.step label input { margin-top: 4px; font-weight: 400; }
.row { display: flex; gap: 8px; align-items: center; margin-bottom: 10px; }
.row > label { flex: 1; }
.row > input { flex: 1; }
fieldset { border: 0; padding: 0; margin: 0 0 10px; }
legend { font-weight: 600; font-size: 13px; margin-bottom: 4px; }
.checks { display: flex; flex-wrap: wrap; gap: 6px 16px; }
.step label.check { display: inline-flex; align-items: center; gap: 6px; font-weight: 400; font-size: 15px; margin: 0; }
.check input { width: auto; margin: 0; }

.form-grid { display: flex; gap: 18px; align-items: flex-start; }
.fields { flex: 1; min-width: 0; }
.cover-preview { width: 140px; border-radius: 6px; flex-shrink: 0; }

.msg { padding: 9px 12px; border-radius: 8px; margin: 0 0 14px; background: var(--info-bg); }
.msg.warn { background: var(--warn-bg); }
.msg.error { background: var(--error-bg); }

.step-head { display: flex; align-items: center; gap: 8px; font-weight: 600; margin: 4px 0 10px; }
.step-head:not(:first-child) { margin-top: 22px; padding-top: 16px; border-top: 1px solid var(--line); }
.num {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 13px;
  flex-shrink: 0;
}
button.wide { width: 100%; margin-bottom: 4px; }
.hint { font-size: 13px; color: var(--muted); margin: -4px 0 10px; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.chip { padding: 4px 11px; border-radius: 999px; font-size: 13px; }
.chip.on { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.chip.on:hover:not(:disabled) { background: var(--accent); }
.chip.more { border-style: dashed; color: var(--accent); }
.add-genre input { max-width: 220px; }

.detail { display: flex; gap: 20px; align-items: flex-start; }
.detail-cover { width: 200px; aspect-ratio: 115 / 140; object-fit: cover; border-radius: 8px; flex-shrink: 0; }
.detail dl { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin: 0 0 14px; }
.detail dt { color: var(--muted); }
.detail dd { margin: 0; }

.plain { padding-left: 18px; }
.plain li { margin-bottom: 6px; }

.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: 999px;
  z-index: 20;
  box-shadow: 0 4px 14px rgba(0, 0, 0, .3);
}

/* ---------- phones ---------- */

@media (max-width: 760px) {
  .topbar { flex-wrap: wrap; gap: 10px; padding: 10px 16px; }
  #search { order: 3; flex-basis: 100%; max-width: none; }
  .actions { flex-wrap: wrap; }
  .actions button { padding: 6px 10px; }
  .only-narrow { display: inline-block; }

  .layout { display: block; }
  .sidebar {
    display: none;
    position: static;
    width: auto;
    max-height: none;
    padding: 10px 16px 0;
  }
  .filters-open .sidebar { display: block; }
  .facets { display: flex; flex-wrap: wrap; gap: 6px; }
  .facet { width: auto; gap: 8px; border: 1px solid var(--line); border-radius: 999px; padding: 4px 12px; }

  main { padding: 12px 16px 40px; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(105px, 1fr)); gap: 12px; }
  .card-title { font-size: 14px; }

  .line { grid-template-columns: 36px minmax(0, 1fr) auto; gap: 10px; }
  .line-ean { display: none; }
  .line .badges { flex-direction: column; align-items: flex-end; }
  .toolbar { gap: 8px; }
  .save-btns { flex: 1 1 100%; }
  .save-btns button { flex: 1; }

  .form-grid, .detail { flex-direction: column; }
  .cover-preview { width: 100px; }
  .detail-cover { width: 140px; }
}

/* ---------- bookmarklet setup ---------- */

.dlg h3 { font-size: 15px; margin: 18px 0 6px; }
.dlg ol { padding-left: 20px; margin: 0; }
.dlg ol li { margin-bottom: 6px; }
.bookmarklet {
  display: inline-block;
  padding: 8px 16px;
  border-radius: 8px;
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
  text-decoration: none;
  cursor: grab;
}
.code {
  width: 100%;
  height: 90px;
  font: 12px/1.3 ui-monospace, Menlo, monospace;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  word-break: break-all;
}
