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

:root {
  --bg: #0a0a0c;
  --surface: #101014;
  --bg-hover: #1a1a22;
  --border: #2a2a35;
  --text: #c8c8d0;
  --text-muted: #606070;
  --primary: #6366f1;
  --primary-hover: #818cf8;
  --danger: #e05555;
  --success: #22c55e;
  --warning: #eab308;
  --info: #3b82f6;
  --purple: #a855f7;
  --radius: 8px;
  --radius-lg: 12px;
}

html {
  scrollbar-gutter: stable;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

/* --- Auth --- */
#login-view {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100dvh;
}

#login-view[hidden] { display: none; }

#auth-container {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 48px 32px;
  width: 100%;
  max-width: 400px;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

#auth-container h1 {
  font-size: 1.75rem;
  font-weight: 600;
  text-align: center;
  letter-spacing: -0.025em;
}

#auth-container form {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

#auth-container form[hidden] { display: none; }

.auth-tabs {
  display: flex;
  gap: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.auth-tabs[hidden] { display: none; }

.auth-tab {
  flex: 1;
  background: transparent;
  color: var(--text-muted);
  border: none;
  border-radius: 0;
  padding: 0.5rem;
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s ease;
}

.auth-tab:hover { color: var(--text); background: var(--bg-hover); }
.auth-tab.active { background: var(--primary); color: white; }
.auth-tab[hidden] { display: none; }

.auth-link {
  display: block;
  text-align: center;
  color: var(--text-muted);
  font-size: 0.9rem;
  text-decoration: underline;
  text-underline-offset: 3px;
  margin-top: 0.5rem;
}
.auth-link:hover { color: var(--primary-hover); }

.auth-hint {
  font-size: 0.9rem;
  color: var(--text-muted);
  text-align: center;
}

/* Recovery code overlay */
#recovery-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  backdrop-filter: blur(4px);
}
#recovery-overlay[hidden] { display: none; }

.recovery-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 2rem;
  max-width: 440px;
  width: 90%;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.recovery-box h2 {
  font-size: 1.2rem;
  font-weight: 600;
  text-align: center;
}

.recovery-desc {
  font-size: 0.9rem;
  color: var(--text-muted);
  line-height: 1.6;
  text-align: center;
}

#recovery-code-display {
  display: block;
  background: var(--bg);
  border: 1px solid var(--primary);
  border-radius: var(--radius);
  padding: 1rem;
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 0.85rem;
  word-break: break-all;
  text-align: center;
  color: var(--text);
  user-select: all;
  cursor: pointer;
}

#recovery-dismiss {
  margin-top: 0.5rem;
}

input[type="text"],
input[type="password"],
input[type="url"] {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px;
  color: var(--text);
  font-size: 15px;
  outline: none;
  transition: border-color 0.15s ease;
  width: 100%;
}

input:focus {
  border-color: var(--primary);
}

button {
  background: var(--primary);
  color: white;
  border: none;
  border-radius: var(--radius);
  padding: 10px 20px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s ease;
}

button:hover { background: var(--primary-hover); }
button:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-spinner {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255,255,255,0.3);
  border-top-color: white;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

.btn-danger .btn-spinner {
  border-color: rgba(224,85,85,0.3);
  border-top-color: var(--danger);
}

.error {
  color: var(--danger);
  font-size: 0.85rem;
  text-align: center;
}

/* --- App --- */
#app-view {
  max-width: 720px;
  margin: 0 auto;
  padding: 1.5rem 1rem;
}

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 2rem;
}

header h1 {
  font-size: 1.25rem;
  font-weight: 600;
  letter-spacing: -0.025em;
}

.header-nav {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}

.btn-nav, #logout-btn {
  background: transparent;
  color: var(--text-muted);
  font-size: 13px;
  padding: 6px 12px;
  border: 1px solid var(--border);
}

.btn-nav:hover, #logout-btn:hover { color: var(--text); border-color: var(--text-muted); background: var(--bg-hover); }

/* --- Submit --- */
#submit-section {
  margin-bottom: 2rem;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  min-height: calc(100vh - 10rem);
  margin-top: -4rem;
}

#submit-section[hidden] { display: none; }

.submit-emoji {
  font-size: 4rem;
  text-align: center;
  margin-bottom: calc(1.5rem - 5px);
  line-height: 1;
}

#submit-form {
  display: flex;
  flex-direction: column;
  gap: calc(0.5rem + 2px);
  width: 80%;
  margin: 0 auto;
}

