:root {
    --bg-dark: #1a1a2e;
    --bg-panel: #16213e;
    --bg-lighter: #0f3460;
    --accent: #e94560;
    --accent-glow: rgba(233, 69, 96, 0.4);
    --success: #4CAF50;
    --warning: #ff9800;
    --mana: #9c27b0;
    --text: #eee;
    --text-dim: #888;
    --border: #2a2a4a;
}

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

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: var(--bg-dark);
    color: var(--text);
    overflow: hidden;
    height: 100vh;
}

#game-container {
    width: 100vw;
    height: 100vh;
}

/* Screens */
.screen {
    display: none;
    width: 100%;
    height: 100%;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

.screen.active {
    display: flex;
}

/* Menu Screen */
#menu-screen h1 {
    font-size: 4rem;
    margin-bottom: 0.5rem;
    text-shadow: 0 0 20px var(--accent-glow);
    background: linear-gradient(135deg, #fff, var(--accent));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.tagline {
    font-size: 1.2rem;
    color: var(--text-dim);
    margin-bottom: 2rem;
}

.menu-buttons {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    margin-bottom: 2rem;
}

button {
    padding: 1rem 2rem;
    font-size: 1.1rem;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s;
    font-weight: 600;
}

.btn-primary {
    background: linear-gradient(135deg, var(--accent), #c73e54);
    color: white;
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 15px var(--accent-glow);
}

.btn-secondary {
    background: var(--bg-panel);
    color: var(--text);
    border: 2px solid var(--border);
}

.btn-secondary:hover {
    background: var(--bg-lighter);
    border-color: var(--accent);
}

.btn-control {
    padding: 0.5rem 1rem;
    font-size: 0.9rem;
}

.btn-highlight {
    background: var(--success);
    color: white;
}

.btn-warning {
    background: var(--warning);
    color: #000;
}

/* Player Setup */
.player-setup {
    background: var(--bg-panel);
    padding: 1.5rem;
    border-radius: 12px;
    border: 2px solid var(--border);
}

.player-setup h3 {
    margin-bottom: 1rem;
    text-align: center;
}

.player-slots {
    display: flex;
    gap: 1rem;
}

.player-slot {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    background: var(--bg-lighter);
    padding: 0.75rem;
    border-radius: 8px;
}

.player-color {
    width: 20px;
    height: 20px;
    border-radius: 50%;
}

.player-slot input[type="text"] {
    background: transparent;
    border: none;
    color: var(--text);
    font-size: 1rem;
    width: 120px;
}

.player-slot label {
    font-size: 0.8rem;
    color: var(--text-dim);
}

/* Game Screen */
#game-screen {
    padding: 1rem;
}

#game-ui {
    display: flex;
    flex-direction: column;
    height: 100%;
    gap: 1rem;
}

/* HUD */
.hud-top {
    display: flex;
    justify-content: space-between;
    background: var(--bg-panel);
    padding: 0.75rem 1.5rem;
    border-radius: 12px;
    border: 2px solid var(--border);
}

