* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  letter-spacing: 0;
}

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

button {
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: var(--surface-2);
  color: var(--text);
  cursor: pointer;
  min-height: 38px;
  padding: 8px 13px;
  transition: border-color .12s ease, background .12s ease, transform .12s ease;
  white-space: nowrap;
}

button:hover {
  border-color: var(--focus);
  background: var(--button-hover);
}

button:active {
  transform: translateY(1px);
}

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

button:disabled:hover {
  border-color: var(--line-strong);
  background: transparent;
}

button.primary {
  background: var(--primary);
  border-color: var(--primary-border);
  color: #ffffff;
  font-weight: 700;
}

button.primary:hover {
  background: var(--primary-hover);
}

button.ghost {
  background: transparent;
}

button.small {
  min-height: 32px;
  padding: 6px 10px;
  font-size: 12px;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface-3);
  color: var(--text);
  outline: none;
}

input,
select {
  min-height: 38px;
  padding: 8px 10px;
}

textarea {
  min-height: 340px;
  padding: 13px;
  resize: vertical;
  line-height: 1.55;
  font-family: var(--mono);
  font-size: 12px;
  word-break: break-all;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--focus);
  box-shadow: 0 0 0 3px var(--focus-ring);
}

label {
  display: block;
  color: var(--muted);
  font-size: 12px;
  margin-bottom: 6px;
}

code,
.mono {
  font-family: var(--mono);
}

.app {
  width: min(1480px, calc(100vw - 32px));
  margin: 0 auto;
  padding: 24px 0 42px;
}

@media (min-width: 1200px) {
  .app {
    width: min(1480px, calc(100vw - 144px));
  }
}

.topbar {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: flex-start;
  margin-bottom: 18px;
  border-bottom: 1px solid var(--line);
  padding-bottom: 18px;
}

.top-actions {
  display: grid;
  gap: 10px;
  justify-items: end;
}

h1 {
  margin: 0 0 7px;
  font-size: 22px;
  line-height: 1.25;
}

.lead {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.5;
}

.status-strip {
  display: grid;
  grid-template-columns: repeat(4, minmax(84px, 1fr));
  gap: 8px;
  min-width: 430px;
}

.theme-switch {
  display: inline-flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 4px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  padding: 4px;
  max-width: 560px;
}

.theme-switch button {
  min-height: 30px;
  border-radius: 999px;
  border-color: transparent;
  background: transparent;
  color: var(--muted);
  padding: 5px 11px;
  font-size: 12px;
  flex: 0 0 auto;
}

.theme-group-label {
  align-self: center;
  color: var(--muted);
  font-size: 11px;
  padding: 0 5px;
}

.theme-group-label:not(:first-child) {
  border-left: 1px solid var(--line);
  margin-left: 2px;
  padding-left: 9px;
}

.theme-switch button.active {
  background: var(--surface-2);
  border-color: var(--line-strong);
  color: var(--text);
}

.metric {
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--surface);
  padding: 9px 10px;
}

.metric b {
  display: block;
  font-size: 17px;
  line-height: 1.1;
}

.metric span {
  display: block;
  color: var(--muted);
  font-size: 11px;
  margin-top: 4px;
}

.layout {
  --top-panel-height: clamp(560px, calc(100vh - 158px), 780px);
  display: grid;
  grid-template-columns: minmax(0, 1.4fr) minmax(340px, .6fr);
  gap: 16px;
  align-items: stretch;
}

.panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.input-panel,
.settings-panel {
  height: var(--top-panel-height);
  min-height: 0;
}

.panel-head {
  min-height: 50px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 13px 14px;
  border-bottom: 1px solid var(--line);
  background: var(--panel-head);
  flex: 0 0 auto;
}

.panel-head h2,
.group-summary h2 {
  margin: 0;
  font-size: 14px;
  line-height: 1.3;
}

.panel-subtitle {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
}

.panel-title-block {
  min-width: 0;
}

.panel-actions {
  justify-content: flex-end;
  flex: 0 0 auto;
}

.panel-toolbar {
  display: flex;
  align-items: end;
  gap: 10px;
  flex-wrap: wrap;
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
  flex: 0 0 auto;
}

.panel-body {
  padding: 14px;
  min-height: 0;
}

.input-panel .panel-body {
  flex: 1;
  display: flex;
}

.settings-panel .panel-body {
  flex: 1;
  overflow: auto;
  scrollbar-gutter: stable;
}

#rawInput {
  flex: 1;
  min-height: 0;
  height: auto;
}

.button-row,
.compact-row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
}

