/* --- Basic Styles & Reset --- */
* {
  box-sizing: border-box;
}
html {
  font-size: 16px;
}
body {
  font-family: sans-serif;
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: 100vh;
  margin: 0;
  padding-top: 60px; /* For fixed header */
  padding-bottom: 60px; /* For fixed footer */
  padding-left: 5px;
  padding-right: 5px;
  background-color: #f0f0f0;
  touch-action: manipulation; /* Prevent double-tap zoom on touch devices */
}

/* --- Fixed Header --- */
header {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  background-color: #4682b4; /* Steel Blue */
  color: white;
  padding: 10px 0;
  text-align: center;
  z-index: 100;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
}
header h1 {
  margin: 0;
  font-size: 1.8rem;
  line-height: 1.2;
}

.game-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  max-width: 900px;
  margin: 15px auto 0 auto;
}

/* --- Top Controls Area (Options + Help Button) --- */
.game-options {
  display: flex;
  flex-wrap: nowrap; /* Prevent wrapping */
  overflow-x: auto; /* Allow horizontal scroll if needed */
  -webkit-overflow-scrolling: touch; /* Smooth scrolling on iOS */
  justify-content: flex-start;
  align-items: center;
  gap: 15px;
  margin-bottom: 15px;
  padding: 8px 12px;
  background-color: #e9e9e9;
  border-radius: 5px;
  box-shadow: inset 1px 1px 3px rgba(0, 0, 0, 0.1);
  font-size: 0.9rem;
  width: 95%;
  max-width: 600px; /* Adjusted max-width */
}
.mode-selector,
.sound-selector {
  display: flex;
  align-items: center;
  flex-shrink: 0; /* Prevent shrinking */
}
.game-options label {
  cursor: pointer;
  margin-left: 5px;
  user-select: none;
  white-space: nowrap;
}
.game-options input[type="checkbox"] {
  cursor: pointer;
  vertical-align: middle;
  flex-shrink: 0;
}
/* Style disabled checkboxes and labels */
.game-options input[type="checkbox"]:disabled + label {
  cursor: not-allowed;
  color: #888;
}
/* Help Button */
#instructionsButton {
  padding: 6px 12px;
  font-size: 0.9rem;
  cursor: pointer;
  background-color: #d0d0d0;
  border: 1px solid #aaa;
  border-radius: 4px;
  transition: background-color 0.2s ease;
  flex-shrink: 0;
  white-space: nowrap;
  margin-left: auto; /* Push to the right */
}
#instructionsButton:hover {
  background-color: #c0c0c0;
}

/* --- Start Button and Timer Row --- */
.start-timer-row {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 20px;
  margin-bottom: 20px;
}
#goButton {
  padding: 12px 30px;
  font-size: 1.2rem;
  cursor: pointer;
  border-radius: 5px;
  transition: background-color 0.2s ease, border-color 0.2s ease;
  min-height: 44px; /* Accessibility minimum */
  margin-bottom: 0;
  border-width: 2px;
  border-style: solid;
}
/* Button states */
#goButton.start-state {
  background-color: #28a745; /* Green */
  color: white;
  border-color: #1e7e34;
}
#goButton.reset-state {
  background-color: #dc3545; /* Red */
  color: white;
  border-color: #bd2130;
}
#goButton.start-state:hover {
  background-color: #218838;
}
#goButton.reset-state:hover {
  background-color: #c82333;
}
/* Timer display */
.timer {
  font-size: 1.5rem;
  color: #444;
  margin-bottom: 0;
}

/* --- Game Layout --- */
.center-row {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 20px;
  position: relative;
  width: 100%;
  flex-wrap: nowrap; /* Keep central elements in line */
}
.ab-button-container {
  display: flex;
  justify-content: center;
  gap: 80px; /* Space between A/B buttons */
  width: 100%;
  margin-top: 10px;
  margin-bottom: 15px; /* Reduced bottom margin */
  height: var(--button-size); /* Match button height */
}

/* --- CSS Variables for Sizing --- */
:root {
  --card-width: 110px;
  --card-height: 165px;
  --button-size: 45px; /* Diameter for A/B buttons */
  --number-button-size: 30px; /* Diameter for 1-5 buttons */
}

