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

html, body {
  height: 100%;
}

body {
  font-family: sans-serif;
  height: 100vh;
  display: flex;
  flex-direction: column;
  padding: 10px 14px;
  background: #f5f5f5;
  overflow: hidden;
}

h1 {
  text-align: center;
  font-size: 1.4rem;
  margin-bottom: 8px;
  flex-shrink: 0;
}

#controls {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
  justify-content: center;
  flex-shrink: 0;
}

#timer-input {
  width: 60px;
  padding: 4px 6px;
  font-size: 1rem;
}

#start-btn {
  padding: 6px 16px;
  font-size: 1rem;
  cursor: pointer;
  min-width: 70px;
}

#countdown {
  font-size: 1.2rem;
  font-weight: bold;
  min-width: 60px;
}

#mode-options-group {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: auto;
  width: 100%;
}

#question-mode {
  width: 100%;
  padding: 6px 4px;
  font-size: 0.8rem;
  cursor: pointer;
  border: 1px solid #ccc;
  border-radius: 4px;
  background: white;
}

#options-wrapper {
  position: relative;
}

#options-btn {
  padding: 6px 16px;
  font-size: 1rem;
  cursor: pointer;
  min-width: 70px;
  width: 100%;
}

#options-popup {
  display: none;
  position: absolute;
  bottom: calc(100% + 6px);
  right: 0;
  background: white;
  border: 1px solid #ccc;
  border-radius: 8px;
  padding: 12px 16px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.15);
  z-index: 100;
  white-space: nowrap;
}

#options-popup.visible {
  display: block;
}

.options-row {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 8px;
}

.options-row:last-child {
  margin-bottom: 0;
}

#countdown.warning {
  color: red;
  animation: pulse 1s infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

#play-area {
  display: flex;
  gap: 16px;
  justify-content: center;
  align-items: flex-start;
}

#game-area {
  display: flex;
  flex-direction: column;
  position: relative;
  width: 70vmin;
  height: 70vmin;
}

/* End-of-game message */
#end-message {
  display: none;
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  background: #fff8e1;
  border: 2px solid #f9a825;
  border-radius: 8px;
  padding: 10px 18px;
  font-size: 1rem;
  white-space: nowrap;
  box-shadow: 0 2px 8px rgba(0,0,0,0.12);
  z-index: 10;
  align-items: center;
  gap: 12px;
}

#end-message.visible {
  display: flex;
}

#show-all-btn {
  padding: 4px 12px;
  font-size: 0.95rem;
  cursor: pointer;
  flex-shrink: 0;
}

/* Instructions — shown on first load */
#instructions {
  display: none;
  width: 80%;
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  background: #e3f2fd;
  border: 2px solid #90caf9;
  border-radius: 8px;
  padding: 10px 18px;
  font-size: 1rem;
  line-height: 1.7;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  z-index: 10;
}

#instructions ol {
  margin-left: 20px;
}

#instructions.visible {
  display: block;
  cursor: pointer;
}

kbd {
  background: #eee;
  border: 1px solid #bbb;
  border-radius: 3px;
  padding: 1px 5px;
  font-size: 0.9em;
}

/* Question box — floats above grid */
#question-box {
  display: none;
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  background: white;
  border: 2px solid #333;
  border-radius: 8px;
  padding: 10px 16px;
  white-space: nowrap;
  gap: 8px;
  align-items: center;
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
  z-index: 10;
}

#question-box.visible {
  display: flex;
}

#question-text {
  font-size: 1.3rem;
  font-weight: bold;
}

#answer-input {
  width: 70px;
  padding: 4px 6px;
  font-size: 1.1rem;
  text-align: center;
}

#submit-btn,
#continue-btn {
  padding: 4px 12px;
  font-size: 1rem;
  cursor: pointer;
}

#skip-btn {
  display: none;
  position: absolute;
  left: calc(100% + 8px);
  top: 50%;
  transform: translateY(-50%);
  padding: 4px 12px;
  font-size: 1rem;
  cursor: pointer;
  background: #fff3e0;
  border: 1px solid #fb8c00;
  color: #e65100;
  border-radius: 4px;
  white-space: nowrap;
}

#skip-btn.visible {
  display: block;
}

#question-box.answer-mode #skip-btn {
  display: none;
}

#hint-btn {
  font-size: 0.85rem;
  color: #888;
  text-decoration: none;
  white-space: nowrap;
}

#hint-btn:hover {
  color: #444;
}

.key-hint {
  font-size: 0.75em;
  opacity: 0.7;
}

#continue-btn {
  display: none;
}

#question-box.answer-mode #answer-input,
#question-box.answer-mode #submit-btn,
#question-box.answer-mode #hint-btn {
  display: none;
}

#question-box.answer-mode #continue-btn {
  display: inline-block;
}

#question-box.wrong-answer {
  background: #ffcdd2;
  border-color: #b71c1c;
}

.wrong-val {
  color: #b71c1c;
}

/* Grid fills game-area */
#grid {
  width: 100%;
  height: calc(100% - 58px);
  table-layout: fixed;
  border-collapse: collapse;
  background: white;
  box-shadow: 0 1px 4px rgba(0,0,0,0.1);
  margin-top: 70px; /* space for floating question-box */
}

#grid th,
#grid td {
  text-align: center;
  font-size: 1.5rem;
  border: 1px solid #ddd;
  overflow: hidden;
}

#grid thead th,
#grid tfoot th {
  background: #eeeeee;
  font-weight: 600;
  color: #444;
}

