:root {
  --paper: #f2f0e9;
  --paper-deep: #e7e3d9;
  --ink: #182420;
  --muted: #66736d;
  --line: #c9cec7;
  --green: #096c5c;
  --green-dark: #064f44;
  --yellow: #e5b84f;
  --red: #bc4d3f;
  --blue: #397a91;
  --white: #fbfaf6;
  --shadow: 0 14px 34px rgba(24, 36, 32, 0.09);
  --radius: 6px;
  --focus: 0 0 0 3px rgba(9, 108, 92, 0.22);
}

* {
  box-sizing: border-box;
  letter-spacing: 0;
}

html {
  min-width: 320px;
  background: var(--paper);
}

body {
  min-height: 100vh;
  margin: 0;
  color: var(--ink);
  background:
    linear-gradient(var(--line) 1px, transparent 1px),
    linear-gradient(90deg, rgba(201, 206, 199, 0.42) 1px, transparent 1px),
    var(--paper);
  background-size: 48px 48px;
  font-family: "Avenir Next", "PingFang SC", "Microsoft YaHei", sans-serif;
  font-size: 15px;
}

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

button,
select,
input[type="checkbox"],
input[type="radio"] {
  cursor: pointer;
}

button:focus-visible,
textarea:focus-visible,
select:focus-visible,
input:focus-visible {
  outline: none;
  box-shadow: var(--focus);
}

.skip-link {
  position: fixed;
  top: 8px;
  left: 8px;
  z-index: 100;
  padding: 8px 12px;
  color: var(--white);
  background: var(--ink);
  transform: translateY(-160%);
}

.skip-link:focus {
  transform: translateY(0);
}

.topbar {
  min-height: 68px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  padding: 12px clamp(18px, 4vw, 56px);
  border-bottom: 1px solid var(--line);
  background: rgba(242, 240, 233, 0.96);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
  font-family: "Iowan Old Style", "Songti SC", serif;
  font-size: clamp(17px, 2vw, 21px);
  font-weight: 700;
}

.brand-mark {
  width: 40px;
  height: 26px;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 2px;
  flex: 0 0 auto;
  padding: 3px;
  border: 1px solid var(--ink);
  background: var(--white);
}

.brand-mark i:nth-child(1) { background: var(--green); }
.brand-mark i:nth-child(2) { background: var(--yellow); }
.brand-mark i:nth-child(3) { background: var(--blue); }
.brand-mark i:nth-child(4) { background: var(--red); }

.connection-state {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 0 0 auto;
  color: var(--muted);
  font-size: 13px;
}

.state-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--green);
  box-shadow: 0 0 0 4px rgba(9, 108, 92, 0.12);
}

.connection-state.is-error .state-dot {
  background: var(--red);
  box-shadow: 0 0 0 4px rgba(188, 77, 63, 0.12);
}

.workspace {
  width: min(1480px, 100%);
  min-height: calc(100vh - 68px);
  display: grid;
  grid-template-columns: minmax(340px, 0.78fr) minmax(560px, 1.5fr);
  margin: 0 auto;
}

.query-pane,
.results-pane {
  min-width: 0;
  padding: clamp(28px, 4vw, 56px);
}

.query-pane {
  border-right: 1px solid var(--line);
  background: rgba(251, 250, 246, 0.78);
}

.results-pane {
  background: rgba(242, 240, 233, 0.84);
}

.pane-heading {
  min-height: 58px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 18px;
  margin-bottom: 30px;
}

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

h1,
h2,
h3,
p {
  overflow-wrap: anywhere;
}

h1,
h2 {
  margin: 0;
  font-family: "Iowan Old Style", "Songti SC", serif;
  font-size: clamp(28px, 4vw, 42px);
  line-height: 1.05;
}

h3 {
  margin: 7px 0 8px;
  font-family: "Iowan Old Style", "Songti SC", serif;
  font-size: clamp(21px, 2.5vw, 28px);
  line-height: 1.18;
}

.quiet-button {
  min-height: 36px;
  padding: 7px 10px;
  border: 0;
  border-bottom: 1px solid currentColor;
  color: var(--muted);
  background: transparent;
}

.field-group {
  display: grid;
  gap: 9px;
}

.field-label-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

label,
legend {
  color: var(--ink);
  font-size: 13px;
  font-weight: 700;
}

.mask-toggle {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  color: var(--muted);
  font-weight: 500;
}

