:root {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #1e222b;
  --line: #2a2f3a;
  --text: #e8eaf0;
  --muted: #9aa3b2;
  --accent: #4f8cff;
  --accent-2: #2ecc8f;
  --danger: #ff5d5d;
  --radius: 12px;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f6fa;
    --panel: #ffffff;
    --panel-2: #f0f3f8;
    --line: #dfe4ec;
    --text: #16181d;
    --muted: #667085;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
}

a { color: var(--accent); }

.wrap {
  max-width: 940px;
  margin: 0 auto;
  padding: 20px 16px 60px;
}

header.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 18px;
  flex-wrap: wrap;
}

.brand {
  font-size: 19px;
  font-weight: 700;
  letter-spacing: -0.2px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.brand .dot {
  width: 10px; height: 10px;
  border-radius: 50%;
  background: var(--muted);
  transition: background .25s;
}
.brand .dot.live { background: var(--accent-2); box-shadow: 0 0 0 4px rgba(46,204,143,.18); }

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
  margin-bottom: 16px;
}

.card h2 {
  margin: 0 0 10px;
  font-size: 15px;
  font-weight: 600;
}

.muted { color: var(--muted); }
.small { font-size: 13px; }

button, .btn {
  font: inherit;
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--text);
  padding: 9px 14px;
  border-radius: 9px;
  cursor: pointer;
  transition: filter .15s, border-color .15s;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
button:hover, .btn:hover { filter: brightness(1.15); }
button:active { transform: translateY(1px); }
button:disabled { opacity: .5; cursor: not-allowed; }

button.primary, .btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
button.danger { color: var(--danger); }

input[type="text"], input[type="tel"] {
  font: inherit;
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 9px;
  padding: 9px 12px;
  width: 100%;
}
input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

/* ---------- Khu vực mã phòng trên PC ---------- */
.pair {
  display: flex;
  gap: 22px;
  align-items: center;
  flex-wrap: wrap;
}
.roomcode {
  font: 700 40px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
  letter-spacing: 6px;
}
#qr {
  background: #fff;
  padding: 10px;
  border-radius: 10px;
  line-height: 0;
}
#qr img, #qr canvas { display: block; width: 148px; height: 148px; }

/* ---------- Danh sách mã ---------- */
.toolbar {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: 12px;
}
.toolbar .spacer { flex: 1; }
.count { font-variant-numeric: tabular-nums; }

ul.codes { list-style: none; margin: 0; padding: 0; }

ul.codes li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel-2);
  margin-bottom: 8px;
}
ul.codes li.new { animation: pop .6s ease; }
@keyframes pop {
  from { background: rgba(79,140,255,.28); transform: translateY(-4px); }
  to   { background: var(--panel-2); transform: none; }
}

.val {
  flex: 1;
  min-width: 0;
  font: 15px/1.35 ui-monospace, SFMono-Regular, Menlo, monospace;
  word-break: break-all;
  cursor: pointer;
  user-select: all;
}
.meta { font-size: 12px; color: var(--muted); margin-top: 3px; user-select: none; }

.iconbtn {
  padding: 7px 10px;
  font-size: 13px;
  flex-shrink: 0;
}

.empty {
  text-align: center;
  padding: 34px 14px;
  color: var(--muted);
}

/* ---------- Thông báo nổi ---------- */
#toast {
  position: fixed;
  left: 50%;
  bottom: 26px;
  transform: translate(-50%, 20px);
  background: #101318;
  color: #fff;
  border: 1px solid var(--line);
  padding: 10px 18px;
  border-radius: 999px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  z-index: 50;
  font-size: 14px;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Màn hình quét trên điện thoại ---------- */
.scanner {
  position: relative;
  width: 100%;
  aspect-ratio: 3 / 4;
  max-height: 62vh;
  background: #000;
  border-radius: var(--radius);
  overflow: hidden;
}
.scanner video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.reticle {
  position: absolute;
  inset: 18% 8%;
  border: 2px solid rgba(255, 255, 255, .85);
  border-radius: 12px;
  box-shadow: 0 0 0 100vmax rgba(0, 0, 0, .35);
}
.reticle::after {
  content: "";
  position: absolute;
  left: 0; right: 0; top: 0;
  height: 2px;
  background: var(--accent);
  animation: sweep 2s ease-in-out infinite alternate;
}
@keyframes sweep { from { top: 2%; } to { top: 98%; } }

