* {
  /* Disable user selection to prevent accidental highlighting, especially during touch drag */
  -webkit-user-select: none; /* Safari */
  -moz-user-select: none; /* Firefox */
  -ms-user-select: none; /* IE/Edge */
  user-select: none; /* Standard */
  /* Disable tap highlighting on mobile */
  -webkit-tap-highlight-color: transparent;
  box-sizing: border-box; /* Apply box-sizing globally */
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden; /* Prevent scrollbars on body */
  font-family: sans-serif;
  background-color: #f0f0f0;
}

#game-container {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  border: none; /* Remove border for full screen */
  background-color: #fff;
  /* Default cursor state (can be overridden by JS) */
  cursor: default;
}

#maze-canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;        /* Prevents default touch gestures to allow smooth drawing on mobile */
  -ms-touch-action: none;    /* for older IE/Edge compatibility */
  /* Cursor behavior managed by JS (drawing.js, input.js) */
}

#joystick-container {
  position: absolute;
  bottom: 30px; /* Adjust vertical position */
  left: 30px;  /* Adjust horizontal position */
  width: 100px; /* Size of the joystick area */
  height: 100px;
  opacity: 0.95; /* Make it even MORE opaque */
  /* Hide by default, visibility controlled by JS adding .mobile-controls-visible */
  /* display: none; NO - let the class handle initial display */
  /* Visibility during drawing is handled by JS inline style 'display: none/block' */
  z-index: 15; /* Above game content, below UI potentially */
}

/* This class is added by input.js ONLY on mobile devices to containers */
.mobile-controls-visible {
  display: block !important; /* Use !important to override potential inline styles if needed */
}

#controls {
  position: absolute;
  bottom: 20px; /* Increased bottom offset to move it up slightly */
  left: 50%;
  transform: translateX(-50%);
  width: auto; /* Allow width to adjust to content */
  max-width: 90%; /* Adjust max-width slightly for potential extra button */
  background-color: rgba(255, 255, 255, 0.85); /* Slightly more opaque background */
  padding: 8px; /* Slightly reduced padding */
  border-radius: 5px;
  /* START HIDDEN by default, controlled by JS */
  display: none;
  flex-direction: column;
  align-items: stretch; /* Make children fill width if needed */
  z-index: 20; /* Ensure controls are above joystick/zoom buttons */
  box-shadow: 0 2px 5px rgba(0,0,0,0.2); /* Add subtle shadow */
}

/* --- Default (Portrait) Palette Styling --- */
/* Palette container holds light and dark groups */
#palette-container {
    display: flex;
    flex-direction: row; /* Arrange groups side-by-side */
    justify-content: center; /* Center the groups horizontally */
    gap: 10px; /* Reduced space between light and dark groups */
    margin-bottom: 4px; /* Reduced space below palette */
}

/* Container for light colors (Floors) - arranged vertically as one row in the group */
#palette-light {
    display: flex;
    flex-direction: row; /* Arrange buttons horizontally */
    flex-wrap: wrap; /* Allow wrapping */
    justify-content: flex-start; /* Align buttons to the start */
    gap: 4px; /* Reduced gap */
    /* Adjust width based on new button size/gap */
    width: calc(22px * 4 + 4px * 3); /* Width for 4 buttons */
}

/* Container for dark colors (Walls) - arranged vertically as one row in the group */
#palette-dark {
    display: flex;
    flex-direction: row; /* Arrange buttons horizontally */
    flex-wrap: wrap; /* Allow wrapping */
    justify-content: flex-start; /* Align buttons to the start */
    gap: 4px; /* Reduced gap */
    /* Adjust width based on new button size/gap */
    width: calc(22px * 4 + 4px * 3); /* Width for 4 buttons */
}

.palette-button {
    width: 22px; /* Slightly smaller buttons */
    height: 22px;
    border: 1px solid #ccc;
    cursor: pointer;
    box-sizing: border-box; /* Include border in size */
    padding: 0; /* Remove default padding */
    flex-shrink: 0; /* Prevent buttons from shrinking */
}

.palette-button.selected {
    /* Keep selected border */
    border: 3px solid black;
}

#brush-container {
    display: flex;
    flex-direction: column; /* Stack rows vertically if needed */
    gap: 4px; /* Reduced gap */
    margin-bottom: 4px; /* Reduced space below brushes */
    align-items: center; /* Center rows */
}

