/* ============================================================
   MAIL OTP - giao dien toi, mobile-first.

   Nguyen tac layout:
   - Dien thoai: trang cuon binh thuong, noi dung xep doc. KHONG dung
     flex-grow theo chieu doc (truoc day .field.grow keo o API key gian
     ra ca man hinh, tao khoang trong khong lo).
   - Man hinh rong (>= 900px): layout kieu app, chi #list co thanh cuon.
   ============================================================ */

:root {
  --bg:      #0b1016;
  --panel:   #151f2e;
  --panel2:  #1b273a;
  --line:    #24334a;
  --line2:   #33456a;
  --text:    #e8eef7;
  --dim:     #8fa3bd;
  --accent:  #4c8dff;
  --accent2: #6ea8ff;
  --otp:     #3fb950;
  --danger:  #f85149;
  --radius:  14px;
  --shadow:  0 6px 24px rgba(0, 0, 0, .35);

  /* Chieu cao toi thieu cho vung bam tay - 44px la nguong de bam tren dien thoai */
  --tap:     46px;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100dvh;
  background:
    radial-gradient(1100px 500px at 12% -10%, #17263c 0%, transparent 60%),
    radial-gradient(900px 420px at 105% 0%, #1a2033 0%, transparent 55%),
    var(--bg);
  background-attachment: fixed;
  color: var(--text);
  font: 15px/1.55 system-ui, -apple-system, "Segoe UI", "Noto Sans", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

/* ============================================================
   HEADER
   ============================================================ */
.top {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  padding-top: max(12px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--line);
  background: rgba(11, 16, 22, .86);
  backdrop-filter: blur(12px);
}

.brand {
  display: flex;
  align-items: center;
  gap: 11px;
  min-width: 0;                     /* cho phep text co lai thay vi day header */
  flex: 1 1 auto;
}

.logo {
  display: grid;
  place-items: center;
  width: 38px; height: 38px;
  flex: 0 0 auto;
  border-radius: 11px;
  font-size: 19px;
  background: linear-gradient(140deg, var(--accent), #8b5cf6);
  box-shadow: 0 4px 14px rgba(76, 141, 255, .35);
}

.brand-text { min-width: 0; }

.top h1 {
  margin: 0;
  font-size: 16px;
  font-weight: 650;
  letter-spacing: .2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sub, .sub-inline {
  margin: 1px 0 0;
  font-size: 12.5px;
  color: var(--dim);
}

.sub b { color: var(--accent2); font-weight: 600; }

/* Chi so trang thai: khong bao gio duoc chen ep tieu de */
.top-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 0 0 auto;
  max-width: 45%;
}

.live-dot {
  width: 8px; height: 8px;
  flex: 0 0 auto;
  border-radius: 50%;
  background: var(--otp);
  box-shadow: 0 0 0 3px rgba(63, 185, 80, .18);
  animation: pulse 2s infinite;
}
.live-dot.off { background: var(--dim); animation: none; box-shadow: none; }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50%      { opacity: .35; }
}

.status {
  font-size: 12px;
  color: var(--dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ============================================================
   VUNG GIUA
   ============================================================ */
.wrap {
  width: 100%;
  max-width: 1080px;
  margin: 0 auto;
  padding: 14px 16px 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.panel {
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: linear-gradient(180deg, var(--panel), var(--panel2));
  box-shadow: var(--shadow);
}

.panel.narrow { max-width: 420px; margin: 32px auto; }

.panel-title {
  margin: 0 0 12px;
  font-size: 14.5px;
  font-weight: 620;
}

/* .row tren dien thoai: xep doc, KHONG co flex-grow doc */
.row {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.row.options {
  margin-top: 14px;
  padding-top: 13px;
  border-top: 1px solid var(--line);
  flex-direction: row;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
}

.spacer { flex: 1 1 auto; }

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
}

.field label {
  font-size: 12px;
  color: var(--dim);
  font-weight: 550;
}

/* ============================================================
   INPUT
   ============================================================ */
input[type="text"], input[type="password"] {
  width: 100%;
  min-height: var(--tap);
  padding: 10px 13px;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: #0e1622;
  color: var(--text);
  /* 16px: duoi nguong nay Safari/iOS tu dong zoom khi focus vao input */
  font-size: 16px;
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}

input[type="text"]:focus, input[type="password"]:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(76, 141, 255, .16);
}

input::placeholder { color: #5d7290; }

.mono {
  font-family: ui-monospace, "Cascadia Mono", Consolas, monospace;
  font-size: 15px;
  letter-spacing: .3px;
}

/* O API key: co nut hien/an ben trong */
.key-input { position: relative; display: flex; }

.key-input input { padding-right: 52px; }

.key-toggle {
  position: absolute;
  top: 0; right: 0;
  width: 48px;
  height: 100%;
  display: grid;
  place-items: center;
  border: none;
  background: none;
  color: var(--dim);
  font-size: 16px;
  cursor: pointer;
  border-radius: 0 11px 11px 0;
}
.key-toggle:hover { color: var(--accent2); }

/* O dia chi + hau to domain */
.addr-input { display: flex; align-items: stretch; min-width: 0; }

.addr-input input {
  min-width: 0;
  border-top-right-radius: 0;
  border-bottom-right-radius: 0;
  border-right: none;
}

.addr-input .suffix {
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  padding: 0 11px;
  border: 1px solid var(--line);
  border-left: none;
  border-radius: 0 11px 11px 0;
  background: #101a28;
  color: var(--dim);
  font-size: 12.5px;
  white-space: nowrap;
  /* Domain dai khong duoc chiem het cho cua o nhap */
  max-width: 48%;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ============================================================
   NUT
   ============================================================ */
.btn {
  min-height: var(--tap);
  padding: 10px 18px;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: #1c2942;
  color: var(--text);
  font-size: 14.5px;
  font-weight: 560;
  cursor: pointer;
  white-space: nowrap;
  transition: transform .08s, background .15s, border-color .15s;
  -webkit-tap-highlight-color: transparent;
}

.btn:hover  { background: #22314e; border-color: var(--line2); }
.btn:active { transform: scale(.98); }

.btn.primary {
  border-color: transparent;
  background: linear-gradient(140deg, var(--accent), #3a6fd8);
  box-shadow: 0 3px 12px rgba(76, 141, 255, .3);
}
.btn.primary:hover { background: linear-gradient(140deg, var(--accent2), #4478e0); }

.btn.ghost  { background: transparent; }
.btn.small  { min-height: 38px; padding: 7px 14px; font-size: 13px; }
.btn.tiny   { min-height: 32px; padding: 5px 10px; font-size: 12px; }
.btn.danger { border-color: #5c2a2a; color: #ff9a94; }
.btn.danger:hover { background: #3a1e1e; }
.btn.copied { background: #1f4a2a; border-color: #2d6b3c; color: #9ff0ab; }

/* ============================================================
   CONG TAC TU DONG LAM MOI
   ============================================================ */
.switch {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  min-height: 38px;
  font-size: 13.5px;
  color: var(--dim);
  cursor: pointer;
  user-select: none;
}

.switch input {
  width: 18px; height: 18px;
  accent-color: var(--accent);
  cursor: pointer;
  flex: 0 0 auto;
}

.keyinfo {
  padding: 4px 11px;
  border: 1px solid #2d5a3a;
  border-radius: 20px;
  background: rgba(63, 185, 80, .12);
  color: #7ee08c;
  font-size: 12px;
  font-weight: 560;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 100%;
}

/* ============================================================
   KHUNG DANH SACH
   ============================================================ */
.listwrap {
  display: flex;
  flex-direction: column;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(21, 31, 46, .55);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.listhead {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 11px 14px;
  border-bottom: 1px solid var(--line);
  background: rgba(14, 22, 34, .6);
}

.list-title {
  margin: 0;
  font-size: 12.5px;
  font-weight: 620;
  color: var(--dim);
  text-transform: uppercase;
  letter-spacing: .6px;
}

.count { font-size: 12.5px; color: var(--dim); white-space: nowrap; }

.list {
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.list:empty { display: none; }

.list:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }

/* ============================================================
   THE THU
   ============================================================ */
.mail {
  border: 1px solid var(--line);
  border-radius: 12px;
  background: linear-gradient(180deg, #17212f, #141d2a);
  padding: 13px 14px;
  transition: border-color .15s;
}

.mail:hover { border-color: var(--line2); }

.mail.fresh {
  border-color: #2d6b3c;
  animation: pop .45s ease;
}

@keyframes pop {
  0%   { transform: scale(.985); box-shadow: 0 0 0 0 rgba(63, 185, 80, .5); }
  60%  { box-shadow: 0 0 0 8px rgba(63, 185, 80, 0); }
  100% { transform: scale(1); }
}

.mail-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 5px;
}

.mail-to {
  font-weight: 620;
  font-size: 13.5px;
  color: var(--accent2);
  word-break: break-all;
  min-width: 0;
}

.mail-time { font-size: 11.5px; color: var(--dim); white-space: nowrap; flex: 0 0 auto; }

.mail-from {
  font-size: 12px;
  color: var(--dim);
  margin-bottom: 6px;
  word-break: break-all;
}

.mail-subject {
  font-size: 14px;
  font-weight: 560;
  margin-bottom: 8px;
  word-break: break-word;
}

/* ---------- Khoi OTP: ca khoi la nut sao chep ---------- */
.otp-row {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  margin: 9px 0;
  padding: 11px 13px;
  border: 1px dashed #2d6b3c;
  border-radius: 12px;
  background: rgba(63, 185, 80, .07);
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
  transition: background .15s, border-color .15s, transform .08s;
  -webkit-tap-highlight-color: transparent;
}

.otp-row:hover  { background: rgba(63, 185, 80, .13); border-color: #3fb950; }
.otp-row:active { transform: scale(.99); }
.otp-row.copied { background: rgba(63, 185, 80, .2); border-style: solid; }

.otp-main { flex: 1 1 auto; min-width: 0; }

.otp-label {
  display: block;
  font-size: 11px;
  color: var(--dim);
  text-transform: uppercase;
  letter-spacing: .6px;
}

.otp-code {
  display: block;
  font-family: ui-monospace, "Cascadia Mono", Consolas, monospace;
  font-size: 26px;
  font-weight: 700;
  letter-spacing: 4px;
  line-height: 1.25;
  color: var(--otp);
  word-break: break-all;
}

.otp-hint {
  flex: 0 0 auto;
  font-size: 11.5px;
  font-weight: 560;
  color: #7ee08c;
  padding: 5px 10px;
  border-radius: 8px;
  background: rgba(63, 185, 80, .14);
}

/* ---------- Noi dung thu ---------- */
.mail-body {
  font-size: 13px;
  color: #b9c8dc;
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 92px;
  overflow: hidden;
  position: relative;
}

/* Mo dan phan bi cat de nguoi doc biet con noi dung ben duoi */
.mail-body:not(.open)::after {
  content: "";
  position: absolute;
  left: 0; right: 0; bottom: 0;
  height: 34px;
  background: linear-gradient(transparent, #151e2c);
  pointer-events: none;
}

.mail-body.open { max-height: none; }

.mail-body mark {
  background: rgba(63, 185, 80, .22);
  color: var(--otp);
  font-weight: 700;
  padding: 0 3px;
  border-radius: 3px;
}

.more {
  margin-top: 8px;
  min-height: 34px;
  padding: 0;
  background: none;
  border: none;
  color: var(--accent2);
  font-size: 12.5px;
  font-weight: 550;
  cursor: pointer;
}
.more:hover { text-decoration: underline; }

/* ---------- Trang thai rong ---------- */
.empty {
  display: grid;
  place-items: center;
  gap: 4px;
  padding: 44px 22px;
  text-align: center;
  color: var(--dim);
  font-size: 13.5px;
  line-height: 1.6;
}
.empty b { color: var(--accent2); }
.empty-icon { font-size: 32px; opacity: .5; }

.hidden { display: none !important; }

/* ============================================================
   FOOTER
   ============================================================ */
.foot {
  display: flex;
  justify-content: center;
  gap: 6px;
  flex-wrap: wrap;
  text-align: center;
  padding: 14px 16px;
  padding-bottom: max(14px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  font-size: 11.5px;
  color: var(--dim);
}

.dim { color: var(--dim); }

/* ============================================================
   MAN HINH RONG: layout kieu app, chi #list cuon
   ============================================================ */
@media (min-width: 900px) {
  html, body { height: 100%; }

  body {
    display: flex;
    flex-direction: column;
    overflow: hidden;
    min-height: 0;
  }

  /* Trang admin cuon binh thuong, khong dung layout co dinh cao */
  body.admin-page { display: block; overflow: auto; height: auto; }
  .admin-page .wrap { flex: none; min-height: 0; display: block; }
  .admin-page .wrap > * + * { margin-top: 12px; }
  .admin-page .top-actions { max-width: none; }

  .top { flex: 0 0 auto; padding: 14px 22px; }
  .top h1 { font-size: 18px; }

  .wrap {
    flex: 1 1 auto;
    min-height: 0;
    padding: 14px 22px;
  }

  /* Tren desktop moi cho phep chia ngang - va chi ngang, khong doc */
  .row { flex-direction: row; align-items: flex-end; flex-wrap: wrap; }
  .field.grow { flex: 1 1 260px; }
  .row > .btn { flex: 0 0 auto; }

  .listwrap { flex: 1 1 auto; min-height: 0; }

  .list {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    scrollbar-width: thin;
    scrollbar-color: #33456a transparent;
  }

  .list::-webkit-scrollbar       { width: 10px; }
  .list::-webkit-scrollbar-track { background: transparent; }
  .list::-webkit-scrollbar-thumb {
    background: #2b3c5a;
    border: 2px solid transparent;
    background-clip: content-box;
    border-radius: 8px;
  }
  .list::-webkit-scrollbar-thumb:hover { background: #3a5080; background-clip: content-box; }

  .empty { flex: 1 1 auto; align-content: center; }

  .foot { flex: 0 0 auto; justify-content: space-between; text-align: left; padding: 9px 22px; }
}

/* ============================================================
   MAN HINH RAT HEP
   ============================================================ */
@media (max-width: 380px) {
  .wrap { padding: 12px 12px 18px; }
  .top  { padding: 10px 12px; }
  .logo { width: 34px; height: 34px; font-size: 17px; }
  .top h1 { font-size: 15px; }
  .otp-code { font-size: 23px; letter-spacing: 3px; }
  .otp-hint { display: none; }        /* con nut nho: ca khoi van bam duoc */
  .panel { padding: 12px; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}

/* ============================================================
   TRANG ADMIN
   ============================================================ */
.alert {
  padding: 11px 14px;
  border-radius: 11px;
  font-size: 13.5px;
  margin-bottom: 12px;
  border: 1px solid;
}
.alert.error { border-color: #5c2a2a; background: rgba(248, 81, 73, .1); color: #ff9a94; }
.alert.ok    { border-color: #2d6b3c; background: rgba(63, 185, 80, .1); color: #7ee08c; }

.login-form { display: flex; flex-direction: column; gap: 10px; }
.login-form label { font-size: 12px; color: var(--dim); }

.keybox { border-color: #2d6b3c; }

.copyrow { display: flex; gap: 8px; flex-wrap: wrap; }
.copyrow input { flex: 1 1 200px; min-width: 0; }

.hint { margin: 8px 0 0; font-size: 12.5px; color: var(--dim); }
.hint code, .code code { color: var(--accent2); }

code {
  font-family: ui-monospace, "Cascadia Mono", Consolas, monospace;
  font-size: 12.5px;
  padding: 1px 5px;
  border-radius: 4px;
  background: #0e1622;
  color: var(--accent2);
  word-break: break-all;
}

pre.code {
  margin: 8px 0 0;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: #0b1320;
  color: #b9c8dc;
  font-family: ui-monospace, "Cascadia Mono", Consolas, monospace;
  font-size: 12.5px;
  overflow-x: auto;
  white-space: pre;
}

.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.inline { display: inline; margin: 0; }

/* ---------- Danh sach key: THE thay vi bang 9 cot ----------
   Bang 9 cot buoc phai cuon ngang tren dien thoai - vua kho doc vua
   de bam nham nut Xoa. The xep doc doc duoc o moi kho man hinh. */
.keylist {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.keycard {
  padding: 13px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: linear-gradient(180deg, #17212f, #141d2a);
  display: flex;
  flex-direction: column;
  gap: 9px;
}

.keycard.off { opacity: .58; }

.keycard-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.keycard-name {
  display: flex;
  align-items: baseline;
  gap: 8px;
  min-width: 0;
}
.keycard-name strong { font-size: 14.5px; font-weight: 620; }
.keycard-id { font-size: 11.5px; color: var(--dim); flex: 0 0 auto; }

.keycard-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.keycard-lbl {
  flex: 0 0 auto;
  font-size: 11px;
  color: var(--dim);
  text-transform: uppercase;
  letter-spacing: .5px;
  min-width: 52px;
}

.keycard-addr {
  flex: 1 1 180px;
  min-width: 0;
  font-size: 12px;
  word-break: break-all;
}

.keycard-key { flex: 1 1 auto; min-width: 0; font-size: 12.5px; }

.keycard-stats {
  display: flex;
  gap: 14px;
  flex-wrap: wrap;
  padding-top: 8px;
  border-top: 1px solid #1c2738;
  font-size: 12px;
  color: var(--dim);
}
.keycard-stats b { color: var(--text); font-weight: 620; }

.keycard-actions { display: flex; gap: 8px; }
.keycard-actions .btn { flex: 1 1 auto; }

@media (min-width: 620px) {
  .keycard-actions { justify-content: flex-end; }
  .keycard-actions .btn { flex: 0 0 auto; }
}

.badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 20px;
  font-size: 11.5px;
  font-weight: 600;
  border: 1px solid;
  white-space: nowrap;
}
.badge.on  { border-color: #2d6b3c; background: rgba(63, 185, 80, .12); color: #7ee08c; }
.badge.off { border-color: #4a3a1e; background: rgba(210, 153, 34, .12); color: #d8b562; }

.empty-inline { color: var(--dim); font-size: 13.5px; margin: 0; }
