:root {
  /* Retro Vibes - Poolsuite Palette inspired */
  --bg-main: #ffdad5; /* Classic Poolsuite Peach */
  --bg-container: #fff8f0; /* Lighter container bg */
  --bg-cell: #ffffff; /* Canvas default */
  --border-cell: rgba(0, 0, 0, 0.1); /* Default grid line color */
  --text-main: #000000;

  /* Retro Buttons */
  --btn-bg: #ececec;
  --btn-text: #000000;
  --btn-border: #000000;
  --btn-hover: #ffffff;
  --btn-active-bg: #000000;
  --btn-active-text: #ffffff;
}

html,
body {
  background-color: var(--bg-main);
  color: var(--text-main);
  height: 100%;
  width: 100%;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  font-family: "VT323", monospace; /* Retro Font */
  font-size: 20px; /* Slightly larger for retro readability */
  overflow: hidden; /* Prevent scrolling on main body */
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.content-wrapper {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between; /* push footer down */
  height: 100vh;
  width: 100%;
  /* Use safe area inset for notch/island + extra padding. 
     Increased base to 60px to ensure it clears heavy notches if env fails or is small 
  */
  padding-top: max(60px, env(safe-area-inset-top) + 20px);
  padding-bottom: 20px;
}

.game-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 100%;
  flex: 1; /* Take available space */
}

.modal-buttons {
  display: flex;
  gap: 20px; /* Increased gap from 10px */
  margin-top: 25px; /* Increased top margin */
}

/* Retro Button Styles */
button,
.changeGridNumber {
  background-color: var(--btn-bg);
  color: var(--btn-text);
  padding: 0 15px;
  height: 50px;
  border: 2px solid var(--btn-border); /* Thicker border */
  font-weight: normal;
  font-family: "VT323", monospace;
  font-size: 24px; /* Larger Retro Font */
  text-transform: uppercase;
  appearance: none;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 0; /* SHARP CORNERS */
  cursor: pointer;
  box-shadow: 4px 4px 0px rgba(0, 0, 0, 1); /* Hard Shadow */
  transition: transform 0.1s, box-shadow 0.1s;
}

button:hover,
.changeGridNumber:hover {
  background-color: var(--btn-hover);
  transform: translate(-1px, -1px);
  box-shadow: 5px 5px 0px rgba(0, 0, 0, 1);
}

button:active,
.changeGridNumber:active {
  transform: translate(2px, 2px);
  box-shadow: 2px 2px 0px rgba(0, 0, 0, 1);
  background-color: var(--btn-active-bg);
  color: var(--btn-active-text);
}

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

.header {
  color: var(--text-main);
  font-weight: bold;
  font-size: 40px; /* Larger title */
  letter-spacing: 2px;
  width: 100%;
  padding: 5px 20px;
  display: flex; /* Flexbox for alignment */
  align-items: center;
  justify-content: center; /* Center the title */
  position: relative; /* For absolute positioning of children */
  z-index: 10;
}

.title-text {
  /* Ensure title stays centered even with side buttons */
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  text-align: center;
  /* BOLDER & EYE-CATCHING VIBE */
  font-size: 52px; /* Bigger */
  -webkit-text-stroke: 2px var(--text-main); /* Thicker stroke */
  /* Sharper, harder shadow for more retro vibe */
  text-shadow: 3px 3px 0px rgba(0, 0, 0, 0.3); /* Deeper shadow */
  white-space: nowrap;
  letter-spacing: 5px; /* More spacing for the bold look */
}

#undoBtn {
  background-color: var(--btn-bg);
  border: 2px solid var(--btn-border);
  color: var(--btn-text);
  cursor: pointer;
  padding: 8px;
  border-radius: 0;
  transition: transform 0.1s, box-shadow 0.1s;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  margin: 0;
  appearance: none;
  box-shadow: 4px 4px 0px rgba(0, 0, 0, 1);
  position: absolute;
  left: 10px;
}

#undoBtn:hover {
  background-color: var(--btn-hover);
  transform: translate(-1px, -1px);
  box-shadow: 5px 5px 0px rgba(0, 0, 0, 1);
  border: 2px solid var(--btn-border);
}