#submit-form input[type="url"] {
  width: 100%;
  max-width: 100%;
}

#submit-form button {
  white-space: nowrap;
}

/* --- Jobs --- */
#jobs-section h2 {
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  margin-bottom: 0.75rem;
}

#jobs-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.job-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.875rem 1rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.job-id {
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.job-meta {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: 0.25rem;
}

.job-badge {
  font-size: 0.75rem;
  font-weight: 600;
  padding: 0.2rem 0.6rem;
  border-radius: 9999px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.badge-extracting { background: rgba(59,130,246,0.15); color: var(--info); }
.badge-downloading { background: rgba(234,179,8,0.15); color: var(--warning); }
.badge-encrypting { background: rgba(168,85,247,0.15); color: var(--purple); }
.badge-done { background: rgba(34,197,94,0.15); color: var(--success); }
.badge-failed { background: rgba(239,68,68,0.15); color: var(--danger); }

.job-error {
  font-size: 0.8rem;
  color: var(--danger);
  margin-top: 0.25rem;
}

.empty {
  text-align: center;
  color: var(--text-muted);
  padding: 3rem 0;
  font-size: 0.9rem;
}

/* --- Results --- */
#results-section { margin-bottom: 2rem; }

#results-header {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

#results-header h2 {
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}

.btn-back {
  background: transparent;
  color: var(--text-muted);
  font-size: 13px;
  padding: 6px 12px;
  border: 1px solid var(--border);
}
.btn-back:hover { color: var(--text); border-color: var(--text-muted); background: var(--bg-hover); }

#results-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.video-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.875rem 1rem;
  display: flex;
  gap: 0.875rem;
  align-items: flex-start;
  overflow: hidden;
}

.thumb-wrapper {
  position: relative;
  width: 160px;
  height: 90px;
  flex-shrink: 0;
  background: var(--border);
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.thumb-spinner {
  position: absolute;
}

.video-thumb {
  width: 160px;
  height: 90px;
  object-fit: cover;
  border-radius: 4px;
  position: relative;
  z-index: 1;
}

.video-info {
  flex: 1;
  min-width: 0;
}

.video-tags {
  display: flex;
  gap: 0.375rem;
  flex-wrap: wrap;
  align-items: center;
  margin-bottom: 0.375rem;
}

.type-badge {
  font-size: 0.7rem;
  font-weight: 600;
  padding: 0.15rem 0.5rem;
  border-radius: 9999px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.badge-hls { background: rgba(59,130,246,0.15); color: var(--info); }
.badge-dash { background: rgba(168,85,247,0.15); color: var(--purple); }
.badge-direct { background: rgba(34,197,94,0.15); color: var(--success); }

.quality-badge {
  font-size: 0.7rem;
  font-weight: 600;
  padding: 0.15rem 0.5rem;
  border-radius: 9999px;
  background: rgba(255,255,255,0.08);
  color: var(--text);
}

.meta-tag {
  font-size: 0.75rem;
  padding: 0.1rem 0.45rem;
  border-radius: 9999px;
  background: rgba(255,255,255,0.06);
  color: var(--text-muted);
}

.video-domain {
  font-size: 0.8rem;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.video-actions {
  display: flex;
  gap: 0.375rem;
  margin-top: 0.5rem;
  flex-wrap: wrap;
}

.btn-download, .btn-upload {
  font-size: 13px;
  padding: 6px 12px;
  border-radius: 6px;
  min-width: 0;
  text-align: center;
  white-space: nowrap;
}

.btn-download {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
}
.btn-download:hover { border-color: var(--text-muted); background: var(--bg-hover); }

.btn-upload {
  background: var(--primary);
  color: white;
}

.btn-downloading {
  background: var(--surface);
  color: var(--warning);
  border: 1px solid var(--warning);
  cursor: wait;
  font-size: 13px;
  padding: 6px 12px;
  border-radius: 6px;
  min-width: 0;
  text-align: center;
}

/* --- Spinner --- */
.spinner {
  display: inline-block;
  width: 1rem;
  height: 1rem;
  border: 2px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  vertical-align: middle;
  margin-right: 0.5rem;
}

.spinner-mini {
  display: inline-block;
  width: 0.7rem;
  height: 0.7rem;
  border: 1.5px solid var(--border);
  border-top-color: var(--text-muted);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  vertical-align: middle;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

#extract-error { margin-top: 0.5rem; }

/* --- Filtered / hidden results --- */
#filtered-section, #images-section {
  margin-top: 1rem;
}

#filtered-section summary, #images-section summary {
  font-size: 0.8rem;
  color: var(--text-muted);
  cursor: pointer;
  padding: 0.5rem 0;
  user-select: none;
}

#filtered-section summary:hover, #images-section summary:hover {
  color: var(--text);
}

#filtered-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-top: 0.5rem;
  opacity: 0.6;
}

