:root {
  --bg: #0b0e14;
  --bg-soft: #11151f;
  --card: #151a26;
  --card-2: #1a2030;
  --border: #242c3f;
  --border-soft: #1d2434;
  --text: #e6e9f0;
  --muted: #8b93a7;
  --accent: #4f8cff;
  --accent-soft: rgba(79, 140, 255, 0.14);
  --green: #34d399;
  --red: #f87171;
  --amber: #fbbf24;
  --radius: 10px;
  --mono: ui-monospace, "SF Mono", "Cascadia Code", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

h1 { font-size: 16px; margin: 0; }
h2 { font-size: 18px; margin: 0; }
h3 { font-size: 14px; margin: 0; }
p { margin: 4px 0; }
code {
  font-family: var(--mono);
  font-size: 12px;
  background: var(--bg-soft);
  padding: 1px 5px;
  border-radius: 4px;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.spacer { flex: 1; }
.right { text-align: right; }
.num { text-align: right; }

/* ---------- layout ---------- */

.app {
  display: grid;
  grid-template-columns: 240px 1fr;
  height: 100vh;
}

.sidebar {
  background: var(--bg-soft);
  border-right: 1px solid var(--border-soft);
  display: flex;
  flex-direction: column;
  padding: 16px 12px;
  gap: 14px;
}

.brand { display: flex; gap: 10px; align-items: center; padding: 2px 6px; }
.brand-mark { font-size: 24px; }
.brand p { margin: 0; color: var(--muted); font-size: 11px; letter-spacing: 0.06em; text-transform: uppercase; }

.instance-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.inst-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 10px;
  border-radius: 8px;
  cursor: pointer;
  border: 1px solid transparent;
  user-select: none;
}
.inst-item:hover { background: var(--card); }
.inst-item.active { background: var(--card-2); border-color: var(--border); }
.inst-item .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.inst-item .sub { font-size: 11px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 120px; }

.sidebar-foot { display: flex; flex-direction: column; gap: 8px; }
.conn-status { font-size: 11px; color: var(--muted); text-align: center; }

.main {
  overflow-y: auto;
  padding: 20px 24px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.card {
  background: var(--card);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  padding: 16px;
}

.section-title { margin-bottom: 10px; font-weight: 600; letter-spacing: 0.02em; }
.section-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.section-tools { display: flex; gap: 6px; align-items: center; }

/* ---------- status pills ---------- */

.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  font-weight: 600;
  padding: 3px 9px;
  border-radius: 99px;
  background: var(--card-2);
  border: 1px solid var(--border);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.pill .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); }
.pill.online .dot { background: var(--green); box-shadow: 0 0 6px var(--green); }
.pill.error .dot, .pill.failed .dot { background: var(--red); }
.pill.downloading .dot, .pill.queued .dot { background: var(--accent); }
.pill.downloading .dot { animation: pulse 1.2s infinite; }
.pill.done .dot { background: var(--green); }
.pill.cancelled .dot, .pill.interrupted .dot { background: var(--muted); }
.pill.uploading .dot { background: var(--amber); animation: pulse 1.2s infinite; }

@keyframes pulse { 50% { opacity: 0.35; } }

/* ---------- instance header ---------- */

.inst-head { display: flex; justify-content: space-between; gap: 16px; align-items: center; flex-wrap: wrap; }
.inst-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ---------- forms ---------- */

label { display: flex; flex-direction: column; gap: 5px; font-size: 13px; }
label > span { color: var(--muted); font-size: 12px; }
label em { font-style: normal; }
.field { display: flex; flex-direction: column; gap: 5px; }