#undoBtn:active {
  transform: translate(2px, 2px);
  box-shadow: 2px 2px 0px rgba(0, 0, 0, 1);
  background-color: var(--btn-active-bg);
  color: var(--btn-active-text);
}

#undoBtn svg {
  width: 24px;
  height: 24px;
  filter: none;
}

.container {
  display: flex;
  box-shadow: 8px 8px 0px rgba(0, 0, 0, 1); /* Hard Retro Shadow */
  border: 2px solid #000;
  background-color: var(--bg-container); /* Light background for grid area */

  /* Responsive sizing - FITS AVAILABLE SPACE WITHOUT OVERFLOW */
  width: 85vmin; /* Use vmin to never exceed viewport */
  height: 85vmin;

  flex-wrap: wrap;
  margin: 0 auto 10px auto;
  touch-action: none;
}

.cell {
  border: 1px dotted var(--border-cell); /* Dotted grid lines are very retro */
  flex-wrap: wrap;
  display: flex;
}

.cell:hover {
  background-color: rgb(107, 107, 107);
}

.tools {
  display: flex;
  gap: 15px;
  align-items: center;
  justify-content: center;
  margin-top: 20px;
  width: 100%;
  flex-wrap: wrap; /* allow wrapping on small screens */
}

/* Remove old grid styles */

#eraserBtn {
  margin-top: 0;
}

#eraserBtn.active {
  background-color: var(--btn-active-bg);
  color: var(--btn-active-text);
  box-shadow: inset 3px 3px 0px rgba(255, 255, 255, 0.3);
}

#resetBtn {
  margin-top: 0;
  width: auto; /* Reset specific width */
  border-color: #000;
  padding: 0 15px; /* Match other buttons */
}

#resetBtn:hover {
  background-color: #ff5555;
  color: white;
  border-color: #000;
}

#colorPicker {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  width: 54px; /* Match button height approx w/ borders */
  height: 54px;
  background-color: transparent;
  border: none;
  cursor: pointer;
  margin: 0;
  padding: 0;
}

#colorPicker::-webkit-color-swatch-wrapper {
  padding: 0;
}

#colorPicker::-webkit-color-swatch {
  /* Match buttons */
  border: 2px solid var(--btn-border);
  border-radius: 0;
  box-shadow: 4px 4px 0px rgba(0, 0, 0, 1);
}

#colorPicker::-moz-color-swatch {
  border: 2px solid var(--btn-border);
  border-radius: 0;
  box-shadow: 4px 4px 0px rgba(0, 0, 0, 1);
}

.footer {
  color: var(--text-main);
  display: flex;
  justify-content: center;
  width: 100%;
  font-size: 18px;
  opacity: 0.7;
}

/* Landscape Mode (Mobile & Tablet) - Side by Side Layout with CENTERED Grid */
/* Landscape Mode (Mobile & Tablet) - Absolute Layout to Guarantee Center */
@media screen and (orientation: landscape) {
  .content-wrapper {
    flex-direction: column;
    justify-content: space-between;
    height: 100vh;
    padding-top: max(20px, env(safe-area-inset-top));
    padding-bottom: 10px;
    position: relative; /* Context reference */
  }

  .game-area {
    display: flex;
    align-items: center; /* Vertical Center */
    justify-content: center; /* Horizontal Center (Grid) */
    width: 100%;
    flex: 1;
    position: relative;
  }

  .tools {
    position: absolute;
    /* Calculated placement: 50% (Center) + 40vmin (Half Grid) + 20px (Gap) */
    /* This puts the tools to the LEFT of the centered grid */
    right: calc(50% + 40vmin + 20px);
    left: auto; /* Unset the previous left pinning */
    top: 50%;
    transform: translateY(-50%); /* Vertical Center */

    display: flex;
    flex-direction: column;
    width: auto;
    margin: 0;
    gap: 15px;
    z-index: 20; /* Ensure above anything */
  }

  /* The Grid Container */
  .container {
    margin: 0 auto; /* Dead Center */
    /* Fit nicely in landscape height */
    width: 80vmin;
    height: 80vmin;
    flex-shrink: 0;
    box-shadow: 8px 8px 0px rgba(0, 0, 0, 1);
  }
}