.settings-grid {
  display: grid;
  gap: 13px;
}

.settings-tabs {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.settings-tab {
  min-height: 30px;
  padding: 5px 10px;
  font-size: 12px;
  background: transparent;
}

.settings-tab.active {
  border-color: var(--primary-border);
  background: var(--active-tab);
  color: var(--text);
  font-weight: 700;
}

.settings-tab-panel {
  display: grid;
  gap: 13px;
}

.settings-tab-panel[hidden] {
  display: none;
}

.settings-section {
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--surface-3);
  padding: 11px;
  display: grid;
  gap: 11px;
}

.settings-section-title {
  margin: 0;
  color: var(--soft);
  font-size: 13px;
  line-height: 1.3;
  font-weight: 700;
}

.two-cols {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.check-row {
  display: grid;
  grid-template-columns: 18px 1fr;
  gap: 8px;
  align-items: start;
  color: var(--soft);
  font-size: 13px;
  line-height: 1.45;
}

.check-row input {
  width: 16px;
  min-height: auto;
  margin: 3px 0 0;
}

.protocol-filter {
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--surface-3);
  padding: 11px;
}

.protocol-filter-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 9px;
}

.protocol-filter-head label {
  margin: 0;
}

.protocol-options {
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
}

.protocol-option {
  display: inline-grid;
  grid-template-columns: 15px auto;
  align-items: center;
  gap: 6px;
  min-height: 30px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface-2);
  padding: 5px 10px;
  color: var(--soft);
  font-size: 12px;
  cursor: pointer;
  user-select: none;
}

.protocol-option input {
  width: 14px;
  min-height: auto;
  margin: 0;
}

.forward-panel {
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--surface-3);
  padding: 11px;
}

.forward-panel summary {
  cursor: pointer;
  color: var(--soft);
  font-size: 13px;
}

.forward-grid {
  display: grid;
  grid-template-columns: 1.2fr .8fr;
  gap: 10px;
  margin-top: 11px;
}

.forward-full {
  grid-column: 1 / -1;
}

.template-presets {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-top: 7px;
}

.note {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.55;
  margin: 0;
}

.message {
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--surface-3);
  color: var(--muted);
  min-height: 40px;
  padding: 10px 11px;
  font-size: 12px;
  line-height: 1.45;
}

.message.ok {
  color: var(--green);
  border-color: rgba(92, 200, 120, .34);
}

.message.warn {
  color: var(--amber);
  border-color: rgba(228, 185, 92, .36);
}

.message.err {
  color: var(--red);
  border-color: rgba(255, 115, 115, .38);
}

.ignored-preview {
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--surface-3);
  padding: 10px 11px;
  font-size: 12px;
  color: var(--muted);
}

.ignored-preview summary {
  cursor: pointer;
  color: var(--soft);
}

#outputReport {
  margin-top: 10px;
  border-color: rgba(228, 185, 92, .48);
  background: var(--surface-2);
}

#outputReport summary {
  color: var(--amber);
  font-weight: 700;
}

.ignored-preview pre {
  max-height: 190px;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-all;
  margin: 10px 0 0;
  padding-top: 10px;
  border-top: 1px solid var(--line);
  font-family: var(--mono);
  line-height: 1.45;
}

.detail-cell {
  width: 58px;
  min-width: 58px;
}

.detail-cell .small {
  min-height: 28px;
  padding: 4px 8px;
  font-size: 11px;
}

.node-detail-row td {
  background: var(--detail-bg);
  border-top: 0;
}

.node-detail {
  margin: 0;
  color: var(--soft);
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.55;
  font-family: var(--mono);
  font-size: 12px;
}

.groups {
  display: grid;
  gap: 14px;
  margin-top: 16px;
}

.group-panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  overflow: hidden;
}

.group-head {
  display: grid;
  grid-template-columns: minmax(210px, .8fr) minmax(440px, 1.2fr) auto;
  gap: 14px;
  align-items: end;
  padding: 13px 14px;
  border-bottom: 1px solid var(--line);
  background: var(--panel-head);
}

.group-summary {
  min-width: 0;
}

.group-summary p {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
}

.group-fields {
  display: grid;
  grid-template-columns: repeat(2, minmax(220px, 1fr));
  gap: 10px;
  align-items: end;
  min-width: 0;
}

.group-fields input {
  min-width: 0;
  height: 38px;
  text-overflow: ellipsis;
}

.group-tools {
  justify-content: flex-end;
}

.badges {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}

