:root {
  --bg: #f4f1e8;
  --ink: #22262b;
  --paper: #fffdf7;
  --accent: #2a6f77;
  --accent-dark: #1d4e54;
  --grid-line: rgba(34, 38, 43, 0.08);
  --shadow: 0 2px 10px rgba(30, 30, 20, 0.12);
  /* Read by the canvas code, which cannot use CSS variables directly. */
  --cell-fill: rgba(255, 255, 255, 0.35);
  --cell-stroke: rgba(60, 70, 90, 0.18);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14181c;
    --ink: #eef1f3;
    --paper: #1c2228;
    --accent: #4fb3ac;
    --accent-dark: #3a8983;
    --grid-line: rgba(255, 255, 255, 0.08);
    --shadow: 0 2px 14px rgba(0, 0, 0, 0.4);
    /* Empty cells have to recede here as they do in the light theme. Left
       at the light values, a 35%-white fill over the dark board made the
       empty grid the brightest thing on the screen. */
    --cell-fill: rgba(255, 255, 255, 0.05);
    --cell-stroke: rgba(255, 255, 255, 0.1);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  touch-action: manipulation;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Hiragino Sans", "Noto Sans JP", sans-serif;
  background-image:
    linear-gradient(var(--grid-line) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid-line) 1px, transparent 1px);
  background-size: 24px 24px;
}

#app {
  max-width: 720px;
  margin: 0 auto;
  padding: 16px 16px 24px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

h1 {
  font-size: 1.3rem;
  margin: 0;
  display: flex;
  flex-direction: column;
  line-height: 1.25;
}

h1 .subtitle {
  font-size: 0.7rem;
  font-weight: 400;
  color: var(--accent);
  letter-spacing: 0.04em;
}

.scores {
  display: flex;
  align-items: center;
  gap: 10px;
}

#scores.hidden { display: none; }

#best-mode-tag {
  text-transform: none;
  letter-spacing: 0;
}

.score-box {
  background: var(--paper);
  border-radius: 10px;
  padding: 6px 12px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 52px;
}

.score-box .label {
  font-size: 0.62rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  opacity: 0.6;
}

.score-box span:last-child {
  font-weight: 700;
  font-size: 1.05rem;
}

.play-area {
  display: flex;
  gap: 14px;
  align-items: flex-start;
  flex-wrap: wrap;
  justify-content: center;
}

.board-wrap {
  background: var(--paper);
  border-radius: 14px;
  padding: 8px;
  box-shadow: var(--shadow);
  flex: 1 1 320px;
  min-width: 260px;
  display: flex;
  justify-content: center;
}

#board-canvas {
  display: block;
  max-width: 100%;
  touch-action: none;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

#side-panel {
  background: var(--paper);
  border-radius: 14px;
  padding: 12px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 150px;
  flex: 0 0 150px;
}

.panel-block h2 {
  margin: 0 0 6px;
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  opacity: 0.6;
}

#next-canvas {
  display: block;
  margin: 0 auto;
}

/* A button so it is tappable and focusable, but it has to opt out of the
   global button styling to sit in the panel like the other blocks. */
#hold-block {
  background: none;
  color: var(--ink);
  font-weight: 400;
  border: 1px dashed rgba(127, 133, 140, 0.45);
  border-radius: 10px;
  padding: 6px 4px;
  width: 100%;
  display: block;
  text-align: center;
}

#hold-block:hover { background: rgba(127, 133, 140, 0.1); }

#hold-canvas {
  display: block;
  margin: 0 auto;
}

.molecule-label {
  text-align: center;
  font-size: 0.78rem;
  font-weight: 600;
  min-height: 1.2em;
}

#mode-toggle {
  display: flex;
  gap: 8px;
  justify-content: center;
}

#home-btn {
  background: var(--paper);
  color: var(--ink);
  box-shadow: var(--shadow);
  padding: 6px 10px;
}

#game-screen.hidden { display: none; }

#home-screen {
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 24px 8px;
}

#home-screen.hidden { display: none; }

#home-hero {
  display: block;
  width: 100%;
  max-width: 440px;
  margin: 0 auto;
  height: auto;
}

#lang-selector {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: center;
}

/* A dropdown rather than a row of pills: at sixteen languages the pills
   wrapped to five rows on a phone and pushed the mode cards off-screen. */
