@import url('https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap');

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    height: 100%;
    /* Prevent iOS bounce scroll from revealing background */
    overflow: hidden;
}

body {
    background-color: #222;
    color: white;
    font-family: 'Press Start 2P', monospace;
    overflow: hidden;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    touch-action: none;
}

#game-container {
    position: relative;
    width: 100vw;
    height: 100vh;       /* fallback */
    height: 100dvh;      /* dynamic viewport height: adjusts as browser chrome shows/hides */
    background-color: #000;
}

canvas#game-canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 1;
}

#ui-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 10;
    pointer-events: none; /* Let clicks pass through if needed, though gaze is used */
}

.screen {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.8);
    transition: opacity 0.3s;
    overflow-y: auto;
    /* Respect notch / home-indicator safe areas */
    padding: max(20px, env(safe-area-inset-top))
             max(20px, env(safe-area-inset-right))
             max(20px, env(safe-area-inset-bottom))
             max(20px, env(safe-area-inset-left));
}

.screen:not(.hidden) {
    pointer-events: auto;
    touch-action: auto;
}

.screen.hidden {
    opacity: 0;
    pointer-events: none;
}
/* pointer-events: none on a parent doesn't inherit to children in CSS —
   explicitly disable all descendants so hidden screens can't intercept clicks */
.screen.hidden * {
    pointer-events: none;
}

h1 {
    font-size: 4rem;
    color: #FF9900;
    text-shadow: 4px 4px 0 #000;
    margin-bottom: 10px;
    text-align: center;
}

h2 {
    font-size: 3rem;
    color: #FFF;
    margin-bottom: 20px;
    text-align: center;
}

.subtitle {
    font-size: 1.5rem;
    color: #FFF;
    margin-bottom: 40px;
}

.instructions p {
    font-size: 1rem;
    line-height: 2;
    margin-bottom: 10px;
    text-align: center;
}

#eye-tracking-option {
    margin: 20px 0;
    display: flex;
    align-items: center;
    gap: 10px;
    background: rgba(255, 255, 255, 0.1);
    padding: 10px 20px;
    border-radius: 8px;
    pointer-events: auto;
}

#eye-tracking-option input {
    width: 25px;
    height: 25px;
}

#eye-tracking-option label {
    font-size: 14px;
    cursor: pointer;
}

#loading-msg {
    font-size: 12px;
    color: #FF9900;
}

button {
    font-family: 'Press Start 2P', monospace;
    font-size: 1.5rem;
    padding: 15px 30px;
    margin-top: 30px;
    background-color: #FF9900;
    color: #000;
    border: 4px solid #FFF;
    cursor: pointer;
    pointer-events: auto;
    transition: transform 0.1s, background-color 0.2s;
}

button:hover:not(:disabled) {
    background-color: #FFCC00;
    transform: scale(1.05);
}

button:disabled {
    background-color: #555;
    color: #999;
    cursor: not-allowed;
    border-color: #777;
}

/* Calibration */
#calib-point {
    position: absolute;
    width: 30px;
    height: 30px;
    background-color: red;
    border-radius: 50%;
    box-shadow: 0 0 15px red;
    transform: translate(-50%, -50%);
    transition: top 0.5s ease, left 0.5s ease;
    z-index: 20;
}

#calib-progress {
    margin-top: 20px;
    font-size: 1.2rem;
}

/* HUD */
#hud-screen {
    background: transparent;
    justify-content: space-between;
    pointer-events: none !important;
    touch-action: none !important;
}

.hud-top, .hud-bottom {
    display: flex;
    width: 100%;
    padding: 20px;
    justify-content: space-between;
    text-shadow: 2px 2px 0 #000;
}

.hud-top {
    padding-top: max(20px, env(safe-area-inset-top));
    padding-left: max(20px, env(safe-area-inset-left));
    padding-right: max(20px, env(safe-area-inset-right));
}

.hud-bottom {
    padding-bottom: max(20px, env(safe-area-inset-bottom));
    padding-left: max(20px, env(safe-area-inset-left));
    padding-right: max(20px, env(safe-area-inset-right));
}

.hud-top div, .hud-bottom div {
    font-size: 1.2rem;
    line-height: 1.5;
    text-align: center;
}

#reticle {
    position: absolute;
    width: 40px;
    height: 40px;
    border: 3px solid rgba(255, 0, 0, 0.8);
    border-radius: 50%;
    transform: translate(-50%, -50%);
    pointer-events: none;
    z-index: 100;
}

#reticle::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 4px;
    height: 4px;
    background: red;
    border-radius: 50%;
    transform: translate(-50%, -50%);
}

#flash {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: white;
    opacity: 0;
    pointer-events: none;
    z-index: 50;
    transition: opacity 0.1s ease-out;
}

/* Mobile Adaptations */
/* Reticle visibility is controlled by JS (useEyeTracking), not by mobile class */

