:root {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-synthesis: none;
  color: #233630;
  background: #f5f6f5;
  --ink: #19332e;
  --muted: #697771;
  --line: #dfe6e1;
  --lime: #c9f476;
  --ease-out: cubic-bezier(.2,.8,.2,1);
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; overflow: hidden; }
body { overscroll-behavior: none; }
button, input { font: inherit; }
button, input, a { -webkit-tap-highlight-color: transparent; }
button, a { touch-action: manipulation; }
button { cursor: pointer; }
button:disabled { cursor: default; opacity: .3; }
button, .primary, .file-download, .original-download {
  transition: transform 120ms var(--ease-out), background-color 150ms ease, color 150ms ease, opacity 150ms ease;
}
a { color: inherit; }
svg {
  width: 22px; height: 22px; flex-shrink: 0;
  fill: none; stroke: currentColor; stroke-width: 1.7;
  stroke-linecap: round; stroke-linejoin: round;
}
.icon-defs { position: absolute; width: 0; height: 0; overflow: hidden; }
[hidden] { display: none !important; }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0;
}
:focus-visible { outline: 2px solid #6c914b; outline-offset: -3px; }

/* Each result occupies the space left by the source card and browser chrome. */
main {
  width: 100%; max-width: 760px; height: 100%; height: 100dvh;
  margin: 0 auto; display: flex; flex-direction: column; justify-content: center;
  gap: 12px; padding: 24px;
  padding-top: max(16px, env(safe-area-inset-top));
  padding-bottom: max(16px, env(safe-area-inset-bottom));
  padding-left: max(16px, env(safe-area-inset-left));
  padding-right: max(16px, env(safe-area-inset-right));
}
.workspace { flex: 0 0 auto; min-width: 0; }
.download-form {
  display: grid; gap: 4px; padding: 8px; background: #fff;
  border: 1px solid var(--line); border-radius: 16px;
  box-shadow: 0 4px 28px #19332e06;
  transition: border-color 150ms ease, box-shadow 150ms ease;
}
.download-form:focus-within { border-color: #90a695; }
.input-row { display: flex; align-items: center; gap: 10px; padding-left: 10px; }
.input-icon { color: #7c8b7e; width: 20px; height: 20px; }
input {
  min-width: 0; width: 100%; padding: 14px 0;
  background: none; border: 0; outline: none; font-size: 16px; color: var(--ink);
}
input:focus-visible { outline: none; }
input::placeholder { color: #829087; }
.paste-button, .gallery-button {
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0; min-width: 48px; min-height: 48px;
  border: 0; border-radius: 10px; background: transparent; color: #607568;
}
.paste-button { padding: 12px; gap: 6px; background: #edf2ed; font-size: 13px; }
.paste-button:hover:not(:disabled), .gallery-button:hover:not(:disabled) { background: #e1eae1; }
.source-preview { display: flex; align-items: center; gap: 10px; min-width: 0; padding-left: 4px; }
.source-icon {
  width: 36px; height: 36px; display: grid; place-items: center;
  flex-shrink: 0; color: var(--ink); background: #edf3e9; border-radius: 10px;
}
.source-text { min-width: 0; flex: 1; }
.source-text p { margin: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#source-author { font-weight: 650; font-size: 14px; color: var(--ink); }
.form-error {
  margin: 0; padding: 8px 10px; color: #a7422d; font-size: 14px; line-height: 1.5;
  max-height: 40dvh; overflow: auto; overflow-wrap: anywhere;
}
.results, #success { display: flex; flex-direction: column; flex: 1; min-height: 0; }
#success { gap: 8px; }
.loading-panel {
  display: flex; align-items: center; gap: 12px; padding: 8px 16px;
  background: #eef4e9; border-radius: 15px;
}
.loading-panel p { white-space: nowrap; margin: 0; font-size: 14px; color: var(--muted); }
.spinner {
  height: 20px; width: 20px; flex-shrink: 0;
  border: 2px solid #cad8c3; border-top-color: #436542; border-radius: 50%;
  animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.text-button { min-height: 44px; padding: 8px 4px; margin-left: auto; border: 0; background: none; color: #607760; font-size: 14px; }
.result-heading { display: flex; justify-content: space-between; gap: 8px; align-items: center; flex-shrink: 0; }
#result-summary { font-size: 12px; line-height: 20px; color: var(--muted); margin: 0 4px; }
.primary {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 44px; padding: 10px 12px; border: 0; border-radius: 10px;
  background: var(--lime); color: var(--ink); font-size: 13px; font-weight: 650; text-decoration: none;
}
.primary:hover { background: #bce767; }
.primary svg { width: 18px; height: 18px; }
.gallery-navigation { display: flex; align-items: center; gap: 2px; }
.gallery-button { min-width: 44px; min-height: 44px; }
.gallery-button svg { width: 18px; height: 18px; }
#previous-media svg { transform: rotate(180deg); }
#gallery-position { margin: 0; min-width: 38px; text-align: center; color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; }
.notice {
  flex-shrink: 0; max-height: 15dvh; overflow: auto; margin: 0;
  background: #fff6de; border: 1px solid #ebddb6; border-radius: 10px;
  padding: 8px 12px; font-size: 12px; line-height: 1.4; color: #80621e;
}
.media-grid {
  position: relative; display: flex; gap: 12px; flex: 1; min-height: 0; min-width: 0;
  overflow-x: auto; overflow-y: hidden; scroll-snap-type: x mandatory;
  overscroll-behavior-x: contain; scrollbar-width: none; border-radius: 16px;
}
.media-grid::-webkit-scrollbar { display: none; }
.media-card {
  flex: 0 0 100%; min-width: 0; height: 100%; display: flex; flex-direction: column;
  scroll-snap-align: start; scroll-snap-stop: always;
  border: 1px solid var(--line); border-radius: 16px; background: #fff; overflow: hidden;
}
.media-preview {
  flex: 1; min-height: 0; position: relative; overflow: hidden; background: #e9eee8;
  display: flex; align-items: center; justify-content: center; color: var(--muted); font-size: 14px;
}
.media-preview img, .media-preview video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; }
.media-preview audio { max-width: 95%; }
.media-info { flex: 0 0 auto; padding: 0 12px 12px; min-width: 0; }
.media-caption { display: flex; align-items: flex-start; gap: 8px; }
.media-name {
  flex: 1; min-width: 0; margin: 0; padding: 11px 0;
  font-size: 14px; font-weight: 600; line-height: 22px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.media-name.expanded {
  white-space: pre-wrap; overflow-wrap: anywhere; overflow: auto;
  max-height: min(28dvh, 180px); overscroll-behavior: contain;
}
.caption-toggle {
  flex-shrink: 0; min-width: 44px; min-height: 44px; padding: 10px 0;
  border: 0; border-radius: 8px; background: none; color: var(--muted); font-size: 13px;
}
.caption-toggle:hover { color: var(--ink); background: #f0f5ec; }
.media-meta { font-size: 12px; line-height: 18px; color: var(--muted); margin: 0 0 10px; }
.file-download {
  display: flex; align-items: center; justify-content: center; gap: 8px; min-height: 48px;
  border-radius: 10px; padding: 10px; text-decoration: none; font-weight: 650; font-size: 15px;
  background: #e5efd9; color: var(--ink);
}
.file-download:hover { background: #d8e7c8; }
.file-download svg { width: 19px; height: 19px; }
.download-actions { display: flex; gap: 8px; }
.download-actions .file-download { flex: 1; }
.original-download {
  display: flex; align-items: center; justify-content: center; min-width: 72px; min-height: 48px;
  padding: 10px; border-radius: 10px; background: #f0f3ef; color: var(--muted);
  font-size: 13px; text-decoration: none;
}
.original-download:hover { background: #e5ece1; color: var(--ink); }
#toast {
  position: fixed; z-index: 2; bottom: max(16px, env(safe-area-inset-bottom)); left: 50%; transform: translateX(-50%);
  padding: 12px 18px; background: var(--ink); color: #fff; border-radius: 12px;
  font-size: 14px; line-height: 1.4; width: max-content; max-width: 90vw; text-align: center; box-shadow: 0 6px 25px #0002;
}
/* Short entrances run only when a panel becomes visible, never on polling. */
@keyframes reveal {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes toast-in {
  from { opacity: 0; transform: translate(-50%, 6px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}
.input-row, .source-preview { animation: fade-in 160ms ease-out; }
.loading-panel, .form-error, .notice { animation: reveal 180ms var(--ease-out); }
#success { animation: reveal 240ms var(--ease-out); }
#toast { animation: toast-in 180ms var(--ease-out); }
@media (prefers-reduced-motion: no-preference) {
  button:not(:disabled):active, .primary:active, .file-download:active, .original-download:active { transform: scale(.97); }
}
@media (max-width: 600px) {
  main { gap: 10px; padding: max(12px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left)); }
  .input-icon, .paste-button span { display: none; }
  .input-row { padding-left: 6px; gap: 6px; }
}
/* A sideways phone keeps the player beside its controls. */
@media (min-width: 500px) and (max-height: 520px) {
  main { max-width: 960px; gap: 8px; padding-top: max(8px, env(safe-area-inset-top)); padding-bottom: max(8px, env(safe-area-inset-bottom)); }
  .media-card { display: grid; grid-template-columns: minmax(0, 1.2fr) minmax(220px, 1fr); }
  .media-info { display: flex; flex-direction: column; justify-content: flex-end; padding: 8px 12px 12px; }
  .media-name.expanded { max-height: 24dvh; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
}
