:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eceef2;
  --text: #16181d;
  --muted: #5d6470;
  --border: #d9dde4;
  --accent: #e60012;
  --accent-text: #ffffff;
  --ok: #1f7a4d;
  --ok-bg: #e2f4ea;
  --warn: #8a5a00;
  --warn-bg: #fdf0d5;
  --info: #1d4f91;
  --info-bg: #e3ecfa;
  --shadow: 0 1px 2px rgb(0 0 0 / 6%), 0 4px 16px rgb(0 0 0 / 6%);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111317;
    --surface: #1a1d23;
    --surface-2: #242830;
    --text: #e8eaee;
    --muted: #9aa1ad;
    --border: #2e333c;
    --accent: #ff3b47;
    --ok: #6fd3a0;
    --ok-bg: #173628;
    --warn: #f3c46b;
    --warn-bg: #3a2c10;
    --info: #8db8f5;
    --info-bg: #182a44;
    --shadow: 0 1px 2px rgb(0 0 0 / 40%), 0 4px 16px rgb(0 0 0 / 30%);
  }
}

* { box-sizing: border-box; }

#connection-dialog { max-width: 600px; }
#connection-dialog dt { font-weight: 600; margin-top: 12px; }
#connection-dialog dd { margin-left: 0; user-select: all; }
#connection-code { font-size: 32px; letter-spacing: 0.15em; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

button, input, select { font: inherit; color: inherit; }

button {
  background: var(--accent);
  color: var(--accent-text);
  border: 0;
  border-radius: 8px;
  padding: 8px 14px;
  cursor: pointer;
}
button.ghost { background: transparent; color: var(--muted); border: 1px solid var(--border); }
button:focus-visible, input:focus-visible, select:focus-visible, .card:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

input, select {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
}

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

