/* ============ Tokens ============ */
:root {
  --bg: #F5F6F4;
  --surface: #FFFFFF;
  --surface-sunken: #EEF0EC;
  --ink: #1A2128;
  --ink-soft: #5C6570;
  --ink-faint: #8A929B;
  --line: #DFE2DD;
  --line-strong: #C7CBC4;
  --accent: #2E6B68;
  --accent-soft: #E4EFEE;
  --accent-ink: #1E4E4B;

  --delivered: #2E7D53;
  --delivered-bg: #E4F3EA;
  --deferred: #A9761F;
  --deferred-bg: #FBF0DC;
  --failed: #B8433A;
  --failed-bg: #FBE7E5;
  --processing: #33568F;
  --processing-bg: #E6ECF7;
  --unknown: #767D85;
  --unknown-bg: #E9EAE7;

  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --font-mono: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;

  --radius: 10px;
  --radius-sm: 6px;
  --shadow-card: 0 1px 2px rgba(20, 24, 20, 0.05), 0 6px 20px -12px rgba(20, 24, 20, 0.18);
}

[data-theme="dark"] {
  --bg: #10141A;
  --surface: #171D25;
  --surface-sunken: #10151B;
  --ink: #E7EAEC;
  --ink-soft: #9BA5AF;
  --ink-faint: #6B7580;
  --line: #262E38;
  --line-strong: #333D49;
  --accent: #58B3AC;
  --accent-soft: #16302E;
  --accent-ink: #8AD1CA;

  --delivered: #55B384;
  --delivered-bg: #10281C;
  --deferred: #D6A94C;
  --deferred-bg: #2E2410;
  --failed: #E17870;
  --failed-bg: #301715;
  --processing: #7DA0E0;
  --processing-bg: #16223A;
  --unknown: #8B94A0;
  --unknown-bg: #1D222A;

  --shadow-card: 0 1px 2px rgba(0, 0, 0, 0.25), 0 10px 26px -14px rgba(0, 0, 0, 0.55);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-sans);
  transition: background 0.15s ease, color 0.15s ease;
}

body {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

::selection { background: var(--accent-soft); color: var(--accent-ink); }

/* ============ Header ============ */
.app-header {
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}

.header-inner {
  max-width: 920px;
  margin: 0 auto;
  padding: 22px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.brand { display: flex; align-items: center; gap: 14px; }

.brand-mark {
  width: 34px;
  height: 34px;
  border-radius: 9px;
  flex-shrink: 0;
  background: linear-gradient(155deg, var(--accent) 0%, var(--accent-ink) 100%);
  position: relative;
}
.brand-mark::before {
  content: "";
  position: absolute;
  inset: 9px;
  border: 1.5px solid rgba(255,255,255,0.75);
  border-radius: 3px;
}
.brand-mark::after {
  content: "";
  position: absolute;
  left: 9px;
  right: 9px;
  top: 9px;
  height: 1.5px;
  background: rgba(255,255,255,0.75);
  transform-origin: left;
  transform: rotate(21deg) translateY(6px) scaleX(0.78);
}

.brand-text h1 {
  font-size: 17px;
  margin: 0;
  letter-spacing: -0.01em;
}
.brand-text p {
  margin: 2px 0 0;
  font-size: 13px;
  color: var(--ink-soft);
  max-width: 46ch;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 4px;
}

.theme-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  background: none;
  border: none;
  cursor: pointer;
  color: var(--ink-soft);
  font-family: var(--font-sans);
  font-size: 12.5px;
  padding: 6px;
}
.theme-toggle-track {
  width: 34px;
  height: 19px;
  border-radius: 999px;
  background: var(--surface-sunken);
  border: 1px solid var(--line-strong);
  display: inline-flex;
  align-items: center;
  padding: 2px;
}
.theme-toggle-thumb {
  width: 13px;
  height: 13px;
  border-radius: 50%;
  background: var(--accent);
  transition: transform 0.15s ease;
}
[data-theme="dark"] .theme-toggle-thumb { transform: translateX(15px); }

/* ============ Main layout ============ */
.app-main {
  flex: 1;
  max-width: 920px;
  width: 100%;
  margin: 0 auto;
  padding: 28px 24px 60px;
}

/* ============ Input panel ============ */
.input-panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow-card);
}

.input-label {
  display: block;
  font-size: 12.5px;
  font-weight: 600;
  letter-spacing: 0.02em;
  color: var(--ink-soft);
  margin-bottom: 8px;
}

