:root {
  color-scheme: dark;
  --bg: #0a0f14;
  --surface: #10171f;
  --surface-raised: #151f29;
  --surface-input: #0c131a;
  --border: #283440;
  --border-soft: #202c37;
  --text: #edf3f7;
  --muted: #93a4b2;
  --faint: #637584;
  --accent: #58d6b1;
  --accent-strong: #36b991;
  --accent-ink: #06120f;
  --warning: #f2ba64;
  --danger: #ff6b6b;
  --danger-soft: rgba(255, 107, 107, 0.1);
  --success: #62d49c;
  --info: #6fb7f4;
  --focus: #9be8d1;
  --radius-sm: 7px;
  --radius: 12px;
  --radius-lg: 16px;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.22);
  --font-sans: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", sans-serif;
  --font-mono: "Cascadia Code", "SFMono-Regular", Consolas, monospace;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html { min-width: 320px; background: var(--bg); }

body {
  margin: 0;
  min-height: 100vh;
  color: var(--text);
  background:
    radial-gradient(circle at 78% -12%, rgba(46, 154, 129, 0.11), transparent 34rem),
    var(--bg);
  font-family: var(--font-sans);
  font-size: 15px;
  line-height: 1.5;
}

button, input, textarea { font: inherit; }
button { color: inherit; }

:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

.skip-link {
  position: fixed;
  z-index: 100;
  top: 10px;
  left: 10px;
  padding: 8px 12px;
  color: var(--accent-ink);
  background: var(--accent);
  border-radius: var(--radius-sm);
  transform: translateY(-160%);
}

.skip-link:focus { transform: translateY(0); }

.topbar {
  position: sticky;
  z-index: 20;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  min-height: 76px;
  padding: 12px clamp(18px, 4vw, 56px);
  border-bottom: 1px solid rgba(64, 82, 96, 0.65);
  background: rgba(10, 15, 20, 0.88);
  backdrop-filter: blur(16px);
}

.brand-block, .service-controls, .section-heading, .panel-header,
.inline-actions, .form-footer, .dialog-actions { display: flex; align-items: center; }

.brand-block { gap: 12px; }

.brand-mark {
  display: grid;
  width: 40px;
  height: 40px;
  place-items: center;
  color: var(--accent-ink);
  background: var(--accent);
  border-radius: 10px;
  font-family: var(--font-mono);
  font-size: 20px;
  font-weight: 800;
  box-shadow: inset 0 -2px 0 rgba(0, 0, 0, 0.16);
}

h1, h2, p { margin-top: 0; }
h1 { margin-bottom: 0; font-size: 18px; letter-spacing: -0.02em; }
h2 { margin-bottom: 0; font-size: 20px; letter-spacing: -0.02em; }

.eyebrow {
  margin-bottom: 2px;
  color: var(--muted);
  font-family: var(--font-mono);
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

.service-controls { justify-content: flex-end; gap: 9px; flex-wrap: wrap; }

.state-pill, .mode-badge, .save-state, .live-label {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-height: 32px;
  padding: 5px 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--muted);
  background: var(--surface);
  font-size: 12px;
  font-weight: 650;
  white-space: nowrap;
}

.state-dot, .live-label span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: currentColor;
  box-shadow: 0 0 0 4px rgba(147, 164, 178, 0.1);
}

.state-pill--running { color: var(--success); }
.state-pill--stopped { color: var(--warning); }
.state-pill--error { color: var(--danger); }
.state-pill--loading .state-dot { animation: pulse 1.2s ease-in-out infinite; }

