:root {
  --navy: #0b1f3a;
  --navy-2: #13315c;
  --gold: #f2b705;
  --bg: #e9edf3;
  --card: #ffffff;
  --ink: #14202e;
  --muted: #64748b;
  --line: #dde3ea;
  --ok: #0f8a5f;
  --ok-bg: #e5f6ee;
  --bad: #c62828;
  --bad-bg: #fdecec;
  --warn: #a15c00;
  --warn-bg: #fff4e0;
  --wa-head: #008069;
  --wa-bg: #efeae2;
  --wa-out: #d9fdd3;
  --wa-in: #ffffff;
  --trader: #008069;
  --customer: #6d4bd8;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--ink);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); }
button, input { font: inherit; }
.muted { color: var(--muted); font-size: 13px; }

/* ---------- Stage layout ---------- */
.stage {
  --phone-h: min(calc(100vh - 92px), 760px);
  --phone-w: calc(var(--phone-h) * 0.47);
  display: grid;
  grid-template-columns: var(--phone-w) minmax(0, 1fr) var(--phone-w);
  gap: 20px;
  height: 100vh;
  padding: 14px 20px;
  overflow: hidden;
}
body.single .stage { grid-template-columns: minmax(0, 1fr); justify-items: center; }
body.single [data-view]:not(.active) { display: none; }
body.single .dash { width: 100%; }

.device-col { display: flex; flex-direction: column; align-items: center; gap: 8px; min-width: 0; }
.device-label { font-weight: 700; font-size: 14px; letter-spacing: .02em; text-transform: uppercase; color: var(--navy); display: flex; align-items: center; gap: 8px; }
.device-meta { font-size: 12px; color: var(--muted); text-align: center; min-height: 16px; }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.dot-trader { background: var(--trader); }
.dot-customer { background: var(--customer); }

/* ---------- Phone ---------- */
.phone {
  width: var(--phone-w);
  height: var(--phone-h);
  background: #111;
  border-radius: 38px;
  padding: 10px;
  box-shadow: 0 20px 50px rgba(11, 31, 58, .28), inset 0 0 0 2px #333;
  flex: none;
}
.screen {
  position: relative;
  width: 100%; height: 100%;
  border-radius: 30px;
  overflow: hidden;
  display: flex; flex-direction: column;
  background: var(--wa-bg);
}
.status-bar { display: flex; justify-content: space-between; padding: 8px 20px 4px; font-size: 12px; font-weight: 600; background: var(--wa-head); color: #fff; }
.sb-icons { letter-spacing: 1px; opacity: .9; }
.wa-head { display: flex; align-items: center; gap: 10px; padding: 8px 12px 10px; background: var(--wa-head); color: #fff; }
.avatar { width: 36px; height: 36px; border-radius: 50%; background: var(--gold); color: var(--navy); display: grid; place-items: center; font-weight: 800; flex: none; }
.wa-title { display: flex; flex-direction: column; line-height: 1.2; min-width: 0; }
.wa-title strong { font-size: 15px; }
.wa-title small { font-size: 11.5px; opacity: .85; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.verified { display: inline-grid; place-items: center; width: 15px; height: 15px; border-radius: 50%; background: #25d366; color: #fff; font-size: 9px; vertical-align: 1px; }

.chat {
  flex: 1;
  overflow-y: auto;
  padding: 10px 10px 4px;
  display: flex; flex-direction: column; gap: 4px;
  background-color: var(--wa-bg);
  background-image: radial-gradient(rgba(0,0,0,.035) 1px, transparent 1px);
  background-size: 14px 14px;
  scrollbar-width: thin;
}
.day-chip { align-self: center; background: #fff; color: var(--muted); font-size: 11px; padding: 3px 10px; border-radius: 8px; margin: 4px 0 6px; box-shadow: 0 1px 1px rgba(0,0,0,.06); }
.msg {
  max-width: 86%;
  padding: 6px 8px 4px 9px;
  border-radius: 9px;
  font-size: 13.5px;
  line-height: 1.38;
  box-shadow: 0 1px 1px rgba(0,0,0,.09);
  white-space: pre-wrap;
  word-wrap: break-word;
  animation: pop .22s ease-out;
}
.msg.in { align-self: flex-start; background: var(--wa-in); border-top-left-radius: 2px; }
.msg.out { align-self: flex-end; background: var(--wa-out); border-top-right-radius: 2px; }
.msg.agent { border-left: 3px solid #e67e22; }
.msg .who { display: block; font-size: 12px; font-weight: 700; color: #e67e22; margin-bottom: 2px; }
.msg .meta { display: block; text-align: right; font-size: 10.5px; color: #667781; margin-top: 2px; }
.msg.out .meta::after { content: " ✓✓"; color: #53bdeb; }
.msg .opts { display: block; margin-top: 4px; }
.msg .opt-line { display: block; }
.msg .opt-line b { display: inline-block; min-width: 18px; color: var(--wa-head); }
@keyframes pop { from { opacity: 0; transform: translateY(6px) scale(.98); } to { opacity: 1; transform: none; } }

.typing { align-self: flex-start; background: #fff; border-radius: 9px; padding: 9px 12px; display: flex; gap: 4px; box-shadow: 0 1px 1px rgba(0,0,0,.09); }
.typing i { width: 6px; height: 6px; border-radius: 50%; background: #8696a0; animation: blink 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: .2s; }
.typing i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 60%, 100% { opacity: .3; transform: translateY(0); } 30% { opacity: 1; transform: translateY(-3px); } }

.chips { display: flex; flex-wrap: wrap; gap: 6px; padding: 4px 10px 6px; background: var(--wa-bg); }
.chips:empty { display: none; }
.chip {
  border: 0; background: #fff; color: var(--wa-head); font-weight: 600; font-size: 12.5px;
  padding: 7px 11px; border-radius: 16px; cursor: pointer; box-shadow: 0 1px 1px rgba(0,0,0,.1);
  transition: transform .1s, background .15s;
}
.chip:hover { background: #f0fdf4; }
.chip:active { transform: scale(.96); }
.chip b { margin-right: 4px; }

.composer { display: flex; gap: 6px; padding: 6px 8px 12px; background: var(--wa-bg); }
.composer input { flex: 1; min-width: 0; border: 0; border-radius: 20px; padding: 9px 14px; font-size: 14px; outline: none; background: #fff; }
.composer button { width: 40px; height: 40px; flex: none; border: 0; border-radius: 50%; background: var(--wa-head); color: #fff; cursor: pointer; font-size: 16px; }

/* Receipt + payment cards inside chat */
.receipt {
  margin-top: 6px; background: #fff; border: 1px solid var(--line); border-radius: 8px; overflow: hidden;
  white-space: normal; font-size: 12.5px;
}
.receipt-head { background: var(--navy); color: #fff; padding: 7px 10px; display: flex; justify-content: space-between; align-items: center; }
.receipt-head strong { font-size: 12px; letter-spacing: .06em; }
.receipt-head span { color: var(--gold); font-weight: 700; font-size: 11px; }
.receipt-body { padding: 8px 10px; display: grid; grid-template-columns: auto 1fr; gap: 3px 10px; }
.receipt-body dt { color: var(--muted); }
.receipt-body dd { margin: 0; text-align: right; font-weight: 600; }
.receipt-total { grid-column: 1 / -1; display: flex; justify-content: space-between; border-top: 1px dashed var(--line); padding-top: 6px; margin-top: 3px; font-size: 14px; font-weight: 800; }
.receipt-foot { display: flex; gap: 10px; align-items: center; padding: 8px 10px; background: #f7f9fb; border-top: 1px solid var(--line); }
.receipt-foot img { width: 64px; height: 64px; flex: none; }
.receipt-code { font-family: ui-monospace, Menlo, monospace; font-weight: 800; font-size: 13px; color: var(--navy); }
.receipt-hash { font-family: ui-monospace, Menlo, monospace; font-size: 10.5px; color: var(--muted); word-break: break-all; }
.receipt-btn { display: block; width: 100%; border: 0; border-top: 1px solid var(--line); background: #fff; color: #0a7cff; font-weight: 700; padding: 9px; cursor: pointer; }
.receipt-btn:hover { background: #f2f7ff; }
.receipt.pay .receipt-head { background: var(--ok); }
.receipt.pay .receipt-head span { color: #fff; }

/* Payment overlay */
.pay-overlay { position: absolute; inset: 0; background: rgba(0,0,0,.55); display: flex; align-items: flex-end; animation: fade .2s; z-index: 5; }
.pay-overlay[hidden] { display: none; }
@keyframes fade { from { opacity: 0; } }
.pay-sheet { width: 100%; background: #fff; border-radius: 18px 18px 0 0; overflow: hidden; animation: up .25s ease-out; }
@keyframes up { from { transform: translateY(40%); } }
.pay-band { padding: 12px 16px; color: #fff; display: flex; justify-content: space-between; align-items: baseline; font-weight: 800; font-size: 17px; background: #e60000; }
.pay-band small { font-weight: 500; font-size: 12px; opacity: .9; }
.pay-band.mixx { background: linear-gradient(90deg, #ffd200, #00a0e3); color: #0b1f3a; }
.pay-band.airtel { background: #ed1c24; }
.pay-body { padding: 12px 16px 16px; text-align: center; }
.pay-to { font-size: 12.5px; color: var(--muted); }
.pay-to strong { color: var(--ink); }
.pay-amount { font-size: 26px; font-weight: 800; margin: 4px 0 0; }
.pay-ctrl { font-size: 12px; color: var(--muted); font-family: ui-monospace, Menlo, monospace; }
.pin-dots { display: flex; justify-content: center; gap: 12px; margin: 12px 0; }
.pin-dots i { width: 14px; height: 14px; border-radius: 50%; border: 2px solid var(--navy); }
.pin-dots i.on { background: var(--navy); }
.keypad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.keypad button { border: 0; background: #f1f4f8; border-radius: 10px; padding: 9px 0; font-size: 17px; font-weight: 600; cursor: pointer; }
.keypad button:active { background: #dde3ea; }
.pay-actions { display: flex; gap: 8px; margin-top: 10px; }
.pay-actions button { flex: 1; border: 0; border-radius: 10px; padding: 10px; font-weight: 700; cursor: pointer; }
.btn-ghost { background: #f1f4f8; }
.btn-pay { background: var(--ok); color: #fff; }
.btn-pay:disabled { opacity: .4; cursor: default; }

/* ---------- Dashboard ---------- */
.dash { display: flex; flex-direction: column; gap: 12px; min-width: 0; min-height: 0; }
.dash-head {
  display: flex; gap: 16px; justify-content: space-between; align-items: center;
  background: var(--navy); color: #fff; border-radius: 14px; padding: 12px 18px;
  border-bottom: 4px solid var(--gold);
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark { background: var(--gold); color: var(--navy); font-weight: 900; border-radius: 10px; padding: 8px 10px; letter-spacing: .05em; }
.brand h1 { font-size: 20px; margin: 0; }
.brand p { margin: 0; font-size: 12.5px; opacity: .8; }
.head-right { display: flex; align-items: center; gap: 14px; }
.sim-clock { font-size: 13px; opacity: .85; }
.chain-pill { font-weight: 800; padding: 7px 14px; border-radius: 20px; font-size: 14px; transition: background .3s; }
.chain-pill.ok { background: var(--ok); }
.chain-pill.bad { background: var(--bad); animation: pulse 1s infinite; }
@keyframes pulse { 50% { box-shadow: 0 0 0 6px rgba(198,40,40,.35); } }

.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
.kpi { background: var(--card); border-radius: 12px; padding: 12px 14px; display: flex; flex-direction: column; box-shadow: 0 1px 2px rgba(0,0,0,.05); border-top: 3px solid var(--navy-2); }
.kpi.gold { border-top-color: var(--gold); }
.kpi span { font-size: 12.5px; color: var(--muted); font-weight: 600; }
.kpi strong { font-size: clamp(17px, 1.55vw, 24px); white-space: nowrap; font-variant-numeric: tabular-nums; margin: 2px 0; }
.kpi em { font-style: normal; font-size: 11.5px; color: var(--muted); }
.kpi strong.bump { animation: bump .6s; }
@keyframes bump { 30% { color: var(--ok); transform: scale(1.06); } }

.dash-grid { flex: 1; min-height: 0; display: grid; grid-template-columns: minmax(0, 1.5fr) minmax(0, 1fr); gap: 12px; }
.card { background: var(--card); border-radius: 12px; padding: 12px 14px; box-shadow: 0 1px 2px rgba(0,0,0,.05); display: flex; flex-direction: column; min-height: 0; }
.card-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 8px; }
.card h2 { font-size: 15px; margin: 0; color: var(--navy); }
.side { display: flex; flex-direction: column; gap: 12px; min-height: 0; }
.tickets-card { flex: 1; overflow: hidden; }
#tickets { overflow-y: auto; min-height: 0; }

.alert { background: var(--bad-bg); color: var(--bad); border: 1px solid #f5c2c2; border-radius: 10px; padding: 9px 12px; font-size: 13px; margin-bottom: 8px; font-weight: 600; }

.ledger { overflow-y: auto; min-height: 0; display: flex; flex-direction: column; gap: 0; padding-right: 4px; scrollbar-width: thin; }
.block {
  position: relative; display: grid; grid-template-columns: 46px 1fr auto; gap: 2px 10px; align-items: center;
  border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; background: #fff;
  margin-bottom: 14px; transition: background .3s, border-color .3s;
}
.block::after { /* chain link to the next (older) block below */
  content: ""; position: absolute; left: 34px; bottom: -15px; width: 2px; height: 14px; background: var(--ok);
}
.block:last-child::after { display: none; }
.block.mine { border-color: #9bd8c2; background: #f3fbf7; }
.block.new { animation: drop .5s ease-out; }
@keyframes drop { from { opacity: 0; transform: translateY(-14px); background: #fff8d6; } }
.block.tampered { background: var(--bad-bg); border-color: var(--bad); }
.block.tampered::after, .block.untrusted::after { background: var(--bad); }
.block.untrusted { background: #fff8f8; border-color: #f0b4b4; }
.bidx { grid-row: span 2; font-family: ui-monospace, Menlo, monospace; font-weight: 800; color: var(--navy); font-size: 14px; text-align: center; background: #f1f4f8; border-radius: 8px; padding: 6px 0; }
.block.tampered .bidx { background: var(--bad); color: #fff; }
.bsum { font-size: 13px; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bsum .badge { font-size: 10.5px; font-weight: 800; padding: 2px 6px; border-radius: 5px; margin-right: 6px; letter-spacing: .03em; }
.badge.MAUZO { background: #e6f0ff; color: #1c4fb8; }
.badge.USAJILI { background: #efe7ff; color: #5b34c7; }
.badge.RITANI { background: #fff1d6; color: #8a5a00; }
.badge.MALIPO { background: var(--ok-bg); color: var(--ok); }
.badge.MWANZO { background: #eee; color: #555; }
.bhash { grid-column: 2; font-family: ui-monospace, Menlo, monospace; font-size: 11px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.block.tampered .bhash { white-space: normal; }
.bhash .bad { color: var(--bad); font-weight: 700; }
.bact { grid-row: span 2; display: flex; flex-direction: column; align-items: flex-end; gap: 4px; }
.bstatus.ok { display: none; }
.block:not(.mine) .bact button.tamper { opacity: 0; transition: opacity .15s; }
.block:hover .bact button.tamper { opacity: 1; }
.bstatus { font-size: 11px; font-weight: 700; }
.bstatus.ok { color: var(--ok); }
.bstatus.tampered, .bstatus.untrusted { color: var(--bad); }
.bact button { border: 1px solid var(--line); background: #fff; border-radius: 7px; font-size: 11.5px; padding: 3px 8px; cursor: pointer; }
.bact button.tamper { color: var(--bad); border-color: #f0b4b4; }
.bact button.fix { color: var(--ok); border-color: #9bd8c2; }
.btime { font-size: 11px; color: var(--muted); }

.verify-form { display: flex; gap: 8px; }
.verify-form input { flex: 1; min-width: 0; border: 1px solid var(--line); border-radius: 9px; padding: 9px 12px; font-family: ui-monospace, Menlo, monospace; font-size: 14px; text-transform: uppercase; }
.verify-form button, .controls button, .ticket button {
  border: 0; background: var(--navy); color: #fff; border-radius: 9px; padding: 8px 14px; font-weight: 700; cursor: pointer;
}
.vres { margin-top: 10px; border-radius: 10px; padding: 10px 12px; font-size: 13px; animation: pop .25s; }
.vres.ok { background: var(--ok-bg); color: #0b5f42; }
.vres.bad { background: var(--bad-bg); color: #8e1c1c; }
.vres.warn { background: var(--warn-bg); color: var(--warn); }
.vres strong { font-size: 15px; display: block; margin-bottom: 3px; }
.vres code { font-size: 11px; word-break: break-all; }

.judge-qr { display: flex; gap: 12px; align-items: center; margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--line); }
.judge-qr img { width: 96px; height: 96px; flex: none; border-radius: 6px; }
.judge-label { font-size: 18px; color: var(--navy); }
.judge-qr code { word-break: break-all; }
.judge-qr p { margin: 2px 0 4px; font-size: 12.5px; color: var(--muted); }
.judge-qr code { font-size: 11.5px; color: var(--navy-2); }

.ticket { border: 1px solid var(--line); border-radius: 10px; padding: 10px; margin-bottom: 8px; font-size: 13px; }
.ticket.closed { opacity: .55; }
.ticket-top { display: flex; justify-content: space-between; align-items: baseline; }
.ticket-top strong { color: var(--navy); }
.tstatus { font-size: 11px; font-weight: 800; padding: 2px 7px; border-radius: 5px; background: var(--warn-bg); color: var(--warn); }
.ticket.closed .tstatus { background: #eee; color: #666; }
.thist { display: flex; flex-wrap: wrap; gap: 4px 12px; font-size: 12px; color: var(--muted); margin: 4px 0 6px; padding: 6px 8px; background: #f7f9fb; border-radius: 7px; }
.thist b { color: var(--ink); }
.tthread { display: flex; flex-direction: column; gap: 4px; margin-bottom: 6px; }
.tthread div { padding: 5px 8px; border-radius: 7px; max-width: 90%; }
.tthread .trader { background: #f1f4f8; align-self: flex-start; }
.tthread .agent { background: #fff1e3; align-self: flex-end; }
.treply { display: flex; flex-wrap: wrap; gap: 6px; }
.treply input { flex-basis: 100%; }
.treply button { flex: 1; }
.treply input { flex: 1; min-width: 0; border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; }
.ticket button.close { background: #eef1f5; color: var(--ink); }
.empty { color: var(--muted); font-size: 13px; text-align: center; padding: 18px 8px; }

.controls { display: flex; align-items: center; gap: 8px; }
.controls button { background: var(--navy-2); font-size: 13px; }
.controls button.danger { background: #fff; color: var(--bad); border: 1px solid #f0b4b4; }
.conn { margin-left: auto; font-size: 12px; color: var(--ok); }
.conn.off { color: var(--bad); }

/* Narrow screens: stack panels and let the page scroll */
@media (max-width: 1100px) {
  .stage { grid-template-columns: 1fr; height: auto; overflow: visible; --phone-h: 680px; --phone-w: min(330px, calc(100vw - 32px)); padding: 16px; }
  .dash { order: -1; }
  .kpis { grid-template-columns: repeat(2, 1fr); }
  .dash-grid { grid-template-columns: 1fr; }
  .ledger { max-height: 480px; }
}