.mask-toggle input {
  width: 16px;
  height: 16px;
  accent-color: var(--green);
}

textarea,
select {
  width: 100%;
  border: 1px solid #aeb7b0;
  border-radius: var(--radius);
  color: var(--ink);
  background: var(--white);
  transition: border-color 160ms ease-out, box-shadow 160ms ease-out;
}

textarea {
  min-height: 190px;
  resize: vertical;
  padding: 16px;
  font-family: "SFMono-Regular", Consolas, monospace;
  font-size: 13px;
  line-height: 1.7;
  overflow-wrap: normal;
}

textarea.is-masked {
  -webkit-text-security: disc;
}

textarea:hover,
select:hover {
  border-color: #7e8c84;
}

.field-error {
  min-height: 20px;
  margin: 0;
  color: var(--red);
  font-size: 12px;
}

.query-options {
  display: grid;
  grid-template-columns: 1fr 120px;
  align-items: end;
  gap: 18px;
  margin: 18px 0 24px;
}

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

.segmented-field legend {
  margin-bottom: 9px;
}

.segmented-control {
  height: 44px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  padding: 3px;
  border: 1px solid #aeb7b0;
  border-radius: var(--radius);
  background: var(--paper-deep);
}

.segmented-control label {
  position: relative;
  min-width: 0;
}

.segmented-control input {
  position: absolute;
  inset: 0;
  opacity: 0;
}

.segmented-control span {
  height: 100%;
  display: grid;
  place-items: center;
  border-radius: 3px;
  color: var(--muted);
  font-size: 13px;
  transition: color 160ms ease-out, background-color 160ms ease-out;
}

.segmented-control input:checked + span {
  color: var(--white);
  background: var(--ink);
}

.segmented-control input:focus-visible + span {
  box-shadow: var(--focus);
}

select {
  height: 44px;
  padding: 0 32px 0 12px;
}

.primary-button,
.secondary-button {
  border: 0;
  border-radius: var(--radius);
  font-weight: 700;
  transition: background-color 160ms ease-out, transform 160ms ease-out;
}

.primary-button {
  width: 100%;
  min-height: 50px;
  display: grid;
  place-items: center;
  color: var(--white);
  background: var(--green);
  box-shadow: 0 8px 18px rgba(9, 108, 92, 0.18);
}

.primary-button:hover:not(:disabled) {
  background: var(--green-dark);
  transform: translateY(-1px);
}

.primary-button:disabled {
  cursor: wait;
  opacity: 0.78;
}

.button-progress {
  display: none;
  width: 18px;
  height: 18px;
  border: 2px solid rgba(255, 255, 255, 0.4);
  border-top-color: var(--white);
  border-radius: 50%;
  animation: spin 800ms linear infinite;
}

.primary-button.is-loading .button-label { display: none; }
.primary-button.is-loading .button-progress { display: block; }

.privacy-rail {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 18px;
  margin-top: 24px;
  padding-top: 18px;
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-family: "SFMono-Regular", Consolas, monospace;
  font-size: 10px;
  text-transform: uppercase;
}

.privacy-rail span::before {
  content: "";
  width: 5px;
  height: 5px;
  display: inline-block;
  margin: 0 7px 1px 0;
  background: var(--green);
}

.results-heading {
  align-items: flex-end;
}

.result-meta {
  max-width: 320px;
  margin: 0;
  color: var(--muted);
  font-family: "SFMono-Regular", Consolas, monospace;
  font-size: 11px;
  text-align: right;
}

.empty-state {
  min-height: 430px;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 18px;
  border: 1px dashed #aeb7b0;
  color: var(--muted);
  background: rgba(251, 250, 246, 0.44);
}

.empty-state p {
  margin: 0;
  font-family: "SFMono-Regular", Consolas, monospace;
  font-size: 12px;
}

.empty-glyph {
  width: 82px;
  height: 54px;
  position: relative;
  border: 2px solid #8e9b94;
  background: var(--paper);
}

.empty-glyph::before,
.empty-glyph::after {
  content: "";
  position: absolute;
  top: 9px;
  width: 48px;
  height: 2px;
  background: #8e9b94;
}

.empty-glyph::before { left: 0; transform: rotate(31deg); }
.empty-glyph::after { right: 0; transform: rotate(-31deg); }