.brush-row {
    display: flex;
    flex-wrap: wrap; /* Allow wrapping */
    justify-content: center;
    gap: 6px; /* Reduced space between brush buttons */
}

.brush-button {
    width: 28px; 
    height: 28px;
    border: 1px solid #ccc;
    cursor: pointer;
    box-sizing: border-box;
    padding: 0;
    flex-shrink: 0;
    border-radius: 50%; 
    background-color: #f0f0f0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 11px; 
    font-weight: bold;
    color: #333;
}

.brush-button.selected {
    /* Keep selected border */
    border: 3px solid blue;
    background-color: #d0d0ff; 
}

.fill-bucket-button {
    width: 28px;
    height: 28px;
    border: 1px solid #ccc;
    cursor: pointer;
    background-color: #f0f0f0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px; 
    color: #333;
    border-radius: 50%; /* Match brush buttons */
}

.fill-bucket-button.selected {
    border: 3px solid blue;
    background-color: #d0d0ff;
}

#drawing-controls {
    /* This container is always within #controls */
    display: flex; /* Use flex internally */
    flex-direction: column;
    gap: 4px; /* Reduced gap */
    align-items: center; /* Center palette/brushes */
    width: 100%; /* Take full width of controls parent */
}

#drawing-controls button#extra-mode-button {
    margin-top: 8px;
    width: 100%;
    padding: 8px;
    background-color: #9c27b0;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-weight: bold;
}

#drawing-controls button#extra-mode-button:hover {
    background-color: #7b1fa2;
}

#drawing-controls button#extra-mode-button:disabled {
    background-color: #ccc;
    cursor: not-allowed;
}

/* Container for action buttons (Undo/Import/Finish) */
.drawing-actions-container {
    display: flex;
    flex-direction: row; /* Default: side-by-side (Portrait) */
    justify-content: center; /* Center buttons horizontally */
    align-items: center;
    gap: 8px; /* Space between Undo, Import, and Finish */
    margin-top: 4px; /* Reduced space above the buttons */
    width: 100%; /* Take full width */
}

#drawing-controls button#finish-drawing-button,
#drawing-controls button#import-image-button, 
#drawing-controls button#undo-drawing-button, 
#drawing-controls button#cancel-drawing-button { 
    padding: 6px 10px; 
    font-size: 12px; 
    cursor: pointer;
    flex-grow: 1; 
    flex-basis: 0; 
    text-align: center;
}

/* Disable undo button visually */
#drawing-controls button#undo-drawing-button:disabled,
#drawing-controls button#finish-drawing-button:disabled,
#drawing-controls button#cancel-drawing-button:disabled,
#drawing-controls button#import-image-button:disabled { 
    cursor: not-allowed;
    opacity: 0.5;
    background-color: #ccc; 
}

/* --- Landscape Styling --- */
@media (orientation: landscape) {
  #controls {
    left: 10px;
    top: 50%;
    bottom: auto; 
    transform: translateY(-50%); 
    width: auto; 
    max-width: 90px; 
    flex-direction: column; 
    align-items: center; 
    padding: 6px; 
  }

  /* --- Landscape Palette Styling --- */
  /* Palette container holds light and dark groups */
  #palette-container {
    display: flex;
    flex-direction: column; 
    align-items: center; 
    gap: 8px; 
    margin-bottom: 6px; 
  }

  /* Style for light/dark groups in landscape: 2 columns */
  #palette-light,
  #palette-dark {
    display: flex;
    flex-direction: row; 
    flex-wrap: wrap; 
    justify-content: center; 
    gap: 4px; 
    width: calc(22px * 2 + 4px); 
  }

  .palette-button {
    width: 22px; 
    height: 22px;
  }

   #brush-container {
    align-items: center; 
    margin-bottom: 6px; 
  }

  .brush-row {
    justify-content: center; 
    width: 100%; 
    gap: 5px; 
  }

  .brush-button {
    width: 26px; 
    height: 26px;
    font-size: 10px; 
  }

  /* Landscape: Stack Undo, Import, Finish */
  .drawing-actions-container {
      flex-direction: column; 
      gap: 6px; 
      margin-top: 6px; 
  }

  #drawing-controls button#finish-drawing-button,
  #drawing-controls button#import-image-button, 
  #drawing-controls button#undo-drawing-button,
  #drawing-controls button#cancel-drawing-button { 
    padding: 5px 8px; 
    font-size: 11px; 
    width: 90%; 
    box-sizing: border-box;
  }

  /* Adjust joystick container in landscape */
  #joystick-container {
      bottom: 30px;
      left: 150px; 
  }
}