#lang-select {
  font-family: inherit;
  font-size: 0.82rem;
  color: var(--ink);
  background: var(--paper);
  border: 1px solid rgba(127, 133, 140, 0.35);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 7px 10px;
  max-width: 100%;
  cursor: pointer;
}

.home-tagline {
  text-align: center;
  margin: 0;
  opacity: 0.75;
  font-size: 0.95rem;
}

.home-cards {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  justify-content: center;
}

.home-card {
  flex: 1 1 220px;
  max-width: 280px;
  background: var(--paper);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 24px 20px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  color: var(--ink);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.home-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 6px 18px rgba(30, 30, 20, 0.18);
}

.home-card-icon {
  font-size: 2.2rem;
  color: var(--accent);
}

.difficulty-card .home-card-icon {
  font-size: 1.6rem;
}

.icon-rotated-hex {
  display: inline-block;
  transform: rotate(30deg);
}

.home-card h2 {
  margin: 0;
  font-size: 1.1rem;
}

.home-card p {
  margin: 0;
  font-size: 0.85rem;
  opacity: 0.75;
  line-height: 1.4;
}

.home-card p.home-card-best {
  margin-top: 4px;
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--accent);
  opacity: 1;
}

#collection {
  background: var(--paper);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 14px 16px 16px;
}

#collection h2 {
  margin: 0 0 10px;
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  opacity: 0.6;
  text-align: center;
}

#collection-count {
  color: var(--accent);
  font-weight: 700;
}

#collection-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(78px, 1fr));
  gap: 10px 6px;
  justify-items: center;
}

.collection-tile {
  text-align: center;
  max-width: 88px;
}

.collection-name {
  font-size: 0.64rem;
  font-weight: 600;
  line-height: 1.2;
  word-break: break-word;
}

.collection-formula {
  font-size: 0.6rem;
  opacity: 0.6;
}

#difficulty-screen, #orientation-screen {
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 24px 8px;
  align-items: center;
}

#difficulty-screen.hidden, #orientation-screen.hidden { display: none; }

.difficulty-cards {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  justify-content: center;
}

.difficulty-card {
  flex: 1 1 160px;
  max-width: 190px;
  background: var(--paper);
  border-radius: 14px;
  box-shadow: var(--shadow);
  padding: 18px 16px;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 6px;
  color: var(--ink);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.difficulty-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 6px 18px rgba(30, 30, 20, 0.18);
}

.difficulty-card h2 {
  margin: 0;
  font-size: 1rem;
  color: var(--accent);
}

.difficulty-card p {
  margin: 0;
  font-size: 0.78rem;
  opacity: 0.75;
  line-height: 1.35;
}

#controls-help {
  background: var(--paper);
  border-radius: 14px;
  box-shadow: var(--shadow);
  padding: 14px 18px;
  width: 100%;
  max-width: 700px;
}

#controls-help h2 {
  margin: 0 0 10px;
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  opacity: 0.6;
  text-align: center;
}

.controls-help-cols {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 24px;
}

.controls-help-col {
  flex: 1 1 190px;
  min-width: 0;
}

.controls-help-col h3 {
  margin: 0 0 6px;
  font-size: 0.8rem;
  color: var(--accent);
}

.help-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  font-size: 0.78rem;
  padding: 2px 0;
}

.help-label { opacity: 0.75; }

.help-keys {
  font-weight: 600;
  text-align: right;
  white-space: nowrap;
}

.help-note {
  font-size: 0.78rem;
  opacity: 0.75;
  line-height: 1.45;
  padding: 2px 0;
}

.text-back-btn {
  background: none;
  color: var(--ink);
  opacity: 0.6;
  box-shadow: none;
  padding: 6px 10px;
  font-weight: 500;
}

.text-back-btn:hover {
  background: none;
  opacity: 1;
}


.mode-btn {
  background: var(--paper);
  color: var(--ink);
  opacity: 0.55;
  box-shadow: var(--shadow);
}

.mode-btn.active {
  background: var(--accent);
  color: #fff;
  opacity: 1;
}

#place-panel {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

#place-panel.hidden { display: none; }
#fall-panel.hidden { display: none; }
#level-box.hidden { display: none; }
#touch-controls.hidden { display: none; }
#pause-btn.hidden { display: none; }

.tray-slot {
  border: 2px solid transparent;
  border-radius: 10px;
  padding: 4px;
  transition: border-color 0.15s ease, opacity 0.15s ease;
}

