/* ===== Variables & Reset ===== */
:root {
  --bg: #111114;
  --surface: #1c1c21;
  --surface2: #26262d;
  --surface3: #313138;
  --border: #3a3a44;
  --accent: #4f8ef7;
  --accent-hover: #6ba3f9;
  --danger: #e05555;
  --danger-hover: #f06060;
  --success: #4caf7d;
  --warning: #f0a030;
  --text: #e2e2e8;
  --text-muted: #8888a0;
  --text-dim: #55556a;
  --radius: 8px;
  --radius-sm: 4px;
  --shadow: 0 4px 20px rgba(0,0,0,0.5);
  --transition: 0.15s ease;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  overflow: hidden;
}

#app { height: 100vh; display: flex; flex-direction: column; overflow: hidden; }

/* ===== Scrollbar ===== */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--surface3); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--border); }

/* ===== Buttons ===== */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 7px 14px; border-radius: var(--radius-sm);
  border: none; cursor: pointer;
  font-size: 13px; font-weight: 500;
  transition: background var(--transition), opacity var(--transition);
  white-space: nowrap;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-ghost { background: transparent; color: var(--text-muted); border: 1px solid var(--border); }
.btn-ghost:hover { background: var(--surface2); color: var(--text); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: var(--danger-hover); }
.btn-success { background: var(--success); color: #fff; }
.btn-sm { padding: 4px 10px; font-size: 12px; }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn-icon { padding: 7px; }

/* ===== Forms ===== */
input, select, textarea {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  padding: 8px 12px;
  font-size: 14px;
  outline: none;
  transition: border-color var(--transition);
  width: 100%;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
input::placeholder { color: var(--text-dim); }
label { display: block; margin-bottom: 6px; color: var(--text-muted); font-size: 12px; font-weight: 500; text-transform: uppercase; letter-spacing: 0.05em; }
.form-group { margin-bottom: 16px; }
.form-row { display: flex; gap: 12px; }
.form-row > * { flex: 1; }

/* ===== Nav ===== */
.navbar {
  display: flex; align-items: center; gap: 0;
  background: var(--surface); border-bottom: 1px solid var(--border);
  padding: 0 16px; height: 52px; flex-shrink: 0;
  z-index: 100;
}
.navbar-brand {
  font-size: 18px; font-weight: 700; color: var(--text);
  margin-right: 24px; letter-spacing: -0.5px;
}
.navbar-brand span { color: var(--accent); }
.nav-links { display: flex; gap: 2px; flex: 1; }
.nav-link {
  padding: 6px 12px; border-radius: var(--radius-sm);
  color: var(--text-muted); cursor: pointer;
  transition: background var(--transition), color var(--transition);
  font-size: 13px;
}
.nav-link:hover { background: var(--surface2); color: var(--text); }
.nav-link.active { background: var(--surface3); color: var(--text); }
.navbar-right { display: flex; align-items: center; gap: 8px; margin-left: auto; }
.user-badge {
  font-size: 12px; color: var(--text-muted);
  padding: 4px 10px; background: var(--surface2);
  border-radius: var(--radius-sm); border: 1px solid var(--border);
}

/* ===== Search Bar ===== */
.search-bar {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 16px; background: var(--surface);
  border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.search-input-wrap { position: relative; flex: 1; max-width: 600px; }
.search-input-wrap input { padding-left: 36px; }
.search-icon {
  position: absolute; left: 10px; top: 50%; transform: translateY(-50%);
  color: var(--text-dim); pointer-events: none; font-size: 16px;
}
.search-clear {
  position: absolute; right: 8px; top: 50%; transform: translateY(-50%);
  background: none; border: none; color: var(--text-dim); cursor: pointer;
  padding: 2px; border-radius: 50%; display: none; font-size: 16px;
  line-height: 1; width: auto;
}
.search-clear.visible { display: block; }
.search-clear:hover { color: var(--text); }
.sort-select { background: var(--surface2); border: 1px solid var(--border); color: var(--text); padding: 7px 10px; border-radius: var(--radius-sm); font-size: 13px; cursor: pointer; width: auto; flex-shrink: 0; }
.search-hint { font-size: 11px; color: var(--text-dim); white-space: nowrap; flex-shrink: 0; }

/* ===== Gallery Layout ===== */
.gallery-layout { display: flex; flex: 1; overflow: hidden; }

.tag-sidebar {
  width: 220px; flex-shrink: 0; overflow-y: auto;
  background: var(--surface); border-right: 1px solid var(--border);
  padding: 12px 0;
  transition: width var(--transition);
}
.tag-sidebar.hidden { width: 0; overflow: hidden; padding: 0; }
.sidebar-title {
  padding: 4px 16px 8px; font-size: 11px; font-weight: 600;
  text-transform: uppercase; letter-spacing: 0.08em; color: var(--text-dim);
}
.tag-item {
  display: flex; justify-content: space-between; align-items: center;
  padding: 5px 16px; cursor: pointer;
  transition: background var(--transition);
  border-radius: 0;
}
.tag-item:hover { background: var(--surface2); }
.tag-item.active { background: var(--surface3); color: var(--accent); }
.tag-name { font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tag-count {
  font-size: 11px; color: var(--text-dim);
  background: var(--surface3); padding: 1px 6px;
  border-radius: 10px; flex-shrink: 0; margin-left: 6px;
}

/* ===== Gallery Grid ===== */
.gallery-main { flex: 1; display: flex; flex-direction: column; overflow: hidden; }

.gallery-grid-wrap { flex: 1; overflow-y: auto; padding: 12px; }

.gallery-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 8px;
}
.gallery-grid.size-sm { grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); }
.gallery-grid.size-lg { grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); }

.media-card {
  position: relative; aspect-ratio: 1; overflow: hidden;
  border-radius: var(--radius-sm); background: var(--surface2);
  cursor: pointer; transition: transform var(--transition), box-shadow var(--transition);
  border: 2px solid transparent;
}
.media-card:hover { transform: scale(1.02); box-shadow: 0 4px 16px rgba(0,0,0,0.5); }
.media-card.focused { border-color: var(--accent); }
.media-card.selected { border-color: var(--accent); }

.media-card img {
  width: 100%; height: 100%; object-fit: cover;
  display: block; transition: opacity var(--transition);
}
.media-card img.loading { opacity: 0; }

.card-type-badge {
  position: absolute; bottom: 4px; right: 4px;
  background: rgba(0,0,0,0.7); color: #fff;
  font-size: 10px; padding: 2px 5px; border-radius: 3px;
  backdrop-filter: blur(4px);
}
.card-duration {
  position: absolute; bottom: 4px; left: 4px;
  background: rgba(0,0,0,0.7); color: #fff;
  font-size: 10px; padding: 2px 5px; border-radius: 3px;
}

.media-card .card-overlay {
  position: absolute; inset: 0;
  background: rgba(0,0,0,0.6);
  opacity: 0; transition: opacity var(--transition);
  display: flex; align-items: flex-end;
  padding: 8px;
}
.media-card:hover .card-overlay { opacity: 1; }
.card-filename {
  font-size: 11px; color: #fff; overflow: hidden;
  text-overflow: ellipsis; white-space: nowrap; width: 100%;
}

.no-thumbnail {
  width: 100%; height: 100%;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  color: var(--text-dim); gap: 8px; font-size: 12px;
}
.no-thumbnail-icon { font-size: 32px; }

/* ===== Gallery Status Bar ===== */
.gallery-status {
  display: flex; align-items: center; justify-content: space-between;
  padding: 6px 16px; background: var(--surface);
  border-top: 1px solid var(--border); font-size: 12px; color: var(--text-muted);
  flex-shrink: 0;
}
.pagination { display: flex; gap: 4px; align-items: center; }
.page-btn {
  padding: 3px 8px; border-radius: var(--radius-sm);
  background: var(--surface2); border: 1px solid var(--border);
  color: var(--text-muted); cursor: pointer; font-size: 12px;
  transition: background var(--transition);
}
.page-btn:hover:not(:disabled) { background: var(--surface3); color: var(--text); }
.page-btn.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.page-btn:disabled { opacity: 0.3; cursor: not-allowed; }
.grid-size-controls { display: flex; gap: 4px; }
.size-btn {
  padding: 3px 8px; border-radius: var(--radius-sm);
  background: transparent; border: 1px solid var(--border);
  color: var(--text-dim); cursor: pointer; font-size: 12px;
}
.size-btn.active { background: var(--surface3); color: var(--text); }

/* ===== Sort Actions Bar ===== */
.sort-actions-bar {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 16px; background: var(--surface);
  border-top: 1px solid var(--border); flex-shrink: 0;
  flex-wrap: wrap;
}
.sort-actions-bar .bar-label {
  font-size: 11px; color: var(--text-dim);
  text-transform: uppercase; letter-spacing: 0.06em;
  margin-right: 4px; white-space: nowrap;
}
.sort-action-btn {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 5px 12px; border-radius: var(--radius-sm);
  background: var(--surface2); border: 1px solid var(--border);
  color: var(--text-muted); cursor: pointer;
  font-size: 12px; transition: all var(--transition);
}
.sort-action-btn:hover { background: var(--surface3); color: var(--text); border-color: var(--accent); }
.sort-action-btn .key-badge {
  display: inline-flex; align-items: center; justify-content: center;
  width: 18px; height: 18px; border-radius: 3px;
  background: var(--surface3); color: var(--accent);
  font-size: 10px; font-weight: 700; border: 1px solid var(--border);
}
.sort-action-btn.trash-btn { border-color: var(--danger); }
.sort-action-btn.trash-btn .key-badge { color: var(--danger); }
.sort-action-btn.trash-btn:hover { background: rgba(224,85,85,0.15); color: var(--danger); }

/* Sort bar inline edit mode */
.sort-actions-bar.editing,
.viewer-sort-bar.editing {
  flex-wrap: wrap;
  height: auto;
  padding: 10px 12px;
  gap: 8px;
  align-items: flex-start;
}
.sort-edit-rows { flex: 1 1 100%; display: flex; flex-direction: column; gap: 6px; }
.sort-edit-row { display: flex; align-items: center; gap: 6px; }
.sort-edit-row .key-input { width: 52px; font-size: 12px; text-align: center; font-family: monospace; flex-shrink: 0; }
.sort-edit-row .label-input { width: 110px; font-size: 12px; flex-shrink: 0; }
.sort-edit-footer { flex: 1 1 100%; display: flex; align-items: center; gap: 6px; }
.sort-edit-toggle { font-size: 13px; padding: 2px 6px; opacity: 0.5; }
.sort-edit-toggle:hover { opacity: 1; }

/* Dry run mode */
.sort-actions-bar.dry-run-mode {
  border-color: rgba(255,180,0,0.4);
  background: rgba(255,180,0,0.05);
}
.sort-action-btn.dry-run-active {
  background: rgba(255,180,0,0.15);
  border-color: rgba(255,180,0,0.7);
  color: #ffb400;
}
.sort-action-btn.dry-run-active:hover {
  background: rgba(255,180,0,0.25);
  border-color: #ffb400;
  color: #ffb400;
}

/* ===== Viewer ===== */
.viewer-overlay {
  position: fixed; inset: 0; z-index: 1000;
  background: rgba(0,0,0,0.95);
  display: flex; flex-direction: column;
  animation: fadeIn 0.15s ease;
}
@keyframes fadeIn { from { opacity: 0 } to { opacity: 1 } }

.viewer-toolbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px; background: rgba(0,0,0,0.5);
  backdrop-filter: blur(10px); flex-shrink: 0; gap: 12px;
}
.viewer-title {
  font-size: 14px; font-weight: 500; overflow: hidden;
  text-overflow: ellipsis; white-space: nowrap; flex: 1;
}
.viewer-tags {
  display: flex; gap: 4px; flex-wrap: wrap; align-items: center;
}
.tag-pill {
  padding: 2px 8px; border-radius: 12px;
  background: var(--surface3); color: var(--text-muted);
  font-size: 11px; cursor: pointer; border: 1px solid var(--border);
  transition: all var(--transition);
}
.tag-pill:hover { background: var(--accent); color: #fff; border-color: var(--accent); }

.viewer-body { flex: 1; display: flex; align-items: center; justify-content: center; overflow: hidden; position: relative; }

.viewer-media {
  max-width: 100%; max-height: 100%;
  object-fit: contain;
  animation: mediaIn 0.1s ease;
}
@keyframes mediaIn { from { opacity: 0; transform: scale(0.98) } to { opacity: 1; transform: scale(1) } }

.viewer-nav {
  position: absolute; top: 50%; transform: translateY(-50%);
  background: rgba(0,0,0,0.5); border: none; color: #fff;
  width: 44px; height: 64px; border-radius: var(--radius-sm);
  cursor: pointer; font-size: 22px;
  display: flex; align-items: center; justify-content: center;
  transition: background var(--transition);
  backdrop-filter: blur(4px);
}
.viewer-nav:hover { background: rgba(255,255,255,0.15); }
.viewer-nav.prev { left: 12px; }
.viewer-nav.next { right: 12px; }
.viewer-nav:disabled { opacity: 0.2; cursor: not-allowed; }

.viewer-info {
  padding: 10px 16px; background: rgba(0,0,0,0.5);
  backdrop-filter: blur(10px); flex-shrink: 0;
  display: flex; gap: 16px; align-items: center;
  font-size: 12px; color: var(--text-muted);
}
.viewer-info span { white-space: nowrap; }
.viewer-counter { margin-left: auto; }

.viewer-sort-bar {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 16px; background: rgba(0,0,0,0.7);
  backdrop-filter: blur(10px); flex-shrink: 0; flex-wrap: wrap;
}

/* ===== Library View ===== */
.page-layout {
  flex: 1; overflow-y: auto; padding: 24px;
  display: flex; flex-direction: column; gap: 20px;
}
.page-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 4px;
}
.page-title { font-size: 20px; font-weight: 600; }

