/* ===== CSS Variables (Light Mode Default) ===== */
:root {
  --bg: #F9F3EE;
  --tile-bg: #FFFFFF;
  --tile-border: #CCBBAA;
  --tile-orange: #F7931A;
  --tile-blue: #5B8DEF;
  --tile-beige: #C8B8A8;
  --key-bg: #EDE0D4;
  --key-orange: #F7931A;
  --key-blue: #5B8DEF;
  --key-beige: #C8B8A8;
  --btn-hint: #C97A10;
  --btn-giveup: #D94F4F;
  --btn-newround: #555555;
  --btn-share: #5B8DEF;
  --title-bitcoin: #F7931A;
  --title-seedle: #333333;
  --text: #333333;
  --text-on-color: #FFFFFF;
  --tile-active-border: #AA9988;
  --modal-bg: #FFFFFF;
  --banner-bg: #F7931A;
  --banner-text: #FFFFFF;
  --key-text: #333333;
  --key-text-beige: #999999;
}

/* Dark mode variables (shared by auto-detect and explicit toggle) */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1A1A1A;
    --tile-bg: #2C2C2C;
    --tile-border: #555555;
    --tile-orange: #F7931A;
    --tile-blue: #4A74CC;
    --tile-beige: #4A4A4A;
    --key-bg: #3A3A3A;
    --key-orange: #F7931A;
    --key-blue: #4A74CC;
    --key-beige: #2A2A2A;
    --btn-hint: #C97A10;
    --btn-giveup: #B33A3A;
    --btn-newround: #666666;
    --btn-share: #4A74CC;
    --title-bitcoin: #F7931A;
    --title-seedle: #E0E0E0;
    --text: #E0E0E0;
    --text-on-color: #FFFFFF;
    --tile-active-border: #888888;
    --modal-bg: #2C2C2C;
    --banner-bg: #F7931A;
    --banner-text: #FFFFFF;
    --key-text: #E0E0E0;
    --key-text-beige: #666666;
  }
}

/* Explicit dark mode override (user toggled) */
:root[data-theme="dark"] {
  --bg: #1A1A1A;
  --tile-bg: #2C2C2C;
  --tile-border: #555555;
  --tile-orange: #F7931A;
  --tile-blue: #4A74CC;
  --tile-beige: #4A4A4A;
  --key-bg: #3A3A3A;
  --key-orange: #F7931A;
  --key-blue: #4A74CC;
  --key-beige: #2A2A2A;
  --btn-hint: #C97A10;
  --btn-giveup: #B33A3A;
  --btn-newround: #666666;
  --btn-share: #4A74CC;
  --title-bitcoin: #F7931A;
  --title-seedle: #E0E0E0;
  --text: #E0E0E0;
  --text-on-color: #FFFFFF;
  --tile-active-border: #888888;
  --modal-bg: #2C2C2C;
  --banner-bg: #F7931A;
  --banner-text: #FFFFFF;
  --key-text: #E0E0E0;
  --key-text-beige: #666666;
}

/* ===== Reset & Base ===== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow-x: hidden;
}

/* ===== Countdown Overlay ===== */
#countdown-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: rgba(0, 0, 0, 0.85);
  display: flex;
  align-items: center;
  justify-content: center;
}

#countdown-overlay.hidden {
  display: none;
}

#countdown-number {
  font-size: 8rem;
  font-weight: 800;
  color: var(--title-bitcoin);
  text-shadow: 0 0 40px rgba(247, 147, 26, 0.5);
}

/* ===== Game Container ===== */
#game-container {
  max-width: 600px;
  margin: 0 auto;
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100dvh;
}

/* ===== Header ===== */
#game-header {
  display: grid;
  grid-template-columns: 1fr auto auto 1fr;
  grid-template-rows: auto auto;
  align-items: center;
  gap: 4px 8px;
  margin-bottom: 12px;
  padding-bottom: 8px;
}

#game-title {
  grid-column: 1 / -1;
  grid-row: 1;
  text-align: center;
  font-size: 1.6rem;
  font-weight: 800;
  letter-spacing: -0.02em;
}

.title-bitcoin { color: var(--title-bitcoin); }
.title-seedle { color: var(--title-seedle); }

#round-display {
  grid-column: 1;
  grid-row: 2;
  font-size: 0.85rem;
  font-weight: 600;
  opacity: 0.8;
}

.icon-btn {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 2px solid var(--title-bitcoin);
  background: transparent;
  color: var(--title-bitcoin);
  font-size: 1rem;
  font-weight: 800;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  padding: 0;
}

.icon-btn:hover { filter: brightness(1.15); }

#help-btn {
  grid-column: 2;
  grid-row: 2;
}

#theme-toggle {
  grid-column: 3;
  grid-row: 2;
}

/* Theme toggle icon visibility */
.theme-icon { display: none; }

/* Light mode: show moon (click to go dark) */
:root:not([data-theme="dark"]) #icon-moon { display: block; }
:root[data-theme="dark"] #icon-sun { display: block; }

/* When OS is dark and no explicit theme, show sun */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]):not([data-theme="dark"]) #icon-moon { display: none; }
  :root:not([data-theme="light"]):not([data-theme="dark"]) #icon-sun { display: block; }
}

#timer-display {
  grid-column: 4;
  grid-row: 2;
  text-align: right;
  font-size: 0.85rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  opacity: 0.8;
}

