:root {
  --bg: #0f1419;
  --panel: #1a222c;
  --text: #e7ecf1;
  --muted: #8b9aab;
  --accent: #3d9cf0;
  --ok: #3dbf7a;
  --warn: #e0a106;
  --bad: #e35d5d;
  --line: #2a3542;
  --font: "IBM Plex Sans", "Segoe UI", sans-serif;
  --mono: "IBM Plex Mono", ui-monospace, monospace;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--font);
  background:
    radial-gradient(1200px 600px at 10% -10%, #1b2a3a 0%, transparent 60%),
    var(--bg);
  color: var(--text);
  min-height: 100vh;
  overflow-x: hidden;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 1.5rem;
  border-bottom: 1px solid var(--line);
  background: rgba(15, 20, 25, 0.85);
  backdrop-filter: blur(8px);
  position: sticky;
  top: 0;
  gap: 0.75rem;
  flex-wrap: wrap;
  z-index: 10;
}

.brand {
  font-weight: 700;
  letter-spacing: 0.02em;
  color: var(--text);
  font-size: 1.1rem;
}

nav {
  display: flex;
  gap: 1rem;
  align-items: center;
  flex-wrap: wrap;
}

main {
  padding: 1.5rem;
  max-width: 1100px;
  width: 100%;
  margin: 0 auto;
  overflow-x: hidden;
}

h1 { margin-top: 0; font-size: 1.6rem; }
h2 { font-size: 1.1rem; margin-top: 2rem; }

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

table {
  width: 100%;
  max-width: 100%;
  table-layout: fixed;
  border-collapse: collapse;
  background: var(--panel);
  border: 1px solid var(--line);
}

th, td {
  text-align: left;
  padding: 0.65rem 0.75rem;
  border-bottom: 1px solid var(--line);
  vertical-align: middle;
  min-width: 0;
}

th {
  color: var(--muted);
  font-weight: 600;
  font-size: 0.85rem;
}

.mono { font-family: var(--mono); font-size: 0.85rem; }
.truncate {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
  max-width: 100%;
}

.tabular {
  font-variant-numeric: tabular-nums;
}

.nowrap { white-space: nowrap; }

