body {
    display: flex;
    flex-direction: row; 
    align-items: stretch;
    height: 100vh;
    margin: 0;
    background-color: #1a1a1a;
    color: #e0e0e0;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    overflow: hidden;
}

#main-content {
    flex-grow: 1;
    display: flex;
    justify-content: center;
    align-items: center;
}

.view { 
    display: none; 
    flex-direction: column; 
    align-items: center;
    width: 100%;
    padding: 20px;
    box-sizing: border-box;
}

.view.active { 
    display: flex; 
}

#updates-container {
    width: 80%;
    max-width: 600px;
    background-color: #252525;
    border: 1px solid #444;
    border-radius: 8px;
    padding: 15px 25px;
    margin-bottom: 25px;
    text-align: center;
}
.update-title {
    font-size: 1.8em;
    font-weight: bold;
    color: #4CAF50;
    margin: 0 0 10px 0;
}
.update-description {
    font-size: 1.1em;
    color: #ffffff;
    margin: 0;
}

#lobby-view h1 { 
    margin-bottom: 20px; 
}
#tank-list { 
    list-style: none; 
    padding: 0; 
    margin: 20px 0; 
    width: 400px; 
}
#tank-list li {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px;
    background-color: #2a2a2a;
    border: 1px solid #555;
    border-radius: 8px;
    margin-bottom: 10px;
}

.game-main-area {
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 15px;
    margin-top: 15px;
}

#left-ui-panel {
    display: flex;
    flex-direction: column;
    gap: 15px;
    width: 160px;
}

canvas {
    background-color: #0a1f33;
    border: 2px solid #4CAF50;
    border-radius: 8px;
}

/* NEW: Radar Canvas Style */
#radarCanvas {
    background-color: rgba(0, 50, 0, 0.8); /* Opaque green */
    border: 2px solid #0f0;
    border-radius: 50%; /* Makes it a circle */
}

#role-selection {
    display: flex;
    flex-direction: column;
    gap: 10px; 
    margin: 10px 0;
}

.controls { 
    display: flex; 
    gap: 20px; 
    margin-top: 15px;
    align-items: flex-start; /* Align items to the top */
}

#status { 
    font-size: 18px; 
    color: #ffeb3b; 
    margin-top: 10px;
}
.game-info {
    font-size: 16px;
    color: #c0c0c0;
    margin: 10px 0;
    background-color: #2a2a2a;
    padding: 5px 15px;
    border-radius: 5px;
    border: 1px solid #444;
    text-align: center;
}
.control-set { 
    border: 1px solid #555; 
    padding: 10px; 
    border-radius: 8px; 
    background-color: #2a2a2a; 
    min-width: 200px; /* Ensure sets have a consistent width */
}

/* NEW: Gunner Stat Display Styles */
.stat-bar-container {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 8px;
    font-size: 14px;
}
.stat-bar-container label {
    margin-right: 10px;
}
.stat-bar-container progress {
    width: 100px;
    height: 15px;
}
/* Style for progress bar appearance */
progress[value] {
    -webkit-appearance: none;
    appearance: none;
    border: 1px solid #555;
    border-radius: 3px;
    background-color: #333;
}
progress[value]::-webkit-progress-bar {
    background-color: #333;
    border-radius: 3px;
}
progress[value]::-webkit-progress-value {
    background-color: #4CAF50;
    border-radius: 2px; 
    transition: width 0.2s ease-in-out;
}
progress[value]::-moz-progress-bar {
    background-color: #4CAF50;
    border-radius: 2px;
}
h2, h3 { 
    margin-top: 0; 
    color: #4CAF50; 
    text-align: center; 
}

button {
    padding: 10px 15px; 
    font-size: 16px; 
    border: 2px solid #4CAF50;
    background-color: #2a2a2a; 
    color: #e0e0e0; 
    cursor: pointer; 
    border-radius: 5px;
    transition: background-color 0.2s, color 0.2s;
}
button:hover:not(:disabled) { 
    background-color: #4CAF50; 
    color: #1a1a1a; 
}
button:disabled { 
    border-color: #555; 
    color: #777; 
    cursor: not-allowed; 
}