.folder-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px;
  display: flex; align-items: center; gap: 16px;
}
.folder-icon { font-size: 28px; flex-shrink: 0; }
.folder-info { flex: 1; overflow: hidden; }
.folder-path {
  font-size: 13px; font-family: monospace;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  color: var(--text);
}
.folder-meta { font-size: 12px; color: var(--text-muted); margin-top: 4px; }
.folder-status {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 2px 8px; border-radius: 12px; font-size: 11px;
  font-weight: 500;
}
.folder-status.pending { background: var(--surface3); color: var(--text-muted); }
.folder-status.scanning { background: rgba(79,142,247,0.2); color: var(--accent); }
.folder-status.done { background: rgba(76,175,125,0.2); color: var(--success); }
.folder-status.error { background: rgba(224,85,85,0.2); color: var(--danger); }
.progress-bar {
  height: 3px; background: var(--surface3); border-radius: 2px;
  margin-top: 6px; overflow: hidden;
}
.progress-fill {
  height: 100%; background: var(--accent); border-radius: 2px;
  transition: width 0.3s ease;
}

/* ===== File Browser ===== */
.file-browser {
  background: var(--surface2); border: 1px solid var(--border);
  border-radius: var(--radius); overflow: hidden;
}
.file-browser-header {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 14px; background: var(--surface);
  border-bottom: 1px solid var(--border);
  font-size: 12px; font-family: monospace; color: var(--text-muted);
}
.browser-entry {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 14px; cursor: pointer;
  transition: background var(--transition); border-bottom: 1px solid var(--border);
}
.browser-entry:last-child { border-bottom: none; }
.browser-entry:hover { background: var(--surface3); }
.browser-entry .entry-icon { font-size: 16px; }
.browser-entry .entry-name { font-size: 13px; }

