:root {
  --ink: #12233f;
  --blue: #2657d8;
  --amber: #ffb423;
  --ice: #eef5ff;
  --paper: #f9fbff;
  --line: #cbd8ea;
  --muted: #63728a;
  --danger: #bd3f4b;
  --success: #16856b;
  --shadow: 0 18px 50px rgba(18, 35, 63, 0.1);
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
[hidden] { display: none !important; }

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background:
    linear-gradient(90deg, rgba(38, 87, 216, 0.04) 1px, transparent 1px) 0 0 / 44px 44px,
    linear-gradient(rgba(38, 87, 216, 0.04) 1px, transparent 1px) 0 0 / 44px 44px,
    var(--ice);
  font-family: "Segoe UI", Tahoma, sans-serif;
}

.login-shell { display: grid; place-items: center; min-height: 100vh; padding: 24px; }
.login-card { width: min(460px, 100%); padding: 38px; border: 1px solid var(--line); border-left: 6px solid var(--blue); border-radius: 20px; background: rgba(249,251,255,.97); box-shadow: var(--shadow); }
.login-card h1 { font-size: clamp(2.6rem, 8vw, 4.4rem); line-height: .9; }
.login-copy { margin: 22px 0 28px; color: var(--muted); line-height: 1.55; }
.login-card form label:first-child { margin-top: 0; }
.login-card .primary { margin-top: 24px; }

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

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  max-width: 1280px;
  margin: 0 auto;
  padding: 38px 34px 22px;
}

.kicker, .step, .microcopy, .counter, .live-mark {
  margin: 0 0 7px;
  color: var(--blue);
  font-family: Consolas, "Courier New", monospace;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.09em;
  text-transform: uppercase;
}

h1, h2 { margin: 0; font-family: "Arial Narrow", "Roboto Condensed", sans-serif; }
h1 { font-size: clamp(2.4rem, 5vw, 4.8rem); font-stretch: condensed; letter-spacing: -0.055em; line-height: 0.82; }
h2 { font-size: 1.55rem; letter-spacing: -0.025em; }
.topbar-copy { max-width: 560px; margin: 16px 0 0; color: var(--muted); font-size: 0.9rem; line-height: 1.5; }
.topbar-session { display: flex; align-items: center; gap: 10px; }
.user-chip { padding: 8px 12px; border-left: 1px solid var(--line); }
.user-chip strong, .user-chip small { display: block; }
.user-chip small { margin-top: 2px; color: var(--muted); font-family: Consolas, monospace; font-size: .65rem; text-transform: uppercase; }

.status-chip {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 10px 14px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(249, 251, 255, 0.8);
  font-size: 0.82rem;
}

.status-chip span { width: 9px; height: 9px; border-radius: 50%; background: var(--amber); box-shadow: 0 0 0 4px rgba(255, 180, 35, 0.18); }
.status-chip.connected span { background: var(--success); box-shadow: 0 0 0 4px rgba(22, 133, 107, 0.16); }