#back-to-lobby-btn {
    margin-top: 15px;
}

/* --- Speedometer Gauge Styles --- */
.gauge-wrapper {
    display: flex;
    justify-content: center;
    gap: 15px;
    margin-top: 10px;
}
.gauge-container {
    width: 100px;
    height: 50px;
    position: relative;
    overflow: hidden;
    text-align: center;
}
.gauge-arc {
    width: 100px;
    height: 100px;
    border: 8px solid #555;
    border-color: #555 #555 transparent transparent;
    border-radius: 50%;
    position: absolute;
    top: 0;
    left: 0;
    transform: rotate(135deg);
    box-sizing: border-box;
}
.gauge-needle {
    width: 2px;
    height: 45px;
    background-color: #ffeb3b;
    position: absolute;
    bottom: 5px;
    left: 49px;
    transform-origin: bottom center;
    transition: transform 0.1s linear;
}
.gauge-label {
    position: absolute;
    bottom: 0;
    width: 100%;
    text-align: center;
    font-size: 12px;
    color: #aaa;
}

.chat-container {
    display: flex;
    flex-direction: column;
    background-color: #252525;
    padding: 10px;
    height: 95vh;
    box-sizing: border-box;
}
#global-chat-container {
    width: 300px; 
    border-right: 1px solid #444;
}
#tank-chat-container {
    width: 250px; 
    height: 600px;
    border: 1px solid #555;
    border-radius: 8px;
    padding: 10px;
}
.chat-messages {
    flex-grow: 1;
    list-style: none;
    padding: 0 5px;
    margin: 0 0 10px 0;
    overflow-y: auto;
    background-color: #1a1a1a;
    border-radius: 5px;
    border: 1px solid #444;
}
.chat-messages li {
    padding: 4px 6px;
    border-bottom: 1px solid #333;
    word-wrap: break-word;
}
.chat-messages li:last-child {
    border-bottom: none;
}
.chat-messages .sender {
    color: #4CAF50;
    font-weight: bold;
}
.chat-messages .server-message {
    color: #ffeb3b;
    font-style: italic;
}
.chat-input-area {
    display: flex;
}
.chat-input-area input {
    flex-grow: 1;
    padding: 8px;
    border: 1px solid #555;
    background-color: #333;
    color: #e0e0e0;
    border-radius: 5px 0 0 5px;
    outline: none;
}
.chat-input-area button {
    border-radius: 0 5px 5px 0;
    border-left: none;
}

/* NEW: Volume Control Styles */
#volume-control-container {
    margin-top: auto;
    padding: 10px;
    border-top: 1px solid #444;
    display: flex;
    flex-direction: column; /* Stacks items vertically */
    gap: 8px;               /* Space between rows */
}

#volume-slider {
    flex-grow: 1;          /* <--- FILLS REMAINING SPACE NEXT TO LABEL */
    width: auto;           /* <--- REMOVES 100% WIDTH FORCING */
    cursor: pointer;
}

#admin-view {
    width: 95%;
    height: 95%;
}
#adminCanvas {
    width: 100%;
    height: 100%;
    border: 2px solid #ffeb3b; 
}

.hidden {
    display: none !important;
}
#message-box-backdrop {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.7);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
}
#message-box {
    background-color: #2a2a2a;
    padding: 25px;
    border-radius: 8px;
    border: 1px solid #555;
    text-align: center;
    box-shadow: 0 5px 15px rgba(0,0,0,0.5);
}
#message-box p {
    margin: 0 0 20px 0;
    font-size: 18px;
}
#message-box-ok {
    min-width: 80px;
}

#player-counter {
    position: absolute;
    top: 10px;
    right: 10px;
    color: white;
    font-family: monospace;
    z-index: 100;
    pointer-events: none; /* Allows clicking through the text if needed */
}

/* Style for the Name Input wrapper */
#name-input-container {
    width: 100%;
    margin-bottom: 5px;
}

#playerNameInput {
    background: rgba(0, 0, 0, 0.6);
    color: white;
    border: 1px solid #777;
    padding: 4px;
    font-family: monospace;
    width: 100%;           /* <--- FILL THE CONTAINER WIDTH */
    box-sizing: border-box; /* <--- KEEPS PADDING INSIDE THE WIDTH */
    border-radius: 4px;
}