#filtered-list .video-card {
  border-style: dashed;
}

#images-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-top: 0.5rem;
}

.results-loading {
  text-align: center;
  color: var(--text-muted);
  padding: 1rem 0;
  font-size: 0.85rem;
}

/* --- Settings & Admin --- */
.settings-group {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 24px;
  margin-bottom: 1rem;
}

.settings-group h3 {
  font-size: 0.95rem;
  font-weight: 600;
  margin-bottom: 0.5rem;
}

.settings-desc {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-bottom: 0.75rem;
}

.settings-group form {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.settings-group form[hidden] { display: none; }

.settings-actions {
  display: flex;
  gap: 0.5rem;
}

.settings-status {
  font-size: 0.8rem;
  margin-top: 0.5rem;
}

.settings-status.success { color: var(--success); }
.settings-status.error { color: var(--danger); }

.settings-label {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 0.25rem;
}

.btn-danger {
  background: transparent;
  color: var(--danger);
  border: 1px solid var(--danger);
}
.btn-danger:hover { background: rgba(224,85,85,0.1); }

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.95rem;
  color: var(--text-muted);
  cursor: pointer;
}

.checkbox-label input[type="checkbox"] {
  width: 1.25rem;
  height: 1.25rem;
  accent-color: var(--primary);
}

.user-card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.625rem 0.875rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.375rem;
}

.user-card-info {
  font-size: 0.85rem;
}

.user-card-info .admin-badge {
  font-size: 0.7rem;
  font-weight: 600;
  padding: 0.1rem 0.4rem;
  border-radius: 9999px;
  background: rgba(99,102,241,0.15);
  color: var(--primary);
  margin-left: 0.5rem;
}

.user-card button {
  font-size: 13px;
  padding: 4px 10px;
}

#settings-header, #admin-header {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

#settings-header h2, #admin-header h2 {
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}

/* Submit row with VPN toggle */
.submit-row {
  display: flex;
  align-items: center;
  gap: 1.25rem;
}
.submit-row button { margin-left: auto; }
.vpn-toggle {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.8rem;
  color: var(--text-muted);
  white-space: nowrap;
  cursor: pointer;
}
.vpn-toggle[hidden] {
  display: none;
}
.vpn-location {
  font-size: 0.7rem;
  color: var(--primary);
  font-weight: 600;
}

/* VPN country selector */
.vpn-selector {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}
.vpn-selector select {
  flex: 1;
  padding: 0.5rem;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.85rem;
}
.vpn-selector button {
  padding: 0.5rem 1rem;
}

/* --- Mobile --- */
@media (max-width: 768px) {
  /* Prevent iOS zoom on input focus */
  input, select, textarea { font-size: 16px !important; }

  #app-view {
    padding: 1rem 0.75rem;
  }

  #auth-container {
    padding: 32px 24px;
    margin: 0 16px;
  }

  #auth-container h1 {
    font-size: 1.5rem;
  }

  header h1 {
    font-size: 1.1rem;
  }

  .header-nav {
    gap: 0.375rem;
  }

  .btn-nav, #logout-btn {
    font-size: 12px;
    padding: 5px 8px;
  }

  .submit-emoji {
    font-size: 3rem;
  }

  #submit-form {
    width: 90%;
  }

  #submit-section {
    min-height: calc(100dvh - 8rem);
    margin-top: -2rem;
  }

  /* Stack video cards vertically on mobile */
  .video-card {
    flex-direction: column;
    gap: 0.625rem;
  }

  .thumb-wrapper {
    width: 100%;
    height: auto;
    aspect-ratio: 16 / 9;
  }

  .video-thumb {
    width: 100%;
    height: auto;
    aspect-ratio: 16 / 9;
  }

  .video-actions {
    flex-wrap: wrap;
  }

  .btn-download, .btn-upload, .btn-downloading {
    flex: 1;
    min-width: 0;
  }

  .settings-group {
    padding: 16px;
  }

  .recovery-box {
    padding: 1.5rem;
  }

  .user-card {
    flex-direction: column;
    gap: 0.5rem;
    align-items: flex-start;
  }

  .user-card button {
    align-self: flex-end;
  }
}
