:root {
  color-scheme: light;
  --ink: #17201d;
  --muted: #68736e;
  --paper: #f4f0e7;
  --panel: #fffdf8;
  --line: #d8d2c4;
  --line-strong: #aaa294;
  --green: #0d6b4f;
  --green-dark: #094c39;
  --orange: #df5b2b;
  --orange-soft: #fff0e8;
  --shadow: 0 18px 50px rgba(41, 48, 44, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-width: 320px;
  background:
    linear-gradient(rgba(23, 32, 29, 0.035) 1px, transparent 1px),
    linear-gradient(90deg, rgba(23, 32, 29, 0.035) 1px, transparent 1px),
    var(--paper);
  background-size: 28px 28px;
  color: var(--ink);
  font-family: "Avenir Next", "PingFang SC", "Microsoft YaHei", sans-serif;
}

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

button,
select {
  cursor: pointer;
}

.shell {
  width: min(1180px, calc(100% - 40px));
  margin: 0 auto;
  padding: 44px 0 64px;
}

.masthead {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 32px;
  padding: 0 4px 28px;
}

.eyebrow,
.section-index {
  margin: 0 0 8px;
  color: var(--green);
  font-family: "SFMono-Regular", Consolas, monospace;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.16em;
}

h1,
h2,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 10px;
  font-family: "Songti SC", "STSong", serif;
  font-size: clamp(36px, 6vw, 64px);
  font-weight: 700;
  letter-spacing: -0.045em;
  line-height: 1;
}

h2 {
  margin-bottom: 5px;
  font-family: "Songti SC", "STSong", serif;
  font-size: 28px;
  letter-spacing: -0.02em;
}

.lede,
.section-heading p {
  max-width: 700px;
  margin-bottom: 0;
  color: var(--muted);
  line-height: 1.7;
}

.status-cluster {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  flex: 0 0 auto;
  padding: 10px 14px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(255, 253, 248, 0.78);
  font-size: 13px;
  font-weight: 700;
}

.live-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: #18a66f;
  box-shadow: 0 0 0 5px rgba(24, 166, 111, 0.12);
}

.logout-button {
  min-height: 32px;
  margin-left: 3px;
  padding: 0 9px;
  border: 0;
  border-left: 1px solid var(--line);
  background: transparent;
  color: var(--muted);
  font-size: 12px;
  font-weight: 750;
}

.logout-button:hover {
  color: var(--orange);
}

.telemetry {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr)) 1.8fr;
  margin-bottom: 18px;
  overflow: hidden;
  border: 1px solid var(--ink);
  border-radius: 14px;
  background: var(--ink);
  color: white;
}

.metric {
  min-width: 0;
  padding: 16px 18px;
  border-right: 1px solid rgba(255, 255, 255, 0.16);
}

.metric:last-child {
  border-right: 0;
}

.metric span,
.metric strong {
  display: block;
}

.metric span {
  margin-bottom: 7px;
  color: #abb7b2;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
}

.metric strong {
  overflow: hidden;
  font-family: "SFMono-Regular", Consolas, monospace;
  font-size: 13px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#config-form {
  display: grid;
  gap: 18px;
}

.panel {
  padding: 26px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: rgba(255, 253, 248, 0.94);
  box-shadow: var(--shadow);
}

.section-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 24px;
  padding-bottom: 22px;
  border-bottom: 1px solid var(--line);
}

.section-heading.compact {
  align-items: center;
}

.rules {
  display: grid;
  gap: 16px;
  padding-top: 20px;
}

.rule-card {
  overflow: hidden;
  border: 1px solid var(--line-strong);
  border-radius: 14px;
  background: var(--panel);
  animation: card-in 240ms ease-out both;
}

@keyframes card-in {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
}

.rule-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  background: #ebe6da;
}

.rule-identity {
  display: flex;
  align-items: center;
  min-width: 0;
  flex: 1;
}

.rule-name-field {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  flex: 1;
  font-size: 12px;
  font-weight: 750;
}

.rule-name-field > span {
  flex: 0 0 auto;
}

.rule-name-field input {
  width: min(420px, 100%);
  height: 36px;
  border-color: transparent;
  background: rgba(255, 255, 255, 0.72);
  font-weight: 700;
}

.rule-number {
  display: inline-grid;
  place-items: center;
  width: 30px;
  height: 30px;
  margin-right: 10px;
  border-radius: 8px;
  background: var(--ink);
  color: white;
  font-family: "SFMono-Regular", Consolas, monospace;
  font-size: 11px;
}

.rule-title {
  font-size: 14px;
}

.match-grid,
.destination-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 15px;
  padding: 18px;
}

.match-grid {
  grid-template-columns: 0.8fr 1.35fr 1.65fr;
}

.field {
  display: grid;
  align-content: start;
  gap: 7px;
  min-width: 0;
}

.field > span,
.field > legend {
  color: #3f4945;
  font-size: 12px;
  font-weight: 750;
}

