/* ==========================================================================
   1. Modern CSS Reset & Variable Definitions
   ========================================================================== */
   * {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
  }

  :root {
    --bg-color: #f8fafc;
    --card-bg: #ffffff;
    --text-primary: #1e293b;
    --text-secondary: #64748b;
    --accent: #3b82f6;
    --accent-hover: #2563eb;
    --border-color: #e2e8f0;
    --shadow-md: 0 4px 6px -1px rgba(0,0,0,0.1), 0 2px 4px -1px rgba(0,0,0,0.06);
    --radius: 12px;
    --font-stack: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
  }

  html {
    /* Stop pull-to-refresh / rubber-banding while playing */
    overscroll-behavior: none;
  }

  body {
    font-family: var(--font-stack);
    background-color: var(--bg-color);
    color: var(--text-primary);
    height: 100vh;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-between;
    overflow: hidden;
    /* No double-tap zoom on buttons/controls */
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
  }

  header, footer {
    width: 100%;
    padding: 0.5rem 1rem;
    flex-shrink: 0;
  }

  /* ==========================================================================
     2. Main Layout Grid
     ========================================================================== */
  main {
    display: flex;
    flex-direction: row;
    align-items: stretch;
    justify-content: center;
    gap: 1.5rem;
    width: 100vw;
    padding: 1rem;
    flex: 1;
    overflow: hidden;
  }

  /* ==========================================================================
     3. Game Board (Canvas Workspace)
     ========================================================================== */
  section {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    box-shadow: var(--shadow-md);
    padding: 1rem;
    overflow: hidden;
  }

  #board {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
    display: block;
    /* The game handles all touch itself: no scrolling, zooming, or
       callouts starting from the canvas. */
    touch-action: none;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
    -webkit-user-select: none;
  }

  /* ==========================================================================
     4. Sidebar Controls Panel
     ========================================================================== */
  #controls {
    /* Transparent stack: the three panel-boxes inside are the visible boxes. */
    background: none;
    border: none;
    box-shadow: none;
    padding: 0;
    width: 340px;
    min-width: 340px;
    display: flex;
    flex-direction: column;
    gap: 1.5rem; /* match the board/sidebar separation */
    justify-content: flex-start;
    height: 100%;
  }

  /* The three boxes on the right: scores, setup, test panel. */
  .panel-box {
    background-color: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    box-shadow: var(--shadow-md);
    padding: 24px 16px; /* breathing room so content isn't jammed */
    flex: 1 1 0; /* share the sidebar's full height equally */
    display: flex;
    flex-direction: column;
    justify-content: center;
  }

  /* ==========================================================================
     5. Scoreboard Component
     ========================================================================== */
  .score-board {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
    user-select: none;
    width: 100%;
  }

  /* Each score sits in its own box in the player's board color.
     White text is the most visible on both. The side to move gets
     a thick white border; the other keeps a faint one (same width,
     so the layout doesn't shift). */
  .score-side {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.25rem;
    padding: 0.75rem 0.25rem;
    border-radius: 8px;
    border: 5px solid rgba(255, 255, 255, 0.25);
  }

  #p1Card { background-color: #BF616A; }
  #p2Card { background-color: #5E81AC; }

  #p1Card.active { border-color: #4a1f24; }
  #p2Card.active { border-color: #1e2f47; }

  .score-side .score-name {
    font-size: 0.75rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: #ffffff;
  }

  .score-side output {
    font-size: 2rem;
    font-weight: 700;
    color: #ffffff;
    font-variant-numeric: tabular-nums;
  }

  /* Thinking indicator: head outline with a rotating gear, in the
     card's top-right corner. DotGame adds .thinking to the card while
     that player's engine call is in flight (AI players only). */
  .score-side {
    position: relative;
  }

  .thinking-icon {
    position: absolute;
    top: 6px;
    right: 6px;
    width: 27px;
    height: 27px;
    opacity: 0;
    transition: opacity 0.25s ease;
    pointer-events: none;
  }

  .score-side.thinking .thinking-icon {
    opacity: 1;
  }

  .thinking-icon svg {
    display: block;
    width: 100%;
    height: 100%;
  }

  /* The thinking indicator is a static hourglass. (History: a
     spinning gear, then thought dots in a head — three animation
     mechanisms all froze on Mike's phone while the solver blocks
     the main thread, so the indicator promises no motion: it
     appears when the think starts and disappears when the move
     lands.) */
  .thinking-icon .hourglass {
    fill: rgba(255, 255, 255, 0.95);
  }

  .hidden {
    display: none !important;
  }

  /* Container dropping right under the scoreboard */
.player-toggles {
  display: flex;
  justify-content: space-around;
  align-items: flex-start;
  gap: 12px;
  padding: 6px 12px;
  background-color: rgba(255, 255, 255, 0.05);
  border-radius: 6px;
  margin-bottom: 12px;
}

/* One vertical stack per player: the player-type dropdown. */
.player-col {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 6px;
}

.player-col .toggle-select {
  width: 100%;
}

/* Dev test panel: its own little box, hidden until the Konami code. */
.test-box {
  position: relative;
  gap: 1rem;
  padding-top: 32px; /* clearance so the X doesn't sit on the dropdown */
}

.test-box-close {
  position: absolute;
  top: 4px;
  right: 8px;
  background: none;
  border: none;
  font-size: 1.25rem;
  line-height: 1;
  padding: 4px 6px;
  color: var(--text-secondary);
  cursor: pointer;
}

.test-box-close:hover {
  color: var(--text-primary);
}

.games-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.games-step {
  padding: 4px 14px;
  font-size: 13px;
  background-color: #222;
  color: #fff;
  border: 1px solid #444;
  border-radius: 4px;
  cursor: pointer;
}