/* ===== Settings ===== */
.settings-section {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius);
}
.settings-section-header {
  padding: 14px 18px; border-bottom: 1px solid var(--border);
  font-size: 14px; font-weight: 600;
  border-radius: var(--radius) var(--radius) 0 0;
  background: var(--surface);
}
.settings-section-body { padding: 18px; }

.sort-action-row {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 0; border-bottom: 1px solid var(--border);
}
.sort-action-row:last-child { border-bottom: none; }
.sort-action-row .key-input { width: 60px; text-align: center; font-family: monospace; }
.sort-action-row .label-input { flex: 1; }
.path-input-wrap { position: relative; flex: 2; display: flex; }
.path-input-wrap .path-input { flex: 1; font-family: monospace; font-size: 12px; min-width: 0; }
.path-dropdown {
  display: none; position: absolute; top: calc(100% + 2px); left: 0; right: 0; z-index: 300;
  background: var(--surface2); border: 1px solid var(--border); border-radius: 6px;
  max-height: 200px; overflow-y: auto; box-shadow: 0 4px 16px rgba(0,0,0,0.4);
}
.path-dropdown-item {
  padding: 7px 10px; cursor: pointer; font-size: 12px; font-family: monospace;
  display: flex; align-items: center; gap: 6px; color: var(--text);
  border-bottom: 1px solid var(--border);
}
.path-dropdown-item:last-child { border-bottom: none; }
.path-dropdown-item:hover { background: var(--surface3); }
.path-dropdown-item.create-item { color: var(--accent); font-style: italic; }