.field > legend {
  margin-bottom: 7px;
  padding: 0;
}

.field small {
  color: var(--muted);
  font-weight: 500;
}

.device-type-field {
  min-width: 0;
  margin: 0;
  padding: 0;
  border: 0;
}

.device-type-options {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px;
}

.device-type-option {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 40px;
  padding: 6px 9px;
  border-radius: 8px;
  background: #f2efe8;
  box-shadow: 0 0 0 1px rgba(23, 32, 29, 0.09);
  cursor: pointer;
  font-size: 11px;
  font-weight: 650;
  line-height: 1.3;
  transition-property: background, box-shadow;
  transition-duration: 150ms;
  transition-timing-function: ease-out;
}

.device-type-option:hover {
  background: #e9f1ed;
  box-shadow: 0 0 0 1px rgba(13, 107, 79, 0.25);
}

.device-type-option:has(input:checked) {
  background: #e1f0ea;
  box-shadow: 0 0 0 1px var(--green);
  color: var(--green-dark);
}

.device-type-option input {
  width: 17px;
  height: 17px;
  flex: 0 0 17px;
  margin: 0;
  padding: 0;
  accent-color: var(--green);
}

.device-type-option:has(input:focus-visible) {
  box-shadow: 0 0 0 3px rgba(13, 107, 79, 0.18);
}

.field em {
  color: var(--orange);
  font-size: 10px;
  font-style: normal;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line-strong);
  border-radius: 9px;
  outline: none;
  background: #fff;
  color: var(--ink);
  transition:
    border-color 150ms ease,
    box-shadow 150ms ease,
    background 150ms ease;
}

input,
select {
  height: 44px;
  padding: 0 12px;
}

textarea {
  min-height: 72px;
  padding: 10px 12px;
  line-height: 1.5;
  resize: vertical;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--green);
  box-shadow: 0 0 0 3px rgba(13, 107, 79, 0.12);
}

input:disabled {
  border-style: dashed;
  background: #efede6;
  color: var(--muted);
}

.address-field.muted > span {
  color: #999f9c;
}

.device-selection-summary {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
  min-height: 22px;
  color: var(--muted);
  font-size: 11px;
}

.selection-count,
.device-chip {
  padding: 3px 7px;
  border-radius: 999px;
}

.device-expand-button {
  min-height: 40px;
  padding: 0 8px;
  border: 0;
  background: transparent;
  color: var(--green);
  font-size: 11px;
  font-weight: 750;
}

.device-expand-button:hover {
  color: var(--green-dark);
  text-decoration: underline;
}

.device-expand-button:focus-visible {
  border-radius: 5px;
  outline: 3px solid rgba(13, 107, 79, 0.2);
  outline-offset: 1px;
}

.selection-count {
  background: var(--green);
  color: white;
  font-weight: 750;
}

.device-chip {
  border: 1px solid #c9d8d2;
  background: #edf5f2;
  color: var(--green-dark);
  font-family: "SFMono-Regular", Consolas, monospace;
}

.route-arrow {
  position: relative;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 18px;
  color: var(--green);
  font-family: "SFMono-Regular", Consolas, monospace;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.08em;
}

.route-arrow::before,
.route-arrow::after {
  content: "";
  height: 1px;
  flex: 1;
  background: var(--line);
}

.button,
.icon-button {
  border: 0;
  font-weight: 750;
}

.button {
  min-height: 43px;
  padding: 0 17px;
  border-radius: 10px;
}

.button.primary {
  background: var(--green);
  color: white;
  box-shadow: 0 7px 18px rgba(13, 107, 79, 0.2);
}

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

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

.button.secondary:hover {
  background: var(--ink);
  color: white;
}

.button:disabled {
  cursor: not-allowed;
  opacity: 0.45;
  box-shadow: none;
}

.icon-button {
  padding: 6px 9px;
  border-radius: 7px;
  background: transparent;
  color: #8a3d24;
  font-size: 12px;
}

.icon-button:hover {
  background: var(--orange-soft);
  color: #a33d1a;
}

.empty-state {
  margin-top: 20px;
  padding: 30px;
  border: 1px dashed var(--line-strong);
  border-radius: 12px;
  text-align: center;
}

.empty-state strong,
.empty-state span {
  display: block;
}

.empty-state span {
  margin-top: 6px;
  color: var(--muted);
  font-size: 13px;
}

.switch {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 750;
}

.switch input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
}

.switch-track {
  position: relative;
  width: 44px;
  height: 24px;
  border-radius: 999px;
  background: #bbb8af;
  transition: background 150ms ease;
}

.switch-track::after {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  content: "";
  transition: transform 150ms ease;
}

.switch input:checked + .switch-track {
  background: var(--green);
}

.switch input:checked + .switch-track::after {
  transform: translateX(20px);
}

.switch input:focus-visible + .switch-track {
  outline: 3px solid rgba(13, 107, 79, 0.25);
  outline-offset: 2px;
}

