:root {
  --ink: #2b2233;
  --muted: #7a6f82;
  --paper: #fffaf3;
  --line: #eadfd2;
  --accent: #ff8a5b;
  --accent-ink: #fff;
  --fur: #f6c28b;
  --fur-dark: #e0a66a;
  --belly: #fff1de;
  --blush: #ff9aa2;
  --sky-top: #bfe3ff;
  --sky-bottom: #fff4e0;
  --ground: #cfe8b6;
  --radius: 16px;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  color: var(--ink);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--paper); }
button { font: inherit; cursor: pointer; }
[hidden] { display: none !important; }

/* ---------- Thanh trên ---------- */
.topbar {
  position: fixed; inset: 0 0 auto 0; height: 56px; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 0 18px;
  background: rgba(255, 250, 243, .82); backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; display: flex; align-items: center; gap: 8px; letter-spacing: .2px; }
.brand-dot { width: 14px; height: 14px; border-radius: 50%; background: var(--accent); box-shadow: 8px 0 0 -3px var(--fur); }
.status { display: flex; align-items: center; gap: 16px; font-variant-numeric: tabular-nums; color: var(--muted); }
#clock { font-weight: 600; color: var(--ink); }
.meter { display: flex; align-items: center; gap: 6px; }
.meter i { width: 70px; height: 8px; background: var(--line); border-radius: 99px; overflow: hidden; display: block; }
.meter b { display: block; height: 100%; width: 50%; background: var(--accent); border-radius: 99px; transition: width .4s; }
.meter + .meter b { background: #7cc8a4; }
.actions { display: flex; gap: 8px; }

.btn {
  border: 0; border-radius: 12px; padding: 8px 14px;
  background: var(--accent); color: var(--accent-ink); font-weight: 600;
}
.btn:hover { filter: brightness(1.05); }
.btn.ghost { background: transparent; color: var(--ink); border: 1px solid var(--line); }
.btn.ghost:hover { background: #fff; }
.btn.big { padding: 12px 22px; font-size: 1.05rem; }
.btn.danger { background: #fff; color: #c2410c; border: 1px solid #fed7aa; margin-top: 12px; }

/* ---------- Sân chơi ---------- */
#stage {
  position: fixed; inset: 56px 0 0 0;
  background: linear-gradient(var(--sky-top), var(--sky-bottom) 72%, var(--ground) 72%);
  transition: background 1.5s;
  overflow: hidden;
}
#stage::after { /* bãi cỏ */
  content: ""; position: absolute; left: 0; right: 0; bottom: 0; height: 28%;
  background: radial-gradient(circle at 20% 0, rgba(255,255,255,.35) 0 2px, transparent 3px) 0 0 / 38px 22px, var(--ground);
}
body.t-morning { --sky-top: #ffd9b8; --sky-bottom: #fff3e3; }
body.t-day     { --sky-top: #bfe3ff; --sky-bottom: #f3fbff; }
body.t-evening { --sky-top: #f7a58b; --sky-bottom: #ffe0c2; --ground: #b9d39c; }
body.t-night   { --sky-top: #1e2142; --sky-bottom: #3d3a6b; --ground: #4f6b4a; }
body.t-night .topbar { background: rgba(30, 33, 66, .7); color: #eee; border-color: #3d3a6b; }
body.t-night #clock { color: #fff; }
#sky-label {
  position: absolute; top: 18px; left: 22px; z-index: 1;
  font-size: .9rem; color: rgba(43,34,51,.55);
}
body.t-night #sky-label { color: rgba(255,255,255,.6); }

/* ---------- Thú cưng ---------- */
.pet {
  position: absolute; bottom: 14%; left: 40%; width: 130px; height: 120px; z-index: 5;
  cursor: grab; user-select: none; outline: none;
  transition: left 0s linear;
}
.pet.dragging { cursor: grabbing; transition: none; }
.pet-svg { width: 100%; height: 100%; overflow: visible; }
.pet.face-left .pet-svg { transform: scaleX(-1); }
.pet .shadow { fill: rgba(0,0,0,.12); }
.pet .eye-closed { display: none; }
.pet.blink .eye-open, .pet.sleep .eye-open, .pet.happy .eye-open { display: none; }
.pet.blink .eye-closed, .pet.sleep .eye-closed, .pet.happy .eye-closed { display: inline; }
.pet.happy .eye-closed path { transform: scaleY(-1); transform-box: fill-box; transform-origin: center; }

.pet .body { transform-box: fill-box; transform-origin: 50% 100%; }
.pet.idle .body { animation: breathe 2.6s ease-in-out infinite; }
.pet.walk .body { animation: hop .42s ease-in-out infinite; }
.pet.sit .body { transform: scale(1.06, .9); transition: transform .25s; }
.pet.sleep .body { animation: breathe 4s ease-in-out infinite; transform: scale(1.08, .85); }
.pet.happy .body { animation: wiggle .5s ease-in-out 3; }
.pet.dragging .body { animation: dangle .6s ease-in-out infinite; }
.pet .tail { transform-box: fill-box; transform-origin: 0 100%; animation: wag 1.4s ease-in-out infinite; }
.pet.walk .tail, .pet.happy .tail { animation-duration: .45s; }
.pet.sleep .tail { animation: none; }
.pet .paw { transform-box: fill-box; transform-origin: center; transition: transform .25s; }
.pet.highfive .paw-r { transform: translate(14px, -46px) rotate(-20deg) scale(1.15); }
.pet .ear { transform-box: fill-box; transform-origin: 50% 100%; }
.pet.blink .ear-l, .pet.happy .ear-l { animation: twitch .3s ease-out; }

@keyframes breathe { 50% { transform: scale(1.02, .975); } }
@keyframes hop { 50% { transform: translateY(-7px) scale(.98, 1.03); } }
@keyframes wiggle { 25% { transform: rotate(-6deg); } 75% { transform: rotate(6deg); } }
@keyframes dangle { 50% { transform: rotate(5deg); } 0%, 100% { transform: rotate(-5deg); } }
@keyframes wag { 50% { transform: rotate(14deg); } }
@keyframes twitch { 50% { transform: rotate(-12deg); } }

/* ---------- Mèo thật (video) ----------
   Video 640x360 hiển thị ở 330x186. Trong khung hình, mèo đứng chiếm khoảng
   x 12%–75%, y 17%–90%. Hộp .pet ôm sát con mèo, video tràn ra ngoài (nền trong suốt). */
.pet-video { display: none; }
.pet.real { width: 210px; height: 136px; }
.pet.real .pet-svg { display: none; }
.pet.real .pet-video {
  display: block; position: absolute; left: 0; top: 0; width: 100%; height: 100%;
  pointer-events: none;
}
.pet.real.face-left .pet-video { transform: scaleX(-1); }
.pet.real .pet-video .clip {
  position: absolute; width: 330px; height: 186px; max-width: none;
  left: -40px; bottom: -18px;
  opacity: 0; transition: opacity .22s ease;
}
.pet.real .pet-video .clip.active { opacity: 1; }
.pet.real .pet-video .clip[data-name="sleep"] { transform: scale(.86); transform-origin: 40% 90%; }
/* Video nguồn của chế độ packed (Safari): ẩn nhưng vẫn trong trang để được giải mã */
.pet-video .clip-src { position: absolute; width: 2px; height: 2px; opacity: 0; pointer-events: none; left: 0; top: 0; }
.pet.real::before { /* bóng đổ dưới chân cho có cảm giác đứng trên cỏ */
  content: ""; position: absolute; left: 6%; right: 6%; bottom: -6px; height: 14px;
  background: radial-gradient(closest-side, rgba(0,0,0,.22), transparent);
}
.pet.real.dragging::before { opacity: .4; }
.pet.real.dragging .pet-video { animation: dangle .6s ease-in-out infinite; transform-origin: 50% 0; }
.pet.real.face-left.dragging .pet-video { animation: none; }
.pet.real .hearts { top: 10px; }
.pet.real .zzz { top: 20px; right: 20px; }
.adopt-opt img { width: 120px; height: 78px; object-fit: cover; object-position: 38% 55%; }
.adopt-opt.real { background: linear-gradient(#fff, #fff7ec); }

.zzz { position: absolute; right: 4px; top: -6px; font-weight: 700; color: #8b8fd6; display: none; }
.zzz span { display: inline-block; margin-left: 2px; }
.pet.sleep .zzz { display: block; animation: floatUp 2.4s ease-in-out infinite; }
.zzz span:nth-child(1) { font-size: 1.2em; } .zzz span:nth-child(2) { font-size: 1.45em; }
@keyframes floatUp { 0% { opacity: 0; transform: translateY(8px); } 40% { opacity: 1; } 100% { opacity: 0; transform: translateY(-18px); } }

.hearts { position: absolute; left: 50%; top: 0; pointer-events: none; }
.hearts span { position: absolute; color: #ff6b81; opacity: 0; font-size: 18px; }
.pet.happy .hearts span { animation: heart 1.4s ease-out forwards; }
.hearts span:nth-child(1) { left: -30px; } .hearts span:nth-child(2) { left: -6px; animation-delay: .15s !important; } .hearts span:nth-child(3) { left: 18px; animation-delay: .3s !important; }
@keyframes heart { 0% { opacity: 0; transform: translateY(10px) scale(.6); } 30% { opacity: 1; } 100% { opacity: 0; transform: translateY(-40px) scale(1.1); } }

.bubble {
  position: absolute; bottom: calc(100% + 6px); left: 50%; transform: translateX(-50%);
  min-width: 120px; max-width: 260px; width: max-content;
  background: #fff; border: 1px solid var(--line); border-radius: 14px; padding: 8px 12px;
  font-size: .92rem; line-height: 1.35; box-shadow: 0 6px 18px rgba(43,34,51,.12);
  animation: pop .2s ease-out;
}
.bubble::after {
  content: ""; position: absolute; top: 100%; left: calc(50% + var(--tail, 0px)); margin-left: -7px;
  border: 7px solid transparent; border-top-color: #fff;
}
@keyframes pop { from { opacity: 0; transform: translate(-50%, 6px) scale(.95); } }

.pet-menu {
  position: absolute; z-index: 15; display: grid; grid-template-columns: 1fr 1fr; gap: 4px;
  background: #fff; border: 1px solid var(--line); border-radius: 14px; padding: 6px;
  box-shadow: 0 10px 30px rgba(43,34,51,.18); animation: pop2 .15s ease-out;
}
.pet-menu button { border: 0; background: transparent; padding: 8px 10px; border-radius: 10px; text-align: left; white-space: nowrap; }
.pet-menu button:hover { background: var(--paper); }
@keyframes pop2 { from { opacity: 0; transform: scale(.96); } }

/* ---------- Panel ---------- */
.panel {
  position: fixed; top: 68px; right: 14px; bottom: 14px; width: min(370px, calc(100vw - 28px)); z-index: 30;
  background: #fff; border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: 0 18px 50px rgba(43,34,51,.18);
  display: flex; flex-direction: column; overflow: hidden;
}
.panel-head { display: flex; justify-content: space-between; align-items: center; padding: 14px 16px; border-bottom: 1px solid var(--line); }
.icon { border: 0; background: transparent; font-size: 1rem; color: var(--muted); border-radius: 8px; padding: 4px 8px; }
.icon:hover { background: var(--paper); }

.chat-log { flex: 1; overflow-y: auto; padding: 14px; display: flex; flex-direction: column; gap: 8px; }
.chat-log { min-height: 0; }
.msg { flex-shrink: 0; max-width: 82%; padding: 8px 12px; border-radius: 14px; line-height: 1.4; white-space: pre-wrap; }
.msg.bot { background: var(--paper); border: 1px solid var(--line); align-self: flex-start; border-bottom-left-radius: 4px; }
.msg.me { background: var(--accent); color: #fff; align-self: flex-end; border-bottom-right-radius: 4px; }
.ai-badge { font-size: .72rem; font-weight: 600; padding: 2px 8px; border-radius: 99px; margin-left: 6px; vertical-align: 2px; }
.ai-badge.on { background: #f3e8ff; color: #7c3aed; }
.ai-badge.off { background: var(--line); color: var(--muted); }
.msg.typing { display: flex; gap: 4px; padding: 12px 14px; }
.msg.typing span { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); opacity: .4; animation: dot 1s infinite; }
.msg.typing span:nth-child(2) { animation-delay: .15s; } .msg.typing span:nth-child(3) { animation-delay: .3s; }
@keyframes dot { 50% { opacity: 1; transform: translateY(-3px); } }
.chips { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 14px 10px; }
.chips button { border: 1px solid var(--line); background: #fff; border-radius: 99px; padding: 4px 10px; font-size: .85rem; color: var(--muted); }
.chips button:hover { color: var(--ink); border-color: var(--accent); }
.chat-form { display: flex; gap: 8px; padding: 12px; border-top: 1px solid var(--line); }
.chat-form input { flex: 1; border: 1px solid var(--line); border-radius: 12px; padding: 9px 12px; font: inherit; outline: none; }
.chat-form input:focus { border-color: var(--accent); }

.settings { padding-bottom: 16px; overflow-y: auto; }
.settings label { display: flex; flex-direction: column; gap: 6px; padding: 10px 16px 0; font-size: .9rem; color: var(--muted); }
.settings label.row { flex-direction: row; align-items: center; gap: 8px; }
.settings input:not([type=checkbox]), .settings select { border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; font: inherit; color: var(--ink); }
.settings-sub { padding: 18px 16px 6px; font-weight: 600; font-size: .85rem; text-transform: uppercase; letter-spacing: .5px; color: var(--muted); }
.list { list-style: none; margin: 0; padding: 0 16px; display: flex; flex-direction: column; gap: 6px; }
.list li { display: flex; justify-content: space-between; align-items: center; gap: 8px; background: var(--paper); border-radius: 10px; padding: 8px 10px; font-size: .9rem; }
.list li.empty { color: var(--muted); background: transparent; padding: 0; }
.list button { border: 0; background: transparent; color: var(--muted); }
.settings .btn.danger { margin: 18px 16px 0; }

/* ---------- Nhận nuôi ---------- */
.overlay { position: fixed; inset: 0; z-index: 50; display: grid; place-items: center; background: rgba(43,34,51,.35); backdrop-filter: blur(4px); padding: 20px; }
.adopt-card { background: var(--paper); border-radius: 24px; padding: 28px; width: min(620px, 100%); text-align: center; box-shadow: 0 30px 80px rgba(0,0,0,.25); }
.adopt-card h1 { margin: 0 0 6px; font-size: 1.6rem; }
.adopt-card p { margin: 0 0 20px; color: var(--muted); }
.adopt-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 12px; margin-bottom: 18px; }
.adopt-opt { border: 2px solid var(--line); background: #fff; border-radius: 18px; padding: 12px 8px 10px; display: flex; flex-direction: column; align-items: center; gap: 4px; }
.adopt-opt svg { width: 84px; height: 78px; }
.adopt-opt.sel { border-color: var(--accent); box-shadow: 0 0 0 4px rgba(255,138,91,.18); }
.adopt-opt small { color: var(--muted); }
.adopt-name { display: inline-flex; align-items: center; gap: 10px; margin-bottom: 18px; color: var(--muted); }
.adopt-name input { border: 1px solid var(--line); border-radius: 10px; padding: 8px 12px; font: inherit; text-align: center; }

/* ---------- Toast ---------- */
#toasts { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); z-index: 60; display: flex; flex-direction: column; gap: 8px; align-items: center; }
.toast { background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 12px; box-shadow: 0 10px 30px rgba(0,0,0,.2); animation: pop2 .2s ease-out; }

.brand, .btn { white-space: nowrap; }
@media (max-width: 640px) {
  .topbar { padding: 0 10px; gap: 8px; }
  .status { gap: 8px; }
  .meter i { width: 36px; }
  .brand { font-size: .95rem; }
  #btn-chat .lbl { display: none; }
  .actions .btn { padding: 8px 10px; }
}
@media (max-width: 420px) {
  .brand { font-size: 0; gap: 0; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; }
}
