body {
    margin: 0;
    overflow: hidden;
    background: #151515;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;

    /* --- CẤU HÌNH MOBILE --- */
    user-select: none;
    -webkit-user-select: none;
    touch-action: none;
    -webkit-touch-callout: none;
}

/* --- MÀN HÌNH ĐĂNG NHẬP --- */
#login-screen {
    position: absolute;
    top: 0; left: 0;
    width: 100%; height: 100%;

    /* CŨ: background: rgba(21, 21, 21, 0.9); (Quá tối) */
    /* MỚI: Màu đen mờ (0.3) để nhìn xuyên thấu */
    background: rgba(0, 0, 0, 0.3);
    /* Thêm hiệu ứng mờ hậu cảnh (Blur) cho đẹp */
    backdrop-filter: blur(3px);

    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    z-index: 100;
}

/* Container Logo + Bánh răng (Login) */
.logo-container {
    display: flex;
    flex-direction: row;       /* Ngang */
    align-items: center;       /* Căn giữa dọc */
    justify-content: center;   /* Căn giữa ngang */
    gap: 15px;
    flex-wrap: nowrap;         /* QUAN TRỌNG: Không xuống dòng */
    width: 100%;
    margin-bottom: 30px;
}

/* Logo Lớn (Login) */
.big-logo {
    margin: 0;                 /* Reset margin để không đẩy lệch */
    font-size: 60px;
    color: #00ffcc;
    text-shadow: 0 0 20px rgba(0, 255, 204, 0.8);
    font-style: italic;
    text-transform: uppercase;
    white-space: nowrap;       /* QUAN TRỌNG: Không ngắt dòng chữ */
}

/* Icon Bánh răng (Login) */
#settings-icon {
    width: 50px;
    height: 50px;
    cursor: pointer;
    transition: transform 0.5s ease;
    fill: #888;
    flex-shrink: 0;            /* QUAN TRỌNG: Không bị co nhỏ */
}

#settings-icon:hover {
    transform: rotate(180deg);
    fill: #00ffcc;
}

/* Khu vực nhập tên (Ẩn/Hiện) */
#settings-panel {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-bottom: 20px;
    animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(-10px); }
    to { opacity: 1; transform: translateY(0); }
}

.input-container {
    display: flex;
    flex-direction: column;
    gap: 15px;
}

#username-input {
    padding: 10px;
    font-size: 18px;
    border-radius: 5px;
    border: 1px solid #444;
    background: #222;
    color: white;
    text-align: center;
    width: 200px;
    outline: none;
}

#username-input:focus {
    border-color: #00ffcc;
}

/* MỚI: Container chứa 2 nút */
.button-row {
    display: flex;
    flex-direction: row;
    gap: 15px; /* Khoảng cách giữa 2 nút */
    margin-top: 15px;
}

/* Sửa lại nút Play một chút để bỏ margin-top cũ */
#play-btn {
    margin-top: 0; /* Bỏ margin vì đã có gap của cha */
    padding: 12px 40px;
    font-size: 24px;
    font-weight: 800;
    background: #00ffcc;
    color: #151515;
    border: none;
    border-radius: 50px;
    cursor: pointer;
    box-shadow: 0 0 15px rgba(0, 255, 204, 0.4);
    transition: transform 0.1s;
}

/* Style cho nút Reset */
#reset-btn {
    padding: 12px 20px;
    font-size: 24px; /* Icon to rõ */
    font-weight: 800;
    background: #ff4757; /* Màu đỏ */
    color: white;
    border: none;
    border-radius: 50px; /* Bo tròn */
    cursor: pointer;
    box-shadow: 0 0 15px rgba(255, 71, 87, 0.4); /* Phát sáng đỏ */
    transition: transform 0.1s, background 0.2s;
}

#reset-btn:hover {
    background: #ff6b81; /* Sáng hơn khi di chuột */
    transform: rotate(180deg); /* Xoay nhẹ icon cho đẹp */
}

#reset-btn:active {
    transform: scale(0.95);
}

#play-btn:active {
    transform: scale(0.95);
}

canvas {
    display: block;
}

/* --- UI TRONG GAME --- */
#ui {
    position: absolute;
    top: 20px;
    left: 20px;
    color: white;
    font-size: 16px;
    /* pointer-events: none để click xuyên qua vào canvas,
       nhưng các nút con phải set auto lại */
    pointer-events: none;
    z-index: 10;
    display: none; /* Ẩn lúc đầu */
}

/* Header trong game (Icon + Logo) */
.ingame-header {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: nowrap;      /* QUAN TRỌNG: Không xuống dòng */
    width: max-content;     /* Rộng theo nội dung */
}