.hud-section {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.hud-section .icon {
    font-size: 1.5rem;
}

.health-bar {
    width: 150px;
    height: 20px;
    background: #333;
    border-radius: 10px;
    overflow: hidden;
}

.health-fill {
    height: 100%;
    background: linear-gradient(90deg, var(--success), #81c784);
    transition: width 0.3s;
}

/* Game Main Layout */
.game-main {
    display: flex;
    flex: 1;
    gap: 1rem;
    overflow: hidden;
}

/* Puzzle Panel */
.puzzle-panel {
    width: 320px;
    background: var(--bg-panel);
    border-radius: 12px;
    border: 2px solid var(--border);
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.puzzle-header {
    text-align: center;
    border-bottom: 2px solid var(--border);
    padding-bottom: 0.5rem;
}

.puzzle-header h3 {
    margin-bottom: 0.5rem;
}

.streak {
    color: var(--warning);
    font-weight: bold;
}

.puzzle-area {
    background: var(--bg-lighter);
    border-radius: 8px;
    padding: 1rem;
    border: 2px solid transparent;
    transition: border-color 0.2s;
}

.puzzle-area.active {
    border-color: var(--accent);
    box-shadow: 0 0 10px var(--accent-glow);
}

.player-label {
    font-weight: bold;
    margin-bottom: 0.5rem;
}

.puzzle-display {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 1rem;
    margin-bottom: 0.5rem;
}

.question {
    font-size: 2rem;
    font-weight: bold;
}

.puzzle-area input {
    width: 80px;
    height: 50px;
    font-size: 1.5rem;
    text-align: center;
    background: var(--bg-dark);
    border: 2px solid var(--border);
    border-radius: 8px;
    color: var(--text);
}

.puzzle-area input:focus {
    outline: none;
    border-color: var(--accent);
}

.puzzle-stats {
    font-size: 0.8rem;
    color: var(--text-dim);
    text-align: center;
}

.feedback {
    text-align: center;
    font-weight: bold;
    min-height: 1.5rem;
}

.feedback.correct {
    color: var(--success);
}

.feedback.wrong {
    color: var(--accent);
}

.shared-mana {
    margin-top: auto;
    text-align: center;
    padding: 1rem;
    background: linear-gradient(135deg, #4a148c, var(--mana));
    border-radius: 8px;
    font-size: 1.3rem;
    font-weight: bold;
}

/* Game Viewport */
.game-viewport {
    flex: 1;
    position: relative;
    background: var(--bg-dark);
    border-radius: 12px;
    overflow: hidden;
    border: 2px solid var(--border);
}

#game-canvas {
    width: 100%;
    height: 100%;
    display: block;
}

/* Build Menu */
.build-menu {
    position: absolute;
    bottom: 1rem;
    left: 50%;
    transform: translateX(-50%);
    background: var(--bg-panel);
    border-radius: 12px;
    padding: 1rem;
    border: 2px solid var(--accent);
    display: none;
}

.build-menu.visible {
    display: block;
}

.build-menu h4 {
    text-align: center;
    margin-bottom: 0.75rem;
}

.tower-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 0.5rem;
}

.tower-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 0.75rem;
    background: var(--bg-lighter);
    border: 2px solid var(--border);
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s;
}

.tower-btn:hover:not(:disabled) {
    border-color: var(--accent);
    transform: translateY(-2px);
}

.tower-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.tower-btn.selected {
    border-color: var(--success);
    background: rgba(76, 175, 80, 0.2);
}

.tower-icon {
    font-size: 1.5rem;
}

.tower-name {
    font-size: 0.8rem;
    font-weight: bold;
}

.tower-cost {
    font-size: 0.7rem;
    color: var(--mana);
}

.build-hint {
    text-align: center;
    margin-top: 0.5rem;
    font-size: 0.75rem;
    color: var(--text-dim);
}

/* Bottom HUD */
.hud-bottom {
    display: flex;
    justify-content: center;
    gap: 1rem;
    background: var(--bg-panel);
    padding: 0.75rem;
    border-radius: 12px;
    border: 2px solid var(--border);
}

/* Game Over Screen */
#game-over-screen h1 {
    font-size: 3rem;
    color: var(--accent);
    margin-bottom: 1rem;
}

.final-stats {
    background: var(--bg-panel);
    padding: 2rem;
    border-radius: 12px;
    margin-bottom: 2rem;
    text-align: center;
}

.final-stats p {
    font-size: 1.2rem;
    margin: 0.5rem 0;
}

.final-stats span {
    color: var(--accent);
    font-weight: bold;
}

/* Modal */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    z-index: 1000;
    align-items: center;
    justify-content: center;
}

.modal.visible {
    display: flex;
}

.modal-content {
    background: var(--bg-panel);
    padding: 2rem;
    border-radius: 12px;
    max-width: 600px;
    max-height: 80vh;
    overflow-y: auto;
    border: 2px solid var(--accent);
}

.modal-content h2 {
    margin-bottom: 1rem;
    text-align: center;
}

.help-section h3 {
    color: var(--accent);
    margin: 1rem 0 0.5rem;
}

.help-section ul {
    margin-left: 1.5rem;
}

.help-section li {
    margin: 0.25rem 0;
}

.close-modal {
    margin-top: 1rem;
    width: 100%;
}

/* Multiplayer UI */
.name-input {
    margin-bottom: 1.5rem;
    text-align: center;
}

.name-input label {
    display: block;
    margin-bottom: 0.5rem;
    color: var(--text-dim);
}

.name-input input {
    padding: 0.75rem 1rem;
    font-size: 1.1rem;
    border-radius: 8px;
    border: 2px solid var(--border);
    background: var(--bg-panel);
    color: var(--text);
    text-align: center;
    width: 250px;
}

