:root {
  --red: #e5384f;
  --red-dark: #c52a40;
  --ink: #2b2230;
  --glass: rgba(255, 255, 255, 0.88);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #8fc7e8; font-family: "Segoe UI", system-ui, sans-serif; color: var(--ink); }
body { min-height: 100%; }
canvas { position: fixed; inset: 0; width: 100%; height: 100%; display: block; }
[hidden] { display: none !important; }

button { font: inherit; cursor: pointer; border: 0; border-radius: 10px; padding: 8px 14px; background: var(--red); color: #fff; font-weight: 600; touch-action: manipulation; }
button:hover { background: var(--red-dark); }
button:disabled { background: #b9aeb4; cursor: default; }
input { font: inherit; border: 1px solid #d9ccd2; border-radius: 10px; padding: 10px 12px; outline: none; min-width: 0; }
input:focus { border-color: var(--red); }

.card { background: var(--glass); backdrop-filter: blur(8px); border-radius: 18px; box-shadow: 0 10px 40px rgba(0, 0, 0, 0.25); }

/* Đăng nhập */
#login { position: fixed; inset: 0; display: grid; place-items: center; background: rgba(40, 20, 50, 0.35); padding: max(12px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left)); }
.pick-label { font-weight: 600; font-size: 14px; color: #6b5d69; margin-top: 2px; }
.gender-pick { display: flex; gap: 12px; justify-content: center; }
.gbtn { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 8px 6px 10px; background: #fff; color: var(--ink); border: 2px solid #eadfe4; border-radius: 16px; transition: transform .15s, border-color .15s, box-shadow .15s, background .15s; }
.gbtn canvas { position: static; width: 150px; height: 210px; max-width: 100%; }
.gbtn:hover { background: #fff6f8; transform: translateY(-2px); }
.gbtn.sel { border-color: var(--red); background: #fff1f4; box-shadow: 0 6px 20px rgba(229, 56, 79, 0.28); }
.gbtn span { font-weight: 700; }
#loginForm { padding: 28px 28px 24px; width: min(420px, 100%); max-height: 100vh; max-height: 100dvh; overflow-y: auto; overflow-x: hidden; text-align: center; display: flex; flex-direction: column; gap: 12px; }
#loginForm h1 { margin: 0; font-size: 28px; line-height: 1.15; }
#loginForm h1 small { font-size: 16px; font-weight: 500; color: #7a6a78; }
#loginForm p { margin: 0; color: #6b5d69; }
#genderFields { display: flex; flex-direction: column; gap: 6px; }
.auth-toggle { background: transparent; color: var(--red-dark); padding: 4px; }
.auth-toggle:hover { background: rgba(229, 56, 79, 0.08); }
.heart { font-size: 40px; color: var(--red); animation: beat 1.4s infinite; }
@keyframes beat { 0%, 100% { transform: scale(1); } 15% { transform: scale(1.18); } 30% { transform: scale(1); } 45% { transform: scale(1.12); } }
.error { color: var(--red-dark); min-height: 1.2em; font-size: 14px; }

/* HUD */
#hud { position: fixed; top: 12px; left: 12px; right: 12px; display: flex; flex-direction: column; gap: 6px; align-items: flex-start; pointer-events: none; }
#logoutBtn { position: fixed; top: 12px; right: 12px; pointer-events: auto; }
#chatHistoryBtn { position: fixed; top: 12px; right: 122px; pointer-events: auto; background: #929d40; }
#chatHistoryBtn > span:first-child { display: flex; align-items: center; gap: 6px; }
#unreadMessageCount { position: absolute; top: -7px; right: -7px; display: grid; place-items: center; min-width: 21px; height: 21px; padding: 0 5px; border: 2px solid #fff; border-radius: 999px; background: var(--red-dark); color: #fff; font-size: 11px; line-height: 1; box-shadow: 0 2px 6px rgba(0, 0, 0, 0.25); }
#unreadMessageCount[hidden] { display: none; }
.pill { background: rgba(255, 255, 255, 0.82); backdrop-filter: blur(6px); padding: 6px 14px; border-radius: 999px; font-size: 14px; font-weight: 600; }
.pill.hint { font-weight: 400; font-size: 12.5px; color: #5d4f5b; }
#photoCredit { position: fixed; left: 10px; bottom: 8px; z-index: 2; padding: 4px 7px; border-radius: 6px; background: rgba(255, 255, 255, 0.72); color: #2b2230; font-size: 11px; text-decoration: none; }
#photoCredit:hover { text-decoration: underline; }

/* Lịch sử trò chuyện */
#chatHistory { position: fixed; inset: 0; z-index: 30; display: grid; place-items: center; padding: 16px; background: rgba(40, 20, 50, 0.4); }
.history-card { display: flex; flex-direction: column; width: min(440px, 100%); max-height: min(620px, 85vh); overflow: hidden; background: var(--glass); backdrop-filter: blur(10px); border-radius: 18px; box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3); }
.history-card header { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 16px; background: #fff; border-bottom: 1px solid #eee; }
.history-card h2 { margin: 0; font-size: 18px; }
#historyClose { background: #eee4e8; color: var(--ink); }
#chatHistoryList { overflow-y: auto; padding: 8px; }
.history-item { display: flex; flex-direction: column; align-items: flex-start; gap: 4px; width: 100%; margin: 2px 0; padding: 10px 12px; background: transparent; color: var(--ink); text-align: left; }
.history-item:hover { background: rgba(229, 56, 79, 0.1); }
.history-item .history-name { font-weight: 700; }
.history-item .history-preview, .history-item time, .history-empty { color: #6b5d69; font-size: 13px; }
.history-item .history-preview { overflow: hidden; max-width: 100%; text-overflow: ellipsis; white-space: nowrap; }
.history-item time { font-size: 11px; }
.history-item .history-unread { color: var(--red-dark); font-size: 12px; font-weight: 700; }
.history-empty { padding: 12px; }

/* Thẻ người chơi */
#playerCard { position: fixed; padding: 12px 14px; display: flex; flex-direction: column; gap: 8px; transform: translate(-50%, -100%); z-index: 5; min-width: 190px; }
#playerCard .who { display: flex; align-items: center; gap: 8px; }
#playerCard .dot { width: 12px; height: 12px; border-radius: 50%; background: var(--red); }

/* Thông báo */
#toasts { position: fixed; top: 64px; right: 12px; display: flex; flex-direction: column; gap: 10px; z-index: 20; width: min(320px, calc(100vw - 24px)); }
.toast { background: #fff; border-radius: 14px; padding: 12px 14px; box-shadow: 0 8px 28px rgba(0, 0, 0, 0.28); animation: slide 0.25s ease-out; border-left: 5px solid var(--red); }
.toast .row { display: flex; gap: 8px; margin-top: 10px; }
.toast .row button { flex: 1; }
.toast .row button.ghost { background: #eee4e8; color: var(--ink); }
@keyframes slide { from { transform: translateX(30px); opacity: 0; } to { transform: none; opacity: 1; } }

/* Chat */
#chat { position: fixed; right: 12px; bottom: 12px; width: min(340px, calc(100vw - 24px)); height: min(460px, 70vh); height: min(460px, 70dvh); display: flex; flex-direction: column; background: var(--glass); backdrop-filter: blur(10px); border-radius: 18px; box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3); overflow: hidden; z-index: 10; }
#chat.min { height: auto; }
#chat.min #messages, #chat.min #chatForm { display: none; }
#chatHead { display: flex; justify-content: space-between; align-items: center; padding: 8px 12px; background: #fff; border-bottom: 1px solid #eee; }
#chatHead button { padding: 3px 9px; font-size: 13px; background: #eee4e8; color: var(--ink); margin-left: 4px; }
#messages { flex: 1; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 6px; }
.msg { max-width: 80%; padding: 7px 11px; border-radius: 14px; background: #fff; align-self: flex-start; word-wrap: break-word; overflow-wrap: anywhere; }
.msg.me { align-self: flex-end; background: var(--red); color: #fff; }
.msg.sys { align-self: center; background: transparent; color: #7a6a78; font-size: 12.5px; font-style: italic; }
.msg-status { display: block; margin-top: 3px; color: #72616b; font-size: 10px; text-align: right; }
.msg.me .msg-status { color: rgba(255, 255, 255, 0.82); }
#chatForm { display: flex; gap: 6px; padding: 8px; background: #fff; border-top: 1px solid #eee; }
#chatForm input { flex: 1; }

@media (max-width: 520px) {
  #loginForm { padding: 20px 18px 16px; gap: 9px; max-height: calc(100vh - 24px); max-height: calc(100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom) - 24px); }
  #loginForm h1 { font-size: 25px; }
  #loginForm input { min-height: 46px; font-size: 16px; }
  .gbtn { min-height: 44px; }
  .gbtn canvas { width: 105px; height: 132px; }
  #hud { top: calc(env(safe-area-inset-top) + 60px); left: 8px; right: 8px; gap: 6px; }
  #logoutBtn { top: calc(env(safe-area-inset-top) + 8px); right: calc(env(safe-area-inset-right) + 8px); min-width: 64px; min-height: 44px; padding: 8px 10px; }
  #chatHistoryBtn { top: calc(env(safe-area-inset-top) + 8px); right: calc(env(safe-area-inset-right) + 110px); width: 44px; height: 44px; padding: 0; display: grid; place-items: center; }
  #chatHistoryBtn > span:first-child { font-size: 0; }
  #chatHistoryBtn > span:first-child::before { content: "\1F4AC"; font-size: 20px; }
  .pill { max-width: 100%; padding: 6px 10px; font-size: 12px; }
  .pill.hint { font-size: 11px; }
  #toasts { top: calc(env(safe-area-inset-top) + 112px); right: 8px; width: min(320px, calc(100vw - 16px)); }
  #chat { right: 8px; bottom: calc(env(safe-area-inset-bottom) + 8px); width: calc(100vw - 16px); height: 58vh; height: 58dvh; max-height: 460px; }
  #chatForm input { min-height: 44px; font-size: 16px; }
  #chatForm button, #chatHead button, #historyClose, #cardBtn { min-height: 44px; }
  #chatHistory { padding: 10px; }
  .history-card { max-height: calc(100vh - 20px); max-height: calc(100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom) - 20px); }
  .history-card header { padding: 12px; }
  .history-card h2 { font-size: 16px; }
  #playerCard { min-width: 0; width: min(220px, calc(100vw - 24px)); }
}

@media (max-height: 620px) and (max-width: 520px) {
  #loginForm { gap: 6px; padding-top: 12px; padding-bottom: 10px; }
  .heart { font-size: 30px; }
  .gbtn canvas { height: 92px; }
  #chat { height: 72dvh; }
}