@media (max-width: 768px) {
    h1 { font-size: 2.5rem; }
    h2 { font-size: 1.8rem; }
    .subtitle { font-size: 1rem; margin-bottom: 20px; }
    .instructions p { font-size: 0.75rem; }
    .hud-top div, .hud-bottom div { font-size: 0.85rem; }
    button { font-size: 1.1rem; padding: 12px 22px; }
    #high-score-display { font-size: 0.8rem; }
}

@media (max-width: 480px) {
    h1 { font-size: 1.7rem; }
    h2 { font-size: 1.3rem; }
    .subtitle { font-size: 0.75rem; margin-bottom: 12px; }
    .instructions p { font-size: 0.6rem; line-height: 1.8; margin-bottom: 6px; }
    .hud-top div, .hud-bottom div { font-size: 0.6rem; }
    .hud-top, .hud-bottom { padding: 10px 8px; }
    button { font-size: 0.85rem; padding: 10px 16px; margin-top: 12px; }
    #eye-tracking-option { padding: 8px 12px; margin: 12px 0; }
    #eye-tracking-option label { font-size: 11px; }
    #loading-msg { font-size: 10px; }
    #calib-progress { font-size: 0.9rem; }
    #high-score-display { font-size: 0.65rem; }
    #final-score, #final-round { font-size: 0.8rem; }
}

/* Landscape phones — compact vertical layout */
@media (max-height: 500px) {
    .screen { justify-content: flex-start; padding-top: 8px; }
    h1 { font-size: 1.3rem; margin-bottom: 2px; }
    h2 { font-size: 1rem; margin-bottom: 6px; }
    .subtitle { font-size: 0.6rem; margin-bottom: 6px; }
    .instructions p { font-size: 0.5rem; line-height: 1.6; margin-bottom: 2px; }
    #eye-tracking-option { margin: 4px 0; padding: 5px 10px; }
    #eye-tracking-option label { font-size: 10px; }
    #loading-msg { font-size: 9px; }
    button { margin-top: 6px; padding: 7px 14px; font-size: 0.75rem; }
    .hud-top, .hud-bottom { padding: 6px 10px; }
    .hud-top div, .hud-bottom div { font-size: 0.55rem; }
    #final-score, #final-round, #high-score-display { font-size: 0.6rem; }
}

/* Landscape phones — two-column title layout to use wide space */
@media (max-height: 500px) and (orientation: landscape) {
    #title-screen .instructions {
        display: grid;
        grid-template-columns: 1fr 1fr;
        column-gap: 20px;
        text-align: left;
        max-width: 480px;
    }
    #title-screen .instructions p { margin-bottom: 2px; }
    #title-screen #eye-tracking-option { grid-column: 1 / -1; justify-content: center; }
    #title-screen #loading-msg { grid-column: 1 / -1; text-align: center; }
}

/* Portrait-mode rotation hint — phones only */
#rotate-prompt {
    display: none; /* hidden by default and on landscape */
    position: absolute;
    top: 0; left: 0;
    width: 100%; height: 100%;
    z-index: 99999;
    background: rgba(0, 0, 0, 0.92);
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 24px;
    pointer-events: auto;
}

#rotate-icon {
    font-size: 5rem;
    color: #FF9900;
    animation: rotate-hint 2.2s ease-in-out infinite;
    display: block;
    line-height: 1;
}

@keyframes rotate-hint {
    0%   { transform: rotate(0deg); }
    40%  { transform: rotate(-90deg); }
    60%  { transform: rotate(-90deg); }
    100% { transform: rotate(0deg); }
}

#rotate-prompt p {
    font-size: 0.75rem;
    color: #FFF;
    text-align: center;
    line-height: 2;
}

/* Show only on portrait phones (≤ 600px wide) */
@media (orientation: portrait) and (max-width: 600px) {
    #rotate-prompt { display: flex; }
}

#rotate-prompt.dismissed { display: none !important; }

#rotate-dismiss-btn {
    font-size: 0.65rem;
    padding: 10px 20px;
    margin-top: 0;
    background: transparent;
    color: #aaa;
    border: 2px solid #555;
}
#rotate-dismiss-btn:hover {
    background: rgba(255,255,255,0.1);
    color: #fff;
    border-color: #aaa;
    transform: none;
}

/* Leaderboard */
#score-entry {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    margin-top: 16px;
}

#player-name {
    font-family: 'Press Start 2P', monospace;
    font-size: 0.9rem;
    padding: 10px 14px;
    background: rgba(255,255,255,0.1);
    border: 3px solid #FF9900;
    color: #fff;
    text-align: center;
    width: 220px;
    outline: none;
}
#player-name::placeholder { color: #666; }

#submit-score-btn {
    font-size: 0.9rem;
    padding: 10px 20px;
    margin-top: 0;
}

#leaderboard-container {
    margin-top: 14px;
    width: 100%;
    max-width: 420px;
    overflow-y: auto;
    max-height: 240px;
}

#leaderboard-title {
    font-size: 0.75rem;
    color: #FF9900;
    text-align: center;
    margin-bottom: 8px;
}

#leaderboard-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.55rem;
}