.name-input input:focus {
    outline: none;
    border-color: var(--accent);
}

.join-form {
    display: flex;
    gap: 0.5rem;
    align-items: center;
    margin-top: 1rem;
}

.join-form.hidden {
    display: none;
}

.join-form input {
    padding: 0.75rem;
    font-size: 1.1rem;
    border-radius: 8px;
    border: 2px solid var(--border);
    background: var(--bg-panel);
    color: var(--text);
    text-align: center;
    width: 150px;
    text-transform: uppercase;
}

.join-form button {
    padding: 0.75rem 1rem;
}

.connection-status {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-top: 1rem;
    font-size: 0.9rem;
    color: var(--text-dim);
}

.status-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: var(--warning);
}

.status-dot.connected {
    background: var(--success);
}

.status-dot.error {
    background: var(--accent);
}

/* Lobby Screen */
#lobby-screen h2 {
    font-size: 2.5rem;
    margin-bottom: 1rem;
}

.room-code-display {
    background: var(--bg-panel);
    padding: 1rem 2rem;
    border-radius: 12px;
    border: 2px solid var(--border);
    display: flex;
    align-items: center;
    gap: 1rem;
    margin-bottom: 1.5rem;
}

.room-code-display .code {
    font-size: 2rem;
    font-weight: bold;
    color: var(--accent);
    letter-spacing: 4px;
}

.btn-small {
    padding: 0.5rem 0.75rem;
    font-size: 0.9rem;
}

.lobby-players {
    background: var(--bg-panel);
    border-radius: 12px;
    border: 2px solid var(--border);
    padding: 1.5rem;
    width: 400px;
    margin-bottom: 1.5rem;
}

.lobby-players h3 {
    text-align: center;
    margin-bottom: 1rem;
}

.player-list {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.lobby-player {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.75rem;
    background: var(--bg-lighter);
    border-radius: 8px;
}

.lobby-player .player-color {
    width: 16px;
    height: 16px;
    border-radius: 50%;
}

.lobby-player .player-name {
    flex: 1;
}

.lobby-player .ready-status {
    font-size: 0.8rem;
}

.lobby-player.ready .ready-status {
    color: var(--success);
}

.lobby-buttons {
    display: flex;
    gap: 1rem;
}

.lobby-buttons .hidden {
    display: none;
}

/* Other Players Panel */
.other-players {
    background: var(--bg-lighter);
    border-radius: 8px;
    padding: 0.75rem;
    max-height: 150px;
    overflow-y: auto;
}

.other-players h4 {
    font-size: 0.9rem;
    margin-bottom: 0.5rem;
    color: var(--text-dim);
}

.other-player {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.25rem 0;
    font-size: 0.85rem;
}

.other-player .player-info {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.other-player .player-color {
    width: 10px;
    height: 10px;
    border-radius: 50%;
}

.other-player .streak {
    color: var(--warning);
    font-size: 0.75rem;
}

/* Animations */
@keyframes pulse {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.05); }
}

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    25% { transform: translateX(-5px); }
    75% { transform: translateX(5px); }
}

.shake {
    animation: shake 0.3s;
}

.pulse {
    animation: pulse 0.5s;
}

/* ==================== HUD ACTIONS ==================== */
.hud-actions {
    display: flex;
    gap: 0.5rem;
    margin-left: auto;
}

.btn-small {
    padding: 0.5rem 0.75rem;
    font-size: 0.85rem;
}

/* ==================== SAVE/LOAD/SETTINGS MODALS ==================== */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.8);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 1000;
}

.modal.active {
    display: flex;
}

.modal-content {
    background: var(--bg-panel);
    border-radius: 16px;
    padding: 2rem;
    max-width: 600px;
    width: 90%;
    max-height: 80vh;
    overflow-y: auto;
    border: 2px solid var(--border);
}

.modal-content h2 {
    font-size: 1.8rem;
    margin-bottom: 1.5rem;
    color: var(--text);
    text-align: center;
}

/* Save Menu */
.save-menu .save-slots {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    margin-bottom: 1.5rem;
}

.save-slot {
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: var(--bg-lighter);
    padding: 1rem;
    border-radius: 8px;
    border: 2px solid var(--border);
}

.save-slot .save-info {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    flex: 1;
}

