:root {
  color-scheme: dark;
  --bg: #171a1e;
  --surface: #202429;
  --surface-raised: #262b31;
  --text: #edf0f2;
  --text-secondary: #b2bac2;
  --text-muted: #89939d;
  --border: #363d45;
  --accent: #82b8d4;
  --accent-strong: #284e62;
  --success: #8dc5a3;
  --error: #e39a91;
  --focus: #a9d8f0;
  --space-1: 0.375rem;
  --space-2: 0.625rem;
  --space-3: 0.875rem;
  --space-4: 1.125rem;
  --space-5: 1.5rem;
  --space-6: 2rem;
  --space-7: 2.75rem;
  --radius-sm: 5px;
  --radius-md: 8px;
  --control-height: 42px;
  --transition: 140ms ease;
}

body.light-theme {
  color-scheme: light;
  --bg: #f3f5f6;
  --surface: #ffffff;
  --surface-raised: #f8f9fa;
  --text: #20262c;
  --text-secondary: #4d5963;
  --text-muted: #697680;
  --border: #d8dee3;
  --accent: #276886;
  --accent-strong: #e5f1f6;
  --success: #257348;
  --error: #a43832;
  --focus: #176b91;
}

* {
  box-sizing: border-box;
}

html {
  min-width: 320px;
  min-height: 100%;
  background: var(--bg);
}

body {
  min-width: 320px;
  min-height: 100vh;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 "Segoe UI", system-ui, sans-serif;
  transition: background-color var(--transition), color var(--transition);
}

button,
input,
select {
  font: inherit;
}

button,
a,
input,
select {
  -webkit-tap-highlight-color: transparent;
}

button:focus-visible,
a:focus-visible,
input:focus-visible,
select:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

.app-shell {
  width: min(100% - 48px, 920px);
  margin: 0 auto;
}

.topbar {
  min-height: 64px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  color: var(--text);
  font-size: 14px;
  font-weight: 650;
  text-decoration: none;
}

.brand-mark {
  display: inline-flex;
  width: 26px;
  height: 26px;
  align-items: center;
  justify-content: center;
  color: var(--accent);
}

.brand-mark svg,
.empty-icon svg {
  width: 100%;
  height: 100%;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 1.5;
}

.brand-mark svg path:first-child {
  fill: currentColor;
  stroke: none;
}

.brand-mark svg .brand-cutout {
  fill: var(--bg);
  stroke: none;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: var(--space-4);
}

.local-label,
.dependency-status {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  color: var(--text-muted);
  font-size: 12px;
}

.local-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--success);
}

.theme-toggle {
  min-height: 32px;
  padding: 0 var(--space-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  transition: border-color var(--transition), color var(--transition);
}

.theme-toggle:hover {
  border-color: var(--text-muted);
  color: var(--text);
}

.workspace {
  max-width: 760px;
  margin: var(--space-7) auto 0;
}

.page-heading {
  margin-bottom: var(--space-5);
}

.section-kicker {
  margin: 0 0 var(--space-1);
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 650;
  letter-spacing: 0.07em;
  text-transform: uppercase;
}

h1,
h2,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: var(--space-1);
  font-size: 24px;
  font-weight: 650;
  letter-spacing: -0.025em;
  line-height: 1.25;
}

h2 {
  margin-bottom: 0;
  font-size: 16px;
  font-weight: 620;
  line-height: 1.35;
}

.intro {
  margin-bottom: 0;
  color: var(--text-secondary);
}

.url-form {
  padding: var(--space-4);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface);
}

.url-form > label,
.field-control > span {
  display: block;
  margin-bottom: var(--space-2);
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 600;
}

.url-controls {
  display: flex;
  gap: var(--space-2);
}

input,
select {
  min-width: 0;
  height: var(--control-height);
  padding: 0 var(--space-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text);
}

input {
  flex: 1;
}

input::placeholder {
  color: var(--text-muted);
  opacity: 1;
}

.button {
  min-height: var(--control-height);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  padding: 0 var(--space-4);
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  font-weight: 600;
  text-decoration: none;
  white-space: nowrap;
  cursor: pointer;
  transition: background-color var(--transition), border-color var(--transition), color var(--transition), opacity var(--transition);
}

.button-primary {
  background: var(--accent);
  color: #10191e;
}

.button-primary:hover {
  background: color-mix(in srgb, var(--accent) 85%, white);
}

.button-secondary {
  border-color: var(--border);
  background: transparent;
  color: var(--text);
}

.button-secondary:hover {
  background: var(--surface-raised);
}

.button-danger {
  border-color: color-mix(in srgb, var(--error) 48%, var(--border));
  background: transparent;
  color: var(--error);
}

.button-danger:hover {
  background: color-mix(in srgb, var(--error) 10%, transparent);
}

.button:disabled,
.theme-toggle:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.field-hint {
  margin: var(--space-2) 0 0;
  color: var(--text-muted);
  font-size: 12px;
}

.message {
  min-height: 0;
  margin-top: var(--space-2);
  color: var(--text-secondary);
  font-size: 13px;
}

.message:empty {
  display: none;
}

.message.error {
  color: var(--error);
}

.message.success {
  color: var(--success);
}

.empty-state {
  min-height: 150px;
  display: flex;
  align-items: center;
  gap: var(--space-4);
  margin-top: var(--space-5);
  padding: var(--space-5);
  border: 1px dashed var(--border);
  border-radius: var(--radius-md);
}