.games-step:hover:not(:disabled) {
  border-color: #0076ff;
}

.games-step:disabled {
  opacity: 0.4;
  cursor: default;
}

#games-counter,
#history-counter {
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}

/* Move-history review in the setup box. The Resume AI button spans the
   panel so the stalled game has an obvious way forward. */
#historyBox {
  margin-top: 0.75rem;
}

#history-unpause {
  width: 100%;
  margin-top: 0.5rem;
  padding: 0.5rem 1rem;
  font-weight: 600;
}





/* Dropdown styling matching a dark/modern theme fallback */
.toggle-select {
  padding: 4px 8px;
  font-size: 13px;
  background-color: #222;
  color: #fff;
  border: 1px solid #444;
  border-radius: 4px;
  cursor: pointer;
  outline: none;
  transition: border-color 0.2s ease;
}

.toggle-select:hover,
.toggle-select:focus {
  border-color: #0076ff; /* Highlight matching user interaction focus */
}

/* Test-panel dropdowns: shrink to fit the 340px panel instead of letting
   long option text blow out the fieldset's min-content width. */
.test-select {
  flex: 1;
  min-width: 0;
  max-width: 100%;
}




  /* ==========================================================================
     6. Semantic Configuration Panel & Sliders
     ========================================================================== */
  .settings-group {
    border: none;
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
    width: 100%;
    /* fieldset defaults to min-width: min-content, which let the long
       test-case option text force the whole panel wider than 340px */
    min-width: 0;
    margin: 0;
    padding: 0;
  }

  /* Utility to hide accessibility legends cleanly from visual layout viewports */
  .visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    border: 0;
  }

  .control-group {
    display: flex;
    flex-direction: row;
    gap: 1rem;
    align-items: center;
    min-width: 0;
  }

  .control-group label {
    width: auto;
    min-width: 65px;
    flex-shrink: 0;
    white-space: nowrap;
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    user-select: none;
  }

  #controls input[type="range"] {
    -webkit-appearance: none;
    appearance: none;
    flex: 1;
    min-width: 0;
    height: 6px;
    background: var(--border-color);
    border-radius: 3px;
    outline: none;
  }

  #controls input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
    transition: transform 0.1s ease, background-color 0.1s ease;
  }

  #controls input[type="range"]::-webkit-slider-thumb:hover {
    background: var(--accent-hover);
    transform: scale(1.1);
  }

  #controls input[type="range"]::-moz-range-thumb {
    width: 18px;
    height: 18px;
    border: none;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
    transition: transform 0.1s ease, background-color 0.1s ease;
  }

  #controls input[type="range"]::-moz-range-thumb:hover {
    background: var(--accent-hover);
    transform: scale(1.1);
  }

  .control-group output {
    width: 32px;
    min-width: 32px;
    text-align: right;
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--text-primary);
    font-variant-numeric: tabular-nums;
    user-select: none;
  }

  /* ==========================================================================
     7. Buttons
     ========================================================================== */
  #controls input[type="button"] {
    font-family: inherit;
    font-size: 1rem;
    font-weight: 600;
    padding: 0.75rem 1rem;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    transition: background-color 0.15s ease, transform 0.1s ease;
    user-select: none;
    width: 100%;
  }

  #newGameButton {
    background-color: var(--accent);
    color: #ffffff;
  }

  #newGameButton:hover {
    background-color: var(--accent-hover);
  }

  #newGameButton:active {
    transform: scale(0.98);
  }

  /* ==========================================================================
     8. Game-over banner (replaces the old alert() popup)
     ========================================================================== */
  .game-over-banner {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 1000;
    background: rgba(15, 23, 42, 0.55);
    align-items: center;
    justify-content: center;
    padding: 1.5rem;
  }

  .game-over-banner.show {
    display: flex;
  }

  .game-over-card {
    background: var(--card-bg);
    border-radius: var(--radius);
    box-shadow: var(--shadow-md);
    padding: 2rem 2.5rem;
    text-align: center;
    max-width: 90vw;
  }

  .game-over-card h2 {
    font-size: 1.75rem;
    margin-bottom: 0.5rem;
  }

  .game-over-card p {
    font-size: 1.25rem;
    color: var(--text-secondary);
    font-variant-numeric: tabular-nums;
    margin-bottom: 1.25rem;
  }

  .game-over-card button {
    font-family: inherit;
    font-size: 1rem;
    font-weight: 600;
    padding: 0.75rem 1.5rem;
    border: none;
    border-radius: 8px;
    background-color: var(--accent);
    color: #ffffff;
    cursor: pointer;
  }

  .game-over-card button:active {
    transform: scale(0.98);
  }

  /* ==========================================================================
     9. Mobile Layout Configurations
     ========================================================================== */
  @media (max-width: 768px) {
    body {
      overflow: auto;
    }

    main {
      flex-direction: column;
      padding: 0.75rem;
      gap: 0.75rem;
      height: auto;
      overflow: visible;
    }

    section {
      aspect-ratio: 1 / 1;
      width: 100%;
      height: auto;
      padding: 0.5rem;
    }

    #controls {
      width: 100%;
      min-width: unset;
      max-width: 100%;
      height: auto;
      gap: 1rem;
      padding: 1rem;
    }

    .settings-group {
      margin-top: 0;
      gap: 1rem;
    }

    /* Bigger slider thumbs for fingers */
    #controls input[type="range"]::-webkit-slider-thumb {
      width: 26px;
      height: 26px;
    }

    #controls input[type="range"]::-moz-range-thumb {
      width: 26px;
      height: 26px;
    }

    #controls input[type="button"] {
      padding: 0.9rem 1rem;
      font-size: 1.05rem;
    }

    .toggle-select {
      padding: 8px 10px;
      font-size: 15px;
    }
  }