/* ===== Folder picker modal ===== */
.modal-overlay {
  position: fixed; inset: 0; z-index: 2000;
  background: rgba(0,0,0,0.7);
  display: flex; align-items: center; justify-content: center;
  animation: fadeIn 0.12s ease;
}
.modal-box {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; box-shadow: 0 8px 32px rgba(0,0,0,0.5);
  overflow: hidden;
}

/* ===== Trash View ===== */
.trash-toolbar {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 16px; background: var(--surface);
  border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.trash-toolbar .trash-count { color: var(--text-muted); font-size: 13px; margin-right: auto; }

/* ===== Login ===== */
.login-page {
  flex: 1; display: flex; align-items: center; justify-content: center;
  background: var(--bg);
}
.login-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 40px;
  width: 360px; max-width: 100%;
  box-shadow: var(--shadow);
}
.login-logo { text-align: center; margin-bottom: 28px; }
.login-logo-text { font-size: 28px; font-weight: 700; }
.login-logo-text span { color: var(--accent); }
.login-subtitle { font-size: 13px; color: var(--text-muted); margin-top: 4px; }
.login-error {
  background: rgba(224,85,85,0.15); border: 1px solid var(--danger);
  color: var(--danger); padding: 10px 14px;
  border-radius: var(--radius-sm); margin-bottom: 16px;
  font-size: 13px;
}

