:root {
  --bg: #121212;
  --panel: #1b1b1b;
  --line: #2c2c2c;
  --text: #ececec;
  --muted: #8d8d8d;
  --accent: #a2252d;
  --accent-hover: #fa5365;
  --ok: #8aff8a;
  --warn: #ffff8a;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.topbar, .tabs, .toolbar, .settings {
  padding: 16px 20px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: flex-start;
  border-bottom: 1px solid var(--line);
}

.topbar-right {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: flex-start;
  justify-content: flex-end;
}

.login-body {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 20px;
}

.login-card {
  width: min(400px, 100%);
  display: grid;
  gap: 12px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 24px;
}

.login-card .flash { margin: 0; }

h1, h2 { margin: 0 0 6px; font-size: 20px; }
.muted { color: var(--muted); font-size: 13px; }

.stats {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.stat {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 12px;
}

button.stat {
  background: var(--panel);
  color: inherit;
  font-family: inherit;
  font-size: 12px;
  font-weight: inherit;
  line-height: inherit;
}

button.stat:hover {
  background: #2a2a2a;
}

button.stat.ready:hover {
  background: #16341d;
}

button.stat.error:hover {
  background: #3d1313;
}

.stat.ready {
  border-color: #1a8a3c;
  color: var(--ok);
  background: #16341d;
}
.stat.loading { border-color: #c9a227; color: var(--warn); }
.stat.error {
  border-color: #a2252d;
  color: #ff8a8a;
  background: #3d1313;
}

.tabs {
  display: flex;
  gap: 8px;
  border-bottom: 1px solid var(--line);
}

.tab, button, .button-link {
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 999px;
  padding: 8px 14px;
  cursor: pointer;
  font-size: 13px;
}

.tab { background: #414141; }
.tab.active, button:hover, .tab:hover { background: var(--accent-hover); }
button.ghost, .button-link {
  background: #414141;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
}

.toolbar {
  display: grid;
  gap: 12px;
}

.toolbar.compact {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.toolbar.filters {
  display: grid;
  gap: 14px;
  background: #161616;
  border-bottom: 1px solid var(--line);
}

.filter-row {
  display: flex;
  flex-wrap: wrap;
  gap: 12px 16px;
  align-items: flex-end;
}

.field {
  display: grid;
  gap: 6px;
  min-width: 148px;
}

.field-search { flex: 1 1 220px; min-width: 200px; max-width: 320px; }

.field-narrow {
  min-width: 110px;
  max-width: 140px;
}

.field-check { min-width: 150px; }

.field-label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .04em;
  text-transform: uppercase;
  color: var(--muted);
}

.info-tip {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #3a3a3a;
  color: #d0d0d0;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0;
  text-transform: none;
  cursor: help;
}

.info-tip-text {
  position: absolute;
  left: 50%;
  bottom: calc(100% + 8px);
  transform: translateX(-50%);
  width: max-content;
  max-width: 220px;
  padding: 6px 8px;
  border-radius: 6px;
  background: #2a2a2a;
  color: var(--text);
  font-size: 11px;
  font-weight: 400;
  line-height: 1.35;
  letter-spacing: 0;
  text-transform: none;
  box-shadow: 0 4px 16px rgba(0,0,0,.4);
  opacity: 0;
  pointer-events: none;
  white-space: nowrap;
  z-index: 5;
}

.info-tip:hover .info-tip-text,
.info-tip:focus .info-tip-text {
  opacity: 1;
}

.field input,
.field select {
  width: 100%;
  min-width: 0;
}

.filter-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  padding-top: 12px;
  border-top: 1px solid var(--line);
}

.gallery-filters .filter-actions {
  border-top: none;
  padding-top: 0;
}

.gallery-head {
  display: flex;
  flex-wrap: wrap;
  gap: 12px 16px;
  align-items: center;
}

.gallery-head-text {
  flex: 1 1 220px;
  min-width: 180px;
}

.gallery-filters #galleryTitle {
  margin: 0;
}

.gallery-sort {
  min-width: 150px;
}

.gallery-chip-rows {
  display: grid;
  gap: 10px;
}

.chip-group {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
}

.chip-label {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .04em;
  text-transform: uppercase;
  color: var(--muted);
  min-width: 44px;
}

.chip {
  background: #2a2a2a;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 6px 11px;
  font-size: 12px;
  line-height: 1.2;
}

.chip:hover {
  background: #3a3a3a;
}

.chip.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.chip:disabled {
  opacity: .4;
  cursor: default;
}

.chip-count {
  color: inherit;
  opacity: .75;
  font-variant-numeric: tabular-nums;
}

.check-line {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 40px;
  color: var(--text);
  font-size: 13px;
}

.check-line input[type="checkbox"] {
  width: 16px;
  min-width: 16px;
  height: 16px;
  accent-color: var(--accent);
}

textarea, input, select {
  width: 100%;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  font: inherit;
}

.toolbar.compact input,
.toolbar.compact select { width: auto; min-width: 160px; }

.toolbar-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.toolbar-actions select { width: auto; min-width: 120px; }

.flash {
  margin: 12px 20px;
  padding: 10px 14px;
  border-radius: 8px;
  background: #1a3813;
  color: var(--ok);
}

.detect-status {
  margin: 0 20px 12px;
  padding: 10px 14px;
  border-radius: 8px;
  background: #383813;
  color: var(--warn);
  font-size: 13px;
}

.flash.error { background: #3d1313; color: #ff8a8a; }

.table-wrap { padding: 0 20px 24px; overflow-x: auto; }

table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--line); }
tr:hover td { background: #181818; }

.nick-open {
  background: none;
  border: none;
  padding: 0;
  color: inherit;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  border-radius: 0;
}

.nick-open:hover {
  color: var(--accent-hover);
  background: none;
}

.dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #555;
  margin-right: 6px;
}