.badge {
  display: inline-block;
  padding: 0.15rem 0.5rem;
  border: 1px solid var(--line);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.badge-completed, .badge-ready { color: var(--ok); border-color: var(--ok); }
.badge-running, .badge-converting, .badge-queued, .badge-preparing,
.badge-ready_to_upload, .badge-uploading { color: var(--warn); border-color: var(--warn); }
.badge-failed { color: var(--bad); border-color: var(--bad); }
.badge-cancelled, .badge-not_converted { color: var(--muted); }

button, .encode-form button {
  background: var(--accent);
  color: #061018;
  border: 0;
  padding: 0.4rem 0.75rem;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

button:hover { filter: brightness(1.08); }

a.button {
  display: inline-block;
  background: transparent;
  color: var(--text);
  border: 1px solid var(--line);
  padding: 0.4rem 0.75rem;
  font-weight: 600;
  text-decoration: none;
}
a.button:hover {
  text-decoration: none;
  border-color: var(--accent);
  color: var(--accent);
}

.inline { display: inline; margin: 0; }
.inline button {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--line);
}

.encode-form {
  display: flex;
  gap: 0.4rem;
  align-items: center;
  min-width: 0;
}

.source-encode {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  align-items: stretch;
}

.source-encode .cache-btn {
  align-self: flex-start;
}

.encode-form input[type="text"] {
  background: var(--bg);
  border: 1px solid var(--line);
  color: var(--text);
  padding: 0.4rem 0.55rem;
  font-family: var(--mono);
  min-width: 0;
  flex: 1 1 7rem;
  width: 100%;
  max-width: 100%;
}

.encode-form input[type="text"]:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.toolbar {
  display: flex;
  gap: 0.75rem;
  align-items: center;
  margin: 0 0 1rem;
  flex-wrap: wrap;
}

.meta {
  display: grid;
  grid-template-columns: 8rem minmax(0, 1fr);
  gap: 0.4rem 1rem;
  background: var(--panel);
  border: 1px solid var(--line);
  padding: 1rem;
}

.meta dt { color: var(--muted); }
.meta dd { margin: 0; }

.actions {
  display: flex;
  gap: 0.75rem;
  align-items: center;
  margin: 1rem 0;
}

.log {
  background: #0a0e12;
  border: 1px solid var(--line);
  padding: 1rem;
  overflow: auto;
  max-height: 28rem;
  font-family: var(--mono);
  font-size: 0.8rem;
  white-space: pre-wrap;
}

.error { color: var(--bad); }

.login-body {
  display: grid;
  place-items: center;
  min-height: 100vh;
}

.login-form {
  width: min(24rem, 92vw);
  background: var(--panel);
  border: 1px solid var(--line);
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.login-form input {
  background: var(--bg);
  border: 1px solid var(--line);
  color: var(--text);
  padding: 0.6rem 0.7rem;
  font: inherit;
}

.nav-user {
  font-size: 0.85rem;
}

.ok-msg { color: var(--ok); }

.panel-form {
  background: var(--panel);
  border: 1px solid var(--line);
  padding: 1rem 1.25rem;
  margin: 0 0 1.5rem;
  max-width: 28rem;
}

.panel-form h2 { margin-top: 0; }

.user-create-form,
.inline-form {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
  min-width: 0;
}

.user-create-form {
  flex-direction: column;
  align-items: stretch;
}

.user-create-form input,
.user-create-form select,
.inline-form input,
.inline-form select {
  background: var(--bg);
  border: 1px solid var(--line);
  color: var(--text);
  padding: 0.4rem 0.55rem;
  font: inherit;
}

.inline-form input,
.inline-form select {
  min-width: 0;
  max-width: 100%;
  flex: 1 1 7rem;
}

.inline-form input[type="password"] {
  width: auto;
  min-width: 0;
  flex: 1 1 8rem;
  font-family: var(--mono);
  font-size: 0.85rem;
}

button.button-muted {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--line);
}

.badge-role-admin { color: var(--accent); border-color: var(--accent); }
.badge-role-operator { color: var(--warn); border-color: var(--warn); }
.badge-role-viewer { color: var(--muted); border-color: var(--line); }

.upload-panel {
  max-width: 36rem;
}

.upload-panel input[type="file"] {
  background: var(--bg);
  border: 1px solid var(--line);
  color: var(--text);
  padding: 0.5rem;
  font: inherit;
}

.upload-live-log {
  margin-top: 0.25rem;
  font-size: 0.75rem;
  max-width: 28rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.mh-upload-options {
  border: 1px solid var(--line);
  padding: 0.75rem 1rem;
  margin: 0.5rem 0 0;
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
}

.mh-upload-options legend {
  padding: 0 0.35rem;
  color: var(--muted);
  font-size: 0.85rem;
}

.mh-radio {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  font-weight: 500;
}

.mh-conditional {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  margin-top: 0.25rem;
  padding-top: 0.35rem;
  border-top: 1px solid var(--line);
}

.mh-conditional[hidden] {
  display: none;
}

.site-actions {
  display: flex;
  gap: 0.35rem;
  align-items: center;
  white-space: nowrap;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2rem;
  height: 2rem;
  padding: 0;
  background: transparent;
  color: var(--text);
  border: 1px solid var(--line);
  font-weight: 700;
  line-height: 1;
}

.icon-btn:hover:not(:disabled) {
  border-color: var(--accent);
  color: var(--accent);
  filter: none;
}

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

.icon-plus { font-size: 1.15rem; }
.icon-link { font-size: 0.95rem; }
.icon-info {
  font-size: 0.85rem;
  font-style: italic;
  font-family: var(--mono);
}

.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;
}

.mh-dialog {
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  padding: 0;
  max-width: min(32rem, 94vw);
  width: 100%;
}

.mh-dialog::backdrop {
  background: rgba(0, 0, 0, 0.55);
}

.mh-dialog form,
.mh-info {
  padding: 1.25rem 1.35rem;
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}

.mh-dialog h2 {
  margin: 0;
  font-size: 1.15rem;
}

.mh-label {
  font-size: 0.85rem;
  color: var(--muted);
}

.mh-dialog input[type="text"] {
  background: var(--bg);
  border: 1px solid var(--line);
  color: var(--text);
  padding: 0.5rem 0.6rem;
  font-family: var(--mono);
  font-size: 0.9rem;
}

.mh-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.35rem;
}