.save-slot .save-name {
    font-weight: bold;
    font-size: 1.1rem;
}

.save-slot .save-date {
    font-size: 0.85rem;
    color: var(--text-dim);
}

.save-slot .save-wave {
    font-size: 0.9rem;
    color: var(--accent);
}

/* Load Menu */
.load-menu .load-slots {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    margin-bottom: 1.5rem;
}

.load-slot {
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: var(--bg-lighter);
    padding: 1rem;
    border-radius: 8px;
    border: 2px solid var(--border);
}

.load-slot .save-info {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    flex: 1;
}

.load-slot .save-score {
    font-size: 0.9rem;
    color: var(--success);
}

/* Settings Menu */
.settings-menu .settings-content {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    margin-bottom: 1.5rem;
}

.setting-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
}

.setting-row label {
    font-size: 1rem;
    color: var(--text);
    flex: 1;
}

.setting-row input[type="text"] {
    padding: 0.75rem 1rem;
    font-size: 1rem;
    border-radius: 8px;
    border: 2px solid var(--border);
    background: var(--bg-lighter);
    color: var(--text);
    flex: 2;
    max-width: 300px;
}

.setting-row input[type="text"]:focus {
    outline: none;
    border-color: var(--accent);
}

/* Toggle Switch */
.toggle-switch {
    position: relative;
    display: inline-block;
    width: 50px;
    height: 26px;
}

.toggle-switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.toggle-slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: var(--border);
    transition: 0.4s;
    border-radius: 26px;
}

.toggle-slider:before {
    position: absolute;
    content: "";
    height: 18px;
    width: 18px;
    left: 4px;
    bottom: 4px;
    background-color: white;
    transition: 0.4s;
    border-radius: 50%;
}

input:checked + .toggle-slider {
    background-color: var(--success);
}

input:checked + .toggle-slider:before {
    transform: translateX(24px);
}

/* Settings Divider */
.settings-divider {
    border: none;
    border-top: 1px solid var(--border);
    margin: 1rem 0;
}

/* Volume Sliders */
.setting-row input[type="range"] {
    -webkit-appearance: none;
    width: 150px;
    height: 6px;
    border-radius: 3px;
    background: var(--border);
    outline: none;
    flex: 2;
}

.setting-row input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
}

.setting-row input[type="range"]::-moz-range-thumb {
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
    border: none;
}

.setting-row input[type="range"]:focus {
    box-shadow: 0 0 0 2px var(--accent-glow);
}

/* Volume Display */
#master-volume-display,
#sfx-volume-display,
#music-volume-display {
    font-size: 0.875rem;
    color: var(--text-muted);
    min-width: 40px;
    text-align: right;
}

.setting-row label + input[type="range"] + span {
    flex: 0 0 auto;
    padding-left: 0.5rem;
}

/* ==================== CAMPAIGN MODE ==================== */
#campaign-screen {
    padding: 2rem;
    max-width: 900px;
    margin: 0 auto;
}

#campaign-screen h2 {
    font-size: 2.5rem;
    margin-bottom: 2rem;
    color: var(--text);
}

/* Campaign Progress Header */
.campaign-progress {
    background: var(--bg-panel);
    border-radius: 12px;
    border: 2px solid var(--border);
    padding: 1.5rem;
    margin-bottom: 2rem;
}

.campaign-header-info {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 2rem;
}

.progress-bar-container {
    flex: 1;
}

.progress-bar-label {
    font-size: 0.9rem;
    color: var(--text-dim);
    margin-bottom: 0.5rem;
}

.progress-bar {
    height: 24px;
    background: var(--bg-lighter);
    border-radius: 12px;
    overflow: hidden;
    border: 2px solid var(--border);
}