.badge {
  display: inline-flex;
  align-items: center;
  min-height: 25px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface-2);
  color: var(--soft);
  padding: 4px 9px;
  font-size: 12px;
}

.badge.protocol {
  color: var(--badge-protocol-text);
  border-color: rgba(92, 200, 120, .34);
}

.table-wrap {
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 1420px;
}

th,
td {
  border-bottom: 1px solid var(--line);
  padding: 10px;
  text-align: left;
  vertical-align: middle;
  font-size: 12px;
}

th {
  color: var(--muted);
  font-weight: 600;
  background: var(--table-head);
  line-height: 1.25;
  white-space: nowrap;
}

tr:last-child td {
  border-bottom: 0;
}

td.host,
td.port,
td.name,
td.protocol,
td.link {
  font-family: var(--mono);
}

td.host,
td.port,
td.name {
  min-width: 0;
}

td.protocol {
  width: 105px;
}

td.port,
.forward-select-cell {
  width: 64px;
}

.forward-host-cell {
  width: 170px;
}

.forward-port-cell {
  width: 96px;
}

.final-name-cell,
.forward-name-cell {
  width: 220px;
}

.cell-clip {
  display: block;
  max-width: 180px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  word-break: normal;
}

td.host .cell-clip {
  max-width: 150px;
}

td.link {
  color: var(--muted);
  width: 120px;
  min-width: 120px;
}

.qr-link-btn {
  min-height: 28px;
  padding: 4px 8px;
  font-size: 11px;
}

.link-actions {
  display: flex;
  gap: 6px;
}

.name-input {
  min-width: 0;
  width: 220px;
  max-width: 220px;
  font-family: var(--sans);
}

[data-forward-host] {
  min-width: 150px;
}

[data-forward-port] {
  min-width: 80px;
}

.qr-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(260px, 340px);
  gap: 14px;
  align-items: start;
}

.qr-input-side {
  display: grid;
  gap: 10px;
}

#singleNodeInput {
  min-height: 150px;
}

.qr-output-side {
  display: grid;
  gap: 10px;
}

.qr-preview {
  position: relative;
  display: grid;
  place-items: center;
  width: 100%;
  min-height: 260px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--surface-3);
  padding: 12px;
}

.qr-preview canvas {
  display: block;
  width: min(100%, 320px);
  height: auto;
  image-rendering: pixelated;
  border: 1px solid #d8dde6;
  border-radius: 4px;
  background: #ffffff;
}

.qr-placeholder {
  position: absolute;
  inset: 12px;
  display: grid;
  place-items: center;
  border: 1px dashed var(--line-strong);
  border-radius: 6px;
  background: var(--surface-3);
  color: var(--muted);
  font-size: 12px;
}

.qr-preview.has-qr .qr-placeholder {
  display: none;
}

.qr-meta {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
  word-break: break-all;
}

.qr-message {
  min-height: 38px;
}

.output-panel {
  margin-top: 16px;
}

.output-panel-head {
  align-items: flex-start;
}

.tabs {
  display: flex;
  gap: 6px;
  align-items: center;
  flex-wrap: wrap;
}

.tabs button.active {
  border-color: var(--focus);
  color: var(--text);
  background: var(--active-tab);
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: center;
  background: rgba(0, 0, 0, .54);
  padding: 20px;
}

.modal-backdrop[hidden] {
  display: none;
}

.qr-modal {
  width: min(920px, calc(100vw - 24px));
  max-height: calc(100vh - 40px);
}

.qr-modal .panel-body {
  overflow: auto;
}

.side-qr-trigger {
  position: fixed;
  right: 18px;
  top: 50%;
  z-index: 90;
  transform: translateY(-50%);
  min-width: 40px;
  min-height: 126px;
  padding: 12px 9px;
  border-color: var(--primary-border);
  background: var(--surface);
  color: var(--soft);
  box-shadow: var(--shadow);
  writing-mode: vertical-rl;
  text-orientation: mixed;
  letter-spacing: 0;
}

.side-qr-trigger:hover {
  background: var(--surface-2);
  color: var(--text);
}

.side-qr-trigger:active {
  transform: translateY(-50%);
}

@media (max-width: 1199px) {
  .side-qr-trigger {
    right: 14px;
    bottom: 14px;
    top: auto;
    transform: none;
    min-width: 0;
    min-height: 34px;
    padding: 7px 10px;
    writing-mode: horizontal-tb;
  }

  .side-qr-trigger:active {
    transform: none;
  }
}

.output-format {
  width: 220px;
}

.output-field {
  display: grid;
  gap: 4px;
  min-width: 190px;
}