input, select, textarea {
  background: var(--bg-soft);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 8px 10px;
  font: inherit;
  width: 100%;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
textarea { font-family: var(--mono); font-size: 12px; resize: vertical; }

.row { display: flex; gap: 10px; align-items: flex-end; }
.row > * { flex: 1; }
.row .grow { flex: 2; }
.job-form { display: flex; flex-direction: column; gap: 10px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 10px; }
#instance-form { display: flex; flex-direction: column; gap: 10px; }
#instance-form label.grow { width: 100%; }
.path-picker { display: flex; align-items: stretch; gap: 8px; }
.path-picker input { flex: 1; min-width: 0; }
.dest-hint { margin: 2px 0 6px; }
#ssh-fields, #pc-fields { display: flex; flex-direction: column; gap: 10px; margin-bottom: 4px; padding: 12px; border: 1px dashed var(--border); border-radius: 10px; }
#pc-fields > label { width: 100%; }
.pc-bridge { border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; background: var(--bg, rgba(255,255,255,0.02)); }
.pc-bridge-head { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.pc-cmd-row { display: flex; align-items: stretch; gap: 8px; margin-top: 8px; }
.pc-cmd {
  flex: 1; min-width: 0; overflow-x: auto; white-space: nowrap;
  font-size: 12px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--card-2);
}

.btn {
  background: var(--card-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 14px;
  font: inherit;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s;
  white-space: nowrap;
}
.btn:hover { background: var(--border); }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: #3d7bf0; }
.btn.ghost { background: transparent; }
.btn.small { padding: 4px 9px; font-size: 12px; border-radius: 6px; }
.btn.danger { color: var(--red); }
.btn.danger:hover { background: rgba(248, 113, 113, 0.12); border-color: rgba(248, 113, 113, 0.4); }
.btn.full { width: 100%; }

/* ---------- jobs ---------- */

.job-list { display: flex; flex-direction: column; gap: 8px; }
.job {
  border: 1px solid var(--border-soft);
  border-radius: 8px;
  background: var(--bg-soft);
  overflow: hidden;
}
.job-head {
  display: grid;
  grid-template-columns: 130px 1fr 130px auto;
  gap: 12px;
  align-items: center;
  padding: 10px 14px;
  cursor: default;
}
.job-label { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.job-source { font-family: var(--mono); font-size: 11px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.job-stats { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--muted); justify-content: flex-end; }
.job-stats b { color: var(--text); font-variant-numeric: tabular-nums; }

.job-actions { display: flex; gap: 4px; }

.bar {
  grid-column: 1 / -1;
  height: 4px;
  background: var(--border-soft);
  border-radius: 2px;
  overflow: hidden;
}
.bar > div {
  height: 100%;
  width: 0%;
  background: var(--accent);
  border-radius: 2px;
  transition: width 0.3s ease;
}
.job.done .bar > div { background: var(--green); }
.job.failed .bar > div { background: var(--red); }
.job.cancelled .bar > div, .job.interrupted .bar > div { background: var(--muted); }

.job-log {
  display: none;
  background: var(--bg);
  border-top: 1px solid var(--border-soft);
  padding: 10px 14px;
  font-family: var(--mono);
  font-size: 11px;
  color: var(--muted);
  max-height: 220px;
  overflow-y: auto;
  white-space: pre-wrap;
  word-break: break-all;
}
.job.open .job-log { display: block; }
.job-error { color: var(--red); }

/* ---------- browser ---------- */

.browser-path { display: flex; gap: 8px; align-items: center; margin-bottom: 10px; }
.browser-path input { flex: 1; font-family: var(--mono); font-size: 12px; }

.browser-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.browser-table th {
  text-align: left;
  color: var(--muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 6px 8px;
  border-bottom: 1px solid var(--border-soft);
}
.browser-table td { padding: 7px 8px; border-bottom: 1px solid var(--border-soft); }
.browser-table tr:hover td { background: var(--bg-soft); }
.browser-table .name-cell { font-weight: 500; cursor: pointer; display: flex; align-items: center; gap: 7px; }
.browser-table .name-cell .ico { opacity: 0.8; }
.browser-table .mono { font-family: var(--mono); font-size: 11px; color: var(--muted); }
.browser-empty { text-align: center; color: var(--muted); padding: 24px; }

/* ---------- modals ---------- */

.modal-root {
  position: fixed;
  inset: 0;
  background: rgba(4, 6, 10, 0.7);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 8vh 16px 16px;
  z-index: 50;
  backdrop-filter: blur(3px);
}
.modal {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  width: 560px;
  max-width: 100%;
  max-height: 84vh;
  overflow-y: auto;
  padding: 18px;
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.5);
}
.modal.narrow { width: 440px; }
.modal.folder-modal { width: 680px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; }
.modal-head p { margin: 3px 0 0; }
.folder-toolbar { display: flex; align-items: center; gap: 7px; margin-bottom: 10px; }
.folder-toolbar input { flex: 1; min-width: 0; font-family: var(--mono); font-size: 12px; }
.folder-list {
  min-height: 220px;
  max-height: 46vh;
  overflow-y: auto;
  border: 1px solid var(--border-soft);
  border-radius: 9px;
  background: var(--bg-soft);
}
.folder-item {
  display: flex;
  align-items: center;
  gap: 9px;
  width: 100%;
  padding: 10px 12px;
  border: 0;
  border-bottom: 1px solid var(--border-soft);
  background: transparent;
  color: var(--text);
  text-align: left;
  font: inherit;
  cursor: pointer;
}
.folder-item:last-child { border-bottom: 0; }
.folder-item:hover, .folder-item:focus-visible { background: var(--card-2); outline: none; }
.folder-icon { opacity: 0.9; }
.folder-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.folder-badge { color: var(--muted); font-size: 10px; text-transform: uppercase; letter-spacing: 0.06em; }
.folder-selection { max-width: 58%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-family: var(--mono); }
.modal-x {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 15px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 6px;
}
.modal-x:hover { background: var(--card-2); color: var(--text); }
.modal-foot { display: flex; gap: 8px; align-items: center; margin-top: 14px; }

.test-result {
  border-radius: 8px;
  padding: 10px 12px;
  font-family: var(--mono);
  font-size: 12px;
  white-space: pre-wrap;
  word-break: break-all;
  margin-top: 4px;
}
.test-result.ok { background: rgba(52, 211, 153, 0.1); border: 1px solid rgba(52, 211, 153, 0.35); color: var(--green); }
.test-result.err { background: rgba(248, 113, 113, 0.1); border: 1px solid rgba(248, 113, 113, 0.35); color: var(--red); }

/* ---------- toasts ---------- */

.toasts {
  position: fixed;
  bottom: 18px;
  right: 18px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 100;
}
.toast {
  background: var(--card-2);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: 8px;
  padding: 10px 14px;
  max-width: 380px;
  font-size: 13px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.4);
  animation: slidein 0.18s ease;
}
.toast.err { border-left-color: var(--red); }
.toast.ok { border-left-color: var(--green); }
@keyframes slidein { from { transform: translateY(8px); opacity: 0; } }

/* ---------- gate ---------- */

.gate {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
}
.gate-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 34px;
  width: 380px;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.gate-logo { font-size: 40px; }
.gate-card form { display: flex; flex-direction: column; gap: 10px; }

/* ---------- empty state ---------- */

.empty {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  text-align: center;
  color: var(--muted);
}
.empty h2 { color: var(--text); }
.empty-icon { font-size: 44px; }

@media (max-width: 800px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { flex-direction: row; align-items: center; }
  .instance-list { flex-direction: row; }
  .job-head { grid-template-columns: 1fr; }
  .folder-toolbar { flex-wrap: wrap; }
  .folder-toolbar input { order: -1; flex-basis: 100%; }
  .folder-selection { display: none; }
}