#leaderboard-table th {
    color: #FF9900;
    border-bottom: 2px solid #FF9900;
    padding: 4px 6px;
    text-align: left;
}

#leaderboard-table td {
    padding: 5px 6px;
    border-bottom: 1px solid rgba(255,255,255,0.1);
    color: #ddd;
}

#leaderboard-table tr.current-score-row td {
    color: #FF9900;
    font-weight: bold;
}

/* Return to Anaubi */
.anaubi-btn {
    font-size: 0.7rem !important;
    padding: 8px 16px !important;
    margin-top: 10px !important;
    background: transparent !important;
    color: #aaa !important;
    border: 2px solid #555 !important;
}
.anaubi-btn:hover:not(:disabled) {
    background: rgba(255,255,255,0.08) !important;
    color: #fff !important;
    border-color: #aaa !important;
    transform: none !important;
}

/* Penalty Indicator */
#penalty-indicator {
    position: absolute;
    top: 30%;
    left: 50%;
    transform: translateX(-50%);
    font-size: 0.9rem;
    color: #ff4444;
    text-shadow: 2px 2px 0 #000;
    pointer-events: none;
    opacity: 0;
    text-align: center;
    white-space: nowrap;
    z-index: 200;
    transition: opacity 0.15s;
}
#penalty-indicator.active {
    opacity: 1;
    animation: penalty-pop 1.2s forwards;
}
@keyframes penalty-pop {
    0%   { opacity: 1; transform: translateX(-50%) translateY(0) scale(1.2); }
    60%  { opacity: 1; transform: translateX(-50%) translateY(-30px) scale(1); }
    100% { opacity: 0; transform: translateX(-50%) translateY(-50px) scale(0.9); }
}

@media (max-width: 480px) {
    #player-name { font-size: 0.65rem; width: 180px; }
    #submit-score-btn { font-size: 0.7rem; }
    #leaderboard-table { font-size: 0.45rem; }
    #leaderboard-title { font-size: 0.6rem; }
    .anaubi-btn { font-size: 0.55rem !important; }
    #penalty-indicator { font-size: 0.7rem; }
}

#flash.active {
    opacity: 0.8;
    transition: none;
}

/* Debug Layer */
#debug-layer {
    position: absolute;
    bottom: 10px;
    right: 10px;
    width: 320px;
    height: 240px;
    z-index: 100;
    background: rgba(0,0,0,0.5);
    border: 2px solid #555;
    border-radius: 8px;
    overflow: hidden;
}

#debug-layer.hidden {
    display: none;
}

#webcam-video, #debug-canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    transform: scaleX(-1); /* Mirror */
}

#debug-stats {
    position: absolute;
    top: 5px;
    left: 5px;
    font-family: monospace;
    font-size: 12px;
    color: #0f0;
    text-shadow: 1px 1px #000;
    pointer-events: none;
    z-index: 102;
    line-height: 1.4;
}

#debug-toggle, #settings-toggle {
    position: absolute;
    top: 10px; /* Moved to top */
    z-index: 9999; /* Ensure it's on the very top */
    font-family: monospace;
    font-size: 10px;
    padding: 5px 10px !important; /* Force smaller padding */
    margin: 0 !important; /* Remove margin-top from general button style */
    background: #333;
    border: 1px solid #777;
    color: #FFF;
    width: auto;
    height: auto;
    line-height: normal;
}

#debug-toggle { right: 10px; }
#settings-toggle { right: 120px; }

#settings-panel {
    position: absolute;
    top: 60px;
    right: 10px;
    width: 250px;
    background: rgba(0, 0, 0, 0.9);
    border: 3px solid #FF9900;
    padding: 20px;
    z-index: 9999;
    display: flex;
    flex-direction: column;
    gap: 15px;
    pointer-events: auto;
}

#settings-panel.hidden { display: none; }

#settings-panel h3 {
    font-size: 14px;
    color: #FF9900;
    margin-bottom: 10px;
    text-align: center;
}

.setting {
    display: flex;
    flex-direction: column;
    gap: 5px;
}

.setting label {
    font-size: 9px;
    text-transform: uppercase;
}

.setting input[type="range"] {
    width: 100%;
    cursor: pointer;
}

/* Shot indicator */
#shot-indicator {
    position: absolute;
    width: 50px;
    height: 50px;
    border: 3px solid rgba(255, 255, 0, 0.9);
    border-radius: 50%;
    transform: translate(-50%, -50%) scale(0.2);
    pointer-events: none;
    z-index: 99;
    opacity: 0;
}

#shot-indicator.active {
    animation: shot-burst 0.35s ease-out forwards;
}

@keyframes shot-burst {
    0%   { opacity: 1;   transform: translate(-50%, -50%) scale(0.2); }
    60%  { opacity: 0.8; transform: translate(-50%, -50%) scale(1.2); }
    100% { opacity: 0;   transform: translate(-50%, -50%) scale(1.6); }
}

/* High score on game over */
#high-score-display {
    font-size: 1rem;
    color: #FF9900;
    margin-top: 10px;
}