.dot.on { background: #2ecc71; }
.dot.off { background: #888; }

.top-badge {
  display: inline-block;
  margin-left: 6px;
  padding: 1px 6px;
  border-radius: 999px;
  background: #6b2a12;
  color: #ffc48a;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: .03em;
  vertical-align: middle;
}

.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 10px;
  padding: 16px 20px 32px;
}

.card {
  position: relative;
  aspect-ratio: 1;
  background: #000;
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
  content-visibility: auto;
  contain-intrinsic-size: 140px;
}

.card img, .card video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.badge {
  position: absolute;
  top: 6px;
  right: 6px;
  background: rgba(0,0,0,.75);
  color: white;
  font-size: 11px;
  padding: 2px 6px;
  border-radius: 999px;
}

.badge.free { background: rgba(26, 138, 60, .9); }
.badge.preview { background: rgba(70, 70, 70, .92); }
.badge.sound { background: rgba(30, 90, 160, .92); }

.likes {
  position: absolute;
  left: 6px;
  bottom: 6px;
  background: rgba(162, 37, 45, .9);
  font-size: 11px;
  padding: 2px 6px;
  border-radius: 999px;
}

.person-badge {
  position: absolute;
  left: 6px;
  top: 6px;
  font-size: 11px;
  padding: 2px 6px;
  border-radius: 999px;
  background: rgba(80, 80, 80, .85);
  cursor: pointer;
  z-index: 2;
}

.person-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 12px 0;
}