.mh-error { margin: 0; }
.mh-info .meta { margin: 0; }

.mh-busy {
  display: none;
  align-items: center;
  gap: 0.55rem;
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
}

.mh-dialog.is-busy .mh-busy {
  display: flex;
}

.mh-dialog.is-busy form,
.mh-dialog.is-busy .mh-info {
  pointer-events: none;
}

.mh-dialog.is-busy #link-submit,
.mh-dialog.is-busy #link-cancel,
.mh-dialog.is-busy #info-refresh,
.mh-dialog.is-busy #info-update-src,
.mh-dialog.is-busy #info-edit-toggle,
.mh-dialog.is-busy #info-edit-save,
.mh-dialog.is-busy #info-edit-unlink,
.mh-dialog.is-busy #info-edit-cancel,
.mh-dialog.is-busy #info-close {
  pointer-events: none;
}

button.button-danger {
  background: transparent;
  color: var(--bad);
  border: 1px solid var(--bad);
}

button.button-danger:hover:not(:disabled) {
  filter: brightness(1.1);
}

.mh-edit {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 0.75rem 0.85rem;
  border: 1px solid var(--line);
  background: var(--bg);
}

.mh-edit[hidden] {
  display: none !important;
}

.mh-edit-hint {
  margin: 0;
  font-size: 0.85rem;
}

.mh-edit-actions {
  margin-top: 0.15rem;
}

.mh-spinner {
  width: 1rem;
  height: 1rem;
  border: 2px solid var(--line);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: mh-spin 0.7s linear infinite;
  flex-shrink: 0;
}

.mh-spinner-btn {
  display: inline-block;
  width: 0.85rem;
  height: 0.85rem;
  vertical-align: -0.1rem;
  margin-right: 0.35rem;
  border-color: rgba(6, 16, 24, 0.25);
  border-top-color: #061018;
}

#link-submit,
#info-refresh,
#info-update-src,
#info-edit-save,
#info-edit-toggle,
#sync-catalog-btn,
#upload-submit-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 8.5rem;
}

#sync-catalog-form.is-busy #sync-catalog-btn,
#upload-form.is-busy #upload-submit-btn {
  pointer-events: none;
}

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

.upload-file-list {
  list-style: none;
  margin: 0;
  padding: 0.35rem 0 0;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

.upload-file-list li {
  display: flex;
  justify-content: space-between;
  gap: 0.75rem;
  font-size: 0.9rem;
  min-width: 0;
}

.upload-file-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

.upload-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
}

.upload-progress {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  margin-top: 0.35rem;
}

.upload-progress-meta {
  margin: 0;
  font-size: 0.85rem;
}

.pct-meter {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  min-width: 0;
}

.pct-meter-lg {
  width: 100%;
}

.pct-meter-track {
  flex: 1 1 auto;
  min-width: 0;
  height: 0.55rem;
  background: var(--bg);
  border: 1px solid var(--line);
  overflow: hidden;
}

.pct-meter-lg .pct-meter-track {
  height: 0.7rem;
}

.pct-meter-fill {
  display: block;
  height: 100%;
  width: 100%;
  background: var(--accent);
  transform: scaleX(0);
  transform-origin: left center;
  transition: transform 0.18s ease-out;
}

.pct-meter-track.is-processing .pct-meter-fill {
  background: var(--ok);
}