/* ---------- Hiệu ứng khi quét thành công ---------- */

.scanner.hit .reticle {
  border-color: var(--accent-2);
  box-shadow: 0 0 0 100vmax rgba(0, 0, 0, .35), 0 0 30px rgba(46, 204, 143, .75) inset;
}
/* Tắt vạch quét chạy qua chạy lại trong lúc đang báo thành công */
.scanner.hit .reticle::after { animation: none; opacity: 0; }

/* Lớp sáng xanh loé lên toàn khung hình */
.flash {
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0;
  background: radial-gradient(circle at center, rgba(46, 204, 143, .5), rgba(46, 204, 143, 0) 72%);
}
.scanner.hit .flash { animation: flash .6s ease-out; }
@keyframes flash {
  0%   { opacity: 0; }
  12%  { opacity: 1; }
  100% { opacity: 0; }
}

/* Trường hợp gửi lên máy chủ thất bại: loé đỏ thay vì xanh */
.scanner.err .reticle { border-color: var(--danger); }
.scanner.err .flash {
  background: radial-gradient(circle at center, rgba(255, 93, 93, .5), rgba(255, 93, 93, 0) 72%);
  animation: flash .6s ease-out;
}

/* Vòng tròn lan toả từ tâm */
.ring {
  position: absolute;
  left: 50%; top: 50%;
  width: 86px; height: 86px;
  margin: -43px 0 0 -43px;
  border-radius: 50%;
  border: 3px solid var(--accent-2);
  opacity: 0;
  pointer-events: none;
}
.scanner.hit .ring { animation: ripple .75s cubic-bezier(.2, .7, .3, 1); }
@keyframes ripple {
  0%   { opacity: .9; transform: scale(.55); }
  100% { opacity: 0;  transform: scale(2.7); }
}

/* Dấu tích ở giữa màn hình */
.tick {
  position: absolute;
  left: 50%; top: 50%;
  width: 86px; height: 86px;
  margin: -43px 0 0 -43px;
  opacity: 0;
  pointer-events: none;
}
.scanner.hit .tick { animation: tickPop .8s cubic-bezier(.2, .8, .3, 1); }
@keyframes tickPop {
  0%   { opacity: 0; transform: scale(.4); }
  22%  { opacity: 1; transform: scale(1.1); }
  55%  { opacity: 1; transform: scale(1); }
  100% { opacity: 0; transform: scale(1); }
}
.tick .disc { fill: rgba(46, 204, 143, .92); }
.tick .mark {
  fill: none;
  stroke: #fff;
  stroke-width: 8;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-dasharray: 48;
  stroke-dashoffset: 48;
}
.scanner.hit .tick .mark { animation: draw .3s .14s ease-out forwards; }
@keyframes draw { to { stroke-dashoffset: 0; } }

.lastscan {
  font: 600 17px/1.3 ui-monospace, SFMono-Regular, Menlo, monospace;
  word-break: break-all;
  border-radius: 8px;
  padding: 4px 6px;
  margin: 0 -6px;
}
.lastscan.hit { animation: lastHit .9s ease-out; }
@keyframes lastHit {
  0%   { background: rgba(46, 204, 143, .45); transform: scale(1.04); }
  100% { background: transparent; transform: scale(1); }
}

#count.hit { animation: countPop .5s ease-out; display: inline-block; }
@keyframes countPop {
  0%   { transform: scale(1.7); color: var(--accent-2); }
  100% { transform: scale(1); }
}

/* Tôn trọng cài đặt giảm chuyển động của người dùng */
@media (prefers-reduced-motion: reduce) {
  .scanner.hit .flash, .scanner.hit .ring, .scanner.hit .tick,
  .lastscan.hit, #count.hit, .reticle::after { animation: none; }
  .scanner.hit .tick { opacity: 1; }
  .scanner.hit .tick .mark { stroke-dashoffset: 0; }
}

.row { display: flex; gap: 8px; }
.row > * { flex: 1; }

.hidden { display: none !important; }