.output-field label {
  margin: 0;
}

.output-format:disabled {
  cursor: not-allowed;
  opacity: .55;
}

#output {
  min-height: 260px;
  max-height: 520px;
  overflow: auto;
  white-space: pre;
  word-break: break-all;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--output-bg);
  color: var(--output-text);
  padding: 13px;
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.55;
}

.empty-state {
  color: var(--muted);
  padding: 18px 0 4px;
  font-size: 13px;
}

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

  .layout {
    --top-panel-height: auto;
  }

  .input-panel,
  .settings-panel {
    height: auto;
  }

  .settings-panel .panel-body {
    overflow: visible;
  }

  #rawInput {
    min-height: 360px;
  }

  .top-actions {
    justify-items: stretch;
  }

  .theme-switch {
    justify-self: end;
    max-width: 100%;
  }

  .status-strip {
    min-width: 0;
  }

  .qr-grid {
    grid-template-columns: 1fr;
  }

  .group-head {
    grid-template-columns: 1fr;
    align-items: stretch;
  }

  .group-fields {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .group-tools {
    justify-content: flex-start;
  }
}

@media (max-width: 900px) {
  .table-wrap {
    overflow: visible;
  }

  table,
  thead,
  tbody,
  tr,
  th,
  td {
    display: block;
  }

  table {
    min-width: 0;
  }

  thead {
    display: none;
  }

  tbody {
    display: grid;
    gap: 10px;
    padding: 10px;
  }

  tr {
    border: 1px solid var(--line);
    border-radius: 7px;
    background: var(--surface-3);
    overflow: hidden;
  }

  td {
    display: grid;
    grid-template-columns: minmax(100px, 28%) minmax(0, 1fr);
    gap: 10px;
    align-items: center;
    border-bottom: 1px solid var(--line);
    padding: 9px 10px;
    min-width: 0;
  }

  .detail-cell,
  td.protocol,
  td.port,
  .forward-select-cell,
  .forward-host-cell,
  .forward-port-cell,
  .final-name-cell,
  .forward-name-cell {
    width: auto;
    min-width: 0;
  }

  td::before {
    color: var(--muted);
    font-family: var(--sans);
    font-size: 11px;
    line-height: 1.25;
  }

  td:nth-child(1)::before { content: "详情"; }
  td:nth-child(2)::before { content: "协议"; }
  td:nth-child(3)::before { content: "入口"; }
  td:nth-child(4)::before { content: "端口"; }
  td:nth-child(5)::before { content: "原名称"; }
  td:nth-child(6)::before { content: "推荐名称"; }
  td:nth-child(7)::before { content: "最终名称"; }
  td:nth-child(8)::before { content: "转发"; }
  td:nth-child(9)::before { content: "转发入口"; }
  td:nth-child(10)::before { content: "转发端口"; }
  td:nth-child(11)::before { content: "转发名称"; }
  td:nth-child(12)::before { content: "二维码"; }

  .node-detail-row {
    border: 0;
    background: transparent;
  }

  .node-detail-row td {
    display: block;
    padding: 0;
    border: 0;
  }

  .node-detail-row td::before {
    content: none;
  }

  td.link {
    max-width: none;
    min-width: 0;
    width: auto;
  }

  .name-input {
    width: 100%;
    max-width: none;
  }

  .link-actions {
    flex-wrap: wrap;
  }
}

@media (max-width: 720px) {
  .app {
    width: min(100vw - 20px, 1480px);
    padding-top: 12px;
  }

  .status-strip,
  .two-cols,
  .group-head,
  .group-fields {
    grid-template-columns: 1fr;
  }

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

  .settings-tabs {
    justify-content: flex-start;
    width: 100%;
  }

  .panel-toolbar {
    align-items: stretch;
    flex-direction: column;
  }

  .tabs,
  .output-field,
  .output-format {
    width: 100%;
  }

  textarea {
    min-height: 260px;
  }

  #singleNodeInput {
    min-height: 180px;
  }

  td {
    grid-template-columns: minmax(92px, 34%) minmax(0, 1fr);
  }

  .modal-backdrop {
    align-items: start;
    padding: 10px;
  }

  .qr-modal {
    width: 100%;
    max-height: calc(100vh - 20px);
  }

  .side-qr-trigger {
    right: 10px;
    bottom: 10px;
    top: auto;
    transform: none;
    min-width: 0;
    min-height: 34px;
    padding: 7px 10px;
    writing-mode: horizontal-tb;
  }

  .side-qr-trigger:active {
    transform: none;
  }
}