.player-avatar {
  position: absolute;
  border-radius: 50%;
  border: 1px solid black; 
  box-sizing: border-box;
  transition: none; 
  pointer-events: none; 
  z-index: 5; 
}

.player-name {
    position: absolute;
    background-color: rgba(0, 0, 0, 0.7);
    color: white;
    padding: 1px 3px; 
    border-radius: 2px;
    font-size: 9px; 
    white-space: nowrap;
    transform: translateX(-50%);
    left: 50%;
    pointer-events: none; 
}

.nipple .front {
    background-color: rgba(128, 128, 128, 0.9) !important; 
}
.nipple .back {
    background-color: rgba(200, 200, 200, 0.6) !important; 
}

/* --- Admin Panel Styles --- */
#admin-panel {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 300px;
    max-width: 90%;
    background-color: rgba(240, 240, 240, 0.95);
    border: 1px solid #aaa;
    border-radius: 8px;
    padding: 15px;
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
    z-index: 1001; 
    font-size: 14px;
    color: #333;
}

#admin-panel h3 {
    margin-top: 0;
    margin-bottom: 15px;
    text-align: center;
    color: #d9534f; 
    font-weight: bold;
}

#admin-panel .admin-section {
    margin-bottom: 15px;
    padding: 10px;
    background-color: rgba(255, 255, 255, 0.6);
    border-radius: 4px;
}

#admin-panel .admin-section h4 {
    margin-top: 0;
    margin-bottom: 10px;
    border-bottom: 1px solid #ddd;
    padding-bottom: 5px;
}

#admin-panel label {
    margin-right: 5px;
}

#admin-panel input[type="number"] {
    width: 60px;
    padding: 4px;
    margin-right: 10px;
    border: 1px solid #ccc;
    border-radius: 3px;
}

#admin-panel button {
    padding: 6px 12px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 13px;
    transition: background-color 0.2s ease;
}

#admin-panel #admin-delete-button,
#admin-panel #admin-delete-here-button {
    background-color: #f0ad4e; 
    color: white;
    margin-right: 5px;
}
#admin-panel #admin-delete-button:hover,
#admin-panel #admin-delete-here-button:hover {
    background-color: #ec971f;
}

#admin-panel #admin-reset-map-button {
    background-color: #d9534f; 
    color: white;
    width: 100%; 
}
#admin-panel #admin-reset-map-button:hover {
    background-color: #c9302c;
}

#admin-panel hr {
    border: 0;
    border-top: 1px solid #ccc;
    margin: 15px 0;
}

/* Ensure close button positioning is correct */
#admin-panel .modal-close {
    color: #888;
    background: none;
    border: none;
    font-size: 24px;
    font-weight: bold;
    line-height: 1;
    padding: 0;
    cursor: pointer;
}
#admin-panel .modal-close:hover {
    color: #333;
}

/* --- Edit Square Button --- */
#edit-square-button {
    position: absolute;
    bottom: 20px; 
    left: 50%;
    transform: translateX(-50%);
    padding: 10px 20px;
    font-size: 16px;
    font-weight: bold;
    background-color: #4CAF50; 
    color: white;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    box-shadow: 0 2px 5px rgba(0,0,0,0.2);
    z-index: 18; 
    display: block; 
    transition: background-color 0.2s ease, opacity 0.2s ease; 
}

#edit-square-button:hover:not(:disabled) { 
    background-color: #45a049;
}

/* Styling for the disabled state */
#edit-square-button:disabled,
#edit-square-button.disabled { 
    background-color: #cccccc; 
    color: #666666; 
    cursor: not-allowed;
    opacity: 0.7; 
}

/* Ensure edit button is hidden when drawing controls are visible */
#controls[style*="display: flex;"] ~ #edit-square-button {
    display: none !important;
}

@media (orientation: landscape) {
    #edit-square-button {
        bottom: 20px;
        left: 120px; 
        transform: translateX(0); 
        padding: 8px 15px; 
        font-size: 14px;
    }

    /* Ensure edit button is hidden when landscape drawing controls are visible */
    #controls[style*="display: flex;"] ~ #edit-square-button {
        display: none !important;
    }
}

