:root {
  --bg: #0f1216;
  --surface: #171b21;
  --surface-2: #1f242c;
  --border: #2a313b;
  --text: #e8ecf1;
  --muted: #97a2b2;
  --primary: #3b82f6;
  --primary-strong: #2563eb;
  --secondary: #334155;
  --photo: #f59e0b;
  --video: #8b5cf6;
  --danger: #ef4444;
  --ok: #22c55e;
  --radius: 6px;
}

[data-theme="light"] {
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-2: #f1f5f9;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --primary: #2563eb;
  --primary-strong: #1d4ed8;
  --secondary: #e2e8f0;
  --photo: #d97706;
  --video: #7c3aed;
  --danger: #dc2626;
  --ok: #16a34a;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  line-height: 1.5;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.app-header {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  max-width: 900px;
  margin: 0 auto;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

#theme-toggle {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  padding: 0;
}

#theme-toggle:hover {
  background: var(--surface-2);
}

#theme-toggle .icon-sun {
  display: none;
}

#theme-toggle .icon-moon {
  display: inline-flex;
}

[data-theme="light"] #theme-toggle .icon-sun {
  display: inline-flex;
}

[data-theme="light"] #theme-toggle .icon-moon {
  display: none;
}

.brand-logo {
  width: 44px;
  height: 44px;
  object-fit: contain;
}

.brand-name {
  font-weight: 600;
}

.container {
  width: 100%;
  max-width: 900px;
  margin: 0 auto;
  padding: 20px 16px 48px;
  flex: 1;
}

.hero h1 {
  font-size: 1.6rem;
  margin: 8px 0 6px;
}

.subtitle {
  color: var(--muted);
  margin: 0 0 20px;
}

.input-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}

.url-field {
  position: relative;
  display: flex;
  align-items: center;
}

.url-field input {
  width: 100%;
  min-width: 0;
  padding: 12px 92px 12px 14px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 1rem;
}

.url-field input:focus {
  outline: 2px solid var(--primary);
  outline-offset: 1px;
}

.icon-btn {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 34px;
  height: 34px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  padding: 0;
}

.icon-btn:hover:not(:disabled) {
  color: var(--text);
}

.icon-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

#clear-btn {
  right: 50px;
  display: none;
}

.url-field.has-value #clear-btn {
  display: inline-flex;
}

#process-btn {
  right: 0;
  top: 0;
  bottom: 0;
  width: 46px;
  height: auto;
  transform: none;
  border-radius: 0 6px 6px 0;
}

.icon-btn-primary {
  background: var(--primary);
  color: #fff;
}

.icon-btn-primary:hover:not(:disabled) {
  background: var(--primary-strong);
  color: #fff;
}

.icon-btn.is-loading svg {
  display: none;
}

.icon-btn.is-loading::after {
  content: '';
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255, 255, 255, 0.35);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

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

.btn {
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 11px 16px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  color: var(--text);
  background: var(--secondary);
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
}

.btn-primary:hover:not(:disabled) {
  background: var(--primary-strong);
}

.btn-secondary {
  background: var(--secondary);
}

.btn-ghost {
  background: transparent;
  border-color: var(--border);
  color: var(--muted);
}

.btn-ghost:hover:not(:disabled) {
  color: var(--text);
  border-color: var(--muted);
}

.muted {
  color: var(--muted);
}

#results-meta {
  font-size: 0.8rem;
}

.alert {
  margin-top: 16px;
  padding: 12px 14px;
  border-radius: 6px;
  border: 1px solid var(--danger);
  background: rgba(239, 68, 68, 0.12);
  color: #fecaca;
  white-space: pre-line;
}

.notice {
  margin: 12px 0;
  padding: 10px 12px;
  border-radius: 6px;
  border: 1px solid #92400e;
  background: rgba(245, 158, 11, 0.12);
  color: #fcd34d;
  font-size: 0.9rem;
}

.results {
  margin-top: 24px;
}

.results-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}

.results-head h2 {
  margin: 0;
  font-size: 1.2rem;
}

.media-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 10px;
}

.media-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
}

.item-download {
  flex-shrink: 0;
  width: 38px;
  height: 38px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--primary);
  border-radius: 6px;
  background: var(--primary);
  color: #fff;
  cursor: pointer;
}

.item-download:hover:not(:disabled) {
  background: var(--primary-strong);
}

.item-download:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.media-thumb {
  width: 64px;
  height: 48px;
  border-radius: 4px;
  object-fit: cover;
  background: var(--surface-2);
  flex-shrink: 0;
}

.media-thumb.placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.7rem;
  color: var(--muted);
  text-transform: uppercase;
}

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

.media-name {
  font-size: 0.92rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.media-sub {
  font-size: 0.78rem;
  color: var(--muted);
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.type-tag {
  font-weight: 600;
}

.type-tag.photo {
  color: var(--photo);
}

.type-tag.video {
  color: var(--video);
}

.media-variant {
  margin-top: 6px;
}

.media-variant select {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 4px 8px;
  font-size: 0.8rem;
}

.actions {
  display: flex;
  gap: 10px;
  margin-top: 18px;
  flex-wrap: wrap;
}

.actions .btn {
  flex: 1;
  min-width: 160px;
}

.job-progress {
  margin-top: 12px;
  padding: 10px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
}

.progress-tube {
  height: 8px;
  background: var(--surface-2);
  border-radius: 4px;
  overflow: hidden;
  position: relative;
}

.progress-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--primary), var(--primary-strong));
  border-radius: 4px;
  transition: width 0.5s ease-out;
}

.progress-fill::after {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(90deg, 
    transparent 40%, 
    rgba(255,255,255,0.3) 50%, 
    transparent 60%);
  animation: wave 1.2s linear infinite;
}

.progress-fill.complete {
  width: 100%;
  transition: width 0.4s ease-out;
}

@keyframes wave {
  0% { transform: translateX(-100%); }
  100% { transform: translateX(100%); }
}

.progress-label {
  display: block;
  margin-top: 8px;
  font-size: 0.8rem;
  color: var(--muted);
  text-align: center;
}

.app-footer {
  padding: 16px;
  text-align: center;
  color: var(--muted);
  font-size: 0.8rem;
  border-top: 1px solid var(--border);
}

@media (max-width: 560px) {
  .hero h1 {
    font-size: 1.25rem;
    margin: 4px 0 4px;
    text-align: center;
  }
  .subtitle {
    font-size: 0.85rem;
    margin: 0 0 14px;
    text-align: center;
  }
  .input-card {
    background: transparent;
    border: none;
    padding: 0;
  }
  .url-field input {
    padding: 11px 88px 11px 12px;
    font-size: 0.95rem;
    text-align: left;
  }
  .icon-btn {
    width: 32px;
    height: 32px;
  }
  #process-btn {
    width: 44px;
    height: auto;
  }
  .actions .btn {
    flex-basis: 100%;
  }
  .media-thumb {
    width: 52px;
    height: 40px;
  }
}