/* ===== Toast Notifications ===== */
#toast-container {
  position: fixed; bottom: 20px; right: 20px;
  display: flex; flex-direction: column; gap: 8px;
  z-index: 9999; pointer-events: none;
}
.toast {
  padding: 10px 16px; border-radius: var(--radius-sm);
  font-size: 13px; color: #fff; min-width: 240px;
  box-shadow: var(--shadow); animation: toastIn 0.2s ease;
  pointer-events: all;
}
@keyframes toastIn { from { transform: translateX(60px); opacity: 0 } to { transform: translateX(0); opacity: 1 } }
.toast.info { background: var(--accent); }
.toast.success { background: var(--success); }
.toast.error { background: var(--danger); }
.toast.warning { background: var(--warning); }

/* ===== Misc ===== */
.spinner {
  display: inline-block; width: 18px; height: 18px;
  border: 2px solid var(--surface3); border-top-color: var(--accent);
  border-radius: 50%; animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg) } }

.empty-state {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  color: var(--text-dim); gap: 12px; padding: 60px;
  text-align: center;
}
.empty-state-icon { font-size: 52px; opacity: 0.4; }
.empty-state-text { font-size: 16px; font-weight: 500; }
.empty-state-sub { font-size: 13px; color: var(--text-dim); }

.chip {
  display: inline-flex; align-items: center;
  padding: 3px 8px; background: var(--surface3);
  border-radius: 12px; font-size: 11px; color: var(--text-muted);
  border: 1px solid var(--border);
}

hr.divider { border: none; border-top: 1px solid var(--border); margin: 16px 0; }

.highlight-flash {
  animation: flash 0.4s ease;
}
@keyframes flash {
  0%, 100% { opacity: 1 }
  50% { opacity: 0.4 }
}
