:root {
  --bg: #0b0f14;
  --bg-soft: #10151d;
  --panel: #151b23;
  --panel-2: #1b232e;
  --panel-3: #111820;
  --field: #17202a;
  --border: #293442;
  --border-strong: #3b82f6;
  --text: #e7edf5;
  --muted: #92a0ae;
  --muted-2: #687684;
  --accent: #4c8dff;
  --accent-strong: #2f6fe8;
  --accent-soft: rgba(76, 141, 255, .14);
  --accent-2: #28b06f;
  --warn: #d29a2a;
  --err: #f05252;
  --shadow: 0 18px 44px rgba(0, 0, 0, .26);
  --radius: 8px;
}

* { box-sizing: border-box; }

html { background: var(--bg); }

body {
  margin: 0;
  min-height: 100vh;
  font-family: "Segoe UI", system-ui, -apple-system, "Microsoft YaHei", sans-serif;
  background:
    linear-gradient(180deg, rgba(76, 141, 255, .05), transparent 220px),
    repeating-linear-gradient(90deg, rgba(255, 255, 255, .018) 0, rgba(255, 255, 255, .018) 1px, transparent 1px, transparent 96px),
    var(--bg);
  color: var(--text);
  font-size: 14px;
}

@keyframes selectMenuIn {
  from { opacity: 0; transform: translateY(-6px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes toastIn {
  from { opacity: 0; transform: translateY(-8px); }
  to { opacity: 1; transform: translateY(0); }
}

.topbar {
  display: grid;
  grid-template-columns: minmax(270px, 380px) minmax(0, 1fr);
  align-items: center;
  gap: 18px;
  padding: 16px 28px;
  background: rgba(16, 21, 29, .94);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
  backdrop-filter: blur(14px);
}

.brand {
  display: flex;
  align-items: center;
  gap: 14px;
  min-width: 0;
}

.brand h1 {
  font-size: 17px;
  margin: 0;
  font-weight: 700;
  letter-spacing: 0;
  white-space: nowrap;
}

.dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 10px rgba(76, 141, 255, .58);
}

.navtabs {
  display: inline-flex;
  gap: 4px;
  padding: 3px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--panel-3);
}

.navtab {
  border: 0;
  background: transparent;
  padding: 7px 13px;
  border-radius: 6px;
  color: var(--muted);
}

.navtab.active {
  background: var(--accent);
  color: #fff;
  box-shadow: 0 8px 18px rgba(76, 141, 255, .22);
}

.connection-card {
  display: grid;
  grid-template-columns: minmax(280px, 520px) auto;
  align-items: end;
  justify-content: end;
  gap: 12px;
  min-width: 0;
  padding: 0;
  border: 0;
  background: transparent;
}

.connection-fields {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
  min-width: 0;
}

.field-shell {
  display: grid;
  gap: 6px;
  min-width: 0;
  margin: 0;
  color: var(--muted);
}

.field-shell span {
  font-size: 11px;
  line-height: 1;
  color: var(--muted);
}

.admin-entry-hidden {
  display: none !important;
}

.connection-actions {
  display: grid;
  grid-template-columns: auto auto auto;
  align-items: center;
  justify-content: end;
  gap: 8px;
  white-space: nowrap;
}

.keystate {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 92px;
  height: 34px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, .03);
  font-size: 12px;
  color: var(--muted);
}

.keystate.ok {
  color: var(--accent-2);
  border-color: rgba(40, 176, 111, .25);
  background: rgba(40, 176, 111, .08);
}

button,
.doclink {
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  padding: 8px 14px;
  border-radius: var(--radius);
  font-size: 13px;
  line-height: 1.2;
  text-decoration: none;
  white-space: nowrap;
  transition: border-color .16s ease, color .16s ease, background .16s ease, filter .16s ease, box-shadow .16s ease;
}

button:hover,
.doclink:hover {
  border-color: var(--accent);
}

button:disabled {
  cursor: not-allowed;
  opacity: .5;
}