/* Top bar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  flex-wrap: wrap;
  gap: 12px 24px;
  align-items: center;
  justify-content: space-between;
  padding: 14px 24px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.brand h1 { margin: 0; font-size: 20px; }
.summary { margin: 2px 0 0; color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; }
.controls { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.controls form { margin: 0; }
#search { width: min(320px, 100%); }

main { padding: 24px; max-width: 1600px; margin: 0 auto; }

.download-form { margin: 0 0 24px; }
.download-form label { display: block; margin-bottom: 6px; font-weight: 600; }
.download-controls { display: flex; flex-wrap: wrap; gap: 8px; }
.download-controls input { flex: 1 1 240px; min-width: 0; }
.download-controls button:disabled { opacity: .6; cursor: wait; }
#download-status { margin: 6px 0 0; font-size: 13px; }
#download-status:empty { display: none; }
#upload-help, #upload-status, #upload-results { font-size: 13px; margin: 6px 0; }
#upload-status:empty, #upload-results:empty, #downloads-action-status:empty { display: none; }
#upload-progress { width: 100%; margin-top: 10px; accent-color: var(--accent); }
#upload-results { overflow-wrap: anywhere; }
.extract-controls { margin-bottom: 24px; }
.extract-controls p { margin: 6px 0; font-size: 13px; }
#extract-status:empty { display: none; }
.downloads-heading { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.downloads-heading h2 { margin: 0; }
.download-clear { margin-left: auto; flex-shrink: 0; }
button:disabled { opacity: .6; cursor: default; }

.downloads { margin-bottom: 28px; }
.downloads h2 { margin: 0 0 12px; font-size: 18px; }
#downloads-connection:empty { display: none; }
.download-job { padding: 16px; margin-bottom: 12px; border: 1px solid var(--border); border-radius: 12px; background: var(--surface); }
.download-job-header { display: flex; align-items: center; gap: 14px; margin-bottom: 12px; }
.download-job-header > div { min-width: 0; }
.download-cover { width: 72px; height: 96px; object-fit: contain; border-radius: 6px; background: var(--surface-2); flex: 0 0 auto; }
.download-job h3 { font-size: 14px; margin: 0 0 4px; overflow-wrap: anywhere; }
.download-job-summary { margin: 0 0 12px; color: var(--muted); font-size: 13px; }
.download-parts { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 300px), 1fr)); gap: 12px; }
.download-part { display: flex; align-items: center; gap: 12px; padding: 12px; border-radius: 8px; background: var(--bg); min-width: 0; }
.download-part-info { min-width: 0; }
.download-part-name { margin: 0 0 4px; font-size: 14px; overflow-wrap: anywhere; }
.download-part-status, .download-part-bytes { margin: 2px 0; font-size: 13px; overflow-wrap: anywhere; }
.download-part-bytes { color: var(--muted); font-variant-numeric: tabular-nums; }
.download-part.failed .download-part-status { color: var(--accent); }
.progress-ring { position: relative; flex: 0 0 64px; width: 64px; height: 64px; color: var(--accent); }
.progress-ring svg { display: block; width: 64px; height: 64px; }
.progress-track, .progress-fill { fill: none; stroke-width: 5; }
.progress-track { stroke: var(--border); }
.progress-fill { stroke: currentColor; stroke-linecap: round; transition: stroke-dashoffset .3s linear; }
.progress-ring span { position: absolute; inset: 0; display: grid; place-items: center; font-size: 13px; font-weight: 600; font-variant-numeric: tabular-nums; }
.download-part.completed .progress-ring { color: var(--ok); }
.download-part.queued .progress-ring { color: var(--muted); }
.progress-ring.indeterminate svg { animation: progress-spin 1.5s linear infinite; }
@keyframes progress-spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) {
  .progress-ring.indeterminate svg { animation: none; }
  .progress-fill { transition: none; }
}

/* Grid */
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 16px;
}
.card {
  display: flex;
  flex-direction: column;
  text-align: left;
  padding: 0;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
  box-shadow: var(--shadow);
  transition: transform .12s ease;
}
.card:hover { transform: translateY(-2px); }
.icon-wrap { width: 100%; aspect-ratio: 1; background: var(--surface-2); }
.icon-wrap img, .icon-fallback { width: 100%; height: 100%; object-fit: cover; display: block; }
.icon-fallback {
  display: grid;
  aspect-ratio: 1;
  place-items: center;
  font-size: 42px;
  font-weight: 700;
  color: var(--muted);
}
.card-body { padding: 10px 12px 12px; }
.card h3 {
  margin: 0;
  font-size: 14px;
  line-height: 1.3;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.pub { margin: 2px 0 8px; font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.badges { display: flex; flex-wrap: wrap; gap: 4px; margin: 0; }
.badge {
  font-size: 11px;
  font-weight: 600;
  padding: 2px 7px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.badge.ok { background: var(--ok-bg); color: var(--ok); }
.badge.warn { background: var(--warn-bg); color: var(--warn); }
.badge.info { background: var(--info-bg); color: var(--info); }

.empty { color: var(--muted); grid-column: 1 / -1; text-align: center; padding: 48px 0; }

/* Unidentified */
.unidentified { margin-top: 40px; }
.unidentified h2 { font-size: 16px; margin: 0 0 4px; }
.unidentified ul { padding-left: 18px; font-size: 13px; }

/* Detail dialog */
dialog {
  width: min(760px, calc(100vw - 32px));
  max-height: calc(100vh - 48px);
  padding: 20px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}
dialog::backdrop { background: rgb(0 0 0 / 50%); }
.detail-head { display: grid; grid-template-columns: 128px 1fr auto; gap: 16px; align-items: start; }
.detail-head img, .detail-head .icon-fallback { width: 128px; height: 128px; border-radius: 10px; }
.detail-head h2 { margin: 0 0 4px; font-size: 20px; }
.detail-head p { margin: 0 0 8px; }
.close { padding: 4px 10px; }
.desc { white-space: pre-line; color: var(--muted); font-size: 14px; }
.section h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 20px 0 6px; }
.files { width: 100%; border-collapse: collapse; font-size: 13px; }
.files td { padding: 6px 8px; border-top: 1px solid var(--border); vertical-align: top; }
.files td.num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.files .path { word-break: break-all; color: var(--muted); font-size: 12px; }

/* Login */
.login-page { display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.login-card {
  width: min(360px, 100%);
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 28px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
}
.login-card h1 { margin: 0 0 8px; font-size: 20px; }
.login-error { color: var(--accent); margin: 0; font-size: 13px; }

@media (max-width: 640px) {
  .topbar { padding: 12px 16px; }
  main { padding: 16px; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 12px; }
  .detail-head { grid-template-columns: 80px 1fr auto; }
  .detail-head img, .detail-head .icon-fallback { width: 80px; height: 80px; }
}