.log-textarea {
  width: 100%;
  min-height: 160px;
  resize: vertical;
  background: var(--surface-sunken);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  font-family: var(--font-mono);
  font-size: 12.5px;
  line-height: 1.6;
  color: var(--ink);
  outline: none;
}
.log-textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.log-textarea::placeholder { color: var(--ink-faint); }

.input-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 14px;
}

.btn {
  font-family: var(--font-sans);
  font-size: 13.5px;
  font-weight: 600;
  padding: 9px 16px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  cursor: pointer;
}
.btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.btn-primary {
  background: var(--accent);
  color: #FFFFFF;
}
.btn-primary:hover { background: var(--accent-ink); }
.btn-ghost {
  background: transparent;
  border-color: var(--line-strong);
  color: var(--ink-soft);
}
.btn-ghost:hover { color: var(--ink); border-color: var(--ink-faint); }

.parse-note {
  font-size: 12.5px;
  color: var(--ink-faint);
}
.parse-note.is-error { color: var(--failed); }

/* ============ Empty state ============ */
.empty-state {
  margin-top: 48px;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 14px;
  color: var(--ink-faint);
  font-size: 13.5px;
}
.empty-rail {
  display: flex;
  align-items: center;
  gap: 10px;
}
.empty-node {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--line-strong);
}
.empty-node:nth-child(2) { background: var(--accent); opacity: 0.5; }

/* ============ Results ============ */
.results-section { margin-top: 28px; }
.results-section + .results-section { margin-top: 32px; }

.section-heading {
  font-size: 12.5px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--ink-faint);
  margin: 0 0 12px;
}

.queue-list,
.connection-list {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* ============ Queue card ============ */
.queue-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow-card);
  overflow: hidden;
}

.queue-card-header {
  padding: 16px 20px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 14px;
  border-bottom: 1px solid var(--line);
}

.queue-id {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--ink-faint);
  letter-spacing: 0.01em;
  margin: 0 0 4px;
  text-transform: uppercase;
}

.queue-summary {
  font-size: 14.5px;
  line-height: 1.55;
  margin: 0;
  max-width: 60ch;
}

.status-badge {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 11.5px;
  font-weight: 700;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  padding: 5px 10px;
  border-radius: 999px;
  white-space: nowrap;
}
.status-badge::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
}
.status-delivered { background: var(--delivered-bg); color: var(--delivered); }
.status-deferred  { background: var(--deferred-bg); color: var(--deferred); }
.status-failed    { background: var(--failed-bg); color: var(--failed); }
.status-processing{ background: var(--processing-bg); color: var(--processing); }
.status-unknown   { background: var(--unknown-bg); color: var(--unknown); }

/* ============ Connection card ============ */
.connection-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow-card);
  overflow: hidden;
}

.connection-card-header {
  padding: 16px 20px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 14px;
}

.connection-badges {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.method-badge {
  display: inline-flex;
  align-items: center;
  font-family: var(--font-mono);
  font-size: 11.5px;
  font-weight: 700;
  letter-spacing: 0.02em;
  padding: 5px 9px;
  border-radius: var(--radius-sm);
  background: var(--surface-sunken);
  border: 1px solid var(--line-strong);
  color: var(--ink-soft);
}

.connection-meta {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--ink-faint);
  margin: 0 0 4px;
  text-transform: uppercase;
  letter-spacing: 0.01em;
}

.connection-summary {
  font-size: 14.5px;
  line-height: 1.55;
  margin: 0;
  max-width: 60ch;
}

.connection-card .tech-details {
  margin: 0 20px 18px;
}

/* ============ Timeline ============ */
.timeline {
  list-style: none;
  margin: 0;
  padding: 20px 20px 6px;
}

.timeline-event {
  position: relative;
  padding: 0 0 22px 26px;
  border-left: 2px solid var(--line);
}
.timeline-event:last-child {
  border-left: 2px solid transparent;
  padding-bottom: 2px;
}

.timeline-node {
  position: absolute;
  left: -7px;
  top: 2px;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--surface);
  border: 2px solid var(--accent);
}
.timeline-node.node-delivered { border-color: var(--delivered); }
.timeline-node.node-deferred  { border-color: var(--deferred); }
.timeline-node.node-failed    { border-color: var(--failed); }
.timeline-node.node-warning   { border-color: var(--deferred); }

.event-timestamp {
  font-family: var(--font-mono);
  font-size: 11.5px;
  color: var(--ink-faint);
  margin-bottom: 3px;
}

.event-title {
  font-size: 14.5px;
  font-weight: 700;
  margin-bottom: 3px;
}

