:root {
  --bg: var(--tg-theme-bg-color, #0f1218);
  --surface: var(--tg-theme-secondary-bg-color, #181c24);
  --text: var(--tg-theme-text-color, #eef1f6);
  --muted: var(--tg-theme-hint-color, #8d96a5);
  --accent: var(--tg-theme-button-color, #3e8bff);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --line: rgba(127, 140, 160, 0.18);
  --radius: 12px;
}

* { box-sizing: border-box; }

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

button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }

.app-shell { max-width: 1100px; margin: 0 auto; padding: 12px 12px 40px; }

.topbar { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; margin-bottom: 10px; }
.eyebrow { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .08em; }
.topbar h1 { margin: 0; font-size: 22px; }

.tabs { display: flex; gap: 4px; background: var(--surface); padding: 3px; border-radius: 10px; }
.tab { border: 0; background: transparent; padding: 6px 12px; border-radius: 8px; color: var(--muted); }
.tab.is-active { background: var(--bg); color: var(--text); }

.controls { position: sticky; top: 0; z-index: 5; background: var(--bg); padding: 6px 0 8px; }
#searchInput {
  width: 100%; padding: 10px 12px; border-radius: var(--radius);
  border: 1px solid var(--line); background: var(--surface); outline: none;
}
#searchInput:focus { border-color: var(--accent); }

.filter-row { display: flex; gap: 8px; align-items: center; margin-top: 8px; flex-wrap: wrap; }
.chips { display: flex; gap: 6px; }
.chip, .person-chip, .tag {
  border: 1px solid var(--line); background: var(--surface); border-radius: 999px;
  padding: 5px 12px; font-size: 13px; white-space: nowrap;
}
.chip.is-active { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
#monthSelect {
  margin-left: auto; padding: 6px 10px; border-radius: 999px; border: 1px solid var(--line);
  background: var(--surface); max-width: 55%;
}

.performer-row { display: flex; gap: 6px; overflow-x: auto; margin-top: 8px; padding-bottom: 2px; scrollbar-width: none; }
.performer-row::-webkit-scrollbar { display: none; }

.summary { color: var(--muted); font-size: 13px; margin: 4px 2px 10px; }

.card-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 10px; }
@media (min-width: 700px) { .card-grid { grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); } }

.media-card { min-width: 0; }
.card-hitbox { display: block; width: 100%; padding: 0; border: 0; background: none; text-align: left; }
.thumb-wrap { position: relative; aspect-ratio: 16 / 10; border-radius: var(--radius); overflow: hidden; background: var(--surface); }
.card-thumb { width: 100%; height: 100%; object-fit: cover; display: block; }
.card-thumb.is-missing { visibility: hidden; }
.badge {
  position: absolute; bottom: 6px; padding: 2px 6px; border-radius: 6px;
  background: rgba(0, 0, 0, .7); color: #fff; font-size: 11px; font-variant-numeric: tabular-nums;
}
.badge.duration { right: 6px; }
.badge.quality { left: 6px; }
.fav-mark { position: absolute; top: 6px; right: 6px; font-size: 16px; text-shadow: 0 1px 3px rgba(0,0,0,.6); }
.card-body { padding: 6px 2px 0; }
.card-title {
  margin: 0; font-size: 14px; font-weight: 600;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.card-people { color: var(--accent); font-size: 12px; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card-date { color: var(--muted); font-size: 12px; }

.sentinel { height: 1px; }
.empty-state { text-align: center; color: var(--muted); padding: 40px 0; }

.detail-sheet { position: fixed; inset: 0; z-index: 20; display: flex; align-items: flex-end; justify-content: center; }
.detail-sheet[hidden] { display: none; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, .55); }
.sheet-card {
  position: relative; width: 100%; max-width: 720px; max-height: 92vh; overflow-y: auto;
  background: var(--bg); border-radius: 16px 16px 0 0; padding-bottom: env(safe-area-inset-bottom);
}
.sheet-close {
  position: absolute; top: 8px; right: 8px; width: 32px; height: 32px; border-radius: 50%;
  border: 0; background: rgba(0, 0, 0, .55); color: #fff;
}
.detail-gallery { display: flex; overflow-x: auto; scroll-snap-type: x mandatory; background: #000; }
.detail-gallery img { flex: 0 0 100%; width: 100%; max-height: 52vh; object-fit: contain; scroll-snap-align: center; }
.detail-body { padding: 12px 14px 18px; }
.detail-body h2 { margin: 0 0 8px; font-size: 18px; }
.people, .tags { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.people .person-chip { color: var(--accent); }
.tag { color: var(--muted); }
.detail-meta { color: var(--muted); font-size: 13px; white-space: pre-line; margin-bottom: 8px; }
.detail-actions { display: flex; gap: 8px; margin-top: 12px; }
.primary-btn, .secondary-btn, .icon-btn { border-radius: 10px; padding: 11px 12px; border: 0; }
.primary-btn { flex: 1; background: var(--accent); color: var(--accent-text); font-weight: 600; }
.secondary-btn { flex: 1; background: var(--surface); }
.icon-btn { width: 48px; background: var(--surface); font-size: 20px; }
.icon-btn.is-active { color: #f5c518; }
.primary-btn:disabled { opacity: .6; }
.send-status { color: var(--muted); font-size: 13px; margin-top: 8px; min-height: 18px; }

.gate { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; background: var(--bg); padding: 24px; z-index: 30; }
.gate[hidden] { display: none; }
.gate-card { max-width: 360px; text-align: center; }
.gate-card p { color: var(--muted); }

[hidden] { display: none !important; }
.topbar { flex-wrap: wrap; }
.tabs { flex-wrap: wrap; }
.jobs-grid { grid-template-columns: repeat(auto-fit, minmax(min(100%, 240px), 1fr)); }
.job-card { border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); padding: 12px; }
.job-card h3 { margin: 0 0 8px; font-size: 15px; }
.job-card p { margin: 5px 0; color: var(--muted); font-size: 13px; overflow-wrap: anywhere; }
.job-card .job-status { color: var(--text); font-weight: 600; }
.job-card .source-warning { color: #e3aa62; }
.job-card .job-id { font-variant-numeric: tabular-nums; }
#jobsControls .secondary-btn { flex: 0 0 auto; }
.overview { padding: 12px; margin-bottom: 8px; border-radius: var(--radius); border: 1px solid var(--line); color: var(--muted); font-size: 13px; }
.filter-row select { padding: 6px 10px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); }
.storage-label { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); font-size: 16px; }
.source-card { padding: 14px; background: var(--surface); border-radius: var(--radius); border: 1px solid var(--line); }
.source-card h3 { margin: 0 0 8px; overflow-wrap: anywhere; font-size: 16px; }
.source-card p { color: var(--muted); margin: 4px 0; font-size: 13px; }
.source-card .source-warning { color: #ffc16e; }
.load-more { display: block; margin: 20px auto; }
button:focus-visible, select:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

@media (max-width: 600px) {
  .tabs { width: 100%; display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

.edit-form { margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--line); display: grid; gap: 10px; }
.edit-form label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.edit-form input:not([type=checkbox]) {
  width: 100%; padding: 9px 11px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); color: var(--text); outline: none;
}
.edit-form input:focus { border-color: var(--accent); }
.edit-form .hint { font-size: 12px; opacity: .8; }
.edit-form label.check { display: flex; align-items: center; gap: 8px; color: var(--text); }
.edit-form .detail-actions { margin-top: 2px; }
.link-btn { border: 0; background: none; color: var(--muted); font-size: 13px; text-decoration: underline; padding: 4px 0; justify-self: start; }
.edited-mark { color: var(--muted); font-size: 12px; }

.suggestions { margin: 4px 0 10px; padding: 10px; border: 1px dashed var(--line); border-radius: var(--radius); display: grid; gap: 8px; }
.suggestions h3 { margin: 0; font-size: 13px; color: var(--muted); font-weight: 600; }
.suggestion { display: flex; align-items: center; gap: 8px; }
.suggestion .who { flex: 1; min-width: 0; }
.suggestion .who b { display: block; }
.suggestion .who span { color: var(--muted); font-size: 12px; }
.suggestion button { border: 0; border-radius: 8px; padding: 7px 11px; background: var(--surface); }
.suggestion button.ok { background: var(--accent); color: var(--accent-text); }
.card-suggest { color: #e3aa62; font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.scenes { margin: 4px 0 10px; display: grid; gap: 10px; }
.scene-search h3 { margin: 0 0 6px; font-size: 13px; color: var(--muted); font-weight: 600; }
.scene-search h3 b { color: var(--text); }
.scene-row { display: flex; flex-wrap: wrap; gap: 6px; }
.scene-chip { border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 5px 11px; font-size: 13px; font-variant-numeric: tabular-nums; }
.scene-chip.strong { border-color: var(--accent); }
.scene-chip small { color: var(--muted); margin-left: 4px; }
.scenes .hint { color: var(--muted); font-size: 12px; }
.clip-row { display: grid; grid-template-columns: auto 1fr; gap: 2px 6px; width: 100%; text-align: left; border: 1px solid var(--accent); background: var(--surface); color: var(--text); border-radius: 12px; padding: 8px 11px; margin-bottom: 6px; font-size: 13px; }
.clip-row b { grid-column: 2; }
.clip-row small, .clip-row .clip-times { grid-column: 2; color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.clip-row:disabled, .scene-chip:disabled { opacity: .55; }