/* ===== Message Banner ===== */
#message-banner {
  background: var(--banner-bg);
  color: var(--banner-text);
  text-align: center;
  padding: 10px 16px;
  border-radius: 8px;
  font-weight: 600;
  font-size: 0.95rem;
  margin-bottom: 12px;
}

/* ===== Grid ===== */
#grid-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  margin-bottom: 16px;
}

.grid-row {
  display: flex;
  gap: 6px;
}

.tile {
  width: 48px;
  height: 48px;
  border: 2px solid var(--tile-border);
  border-radius: 8px;
  background: var(--tile-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.4rem;
  font-weight: 700;
  text-transform: uppercase;
  color: var(--text);
  transition: border-color 0.15s;
}

.tile.active {
  border-color: var(--tile-active-border);
  border-width: 2.5px;
}

.tile.locked {
  opacity: 0.95;
}

.tile.orange {
  background: var(--tile-orange);
  border-color: var(--tile-orange);
  color: var(--text-on-color);
}

.tile.blue {
  background: var(--tile-blue);
  border-color: var(--tile-blue);
  color: var(--text-on-color);
}

.tile.beige {
  background: var(--tile-beige);
  border-color: var(--tile-beige);
  color: var(--text-on-color);
}

/* ===== Action Buttons ===== */
#action-buttons {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

.action-btn {
  border: none;
  border-radius: 999px;
  padding: 10px 20px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  color: var(--text-on-color);
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: opacity 0.15s, filter 0.15s;
}

.action-btn:hover { filter: brightness(1.1); }
.action-btn:active { filter: brightness(0.95); }

.action-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  filter: none;
}

.hint-btn { background: var(--btn-hint); }
.giveup-btn { background: var(--btn-giveup); }
.newround-btn { background: var(--btn-newround); }
.share-btn { background: var(--btn-share); }

/* ===== Keyboard ===== */
#keyboard {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  margin-top: auto;
  padding-bottom: 8px;
}

.kb-row {
  display: flex;
  gap: 5px;
  justify-content: center;
}

.kb-key {
  min-width: 32px;
  height: 44px;
  border: none;
  border-radius: 6px;
  background: var(--key-bg);
  color: var(--key-text);
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  text-transform: uppercase;
  transition: background 0.15s, color 0.15s;
  padding: 0 6px;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

.kb-key:active { filter: brightness(0.9); }

.kb-key.wide {
  min-width: 52px;
  font-size: 1.2rem;
}

.kb-key.orange {
  background: var(--key-orange);
  color: var(--text-on-color);
}

.kb-key.blue {
  background: var(--key-blue);
  color: var(--text-on-color);
}

.kb-key.beige {
  background: var(--key-beige);
  color: var(--key-text-beige);
}

/* ===== Help Modal ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 900;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.modal-content {
  background: var(--modal-bg);
  color: var(--text);
  border-radius: 16px;
  padding: 28px 24px;
  max-width: 500px;
  width: 100%;
  max-height: 85vh;
  overflow-y: auto;
  position: relative;
}

.modal-content h2 {
  margin-bottom: 12px;
  font-size: 1.3rem;
}

.modal-content h3 {
  margin-top: 18px;
  margin-bottom: 8px;
  font-size: 1rem;
}

.modal-content p, .modal-content li {
  font-size: 0.9rem;
  line-height: 1.5;
  margin-bottom: 6px;
}

.modal-content ul {
  padding-left: 20px;
}

.modal-content a {
  color: var(--title-bitcoin);
}

.modal-close {
  position: absolute;
  top: 12px;
  right: 16px;
  background: none;
  border: none;
  font-size: 1.6rem;
  cursor: pointer;
  color: var(--text);
  opacity: 0.6;
  line-height: 1;
}

.modal-close:hover { opacity: 1; }

.help-colors {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin: 14px 0;
}

.help-color-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.help-tile {
  width: 40px;
  height: 40px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 1.1rem;
  color: var(--text-on-color);
  flex-shrink: 0;
}

.help-orange { background: var(--tile-orange); }
.help-blue { background: var(--tile-blue); }
.help-beige { background: var(--tile-beige); }

/* ===== Responsive ===== */
@media (max-width: 767px) {
  #game-container {
    padding: 8px 8px;
  }

  #game-title {
    font-size: 1.3rem;
  }

  .tile {
    width: 40px;
    height: 40px;
    font-size: 1.15rem;
    border-radius: 6px;
  }

  .grid-row {
    gap: 4px;
  }

  #grid-container {
    gap: 4px;
  }

  .kb-key {
    min-width: 28px;
    height: 40px;
    font-size: 0.85rem;
    border-radius: 5px;
    padding: 0 4px;
  }

  .kb-key.wide {
    min-width: 44px;
    font-size: 1.1rem;
  }

  .kb-row {
    gap: 4px;
  }

  .action-btn {
    padding: 8px 14px;
    font-size: 0.82rem;
  }

  #keyboard {
    gap: 4px;
  }
}

@media (max-width: 360px) {
  .tile {
    width: 34px;
    height: 34px;
    font-size: 1rem;
  }

  .kb-key {
    min-width: 24px;
    height: 36px;
    font-size: 0.78rem;
  }

  .kb-key.wide {
    min-width: 38px;
  }
}