.settings-row.checkbox-row {
    display: flex;
    align-items: center;
    margin-bottom: 8px;
}

.settings-row.checkbox-row input[type="checkbox"] {
    margin-right: 8px;
}

#image-crop-modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    display: none;
    z-index: 1000;
    justify-content: center;
    align-items: center;
}

.crop-modal-content {
    background: white;
    padding: 20px;
    border-radius: 10px;
    max-width: 90%;
    max-height: 90%;
    display: flex;
    flex-direction: column;
}

.crop-canvas-container {
    position: relative;
    max-width: 100%;
    max-height: 70vh;
    overflow: auto;
}

#source-image-canvas {
    max-width: 100%;
    max-height: 100%;
}

#crop-selector {
    position: absolute;
    border: 2px solid rgba(0, 255, 0, 0.7);
    background: rgba(0, 255, 0, 0.2);
    pointer-events: none;
}

.crop-controls {
    display: flex;
    justify-content: space-between;
    margin-top: 10px;
}

#advanced-drawing-controls {
    position: fixed;
    right: 10px;
    top: 100px;
    background: rgba(255, 255, 255, 0.9);
    padding: 15px;
    border-radius: 8px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.2);
    z-index: 1000;
}

#advanced-drawing-interface {
    position: fixed;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    background: white;
    padding: 20px;
    border-radius: 10px;
    box-shadow: 0 5px 15px rgba(0,0,0,0.3);
    display: flex;
    gap: 20px;
    z-index: 1000;
}

.layer-controls {
    display: flex;
    gap: 10px;
    margin-bottom: 15px;
}

.layer-button {
    padding: 8px 15px;
    border: 1px solid #ccc;
    border-radius: 4px;
    cursor: pointer;
}

.layer-button.active {
    background: #4CAF50;
    color: white;
}

.emoji-picker {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 5px;
    margin: 10px 0;
}

.emoji-option {
    font-size: 20px;
    padding: 5px;
    border: 1px solid #ddd;
    border-radius: 4px;
    cursor: pointer;
}

.extended-palette {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 5px;
    margin-top: 10px;
}

.color-option {
    width: 30px;
    height: 30px;
    border: 1px solid #ccc;
    border-radius: 4px;
    cursor: pointer;
}

#preview-environment {
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.8);
    z-index: 2000;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

#preview-environment canvas {
    background: white;
    border: 2px solid #333;
}

.preview-controls-help {
    color: white;
    margin-top: 10px;
    font-size: 14px;
    text-align: center;
}

#extra-mode-button {
    margin-top: 8px;
    width: 100%;
    padding: 8px;
    background-color: #9c27b0;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-weight: bold;
}

#extra-mode-button:hover {
    background-color: #7b1fa2;
}

#extra-mode-button:disabled {
    background-color: #ccc;
    cursor: not-allowed;
}

.special-effects-menu {
    position: absolute;
    right: -160px; 
    top: 50%;
    transform: translateY(-50%);
    background: rgba(255, 255, 255, 0.9);
    border-radius: 8px 0 0 8px;
    padding: 10px;
    box-shadow: -2px 0 5px rgba(0,0,0,0.2);
    transition: right 0.3s ease;
    z-index: 25;
}

.special-effects-menu.expanded {
    right: 0;
}

.special-effects-toggle {
    position: absolute;
    left: -30px;
    top: 50%;
    transform: translateY(-50%);
    background: rgba(255, 255, 255, 0.9);
    border: none;
    border-radius: 4px 0 0 4px;
    padding: 8px;
    cursor: pointer;
    box-shadow: -2px 0 5px rgba(0,0,0,0.2);
}

.special-effects-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
    width: 150px;
}

.special-effect-button {
    width: 100%;
    height: 30px;
    border: 1px solid #ccc;
    border-radius: 4px;
    cursor: pointer;
    transition: transform 0.2s;
}

.special-effect-button:hover {
    transform: scale(1.05);
}

.special-effect-button[data-effect-type="PORTAL_ORANGE"] {
    background: linear-gradient(45deg, #FF6D00, #FF9100);
}

.special-effect-button[data-effect-type="PORTAL_BLUE"] {
    background: linear-gradient(45deg, #2196F3, #03A9F4);
}