button.primary {
  background: linear-gradient(180deg, #5594ff, var(--accent-strong));
  border-color: var(--accent);
  color: #fff;
  font-weight: 700;
  width: 100%;
  padding: 11px;
  margin-top: 14px;
  box-shadow: 0 10px 22px rgba(76, 141, 255, .18);
}

button.primary:hover {
  filter: brightness(1.05);
  box-shadow: 0 12px 26px rgba(76, 141, 255, .28);
}

button.danger {
  color: var(--err);
  border-color: rgba(240, 82, 82, .36);
}

button.danger:hover {
  background: rgba(240, 82, 82, .12);
  border-color: var(--err);
}

.save-settings {
  color: #dbe7ff;
  border-color: rgba(76, 141, 255, .3);
  background: rgba(76, 141, 255, .12);
}

.doclink,
.doclink:visited {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 34px;
  font-weight: 700;
  color: #fff;
  background: var(--accent);
  border-color: var(--accent);
}

.view { display: none; }

.view.active {
  display: grid;
}

.layout,
.upstream-layout {
  width: min(100%, 1760px);
  margin: 0 auto;
  grid-template-columns: minmax(380px, 460px) minmax(0, 1fr);
  gap: 24px;
  padding: 24px 28px;
  align-items: start;
}

.panel {
  background: linear-gradient(180deg, rgba(255, 255, 255, .025), transparent 130px), var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: 0 1px 0 rgba(255, 255, 255, .035), var(--shadow);
}

.panel h2 {
  font-size: 15px;
  margin: 0 0 14px;
  font-weight: 700;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.panel-head h2 { margin: 0; }

.tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  margin-bottom: 14px;
  padding: 4px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--panel-3);
}

.tab {
  border: 0;
  background: transparent;
  color: var(--muted);
}

.tab.active {
  background: var(--accent);
  color: #fff;
  box-shadow: 0 8px 18px rgba(76, 141, 255, .18);
}

.tabpane { display: none; }

.tabpane.active {
  display: block;
}

label {
  display: block;
  font-size: 12px;
  color: var(--muted);
  margin: 10px 0 5px;
}

textarea,
select,
input[type="text"],
input[type="url"],
input[type="password"],
input[type="number"],
input:not([type]) {
  width: 100%;
  min-width: 0;
  background: var(--field);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
  font-size: 13px;
  outline: none;
  transition: border-color .16s ease, box-shadow .16s ease, background .16s ease;
}

textarea:hover,
select:hover,
input[type="text"]:hover,
input[type="url"]:hover,
input[type="password"]:hover,
input[type="number"]:hover,
input:not([type]):hover {
  border-color: rgba(76, 141, 255, .55);
  background: #1a2531;
}

textarea:focus,
select:focus,
input[type="text"]:focus,
input[type="url"]:focus,
input[type="password"]:focus,
input[type="number"]:focus,
input:not([type]):focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(76, 141, 255, .16);
}

select {
  appearance: none;
  padding-right: 34px;
  background-image:
    linear-gradient(45deg, transparent 50%, var(--muted) 50%),
    linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position:
    calc(100% - 17px) 50%,
    calc(100% - 11px) 50%;
  background-size: 6px 6px, 6px 6px;
  background-repeat: no-repeat;
}

select:focus {
  background-image:
    linear-gradient(45deg, var(--accent) 50%, transparent 50%),
    linear-gradient(135deg, transparent 50%, var(--accent) 50%);
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin: 0;
}

.field span {
  font-size: 12px;
  color: var(--muted);
}

.custom-select {
  position: relative;
  width: 100%;
  min-width: 0;
}

.custom-select .enhanced-native {
  position: absolute;
  inset: 0;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.select-trigger {
  position: relative;
  width: 100%;
  min-height: 43px;
  padding: 10px 34px 10px 12px;
  text-align: left;
  background: var(--field);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
  font-size: 13px;
  font-weight: 700;
}

.select-trigger::after {
  content: "";
  position: absolute;
  right: 14px;
  top: 50%;
  width: 0;
  height: 0;
  border-left: 6px solid transparent;
  border-right: 6px solid transparent;
  border-top: 7px solid var(--muted);
  transform: translateY(-35%);
}

.select-trigger:hover,
.select-trigger:focus {
  border-color: rgba(76, 141, 255, .7);
  background: #1a2531;
  outline: none;
}

.custom-select.open .select-trigger {
  border-color: var(--accent);
}

.select-menu {
  display: none;
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  right: 0;
  z-index: 40;
  max-height: 220px;
  overflow-y: auto;
  padding: 6px;
  border: 1px solid rgba(76, 141, 255, .34);
  border-radius: var(--radius);
  background: #111820;
  box-shadow: 0 18px 34px rgba(0, 0, 0, .36);
}

.custom-select.open .select-menu {
  display: grid;
  gap: 4px;
  animation: selectMenuIn .14s ease-out;
}

.select-option {
  width: 100%;
  padding: 8px 10px;
  border: 0;
  background: transparent;
  color: var(--text);
  text-align: left;
  font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
}

.select-option:hover,
.select-option[aria-selected="true"] {
  background: rgba(76, 141, 255, .14);
  color: #fff;
}

.gen-fields,
.edit-fields {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.gen-fields .full,
.edit-fields .full { grid-column: 1 / -1; }

.hidden { display: none !important; }

.custom-size {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.custom-size.revealed {
  display: grid;
}

textarea#genPrompt {
  min-height: 128px;
  resize: vertical;
}

textarea#editPrompt {
  min-height: 82px;
  resize: vertical;
}

.row {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 10px;
  align-items: end;
}

.row.two {
  grid-template-columns: 1fr auto auto;
  align-items: center;
}

.switchline {
  display: flex;
  align-items: center;
  gap: 7px;
  margin-top: 24px;
  white-space: nowrap;
}

.msg {
  font-size: 13px;
  min-height: 18px;
  margin: 10px 0 0;
}

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

.list-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 14px;
}

