:root {
  --bg: #0f1115;
  --surface: #181b22;
  --surface-2: #20242e;
  --border: #2a2f3a;
  --text: #e7eaf0;
  --text-dim: #9aa3b2;
  --primary: #4f7cff;
  --primary-hover: #3d6af0;
  --danger: #ff5470;
  --success: #2fbf71;
  --warn: #f5a623;
  --radius: 12px;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }

/* ---------------- Buttons & inputs ---------------- */
.btn {
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 10px 16px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, opacity 0.15s;
  white-space: nowrap;
}
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-ghost { background: transparent; color: var(--text); border-color: var(--border); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-danger { background: transparent; color: var(--danger); border-color: var(--border); }
.btn-danger:hover { background: rgba(255, 84, 112, 0.12); border-color: var(--danger); }
.btn-block { width: 100%; }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }

input, select, textarea {
  width: 100%;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  padding: 10px 12px;
  font-size: 14px;
  font-family: inherit;
  outline: none;
  transition: border-color 0.15s;
}
input:focus, select:focus, textarea:focus { border-color: var(--primary); }
textarea { resize: vertical; }
label {
  display: block;
  font-size: 13px;
  color: var(--text-dim);
  margin: 12px 0 6px;
  font-weight: 600;
}
.req { color: var(--danger); }
.form-error { color: var(--danger); font-size: 13px; margin-top: 10px; min-height: 18px; }

/* ---------------- Login ---------------- */
.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 32px 28px;
  box-shadow: var(--shadow);
  text-align: center;
}
.login-logo { font-size: 44px; }
.login-card h1 { margin: 8px 0 2px; font-size: 22px; }
.login-sub { color: var(--text-dim); margin: 0 0 20px; font-size: 14px; }
.login-card label { text-align: left; }
.login-card .btn { margin-top: 22px; }

/* ---------------- Topbar ---------------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.brand { font-size: 18px; font-weight: 700; }
.brand span { font-weight: 700; }
.topbar-right { display: flex; align-items: center; gap: 12px; }
.who { color: var(--text-dim); font-size: 14px; }

/* ---------------- Container & toolbar ---------------- */
.container { 
  /* max-width: 1100px;  */
  margin: 0 auto; padding: 20px; }
.toolbar {
  display: flex;
  gap: 12px;
  align-items: center;
  margin-bottom: 18px;
  flex-wrap: wrap;
}
.search-box {
  display: flex;
  gap: 10px;
  flex: 1;
  min-width: 220px;
}
.search-box input { flex: 1; }
.search-box select { width: auto; min-width: 150px; }
.toolbar .btn-primary { flex-shrink: 0; }

/* ---------------- Table ---------------- */
.list-wrap {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow-x: auto;
}
.keys-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.keys-table th, .keys-table td {
  text-align: left;
  padding: 13px 16px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
.keys-table th {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-dim);
  background: var(--surface-2);
}
.keys-table tbody tr { transition: background 0.12s; }
.keys-table tbody tr:nth-child(even) { background: rgba(255, 255, 255, 0.012); }
.keys-table tbody tr:hover { background: var(--surface-2); }
.keys-table tbody tr:last-child td { border-bottom: none; }

