:root {
  color-scheme: dark;
  --bg: #0f172a;
  --bg-secondary: #111827;
  --surface: rgba(30, 41, 59, 0.9);
  --border: rgba(148, 163, 184, 0.22);
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #22d3ee;
  --accent-dark: #0891b2;
  --accent-soft: rgba(34, 211, 238, 0.15);
  --danger: #f87171;
  --success: #4ade80;
  --radius: 12px;
}

body[data-theme="light"] {
  color-scheme: light;
  --bg: #f8fafc;
  --bg-secondary: #e2e8f0;
  --surface: rgba(255, 255, 255, 0.92);
  --border: rgba(15, 23, 42, 0.12);
  --text: #0f172a;
  --muted: #475569;
  --accent: #0284c7;
  --accent-dark: #0369a1;
  --accent-soft: rgba(2, 132, 199, 0.12);
  --danger: #dc2626;
  --success: #15803d;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background:
    radial-gradient(circle at top, rgba(34, 211, 238, 0.18), transparent 34%),
    radial-gradient(circle at bottom right, rgba(74, 222, 128, 0.11), transparent 28%),
    linear-gradient(160deg, var(--bg), var(--bg-secondary));
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans Arabic", "Noto Sans", sans-serif;
  display: flex;
  justify-content: center;
}

html[dir="rtl"] body {
  font-family: "Noto Sans Arabic", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

.page {
  width: 100%;
  max-width: 560px;
  padding: 4.5rem 1.5rem 3rem;
}

.hero {
  text-align: center;
  margin-bottom: 2rem;
}

/* Pinned to the inline-end corner: top-right in LTR (en), top-left in RTL (ar). */
.toolbar {
  position: absolute;
  top: 1rem;
  inset-inline-end: 1rem;
  display: flex;
  gap: 0.5rem;
}

.icon-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  color: var(--muted);
  text-decoration: none;
  transition: background 0.15s ease, color 0.15s ease;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.icon-toggle:hover,
.icon-toggle:focus-visible {
  background: var(--accent-soft);
  color: var(--text);
}

.icon-toggle-glyph {
  font-size: 0.85rem;
  font-weight: 700;
  line-height: 1;
}

.icon-toggle-glyph:lang(ar) {
  font-size: 1.15rem;
}

.hero h1 {
  font-size: 2rem;
  margin: 0 0 0.5rem;
}

.hero p {
  color: var(--muted);
  margin: 0;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  margin-bottom: 1.5rem;
  box-shadow: 0 20px 45px rgba(2, 8, 23, 0.16);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
}

.dropzone {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 2rem 1rem;
  cursor: pointer;
  transition: border-color 0.15s ease, background 0.15s ease;
}

.dropzone:hover,
.dropzone:focus-within {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.dropzone:has(input:valid) {
  border-style: solid;
  border-color: var(--success);
  background: rgba(74, 222, 128, 0.1);
}

.dropzone input[type="file"] {
  color: var(--muted);
  max-width: 100%;
}

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

.dropzone:has(input:valid) .dropzone-icon {
  color: var(--success);
}

.dropzone-icon--filled {
  display: none;
}

.dropzone:has(input:valid) .dropzone-icon--empty {
  display: none;
}

.dropzone:has(input:valid) .dropzone-icon--filled {
  display: block;
}

.dropzone-text {
  color: var(--muted);
  font-size: 0.9rem;
}

.dropzone-text--filled {
  display: none;
}

.dropzone:has(input:valid) .dropzone-text--empty {
  display: none;
}

.dropzone:has(input:valid) .dropzone-text--filled {
  display: block;
  color: var(--success);
  font-weight: 600;
}

.dropzone-clear {
  display: none;
  appearance: none;
  -webkit-appearance: none;
  width: 100%;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  margin-top: 0.25rem;
  padding: 0;
  border: none;
  background: none;
  color: var(--muted);
  font: inherit;
  font-size: 0.85rem;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.dropzone-clear:hover {
  color: var(--danger);
  text-decoration: underline;
}

.card:has(.dropzone input:valid) .dropzone-clear {
  display: flex;
}

.quality {
  display: flex;
  gap: 0.75rem;
  flex-wrap: wrap;
  border: none;
  padding: 1rem 0 0;
  margin: 0;
}

.quality legend {
  color: var(--muted);
  font-size: 0.85rem;
  padding: 0 0 0.5rem;
}

.quality-option {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  min-height: 44px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.4rem 0.9rem;
  cursor: pointer;
  font-size: 0.9rem;
  touch-action: manipulation;
}

.quality-option input[type="radio"] {
  width: 1.1rem;
  height: 1.1rem;
}

.quality-option:has(input:checked) {
  border-color: var(--accent);
  color: var(--accent);
}

button[type="submit"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  min-height: 44px;
  margin-top: 1.25rem;
  padding: 0.85rem;
  border: none;
  border-radius: var(--radius);
  background: var(--accent);
  color: #04111a;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
  transition: background 0.15s ease;
}

button[type="submit"]:hover {
  background: var(--accent-dark);
  color: var(--text);
}

.htmx-indicator {
  display: none;
  margin-top: 0.75rem;
}

.htmx-request .htmx-indicator,
.htmx-request.htmx-indicator {
  display: block;
}

.progress-bar {
  position: relative;
  width: 100%;
  height: 6px;
  overflow: hidden;
  border-radius: 999px;
  background: var(--border);
}

.progress-bar::after {
  content: "";
  position: absolute;
  inset: 0;
  width: 40%;
  border-radius: 999px;
  background: var(--accent);
  animation: progress-indeterminate 1.2s ease-in-out infinite;
}

@keyframes progress-indeterminate {
  0% {
    transform: translateX(-100%);
  }
  100% {
    transform: translateX(250%);
  }
}

#result:empty {
  display: none;
}

.result-summary {
  text-align: center;
}

.result-summary p {
  margin: 0.25rem 0;
}

.result-savings {
  color: var(--success);
  font-weight: 600;
}

.result-error {
  color: var(--danger);
  margin: 0;
  text-align: center;
}

.download-button {
  display: block;
  text-align: center;
  margin-top: 1rem;
  padding: 0.75rem;
  border-radius: var(--radius);
  background: var(--success);
  color: #061d11;
  font-weight: 600;
  text-decoration: none;
}

.reset-link {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  margin-top: 0.5rem;
  color: var(--muted);
  text-decoration: none;
  font-size: 0.85rem;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.reset-link:hover {
  color: var(--text);
  text-decoration: underline;
}

html[dir="rtl"] .hero,
html[dir="rtl"] .result-summary,
html[dir="rtl"] .result-error,
html[dir="rtl"] .htmx-indicator,
html[dir="rtl"] .download-button,
html[dir="rtl"] .reset-link {
  text-align: center;
}

html[dir="rtl"] .quality {
  justify-content: flex-start;
}

@media (max-width: 560px) {
  .page {
    padding: 4.25rem 1rem 2rem;
  }

  .hero {
    margin-bottom: 1.25rem;
  }

  .toolbar {
    top: 0.75rem;
    inset-inline-end: 0.75rem;
  }

  .quality {
    gap: 0.5rem;
  }

  .quality-option {
    width: 100%;
    justify-content: center;
  }
}
