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

body {
  background: #FFE0B2;
  font-family: 'Segoe UI', Arial, sans-serif;
  height: 100vh;
  height: 100dvh;           /* mobiilin osoitepalkin huomioiva korkeus */
  display: flex;
  align-items: stretch;
  overflow: hidden;
}

#game-container {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100vh;
  height: 100dvh;
}

/* ── Top bar ── */

#top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: #E65100;
  color: #fff;
  padding: 0.4rem 1rem;
  flex-shrink: 0;
  gap: 1rem;
}

.top-left, .top-center, .top-right {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.stat-label {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  opacity: 0.8;
}

.stat-value {
  font-size: 1.2rem;
  font-weight: 700;
  min-width: 2.5rem;
  text-align: center;
}

#seed-input {
  width: 6rem;
  padding: 0.2rem 0.4rem;
  border: none;
  border-radius: 4px;
  font-size: 0.9rem;
  text-align: center;
  background: rgba(255,255,255,0.2);
  color: #fff;
}

#seed-input::placeholder {
  color: rgba(255,255,255,0.5);
}

#new-field-btn {
  padding: 0.25rem 0.75rem;
  border: 2px solid rgba(255,255,255,0.6);
  border-radius: 6px;
  background: transparent;
  color: #fff;
  font-size: 0.85rem;
  cursor: pointer;
  transition: background 0.15s;
}

#new-field-btn:hover {
  background: rgba(255,255,255,0.15);
}

/* ── Settings ── */

#settings-wrap {
  position: relative;
}

#settings-btn {
  padding: 0.25rem 0.5rem;
  border: 2px solid rgba(255,255,255,0.6);
  border-radius: 6px;
  background: transparent;
  color: #fff;
  font-size: 0.9rem;
  cursor: pointer;
  transition: background 0.15s;
}

#settings-btn:hover { background: rgba(255,255,255,0.15); }

#settings-panel {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  background: #BF360C;
  border: 2px solid rgba(255,255,255,0.35);
  border-radius: 8px;
  padding: 0.6rem 0.8rem;
  z-index: 50;
  min-width: 180px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.35);
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

#settings-panel.hidden { display: none; }

.settings-row {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.settings-label {
  font-size: 0.78rem;
  color: rgba(255,255,255,0.85);
  flex: 1;
  white-space: nowrap;
}

.settings-adj {
  background: transparent;
  border: 1px solid rgba(255,255,255,0.45);
  border-radius: 4px;
  color: #fff;
  width: 1.5rem;
  height: 1.5rem;
  font-size: 1rem;
  line-height: 1;
  padding: 0;
  cursor: pointer;
  transition: background 0.1s;
  touch-action: manipulation;
}

.settings-adj:hover    { background: rgba(255,255,255,0.15); }
.settings-adj:disabled { opacity: 0.35; cursor: not-allowed; }

#trash-count-val,
.settings-type-val {
  font-size: 1rem;
  font-weight: 700;
  color: #FFD600;
  min-width: 1.4rem;
  text-align: center;
}

.settings-hint {
  font-size: 0.68rem;
  color: rgba(255,255,255,0.5);
  text-align: right;
  margin: 0;
}

/* ── Canvas ── */

#game-canvas {
  display: block;
  flex: 1 1 auto;
  min-height: 0;
  background: #FFE0B2;
  cursor: default;
}

/* ── Control panel ── */

#control-panel {
  flex-shrink: 0;
  background: #BF360C;
  padding: 0.5rem 0.75rem;
  padding-bottom: max(0.75rem, env(safe-area-inset-bottom)); /* iOS home indicator */
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

/* Command queue row */

#queue-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-height: 2.2rem;
  background: rgba(0,0,0,0.2);
  border-radius: 6px;
  padding: 0.2rem 0.5rem;
}

#command-list {
  display: flex;
  flex-wrap: nowrap;
  gap: 4px;
  flex: 1;
  overflow-x: auto;
  overflow-y: hidden;
  min-height: 1.8rem;
  align-items: center;
}

#command-list .cmd-token.active,
#command-list .cmd-loop-group.active {
  outline: 2px solid #000;
  outline-offset: 1px;
}

#command-list .cmd-token {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.8rem;
  height: 1.8rem;
  background: #FF8A65;
  border-radius: 4px;
  color: #fff;
  font-size: 0.85rem;
  flex-shrink: 0;
}

#command-count {
  font-size: 0.8rem;
  color: rgba(255,255,255,0.7);
  white-space: nowrap;
  flex-shrink: 0;
}

/* Button row */

#button-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

#cmd-buttons {
  display: flex;
  gap: 0.5rem;
}

.cmd-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 0.4rem 0.8rem;
  background: #FF7043;
  border: none;
  border-radius: 8px;
  color: #fff;
  font-size: 0.75rem;
  cursor: pointer;
  transition: background 0.1s, transform 0.1s;
  user-select: none;
}