.person-badge.yes { background: rgba(26, 138, 60, .9); }
.person-badge.no { background: rgba(80, 80, 80, .85); }
.person-badge.unknown { background: rgba(90, 80, 20, .85); color: #ffff8a; }

.style-badge {
  position: absolute;
  left: 6px;
  top: 26px;
  font-size: 11px;
  padding: 2px 6px;
  border-radius: 999px;
  background: rgba(80, 80, 80, .85);
  cursor: pointer;
  z-index: 2;
}

.style-badge.photo { background: rgba(30, 90, 160, .9); }
.style-badge.illustration { background: rgba(140, 60, 160, .9); }
.style-badge.unknown { background: rgba(90, 80, 20, .85); color: #ffff8a; }

.nsfw-badge {
  position: absolute;
  left: 6px;
  top: 46px;
  font-size: 11px;
  padding: 2px 6px;
  border-radius: 999px;
  background: rgba(80, 80, 80, .85);
  cursor: pointer;
  z-index: 2;
}

.nsfw-badge.sfw { background: rgba(26, 138, 60, .9); }
.nsfw-badge.sexy { background: rgba(180, 110, 20, .92); }
.nsfw-badge.explicit { background: rgba(160, 30, 50, .92); }
.nsfw-badge.unknown { background: rgba(90, 80, 20, .85); color: #ffff8a; }

.settings {
  max-width: 520px;
  display: grid;
  gap: 12px;
}

.settings label { display: grid; gap: 6px; font-size: 13px; color: var(--muted); }

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.82);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  z-index: 20;
}

.modal[hidden] { display: none; }

.modal-card {
  position: relative;
  max-width: min(960px, 100%);
  max-height: 90vh;
  overflow: auto;
  background: var(--panel);
  border-radius: 12px;
  padding: 20px;
}

.modal-card img, .modal-card video { max-width: 100%; max-height: 60vh; }

.modal-nav {
  flex-shrink: 0;
  width: 48px;
  height: 72px;
  background: #2a2a2a;
  font-size: 32px;
  line-height: 1;
}

.modal-nav:hover { background: var(--accent-hover); }
.modal-nav:disabled { opacity: .3; cursor: default; background: #2a2a2a; }

.tag-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
  margin: 8px 0;
}

.tag-row .chip-label { min-width: 52px; }

.close {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 32px;
  height: 32px;
  padding: 0;
}

#galleryTitle { margin: 0; font-size: 18px; }

html {
  overflow-x: hidden;
}

.import-panel {
  border-bottom: 1px solid var(--line);
}

.import-panel > summary {
  display: none;
}

.import-panel .toolbar {
  border-bottom: none;
}

.mobile-only { display: none; }

@media (max-width: 760px) {
  body {
    padding-bottom: env(safe-area-inset-bottom);
  }

  .topbar, .tabs, .toolbar, .settings, .flash, .detect-status {
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right));
  }

  .topbar {
    flex-direction: column;
    gap: 10px;
    padding-top: max(12px, env(safe-area-inset-top));
    padding-bottom: 12px;
  }

  .topbar .muted { display: none; }

  h1 { font-size: 18px; margin-bottom: 0; }

  .stats {
    flex-wrap: nowrap;
    overflow-x: auto;
    width: 100%;
    padding-bottom: 2px;
    -webkit-overflow-scrolling: touch;
  }

  .stat {
    flex: 0 0 auto;
    white-space: nowrap;
  }

  .tabs {
    position: sticky;
    top: 0;
    z-index: 8;
    background: var(--bg);
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .import-panel > summary {
    display: block;
    list-style: none;
    cursor: pointer;
    padding: 12px max(12px, env(safe-area-inset-right)) 12px max(12px, env(safe-area-inset-left));
    font-size: 13px;
    font-weight: 700;
  }

  .import-panel > summary::-webkit-details-marker { display: none; }

  .import-panel > summary::after {
    content: ' ▾';
    color: var(--muted);
  }

  .import-panel:not([open]) > summary::after {
    content: ' ▸';
  }

  .import-panel .toolbar {
    padding-top: 0;
  }

  #nickInput { min-height: 72px; }

  .tab, button, .button-link {
    min-height: 44px;
    padding: 10px 14px;
  }

  .toolbar.compact input,
  .toolbar.compact select,
  .toolbar-actions select,
  .toolbar-actions input {
    width: 100%;
    min-width: 0;
  }

  .toolbar-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    align-items: stretch;
  }

  .toolbar-actions button,
  .toolbar-actions select,
  .toolbar-actions .check-line,
  .toolbar-actions .muted {
    width: 100%;
    min-width: 0;
    justify-content: center;
  }

  .toolbar-actions .check-line,
  .toolbar-actions .muted {
    grid-column: 1 / -1;
  }

  .filter-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px 8px;
    align-items: end;
  }

  .field,
  .field-search,
  .field-narrow,
  .field-check {
    min-width: 0;
    max-width: none;
  }

  .field-search,
  .field-check {
    grid-column: 1 / -1;
  }

  .filter-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }

  .filter-actions .muted,
  .gallery-actions #jsonLink {
    grid-column: 1 / -1;
    justify-content: center;
  }

  .gallery-head {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 10px;
  }

  .gallery-head-text {
    min-width: 0;
    flex: none;
  }

  .gallery-sort,
  .gallery-actions {
    grid-column: 1 / -1;
  }

  .chip-label { min-width: 0; width: 100%; }

  .chip-group {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
  }

  .gallery {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
    padding: 12px;
  }

  .card { contain-intrinsic-size: 160px; }

  .table-wrap {
    padding: 0 12px 24px;
    overflow: visible;
  }

  .data-table thead { display: none; }

  .data-table,
  .data-table tbody {
    display: block;
    width: 100%;
  }

  .data-table tr {
    display: grid;
    grid-template-columns: auto 1fr auto;
    gap: 6px 10px;
    align-items: center;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 10px 12px;
    margin-bottom: 8px;
  }

  .data-table tr:hover td { background: transparent; }

  .data-table td {
    display: block;
    padding: 0;
    border: none;
    text-align: left;
    min-width: 0;
  }

  .data-table td::before { display: none; }

  .desktop-only { display: none !important; }
  .mobile-only { display: block; }

  .data-table td[data-label="Wybór"] { grid-column: 1; grid-row: 1; }
  .data-table td[data-label="#"] { grid-column: 3; grid-row: 1; color: var(--muted); }
  .data-table td[data-label="Nickname"] { grid-column: 2; grid-row: 1; }
  .data-table td.row-meta {
    grid-column: 2 / -1;
    grid-row: 2;
    color: var(--muted);
    font-size: 12px;
  }
  .data-table td[data-label="Ostatni skan"] {
    grid-column: 1 / -1;
    grid-row: 3;
    color: var(--muted);
    font-size: 12px;
  }
  .data-table td[data-label="Ostatni skan"]::before {
    display: inline;
    content: 'Skan: ';
  }

  .data-table td.row-actions {
    grid-column: 1 / -1;
    grid-row: 4;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    padding-top: 4px;
  }

  .data-table td.row-actions button { width: 100%; }

  #scansTable .data-table tr {
    grid-template-columns: 1fr 1fr;
  }

  #scansTable .data-table td {
    display: flex;
    justify-content: space-between;
    gap: 8px;
    grid-column: 1 / -1;
  }

  #scansTable .data-table td::before {
    display: inline;
    content: attr(data-label);
    color: var(--muted);
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
  }

  .settings {
    max-width: none;
    padding-bottom: 32px;
  }

  textarea, input, select {
    font-size: 16px;
  }

  .modal {
    padding: 10px;
    align-items: stretch;
  }

  .modal-card {
    width: 100%;
    max-height: 88vh;
    padding: 16px 12px 12px;
    order: 2;
  }

  .modal-card img,
  .modal-card video { max-height: 46vh; }

  .modal-nav {
    position: absolute;
    top: 28%;
    width: 40px;
    height: 56px;
    z-index: 24;
    opacity: .88;
  }

  #modalPrev { left: 6px; }
  #modalNext { right: 6px; }

  .info-tip-text {
    white-space: normal;
    left: 0;
    transform: none;
  }
}