#grid tbody th {
  background: #eeeeee;
  font-weight: 600;
  color: #444;
}

#grid td {
  background: white;
  color: #333;
}

/* Highlight classes — scoped to #grid to beat #grid td specificity */
#grid td.highlight-row,
#grid td.highlight-col,
#grid td.highlight-both {
  background: #eeeeff;
}

/* Browse-highlight classes — used when game is idle/ended */
#grid td.browse-row,
#grid td.browse-col {
  background: #eeeeff;
}

#grid td.browse-both {
  background: #c8e6c9;
  color: #1b5e20;
  font-weight: 900;
}

#grid td.browse-diag {
  background: #eeeeff;
}

th.browse-corner {
  background: #d0d0ff;
  cursor: pointer;
}

/* Active labels (game) and browse labels (idle) */
th.active-label,
th.browse-label {
  font-weight: 900;
  font-size: 1.7rem;
  color: #000;
}

/* Clickable cursor on cells/labels when browsing */
#grid td,
#grid th[data-row],
#grid th[data-col],
#grid th[data-diagonal] {
  cursor: pointer;
}

/* Hint cells */
td.hint-neighbor {
  background: #c8e6c9 !important;
  font-weight: 900;
}

td.hint-answer {
  background: #fff9c4 !important;
  font-weight: 900;
}

/* Revealed answer cell */
td.answer-cell {
  font-weight: 900;
  font-size: 1.65rem;
  background: #c8e6c9 !important;
  color: #1b5e20;
}

/* Wrong answer cell (permanent, shown at end of game) */
td.wrong-cell {
  font-weight: 900;
  font-size: 1.65rem;
  background: #ffcdd2 !important;
  color: #b71c1c;
}

/* Quotient label highlighting after division answer */
th.answer-label {
  background: #c8e6c9 !important;
  color: #1b5e20;
  font-weight: 900;
  font-size: 1.7rem;
}

th.wrong-label {
  background: #ffcdd2 !important;
  color: #b71c1c;
  font-weight: 900;
  font-size: 1.7rem;
}

/* Scoreboard */
#scoreboard {
  display: flex;
  flex-direction: column;
  gap: 12px;
  flex-shrink: 0;
  width: 110px;
  padding-top: 66px; /* align with grid, below question-box space */
  justify-content: flex-start;
}

#score-right,
#score-wrong {
  font-size: 1.2rem;
  font-weight: bold;
  padding: 8px 14px;
  border-radius: 6px;
  background: white;
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
  white-space: nowrap;
}

#score-right {
  color: #2e7d32;
}

#score-wrong {
  color: #c62828;
}

@media (max-width: 540px) {
  body {
    overflow-y: auto;
    height: auto;
    min-height: 100vh;
  }

  /* Stack game-area above scoreboard */
  #play-area {
    flex-direction: column;
    align-items: center;
    gap: 10px;
  }

  /* Game area fills available screen width */
  #game-area {
    width: calc(100vw - 28px);
    height: calc(100vw - 28px);
  }

  /* Scoreboard becomes a horizontal row below the grid */
  #scoreboard {
    width: 100%;
    flex-direction: row;
    justify-content: center;
    align-items: center;
    padding-top: 0;
    margin-top: 10px;
    gap: 16px;
  }

  #mode-options-group {
    flex-direction: column;
    margin-top: 0;
    margin-left: auto;
    width: auto;
    gap: 8px;
  }

  #question-mode {
    width: auto;
    font-size: 0.85rem;
  }

  #options-btn {
    width: auto;
  }

  /* End message: allow wrapping */
  #end-message {
    white-space: normal;
    width: 90%;
  }

  /* Question box: allow wrapping, no overflow */
  #question-box {
    white-space: normal;
    flex-wrap: wrap;
    justify-content: center;
    width: calc(100vw - 28px);
    left: 0;
    transform: none;
  }

  /* Minimum 44px touch targets */
  #start-btn,
  #submit-btn,
  #continue-btn,
  #options-btn {
    min-height: 44px;
    padding: 10px 16px;
  }

  /* Skip flows inline after Hint on mobile */
  #hint-btn {
    order: 9;
  }

  #skip-btn {
    position: static;
    transform: none;
    order: 10;
    font-size: 0.8rem;
    padding: 3px 10px;
    min-height: unset;
  }

  #skip-btn.visible {
    display: inline-block;
  }

  #timer-input,
  #answer-input {
    min-height: 44px;
    padding: 10px 6px;
  }

  /* Grid font: smaller to fit 14 columns in ~362px */
  #grid th,
  #grid td {
    font-size: 0.7rem;
  }

  th.active-label,
  th.browse-label {
    font-size: 0.85rem;
  }

  td.answer-cell,
  td.wrong-cell {
    font-size: 0.8rem;
  }

  th.answer-label,
  th.wrong-label {
    font-size: 0.85rem;
  }
}

/* Watermark icon */
#watermark {
  position: fixed;
  bottom: 0;
  right: 0;
  width: 80px;
  height: 80px;
  opacity: 0.25;
  cursor: pointer;
}

#watermark-label {
  display: none;
  position: fixed;
  bottom: 84px;
  right: 8px;
  background: rgba(0,0,0,0.7);
  color: white;
  font-size: 0.8rem;
  padding: 4px 8px;
  border-radius: 4px;
  white-space: nowrap;
  pointer-events: none;
}

#watermark-label.visible {
  display: block;
}