/* New row style for labels and sliders */
.volume-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    font-size: 14px;
    color: #ccc;
}

.volume-row label {
    width: 50px; /* Fixed width for alignment */
}

.volume-row input[type="range"] {
    flex-grow: 1; /* Slider fills remaining space */
    cursor: pointer;
}

/* --- IPAD OPTIMIZED TOUCH OVERLAY --- */
#touch-ui-overlay {
    position: absolute;
    width: 1000px; /* Matches canvas width */
    height: 750px; /* Matches canvas height */
    top: 0;
    pointer-events: none; /* Let clicks pass through empty space */
    z-index: 10;
}

.touch-role-ui {
    display: none;
    width: 100%;
    height: 100%;
    position: relative;
}

.touch-left-cluster {
    position: absolute;
    bottom: 40px;
    left: 40px;
    pointer-events: auto;
}

.touch-right-cluster {
    position: absolute;
    bottom: 40px;
    right: 40px;
    pointer-events: auto;
    display: flex;
    flex-direction: column;
    gap: 15px;
    align-items: flex-end;
}

/* Touch Buttons - Flat CSS for iPad GPU optimization */
.t-btn {
    background-color: rgba(42, 42, 42, 0.7);
    border: 2px solid rgba(76, 175, 80, 0.5);
    color: white;
    font-size: 24px;
    font-weight: bold;
    border-radius: 12px;
    user-select: none;
    -webkit-user-select: none;
    touch-action: none; /* CRITICAL: Prevents iPad zooming/scrolling */
    transform: translateZ(0); /* Force GPU rendering */
}

.t-btn:active {
    background-color: rgba(76, 175, 80, 0.9);
}

/* Virtual Joystick Styles */
.joystick-container {
    width: 150px;
    height: 150px;
    background-color: rgba(42, 42, 42, 0.5);
    border: 2px solid rgba(76, 175, 80, 0.5);
    border-radius: 50%;
    touch-action: none; /* Crucial for preventing scroll */
    display: flex;
    justify-content: center;
    align-items: center;
}

#t-joystick-knob {
    width: 60px;
    height: 60px;
    background-color: rgba(76, 175, 80, 0.8);
    border-radius: 50%;
    pointer-events: none; /* Let the base handle the touch events */
    transform: translate(0px, 0px) translateZ(0); /* Hardware accelerated */
    transition: transform 0.1s ease-out; /* Smooth snap back to center */
}

#t-joystick-knob.active {
    transition: none; /* Instantly follow finger when dragged */
}

.gears .t-btn { width: 100px; height: 70px; }
.turret { display: flex; gap: 15px; }
.turret .t-btn { width: 100px; height: 80px; }
.weapons .t-btn { width: 150px; height: 70px; }
.weapons .t-fire { height: 100px; background-color: rgba(200, 50, 50, 0.7); border-color: red;}
.weapons .t-fire:active { background-color: rgba(255, 50, 50, 0.9); }


/* --- SCALING WRAPPER --- */
#canvas-wrapper {
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
}

#t-chat-toggles {
    position: absolute; 
    top: 15px; 
    left: 50%; 
    transform: translateX(-50%); 
    display: flex; 
    gap: 20px; 
    pointer-events: auto; 
    z-index: 30;
}

#t-chat-toggles .t-btn {
    font-size: 14px;
    padding: 8px 15px;
    height: auto;
    background-color: rgba(42, 42, 42, 0.8);
}

/* --- TOUCH MODE OVERRIDES (HUD LAYOUT) --- */
body.touch-mode {
    background-color: #000; /* Darken background for letterboxing if needed */
}

body.touch-mode .game-main-area {
    margin: 0;
    width: 100vw;
    height: 100vh;
    justify-content: center;
}

body.touch-mode #canvas-wrapper {
    width: 100%;
    height: 100%;
    max-width: 1000px; /* Constrain to original resolution */
    max-height: 750px;
}