.empty-glyph span {
  position: absolute;
  right: -8px;
  bottom: -8px;
  width: 20px;
  height: 20px;
  border: 3px solid var(--paper);
  background: var(--yellow);
}

.result-layout {
  min-height: 430px;
  display: grid;
  grid-template-columns: minmax(230px, 0.72fr) minmax(320px, 1.45fr);
  border: 1px solid var(--line);
  background: var(--white);
  box-shadow: var(--shadow);
}

.message-list {
  max-height: 650px;
  overflow-y: auto;
  border-right: 1px solid var(--line);
  background: var(--paper-deep);
}

.message-item {
  width: 100%;
  min-height: 108px;
  display: block;
  padding: 17px 16px;
  border: 0;
  border-bottom: 1px solid var(--line);
  color: var(--ink);
  background: transparent;
  text-align: left;
}

.message-item:hover,
.message-item.is-active {
  background: var(--white);
}

.message-item.is-active {
  box-shadow: inset 4px 0 0 var(--green);
}

.message-item strong,
.message-item span,
.message-item time {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.message-item strong {
  margin: 5px 0 7px;
  font-size: 14px;
}

.message-item span,
.message-item time {
  color: var(--muted);
  font-size: 11px;
}

.message-detail {
  min-width: 0;
  max-height: 650px;
  overflow-y: auto;
  padding: clamp(24px, 4vw, 42px);
}

.detail-header {
  padding-bottom: 24px;
  border-bottom: 1px solid var(--line);
}

.detail-sender,
.detail-header time {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
}

.code-strip {
  display: grid;
  grid-template-columns: auto 1fr;
  align-items: center;
  gap: 18px;
  margin: 22px 0;
  padding: 13px 14px;
  border-left: 4px solid var(--yellow);
  background: #f7edcf;
}

.code-strip > span {
  color: #6c5420;
  font-size: 12px;
  font-weight: 700;
}

#codeList {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.code-button {
  min-height: 34px;
  padding: 5px 10px;
  border: 1px solid #b89442;
  border-radius: 4px;
  color: #43330f;
  background: #fff9e7;
  font-family: "SFMono-Regular", Consolas, monospace;
  font-weight: 700;
}

.message-body {
  min-height: 180px;
  margin: 0;
  color: #2d3935;
  font-family: "Avenir Next", "PingFang SC", sans-serif;
  font-size: 14px;
  line-height: 1.75;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.rotated-token {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  margin-top: 18px;
  padding: 16px;
  border: 1px solid #b89442;
  background: #f7edcf;
}

.rotated-token p {
  margin: 3px 0 0;
  color: #6c5420;
  font-size: 12px;
}

.secondary-button {
  min-height: 40px;
  flex: 0 0 auto;
  padding: 8px 13px;
  color: var(--white);
  background: var(--ink);
}

.toast {
  position: fixed;
  right: 20px;
  bottom: 20px;
  z-index: 50;
  max-width: min(380px, calc(100vw - 40px));
  padding: 12px 15px;
  border-radius: var(--radius);
  color: var(--white);
  background: var(--ink);
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transform: translateY(10px);
  transition: opacity 180ms ease-out, transform 180ms ease-out;
}

.toast.is-visible {
  opacity: 1;
  transform: translateY(0);
}

.toast.is-error {
  background: var(--red);
}

.is-hidden {
  display: none !important;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

@media (max-width: 980px) {
  .workspace {
    grid-template-columns: 1fr;
  }

  .query-pane {
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .result-layout {
    grid-template-columns: minmax(210px, 0.72fr) minmax(320px, 1.45fr);
  }
}

@media (max-width: 680px) {
  .topbar {
    min-height: 60px;
    padding: 10px 16px;
  }

  .connection-state span:last-child {
    display: none;
  }

  .workspace {
    min-height: calc(100vh - 60px);
  }

  .query-pane,
  .results-pane {
    padding: 26px 16px 34px;
  }

  .pane-heading {
    margin-bottom: 24px;
  }

  .query-options {
    grid-template-columns: 1fr;
  }

  .count-field {
    grid-template-columns: 95px 1fr;
    align-items: center;
  }

  .result-meta {
    display: none;
  }

  .result-layout {
    grid-template-columns: 1fr;
  }

  .message-list {
    max-height: 270px;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .message-detail {
    max-height: none;
  }

  .rotated-token {
    align-items: stretch;
    flex-direction: column;
  }

  .secondary-button {
    width: 100%;
  }
}

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