/* --- Piles and Cards --- */
.pile {
  width: var(--card-width);
  height: var(--card-height);
  border: 1px solid #ccc;
  background-color: #fff;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 1.2rem;
  font-weight: bold;
  color: #555;
  cursor: default;
  box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.1);
  user-select: none;
  flex-shrink: 0;
}
.card-container {
  width: var(--card-width);
  height: var(--card-height);
  border: 1px solid #000;
  background-color: #fff;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  box-shadow: 3px 3px 7px rgba(0, 0, 0, 0.2);
  user-select: none;
  position: relative; /* For positioning number button */
  cursor: pointer;
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center center;
  flex-shrink: 0;
}
/* Styles for empty hand slots */
.card-container.empty-slot {
  background-color: #eee;
  border-style: dashed;
  border-color: #ccc;
  cursor: default;
}
.card-container.empty-slot .deckofcards-card {
  background-image: none !important;
}
.card-container.empty-slot .card-number-button {
  background-color: lightgray;
  color: #888;
  cursor: default;
}
/* Styles for face-down center piles (before game starts or when empty) */
.card-container.face-down {
  background-image: url("https://deckofcardsapi.com/static/img/back.png");
  background-size: contain;
  border-color: #999;
  font-size: 0; /* Hide potential text */
  cursor: pointer;
}
.card-container.face-down .deckofcards-card {
  background-image: none !important;
} /* Hide card image if face down */

/* --- Cards Remaining Indicator --- */
.cards-remaining-display {
  margin-bottom: 10px; /* Space above user hand */
  font-size: 1rem;
  font-weight: bold;
  color: #333;
  text-align: center;
  width: 100%;
  min-height: 1.2em; /* Prevent layout jump */
}

/* --- User Hand Area --- */
.user-hand {
  display: flex;
  gap: 10px;
  margin-bottom: 25px;
  flex-wrap: nowrap; /* Allow horizontal scroll on small screens */
  justify-content: center;
  align-items: flex-start;
  min-height: calc(var(--card-height) + 10px); /* Ensure space */
  width: 95%;
  max-width: calc(
    5 * var(--card-width) + 4 * 10px + 20px
  ); /* Max width for 5 cards + gaps + padding */
  padding: 5px;
  overflow-x: auto; /* Enable horizontal scroll */
  -webkit-overflow-scrolling: touch; /* Smooth scroll iOS */
}
.user-hand .card-container {
  margin-bottom: 0;
}

/* --- Messages and Internal Card Element --- */
.message {
  margin-top: 15px;
  margin-bottom: 10px;
  font-weight: bold;
  font-size: 1.1rem;
  min-height: 1.5em; /* Prevent layout shifts */
  text-align: center;
  color: #333;
  padding: 0 10px;
}
.deckofcards-card {
  width: 100%;
  height: 100%;
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center center;
  flex-shrink: 0;
}
/* Style for selected hand card */
.selected {
  border: 4px solid blue !important;
  box-shadow: 0 0 10px blue;
}
/* Draw Button Container */
.controls {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 10px;
  min-height: 50px; /* Reserve space */
  width: 100%;
}

/* --- Draw Button --- */
.draw-button {
  padding: 10px 25px;
  font-size: 1.1rem;
  cursor: pointer;
  border: 2px solid gray;
  border-radius: 5px;
  background-color: #e0e0e0;
  transition: background-color 0.2s ease, border-color 0.2s ease,
    border-width 0.2s ease;
  min-height: 44px;
}
.draw-button:disabled {
  cursor: not-allowed;
  background-color: #f5f5f5;
  color: #aaa;
  border-color: #ccc;
}
.draw-button:not(:disabled):hover {
  background-color: #d0d0d0;
}
/* Highlight style when draw is the only option */
.draw-button.highlight-action {
  border-color: #555 !important; /* Darker border */
  border-width: 3px !important; /* Thicker border */
  background-color: #e8e8e8; /* Optional: slightly different background */
}

/* --- Circular Buttons (A, B, 1-5) --- */
.circle-button {
  width: var(--button-size);
  height: var(--button-size);
  border-radius: 50%;
  background-color: lightblue;
  color: white;
  font-size: 1.2rem;
  font-weight: bold;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.2);
  border: none;
  user-select: none;
  transition: background-color 0.2s ease;
  flex-shrink: 0;
}
.circle-button:hover {
  background-color: #90cde3;
}
/* A/B pile buttons (inherit from circle-button) */
.pile-button {
  /* Specific styles if needed */
}
/* 1-5 number buttons on hand cards */
.card-number-button {
  position: absolute;
  bottom: 2px; /* Position at bottom */
  left: 50%;
  transform: translateX(-50%); /* Center horizontally */
  width: var(--number-button-size);
  height: var(--number-button-size);
  font-size: 0.9rem;
}

/* --- Styles for New Elements --- */