.controls {
  display: flex;
  gap: 8px;
  align-items: center;
  flex: 0 0 auto;
}

.controls select {
  min-width: 120px;
  padding: 8px 30px 8px 10px;
}

.controls .custom-select {
  min-width: 120px;
}

.controls .select-trigger {
  min-height: 34px;
  padding: 8px 32px 8px 10px;
  font-family: "Segoe UI", system-ui, -apple-system, "Microsoft YaHei", sans-serif;
  font-weight: 600;
}

.auto {
  font-size: 12px;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 0;
  white-space: nowrap;
}

.tasks {
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-height: 70vh;
  overflow-y: auto;
  padding-right: 2px;
}

.task {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 14px;
  align-items: center;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  cursor: pointer;
  transition: border-color .16s ease, background .16s ease, box-shadow .16s ease;
}

.task:hover {
  border-color: var(--accent);
  background: #1f2a37;
  box-shadow: 0 10px 22px rgba(0, 0, 0, .18);
}

.task .meta { min-width: 0; }

.task .tid {
  font-family: ui-monospace, monospace;
  font-size: 12px;
  color: var(--muted);
}

.task .sub {
  font-size: 12px;
  color: var(--muted);
  margin-top: 3px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.task .thumb {
  width: 50px;
  height: 50px;
  border-radius: 6px;
  object-fit: cover;
  background: var(--bg);
  border: 1px solid var(--border);
}

.badge,
.pill {
  font-size: 11px;
  padding: 3px 9px;
  border-radius: 999px;
  font-weight: 700;
  white-space: nowrap;
}

.badge.pending { background: rgba(146, 160, 174, .14); color: var(--muted); }
.badge.running { background: rgba(76, 141, 255, .16); color: #83b0ff; }
.badge.succeeded { background: rgba(40, 176, 111, .16); color: var(--accent-2); }
.badge.failed { background: rgba(240, 82, 82, .16); color: var(--err); }
.pill.default { background: rgba(210, 154, 42, .16); color: var(--warn); }
.pill.enabled { background: rgba(40, 176, 111, .16); color: var(--accent-2); }
.pill.disabled { background: rgba(146, 160, 174, .14); color: var(--muted); }

.empty {
  color: var(--muted);
  text-align: center;
  padding: 44px 0;
}

.upstream-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-height: 72vh;
  overflow-y: auto;
}

.upstream-card {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 14px;
  align-items: center;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  transition: border-color .16s ease, background .16s ease;
}

.upstream-card:hover {
  border-color: rgba(76, 141, 255, .6);
  background: #1f2a37;
}

.upstream-title {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 7px;
}

.upstream-title strong { font-size: 14px; }

.upstream-url {
  font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
  font-size: 12px;
  color: var(--text);
  word-break: break-all;
}

.upstream-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 8px;
  color: var(--muted);
  font-size: 12px;
}

.upstream-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: flex-end;
  max-width: 320px;
}

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .62);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  backdrop-filter: blur(4px);
}

.modal.hidden { display: none; }

.modal-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 22px;
  width: min(860px, 92vw);
  max-height: 88vh;
  overflow-y: auto;
  position: relative;
  box-shadow: var(--shadow);
}

.modal-close {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 32px;
  height: 32px;
  padding: 0;
  font-size: 18px;
  line-height: 1;
}

.modal-card h3 {
  margin: 0 0 14px;
  font-size: 15px;
}

.kv {
  display: grid;
  grid-template-columns: 130px 1fr;
  gap: 6px 12px;
  font-size: 13px;
  margin-bottom: 14px;
}

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

.kv .v {
  font-family: ui-monospace, monospace;
  word-break: break-all;
}

.imgs {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 12px;
}

.imgs a img {
  width: 100%;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  display: block;
}

.errbox {
  background: rgba(240, 82, 82, .1);
  border: 1px solid rgba(240, 82, 82, .3);
  color: var(--err);
  padding: 10px 12px;
  border-radius: var(--radius);
  font-family: ui-monospace, monospace;
  font-size: 12px;
  white-space: pre-wrap;
  word-break: break-all;
}

pre.summary {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  font-size: 12px;
  overflow-x: auto;
}