.progress-fill {
    height: 100%;
    background: linear-gradient(90deg, var(--accent), #c73e54);
    transition: width 0.5s ease;
    border-radius: 10px;
}

.percentage-text {
    text-align: right;
    font-weight: bold;
    font-size: 1.2rem;
    margin-top: 0.25rem;
    color: var(--accent);
}

.campaign-stats {
    min-width: 180px;
    text-align: right;
}

.progress-text {
    font-size: 1.1rem;
    color: var(--text);
    margin-bottom: 0.5rem;
}

.stars-text {
    font-size: 1.3rem;
    color: var(--warning);
    font-weight: bold;
}

/* Campaign Level List */
.campaign-levels {
    margin-bottom: 2rem;
}

.campaign-levels h3 {
    font-size: 1.3rem;
    margin-bottom: 1rem;
    color: var(--text);
}

.level-list {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.campaign-level-item {
    display: flex;
    align-items: center;
    gap: 1rem;
    background: var(--bg-panel);
    border-radius: 10px;
    padding: 1rem 1.25rem;
    border: 2px solid var(--border);
    transition: all 0.2s;
}

.campaign-level-item.unlocked {
    cursor: pointer;
}

.campaign-level-item.unlocked:hover {
    border-color: var(--accent);
    transform: translateX(5px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

.campaign-level-item.locked {
    opacity: 0.5;
    cursor: not-allowed;
}

.level-icon {
    font-size: 1.5rem;
    width: 36px;
    text-align: center;
}

.level-info {
    flex: 1;
}

.level-name {
    font-size: 1.1rem;
    font-weight: bold;
    color: var(--text);
    margin-bottom: 0.25rem;
}

.level-description {
    font-size: 0.85rem;
    color: var(--text-dim);
}

.best-stats {
    font-size: 0.75rem;
    color: var(--success);
    margin-top: 0.25rem;
}

.level-stars {
    font-size: 1.5rem;
    letter-spacing: 2px;
}

.campaign-buttons {
    display: flex;
    justify-content: center;
}

/* Level Intro Modal */
.level-intro {
    text-align: center;
    min-width: 400px;
}

.intro-title {
    font-size: 2rem;
    margin-bottom: 1rem;
    color: var(--accent);
}

.intro-text {
    font-size: 1.1rem;
    color: var(--text);
    margin-bottom: 1.5rem;
    line-height: 1.6;
}

#btn-start-level {
    width: 100%;
    padding: 1rem;
    font-size: 1.2rem;
}

/* Level Complete Modal */
.level-complete {
    text-align: center;
    min-width: 450px;
}

.level-complete h2 {
    font-size: 2.2rem;
    color: var(--success);
}

.complete-stats {
    background: var(--bg-lighter);
    border-radius: 10px;
    padding: 1.5rem;
    margin: 1.5rem 0;
    text-align: left;
}

.stat-row {
    display: flex;
    justify-content: space-between;
    padding: 0.5rem 0;
    font-size: 1.1rem;
    border-bottom: 1px solid var(--border);
}

.stat-row:last-child {
    border-bottom: none;
}

.stat-label {
    color: var(--text-dim);
}

.stat-value {
    color: var(--text);
    font-weight: bold;
}

.complete-rating-section {
    margin: 1.5rem 0;
}

.complete-stars {
    font-size: 3rem;
    margin-bottom: 0.5rem;
}

.star-filled {
    color: var(--warning);
}

.star-empty {
    color: var(--border);
}

.complete-rating {
    font-size: 1.5rem;
    font-weight: bold;
    color: var(--success);
}

.unlock-notification {
    background: linear-gradient(135deg, var(--warning), #ff6f00);
    color: #000;
    padding: 1rem;
    border-radius: 8px;
    margin: 1rem 0;
    font-weight: bold;
    font-size: 1.1rem;
    animation: pulse 1s infinite;
}

.unlock-notification.hidden {
    display: none;
}

.complete-buttons {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    margin-top: 1.5rem;
}

.complete-buttons button {
    width: 100%;
    padding: 1rem;
    font-size: 1.1rem;
}

.complete-buttons .hidden {
    display: none;
}

/* Utility Classes */
.hidden {
    display: none !important;
}

/* Toast Notifications */
.toast {
    position: fixed;
    top: 20px;
    left: 50%;
    transform: translateX(-50%);
    padding: 1rem 2rem;
    border-radius: 8px;
    font-weight: bold;
    z-index: 10000;
    animation: slide-down 0.3s ease;
}

.toast-success {
    background: var(--success);
    color: white;
}

.toast-info {
    background: var(--bg-panel);
    color: var(--text);
    border: 2px solid var(--accent);
}

@keyframes slide-down {
    from {
        opacity: 0;
        transform: translateX(-50%) translateY(-20px);
    }
    to {
        opacity: 1;
        transform: translateX(-50%) translateY(0);
    }
}

@keyframes fade-in {
    from { opacity: 0; }
    to { opacity: 1; }
}