.section-nav {
  position: sticky;
  z-index: 20;
  top: 10px;
  display: grid;
  grid-template-columns: repeat(7, minmax(135px, 1fr));
  max-width: 1212px;
  margin: 0 auto 18px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: rgba(249, 251, 255, 0.94);
  box-shadow: 0 10px 30px rgba(18, 35, 63, 0.1);
  backdrop-filter: blur(12px);
  overflow-x: auto;
}
.section-nav a { display: flex; align-items: center; gap: 10px; padding: 13px 16px; color: var(--ink); border-right: 1px solid var(--line); font-size: 0.78rem; font-weight: 700; text-decoration: none; }
.section-nav a:last-child { border-right: 0; }
.section-nav a:hover { color: var(--blue); background: #edf4ff; }
.section-nav span { color: var(--blue); font-family: Consolas, monospace; font-size: 0.68rem; }

.console-sections {
  display: flex;
  flex-direction: column;
  gap: 18px;
  max-width: 1280px;
  margin: 0 auto;
  padding: 0 34px 42px;
}

.panel {
  min-width: 0;
  min-height: 0;
  padding: 34px;
  border: 1px solid var(--line);
  border-left: 5px solid var(--blue);
  border-radius: 18px;
  background: rgba(249, 251, 255, 0.94);
  box-shadow: var(--shadow);
  scroll-margin-top: 86px;
}

.panel-heading { margin-bottom: 20px; }
.panel-heading.split { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; }
.muted { max-width: 52ch; margin: 0 0 25px; color: var(--muted); line-height: 1.55; }

.connection-panel { display: grid; grid-template-columns: minmax(300px, 1fr) minmax(260px, 360px); grid-template-rows: auto auto auto auto; column-gap: 64px; align-items: center; }
.connection-panel .panel-heading, .connection-panel .muted, .connection-panel .primary, .connection-panel .microcopy { grid-column: 1; }
.connection-panel .qr-stage { grid-column: 2; grid-row: 1 / 5; }
.connection-panel .primary { max-width: 360px; }
.connection-panel .microcopy { max-width: 360px; }

.qr-stage {
  display: grid;
  place-items: center;
  aspect-ratio: 1;
  width: 100%;
  max-width: 330px;
  margin: 0 auto;
  padding: 20px;
  border: 1px dashed #9fb4d1;
  border-radius: 14px;
  background: white;
  overflow: hidden;
}

.qr-stage img { display: block; width: 100%; height: 100%; object-fit: contain; }
.qr-empty { position: relative; display: grid; place-items: center; width: 100%; height: 100%; color: var(--muted); text-align: center; }
.qr-empty::before, .qr-empty::after, .scan-corner::before, .scan-corner::after { content: ""; position: absolute; width: 28px; height: 28px; border-color: var(--blue); }
.qr-empty::before { top: 0; left: 0; border-top: 3px solid; border-left: 3px solid; }
.qr-empty::after { top: 0; right: 0; border-top: 3px solid; border-right: 3px solid; }
.scan-corner::before { bottom: 0; left: 0; border-bottom: 3px solid; border-left: 3px solid; }
.scan-corner::after { right: 0; bottom: 0; border-right: 3px solid; border-bottom: 3px solid; }

button {
  border: 0;
  border-radius: 10px;
  cursor: pointer;
  font-weight: 700;
  transition: transform 150ms ease, box-shadow 150ms ease, opacity 150ms ease;
}

button:hover:not(:disabled) { transform: translateY(-1px); }
button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible { outline: 3px solid rgba(38, 87, 216, 0.26); outline-offset: 2px; }
button:disabled { cursor: not-allowed; opacity: 0.46; }
.primary { width: 100%; padding: 13px 18px; color: white; background: var(--blue); box-shadow: 0 8px 20px rgba(38, 87, 216, 0.2); }
.quiet { padding: 8px 10px; color: var(--blue); background: transparent; white-space: nowrap; }
.microcopy { margin-top: 13px; color: var(--muted); text-align: center; text-transform: none; }

.send-layout { display: grid; grid-template-columns: minmax(0, 1.08fr) minmax(340px, 0.92fr); gap: 24px; }
.send-column { min-width: 0; padding: 22px; border: 1px solid var(--line); border-radius: 14px; background: rgba(255, 255, 255, 0.72); }
.column-label { margin: 0 0 16px; color: var(--blue); font-family: Consolas, monospace; font-size: 0.7rem; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase; }
.send-column > label:first-of-type { margin-top: 0; }
.send-composer .api-token-field { margin-top: 0; }

label { display: block; margin: 24px 0 8px; font-size: 0.83rem; font-weight: 700; }
input, select, textarea { width: 100%; border: 1px solid var(--line); border-radius: 10px; color: var(--ink); background: white; }
input { min-height: 42px; padding: 0 11px; }
select { min-height: 46px; padding: 0 12px; }
textarea { resize: vertical; min-height: 180px; padding: 14px; line-height: 1.5; }
.group-picker { overflow: hidden; border: 1px solid var(--line); border-radius: 12px; background: white; }
.group-picker[aria-busy="true"] { opacity: 0.72; }
.group-picker-toolbar { display: grid; grid-template-columns: minmax(160px, 1fr) auto auto; gap: 7px; padding: 9px; border-bottom: 1px solid var(--line); background: #f3f7fd; }
.group-picker-toolbar input { min-height: 38px; }
.picker-action { padding: 0 10px; color: var(--blue); border: 1px solid #aec3e4; background: white; font-size: 0.72rem; }
.group-list { max-height: 245px; padding: 7px; overflow-y: auto; }
.group-list-empty { margin: 0; padding: 18px 12px; color: var(--muted); font-size: 0.8rem; text-align: center; }
.group-option { display: grid; grid-template-columns: auto minmax(0, 1fr); align-items: center; gap: 10px; margin: 0; padding: 10px; border: 1px solid transparent; border-radius: 9px; cursor: pointer; }
.group-option:hover { background: #f6f9fe; }
.group-option.selected { border-color: #aec3e4; background: #edf4ff; }
.group-option input { width: 17px; min-height: 17px; margin: 0; accent-color: var(--blue); }
.group-option span { min-width: 0; }
.group-option strong, .group-option small { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.group-option strong { font-size: 0.82rem; }
.group-option small { margin-top: 3px; color: var(--muted); font-family: Consolas, monospace; font-size: 0.66rem; }
.group-picker-footer { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 9px 11px; border-top: 1px solid var(--line); color: var(--muted); background: #f9fbff; font-size: 0.68rem; }
.group-picker-footer strong { color: var(--blue); }
.jid-readout { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 10px; padding: 11px 12px; border: 1px solid #aec3e4; border-radius: 10px; background: #f0f6ff; }
.jid-readout[hidden] { display: none; }
.jid-readout div { min-width: 0; }
.jid-readout span { display: block; margin-bottom: 3px; color: var(--muted); font-size: 0.68rem; font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase; }
.jid-readout code { display: block; overflow: hidden; color: var(--ink); font-family: Consolas, monospace; font-size: 0.76rem; text-overflow: ellipsis; white-space: nowrap; }
.copy-button { flex: 0 0 auto; padding: 7px 10px; color: var(--blue); border: 1px solid #aec3e4; background: white; font-size: 0.75rem; }
.manual-group { margin-top: 12px; padding: 11px 12px; border: 1px dashed var(--line); border-radius: 10px; background: rgba(255,255,255,0.58); }
.manual-group summary { color: var(--blue); cursor: pointer; font-size: 0.8rem; font-weight: 700; }
.manual-group > p { margin: 10px 0 12px; color: var(--muted); font-size: 0.76rem; }
.manual-group-fields { display: grid; grid-template-columns: 1.2fr 1fr; gap: 10px; }
.manual-group label { margin: 0 0 5px; font-size: 0.72rem; }
.secondary { width: 100%; margin-top: 10px; padding: 10px 14px; color: var(--blue); border: 1px solid #aec3e4; background: white; }
.api-token-field { margin-top: 16px; padding: 12px; border: 1px solid #b9c9df; border-radius: 10px; background: #f5f8fd; }
.api-token-field label { margin: 0 0 6px; }
.api-token-field small { display: block; margin-top: 6px; color: var(--muted); font-size: 0.7rem; }
.media-field { margin-top: 16px; padding: 12px; border: 1px dashed var(--line); border-radius: 10px; background: rgba(255,255,255,0.58); }
.media-field label { margin: 0 0 6px; font-size: 0.76rem; }
.media-field label:not(:first-child) { margin-top: 12px; }
.send-actions { display: flex; align-items: center; justify-content: space-between; gap: 18px; margin-top: 12px; }
.send-actions .primary { width: auto; min-width: 170px; }
.counter { margin: 0; color: var(--muted); }
.feedback { min-height: 24px; color: var(--success); font-size: 0.88rem; }
.feedback.error { color: var(--danger); }

.live-mark { display: inline-flex; align-items: center; gap: 7px; margin: 4px 0 0; color: var(--success); white-space: nowrap; }
.live-mark i { display: block; width: 7px; height: 7px; border-radius: 50%; background: var(--success); animation: pulse 1.8s ease-out infinite; }
.activity-feed { position: relative; display: flex; flex-direction: column; gap: 12px; max-height: 460px; padding-left: 27px; overflow-y: auto; }
.activity-feed::before { content: ""; position: absolute; top: 0; bottom: 0; left: 8px; width: 2px; background: linear-gradient(var(--blue), var(--amber), transparent); }
.empty-state { padding: 20px; border: 1px dashed var(--line); border-radius: 10px; color: var(--muted); }
.empty-state p { margin: 0 0 5px; color: var(--ink); font-weight: 700; }
.message-card { position: relative; padding: 14px 16px; border: 1px solid var(--line); border-radius: 11px; background: white; }
.message-card::before { content: ""; position: absolute; top: 19px; left: -24px; width: 10px; height: 10px; border: 3px solid var(--paper); border-radius: 50%; background: var(--blue); }
.message-meta { display: flex; justify-content: space-between; gap: 12px; color: var(--muted); font-family: Consolas, monospace; font-size: 0.7rem; }
.message-card strong { display: block; margin: 7px 0 5px; font-size: 0.9rem; }
.message-card p { margin: 0; overflow-wrap: anywhere; line-height: 1.5; }

.history-panel { min-height: auto; }
.status-legend { display: flex; align-items: center; justify-content: flex-end; gap: 8px; margin: -48px 0 26px auto; color: var(--muted); font-family: Consolas, monospace; font-size: 0.66rem; text-transform: uppercase; }
.status-legend i { width: 18px; height: 1px; background: var(--line); }
.dispatch-feed { display: flex; flex-direction: column; gap: 8px; }
.dispatch-row { display: grid; grid-template-columns: minmax(190px, 1fr) minmax(260px, 1.5fr) minmax(190px, 0.8fr) 110px; align-items: center; gap: 22px; padding: 15px 16px; border: 1px solid var(--line); border-left: 4px solid var(--blue); border-radius: 10px; background: white; }
.dispatch-row.is-failed { border-left-color: var(--danger); background: #fffafb; }
.dispatch-destination, .dispatch-content, .dispatch-delivery { min-width: 0; }
.dispatch-destination strong { display: block; margin-bottom: 5px; overflow: hidden; font-size: 0.88rem; text-overflow: ellipsis; white-space: nowrap; }
.dispatch-destination code, .dispatch-content small { display: block; overflow: hidden; color: var(--muted); font-family: Consolas, monospace; font-size: 0.68rem; text-overflow: ellipsis; white-space: nowrap; }
.dispatch-content p { display: -webkit-box; margin: 0 0 5px; overflow: hidden; line-height: 1.35; overflow-wrap: anywhere; -webkit-box-orient: vertical; -webkit-line-clamp: 2; }
.dispatch-media-thumb { display: block; width: 100%; max-width: 140px; max-height: 90px; margin-bottom: 6px; border: 1px solid var(--line); border-radius: 8px; object-fit: cover; }
.dispatch-media-link { display: inline-block; margin-bottom: 6px; color: var(--blue); font-size: 0.78rem; font-weight: 700; text-decoration: none; }
.dispatch-row time { color: var(--muted); font-family: Consolas, monospace; font-size: 0.68rem; text-align: right; }
.delivery-badge { display: inline-block; margin-bottom: 8px; padding: 4px 7px; border-radius: 5px; color: var(--blue); background: #e9f0ff; font-family: Consolas, monospace; font-size: 0.66rem; font-weight: 700; letter-spacing: 0.04em; text-transform: uppercase; }
.status-delivered, .status-read, .status-played { color: var(--success); background: #e4f5f0; }
.status-failed { color: var(--danger); background: #fdecef; }
.delivery-progress { display: grid; grid-template-columns: repeat(4, 1fr); gap: 3px; }
.delivery-progress i { height: 3px; border-radius: 99px; background: #dfe6f0; }
.delivery-progress i.active { background: var(--blue); }
.delivery-progress.failed i { background: #efd7db; }
.dispatch-error { display: block; max-width: 250px; margin-top: 7px; overflow: hidden; color: var(--danger); font-size: 0.68rem; text-overflow: ellipsis; white-space: nowrap; }
.history-pagination { display: flex; justify-content: center; margin-top: 14px; }
.history-pagination .secondary { width: auto; min-width: 150px; margin: 0; }
.history-pagination .secondary[hidden] { display: none; }

.management-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 22px; }
.management-card { min-width: 0; padding: 20px; border: 1px solid var(--line); border-radius: 14px; background: white; }
.management-card > label:first-of-type { margin-top: 0; }
.management-card textarea { min-height: 92px; }
.management-card .primary { margin-top: 18px; }
.directory-lists, .admin-lists { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 22px; margin-top: 28px; }
h3 { margin: 0 0 12px; font-size: 1rem; }
.record-list { display: flex; flex-direction: column; gap: 8px; }
.record-row { display: flex; align-items: center; justify-content: space-between; gap: 14px; padding: 12px 14px; border: 1px solid var(--line); border-radius: 10px; background: white; }
.record-row > div { min-width: 0; }
.record-row strong, .record-row small { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.record-row small { margin-top: 3px; color: var(--muted); font-family: Consolas, monospace; font-size: .68rem; }
.record-actions { display: flex; gap: 6px; }
.record-actions button { padding: 7px 9px; color: var(--blue); border: 1px solid var(--line); background: white; font-size: .7rem; }
.compact-picker { max-height: 160px; padding: 8px; border: 1px solid var(--line); border-radius: 10px; background: #f9fbff; overflow-y: auto; }
.compact-picker label { display: flex; align-items: center; gap: 8px; margin: 0; padding: 6px; font-size: .76rem; }
.compact-picker input { width: 16px; min-height: 16px; }
.segment-dispatch { display: grid; grid-template-columns: 1fr 1fr auto; align-items: end; gap: 12px; margin-top: 28px; padding: 18px; border: 1px solid #aec3e4; border-radius: 14px; background: #edf4ff; }
.segment-dispatch label { margin-top: 0; }
.segment-dispatch .primary { width: auto; min-height: 46px; }
.segment-dispatch .feedback { grid-column: 1 / -1; margin: 0; }
.template-layout { grid-template-columns: minmax(320px,.8fr) minmax(0,1.2fr); }
.token-reveal { display: block; margin-top: 14px; padding: 12px; color: var(--ink); border: 1px dashed var(--blue); border-radius: 8px; background: #edf4ff; font-family: Consolas, monospace; font-size: .72rem; overflow-wrap: anywhere; }
.audit-list { max-height: 420px; overflow-y: auto; }
.error-dialog { width: min(720px, calc(100% - 32px)); padding: 26px; border: 1px solid var(--line); border-radius: 18px; color: var(--ink); background: var(--paper); box-shadow: 0 30px 80px rgba(18,35,63,.28); }
.error-dialog::backdrop { background: rgba(18,35,63,.5); backdrop-filter: blur(3px); }
.dialog-heading { display: flex; justify-content: space-between; gap: 16px; }
.error-dialog dl { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin: 22px 0; font-size: .78rem; }
.error-dialog dt { color: var(--muted); font-weight: 700; }
.error-dialog dd { margin: 0; overflow-wrap: anywhere; }
.error-dialog pre { max-height: 300px; padding: 16px; border: 1px solid var(--line); border-radius: 10px; background: white; overflow: auto; white-space: pre-wrap; word-break: break-word; }
.error-detail-button { margin-top: 8px; padding: 5px 8px; color: var(--danger); border: 1px solid #efc5cb; background: white; font-size: .68rem; }

@keyframes pulse { 0% { box-shadow: 0 0 0 0 rgba(22, 133, 107, 0.45); } 70% { box-shadow: 0 0 0 8px rgba(22, 133, 107, 0); } 100% { box-shadow: 0 0 0 0 rgba(22, 133, 107, 0); } }

@media (max-width: 1100px) {
  .section-nav { margin-right: 34px; margin-left: 34px; }
  .send-layout { grid-template-columns: 1fr; }
  .management-grid, .template-layout { grid-template-columns: 1fr; }
  .dispatch-row { grid-template-columns: minmax(180px, 1fr) minmax(240px, 1.4fr) minmax(170px, 0.8fr); }
  .dispatch-row time { grid-column: 1 / -1; text-align: left; }
}

@media (max-width: 720px) {
  .topbar { align-items: flex-start; padding: 28px 18px 20px; }
  .topbar-session { align-items: flex-end; flex-direction: column; }
  .user-chip { display: none; }
  .topbar-copy { display: none; }
  .section-nav { grid-template-columns: repeat(4, minmax(118px, 1fr)); margin: 0 18px 14px; overflow-x: auto; }
  .section-nav a { padding: 11px 12px; white-space: nowrap; }
  .console-sections { padding: 0 18px 28px; }
  .panel { min-height: auto; padding: 22px; }
  .connection-panel { display: flex; align-items: stretch; flex-direction: column; }
  .connection-panel .qr-stage { order: 3; margin-bottom: 18px; }
  .connection-panel .primary { order: 4; max-width: none; }
  .connection-panel .microcopy { order: 5; max-width: none; }
  .send-column { padding: 16px; }
  .directory-lists, .admin-lists { grid-template-columns: 1fr; }
  .segment-dispatch { grid-template-columns: 1fr; }
  .segment-dispatch .primary { width: 100%; }
  .panel-heading.split { flex-direction: column; }
  .status-chip strong { display: none; }
  .status-legend { justify-content: flex-start; margin: -8px 0 20px; overflow-x: auto; }
  .dispatch-row { grid-template-columns: 1fr; gap: 12px; }
  .dispatch-row time { grid-column: auto; }
  .manual-group-fields { grid-template-columns: 1fr; }
  .group-picker-toolbar { grid-template-columns: 1fr 1fr; }
  .group-picker-toolbar input { grid-column: 1 / -1; }
  .group-picker-footer { align-items: flex-start; flex-direction: column; }
}

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