.event-explanation {
  font-size: 13.5px;
  color: var(--ink-soft);
  line-height: 1.55;
  margin: 0;
}

.event-note {
  margin-top: 6px;
  font-size: 12.5px;
  color: var(--deferred);
  background: var(--deferred-bg);
  border-radius: var(--radius-sm);
  padding: 6px 10px;
  display: inline-block;
}

/* ============ Technical details ============ */
.tech-details {
  margin-top: 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface-sunken);
}

.tech-toggle {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  background: none;
  border: none;
  cursor: pointer;
  padding: 8px 12px;
  font-family: var(--font-sans);
  font-size: 12px;
  font-weight: 600;
  color: var(--ink-soft);
  letter-spacing: 0.02em;
}
.tech-toggle:hover { color: var(--ink); }

.tech-chevron {
  transition: transform 0.15s ease;
  color: var(--ink-faint);
  font-size: 10px;
}
.tech-details[open] .tech-chevron { transform: rotate(90deg); }

.tech-body {
  padding: 4px 12px 12px;
}

.tech-fields {
  display: grid;
  grid-template-columns: max-content 1fr;
  column-gap: 14px;
  row-gap: 4px;
  font-family: var(--font-mono);
  font-size: 12px;
  margin: 0 0 10px;
}
.tech-fields dt { color: var(--ink-faint); }
.tech-fields dd {
  margin: 0;
  color: var(--ink);
  word-break: break-word;
}

.raw-log-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--ink-faint);
  margin: 0 0 4px;
}

.raw-log {
  font-family: var(--font-mono);
  font-size: 11.5px;
  line-height: 1.5;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
  color: var(--ink-soft);
}

/* ============ Footer ============ */
.app-footer {
  border-top: 1px solid var(--line);
  padding: 16px 24px;
  text-align: center;
}
.app-footer p {
  margin: 0;
  font-size: 12px;
  color: var(--ink-faint);
}

/* ============ Cheat Sheet Modal ============ */
.cheat-sheet-modal {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink);
  padding: 0;
  width: min(640px, calc(100vw - 32px));
  max-height: min(680px, calc(100vh - 64px));
  overflow: hidden;
  box-shadow: var(--shadow-card);
}
.cheat-sheet-modal:not([open]) {
  display: none;
}
.cheat-sheet-modal[open] {
  display: flex;
  flex-direction: column;
}
.cheat-sheet-modal::backdrop {
  background: rgba(10, 14, 12, 0.55);
}

.cheat-sheet-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 14px;
  padding: 18px 20px;
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}
.cheat-sheet-header h2 {
  font-size: 16px;
  margin: 0 0 4px;
}
.cheat-sheet-header p {
  font-size: 12.5px;
  color: var(--ink-soft);
  margin: 0;
}

.modal-close {
  flex-shrink: 0;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 1px solid var(--line-strong);
  background: var(--surface-sunken);
  color: var(--ink-soft);
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
}
.modal-close:hover { color: var(--ink); border-color: var(--ink-faint); }

.cheat-sheet-body {
  padding: 8px 20px 20px;
  overflow-y: auto;
  flex: 1 1 auto;
  min-height: 0;
}

.cheat-sheet-category {
  font-size: 11.5px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--ink-faint);
  margin: 18px 0 8px;
}
.cheat-sheet-category:first-child { margin-top: 12px; }

.cheat-sheet-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.cheat-sheet-item {
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 10px 10px 10px 12px;
}

.cheat-sheet-label {
  font-size: 13px;
  color: var(--ink-soft);
  margin: 0 0 6px;
}

.cheat-sheet-command-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.cheat-sheet-command {
  flex: 1 1 auto;
  min-width: 0;
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--accent-ink);
  background: var(--accent-soft);
  border-radius: var(--radius-sm);
  padding: 7px 9px;
  overflow-x: auto;
  white-space: pre;
}

.cheat-sheet-copy {
  flex-shrink: 0;
  font-family: var(--font-sans);
  font-size: 11.5px;
  font-weight: 600;
  padding: 7px 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--line-strong);
  background: var(--surface-sunken);
  color: var(--ink-soft);
  cursor: pointer;
}
.cheat-sheet-copy:hover { color: var(--ink); border-color: var(--ink-faint); }
.cheat-sheet-copy.is-copied {
  color: var(--delivered);
  border-color: var(--delivered);
}

/* ============ Responsive ============ */
@media (max-width: 600px) {
  .header-inner { padding: 18px 16px; }
  .app-main { padding: 20px 16px 40px; }
  .queue-card-header { flex-direction: column; }
  .brand-text p { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}