.pct-meter-track.is-encoding .pct-meter-fill {
  background: var(--accent);
}

.pct-meter-track.is-uploading .pct-meter-fill {
  background: var(--ok);
}

.pct-meter-track.is-retrying .pct-meter-fill {
  background: var(--warn);
}

.pct-meter-label {
  flex: 0 0 2.75rem;
  text-align: right;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text);
}

main > .pct-meter {
  max-width: 32rem;
  margin: 0 0 1rem;
}

.upload-banner {
  margin: 0;
  padding: 0.5rem 0.7rem;
  font-size: 0.9rem;
  line-height: 1.4;
  border: 1px solid var(--line);
}

.upload-banner.is-retrying {
  color: var(--warn);
  border-color: var(--warn);
  background: rgba(224, 161, 6, 0.08);
}

.upload-banner.is-ok {
  color: var(--ok);
  border-color: var(--ok);
  background: rgba(61, 191, 122, 0.08);
}

.upload-banner.is-bad {
  color: var(--bad);
  border-color: var(--bad);
  background: rgba(227, 93, 93, 0.08);
}

.upload-banner.is-info {
  color: var(--text);
}

.copy-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.85rem;
  height: 1.85rem;
  min-width: 1.85rem;
  padding: 0;
  flex-shrink: 0;
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--line);
  cursor: pointer;
}

.copy-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.copy-btn:hover {
  color: var(--accent);
  border-color: var(--accent);
  filter: none;
}

.copy-btn.is-copied {
  color: var(--ok);
  border-color: var(--ok);
}

.key-row {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  min-width: 0;
  margin-top: 0.2rem;
}

.key-row .truncate,
.key-row .mono {
  min-width: 0;
  flex: 1 1 auto;
}

.file-name {
  font-weight: 600;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.file-meta {
  margin-top: 0.15rem;
  font-size: 0.82rem;
}

.hub-error {
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
  font-size: 0.8rem;
  line-height: 1.35;
}

.cell-updated {
  color: var(--muted);
  font-size: 0.82rem;
  white-space: nowrap;
}

.data-table .col-status { width: 7.75rem; }
.data-table .col-progress { width: 9.5rem; }
.data-table .col-hub { width: 8.5rem; }
.data-table .col-updated { width: 6.75rem; }
.users-table .col-user { width: 8rem; }
.users-table .col-role { width: 6.5rem; }
.users-table .col-active { width: 4.5rem; }
.users-table .col-cache { width: 7.5rem; }
.users-table .col-toggle { width: 7.5rem; }

.sources-table .col-status { width: 8rem; }
.sources-table .col-size { width: 5.5rem; }
.sources-table .col-date { width: 6.75rem; }
.sources-table .col-site { width: 5.25rem; }
.sources-table .col-encode { width: 22rem; }

.page-sources main {
  max-width: 1320px;
}

@media (min-width: 841px) {
  .encode-form input[type="text"] {
    min-width: 12.5rem;
    flex: 1 1 12.5rem;
  }
}

.source-view-bar {
  display: flex;
  gap: 0.75rem 1rem;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  margin: 0 0 0.75rem;
}

.source-filters {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}

.source-filter {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  min-height: 2.25rem;
  padding: 0.35rem 0.7rem;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--muted);
  font-weight: 600;
  font-size: 0.85rem;
  text-decoration: none;
  cursor: pointer;
}

.source-filter:hover {
  text-decoration: none;
  border-color: var(--accent);
  color: var(--text);
}

.source-filter:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.source-filter.is-active {
  border-color: var(--accent);
  color: var(--text);
  background: rgba(61, 156, 240, 0.12);
}

.source-filter-count {
  color: var(--muted);
  font-weight: 500;
  font-size: 0.8rem;
}

.source-filter.is-active .source-filter-count {
  color: var(--accent);
}

.source-sort-form {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  flex-wrap: wrap;
}

.source-sort-form label {
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 600;
}