.progress {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 7px;
}

.progress .track {
  flex: 1;
  height: 6px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  overflow: hidden;
}

.progress .bar {
  height: 100%;
  width: 0;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  transition: width .9s linear;
}

.progress .pct {
  font-size: 11px;
  color: var(--accent);
  min-width: 32px;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.imgcard {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.dlbtn {
  width: 100%;
  padding: 6px 8px;
  font-size: 12px;
}

.dlbtn:hover {
  border-color: var(--accent-2);
  color: var(--accent-2);
}

.refgrid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(84px, 1fr));
  gap: 8px;
  margin-top: 4px;
}

.reftile {
  position: relative;
  aspect-ratio: 3 / 4;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--bg);
}

.reftile img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.reflabel {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 3px 6px;
  font-size: 11px;
  color: #fff;
  background: rgba(0, 0, 0, .55);
  text-align: center;
}

.refdel {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 20px;
  height: 20px;
  padding: 0;
  font-size: 13px;
  line-height: 1;
  border-radius: 50%;
  background: rgba(0, 0, 0, .6);
  border-color: transparent;
  color: #fff;
  display: none;
}

.reftile:hover .refdel { display: block; }
.refdel:hover { background: var(--err); border-color: var(--err); }

.addtile {
  aspect-ratio: 3 / 4;
  border: 1.5px dashed var(--border);
  border-radius: var(--radius);
  background: transparent;
  color: var(--muted);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  font-size: 12px;
}

.addtile:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.addtile .plus {
  font-size: 22px;
  line-height: 1;
  color: var(--accent);
}

.refbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 8px;
  padding: 8px 10px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 12px;
  color: var(--muted);
}

.refbar b { color: var(--accent); }

.clearbtn {
  padding: 4px 10px;
  font-size: 12px;
  color: var(--err);
  border-color: rgba(240, 82, 82, .4);
  background: transparent;
}

.clearbtn:hover {
  border-color: var(--err);
  background: rgba(240, 82, 82, .1);
}

.toast-host {
  position: fixed;
  top: 76px;
  right: 24px;
  z-index: 90;
  display: grid;
  gap: 10px;
  width: min(360px, calc(100vw - 28px));
  pointer-events: none;
}

.toast {
  display: grid;
  gap: 3px;
  padding: 11px 13px;
  border: 1px solid var(--border);
  border-left-width: 3px;
  border-radius: var(--radius);
  background: rgba(21, 27, 35, .96);
  box-shadow: 0 14px 34px rgba(0, 0, 0, .32);
  color: var(--text);
  font-size: 13px;
  line-height: 1.45;
  pointer-events: auto;
  animation: toastIn .16s ease-out;
}

.toast.ok {
  border-left-color: var(--accent-2);
}

.toast.err {
  border-left-color: var(--err);
}

.toast.info {
  border-left-color: var(--accent);
}

.toast-title {
  font-weight: 700;
}

.toast-text {
  color: var(--muted);
  word-break: break-word;
}

@media (max-width: 1360px) {
  .topbar {
    grid-template-columns: 1fr;
    align-items: stretch;
  }

  .connection-card {
    grid-template-columns: minmax(280px, 520px) auto;
  }

  .connection-actions {
    justify-content: end;
  }
}

@media (max-width: 1100px) {
  .layout,
  .upstream-layout {
    grid-template-columns: 1fr;
  }

  .connection-fields {
    grid-template-columns: 1fr;
  }

  .service-slot {
    grid-column: 1 / -1;
  }
}

@media (max-width: 720px) {
  .topbar {
    padding: 14px;
  }

  .brand {
    align-items: flex-start;
    flex-direction: column;
    gap: 10px;
  }

  .navtabs,
  .tabs {
    width: 100%;
  }

  .navtab { flex: 1; }

  .connection-fields,
  .connection-actions,
  .connection-card,
  .layout,
  .upstream-layout,
  .row,
  .row.two,
  .gen-fields,
  .edit-fields,
  .custom-size,
  .upstream-card {
    grid-template-columns: 1fr;
  }

  .layout,
  .upstream-layout {
    padding: 16px 14px;
  }

  .connection-actions {
    justify-content: stretch;
  }

  .doclink,
  .save-settings,
  .keystate {
    width: 100%;
  }

  .switchline { margin-top: 8px; }

  .list-head {
    align-items: flex-start;
    flex-direction: column;
  }

  .controls {
    flex-wrap: wrap;
  }

  .toast-host {
    top: 12px;
    right: 14px;
  }

  .upstream-actions {
    justify-content: flex-start;
    max-width: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    scroll-behavior: auto !important;
    transition-duration: .01ms !important;
  }
}