.tray-slot.selected {
  border-color: var(--accent);
}

.tray-slot.empty {
  opacity: 0.25;
}

.tray-canvas {
  display: block;
  margin: 0 auto;
  cursor: pointer;
  touch-action: none;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

.tray-rotate-btn {
  display: block;
  margin: 4px auto 0;
  padding: 4px 10px;
  font-size: 0.9rem;
}

button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  padding: 8px 10px;
  font-weight: 600;
  font-size: 0.85rem;
  transition: background 0.15s ease, transform 0.1s ease;
  touch-action: manipulation;
}

button:hover { background: var(--accent-dark); }
button:active { transform: scale(0.95); }

#touch-controls {
  display: flex;
  gap: 8px;
  justify-content: center;
  flex: 1 1 100%;
}

#touch-controls button {
  flex: 1 1 0;
  max-width: 80px;
  font-size: 1.1rem;
  padding: 12px 0;
}

#game-over-overlay, #paused-overlay {
  position: fixed;
  inset: 0;
  background: rgba(10, 12, 14, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}

#game-over-overlay.hidden, #paused-overlay.hidden { display: none; }

.game-over-card {
  background: var(--paper);
  border-radius: 16px;
  padding: 28px 32px;
  text-align: center;
  box-shadow: var(--shadow);
  /* Caps the width so the share buttons wrap into rows instead of
     stretching the card across the screen. */
  max-width: min(360px, calc(100vw - 32px));
}

.game-over-card h2 { margin-top: 0; }

.game-over-card #final-score {
  font-size: 2rem;
  font-weight: 800;
  color: var(--accent);
  margin: 4px 0 12px;
}

#run-summary {
  font-size: 0.78rem;
  opacity: 0.7;
  margin: 0 0 6px;
  max-width: 260px;
  line-height: 1.5;
}

#game-over-best {
  font-size: 0.85rem;
  font-weight: 600;
  margin: 0 0 8px;
}

#new-best {
  color: var(--accent);
  font-weight: 700;
}

#new-best.hidden { display: none; }

#share-native-btn {
  display: block;
  margin: 4px auto 12px;
  min-width: 140px;
}

#share-native-btn.hidden { display: none; }

#share-row {
  display: flex;
  gap: 8px;
  justify-content: center;
  margin-bottom: 8px;
  flex-wrap: wrap;
}

.share-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 6px 12px;
  font-size: 0.78rem;
  color: #fff;
  text-decoration: none;
}

.share-x { background: #000; }
.share-instagram { background: linear-gradient(45deg, #f58529, #dd2a7b, #8134af, #515bd4); }
.share-line { background: #06c755; }
.share-whatsapp { background: #25d366; }
.share-telegram { background: #229ed9; }
.share-email { background: #5b6b7a; }
.share-copy { background: var(--accent); }

#share-copied-note {
  color: var(--accent);
  font-weight: 600;
  font-size: 0.85rem;
  margin: 0 0 12px;
}

#share-copied-note.hidden { display: none; }

.game-over-actions {
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
}

.secondary-btn {
  background: transparent;
  color: var(--ink);
  border: 1px solid rgba(127, 133, 140, 0.45);
  opacity: 0.75;
}

.secondary-btn:hover {
  background: transparent;
  opacity: 1;
}

@media (max-width: 480px) {
  #side-panel {
    flex-direction: row;
    flex-wrap: wrap;
    width: 100%;
    flex: 1 1 100%;
    justify-content: space-around;
    align-items: center;
  }
  .panel-block { text-align: center; }

  /* Hold and Next sit side by side below the board rather than stacking,
     which would push the touch controls off the bottom of the screen. */
  #fall-panel {
    flex: 1 1 100%;
    display: flex;
    flex-wrap: wrap;
    align-items: flex-start;
    justify-content: center;
    gap: 8px 14px;
  }

  #hold-block {
    width: auto;
    min-width: 88px;
    align-self: flex-start;
  }

  #fall-panel .molecule-label { font-size: 0.68rem; }
  #fall-panel .panel-block h2 { margin-bottom: 2px; }

  #new-game-btn { flex: 0 0 auto; }

  /* Controls first, reference panel second: the buttons are what you reach
     for constantly, so they should not be pushed below the fold by Hold
     and Next. */
  .board-wrap { order: 0; }
  #touch-controls { order: 1; }
  #side-panel { order: 2; }
}