/* Deck Selector in Game Options */
.deck-selector {
  display: flex;
  align-items: center;
  gap: 5px; /* Space between label and radios */
  flex-shrink: 0; /* Prevent shrinking on small screens */
  margin-right: 15px; /* Add some space before next option */
}
.deck-selector label {
  /* Style for the "Decks:" label and radio labels */
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
  margin-right: 3px; /* Space after radio button */
}
.deck-selector input[type="radio"] {
  cursor: pointer;
  vertical-align: middle;
  margin: 0 2px 0 0; /* Adjust spacing around radio */
}
.deck-selector input[type="radio"]:disabled + label {
  cursor: not-allowed;
  color: #888;
}

/* --- Best Time Display --- */
.times-display {
  margin-top: 15px;
  border: 1px solid #ccc;
  padding: 10px 15px;
  background-color: #f9f9f9;
  border-radius: 5px;
  width: 95%;
  max-width: 650px; /* Slightly wider to accommodate more info */
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between; /* Distribute space */
  align-items: center;
  gap: 10px 15px; /* Row and column gap */
}

.best-time {
  font-size: 1rem;
  color: #555;
  transition: background-color 0.3s ease;
  padding: 3px 10px; /* More horizontal padding */
  border-radius: 3px;
  line-height: 1.4;
  text-align: left; /* Align left for better readability */
  flex-basis: 48%; /* Try to fit two per row */
  min-width: 250px; /* Ensure enough width for longer text */
  flex-shrink: 0;
}

.last-time {
  color: #777;
  font-size: 0.9rem;
  flex-basis: 100%; /* Make last time take full width */
  text-align: center;
  margin-top: 5px; /* Add slight space above */
  padding: 3px 6px;
  border-radius: 3px;
  line-height: 1.4;
}
/* Style for the currently active best time mode */
.best-time.active-mode {
  font-weight: bold;
  background-color: #dff0d8; /* Light green highlight */
}

/* --- Instructions Modal Styles --- */
.modal {
  display: none; /* Hidden by default */
  position: fixed; /* Stay in place */
  z-index: 1001; /* Sit on top */
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  overflow: hidden; /* Prevent background scroll */
  background-color: rgba(0, 0, 0, 0.5); /* Black w/ opacity */
  justify-content: center; /* Center content horizontally */
  align-items: center; /* Center content vertically */
}

/* Modal Content Box */
.modal-content {
  background-color: #fefefe;
  margin: auto; /* Center horizontally */
  border: 1px solid #888;
  width: 90%;
  max-width: 550px;
  border-radius: 8px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2), 0 6px 20px rgba(0, 0, 0, 0.19);
  position: relative; /* For positioning close button */
  max-height: 80vh; /* Limit height */
  display: flex; /* Use Flexbox */
  flex-direction: column; /* Stack body and footer */
  overflow: hidden; /* Hide overflow before body handles scroll */
}

/* Close Button ('X') */
.close-button {
  color: #aaa;
  position: absolute;
  top: 8px;
  right: 15px;
  font-size: 30px;
  font-weight: bold;
  line-height: 1;
  z-index: 10; /* Above body/footer */
  padding: 0 5px;
  background-color: #fefefe; /* Prevent text overlap */
  border-radius: 50%; /* Make it look nicer */
  cursor: pointer;
}
.close-button:hover,
.close-button:focus {
  color: black;
  text-decoration: none;
}

/* Modal Body (Scrollable content area) */
.modal-body {
  padding: 25px;
  padding-bottom: 15px; /* Space above footer */
  overflow-y: auto; /* Enable vertical scrolling */
  flex-grow: 1; /* Allow body to fill available space */
  line-height: 1.6;
  font-size: 1rem;
}
.modal-body h2 {
  margin-top: 0;
  color: #333;
  text-align: center;
  margin-bottom: 15px;
}
.modal-body p {
  margin-bottom: 10px;
}

/* Modal Footer (Fixed at bottom) */
.modal-footer {
  padding: 15px 25px;
  flex-shrink: 0; /* Prevent footer from shrinking */
  border-top: 1px solid #eee; /* Separator line */
  background-color: #f9f9f9; /* Optional different background */
  text-align: center; /* Center button */
  border-bottom-left-radius: 8px; /* Match parent border radius */
  border-bottom-right-radius: 8px;
}

/* Bottom Close Button */
.modal-close-btn {
  display: inline-block;
  padding: 8px 25px;
  font-size: 1rem;
  cursor: pointer;
  background-color: #e0e0e0;
  border: 1px solid #aaa;
  border-radius: 4px;
  transition: background-color 0.2s ease;
  margin: 0; /* Footer handles centering */
}
.modal-close-btn:hover {
  background-color: #c7c7c7;
}