.cell-key .key-wrap { display: flex; flex-direction: column; gap: 4px; }
.key-code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12.5px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  padding: 3px 8px;
  border-radius: 6px;
  word-break: break-all;
}
.key-copy { cursor: pointer; transition: border-color 0.12s, color 0.12s; }
.key-copy:hover { border-color: var(--primary); color: #cdd9ff; }

.col-actions { text-align: right; width: 1%; white-space: nowrap; }
.row-actions { display: inline-flex; gap: 6px; justify-content: flex-end; flex-wrap: nowrap; }

/* nut hanh dong dang icon */
.icon-btn {
  width: 34px; height: 34px; padding: 0;
  display: inline-flex; align-items: center; justify-content: center;
  border: 1px solid var(--border); border-radius: 8px;
  background: transparent; color: var(--text);
  font-size: 15px; line-height: 1; cursor: pointer;
  transition: background 0.12s, border-color 0.12s, color 0.12s, transform 0.06s;
  position: relative; flex-shrink: 0;
}
.icon-btn:hover { background: var(--surface-2); border-color: #3a4250; }
.icon-btn.primary:hover { border-color: var(--primary); color: #9bb4ff; }
.icon-btn.danger:hover { background: rgba(255, 84, 112, 0.12); border-color: var(--danger); color: var(--danger); }
.icon-btn:active { transform: translateY(1px); }
.icon-btn.sign-ok::after, .icon-btn.sign-stale::after {
  content: ''; position: absolute; top: -3px; right: -3px;
  width: 9px; height: 9px; border-radius: 50%; border: 2px solid var(--surface);
}
.icon-btn.sign-ok::after { background: var(--success); }
.icon-btn.sign-stale::after { background: var(--warn); }

/* trang thai active / deleted (chip co cham mau) */
.status {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px; font-weight: 600; padding: 3px 10px; border-radius: 999px; white-space: nowrap;
}
.status::before { content: ''; width: 7px; height: 7px; border-radius: 50%; flex-shrink: 0; }
.st-active { background: rgba(47, 191, 113, 0.12); color: var(--success); }
.st-active::before { background: var(--success); }
.st-deleted { background: rgba(255, 84, 112, 0.12); color: var(--danger); }
.st-deleted::before { background: var(--danger); }

.badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
}
.badge-active { background: rgba(47, 191, 113, 0.15); color: var(--success); }
.badge-disabled { background: rgba(255, 84, 112, 0.15); color: var(--danger); }
.badge-expired { background: rgba(245, 166, 35, 0.15); color: var(--warn); }

.badge-game {
  background: rgba(79, 124, 255, 0.15);
  color: #9bb4ff;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 11px;
}
.cell-dim { color: var(--text-dim); font-size: 13px; }
.empty { padding: 40px 20px; text-align: center; color: var(--text-dim); }
.count-info { color: var(--text-dim); font-size: 13px; margin-bottom: 10px; }
.warn-text { color: var(--warn); margin-left: 8px; }

.btn-sm { padding: 6px 12px; font-size: 13px; }

/* chips xem nhanh data */
.chips { display: flex; flex-wrap: wrap; gap: 5px; }
.chip {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 2px 8px;
  font-size: 12px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.chip b { color: var(--text-dim); font-weight: 600; }
.chip-more { color: var(--primary); }

/* mobile labels (an tren desktop) */
.m-label { display: none; }

/* ---------------- Pager ---------------- */
.pager {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  margin-top: 18px;
}
.page-info { color: var(--text-dim); font-size: 14px; }

/* ---------------- Modal ---------------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 100;
}
.modal-card {
  width: 100%;
  max-width: 460px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  box-shadow: var(--shadow);
  max-height: 92vh;
  overflow-y: auto;
}
.modal-card h2 { margin: 0; font-size: 20px; }
.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
  gap: 12px;
}
.mode-switch {
  display: inline-flex;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 2px;
}
.seg {
  border: none;
  background: transparent;
  color: var(--text-dim);
  padding: 6px 14px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
}
.seg.active { background: var(--primary); color: #fff; }

.data-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 14px;
}
.data-fields { display: flex; flex-direction: column; gap: 8px; margin-top: 6px; }
.data-row {
  display: grid;
  grid-template-columns: 1.2fr 0.9fr 1.3fr auto;
  gap: 8px;
  align-items: center;
}
.data-row .d-name { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 13px; }
.data-row .d-value { display: block; }
.data-row .d-remove { padding: 8px 10px; }

.json-area {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13px;
  line-height: 1.5;
  white-space: pre;
  overflow-wrap: normal;
  overflow-x: auto;
}
.hint { color: var(--text-dim); font-size: 12px; margin: 8px 0 0; }
.hint code {
  background: var(--surface-2);
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 11px;
}

.rekey-cur { font-size: 14px; color: var(--text-dim); margin: 0 0 14px; }
.signbox-btns { display: inline-flex; gap: 8px; }
.check-row { display: flex; align-items: center; gap: 8px; cursor: pointer; }
.check-row input[type="checkbox"] { width: auto; margin: 0; cursor: pointer; }
.login-remember { margin: 16px 0 0; font-size: 14px; color: var(--text-dim); text-align: left; font-weight: 500; }
input[readonly] { opacity: 0.7; cursor: default; }
.exp-row { display: flex; align-items: center; gap: 8px; margin: 8px 0; }
.exp-row label { margin: 0; white-space: nowrap; }
.exp-input { width: 90px; }
.sign-result {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13px;
  line-height: 1.6;
  white-space: pre-wrap;       /* xuong dong tu dong */
  overflow-wrap: anywhere;
  word-break: break-all;
}
.input-with-btn { display: flex; gap: 8px; }
.input-with-btn input { flex: 1; }
.input-with-btn .btn { flex-shrink: 0; }
.form-row { display: flex; gap: 14px; }
.form-row > div { flex: 1; }
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 22px;
}

/* ---------------- Toast ---------------- */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 12px 20px;
  border-radius: 10px;
  box-shadow: var(--shadow);
  z-index: 200;
  font-size: 14px;
  animation: toastIn 0.2s ease;
}
.toast.toast-error { border-color: var(--danger); color: #ffd2da; }
.toast.toast-success { border-color: var(--success); }
@keyframes toastIn {
  from { opacity: 0; transform: translate(-50%, 10px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}

/* ---------------- Status / time / old-key ---------------- */
.search-box select { width: auto; min-width: 150px; }
.badge-deleted { background: rgba(255, 84, 112, 0.15); color: var(--danger); }
.row-deleted { opacity: 0.82; }
.row-deleted .key-code { text-decoration: line-through; text-decoration-color: var(--danger); }
.old-key-hint { font-size: 11px; color: var(--warn); margin-top: 4px; }
.old-key-hint .key-code { font-size: 11px; padding: 1px 5px; }
.time-cell { display: flex; flex-direction: column; gap: 2px; }
.t-line { font-size: 12px; color: var(--text-dim); }
.t-line b { color: var(--text); font-weight: 600; }
.t-del b { color: var(--danger); }

/* ---------------- Modal lon (lich su / nhat ky) ---------------- */
.modal-lg { max-width: 640px; }

/* timeline lich su 1 key */
.hist-keyinfo { font-size: 14px; color: var(--text-dim); margin-bottom: 14px; }
.timeline { display: flex; flex-direction: column; gap: 0; max-height: 60vh; overflow-y: auto; }
.tl-item { display: flex; gap: 12px; padding-bottom: 16px; position: relative; }
.tl-item:not(:last-child)::before {
  content: ''; position: absolute; left: 6px; top: 16px; bottom: 0; width: 2px; background: var(--border);
}
.tl-dot { width: 14px; height: 14px; border-radius: 50%; flex-shrink: 0; margin-top: 3px; background: var(--text-dim); z-index: 1; }
.tl-content { flex: 1; }
.tl-row { display: flex; align-items: center; gap: 10px; }
.tl-time { font-size: 12px; color: var(--text-dim); }
.tl-meta { font-size: 13px; color: var(--text-dim); margin-top: 4px; }
.hist-extra { font-size: 13px; margin-top: 6px; }
.key-code.old { color: var(--text-dim); text-decoration: line-through; }

/* chi tiet "da sua gi" */
.hist-changes { margin-top: 8px; }
.hist-actions { margin-top: 8px; }
.hist-changes-title { font-size: 12px; color: var(--text-dim); font-weight: 600; margin-bottom: 4px; }
.diff {
  display: flex; flex-direction: column; gap: 4px;
  background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px;
}
.diff-row {
  display: flex; flex-wrap: wrap; align-items: center; gap: 6px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12px;
}
.diff-path { color: var(--text-dim); }
.diff-row .old { color: var(--danger); background: rgba(255, 84, 112, 0.12); padding: 1px 6px; border-radius: 5px; }
.diff-row .new { color: var(--success); background: rgba(47, 191, 113, 0.12); padding: 1px 6px; border-radius: 5px; }
.diff-row .arrow { color: var(--text-dim); }
.diff-badge { font-weight: 700; width: 14px; text-align: center; }
.diff-row.added .diff-badge { color: var(--success); }
.diff-row.removed .diff-badge { color: var(--danger); }
.diff-empty { font-size: 12px; color: var(--text-dim); font-style: italic; }
.log-changes { font-size: 12px; color: var(--text-dim); margin-top: 3px; }
.log-changes code {
  background: var(--surface); border: 1px solid var(--border);
  padding: 0 5px; border-radius: 4px; font-size: 11px; color: var(--text);
}

/* badge theo hanh dong */
.badge.a-create { background: rgba(47, 191, 113, 0.15); color: var(--success); }
.badge.a-update { background: rgba(79, 124, 255, 0.18); color: #9bb4ff; }
.badge.a-delete { background: rgba(255, 84, 112, 0.15); color: var(--danger); }
.badge.a-restore { background: rgba(245, 166, 35, 0.18); color: var(--warn); }
.badge.a-sign { background: rgba(155, 120, 255, 0.18); color: #c4b3ff; }
.tl-dot.a-create { background: var(--success); }
.tl-dot.a-update { background: var(--primary); }
.tl-dot.a-delete { background: var(--danger); }
.tl-dot.a-restore { background: var(--warn); }
.tl-dot.a-sign { background: #9b78ff; }

/* nhat ky tong */
.log-toolbar { display: flex; gap: 10px; margin-bottom: 14px; }
.log-toolbar input { flex: 1; }
.log-toolbar select { width: auto; min-width: 150px; }
.log-list { display: flex; flex-direction: column; gap: 8px; max-height: 56vh; overflow-y: auto; }
.log-item {
  display: flex; gap: 12px; align-items: flex-start;
  padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface-2);
}
.log-main { flex: 1; min-width: 0; }
.log-main .key-code { word-break: break-all; }
.log-meta { font-size: 12px; color: var(--text-dim); margin-top: 4px; }

/* ================== RESPONSIVE (MOBILE) ================== */
@media (max-width: 720px) {
  .container { padding: 14px; }
  .toolbar { flex-direction: column; align-items: stretch; }
  .search-box { flex-direction: column; }
  .search-box select { width: 100%; }
  .toolbar .btn-primary { width: 100%; }

  /* Bien bang thanh dang the (card) tren mobile */
  .list-wrap { background: transparent; border: none; overflow: visible; }
  .keys-table thead { display: none; }
  .keys-table, .keys-table tbody, .keys-table tr, .keys-table td { display: block; width: 100%; }
  .keys-table tr {
    border: 1px solid var(--border);
    border-radius: 12px;
    margin: 0 0 12px;
    padding: 12px 14px;
    background: var(--surface);
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
  }
  .keys-table tbody tr:nth-child(even),
  .keys-table tbody tr:hover { background: var(--surface); }
  .keys-table td {
    border: none;
    padding: 5px 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
  }
  .m-label {
    display: inline-block;
    font-size: 12px;
    color: var(--text-dim);
    font-weight: 600;
    flex-shrink: 0;
  }
  /* key / data / thao tac chiem ca hang */
  .cell-key, .cell-data, .col-actions { flex-direction: column; align-items: stretch; }
  .cell-key { border-bottom: 1px solid var(--border); padding-bottom: 10px; margin-bottom: 4px; }
  .cell-key .m-label, .cell-data .m-label { margin-bottom: 6px; }
  .cell-key .key-code { font-size: 13px; }
  .col-actions { width: auto; border-top: 1px solid var(--border); padding-top: 10px; margin-top: 4px; }
  .row-actions { justify-content: flex-start; flex-wrap: wrap; width: 100%; gap: 8px; }
  .icon-btn { width: 40px; height: 40px; font-size: 17px; }
  .form-row { flex-direction: column; gap: 0; }

  /* editor truong data: dat lai cho de bam tren mobile */
  .data-row {
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    padding: 10px;
    border: 1px solid var(--border);
    border-radius: 8px;
  }
  .data-row .d-name { grid-column: 1 / 2; }
  .data-row .d-type { grid-column: 2 / 3; }
  .data-row .d-value { grid-column: 1 / 2; }
  .data-row .d-remove { grid-column: 2 / 3; }
  .modal-head { flex-direction: column; align-items: stretch; }
  .mode-switch { width: 100%; }
  .seg { flex: 1; }

  .search-box select { width: 100%; }
  .log-toolbar { flex-direction: column; }
  .log-toolbar select { width: 100%; }
  .topbar-right { gap: 8px; }
  .topbar-right .who { display: none; }
  .time-cell { align-items: flex-end; }
}

/* ================== SCROLLBAR (mảnh & đẹp) ================== */
/* Vùng cuộn trong popup + textarea: thanh cuộn mảnh, bo tròn, nền trong suốt */
.modal-card, .timeline, .log-list, .json-area, .sign-result {
  scrollbar-width: thin;                          /* Firefox */
  scrollbar-color: var(--border) transparent;
}
.modal-card::-webkit-scrollbar,
.timeline::-webkit-scrollbar,
.log-list::-webkit-scrollbar,
.json-area::-webkit-scrollbar,
.sign-result::-webkit-scrollbar { width: 6px; height: 6px; }
.modal-card::-webkit-scrollbar-track,
.timeline::-webkit-scrollbar-track,
.log-list::-webkit-scrollbar-track,
.json-area::-webkit-scrollbar-track,
.sign-result::-webkit-scrollbar-track { background: transparent; }
.modal-card::-webkit-scrollbar-thumb,
.timeline::-webkit-scrollbar-thumb,
.log-list::-webkit-scrollbar-thumb,
.json-area::-webkit-scrollbar-thumb,
.sign-result::-webkit-scrollbar-thumb { background: var(--border); border-radius: 999px; }
.modal-card::-webkit-scrollbar-thumb:hover,
.timeline::-webkit-scrollbar-thumb:hover,
.log-list::-webkit-scrollbar-thumb:hover,
.json-area::-webkit-scrollbar-thumb:hover,
.sign-result::-webkit-scrollbar-thumb:hover { background: #3a4250; }

/* Thanh cuộn trang & bảng: mảnh cho đồng bộ */
* { scrollbar-width: thin; scrollbar-color: var(--border) transparent; }
::-webkit-scrollbar { width: 9px; height: 9px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 999px; }
::-webkit-scrollbar-thumb:hover { background: #3a4250; }

/* DESKTOP: ẩn hẳn thanh cuộn trong popup (vẫn cuộn được bằng chuột/touchpad) */
@media (min-width: 721px) {
  .modal-card, .timeline, .log-list, .json-area, .sign-result { scrollbar-width: none; }
  .modal-card::-webkit-scrollbar,
  .timeline::-webkit-scrollbar,
  .log-list::-webkit-scrollbar,
  .json-area::-webkit-scrollbar,
  .sign-result::-webkit-scrollbar { width: 0; height: 0; display: none; }
}