.mode-badge--dry { color: #101815; border-color: var(--warning); background: var(--warning); }
.mode-badge--live { color: #fff; border-color: var(--danger); background: #a9343b; }

.button {
  min-height: 38px;
  padding: 8px 14px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 13px;
  font-weight: 700;
  transition: background-color 150ms ease, border-color 150ms ease, transform 150ms ease;
}

.button:hover:not(:disabled) { transform: translateY(-1px); }
.button:active:not(:disabled) { transform: translateY(0); }
.button:disabled { cursor: not-allowed; opacity: 0.47; }
.button--primary { color: var(--accent-ink); background: var(--accent); }
.button--primary:hover:not(:disabled) { background: #73e2c2; }
.button--secondary, .button--test { border-color: var(--border); background: var(--surface-raised); }
.button--secondary:hover:not(:disabled), .button--test:hover:not(:disabled) { border-color: #415363; background: #1b2833; }
.button--ghost { color: var(--muted); border-color: var(--border); background: transparent; }
.button--compact { min-height: 34px; padding: 6px 12px; }
.button--danger { color: white; background: #b23d44; }
.button--danger:hover:not(:disabled) { background: #ca484f; }

.shell { width: min(1460px, 100%); margin: 0 auto; padding: 34px clamp(18px, 4vw, 56px) 64px; }
.section-heading { justify-content: space-between; gap: 20px; margin-bottom: 16px; }

.alert {
  margin-bottom: 18px;
  padding: 11px 14px;
  border: 1px solid rgba(255, 107, 107, 0.45);
  border-radius: var(--radius-sm);
  color: #ffc0c0;
  background: var(--danger-soft);
}

.alert--success { color: #b9f3db; border-color: rgba(98, 212, 156, 0.4); background: rgba(98, 212, 156, 0.08); }

.metrics { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; }

.metric-group-title { grid-column: 1 / -1; display: flex; align-items: baseline; gap: 12px; margin: 9px 2px 0; padding-bottom: 2px; }
.metric-group-title h3 { margin: 0; font-size: 14px; }
.metric-group-title p { margin: 0; color: var(--faint); font-size: 11px; }
.metric-group-title--predownload { margin-top: 17px; }

.metric-card {
  grid-column: span 1;
  min-width: 0;
  min-height: 132px;
  padding: 18px;
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  background: linear-gradient(145deg, rgba(22, 32, 42, 0.96), rgba(14, 21, 28, 0.96));
  box-shadow: var(--shadow);
}

.metric-card--wide { grid-column: span 2; }
.metric-card--danger { border-color: rgba(255, 107, 107, 0.28); }
.metric-card--predownload { border-color: rgba(88, 214, 177, 0.26); }
.metric-label { margin-bottom: 13px; color: var(--muted); font-size: 12px; font-weight: 700; }
.metric-value { margin-bottom: 4px; font-family: var(--font-mono); font-size: clamp(26px, 3vw, 34px); font-weight: 750; letter-spacing: -0.05em; }
.metric-value--time { overflow: hidden; font-size: 19px; letter-spacing: -0.03em; text-overflow: ellipsis; white-space: nowrap; }
.metric-note { margin-bottom: 0; overflow: hidden; color: var(--faint); font-size: 12px; text-overflow: ellipsis; white-space: nowrap; }

.skeleton { color: transparent; border-radius: 5px; background: linear-gradient(90deg, #1b2732, #263644, #1b2732); background-size: 240% 100%; animation: shimmer 1.5s linear infinite; }

.workspace { display: grid; grid-template-columns: minmax(0, 1.45fr) minmax(360px, 0.8fr); gap: 18px; margin-top: 30px; align-items: start; }
.panel { min-width: 0; border: 1px solid var(--border-soft); border-radius: var(--radius-lg); background: var(--surface); box-shadow: var(--shadow); }
.panel-header { justify-content: space-between; gap: 16px; padding: 20px 22px; border-bottom: 1px solid var(--border-soft); }
.save-state { min-height: 28px; padding: 3px 9px; }
.save-state--dirty { color: var(--warning); }
.save-state--saved { color: var(--success); }

.info-notice { display: flex; gap: 12px; margin: 20px 22px 0; padding: 13px 14px; border: 1px solid rgba(88, 214, 177, 0.28); border-radius: var(--radius); background: rgba(88, 214, 177, 0.06); }
.info-notice strong { color: #a3ead5; font-size: 13px; }
.info-notice p { margin: 3px 0 0; color: #aabdb9; font-size: 12px; }
.info-icon, .dialog-icon { display: grid; flex: 0 0 auto; place-items: center; color: var(--accent-ink); background: var(--accent); border-radius: 50%; font-family: var(--font-mono); font-weight: 900; }
.info-icon { width: 24px; height: 24px; font-size: 13px; }
.dialog-icon { color: #fff; background: #a9343b; }
.danger-notice { display: flex; gap: 12px; margin: 12px 22px 0; padding: 13px 14px; border: 1px solid rgba(255, 107, 107, 0.36); border-radius: var(--radius); background: var(--danger-soft); }
.danger-notice strong { color: #ffb5b5; font-size: 13px; }
.danger-notice p { margin: 3px 0 0; color: #dca9a9; font-size: 12px; }
.danger-icon { display: grid; flex: 0 0 auto; width: 24px; height: 24px; place-items: center; color: #fff; background: #a9343b; border-radius: 50%; font-family: var(--font-mono); font-size: 13px; font-weight: 900; }
code { padding: 1px 4px; border-radius: 4px; color: #ffd2d2; background: rgba(255, 255, 255, 0.07); font-family: var(--font-mono); }

#config-form { padding: 2px 22px 22px; }
fieldset { min-width: 0; margin: 22px 0 0; padding: 0 0 22px; border: 0; border-bottom: 1px solid var(--border-soft); }
legend { margin-bottom: 14px; padding: 0; font-size: 13px; font-weight: 800; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.form-grid--three { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.field { display: grid; gap: 6px; min-width: 0; color: #cdd8e0; font-size: 12px; font-weight: 700; }
.field--wide { grid-column: span 2; }
.field--full-row { grid-column: 1 / -1; }
.field-switch { width: 100%; min-height: 52px; color: var(--text); font-size: 13px; }
.field small { color: var(--faint); font-size: 11px; font-weight: 500; }

input, textarea {
  width: 100%;
  min-height: 40px;
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  background: var(--surface-input);
  caret-color: var(--accent);
}

textarea { resize: vertical; }
input::placeholder, textarea::placeholder { color: #536674; }
input:hover, textarea:hover { border-color: #3b4c5b; }
input:focus, textarea:focus { border-color: var(--accent-strong); outline: 2px solid rgba(88, 214, 177, 0.16); }
input[aria-invalid="true"], textarea[aria-invalid="true"] { border-color: var(--danger); }

.inline-actions { gap: 10px; margin-top: 12px; }
.test-result { color: var(--muted); font-size: 12px; }
.test-result--success { color: var(--success); }
.test-result--error { color: var(--danger); }

.switch-list { display: grid; gap: 10px; }
.switch-row { display: flex; align-items: center; justify-content: space-between; gap: 20px; padding: 12px 13px; border: 1px solid var(--border-soft); border-radius: var(--radius-sm); background: rgba(255, 255, 255, 0.015); }
.switch-row span { display: grid; gap: 2px; }
.switch-row strong { font-size: 13px; }
.switch-row small { color: var(--muted); font-weight: 400; }
.switch-row--safe { border-color: rgba(242, 186, 100, 0.22); }

input[role="switch"] {
  appearance: none;
  flex: 0 0 auto;
  width: 42px;
  min-height: 24px;
  padding: 2px;
  border-radius: 999px;
  background: #2a3742;
  cursor: pointer;
}

input[role="switch"]::before { content: ""; display: block; width: 18px; height: 18px; border-radius: 50%; background: #a8b4bd; transition: transform 150ms ease, background 150ms ease; }
input[role="switch"]:checked { border-color: var(--accent-strong); background: rgba(88, 214, 177, 0.24); }
input[role="switch"]:checked::before { background: var(--accent); transform: translateX(18px); }

.form-footer { justify-content: space-between; gap: 20px; padding-top: 20px; }
.form-footer p { margin-bottom: 0; color: var(--muted); font-size: 12px; }

.audit-panel { position: sticky; top: 94px; }
.audit-scroll { max-height: 720px; overflow: auto; border-radius: 0 0 var(--radius-lg) var(--radius-lg); }
.audit-list { margin: 0; padding: 0; list-style: none; }
.audit-entry { padding: 18px 22px; border-bottom: 1px solid var(--border-soft); overflow-wrap: anywhere; }
.audit-entry:last-child { border-bottom: 0; }
.audit-entry-header { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.audit-title { margin: 0; font-size: 14px; line-height: 1.5; }
.audit-time { margin-left: auto; color: var(--muted); font-size: 12px; font-family: var(--font-mono); }
.audit-summary { margin: 10px 0 0; font-size: 13px; }
.audit-details { margin: 8px 0 0; padding-left: 18px; color: var(--muted); font-size: 12px; }
.audit-details li + li { margin-top: 4px; }
.audit-torrents { display: grid; gap: 10px; margin: 12px 0 0; padding: 0; list-style: none; }
.audit-torrents > li { padding: 10px 12px; background: var(--surface-raised); border: 1px solid var(--border-soft); border-radius: var(--radius-sm); }
.audit-torrent-name { display: block; font-size: 13px; }
.audit-message { padding: 36px 18px; text-align: center; color: var(--muted); }
.live-label { border: 0; background: transparent; }
.live-label span { color: var(--success); }
.table-wrap { max-height: 720px; overflow: auto; border-radius: 0 0 var(--radius-lg) var(--radius-lg); }
table { width: 100%; border-collapse: collapse; font-size: 12px; }
th { position: sticky; z-index: 2; top: 0; padding: 10px 12px; color: var(--muted); background: #121b23; text-align: left; font-size: 10px; letter-spacing: 0.08em; text-transform: uppercase; }
td { max-width: 270px; padding: 12px; border-top: 1px solid var(--border-soft); vertical-align: top; overflow-wrap: anywhere; }
td:first-child { color: var(--muted); font-family: var(--font-mono); white-space: nowrap; }
.event-level { display: inline-block; padding: 2px 6px; border-radius: 4px; font-family: var(--font-mono); font-size: 10px; font-weight: 800; text-transform: uppercase; }
.event-level--info { color: var(--info); background: rgba(111, 183, 244, 0.1); }
.event-level--success { color: var(--success); background: rgba(98, 212, 156, 0.1); }
.event-level--warning { color: var(--warning); background: rgba(242, 186, 100, 0.1); }
.event-level--error, .event-level--danger { color: var(--danger); background: var(--danger-soft); }
.event-detail { color: var(--muted); white-space: pre-wrap; }
.table-message td { padding: 36px 18px; color: var(--muted); text-align: center; }

dialog { width: min(480px, calc(100% - 32px)); padding: 0; border: 1px solid #5f373a; border-radius: var(--radius-lg); color: var(--text); background: var(--surface-raised); box-shadow: 0 30px 100px rgba(0,0,0,.65); }
dialog::backdrop { background: rgba(2, 6, 9, 0.78); backdrop-filter: blur(4px); }
.dialog-card { padding: 26px; }
.dialog-icon { width: 38px; height: 38px; margin-bottom: 15px; }
.dialog-card h2 { margin-bottom: 9px; }
.dialog-card > p { color: var(--muted); }
.confirm-check { display: flex; align-items: flex-start; gap: 9px; margin: 18px 0; padding: 11px; border-radius: var(--radius-sm); background: var(--danger-soft); color: #f0c6c6; font-size: 12px; }
.confirm-check input { flex: 0 0 auto; width: 16px; min-height: 16px; margin-top: 2px; padding: 0; accent-color: var(--danger); }
.dialog-actions { justify-content: flex-end; gap: 9px; }

.toast-region { position: fixed; z-index: 80; right: 18px; bottom: 18px; display: grid; gap: 8px; width: min(390px, calc(100% - 36px)); }
.toast { padding: 12px 14px; border: 1px solid var(--border); border-radius: var(--radius-sm); color: var(--text); background: #1a2630; box-shadow: var(--shadow); font-size: 13px; animation: toast-in 180ms ease-out; }
.toast--error { border-color: rgba(255, 107, 107, .5); }
.toast--success { border-color: rgba(98, 212, 156, .45); }

@keyframes pulse { 50% { opacity: 0.35; } }
.backup-panel { margin-top: 24px; }
.backup-content { padding: 0 22px 22px; }
.backup-description, .backup-help { color: var(--muted); font-size: 13px; line-height: 1.7; }
.backup-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 18px; }
.backup-card { display: grid; align-content: start; gap: 14px; min-width: 0; padding: 20px; border: 1px solid var(--border-soft); border-radius: var(--radius-sm); background: var(--surface-raised); }
.backup-card h3, .backup-card p { margin: 0; }
.backup-card .button { justify-self: start; min-height: 44px; }
.backup-card input[type="file"] { width: 100%; min-width: 0; font-size: 13px; }
.backup-confirm { display: flex; align-items: flex-start; gap: 10px; font-size: 13px; }
.backup-confirm input { width: 18px; min-height: 18px; margin-top: 3px; flex-shrink: 0; accent-color: var(--danger); }
.backup-result { overflow-wrap: anywhere; margin-top: 18px; }
.backup-result--error { color: var(--danger); }
.backup-result--success { color: var(--success); }
.login-page { display: grid; align-items: center; min-height: 100vh; min-height: 100svh; }
.login-shell { width: min(440px, calc(100% - 36px)); margin: 36px auto; }
.login-brand { margin-bottom: 24px; }
.login-brand-name { margin: 0; font-size: 19px; font-weight: 750; letter-spacing: -0.02em; }
.login-panel { padding: 28px; }
.login-panel h1 { margin: 0 0 10px; font-size: 24px; line-height: 1.4; letter-spacing: -0.03em; }
.login-description, .login-note, .login-footer { color: var(--muted); font-size: 13px; }
.login-description { margin-bottom: 22px; }
#login-form { display: grid; gap: 18px; }
#login-form input { min-height: 46px; }
.password-control { position: relative; min-width: 0; }
.password-control input { width: 100%; padding-right: 72px; }
.password-visibility { position: absolute; top: 1px; right: 1px; bottom: 1px; min-width: 60px; border: 0; border-radius: 0 var(--radius-sm) var(--radius-sm) 0; background: transparent; color: var(--muted); cursor: pointer; font-size: 13px; }
.password-visibility:hover { color: var(--accent); }
.login-submit { width: 100%; min-height: 46px; justify-content: center; }
.login-message { margin: 0 0 18px; padding: 12px; border-radius: var(--radius-sm); border: 1px solid rgba(255, 107, 107, 0.35); background: var(--danger-soft); color: #ffb6b6; font-size: 13px; overflow-wrap: anywhere; }
.login-note { margin: 20px 0 0; font-size: 12px; line-height: 1.7; }
.login-footer { margin: 18px 0 0; text-align: center; font-size: 12px; }
.logout-button { min-height: 44px; }
@media (max-width: 720px) {
  .login-panel { padding: 22px; }
  .service-controls .logout-button { grid-column: 1 / -1; }
}
#backup-previous { margin-top: 18px; min-height: 44px; }
@media (max-width: 720px) {
  .backup-grid { grid-template-columns: 1fr; }
  .backup-content { padding: 0 16px 16px; }
  .backup-card { padding: 16px; }
  .backup-card .button, #backup-previous { width: 100%; }
}
@keyframes shimmer { to { background-position: -240% 0; } }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

@media (max-width: 1120px) {
  .metrics { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .metric-card--wide { grid-column: span 2; }
  .workspace { grid-template-columns: 1fr; }
  .audit-panel { position: static; }
  .audit-scroll { max-height: 520px; }
  .table-wrap { max-height: 520px; }
}

@media (max-width: 720px) {
  .topbar { position: static; align-items: flex-start; flex-direction: column; padding: 14px 18px; }
  .service-controls { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); width: 100%; }
  .service-controls .button { width: 100%; min-width: 0; }
  .mode-badge, .state-pill { justify-content: center; min-width: 0; }
  .shell { padding-top: 24px; }
  .metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .metric-card, .metric-card--wide { grid-column: span 1; }
  .metric-card--wide { grid-column: 1 / -1; }
  .form-grid, .form-grid--three { grid-template-columns: 1fr; }
  .field--wide { grid-column: span 1; }
  .panel-header, #config-form { padding-left: 16px; padding-right: 16px; }
  .audit-entry { padding: 16px; }
  .audit-time { width: 100%; margin-left: 0; }
  .info-notice, .danger-notice { margin-left: 16px; margin-right: 16px; }
  .form-footer { align-items: stretch; flex-direction: column; }
  .form-footer .button { width: 100%; }
  .table-wrap { overflow-x: auto; }
  table { min-width: 620px; }
}

@media (max-width: 430px) {
  .mode-badge, .state-pill { flex: 1; justify-content: center; }
  .metrics { grid-template-columns: 1fr; }
  .metric-card, .metric-card--wide { grid-column: span 1; min-height: 116px; }
  .switch-row { align-items: flex-start; }
}

@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; }
}