.cmd-btn i {
  font-size: 1.3rem;
}

.cmd-btn:hover {
  background: #FF8A65;
}

.cmd-btn:active {
  transform: scale(0.93);
}

.cmd-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

#action-buttons {
  display: flex;
  gap: 0.5rem;
}

#action-buttons button {
  padding: 0.4rem 0.9rem;
  border: 2px solid rgba(255,255,255,0.4);
  border-radius: 8px;
  background: transparent;
  color: #fff;
  font-size: 0.85rem;
  cursor: pointer;
  transition: background 0.1s;
}

#action-buttons button:hover {
  background: rgba(255,255,255,0.1);
}

#run-btn {
  background: #2E7D32 !important;
  border-color: #4CAF50 !important;
  font-weight: 700;
  font-size: 1rem !important;
  padding: 0.4rem 1.4rem !important;
}

#run-btn:hover {
  background: #388E3C !important;
}

#run-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* ── Game-over overlay ── */

#game-over {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

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

#game-over-box {
  background: #FFF8E1;
  border-radius: 16px;
  padding: 2.5rem 3rem;
  text-align: center;
  box-shadow: 0 8px 32px rgba(0,0,0,0.4);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.75rem;
}

#game-over-trophy { font-size: 4rem; line-height: 1; }

#game-over-box h2 {
  font-size: 2rem;
  color: #E65100;
  margin: 0;
}

#game-over-box p {
  font-size: 1.2rem;
  color: #4E342E;
  margin: 0;
}

#game-over-box strong { color: #E65100; }

#replay-btn {
  margin-top: 0.5rem;
  padding: 0.6rem 2rem;
  background: #2E7D32;
  color: #fff;
  border: none;
  border-radius: 10px;
  font-size: 1.1rem;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.15s;
  touch-action: manipulation;
}

#replay-btn:hover { background: #388E3C; }

/* ── Loop controls ── */

#loop-controls {
  display: flex;
  align-items: center;
  gap: 2px;
}

.loop-adj {
  background: transparent;
  border: 1px solid rgba(255,255,255,0.4);
  border-radius: 4px;
  color: #fff;
  width: 1.4rem;
  height: 1.4rem;
  font-size: 1rem;
  line-height: 1;
  padding: 0;
  cursor: pointer;
  transition: background 0.1s;
  touch-action: manipulation;
}

.loop-adj:hover  { background: rgba(255,255,255,0.15); }
.loop-adj:disabled { opacity: 0.35; cursor: not-allowed; }

#loop-count-display {
  font-size: 0.82rem;
  font-weight: 700;
  color: #FFD600;
  width: 1.8rem;
  text-align: center;
  flex-shrink: 0;
}

/* Loop button — styled like cmd-btn but with distinct purple colour */
.cmd-btn-loop {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 0.4rem 0.7rem;
  background: #6A1B9A;
  border: none;
  border-radius: 8px;
  color: #fff;
  font-size: 0.75rem;
  cursor: pointer;
  transition: background 0.1s, transform 0.1s;
  user-select: none;
  touch-action: manipulation;
}

.cmd-btn-loop i { font-size: 1.3rem; }
.cmd-btn-loop:hover   { background: #7B1FA2; }
.cmd-btn-loop:active  { transform: scale(0.93); }
.cmd-btn-loop:disabled { opacity: 0.4; cursor: not-allowed; }

/* Purple pulsing border while recording */
.cmd-btn-loop.loop-recording {
  background: #7B1FA2;
  box-shadow: 0 0 0 2px #CE93D8;
  animation: loop-pulse 0.9s ease-in-out infinite alternate;
}

@keyframes loop-pulse {
  from { box-shadow: 0 0 0 2px #CE93D8; }
  to   { box-shadow: 0 0 0 4px #9C27B0; }
}

/* Loop group tokens in command list */
.cmd-loop-group {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  background: rgba(106, 27, 154, 0.35);
  border: 1px solid rgba(206, 147, 216, 0.7);
  border-radius: 6px;
  padding: 1px 4px;
  flex-shrink: 0;
}

.cmd-loop-group.cmd-loop-draft {
  border-style: dashed;
  opacity: 0.85;
}

.loop-badge {
  font-size: 0.68rem;
  font-weight: 700;
  color: #E1BEE7;
  padding: 0 1px;
}

.loop-dots {
  font-size: 0.8rem;
  color: rgba(225, 190, 231, 0.75);
  padding: 0 1px;
}

/* ── Touch & accessibility tweaks (Phase 9) ── */

button { touch-action: manipulation; }  /* prevent double-tap zoom on iOS */

@media (max-width: 600px) {
  .cmd-btn span, .cmd-btn-loop span { display: none; }
  .cmd-btn { padding: 0.5rem 0.6rem; }
  .cmd-btn-loop { padding: 0.5rem 0.5rem; }
  #action-buttons button { font-size: 0.75rem; padding: 0.4rem 0.6rem; }
}