.disabled-note {
  margin: 18px 0 0;
  padding: 16px;
  border: 1px solid #e9c5b8;
  border-radius: 10px;
  background: var(--orange-soft);
  color: #7f3c25;
  font-size: 13px;
}

.action-bar {
  position: sticky;
  bottom: 16px;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding: 14px 16px;
  border: 1px solid rgba(23, 32, 29, 0.22);
  border-radius: 14px;
  background: rgba(255, 253, 248, 0.92);
  box-shadow: 0 16px 45px rgba(26, 35, 31, 0.16);
  backdrop-filter: blur(14px);
}

.action-bar strong,
.action-bar span {
  display: block;
}

.action-bar strong {
  font-size: 13px;
}

.action-bar > div > span {
  margin-top: 2px;
  color: var(--muted);
  font-size: 11px;
}

.action-buttons {
  display: flex;
  align-items: center;
  gap: 16px;
}

.status {
  min-height: 18px;
  color: var(--muted);
  font-size: 12px;
  text-align: right;
}

.status.ok {
  color: var(--green);
}

.status.error {
  color: #b63c21;
}

.logs-panel {
  margin-top: 18px;
}

.log-controls {
  display: flex;
  align-items: end;
  gap: 10px;
}

.compact-field {
  min-width: 170px;
}

.log-summary {
  padding: 14px 0 10px;
  color: var(--muted);
  font-size: 12px;
}

.log-table-wrap {
  max-height: min(62vh, 560px);
  overflow: auto;
  overscroll-behavior: contain;
  scrollbar-gutter: stable;
  border: 1px solid var(--line);
  border-radius: 12px;
}

.log-table {
  width: 100%;
  border-collapse: collapse;
  background: #fff;
  font-size: 12px;
}

.log-table th,
.log-table td {
  padding: 11px 13px;
  border-bottom: 1px solid #ebe7de;
  text-align: left;
  vertical-align: top;
}

.log-table th {
  position: sticky;
  z-index: 1;
  top: 0;
  background: #ebe6da;
  color: #4d5753;
  font-size: 11px;
  letter-spacing: 0.04em;
}

.log-table th:first-child,
.log-table td:first-child {
  width: 190px;
  white-space: nowrap;
}

.log-table th:nth-child(2),
.log-table td:nth-child(2) {
  width: 110px;
  white-space: nowrap;
}

.log-table td:last-child {
  min-width: 320px;
  font-family: "SFMono-Regular", Consolas, monospace;
  line-height: 1.55;
}

.log-message {
  display: -webkit-box;
  overflow: hidden;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
}

.log-table td.expanded .log-message {
  display: block;
  overflow: visible;
  -webkit-line-clamp: initial;
}

.log-expand-button {
  min-width: 40px;
  min-height: 40px;
  margin: 4px 0 -8px;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--green);
  font-size: 12px;
  font-weight: 750;
  text-align: left;
}

.log-expand-button:hover {
  color: var(--green-dark);
  text-decoration: underline;
}

.log-expand-button:focus-visible {
  border-radius: 5px;
  outline: 3px solid rgba(13, 107, 79, 0.2);
  outline-offset: 2px;
}

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

.log-table td[data-event="frame-route"] {
  color: var(--green);
  font-weight: 750;
}

.log-table td[data-event="upstream-reply-dropped"] {
  color: var(--orange);
  font-weight: 750;
}

.compact-empty {
  margin: 0;
  border: 0;
  border-radius: 0;
}

[hidden] {
  display: none !important;
}

@media (max-width: 900px) {
  .telemetry {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .metric:nth-child(2) {
    border-right: 0;
  }

  .metric:nth-child(-n + 2) {
    border-bottom: 1px solid rgba(255, 255, 255, 0.16);
  }

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

  .log-controls {
    align-items: stretch;
    flex-direction: column;
    width: 100%;
  }
}

@media (max-width: 640px) {
  .shell {
    width: min(100% - 24px, 1180px);
    padding-top: 24px;
  }

  .masthead,
  .section-heading,
  .action-bar {
    align-items: stretch;
    flex-direction: column;
  }

  .status-cluster {
    align-self: flex-start;
  }

  .telemetry,
  .match-grid,
  .destination-grid {
    grid-template-columns: 1fr;
  }

  .metric {
    border-right: 0;
    border-bottom: 1px solid rgba(255, 255, 255, 0.16);
  }

  .metric:last-child {
    border-bottom: 0;
  }

  .panel {
    padding: 18px;
  }

  .match-grid,
  .destination-grid {
    padding: 15px;
  }

  .action-buttons {
    align-items: stretch;
    flex-direction: column;
  }

  .status {
    text-align: left;
  }

  .rule-header,
  .rule-identity {
    align-items: flex-start;
  }

  .rule-name-field {
    align-items: stretch;
    flex-direction: column;
    gap: 4px;
  }
}