/* Theme Modal */
.theme-modal {
  display: block; /* Always block, controlled by opacity/visibility */
  visibility: hidden;
  position: fixed;
  z-index: 1000; /* High z-index to sit on top */
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.4); /* Black w/ opacity */
  opacity: 0;
  transition: opacity 0.3s ease, visibility 0.3s ease;
  backdrop-filter: blur(2px); /* Slight blur for modern-retro feel */
}

.theme-modal.show {
  visibility: visible;
  opacity: 1;
}

.modal-content {
  background-color: var(--bg-container);
  /* Add safe area inset for dynamic island/notch */
  margin: max(5%, env(safe-area-inset-top) + 10px) auto 5% auto;
  padding: 20px;
  border: 2px solid var(--btn-border);
  width: 90%;
  max-width: 600px;
  max-height: 80vh;
  overflow-y: auto;
  border-radius: 0;
  box-shadow: 10px 10px 0px rgba(0, 0, 0, 1);

  /* Animation Start State */
  transform: scale(0.9);
  opacity: 0;
  transition: transform 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275),
    opacity 0.2s ease;
}

.theme-modal.show .modal-content {
  transform: scale(1);
  opacity: 1;
}

.close {
  color: #000;
  float: right;
  font-size: 32px;
  font-weight: bold;
  cursor: pointer;
  line-height: 20px;
}

.close:hover,
.close:focus {
  color: #ff5555;
  text-decoration: none;
  cursor: pointer;
}

/* Remove old single grid setup, now handled by .theme-grid-container */
.theme-presets {
  margin-bottom: 20px;
}

.theme-grid-container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 15px; /* Spacing for buttons */
  margin-bottom: 20px;
  padding: 10px; /* Internal padding */
}

.theme-section-header {
  color: var(--text-main);
  margin: 15px 0 15px 0;
  font-size: 24px;
  opacity: 1;
  border-bottom: 4px solid #000; /* Heavier underline */
  display: inline-block;
  text-transform: uppercase;
}

.theme-card {
  background-color: var(--bg-container);
  border: 2px solid var(--btn-border);
  border-radius: 0;
  padding: 10px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  transition: all 0.1s ease;
  position: relative; /* For actions */
  box-shadow: 4px 4px 0px rgba(0, 0, 0, 1); /* Pop them out */
  margin: 5px; /* Extra margin around cards */
}

.theme-card:hover {
  transform: translate(-2px, -2px);
  box-shadow: 6px 6px 0px rgba(0, 0, 0, 1);
  background-color: #fff;
  z-index: 2;
}

.theme-card.active {
  border-color: #000;
  box-shadow: inset 0 0 0 2px #000, 4px 4px 0px rgba(0, 0, 0, 1); /* Inner border to show activity */
  background-color: #fff;
}

.card-actions {
  display: flex;
  gap: 5px;
  width: 100%;
  justify-content: flex-end;
  margin-top: 5px;
  border-top: 1px dotted rgba(0, 0, 0, 0.2);
  padding-top: 5px;
}

.card-actions button {
  background: none;
  border: none;
  color: var(--text-main);
  width: 24px;
  height: 24px;
  padding: 0;
  font-size: 16px;
  opacity: 0.6;
  box-shadow: none;
}

.card-actions button:hover {
  opacity: 1;
  background: none;
  transform: scale(1.2);
  box-shadow: none;
}

.card-actions button.delete-btn:hover {
  color: #ff5555;
}

.theme-preview {
  display: flex;
  gap: 0; /* Tight */
  width: 100%;
  height: 40px;
  border-radius: 0;
  border: 2px solid #000;
  overflow: hidden;
  margin-bottom: 5px;
}

.theme-preview div {
  flex: 1;
  height: 100%;
}

.theme-card span {
  font-size: 18px; /* Bigger text */
  font-weight: normal;
  color: var(--text-main);
  text-align: center;
  line-height: 1.1;
}

.custom-theme-inputs {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 10px;
}

.custom-theme-inputs label {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 18px;
}