/* --- Fixed Footer --- */
footer {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  background-color: #2d5778; /* Darker Blue */
  color: #e0e0e0;
  text-align: center;
  padding: 10px 0;
  font-size: 0.8rem;
  border-top: 1px solid #4a6a8a;
  z-index: 100;
  box-shadow: 0 -2px 4px rgba(0, 0, 0, 0.1);
}
footer a {
  color: #87ceeb; /* Sky Blue */
  text-decoration: none;
}
footer a:hover {
  text-decoration: underline;
}

/* --- Media Queries for Responsiveness --- */

/* Larger screens (adjust breakpoint as needed) */
@media (min-width: 850px) {
  .user-hand {
    flex-wrap: nowrap;
  } /* Ensure hand stays in line */
}

/* Tablet size */
@media (max-width: 768px) {
  html {
    font-size: 15px;
  }
  header h1 {
    font-size: 1.6rem;
  }
  body {
    padding-top: 55px;
    padding-bottom: 50px;
  }
  :root {
    --card-width: 90px;
    --card-height: 135px;
    --button-size: 40px;
    --number-button-size: 28px;
  }
  .center-row {
    gap: 15px;
  }
  .user-hand {
    gap: 8px;
    max-width: calc(5 * var(--card-width) + 4 * 8px + 20px);
  }
  .ab-button-container {
    gap: 60px;
  }
  .timer {
    font-size: 1.4rem;
  }
  .times-display {
    margin-top: 15px;
    gap: 8px 12px;
  }
  .best-time {
    min-width: 220px; /* Adjust min-width */
  }
  .modal-content {
    width: 85%;
  }
  footer {
    padding: 8px 0;
    font-size: 0.75rem;
  }
}

/* Smaller tablets and large phones */
@media (max-width: 600px) {
  .best-time {
    flex-basis: 100%; /* Stack best times vertically */
    text-align: center;
    padding-left: 6px;
    min-width: unset; /* Remove min-width */
  }
  .game-options {
    max-width: 95%; /* Allow options to take more width */
  }
}

/* Mobile phones */
@media (max-width: 480px) {
  html {
    font-size: 14px;
  }
  header h1 {
    font-size: 1.4rem;
  }
  body {
    padding-top: 50px;
    padding-bottom: 45px;
  }
  :root {
    --card-width: 70px;
    --card-height: 105px;
    --button-size: 38px;
    --number-button-size: 26px;
  }
  .center-row {
    gap: 10px;
  }
  .user-hand {
    gap: 4px;
    max-width: calc(5 * var(--card-width) + 4 * 4px + 10px);
  }
  .pile {
    font-size: 1rem;
  }
  .ab-button-container {
    gap: 50px;
    margin-bottom: 15px;
  }
  .timer {
    font-size: 1.3rem;
  }
  .start-timer-row {
    gap: 15px;
  }
  #goButton {
    padding: 10px 20px;
    font-size: 1.1rem;
  }
  .draw-button {
    padding: 8px 20px;
    font-size: 1rem;
  }
  .message {
    font-size: 1rem;
    margin-top: 10px;
  }
  .game-options {
    gap: 10px;
    justify-content: flex-start;
  }
  .times-display {
    margin-top: 10px;
    padding: 8px 10px;
    gap: 5px 10px;
  }
  .best-time,
  .last-time {
    font-size: 0.85rem;
  }
  .last-time {
    font-size: 0.75rem;
  }
  .cards-remaining-display {
    font-size: 0.9rem;
    margin-bottom: 8px;
  } /* Adjust card count font */
  .modal-content {
    width: 90%;
    max-height: 75vh;
  }
  .modal-body {
    padding: 20px;
  }
  .modal-footer {
    padding: 10px 20px;
  }
  .close-button {
    font-size: 26px;
    top: 5px;
    right: 10px;
  }
  footer {
    padding: 6px 0;
    font-size: 0.7rem;
  }
}

/* Small mobile phones */
@media (max-width: 360px) {
  header h1 {
    font-size: 1.3rem;
  }
  body {
    padding-top: 48px;
    padding-bottom: 40px;
  }
  :root {
    --card-width: 55px;
    --card-height: 82.5px;
    --button-size: 35px;
    --number-button-size: 24px;
  }
  .center-row {
    gap: 8px;
  }
  .user-hand {
    gap: 3px;
    max-width: calc(5 * var(--card-width) + 4 * 3px + 10px);
  }
  .ab-button-container {
    gap: 40px;
  }
  .start-timer-row {
    flex-direction: column;
    gap: 8px;
  } /* Stack start button and timer */
  .game-options {
    font-size: 0.8rem;
    gap: 8px;
  }
  #instructionsButton {
    padding: 5px 8px;
  }
  .best-time,
  .last-time {
    font-size: 0.8rem;
  }
  .cards-remaining-display {
    font-size: 0.85rem;
  }
  footer {
    padding: 5px 0;
  }
}
