:root {
  --bg: #f4f6fb;
  --card: #ffffff;
  --text: #1c2333;
  --muted: #6b7489;
  --line: #e3e7f0;
  --primary: #3b5bdb;
  --primary-dark: #2f49b3;
  --ok: #2b8a3e;
  --warn: #c77700;
  --danger: #c92a2a;
  --radius: 12px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
}

a { color: var(--primary); text-decoration: none; }

/* ---------- Header ---------- */
.topbar {
  background: #1c2333;
  color: #fff;
  padding: 0 24px;
  height: 56px;
  display: flex;
  align-items: center;
  gap: 24px;
}
.topbar .brand { font-weight: 700; font-size: 17px; color: #fff; }
.topbar nav { display: flex; gap: 4px; flex: 1; }
.topbar nav a {
  color: #c5cbe0; padding: 6px 12px; border-radius: 8px; font-size: 14px;
}
.topbar nav a:hover, .topbar nav a.active { background: #2e3850; color: #fff; }
.mode-badge {
  font-size: 12px; font-weight: 700; padding: 4px 10px; border-radius: 999px;
  background: #ffe8b3; color: #6b4500;
}
.mode-badge.live { background: #c3fae8; color: #0b5d45; }
.mode-badge.real { background: #ffc9c9; color: #7d1a1a; }
.logout-btn {
  background: none; border: 1px solid #3a4560; color: #c5cbe0; border-radius: 8px; padding: 5px 10px; font-size: 13px; cursor: pointer;
}
.logout-btn:hover { background: #2e3850; color: #fff; }

/* ---------- Layout ---------- */
main { max-width: 760px; margin: 32px auto; padding: 0 16px; }
main.wide { max-width: 1100px; }
h1 { font-size: 24px; margin: 0 0 4px; }
.subtitle { color: var(--muted); margin: 0 0 24px; }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 24px;
  margin-bottom: 20px;
  box-shadow: 0 1px 2px rgba(16, 24, 40, .04);
}
.card h2 { font-size: 16px; margin: 0 0 16px; }

/* ---------- Menu ---------- */
.menu-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 16px;
}
.menu-tile {
  display: block;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 28px 24px;
  color: var(--text);
  transition: transform .1s, box-shadow .1s, border-color .1s;
}
.menu-tile:hover {
  transform: translateY(-2px);
  border-color: var(--primary);
  box-shadow: 0 6px 18px rgba(59, 91, 219, .12);
}
.menu-tile .icon { font-size: 30px; margin-bottom: 12px; }
.menu-tile .title { font-size: 18px; font-weight: 700; }
.menu-tile .desc { color: var(--muted); font-size: 14px; margin-top: 4px; }

/* ---------- Forms ---------- */
.field { margin-bottom: 18px; }
.field label { display: block; font-weight: 600; font-size: 14px; margin-bottom: 6px; }
.field .hint { color: var(--muted); font-size: 12px; margin-top: 4px; }
.field .hint.error { color: var(--danger); }
input[type=text], input[type=number], input[type=email], input[type=tel], input[type=password], textarea, select {
  width: 100%;
  font: inherit;
  padding: 10px 12px;
  border: 1px solid #cfd5e3;
  border-radius: 8px;
  background: #fff;
}
input:focus, textarea:focus, select:focus {
  outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(59, 91, 219, .15);
}
textarea { resize: vertical; min-height: 90px; }
.row { display: flex; gap: 12px; }
.row > * { flex: 1; }

.amount-input { position: relative; }
.amount-input span {
  position: absolute; left: 12px; top: 50%; transform: translateY(-50%);
  color: var(--muted); font-weight: 600;
}
.amount-input input { padding-left: 60px; font-size: 22px; font-weight: 600; }

.counter { text-align: right; font-size: 12px; color: var(--muted); margin-top: 4px; }
.counter.over { color: var(--danger); font-weight: 700; }

/* Segmented method selector */
.methods { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.methods label {
  border: 1px solid #cfd5e3; border-radius: 10px; padding: 14px 10px;
  text-align: center; cursor: pointer; font-weight: 600; font-size: 14px;
  background: #fff;
}
.methods label .m-icon { display: block; font-size: 22px; margin-bottom: 4px; }
.methods input { display: none; }
.methods input:checked + label {
  border-color: var(--primary); background: #eef2ff; color: var(--primary-dark);
  box-shadow: 0 0 0 2px rgba(59, 91, 219, .2);
}

.subchoice { display: flex; gap: 20px; margin: 6px 0 14px; }
.subchoice label { font-weight: 500; display: flex; align-items: center; gap: 6px; cursor: pointer; }

.method-panel {
  border: 1px dashed #cfd5e3; border-radius: 10px; padding: 16px; margin-top: 14px;
  background: #fafbfe;
}
.placeholder-box {
  border: 1px dashed #9aa5c4; border-radius: 8px; padding: 14px; color: var(--muted);
  font-size: 14px; background: #fff; text-align: center;
}
.checkbox { display: flex; gap: 8px; font-size: 13px; color: var(--muted); align-items: flex-start; }
.checkbox input { margin-top: 3px; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-block; font: inherit; font-weight: 600; cursor: pointer;
  padding: 11px 20px; border-radius: 8px; border: 1px solid transparent;
  background: var(--primary); color: #fff; text-align: center;
}
.btn:hover { background: var(--primary-dark); }
.btn:disabled { background: #a8b4dd; cursor: not-allowed; }
.btn.secondary { background: #fff; color: var(--text); border-color: #cfd5e3; }
.btn.secondary:hover { background: #f1f3f9; }
.btn.small { padding: 6px 12px; font-size: 13px; }
.btn.block { width: 100%; }
.actions { display: flex; gap: 12px; justify-content: flex-end; margin-top: 8px; }

/* ---------- Breakdown ---------- */
.breakdown { width: 100%; border-collapse: collapse; }
.breakdown td { padding: 10px 0; border-bottom: 1px solid var(--line); }
.breakdown td:last-child { text-align: right; font-variant-numeric: tabular-nums; }
.breakdown tr.fee td { color: var(--danger); }
.breakdown tr.total td { font-weight: 700; font-size: 18px; border-bottom: none; color: var(--ok); }
.breakdown tr.head td { font-weight: 700; font-size: 18px; }
.kv { display: grid; grid-template-columns: 140px 1fr; gap: 6px 12px; font-size: 14px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }

.notice {
  background: #fff8e6; border: 1px solid #ffe2a3; color: #6b4500;
  border-radius: 8px; padding: 10px 14px; font-size: 13px; margin-bottom: 20px;
}
.notice.info { background: #eef2ff; border-color: #cdd7ff; color: #2f3f8f; }
.notice.ok { background: #ebfbee; border-color: #b2f2bb; color: #1b5e20; }

/* ---------- Simulated reader ---------- */
.reader-wrap { display: flex; gap: 24px; align-items: center; }
.reader {
  width: 170px; flex-shrink: 0; background: #20242e; border-radius: 22px; padding: 14px;
  box-shadow: 0 10px 24px rgba(0, 0, 0, .25);
}
.reader .screen {
  background: #e9f1ff; border-radius: 10px; height: 170px; display: flex; flex-direction: column;
  align-items: center; justify-content: center; text-align: center; padding: 10px;
}
.reader .screen .r-amount { font-size: 22px; font-weight: 700; }
.reader .screen .r-msg { font-size: 12px; color: var(--muted); margin-top: 6px; }
.reader .screen .r-icon { font-size: 30px; }
.reader .label { color: #8d95a8; font-size: 11px; text-align: center; margin-top: 10px; }
.status-dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; background: var(--ok); margin-right: 6px; }
.status-dot.off { background: var(--danger); }
.steps { list-style: none; padding: 0; margin: 0; font-size: 14px; }
.steps li { padding: 6px 0; color: var(--muted); }
.steps li.done { color: var(--ok); }
.steps li.current { color: var(--text); font-weight: 700; }

/* ---------- History table ---------- */
table.list { width: 100%; border-collapse: collapse; font-size: 14px; }
table.list th {
  text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: .04em;
  color: var(--muted); padding: 10px 8px; border-bottom: 1px solid var(--line);
}
table.list td { white-space: nowrap; padding: 12px 8px; border-bottom: 1px solid var(--line); vertical-align: middle; }
table.list td.num, table.list th.num { text-align: right; font-variant-numeric: tabular-nums; }
table.list td.fee { color: var(--danger); }
table.list td.net { font-weight: 700; color: var(--ok); }
.tag { display: inline-block; font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 999px; background: #eef2ff; color: var(--primary-dark); }
.tag.pos { background: #fff0f6; color: #a61e4d; }
.tag.ok { background: #ebfbee; color: var(--ok); }
.summary { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 20px; }
.summary .card { margin: 0; padding: 16px; }
.summary .s-label { color: var(--muted); font-size: 12px; }
.summary .s-value { font-size: 20px; font-weight: 700; }

/* ---------- Tax invoice ---------- */
.invoice {
  background: #fff; border: 1px solid var(--line); border-radius: 4px;
  padding: 48px; max-width: 760px; margin: 0 auto;
}
.invoice .inv-head { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 36px; }
.invoice .inv-title { font-size: 32px; font-weight: 800; letter-spacing: .02em; margin: 0; }
.invoice .merchant-name { font-size: 18px; font-weight: 700; }
.invoice .muted { color: var(--muted); font-size: 13px; }
.invoice .meta { text-align: right; font-size: 14px; }
.invoice .meta div { margin-bottom: 4px; }
.invoice table { width: 100%; border-collapse: collapse; margin-top: 12px; font-size: 14px; }
.invoice th { text-align: left; border-bottom: 2px solid var(--text); padding: 8px 0; font-size: 12px; text-transform: uppercase; }
.invoice td { padding: 12px 0; border-bottom: 1px solid var(--line); vertical-align: top; }
.invoice .num { text-align: right; font-variant-numeric: tabular-nums; }
.invoice .totals { margin-left: auto; width: 300px; margin-top: 18px; }
.invoice .totals div { display: flex; justify-content: space-between; padding: 6px 0; font-size: 14px; }
.invoice .totals .grand { border-top: 2px solid var(--text); font-weight: 800; font-size: 17px; padding-top: 10px; margin-top: 4px; }
.invoice .paid { margin-top: 32px; font-size: 14px; }
.invoice .paid-stamp { display: inline-block; border: 2px solid var(--ok); color: var(--ok); font-weight: 800; padding: 4px 12px; border-radius: 6px; letter-spacing: .1em; }
.invoice-toolbar { max-width: 760px; margin: 0 auto 16px; display: flex; justify-content: space-between; align-items: center; }

@media print {
  .topbar, .invoice-toolbar, .no-print { display: none !important; }
  body { background: #fff; }
  main { margin: 0; max-width: none; }
  .invoice { border: none; padding: 0; }
}

@media (max-width: 640px) {
  .methods { grid-template-columns: 1fr; }
  .summary { grid-template-columns: repeat(2, 1fr); }
  .reader-wrap { flex-direction: column; }

  .topbar { flex-wrap: wrap; height: auto; padding: 8px 12px 0; row-gap: 4px; }
  .topbar nav { order: 3; flex-basis: 100%; margin: 0 -4px; padding-bottom: 6px; }
  .topbar .brand { flex: 1; }
  .topbar nav { overflow-x: auto; scrollbar-width: none; }
  .topbar nav a { white-space: nowrap; padding: 6px 8px; }
  .mode-badge { font-size: 10px; padding: 3px 7px; }
  .invoice { padding: 24px 18px; }
  .invoice .inv-head { flex-direction: column; gap: 16px; }
  .invoice .inv-title { font-size: 26px; }
  .invoice .meta { text-align: left; }
  .invoice .totals { width: 100%; }
  .invoice-toolbar { flex-wrap: wrap; gap: 10px; }
  main { margin: 20px auto; }
}

.small-muted { font-size: 12px; color: var(--muted); margin: 10px 0 0; }
.report-bar { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; }
.report-bar input[type=month] { padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; font: inherit; }
body.no-scroll { overflow: hidden; }

/* ---------- Full-screen payment overlay ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 1000;
  display: grid; grid-template-columns: 1fr 1.15fr;
  background: #0f1420; color: #fff;
  transition: background .4s;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  user-select: none; -webkit-user-select: none; -webkit-tap-highlight-color: transparent;
}
.overlay.state-approved { background: #0c3a1f; }
.overlay.state-declined { background: #3d0f12; }

.ov-info {
  display: flex; flex-direction: column; justify-content: center;
  padding: 5vh 5vw; border-right: 1px solid rgba(255, 255, 255, .08);
}
.ov-merchant { font-size: clamp(14px, 2.2vw, 22px); color: #aab3c8; font-weight: 600; margin-bottom: 6vh; }
.ov-label { font-size: clamp(13px, 1.8vw, 18px); color: #aab3c8; text-transform: uppercase; letter-spacing: .12em; }
.ov-amount { font-size: clamp(44px, 8vw, 104px); font-weight: 800; line-height: 1.05; margin: 8px 0 18px; font-variant-numeric: tabular-nums; }
.ov-desc { font-size: clamp(14px, 1.8vw, 20px); color: #d3d9e8; max-height: 30vh; overflow: hidden; }

.ov-stage {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center; padding: 4vh 4vw;
}
.ov-visual { min-height: 230px; display: flex; align-items: center; justify-content: center; margin-bottom: 18px; }
.ov-msg { font-size: clamp(26px, 4.2vw, 52px); font-weight: 800; }
.ov-sub { font-size: clamp(14px, 1.9vw, 20px); color: #b9c2d6; margin-top: 8px; min-height: 1.4em; max-width: 520px; }
.ov-actions { display: flex; flex-wrap: wrap; gap: 12px; justify-content: center; margin-top: 4vh; }
.ov-btn {
  font: inherit; font-weight: 700; font-size: clamp(15px, 1.8vw, 19px);
  min-height: 54px; padding: 12px 26px; border-radius: 14px; cursor: pointer;
  background: rgba(255, 255, 255, .1); color: #fff; border: 1px solid rgba(255, 255, 255, .25);
}
.ov-btn.primary { background: #fff; color: #0f1420; border-color: #fff; }
.ov-btn.ghost { background: transparent; border-color: transparent; color: #aab3c8; }
.ov-btn:active { transform: scale(.97); }

/* Contactless tap target with pulsing rings */
.tap-target {
  position: relative; width: 210px; height: 210px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  background: #3b5bdb; color: #fff; cursor: pointer;
  box-shadow: 0 0 0 10px rgba(59, 91, 219, .25);
}
.tap-target svg { position: relative; z-index: 1; }
.tap-target .ring {
  position: absolute; inset: 0; border-radius: 50%; border: 3px solid #6f8cff;
  animation: pulse 2.4s ease-out infinite; opacity: 0;
}
.tap-target .r2 { animation-delay: .8s; }
.tap-target .r3 { animation-delay: 1.6s; }
@keyframes pulse { 0% { transform: scale(1); opacity: .8; } 100% { transform: scale(1.5); opacity: 0; } }
.state-present .ov-visual { margin: 40px 0 56px; }

.spinner {
  width: 140px; height: 140px; border-radius: 50%;
  border: 12px solid rgba(255, 255, 255, .15); border-top-color: #6f8cff;
  animation: spin .9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.result {
  width: 190px; height: 190px; border-radius: 50%; display: flex; align-items: center; justify-content: center;
  animation: pop .45s cubic-bezier(.2, 1.6, .4, 1);
}
.result.ok { background: #2fb344; box-shadow: 0 0 0 14px rgba(47, 179, 68, .25); }
.result.bad { background: #e03131; box-shadow: 0 0 0 14px rgba(224, 49, 49, .25); }
.result svg { width: 120px; height: 120px; fill: none; stroke: #fff; stroke-width: 5; stroke-linecap: round; stroke-linejoin: round; }
.result.ok svg path { stroke-dasharray: 40; stroke-dashoffset: 40; animation: draw .4s .25s forwards; }
@keyframes pop { from { transform: scale(.3); opacity: 0; } to { transform: scale(1); opacity: 1; } }
@keyframes draw { to { stroke-dashoffset: 0; } }

.ov-emoji { font-size: 110px; animation: bob 1.6s ease-in-out infinite; }
@keyframes bob { 50% { transform: translateY(-10px); } }

.qr-box { background: #fff; padding: 16px 16px 10px; border-radius: 16px; }
.qr { display: grid; grid-template-columns: repeat(25, 8px); }
.qr i { width: 8px; height: 8px; display: block; }
.qr i.on { background: #111; }
.qr-brand { color: #1aad19; font-weight: 800; margin-top: 8px; }

/* Portrait / phones: stack info above stage */
@media (max-aspect-ratio: 1/1) {
  .overlay { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
  .ov-info { border-right: none; border-bottom: 1px solid rgba(255, 255, 255, .08); padding: 4vh 6vw 3vh; text-align: center; }
  .ov-merchant { margin-bottom: 2vh; }
  .ov-desc { max-height: 12vh; }
  .ov-amount { font-size: clamp(44px, 13vw, 96px); margin-bottom: 8px; }
  .tap-target { width: 180px; height: 180px; }
}
/* Short landscape screens (phones turned sideways) */
@media (max-height: 500px) and (min-aspect-ratio: 1/1) {
  .ov-visual { min-height: 0; transform: scale(.7); margin: -30px 0 -20px; }
  .ov-actions { margin-top: 12px; }
  .state-present .ov-visual { margin: -20px 0 -10px; }
  .ov-btn { min-height: 44px; padding: 8px 16px; }
}

table.list td.wrap { white-space: normal; }

/* History as stacked cards on phones */
@media (max-width: 640px) {
  .history-card { padding: 0 !important; background: transparent; border: none; box-shadow: none; }
  table.list thead { display: none; }
  table.list, table.list tbody, table.list tr, table.list td { display: block; width: 100%; }
  table.list tr {
    background: #fff; border: 1px solid var(--line); border-radius: var(--radius);
    padding: 10px 14px; margin-bottom: 12px;
  }
  table.list td {
    display: flex; justify-content: space-between; gap: 12px; text-align: right !important;
    padding: 6px 0; border-bottom: 1px dashed var(--line); white-space: normal; min-width: 0 !important;
  }
  table.list td::before { content: attr(data-label); color: var(--muted); font-size: 12px; text-align: left; flex-shrink: 0; }
  table.list td.pdf-cell { border-bottom: none; padding-top: 10px; }
  table.list td.pdf-cell::before { display: none; }
  table.list td.pdf-cell .btn { width: 100%; }
}

/* ---------- v1.2: invoice contact block, logo, bill to, send ---------- */
.toolbar-actions { display: flex; gap: 10px; flex-wrap: wrap; justify-content: flex-end; }
.invoice .inv-logo { display: block; max-width: 220px; max-height: 72px; margin-bottom: 12px; object-fit: contain; }
.invoice .contact { white-space: pre-line; margin-top: 6px; line-height: 1.5; }
.invoice .inv-right { text-align: right; }
.invoice .inv-right .meta { margin-top: 20px; }
.invoice .bill-to { margin: -8px 0 24px; font-size: 14px; }
.invoice .bill-to .label { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin-bottom: 2px; }
.invoice .bill-to .name { font-weight: 700; }
.row-actions { display: flex; gap: 6px; }

.logo-preview {
  display: flex; align-items: center; justify-content: center; min-height: 90px; padding: 12px;
  border: 1px dashed #cfd5e3; border-radius: 8px; background: #fafbfe; margin-bottom: 12px; color: var(--muted); font-size: 13px;
}
.logo-preview img { max-width: 260px; max-height: 80px; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); z-index: 2000;
  background: #1c2333; color: #fff; padding: 12px 18px; border-radius: 10px; font-size: 14px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .2); opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s;
  max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 640px) {
  .invoice .inv-right { text-align: left; }
  .invoice .inv-right .meta { margin-top: 8px; }
  .toolbar-actions { width: 100%; justify-content: stretch; }
  .toolbar-actions .btn { flex: 1 1 40%; padding: 11px 8px; white-space: nowrap; font-size: 15px; }
  .row-actions { width: 100%; }
  .row-actions .btn { flex: 1; }
}
.card > .hint { font-size: 12px; color: var(--muted); margin-top: 8px; }

/* Bill to: full-width boxes on phones so the example placeholders fit; note stays on one line */
.billto-note { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
@media (max-width: 640px) {
  .billto-row { flex-direction: column; gap: 10px; }
  .card { padding: 18px 16px; }
  /* Same 12px as other hints where it fits, a little smaller on the narrowest phones (iPhone SE), so the
     whole sentence stays on one line. (.field .billto-note: must beat the general .field .hint size) */
  .field .billto-note { font-size: min(12px, calc((100vw - 66px) / 27)); }
}

/* Online WeChat Pay: Stripe's real QR code (test mode) */
.stripe-qr { display: block; width: 240px; height: 240px; image-rendering: pixelated; margin: 0 auto; }