body.touch-mode #gameCanvas {
    width: 100%;
    height: 100%;
    object-fit: contain; /* Keeps the 4:3 aspect ratio perfect on iPad */
}

/* Float Chats over the Game */
body.touch-mode #global-chat-container, 
body.touch-mode #tank-chat-container {
    position: absolute;
    top: 60px; /* Below the toggle buttons */
    height: auto;
    background-color: transparent;
    border: none;
    z-index: 20;
    pointer-events: none; /* Let touches pass through the text to the game */
}

body.touch-mode #global-chat-container { left: 15px; width: 35%; }
body.touch-mode #tank-chat-container { right: 15px; width: 35%; }

/* Style the text for readability over gameplay */
body.touch-mode .chat-messages {
    background-color: rgba(0, 0, 0, 0.4);
    border: none;
    border-radius: 8px;
    display: flex;
    flex-direction: column; /* Allows prepending visually */
    overflow: hidden; /* Hide scrollbars */
}

body.touch-mode .chat-messages li {
    border-bottom: none;
    text-shadow: 1px 1px 2px black;
    font-size: 14px;
}

/* Hide Inputs by default, enable interactions when shown via JS */
body.touch-mode .chat-input-area {
    display: none; 
    pointer-events: auto; 
    margin-top: 5px;
}

body.touch-mode .chat-input-area input {
    background-color: rgba(0,0,0,0.8);
}

/* Hide bulky desktop panels */
body.touch-mode #volume-control-container,
body.touch-mode #left-ui-panel,
/* body.touch-mode .controls, */
body.touch-mode h2,
body.touch-mode h3 {
    display: none !important;
}

/* --- HUD: TOP RIGHT BUTTONS --- */
#top-right-actions {
    margin-top: 15px;
    /* Removed global flex properties so it centers normally on desktop */
}

body.touch-mode #top-right-actions {
    display: flex;
    gap: 10px;
    position: absolute;
    top: 15px;
    right: 15px;
    margin: 0;
    z-index: 40;
}

body.touch-mode #top-right-actions {
    position: absolute;
    top: 15px;
    right: 15px;
    margin: 0;
    z-index: 40;
}

body.touch-mode #back-to-lobby-btn {
    background-color: rgba(180, 40, 40, 0.8);
    border-color: #ff4444;
    margin: 0;
}

body.touch-mode #cycle-role-btn {
    background-color: rgba(40, 150, 180, 0.8); /* Distinct blue color */
    border-color: #44ccff;
    margin: 0;
}

/* --- HUD: DASHBOARDS ON THE RIGHT --- */
body.touch-mode .controls {
    position: absolute;
    top: 60px; /* Directly underneath the back button */
    right: 15px;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 10px;
    z-index: 30;
    pointer-events: none; /* Let touches pass through the blank space */
    width: auto;
}

/* Make dashboards compact and semi-transparent */
body.touch-mode .control-set {
    background-color: rgba(30, 30, 30, 0.7);
    border: 1px solid rgba(255, 255, 255, 0.2);
    padding: 8px 12px;
    min-width: 140px;
    margin: 0;
}

/* Hide the desktop keyboard instructions (WASD, Spacebar) */
body.touch-mode #driver-controls-display p,
body.touch-mode #gunner-controls-display p,
body.touch-mode #commander-controls-display p {
    display: none;
}

/* Keep the active Ammo/Gear readouts tight */
body.touch-mode .control-set .game-info {
    margin: 0;
    padding: 4px;
    font-size: 14px;
}

/* Shrink the gauges for the driver */
body.touch-mode .gauge-wrapper { gap: 5px; margin-top: 5px; }
body.touch-mode .gauge-container { width: 60px; height: 35px; }
body.touch-mode .gauge-arc { width: 60px; height: 60px; border-width: 5px; }
body.touch-mode .gauge-needle { height: 28px; left: 29px; }
body.touch-mode .gauge-label { font-size: 10px; }

/* Shrink the Commander's radar */
body.touch-mode #radarCanvas {
    width: 120px;
    height: 120px;
}

/* Shift Tank Chat down so it doesn't overlap the dashboards */
body.touch-mode #tank-chat-container {
    top: 250px; /* Safely underneath the gauges/radar */
}