.source-sort-form select {
  background: var(--bg);
  border: 1px solid var(--line);
  color: var(--text);
  padding: 0.4rem 0.55rem;
  font: inherit;
  min-height: 2.25rem;
}

.source-sort-form select:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.source-view-meta {
  margin: 0 0 0.75rem;
}

.source-date {
  color: var(--muted);
  font-size: 0.82rem;
  white-space: nowrap;
}

.source-file {
  min-width: 0;
}

.source-name {
  font-weight: 600;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.source-key,
.source-master {
  margin-top: 0.15rem;
  font-size: 0.78rem;
}

.source-size {
  white-space: nowrap;
}

.encode-form button {
  flex: 0 0 auto;
}

@media (max-width: 840px) {
  .data-table thead,
  .sources-table thead {
    display: none;
  }

  .data-table,
  .data-table tbody,
  .sources-table,
  .sources-table tbody {
    display: block;
    border: 0;
    background: transparent;
  }

  .data-table tr,
  .sources-table tr {
    display: grid;
    gap: 0.4rem 0.75rem;
    padding: 0.9rem 1rem;
    margin-bottom: 0.65rem;
    background: var(--panel);
    border: 1px solid var(--line);
  }

  .data-table td,
  .sources-table td {
    border: 0;
    padding: 0;
    width: auto;
  }

  .data-table col,
  .sources-table col {
    width: auto;
  }

  .uploads-table tr,
  .jobs-table tr {
    grid-template-columns: 1fr auto;
    grid-template-areas:
      "status updated"
      "file file"
      "progress progress"
      "hub hub";
  }

  .uploads-table .cell-status,
  .jobs-table .cell-status { grid-area: status; }
  .uploads-table .cell-updated,
  .jobs-table .cell-updated { grid-area: updated; text-align: right; }
  .uploads-table .cell-file,
  .jobs-table .cell-file { grid-area: file; }
  .uploads-table .cell-progress,
  .jobs-table .cell-progress { grid-area: progress; }
  .uploads-table .cell-hub { grid-area: hub; }

  .users-table tr {
    grid-template-columns: 1fr auto;
    grid-template-areas:
      "user role"
      "password password"
      "roleedit roleedit"
      "toggle active"
      "cache cache";
  }
  .users-table .cell-user { grid-area: user; }
  .users-table .cell-role { grid-area: role; justify-self: end; }
  .users-table .cell-active { grid-area: active; }
  .users-table .cell-cache { grid-area: cache; }
  .users-table .cell-password { grid-area: password; }
  .users-table .cell-role-edit { grid-area: roleedit; }
  .users-table .cell-toggle { grid-area: toggle; }

  .sources-table tr {
    grid-template-columns: 1fr auto;
    grid-template-areas:
      "status date"
      "file file"
      "size size"
      "site encode";
  }
  .source-status { grid-area: status; }
  .source-size { grid-area: size; text-align: left; }
  .source-date { grid-area: date; text-align: right; }
  .source-file { grid-area: file; }
  .site-actions { grid-area: site; }
  .source-encode { grid-area: encode; }

  .encode-form {
    justify-content: flex-end;
  }
}

@media (prefers-reduced-motion: reduce) {
  .pct-meter-fill,
  .mh-spinner,
  .mh-spinner-btn {
    transition: none;
    animation: none;
  }
}

.tg-panel { max-width: 36rem; }

.tg-code-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin: 0.4rem 0;
}

.tg-code {
  font-family: var(--mono);
  font-size: 2rem;
  letter-spacing: 0.28em;
  margin: 0;
  font-weight: 700;
}

.tg-code-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  align-items: center;
}

.tg-watch-list {
  list-style: none;
  margin: 0.75rem 0 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.tg-watch-item {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  min-height: 2.75rem;
  padding: 0.25rem 0.35rem;
  cursor: pointer;
}

.tg-watch-item input {
  width: 1.1rem;
  height: 1.1rem;
  accent-color: var(--accent);
  flex-shrink: 0;
}

.tg-watch-name {
  font-weight: 600;
}