.empty-icon {
  width: 34px;
  height: 34px;
  flex: 0 0 auto;
  color: var(--text-muted);
}

.empty-state h2 {
  margin-bottom: var(--space-1);
  font-size: 14px;
}

.empty-state p {
  margin-bottom: 0;
  color: var(--text-muted);
  font-size: 13px;
}

.video-preview,
.download-options,
.download-status,
.download-result {
  margin-top: var(--space-5);
  padding-top: var(--space-5);
  border-top: 1px solid var(--border);
}

.video-preview {
  display: grid;
  grid-template-columns: minmax(180px, 260px) minmax(0, 1fr);
  align-items: center;
  gap: var(--space-5);
}

.thumbnail-wrap {
  position: relative;
  overflow: hidden;
  aspect-ratio: 16 / 9;
  border-radius: var(--radius-sm);
  background: var(--surface-raised);
}

.thumbnail-wrap img {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: cover;
}

.duration-badge {
  position: absolute;
  right: var(--space-2);
  bottom: var(--space-2);
  padding: 2px 6px;
  border-radius: 3px;
  background: rgb(0 0 0 / 78%);
  color: #fff;
  font-size: 11px;
  font-variant-numeric: tabular-nums;
}

.video-details {
  min-width: 0;
}

.video-details h2 {
  margin-bottom: var(--space-2);
  overflow-wrap: anywhere;
  font-size: 17px;
}

.video-channel,
.video-duration {
  margin: 0;
  color: var(--text-secondary);
  font-size: 13px;
}

.video-duration {
  margin-top: var(--space-1);
  color: var(--text-muted);
}

.available-formats {
  margin: var(--space-2) 0 0;
  color: var(--text-muted);
  font-size: 12px;
}

.section-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  margin-bottom: var(--space-4);
}

.dependency-note {
  color: var(--text-muted);
  font-size: 12px;
  text-align: right;
}

.controls-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: var(--space-3);
}

.field-control {
  min-width: 0;
}

.field-control > span {
  margin-bottom: var(--space-1);
}

.field-control select {
  width: 100%;
  padding-right: 30px;
}

.action-row {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  margin-top: var(--space-4);
}

.status-heading {
  margin-bottom: var(--space-3);
}

.status-label {
  color: var(--text-secondary);
  font-size: 13px;
  font-weight: 600;
  text-transform: capitalize;
}

.progress-track {
  height: 7px;
  overflow: hidden;
  border-radius: 4px;
  background: var(--surface-raised);
}

.progress-fill {
  width: 0;
  height: 100%;
  border-radius: inherit;
  background: var(--accent);
  transition: width 220ms ease;
}

.progress-track.is-indeterminate .progress-fill {
  width: 32%;
  animation: progress-indeterminate 1.8s ease-in-out infinite alternate;
}

.progress-summary {
  display: flex;
  justify-content: space-between;
  gap: var(--space-3);
  margin-top: var(--space-2);
  color: var(--text-secondary);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

.progress-stats {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: var(--space-3);
  margin: var(--space-4) 0 0;
}

.progress-stats div {
  min-width: 0;
}

.progress-stats dt {
  color: var(--text-muted);
  font-size: 11px;
}

.progress-stats dd {
  margin: 2px 0 0;
  overflow-wrap: anywhere;
  color: var(--text-secondary);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}

.download-result {
  padding-bottom: var(--space-1);
}

.result-heading {
  display: flex;
  align-items: center;
  gap: var(--space-3);
}

.result-heading h2 {
  font-size: 16px;
}

.result-mark {
  width: 28px;
  height: 28px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid color-mix(in srgb, var(--success) 60%, var(--border));
  border-radius: 50%;
  color: var(--success);
}

.result-filename {
  margin: var(--space-3) 0 0;
  overflow-wrap: anywhere;
  color: var(--text-secondary);
}

.app-footer {
  max-width: 760px;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: var(--space-4);
  margin: var(--space-7) auto var(--space-5);
  padding-top: var(--space-3);
  border-top: 1px solid var(--border);
}

.app-footer p {
  max-width: 520px;
  margin: 0;
  color: var(--text-muted);
  font-size: 11px;
}

.hidden {
  display: none !important;
}

.button-spinner {
  width: 14px;
  height: 14px;
  border: 2px solid rgb(16 25 30 / 30%);
  border-top-color: #10191e;
  border-radius: 50%;
  animation: spin 700ms linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

@keyframes progress-indeterminate {
  from { transform: translateX(-10%); }
  to { transform: translateX(220%); }
}

@media (max-width: 640px) {
  .app-shell {
    width: min(100% - 32px, 920px);
  }

  .workspace {
    margin-top: var(--space-6);
  }

  .video-preview {
    grid-template-columns: minmax(0, 1fr);
    gap: var(--space-3);
  }

  .thumbnail-wrap {
    max-width: 420px;
  }

  .controls-grid {
    grid-template-columns: minmax(0, 1fr);
  }

  .progress-stats {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 420px) {
  .app-shell {
    width: min(100% - 24px, 920px);
  }

  .topbar {
    min-height: 58px;
  }

  .header-actions {
    gap: var(--space-2);
  }

  .local-label {
    display: none;
  }

  .url-form {
    padding: var(--space-3);
  }

  .url-controls {
    flex-direction: column;
  }

  .url-controls .button {
    width: 100%;
  }

  .empty-state {
    padding: var(--space-3);
  }

  .app-footer {
    flex-direction: column;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