/* Logo Nhỏ (Trong game) */
.logo {
    margin: 0 0 5px 0;
    font-size: 32px;
    font-weight: 800;
    color: #00ffcc;
    text-transform: uppercase;
    letter-spacing: 2px;
    text-shadow: 0 0 10px rgba(0, 255, 204, 0.6);
    font-style: italic;
    white-space: nowrap;    /* QUAN TRỌNG: Không ngắt dòng */
}

/* Nút Bánh răng (Trong game) */
#ingame-settings-btn {
    cursor: pointer;
    transition: transform 0.3s;
    pointer-events: auto;   /* QUAN TRỌNG: Cho phép click */
    flex-shrink: 0;         /* Không bị co */
    fill: #ffffff;
}

#ingame-settings-btn path {
    fill: inherit;
}

#ingame-settings-btn:hover {
    fill: #00ffcc; /* Vẫn giữ hiệu ứng hover màu xanh */
    transform: rotate(90deg);
}

/* Ô nhập tên (Trong game) */
#ingame-rename-box {
    margin-top: 5px;
    margin-bottom: 10px;
    display: flex;
    gap: 5px;
    pointer-events: auto;   /* QUAN TRỌNG: Cho phép click/nhập */
}


/* Style cho nút Reset trong game */
#ingame-reset-btn {
    background: #ff4757; /* Màu đỏ */
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-weight: bold;
    color: white;
    padding: 0 10px;      /* Độ rộng vừa phải */
    font-size: 18px;      /* Icon to rõ */
    pointer-events: auto; /* QUAN TRỌNG: Để bấm được */
    transition: transform 0.3s, background 0.2s;
}

#ingame-reset-btn:hover {
    background: #ff6b81;       /* Sáng hơn khi di chuột */
    transform: rotate(180deg); /* Xoay vòng */
}

#ingame-reset-btn:active {
    transform: scale(0.95);
}

#rename-input {
    background: rgba(0,0,0,0.5);
    border: 1px solid #555;
    color: white;
    padding: 5px;
    border-radius: 4px;
    width: 100px;
    font-size: 14px;
}

#save-name-btn {
    background: #00ffcc;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-weight: bold;
    padding: 0 10px;
}

/* Hướng dẫn chơi */
.instruction {
    position: absolute;
    bottom: 30px;
    width: 100%;
    text-align: center;
    color: rgba(255, 255, 255, 0.5);
    font-size: 14px;
    pointer-events: none;
    z-index: 10;
    text-transform: uppercase;
    letter-spacing: 1.5px;
    font-weight: 600;
}

/* --- BẢNG XẾP HẠNG (Bên phải) --- */
#leaderboard {
    position: absolute;
    top: 20px;
    right: 20px;
    background: rgba(0, 0, 0, 0.5);
    padding: 15px;
    border-radius: 10px;
    color: white;
    width: 200px;
    pointer-events: none;
    z-index: 10;
    text-align: right;
    display: none; /* Ẩn lúc đầu */
}

#leaderboard h3 {
    margin: 0 0 10px 0;
    text-align: center;
    font-size: 16px;
    color: #ffd700;
    border-bottom: 1px solid rgba(255, 255, 255, 0.2);
    padding-bottom: 5px;
}

#leaderboard-list {
    list-style: none;
    padding: 0;
    margin: 0;
}

#leaderboard-list li {
    font-size: 14px;
    margin-bottom: 5px;
    display: flex;
    justify-content: space-between;
}

/* Màu Top 3 */
#leaderboard-list li:nth-child(1) { color: #ffd700; font-weight: bold; }
#leaderboard-list li:nth-child(2) { color: #c0c0c0; }
#leaderboard-list li:nth-child(3) { color: #cd7f32; }

/* MỚI: Màn hình chờ kết nối */
#connecting-overlay {
    position: fixed;
    top: 0; left: 0;
    width: 100%; height: 100%;
    background: #151515; /* Nền tối hoàn toàn để che lúc chưa load xong */
    display: flex;       /* Dùng Flex để căn giữa */
    flex-direction: column;
    justify-content: center;
    align-items: center;
    z-index: 9999;       /* Luôn nằm trên cùng, che cả Login */
    color: #00ffcc;
    font-family: 'Segoe UI', sans-serif;
}

/* Vòng tròn xoay (Spinner) */
.spinner {
    width: 50px;
    height: 50px;
    border: 5px solid rgba(0, 255, 204, 0.3); /* Viền mờ */
    border-top: 5px solid #00ffcc;           /* Viền sáng */
    border-radius: 50%;
    animation: spin 1s linear infinite;      /* Hiệu ứng xoay */
    margin-bottom: 20px;
}

/* Định nghĩa hiệu ứng xoay */
@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}