/* Theme: cute pixel game */
:root {
  --ink: #3a2d4d;
  --ink-soft: #6b5b80;
  --bg: #fff4e6;
  --bg-check: #ffe9f1;
  --card: #fffdf8;
  --pink: #ff8fb1;
  --pink-dark: #e96a92;
  --mint: #8ee6c1;
  --sky: #9fd4ff;
  --yellow: #ffd966;
  --lav: #cbb9ff;
  --red: #ff6b6b;
  --green: #5fd38d;
  --shadow: rgba(58, 45, 77, .2);
  --bar-h: 60px;

  /* viền pixel (bo góc kiểu bậc thang) bằng box-shadow */
  --px: 0 -4px 0 0 var(--ink), 0 4px 0 0 var(--ink), -4px 0 0 0 var(--ink), 4px 0 0 0 var(--ink);
  --px-s: 0 -3px 0 0 var(--ink), 0 3px 0 0 var(--ink), -3px 0 0 0 var(--ink), 3px 0 0 0 var(--ink);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { scroll-padding-top: calc(var(--bar-h) + 80px); }
body {
  margin: 0;
  font-family: "VT323", "Courier New", monospace;
  font-size: 21px;
  line-height: 1.25;
  color: var(--ink);
  background-color: var(--bg);
  background-image:
    linear-gradient(45deg, var(--bg-check) 25%, transparent 25%, transparent 75%, var(--bg-check) 75%),
    linear-gradient(45deg, var(--bg-check) 25%, transparent 25%, transparent 75%, var(--bg-check) 75%);
  background-size: 32px 32px;
  background-position: 0 0, 16px 16px;
  background-attachment: fixed;
}
a { color: var(--pink-dark); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 40px; font-weight: 400; margin: 4px 0; line-height: 1; text-shadow: 3px 3px 0 var(--yellow); }
h2 { font-size: 32px; font-weight: 400; margin: 0 0 8px; line-height: 1.05; }
b, strong { font-weight: 400; color: var(--pink-dark); }
.container { max-width: 1040px; margin: 0 auto; padding: 28px 20px 80px; }
.muted { color: var(--ink-soft); }
.center { text-align: center; }
.grow { flex: 1; }
.hint { font-size: 18px; margin: 10px 0 0; }

/* ---------- Sprite pixel ---------- */
.sprite { display: inline-block; width: 24px; vertical-align: middle; flex: none; }
.sprite svg { display: block; width: 100%; height: auto; }
.sprite.big { width: 96px; }
.sprite.bob { animation: bob 1s steps(2) infinite; }
.sprite.twinkle { animation: twinkle 1.2s steps(2) infinite; }
.sprite.twinkle:nth-of-type(2n) { animation-delay: .6s; }
@keyframes bob { 50% { transform: translateY(-6px); } }
@keyframes twinkle { 50% { transform: scale(.8); opacity: .7; } }
.mascot { display: flex; justify-content: center; align-items: flex-end; gap: 10px; margin-bottom: 12px; }

/* ---------- Thành phần ---------- */
.card {
  background: var(--card);
  padding: 18px;
  margin: 4px 4px 28px;
  box-shadow: var(--px), 0 12px 0 0 var(--shadow);
}
.card.narrow { max-width: 440px; margin: 48px auto; }

label { display: block; margin-bottom: 14px; }
input:not([type="radio"]):not([type="file"]):not(.box-input) {
  display: block;
  width: calc(100% - 6px);
  margin: 7px 3px 3px;
  padding: 8px 10px;
  border: none;
  border-radius: 0;
  background: #fff;
  color: var(--ink);
  font: inherit;
  box-shadow: var(--px-s);
}
input:not(.box-input):focus { outline: none; background: #fffbe0; }
input::placeholder { color: #b9aec7; }

input[type="file"] { display: block; margin-top: 6px; font: inherit; color: var(--ink-soft); max-width: 100%; }
input[type="file"]::file-selector-button {
  font: inherit;
  margin: 3px 12px 3px 3px;
  padding: 5px 12px;
  border: none;
  background: var(--sky);
  color: var(--ink);
  cursor: pointer;
  box-shadow: var(--px-s), inset -3px -3px 0 0 rgba(0, 0, 0, .12);
}

input[type="radio"] {
  appearance: none;
  -webkit-appearance: none;
  width: 16px;
  height: 16px;
  margin: 3px 6px 3px 3px;
  background: #fff;
  box-shadow: var(--px-s);
  cursor: pointer;
  flex: none;
}
input[type="radio"]:checked { background: var(--pink); box-shadow: var(--px-s), inset 0 0 0 3px #fff; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  margin: 4px;
  padding: 7px 16px;
  border: none;
  border-radius: 0;
  background: #fff;
  color: var(--ink);
  font: inherit;
  line-height: 1.1;
  cursor: pointer;
  white-space: nowrap;
  box-shadow: var(--px), inset -4px -4px 0 0 rgba(0, 0, 0, .12), 0 8px 0 0 var(--shadow);
  transition: none;
}
.btn:hover { text-decoration: none; filter: brightness(1.06); }
.btn:active {
  transform: translateY(4px);
  box-shadow: var(--px), inset 4px 4px 0 0 rgba(0, 0, 0, .12);
}
.btn:focus-visible { outline: 3px dashed var(--pink-dark); outline-offset: 6px; }
.btn:disabled { filter: grayscale(.7); opacity: .7; cursor: not-allowed; }
.btn.busy { filter: grayscale(.5); opacity: .75; cursor: wait; }
.btn.primary { background: var(--pink); }
.btn.mint { background: var(--mint); }
.btn.sky { background: var(--sky); }
.btn.yellow { background: var(--yellow); }
.btn.danger { background: #ffc2c2; }
.btn.ghost { background: var(--card); }
.btn.small {
  padding: 3px 9px;
  font-size: 18px;
  margin: 3px;
  box-shadow: var(--px-s), inset -3px -3px 0 0 rgba(0, 0, 0, .12);
}
.btn.small:active { transform: translateY(2px); box-shadow: var(--px-s), inset 3px 3px 0 0 rgba(0, 0, 0, .12); }
.btn.block { width: calc(100% - 8px); padding: 10px 16px; font-size: 24px; }

.alert { padding: 10px 14px; margin: 4px 4px 18px; box-shadow: var(--px); }
.alert.error { background: #ffd4d4; color: #8a1f2d; }
.sticky-alert { position: sticky; top: calc(var(--bar-h) + 14px); z-index: 20; }

.tag {
  display: inline-block;
  font-size: 16px;
  padding: 0 6px;
  margin-left: 6px;
  background: var(--yellow);
  box-shadow: 0 -2px 0 0 var(--ink), 0 2px 0 0 var(--ink), -2px 0 0 0 var(--ink), 2px 0 0 0 var(--ink);
}

.tag.green { background: var(--mint); }
.tag.sky { background: var(--sky); }
.backup-line { display: flex; align-items: center; gap: 8px; margin: -8px 4px 18px; flex-wrap: wrap; }

.toast {
  position: fixed;
  bottom: 28px;
  left: 50%;
  transform: translateX(-50%);
  max-width: calc(100vw - 40px);
  padding: 10px 18px;
  background: var(--ink);
  color: #fff;
  z-index: 100;
  box-shadow: 0 -4px 0 0 #fff, 0 4px 0 0 #fff, -4px 0 0 0 #fff, 4px 0 0 0 #fff, 0 0 0 8px var(--ink);
  animation: pop .2s steps(3);
}
.toast.error { background: #c93a4e; box-shadow: 0 -4px 0 0 #fff, 0 4px 0 0 #fff, -4px 0 0 0 #fff, 4px 0 0 0 #fff, 0 0 0 8px #c93a4e; }
@keyframes pop { from { transform: translateX(-50%) translateY(16px); opacity: 0; } }

/* ---------- Cảnh báo ổ đĩa ---------- */
.disk-warning {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 8px 20px;
  color: var(--ink);
  background: repeating-linear-gradient(-45deg, #ffd966 0 16px, #ffc94a 16px 32px);
  border-bottom: 4px solid var(--ink);
}
.disk-warning b { color: #b3261e; }
.disk-warning-icon {
  flex: none;
  width: 28px;
  height: 28px;
  background: #e0334a;
  color: #fff;
  font-size: 26px;
  line-height: 28px;
  text-align: center;
  box-shadow: var(--px-s);
  animation: blink 1s steps(2) infinite;
}

/* ---------- Bố cục ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  min-height: var(--bar-h);
  padding: 4px 20px;
  background: var(--pink);
  border-bottom: 4px solid var(--ink);
  box-shadow: 0 6px 0 0 var(--shadow);
}
.topbar form { margin: 0; }
.brand {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  font-size: 28px;
  color: #fff;
  text-shadow: 2px 2px 0 var(--ink);
}
.brand:hover { text-decoration: none; }
.brand .sprite { width: 28px; }

.page-head {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 14px;
  flex-wrap: wrap;
  margin-bottom: 22px;
}
.page-head p { margin: 6px 0 0; }
.back { display: inline-block; margin-bottom: 4px; color: var(--ink-soft); }
.actions { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.actions.center { justify-content: center; }

.table-wrap { padding: 0; overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 10px 14px; text-align: left; }
th { background: var(--lav); font-weight: 400; border-bottom: 4px solid var(--ink); white-space: nowrap; }
tbody tr + tr td { border-top: 2px dashed #d9cde6; }
tbody tr:nth-child(even) { background: #fff7fa; }
tbody tr:hover { background: #fff3c9; }
.row-actions { white-space: nowrap; text-align: right; }
.empty { padding: 30px 18px; }

.share-box { display: flex; align-items: center; gap: 10px; background: #e9fbf3; }
.share-box input { margin: 3px !important; flex: 1; min-width: 0; }
.share-box span { white-space: nowrap; }

.tabs { display: flex; gap: 6px; margin: 0 4px 22px; border-bottom: 4px solid var(--ink); padding: 0 8px; }
.tab {
  margin: 0 4px;
  padding: 6px 16px;
  border: none;
  background: #eadfff;
  color: var(--ink-soft);
  font: inherit;
  cursor: pointer;
  box-shadow: 0 -4px 0 0 var(--ink), -4px 0 0 0 var(--ink), 4px 0 0 0 var(--ink);
  transform: translateY(4px);
}
.tab.active { background: var(--yellow); color: var(--ink); transform: none; padding-bottom: 10px; }

/* ---------- Editor ---------- */
.editor-toolbar { position: sticky; top: calc(var(--bar-h) + 14px); z-index: 25; padding: 12px 16px; }
.field-row { display: flex; gap: 8px 18px; align-items: center; flex-wrap: wrap; }
.field-row > label { margin-bottom: 8px; }
.field-row .grow { min-width: 200px; }
.field-row + .field-row { padding-top: 8px; border-top: 2px dashed #d9cde6; }
.mode-switch, .default-size { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.radio { display: inline-flex; align-items: center; margin: 0; cursor: pointer; }
.default-size label { display: flex; align-items: center; gap: 6px; margin: 0; }
.default-size input { width: 70px !important; margin: 3px !important; padding: 3px 6px !important; }

/* ---------- PDF ---------- */
.pdf-container { display: flex; flex-direction: column; gap: 28px; padding: 4px; }
.pdf-container > .card { margin: 0; }
.pdf-page {
  position: relative;
  width: 100%;
  background: #fff;
  box-shadow: var(--px), 0 12px 0 0 var(--shadow);
  container-type: inline-size;
}
.pdf-page canvas { display: block; width: 100%; height: 100%; }
.pdf-overlay { position: absolute; inset: 0; }
.editing .pdf-overlay { cursor: crosshair; }

/* ---------- Ô trên PDF (chữ dùng font thường cho dễ đọc) ---------- */
.box {
  position: absolute;
  border: 2px solid var(--pink-dark);
  background: rgba(255, 255, 255, .9);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  line-height: 1;
}
/* Chữ trong ô: textarea (nhập) và .box-text (chỉ đọc) dàn chữ giống hệt nhau,
   cỡ chữ + padding-top + thanh cuộn do PdfViewer.fit() đặt. */
.box-input, .box-text, .box-mirror {
  box-sizing: border-box;
  margin: 0;
  padding: 0 .25em;
  border: none;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  line-height: 1.15;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
}
.box-input, .box-text {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: 0;
  background: transparent;
  color: #1c1326;
  overflow-x: hidden;
  overflow-y: hidden;
  resize: none;
  scrollbar-width: thin;
  scrollbar-color: var(--ink-soft) transparent;
}
.box-mirror { position: fixed; left: -99999px; top: 0; visibility: hidden; pointer-events: none; }
.box-input:focus { outline: none; }
.box-input::placeholder { color: #b9aec7; }
.box:focus-within { background: #fffbe0; box-shadow: 0 0 0 3px var(--yellow); z-index: 1; }
.box.missing {
  border-color: #e02d44;
  background: #ffe0e0;
  box-shadow: 0 0 0 3px var(--red);
  animation: shake .3s steps(4) 2;
}
@keyframes shake { 25% { transform: translateX(-3px); } 75% { transform: translateX(3px); } }

/* số thứ tự ô */
.box[data-num]::after {
  content: attr(data-num);
  position: absolute;
  top: -11px;
  right: -11px;
  min-width: 18px;
  height: 18px;
  padding: 0 3px;
  background: var(--ink);
  color: var(--yellow);
  font: 16px/18px "VT323", monospace;
  text-align: center;
  pointer-events: none;
}

/* ô trong editor */
.box.edit { background: rgba(255, 232, 240, .82); }
.box.edit.active { opacity: .75; }
.handle-move {
  position: absolute;
  bottom: -8px;
  left: -8px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--pink);
  border: 3px solid var(--ink);
  cursor: move;
  touch-action: none;
  z-index: 2;
}
.handle-resize {
  position: absolute;
  right: -7px;
  bottom: -7px;
  width: 12px;
  height: 12px;
  background: var(--yellow);
  border: 3px solid var(--ink);
  cursor: nwse-resize;
  touch-action: none;
  z-index: 2;
}
.box-del {
  position: absolute;
  top: -11px;
  right: 12px;
  width: 18px;
  height: 18px;
  padding: 0;
  border: none;
  background: var(--red);
  color: #fff;
  font: 18px/18px "VT323", monospace;
  cursor: pointer;
  display: none;
  z-index: 3;
  box-shadow: 0 0 0 2px var(--ink);
}
.box.edit:hover .box-del, .box.edit:focus-within .box-del { display: block; }
.box.ghost { border-style: dashed; background: rgba(255, 143, 177, .2); pointer-events: none; }

/* ô chỉ đọc */
.box.static.key { border-color: #4a8fd4; background: rgba(214, 236, 255, .9); }
.box.static.correct { border-color: #249a57; background: rgba(150, 240, 185, .85); }
.box.static.wrong { border-color: #e02d44; background: rgba(255, 170, 170, .85); }
.box.static.ungraded { border-color: #9a8fa8; background: rgba(255, 255, 255, .9); }

/* ---------- Làm bài / kết quả ---------- */
.exam-bar { background: var(--lav); }
.exam-bar .brand { flex: 1; overflow: hidden; white-space: nowrap; font-size: 24px; }
.exam-bar .brand > span { overflow: hidden; text-overflow: ellipsis; }
.timer {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin: 4px;
  padding: 0 14px;
  background: var(--ink);
  color: var(--yellow);
  font-size: 34px;
  line-height: 40px;
  letter-spacing: 2px;
  box-shadow: 0 -4px 0 0 var(--ink), 0 4px 0 0 var(--ink), -4px 0 0 0 var(--ink), 4px 0 0 0 var(--ink);
}
.timer.warn { background: #c93a4e; color: #fff; animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: .55; } }

.score-panel {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  margin: 4px;
  padding: 6px 18px;
  background: var(--ink);
  color: var(--yellow);
  box-shadow: var(--px), 0 10px 0 0 var(--shadow);
}
.score-panel small { font-size: 20px; color: #fff; letter-spacing: 2px; }
.score { font-size: 48px; line-height: 1; }

.result-bar { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.legend { display: inline-flex; align-items: center; gap: 8px; }
.sw { display: inline-block; width: 18px; height: 18px; border: 3px solid; }
.sw.correct { background: #96f0b9; border-color: #249a57; }
.sw.wrong { background: #ffaaaa; border-color: #e02d44; }
.sw.ungraded { background: #fff; border-color: #9a8fa8; }

.stats { display: flex; justify-content: center; gap: 10px; flex-wrap: wrap; margin: 10px 0 16px; }
.stat { padding: 2px 12px; background: #fff3c9; box-shadow: var(--px-s); margin: 3px; }

@media (max-width: 640px) {
  body { font-size: 19px; }
  h1 { font-size: 32px; }
  .topbar { padding: 4px 10px; }
  .timer { font-size: 26px; padding: 0 8px; }
  .brand { font-size: 22px; }
  .row-actions { white-space: normal; }
  .share-box { flex-wrap: wrap; }
}

@media (prefers-reduced-motion: reduce) {
  .sprite, .timer.warn, .box.missing, .toast { animation: none; }
}

/* ================= Animation (kiểu pixel: chuyển động giật theo bước) ================= */
.container { position: relative; z-index: 1; }

/* vào trang: tiêu đề trượt vào, khung nảy lên */
.page-head h1 { animation: slide-in .35s steps(5) backwards; }
@keyframes slide-in { from { transform: translateX(-24px); opacity: 0; } }
.card, .pdf-page, .score-panel, .tabs { animation: pop-in .3s steps(4) backwards; }
.container > .card:nth-child(2), .pdf-page:nth-child(2) { animation-delay: .08s; }
.container > .card:nth-child(3), .pdf-page:nth-child(3) { animation-delay: .16s; }
@keyframes pop-in { from { transform: translateY(14px) scale(.97); opacity: 0; } }

/* các dòng của bảng hiện lần lượt */
tbody tr { animation: row-in .25s steps(3) backwards; }
tbody tr:nth-child(1) { animation-delay: .10s; }
tbody tr:nth-child(2) { animation-delay: .15s; }
tbody tr:nth-child(3) { animation-delay: .20s; }
tbody tr:nth-child(4) { animation-delay: .25s; }
tbody tr:nth-child(5) { animation-delay: .30s; }
tbody tr:nth-child(6) { animation-delay: .35s; }
tbody tr:nth-child(7) { animation-delay: .40s; }
tbody tr:nth-child(n + 8) { animation-delay: .45s; }
@keyframes row-in { from { transform: translateX(-12px); opacity: 0; } }

/* đổi tab */
.tab-panel:not([hidden]) { animation: pop-in .25s steps(3) backwards; }

/* nút: nhấc lên khi rê chuột, nút chính "thở" nhẹ */
.btn:hover:not(:disabled):not(.busy) { transform: translateY(-2px); }
.btn:active:not(:disabled) { transform: translateY(4px); }
.btn.small:active:not(:disabled) { transform: translateY(2px); }
.btn.block.primary { animation: breathe 1.6s steps(2) infinite; }
.btn.busy { animation: blink .8s steps(2) infinite; }
@keyframes breathe { 50% { filter: brightness(1.08); } }

/* sprite: tim đập trên thanh tiêu đề, mèo nhảy khi rê chuột */
.brand .sprite { animation: heartbeat 1.2s steps(2) infinite; }
@keyframes heartbeat { 50% { transform: scale(1.18); } }
.sprite.big { cursor: pointer; }
.sprite.big:hover { animation: jump .5s steps(4) infinite; }
@keyframes jump { 25% { transform: translateY(-14px) rotate(-6deg); } 75% { transform: translateY(-14px) rotate(6deg); } }

/* sọc cảnh báo ổ đĩa chạy ngang */
.disk-warning { background-size: 45.25px 100%; animation: stripes 1.2s linear infinite; }
@keyframes stripes { to { background-position: 45.25px 0; } }

/* ô trên PDF: nảy ra khi xuất hiện; trang kết quả thì lần lượt từng ô */
/* .pop do JS gắn lúc tạo ô rồi gỡ ngay khi chạy xong, để không chạy lại lúc đang gõ */
.box.pop { animation: box-pop .2s steps(3) backwards; }
.box.static { animation: box-pop .25s steps(3) backwards; animation-delay: min(calc(var(--i, 0) * 60ms + .3s), 2s); }
@keyframes box-pop { from { transform: scale(.6); opacity: 0; } 70% { transform: scale(1.08); } }

/* bảng điểm nảy khi đếm xong */
.score-panel.bump { animation: bump .4s steps(4); }
@keyframes bump { 40% { transform: scale(1.15) rotate(-2deg); } 70% { transform: scale(.97); } }

/* đồng hồ nhịp theo giây */
.timer .sprite { animation: tick 1s steps(2) infinite; }
@keyframes tick { 50% { transform: rotate(12deg); } }

/* sao/tim bay nền ở các trang có mèo */
.floaties { position: fixed; inset: 0; overflow: hidden; pointer-events: none; z-index: 0; }
.floaties .sprite { position: absolute; bottom: -40px; opacity: .55; animation: float-up linear infinite; }
@keyframes float-up {
  0% { transform: translateY(0) rotate(0); }
  50% { transform: translateY(-55vh) translateX(18px) rotate(10deg); }
  100% { transform: translateY(-115vh) translateX(-12px) rotate(-10deg); }
}

/* pháo giấy pixel */
.confetti { position: fixed; inset: 0; overflow: hidden; pointer-events: none; z-index: 90; }
.confetti i { position: absolute; top: -16px; width: 10px; height: 10px; animation: confetti-fall steps(12) forwards; }
@keyframes confetti-fall { to { transform: translateY(110vh) translateX(var(--dx, 0)) rotate(540deg); } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; }
  .floaties, .confetti { display: none; }
}
